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

$DBName = isset($_SESSION['DBName']) ? $_SESSION['DBName'] : 'b2b_solar_sc';
$objDBConnect	= new DBConnect($DBName);
$objDBQuery		= new DBQuery($objDBConnect);
$classBusiLogic	= new BusiLogic($objDBQuery, $DBName);
$objSolarCustomer	= 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, 'L'); 


$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)
						
$curdate= date('Y-m-d');
$first_day_this_month = date('Y-m-01'); 
$second_day_this_month = date('Y-m-02'); 
$last_day_this_month  = date('Y-m-t');
$uptoday =date('j');
$totdays = cal_days_in_month(CAL_GREGORIAN, date('m'), date('Y'));	
$array_item = array('20000120', '20000121', '20005315', '20001340, 20008095', '20009157', '20001341', '20002672');
$openingstock=array();
$uptodisptch=array();
$closingstock=array();						
$totalopeningstk=$totalclosingingstk=$totaldispatch=$totalproduction=0;

$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;"';

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

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

$Rowfirst = 'Style="font-size: 16px; border-bottom:1px solid grey;border-left:1px solid grey;border-right:1px solid grey;"';
$Row = 'Style="font-size: 16px; border-bottom:1px solid grey;border-right:1px solid grey;"';
$html_str = '';


