<?php
require_once("../special/spl.class.Special.php");
require_once('swift/swift_required.php'); 
require_once('../classes/b2b.class.dbConnect.php');
require_once('../classes/b2b.class.dbQuery.php');
require_once('../classes/b2b.class.BusiLogic.php');

$DBName = isset($_SESSION['DBName']) ? $_SESSION['DBName'] : 'b2b_solar_sc';
$objDBConnect	= new DBConnect($DBName);
$objDBQuery		= new DBQuery($objDBConnect);
$classBusiLogic	= new BusiLogic($objDBQuery, $DBName);
$classSpecial	= new SpecialClass($objDBQuery, $DBName, $classBusiLogic);
// create new PDF document
include("PHPPDF/mpdf.php");
$mpdf=new mPDF('','A4',10,'',5,3,10,10,9,9, 'P'); 


$mpdf->useAdobeCJK = true;		// Default setting in config.php
						// You can set this to false if you have defined other CJK fonts

$mpdf->SetAutoFont(AUTOFONT_ALL);	//	AUTOFONT_CJK | AUTOFONT_THAIVIET | AUTOFONT_RTL | AUTOFONT_INDIC	// AUTOFONT_ALL
						// () = default ALL, 0 turns OFF (default initially)

$CustomerCd = $_GET['CustomerCd'];	
$Year = $_GET['Year'];	
$Month = $_GET['Month'];	
$Date = $_GET['Date'];	
	
$listData = $classBusiLogic->getDealerSubdealerAEDAllocation($CustomerCd, $Year, $Month);


$HeadingRowStyleFirst = '';
$HeadingRowStyleOther = '';
$RowStyleFirst = '';
$RowStyleOther = '';

$HeadingRowStyleFirst = 'Style="font-size: 12px; font-weight: bold;border-bottom:1px solid grey;border-left:1px solid grey;border-right:1px solid grey;"';
$HeadingRowStyleOther = 'Style="font-size: 12px; font-weight: bold;border-bottom:1px solid grey;border-right:1px solid grey;"';

$RowStyleFirst = 'Style="font-size: 10px; font-weight: bold;border-bottom:1px solid grey;border-left:1px solid grey;border-right:1px solid grey;"';
$RowStyleOther = 'Style="font-size: 10px; font-weight: bold;border-bottom:1px solid grey;border-right:1px solid grey;"';


$html_str .='<table border="0" width="700px" cellspacing="0" cellpadding="0" align="center">
				  <tr>
					<td class="pageHeading" align="center" style="background-color:#B1E0E0;width:100%;font-size:18px;font:weight:bold;">Total AED Dispatch Planning </td>
				  </tr>
				  <tr>
					<td valign="top" width="70%">
						<table style="border:1px solid black;" width="700px" cellspacing="0" cellpadding="0">
							<tr style="background-color: #AFAFAF;">
								<td '.$HeadingRowStyleFirst.' align="center" rowspan="2" width="40px">S No.</td>
								<td '.$HeadingRowStyleFirst.' align="center" rowspan="2" width="300px">Dealer Name</td>
								<td '.$HeadingRowStyleOther.' align="center" colspan="2" width="200px">Supreme AED</td>
								<td '.$HeadingRowStyleOther.' align="center" colspan="2" width="200px">Solar AED</td>
								</tr>
							<tr style="background-color: #AFAFAF;">
								<td '.$HeadingRowStyleOther.' align="center" width="100px">Qty</td>
								<td '.$HeadingRowStyleOther.' align="center" width="100px">Date</td>
								<td '.$HeadingRowStyleOther.' align="center" width="100px">Qty</td>
								<td '.$HeadingRowStyleOther.' align="center" width="100px">Date</td>
							</tr>';
							$rowcnt = 0;
							$Dsrno = 1;
								$CustomerCd 				=	$listData[0]['CustomerCd'];	
								$Dealer_Name	 			=	$listData[0]['Dealer_Name'];	
								$Customer_SupremeAED_Date	= 	$classBusiLogic->convertDate($listData[0]['Customer_SupremeAED_Date'], False, 1);	
								$Customer_SupremeAED_Qty 	= 	$listData[0]['Customer_SupremeAED_Qty'];	
								$Customer_SolarAED_Date 	=  	$classBusiLogic->convertDate($listData[0]['Customer_SolarAED_Date'], False, 1);
								$Customer_SolarAED_Qty	 	= 	$listData[0]['Customer_SolarAED_Qty'];	
								
							$html_str .= '<tr>
									<td '.$RowStyleFirst.' width="40px">'.$Dsrno.'</td>
									<td '.$RowStyleOther.' width="300px">'.strtoupper($Dealer_Name).'</td>
									<td '.$RowStyleOther.' width="100px" align="center">'.$Customer_SupremeAED_Qty.'</td>
									<td '.$RowStyleOther.' '.$bgcolor.' width="100px" align="center">'.$Customer_SupremeAED_Date.'</td>
									<td '.$RowStyleOther.' width="100px" align="center">'.$Customer_SolarAED_Qty.'</td>
									<td '.$RowStyleOther.' width="100px">'.$Customer_SolarAED_Date.'</td>
									</tr>';
								$Dsrno++;
							
						$html_str .= '</table>
					</td>
				</tr>
			</table><br/>';	

$html_str .='<table border="0" width="700px" cellspacing="0" cellpadding="0" align="center">
				  <tr>
					<td class="pageHeading" align="center" style="background-color:#B1E0E0;width:100%;font-size:18px;font:weight:bold;">Subdealers AED Planning</td>
				  </tr>
				  <tr>
					<td valign="top" width="70%">
						<table style="border:1px solid black;" width="700px" cellspacing="0" cellpadding="0">
							<tr style="background-color: #AFAFAF;">
								<td '.$HeadingRowStyleFirst.' align="center" rowspan="2" width="40px">S No.</td>
								<td '.$HeadingRowStyleFirst.' align="center" rowspan="2" width="300px">Subdealer Name</td>
								<td '.$HeadingRowStyleOther.' align="center" colspan="2" width="200px">Supreme AED</td>
								<td '.$HeadingRowStyleOther.' align="center" colspan="2" width="200px">Solar AED</td>
								</tr>
							<tr style="background-color: #AFAFAF;">
								<td '.$HeadingRowStyleOther.' align="center" width="100px">Qty</td>
								<td '.$HeadingRowStyleOther.' align="center" width="100px">Date</td>
								<td '.$HeadingRowStyleOther.' align="center" width="100px">Qty</td>
								<td '.$HeadingRowStyleOther.' align="center" width="100px">Date</td>
							</tr>';
							$rowcnt = 0;
							$srno = 1;
							$total_supremeAED= 0;
							$total_solarAED= 0;
							foreach($listData as $curRow){
								$SubdealerCd 				=	$curRow['SubdealerCd'];	
								$Subdealer_Name 			=	$curRow['subdealer_Name'];	
								$SubdealerSupremeAED_Date	= 	$classBusiLogic->convertDate($curRow['SubdealerSupremeAED_Date'], False, 1);	
								$SubdealerSupremeAED_Qty 	= 	$curRow['SubdealerSupremeAED_Qty'];	
								$SubdealerSolarAED_Date 	=  	$classBusiLogic->convertDate($curRow['SubdealerSolarAED_Date'], False, 1);
								$SubdealerSolarAED_Qty	 	= 	$curRow['SubdealerSolarAED_Qty'];	
								
							$html_str .= '<tr>
									<td '.$RowStyleFirst.' width="40px">'.$srno.'</td>
									<td '.$RowStyleOther.' width="300px">'.strtoupper($Subdealer_Name).'</td>
									<td '.$RowStyleOther.' width="100px" align="center">'.$SubdealerSupremeAED_Qty.'</td>
									<td '.$RowStyleOther.' '.$bgcolor.' width="100px" align="center">'.$SubdealerSupremeAED_Date.'</td>
									<td '.$RowStyleOther.' width="100px" align="center">'.$SubdealerSolarAED_Qty.'</td>
									<td '.$RowStyleOther.' width="100px">'.$SubdealerSolarAED_Date.'</td>
									</tr>';
								$srno++;
								$total_supremeAED	= $total_supremeAED+$SubdealerSupremeAED_Qty;
								$total_solarAED		= $total_solarAED+$SubdealerSolarAED_Qty;
							}
							$html_str .= '<tr style="background-color:#ffccff;">
									<td '.$RowStyleFirst.' width="40px"></td>
									<td '.$RowStyleOther.' align="center" width="300px">Total</td>
									<td '.$RowStyleOther.' width="100px" align="center">'.$total_supremeAED.'</td>
									<td '.$RowStyleOther.' '.$bgcolor.' width="100px" align="center"></td>
									<td '.$RowStyleOther.' width="100px" align="center">'.$total_solarAED.'</td>
									<td '.$RowStyleOther.' width="100px"></td>
									</tr>';
							
						$html_str .= '</table>
					</td>
				</tr>
			</table>';	
		//if(count($listData)==0){	
		//$html_str .= '<table id="tblHeading" width="100%" cellspacing="0" cellpadding="6" border="0"><tr><td><b>No phone record exist!!!!</b></td></tr></table>';
	//	}
		$mpdf->AddPages('P');
		$mpdf->setHTMLFooter('<div align="center"><b>{PAGENO} / {nbpg}</b></div>') ;
		$mpdf->setHTMLFooter('<div align="center"><b><i>{PAGENO} / {nbpg}</i></b></div>','E') ;
		$mpdf->WriteHTML($html_str);