$html_str .= '<table width="100%" cellspacing="0" cellpadding="0" class="content-border" border="0" style="border-collapse:collapse">
	<tr>
		<td><h3>AED Production Report As On'. date("d-m-Y H:i") .'</h3>
		<div width="50%" class="">
		<br/><br/>
		<table width="45%" cellspacing="1" cellpadding="5" border="1" style="border-collapse:collapse;" class="grid report">
			<tr style="background-color:#94E9FF">
				<th rowspan="2" align="center" width="5%">Particulars</th>
				<th colspan="3" width="5%">SIIL</th>
				<th colspan="4"  width="5%">EEL</th>	
				<th style="background-color:#B9DBCA" rowspan="2" align="center" width="5%">Total</th>
			</tr>
			<tr style="background-color:#94E9FF">
				<th width="5%">SED 1.5</th>
				<th width="5%">SED 1.8</th>
				<th width="5%">SED 3.0</th>
				<th width="5%">EED 1.5</th>	
				<th width="5%">EED 1.5 US</th>	
				<th width="5%">EED 1.8</th>	
				<th width="5%">EED 3.0</th>	
			</tr>
		 	<tr align="right">
				<td style="background-color:#EBF5FF" align="left" height="50"  width="15%"><b>Opening Stock For 1 st Day</b></td>';
			$i=0;
			foreach($array_item as $item){
				$sql = "SELECT B.Dispatch From scm_solar_sc.scm_stock_sales A JOIN scm_solar_sc.scm_stock_sales_item B ON A.StockSalesID = B.StockSalesID 
					 WHERE B.ItemCd IN ($item) and  SectorCd ='T' and DATE(A.DateAdded)='$first_day_this_month'";
					$Opening = $objSolarCustomer->execSelectQuery($sql);
					if($Opening[0]['Dispatch']=='' || $Opening[0]['Dispatch']=='0' ){
						$sql = "SELECT B.Dispatch From scm_solar_sc.scm_stock_sales A JOIN scm_solar_sc.scm_stock_sales_item B ON A.StockSalesID = B.StockSalesID 
						WHERE B.ItemCd IN ($item) and  SectorCd ='T' and DATE(A.DateAdded)='$second_day_this_month'";
						$Opening = $objSolarCustomer->execSelectQuery($sql);
					}
					$openingstk=$Opening[0]['Dispatch'];
					if($item== '20002672'){
						$openingstk=$openingstk*1000;
					}
					else{ $openingstk=$openingstk*1500;  }
					
					$openingstock[$i]=$openingstk;
		$html_str .='<td width="5%">'. sprintf("%.3f", $openingstk) .'</td>';
			$totalopeningstk= $totalopeningstk+$openingstk;
			$i++;
			}
		$html_str .='<td  style="background-color:#B9DBCA" width="5%"><b>'.sprintf("%.3f", $totalopeningstk) .'</b></td>
			</tr>
		 	<tr align="right">
				<td style="background-color:#EBF5FF" align="left" height="50" width="15%"><b>Total Up to Dispatch for this Month</b></td>';
			
			$j=0;
				foreach($array_item as $item){
				$sql = "SELECT sum(A.DespatchedQty) as DespatchedQty From so_expdet_magazine_item A JOIN 
					so_expdet B ON A.IdExpDet= B.IdExpdet JOIN so_expdet_magazine C ON A.IdExpDet= C.IdExpdet and A.MagazineCd=C.MagazineCd WHERE A.ItemCd IN ($item) 
					and StatusCd IN ('C', 'B') and DATE(C.SAPInvDate) between '$first_day_this_month' and '$last_day_this_month'" ;
				$UptoDisp = $objSolarCustomer->execSelectQuery($sql);
				$uptodisptch[$j]=$UptoDisp[0]['DespatchedQty'];
			
		$html_str .='<td width="5%">'. sprintf("%.3f", $UptoDisp[0]["DespatchedQty"]) .'</td>';
			$totaldispatch=$totaldispatch+$UptoDisp[0]['DespatchedQty'];
			$j++;
			}
		$html_str .='<td style="background-color:#B9DBCA" width="5%"><b>'. sprintf("%.3f", $totaldispatch) .'</b></td>			
			</tr>
			<tr align="right">
				<td style="background-color:#EBF5FF" align="left" height="50" width="15%"  ><b>Closing Todays Stock</b></td>';
			$k=0;
			foreach($array_item as $item){
			$sql = "SELECT B.Stock From scm_solar_sc.scm_stock_sales A JOIN scm_solar_sc.scm_stock_sales_item B ON A.StockSalesID = B.StockSalesID 
					 WHERE B.ItemCd IN ($item) and  SectorCd ='T' and DATE(A.DateAdded)='$curdate'";
				$Closing = $objSolarCustomer->execSelectQuery($sql);
				$closingstk=$Closing[0]['Stock'];
					if($item== '20002672'){
						$closingstk=$closingstk*1000;
					}
					else{ $closingstk=$closingstk*1500;  }
					
					$closingstock[$k]=$closingstk;
		$html_str .='<td width="5%">'. sprintf("%.3f", $closingstk) .'</td>';
		$totalclosingingstk=$totalclosingingstk+$closingstk;
			$k++;
			}
			$SEDProd15= $closingstock[0]+$uptodisptch[0]-$openingstock[0];
			$SEDProd18= $closingstock[1]+$uptodisptch[1]-$openingstock[1];
			$SEDProd30= $closingstock[2]+$uptodisptch[2]-$openingstock[2];
			$EEDProd15= $closingstock[3]+$uptodisptch[3]-$openingstock[3];
			$EEDProd15US= $closingstock[4]+$uptodisptch[4]-$openingstock[4];
			$EEDProd18= $closingstock[5]+$uptodisptch[5]-$openingstock[5];
			$EEDProd30= $closingstock[6]+$uptodisptch[6]-$openingstock[6];
			$totalproduction=$SEDProd15+$SEDProd18+$SEDProd30+$EEDProd15+$EEDProd15US+$EEDProd18+$EEDProd30;
			$uptodays=date('d');
		$html_str .='<td style="background-color:#B9DBCA" width="5%"><b>'. sprintf("%.3f", $totalclosingingstk) .'</b></td>
			</tr>
		 	<tr align="right">
				<td align="left" style="background-color:#EBF5FF" height="50" width="15%"><b>Production of This Month</b></td>
				<td width="5%">'. sprintf("%.3f", $SEDProd15).'</td>
				<td width="5%">'. sprintf("%.3f", $SEDProd18).'</td>
				<td width="5%">'. sprintf("%.3f", $SEDProd30).'</td>
				<td width="5%">'. sprintf("%.3f", $EEDProd15).'</td>
				<td width="5%">'. sprintf("%.3f", $EEDProd15US).'</td>
				<td width="5%">'. sprintf("%.3f", $EEDProd18) .'</td>
				<td width="5%">'. sprintf("%.3f", $EEDProd30) .'</td>
				<td style="background-color:#B9DBCA" width="5%"><b>'. sprintf("%.3f", $totalproduction) .'</b></td>			
			</tr>';
			$html_str .='<tr align="right" style="background-color:#CCE6FF">
				<td align="left" height="50" width="15%"><b>Average Production</b></td>
				<td width="5%"><b>'. sprintf("%.3f", $SEDProd15/$uptodays)   .'</b></td>
				<td width="5%"><b>'. sprintf("%.3f", $SEDProd18/$uptodays)   .'</b></td>
				<td width="5%"><b>'. sprintf("%.3f", $SEDProd30/$uptodays)   .'</b></td>
				<td width="5%"><b>'. sprintf("%.3f", $EEDProd15/$uptodays) .'</b></td>
				<td width="5%"><b>'. sprintf("%.3f", $EEDProd15US/$uptodays) .'</b></td>
				<td width="5%"><b>'. sprintf("%.3f", $EEDProd18/$uptodays) .'</b></td>
				<td width="5%"><b>'. sprintf("%.3f", $EEDProd30/$uptodays) .'</b></td>
				<td style="background-color:#B9DBCA" width="5%"><b>'. sprintf("%.3f", $totalproduction/$uptodays) .'</b></td>			
			</tr>';
			
			
		$html_str .='</table>';	
		$html_str .='</div></td></tr></table>';	
			
		$html = $html_str;
		$mpdf->AddPages('L');
		$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);
		
	$filename = "AEDProductionReport";
	$Date = date('d-m-Y');
	$filename = $filename.$Date;

	if(isset($_GET['subAction']) && $_GET['subAction'] == 'SendReport') {
	$content = $mpdf->Output("", "S");
	$html_message = '<p>Dear Sir,<br><br>Please find the attached Daily Dealer Phone Report file.<br><br><br>Thanks & Regards!!<br>B2B Solar Explosives';
	$mailer = new Swift_Mailer(new Swift_MailTransport()); // Create new instance of SwiftMailer  
	$message = Swift_Message::newInstance()  
			->setSubject('Highlighted Phone Report of '.$Date) // Message subject  
			//->setTo(array('rahul@solargroup.com' => 'Rahul Sir', 'r.somani@solargroup.com' => 'R.Somaniji', 'm.bhalerao@solargroup.com' => 'M.Bhaleraoji', 'tsm2@solargroup.com' => 'S.Bangarji', 'k.bhadada@solargroup.com' => 'K.Bhadadaji', 'tsm3@solargroup.com' => 'A.Mandowara', 'kcn@solargroup.com' => 'kcn@solargroup.com', 'websupport@solargroup.com' => 'Web Support')) // Array of people to send to 
			->setTo(array('websupport@solargroup.com' => 'Atul Gupta', 'm.bhalerao@solargroup.com' => 'M.Bhaleraoji')) // 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.close(); </script>";
	}else  {
		echo "<script type='text/javascript'> alert('Mail Not Sent Please Check the Connection!!!!'); window.close(); </script>";
	}
	
} else{
	$mpdf->Output($filename,'I');
	exit;
}



//============================================================+
// END OF FILE
//============================================================+
?>