//print_r($listData);
$filename = "Dealer_subdealer_AED_Aloocation";
$Date = date('d-m-Y');
$filename = $filename.$Date;

$EmailIdarr = Explode(';', $listData[0]['CustomerEmailId']);
$EmailId='';
foreach($EmailIdarr as $email){
	$EmailId .="'".$email."',";
}
$EmailId = rtrim($EmailId, ',');
//echo $html_str;
$content = $mpdf->Output("", "S");
$html_message = '<p>Dear Sir,<br><br>Based on the allocation update provided by you to our Subdealer Representative please find attached reference data sheet.
				<br>
				<br><br>Thank You<br>Customer Service Department';
$mailer = new Swift_Mailer(new Swift_MailTransport()); // Create new instance of SwiftMailer  
$message = Swift_Message::newInstance()  
			->setSubject('AED Planning '.$Date) // Message subject  
			->setTo(array( 'websupport@solargroup.com')) // Array of people to send to 
			//->setTo(array($EmailId)) // Array of people to send to 
			->setFrom(array('b2b.solarexplosives@gmail.com' => 'Admin@B2B.SolarExplosives')) // From:  
			->setBody($html_message, 'text/html') // Attach that HTML message from earlier  
			->attach(Swift_Attachment::newInstance($content, $filename, 'application/pdf')); // Attach the generated PDF from earlier  
  
// Send the email, and show user message  
if ($mailer->send($message))  {
	echo "<script type='text/javascript'> alert('Mail Sent Successfully!!!!'); 	window.location.href='../index.php?action=viewDealerSubdealerAEDAllocation' ; </script>";
//	echo "<script type='text/javascript'> alert('Mail Sent Successfully!!!!'); 	 </script>";
}else  {
	echo "<script type='text/javascript'> alert('Mail Not Sent Please Check the Connection!!!!'); window.location.href='../index.php?action=viewDealerSubdealerAEDAllocation' ;  </script>";
}
//============================================================+
// END OF FILE
//============================================================+
?>
