<?php
// Include the main TCPDF library (search for installation path).

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);
$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)

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

$CustomerCd = '';
$IdSubdealer = '';
$CompanyCd = '';
$where_str = '';

if(isset($_GET['action']) && $_GET['action'] == 're11'){
	$filename = "RE11_Report_";
	$count = 0;
	$headerData = $_GET;
	
	$DTS = $headerData['DTS'];
	$DTE = $headerData['DTE'];
	$CompanyCd    = $headerData['CompanyCd'];
	$DTSAnsi = $classBusiLogic->convertDate($DTS, TRUE, 1);
	$DTEAnsi = $classBusiLogic->convertDate($DTE, TRUE, 1);

	if(isset($headerData['CustomerCd']) && $headerData['CustomerCd'] !=''){
		 $CustomerCd = $headerData['CustomerCd'];
	}
	
	$SrNo = 0;
	$count = 1;
	$prevCustomerCd = 0;
	$RE11Details = $classSpecial->reportRE11($DTSAnsi, $DTEAnsi, $CompanyCd, $CustomerCd);
	$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 = '';
	$html_str .='<table width="100%" cellspacing="0" cellpadding="6" border="0">
					<tr bgcolor="#8D8D8D">
						<td>RE11 Report'.(($CustomerCd !='' && count($RE11Details) > 0) ? ' For '.strtoupper($RE11Details[0]['Customer']) : '' ).'</td>
						<td align="right">'.($DTS==$DTE ? ' As On ' . $DTS : ' For ' . $DTS . ' To ' . $DTE).'</td>
					</tr>
				 </table>';
				if(!empty($RE11Details) && count($RE11Details) > 0){					
	$html_str .='<table border="0" width="100%" cellspacing="1" cellpadding="2" class="dynamic" id="vtblBreak">
					<tr style="background-color: #AFAFAF;">
						<th align="center" width="2%" '.$HeadingRowStyleFirst.'>SrNo.</th>';
						if($CustomerCd ==''){
			$html_str .='<th width="25%" '.$HeadingRowStyleOther.'>Dealer Name</th>';
						}
			$html_str .='<th width="%" '.$HeadingRowStyleOther.'>Company</th> 
						<th align="center" width="%" '.$HeadingRowStyleOther.'>RE11 No.</th>
						<th align="center" width="%" '.$HeadingRowStyleOther.'>RE11 Date</th>						
						<th align="center" width="%" '.$HeadingRowStyleOther.'>Ready to Received Date</th>
						<th align="center" width="%" '.$HeadingRowStyleOther.'>Expire Date</th>
						<th align="center" width="%" '.$HeadingRowStyleOther.'>Courier Name</th>
						<th align="center" width="%" '.$HeadingRowStyleOther.'>Docket Id</th>
						<th align="center" width="%" '.$HeadingRowStyleOther.'>Received Date</th>
						<th align="center" width="%" '.$HeadingRowStyleOther.'>Status</th>
						<th align="center" width="%" '.$HeadingRowStyleOther.'>Item</th>
						<th align="center" width="%" '.$HeadingRowStyleOther.'>Quantity</th>
					</tr>';
					
					foreach($RE11Details as $index => $Value) {
						$IsExist = $classSpecial->getRE11Details($RE11Details[$index]['INDETNo'],$RE11Details[$index]['INDETDate']);
						if($IsExist[0]['StatusCd'] != 'A'){
							$Status = (($IsExist[0]['PONo'] !='') ? 'Use In Po: '.$IsExist[0]['PONo'] : '-');
				$html_str .='<tr onmouseout="setRowHover(this);" onmouseover="setRowHover(this);"'. (($SrNo%2==0) ? 'class="even_row"' : 'class="odd_row"' ) .'>';
				if($count == 1 && $prevCustomerCd != $RE11Details[$index]['Id']) {
					$TotalRE11 = $classSpecial->getRE11($RE11Details[$index]['Id']);
					$rowspan = sizeof($TotalRE11);
					$SrNo++;
					$prevCustomerCd = $RE11Details[$index]['Id'];
				$html_str .='<td width="%" rowspan="'.$rowspan.'" '.$RowStyleFirst.'>'. $SrNo.'</td>';
				if($CustomerCd =='') { 
					$html_str .='<td width="25%" rowspan="'.$rowspan.'" '.$RowStyleOther.'>'.$RE11Details[$index]['Customer'].'</td>';
				}
					$html_str .='<td width="%" rowspan="'.$rowspan.'" '.$RowStyleOther.'>'.$RE11Details[$index]['Company'].'</td>
						<td width="%" rowspan="'.$rowspan.'" '.$RowStyleOther.'>'.$RE11Details[$index]['INDETNo'].'</td>
						<td width="%" rowspan="'.$rowspan.'" '.$RowStyleOther.'>'.$classBusiLogic->convertDate($RE11Details[$index]['INDETDate'], FALSE).'</td>
						<td width="%" rowspan="'.$rowspan.'" '.$RowStyleOther.'>'.$classBusiLogic->convertDate($RE11Details[$index]['ReadyDate'], FALSE).'</td>
						<td width="%" rowspan="'.$rowspan.'" '.$RowStyleOther.'>'.$classBusiLogic->convertDate($RE11Details[$index]['ExpireDate'], FALSE).'</td>
						<td width="%" rowspan="'.$rowspan.'" '.$RowStyleOther.'>'.$RE11Details[$index]['CourierName'].'</td>
						<td width="%" rowspan="'.$rowspan.'" '.$RowStyleOther.'>'.$RE11Details[$index]['DocketId'].'</td>
						<td width="%" rowspan="'.$rowspan.'" '.$RowStyleOther.'>'.$classBusiLogic->convertDate($RE11Details[$index]['ReceivedDate'], FALSE).'</td>
						<td width="%" rowspan="'.$rowspan.'" '.$RowStyleOther.'>'.$Status.'</td>';
				}else{
					$count = 1;
				}
				$html_str .='<td width="%" '.$RowStyleOther.'>'.$RE11Details[$index]['Item'].'</td>
						<td width="%" '.$RowStyleOther.'>'.$RE11Details[$index]['Quantity'].'</td>
					</tr>';
					}
				}
			$html_str .= '</table>';
				  }else { 
			$html_str .= '<table cellpadding="0" cellspacing="0" border="0">
							<tr>
								<td style="font-size:16px;">No data Exist for this Month!!</td>
							</tr>
						</table>';
						}
			$html_str .= '</td>
				</tr>
			</table>';

		$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_str);

}else if(isset($_GET['action']) && $_GET['action'] == 'stock'){
	$filename = "Subdealer_Stock_Report_";
	$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;"';
	$headerData = $_GET;
	$DTS = $headerData['DTS'];
	$DTE = $headerData['DTE'];
	$repTyp = $headerData['repTyp'];
	$DTSAnsi = $classBusiLogic->convertDate($DTS, TRUE, 1);
	$DTEAnsi = $classBusiLogic->convertDate($DTE, TRUE, 1);
	if(isset($headerData['IdSubdealer']) && $headerData['IdSubdealer'] !=''){
		 $IdSubdealer  = $headerData['IdSubdealer'];
	}

	if(isset($headerData['CustomerCd']) && $headerData['CustomerCd'] !=''){
		 $CustomerCd = $headerData['CustomerCd'];
	}
	
	$searchVal = " A.DateAdded LIKE '%".$DTSAnsi."%' ";
		
	$ItemArr = array();
	$StockArr = array();
	$SrNo = 0;
	$SubdealerStockRep = $classSpecial->reportSubDealerStock($searchVal, $IdSubdealer, $CustomerCd);

	if($SubdealerStockRep){
		foreach($SubdealerStockRep AS $index => $value){
			$ItemArr[$SubdealerStockRep[$index]['ItemCd']] = $SubdealerStockRep[$index]['Item'];
			$StockArr[$SubdealerStockRep[$index]['ItemCd']][] = $SubdealerStockRep[$index];
		}
	}

	$html_str = '';
	$html_str .='<table width="100%" cellspacing="0" cellpadding="6" border="0">
					<tr bgcolor="#8D8D8D">
						<td>Sub Dealer Stock Report</td>
						<td align="right"> Date : ' .$DTS.'</td>
						</tr>
					</table>';
				if(!empty($StockArr) && count($StockArr) > 0){
						
					$html_str .='<table border="0" width="100%" cellspacing="1" cellpadding="2" class="dynamic" id="vtblBreak">
						<tr style="background-color: #AFAFAF;">
							<th align="center" width="2%" '.$HeadingRowStyleFirst.'>SrNo.</th>';
							if($repTyp == 0) {
							$html_str .='<th width="25%" '.$HeadingRowStyleOther.'>Dealer Name</th>
							<th width="%" '.$HeadingRowStyleOther.'>Station</th>';
							} if($repTyp == 1) {	
							$html_str .='<th width="25%" '.$HeadingRowStyleOther.'>Sub Dealer Name</th>
							<th width="%" '.$HeadingRowStyleOther.'>Station</th>';
							} 
							$html_str .='<th align="center" width="%" '.$HeadingRowStyleOther.'>RE-12 No.</th>
							<th align="center" width="%" '.$HeadingRowStyleOther.'>RE-12 Date</th>';
							foreach($ItemArr AS $ItemCd => $Item){
							$html_str .='<th align="center" width="%" '.$HeadingRowStyleOther.'>'.$Item.'</th>';
							 } 
							$html_str .='<th align="center" width="%" '.$HeadingRowStyleOther.'>Approved By Dealer</th>
						</tr>';
					
						foreach($StockArr as $Key => $value){
							$StockDetails = $StockArr[$Key];
							foreach($StockDetails as $index => $Value) {
							$SrNo++; 
					
						$html_str .='<tr onmouseout="setRowHover(this);" onmouseover="setRowHover(this);" '.(($SrNo%2==0) ? 'class="even_row"' : 'class="odd_row"' ).'>
						<td width="%" '.$RowStyleFirst.'>'.$SrNo.'</td>';
						 if($repTyp == 0) { 
						$StockId = $StockDetails[$index]['CustomerCd'];
					
						$html_str .='<td width="25%" '.$RowStyleOther.'>'.$StockDetails[$index]['Customer'].'</td>
						<td width="%" '.$RowStyleOther.'>'.$StockDetails[$index]['Station'].'</td>';
						} if($repTyp == 1) {
							$StockId = $StockDetails[$index]['IdSubdealer'];
						
						$html_str .='<td width="25%" '.$RowStyleOther.'>'.$StockDetails[$index]['Subdealer'].'</td>
						<td width="%" '.$RowStyleOther.'>'.$StockDetails[$index]['City'].'</td>';
						 }
						$html_str .='<td width="%" '.$RowStyleOther.'>'.$StockDetails[$index]['RE12NO'].'</td>
						<td width="%" '.$RowStyleOther.'>'.$StockDetails[$index]['RE12Date'].'</td>';
						 foreach($ItemArr AS $ItemCd => $Item){ 
								$Stock = 0;
								if($StockDetails[$index]['Item'] == $Item){
									$Stock = $StockDetails[$index]['Stock'];
								}
						$html_str .='<td width="%" '.$RowStyleOther.'>'.$Stock.'</td>';
						 } 
						$html_str .='<td width="12%" align="center" '.$RowStyleOther.'><input type="checkbox" name="Status['.$StockId.']" '.(($repTyp == 0 || $StockDetails[$index]['Status'] == 'A') ? 'disabled="disabled"' : '').''.(($StockDetails[$index]['Status'] == 'U') ? '' : 'checked="checked"').'></td>
						</tr>';
						}
					}
					
				$html_str .='</table>';
				 }else {
				$html_str .='<table cellpadding="0" cellspacing="0" border="0">
						<tr>
							<td>No Stock Details Exist!!</td>
						</tr>
					</table>';
				}
				$html_str .= '</td>
					</tr>
				</table>';

		$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_str);

}else if(isset($_GET['action']) && $_GET['action'] == 'target'){
	$filename = "Subdealer_Target_Report_";
	$ItemArray = '';
	$ItemCount = '';
	$count = 0;
	$headerData = $_GET;
	$color_array = array('#A9A9A9','#DEB887','#BDB76B','#DB7093','#BC8F8F','#4682B4','#9ACD32');

	
	$SubdealerTgtRep = $classSpecial->reportSubdealerTargetMonthly($headerData);
	$ItemCount = 0;
	if(!empty($SubdealerTgtRep) && count($SubdealerTgtRep) > 0){
		$ItemArray = array_unique($SubdealerTgtRep['Item']);
		$ItemCount = count($ItemArray);
	}
	$MYS = $headerData['MYS'];
	$repType = $headerData['repType'];
	if(isset($headerData['IdSubdealer']) && $headerData['IdSubdealer'] !=''){
		$IdSubdealer = $headerData['IdSubdealer'];
	}
	if(isset($headerData['CustomerCd']) && $headerData['CustomerCd'] !=''){
		$CustomerCd = $headerData['CustomerCd'];
	}
	if(isset($headerData['IsSubDealer']) && $headerData['IsSubDealer'] !=''){
		$IsSubDealer = $headerData['IsSubDealer'];
	}

	
	$StockDisplay = (($IdSubdealer == '' && $CustomerCd == '') ? '0' : '1' );
	$StockCount = 0;

	$html_str = '';
	$html_str .='<table width="100%" cellspacing="0" cellpadding="6" border="0">
					<tr bgcolor="#8D8D8D">
						<td>Sub Dealer Target Report</td>
						<td align="right"> For : ' .$MYS.'</td>
						</tr>
					</table>';
				if(!empty($SubdealerTgtRep) && count($SubdealerTgtRep) > 0){
						if($IsSubDealer) {
		$html_str .='<table border="1" width="30%" cellspacing="0" cellpadding="0" class="dynamic" id="vtblBreak">
						<tr><td colspan="3" align="center"><b>STOCK AS ON</b></td></tr>
						<tr class="grid report">
							<th width="10%">Sr. No.</th>	
							<th width="70%">Item Name</th>						
							<th width="20%">Stock</th>
						</tr>';
					
					foreach ($ItemArray AS $ItemKey => $ItemVal){
						$StockCount++;
						$ItemName = $ItemArray[$ItemKey];
						$TotalStock = 0;
						$html_str .='<tr><td>'.$StockCount.'</td>';
						$ItemName = $ItemArray[$ItemKey];
						$TotalStock = 0;
						$html_str .='<td>'.$ItemName.'</td>';
						$CustomerArr = $SubdealerTgtRep[$IdSubdealer]['Customer'];
						foreach($CustomerArr as $CustomerCd => $value){
						 $Stock = $SubdealerTgtRep[$IdSubdealer][$ItemName]['Stock'][$CustomerCd];
						 $TotalStock = $TotalStock + $Stock;
						}
			$html_str .='<td>'.$TotalStock.'</td></tr>';
					}
			
			$html_str .='</tr>
				</table>';
				 } 
				if($IdSubdealer !=''){
	$html_str .='<table cellspacing="0" cellpadding="0" width="100%"><tr style="font-size:20px;background-color:#008B8B;"><td colspan="2" width="100%" align="center">Monthly Target Report For '.$SubdealerTgtRep[$IdSubdealer]['Subdealer'].'</td></tr></table>';		
					}
	$html_str .='<table width="100%" cellspacing="0" cellpadding="0" border="0" class="dynamic" style="border: 0px solid grey;" id="vtblBreakDealer">
					<tr>
						<td width="100%">
							<table cellspacing="0" cellpadding="0" class="dynamic" id="tblBreakDetailDealer" width="100%" border="1">
								<tr class="grid report">
									<th rowspan="2" style="width:3%;font-size:18px;">Sr No.</th>';
									if ($repType == 0 || $repType == 1 || $CustomerCd !='') {
						$html_str .='<th rowspan="2" style="width:15%;font-size:18px;">SubDealer Name</th>';
									}
									if ($repType == 1 || $IdSubdealer !='') {
						$html_str .='<th rowspan="2" style="width:15%;font-size:18px;">Dealer Name</th>';
									}
									
									if($StockDisplay == 0){ 
						$html_str .='<th colspan="'.$ItemCount.'" style="width:10%;font-size:18px;background-color:#A9A9A9">Stock</th>';
									}
						$html_str .='<th colspan="'.$ItemCount.'" style="width:10%;font-size:18px;background-color:#DEB887;">Target</th>
									<th colspan="'.$ItemCount.'" style="width:10%;font-size:18px;background-color:#BDB76B;">Accept</th>
									<th colspan="'.$ItemCount.'" style="width:10%;font-size:18px;background-color:#DB7093;">Differance Target V/s Accept</th>
									<th colspan="'.$ItemCount.'" style="width:10%;font-size:18px;background-color: 	#BC8F8F;">Achievement</th>
									<th colspan="'.$ItemCount.'" style="width:10%;font-size:18px;background-color:#4682B4;">Accept Target V/s Achievement Variation</th>
									<th colspan="'.$ItemCount.'" style="width:15%;font-size:18px;background-color:#9ACD32;">Accept Target V/s Achievement Variation%</th>
								</tr>
								<tr class="grid report">';
								for($i = $StockDisplay; $i < 7; $i++) {									
									foreach ($ItemArray AS $ItemKey => $ItemVal){ 					
								$html_str .='<th width="1%" style="font-size:16px;background-color:'.$color_array[$i].';">'.$ItemArray[$ItemKey].'</th>';
									}
								}
								
					$html_str .='</tr>';
					
								foreach($SubdealerTgtRep as $index => $value){
									if($index != 'Item'){
										$CustomerArr = $SubdealerTgtRep[$index]['Customer'];
										if(!empty($CustomerArr)) {
										foreach($CustomerArr as $CustomerKey => $value){
											$count++;
								
					$html_str .='<tr onmouseout="setRowHover(this);" onmouseover="setRowHover(this);" '. (($count%2==0) ? 'class="even_row"' : 'class="odd_row"' ) .'>
								<td style="width:2%;font-size:16px;">'.$count.'</td>';									
								if ($repType == 0 || $repType == 1 || $CustomerCd !='') {
					$html_str .='<td style="width:%;font-size:16px;">'. $SubdealerTgtRep[$index]['Subdealer'] .'</td>';
								}
								if ($repType == 1 || $IdSubdealer !='') {
					$html_str .='<td style="width:%;font-size:16px;">'. $SubdealerTgtRep[$index]['Customer'][$CustomerKey] .'</td>';
								}
								if($StockDisplay == 0){
								 foreach ($ItemArray AS $ItemKey => $ItemVal){
									$ItemName = $ItemArray[$ItemKey];								
					$html_str .='<td style="width:5%;font-size:16px;">'. (float)$SubdealerTgtRep[$index][$ItemName]['Stock'][$CustomerKey].' </td>';
									}
								}
								foreach ($ItemArray AS $ItemKey => $ItemVal){
									$ItemName = $ItemArray[$ItemKey];
								
					$html_str .='<td style="width:5%;font-size:16px;">'.(float)$SubdealerTgtRep[$index][$ItemName]['Quantity'][$CustomerKey] .'</td>';
								}
								foreach ($ItemArray AS $ItemKey => $ItemVal){
									$ItemName = $ItemArray[$ItemKey];
					$html_str .='<td style="width:5%;font-size:16px;">'.(float)$SubdealerTgtRep[$index][$ItemName]['MonthStock'][$CustomerKey] .'</td>';
								}
								foreach ($ItemArray AS $ItemKey => $ItemVal){
									$ItemName = $ItemArray[$ItemKey];
					$html_str .='<td style="width:5%;">'.$SubdealerTgtRep[$index][$ItemName]['Difference'][$CustomerKey].'</td>';
								}
								foreach ($ItemArray AS $ItemKey => $ItemVal){
									$ItemName = $ItemArray[$ItemKey];
					$html_str .='<td style="width:5%;font-size:16px;">'. $SubdealerTgtRep[$index][$ItemName]['Achievement'][$CustomerKey].'</td>';
								}
								foreach ($ItemArray AS $ItemKey => $ItemVal){
									$ItemName = $ItemArray[$ItemKey];
					$html_str .='<td style="width:5%;font-size:16px;">'.$SubdealerTgtRep[$index][$ItemName]['Variation'][$CustomerKey].'</td>';
								}
								foreach ($ItemArray AS $ItemKey => $ItemVal){
									$ItemName = $ItemArray[$ItemKey];
					$html_str .='<td style="width:5%;font-size:16px;">'. $SubdealerTgtRep[$index][$ItemName]['VariationpPer'][$CustomerKey].'</td>';
								}
									
				$html_str .= '</tr>';
										}
											}
										}
									}
				$html_str .= '<tr style="font-weight:bold;background-color:#FFE4C4;font-size:18px;">
									<td>&nbsp;</td>
									<td>Total</td>';
					
										foreach ($SubdealerTgtRep['Total'] AS $Key => $Value){						
										if($StockDisplay == 1 && $Key == 'TotalStock'){
											continue;
										}else{
											foreach ($ItemArray AS $ItemKey => $ItemVal){
												$ItemName = $ItemArray[$ItemKey];										
								
					$html_str .= '<td style="width:5%;">'. (float)$SubdealerTgtRep['Total'][$Key][$ItemName].'</td>';
									}
											}
										}
			
									
				$html_str .= '</tr>
						</table>';
								 }else { 
				$html_str .= '<table cellpadding="0" cellspacing="0" border="0">
								<tr>
									<td style="font-size:16px;">No data Exist for this Month!!</td>
								</tr>
							</table>';
							}
				$html_str .= '</td>
					</tr>
				</table>';

		$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_str);

}else{
	$calltype = '';
	$phone_details = '';
	$phone_count = 0;
	$DTS	= $_GET['DTS'];
	$DTE    = $_GET['DTE'];
	if(isset($_GET['CustomerCd']) && $_GET['CustomerCd'] !=''){
		$CustomerCd = $_GET['CustomerCd'];	
	}
	if(isset($_GET['IdSubdealer']) && $_GET['IdSubdealer'] !=''){
		$IdSubdealer = $_GET['IdSubdealer'];
		$where_str .= "AND pc.SubDealerCd='".$IdSubdealer."'";
	}
	if(isset($_GET['call_type_cd']) && $_GET['call_type_cd'] !=''){
		$calltype = $_GET['call_type_cd'];
	}
	$arrCallType = $classSpecial->getCallType($calltype);
	$call_type_cd_array = array();
	foreach($arrCallType as $index => $value){
		$call_type_cd_array['call_type_cd'][] = $arrCallType[$index]['call_type_cd'];
	}	
	$CallType = implode("','", $call_type_cd_array['call_type_cd']);

	$listData = $classSpecial->getPhoneUserList($CustomerCd);
	
	$DTSAnsi = $classBusiLogic->convertDate($DTS, TRUE, 1);
	$DTEAnsi = $classBusiLogic->convertDate($DTE, TRUE, 1);  
	$date_array = $classSpecial->createDateRangeArray($DTSAnsi,$DTEAnsi);

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

	$summary_html = '';
	$summary_html .= '<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;">Summary Dealer Phone Call Report</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">Sr.No</td>
							<td '.$HeadingRowStyleOther.' align="center" rowspan="2" width="200px">Firm Name & Station</td>
							<td '.$HeadingRowStyleOther.' align="center" colspan="2" width="150px">Received</td>
							<td '.$HeadingRowStyleOther.' align="center" rowspan="2" width="450px">Remark</td>
							<td '.$HeadingRowStyleOther.' align="center" rowspan="2" width="80px">Date</td>
						</tr>
						<tr style="background-color: #AFAFAF;">
							<td '.$HeadingRowStyleOther.' align="center" width="75px">Time</td>
							<td '.$HeadingRowStyleOther.' align="center" width="15px">Status</td>
						</tr>';
						$rowcnt = 0;
						$srno = 1;
						foreach($listData as $curRow){
							$Customer = $curRow['Customer'];
							foreach($date_array AS $index => $value){
								$date_added = $date_array[$index];
								$reason = '&nbsp;';
								$status = 'Not Received';
								$late_time = strtotime('07:45 PM');	
								$time = "-";
									
								$reportFill = $classSpecial->getSummaryPhCall($date_added, $curRow['CustomerCd']);
			
								if(!empty($reportFill)){
									$reason = $reportFill[0]['reason'];
									$time = date('h:i A',strtotime($reportFill[0]['date_added']));
									if(strtotime($time) > $late_time){
										$status = 'Late Received';
									}else{
										$status = $reportFill[0]['status'];
									}
								}

								switch ($status){
									case 'Not Received': 
										$bgcolor = 'bgcolor="#FF0000"';
									break;
									case 'Late Received': 
										$bgcolor = 'bgcolor="#FFFF00"';
									break;
									case 'Nil Record': 
										$bgcolor = 'bgcolor="#0000CD"';
									break;
									default :
										$bgcolor = 'bgcolor="#008000"';
									break;
								}
								
								$rowcnt++;
								
								$summary_html .= '<tr>
									<td '.$RowStyleFirst.' width="40px">'.$srno.'</td>
									<td '.$RowStyleOther.' width="200px">'.strtoupper($Customer).'</td>
									<td '.$RowStyleOther.' width="75px" align="center">'.$time.'</td>
									<td '.$RowStyleOther.' '.$bgcolor.' width="75px" align="center">'.$status.'</td>
									<td '.$RowStyleOther.' width="450px" align="center">'.$reason.'</td>
									<td '.$RowStyleOther.' width="80px">'.date('d-m-Y',strtotime($date_added)).'</td>
									</tr>';
								$srno++;
							}
						}
						$summary_html .= '</table>
					</td>
				</tr>
				<tr>
					<td style="font-weight:bold:font-size:14px;">
						<table style="border:1px solid black;" width="20%" cellspacing="0" cellpadding="0">
						<caption>Color Display Chart</caption>
							<tr>
								<td width="50%" '.$HeadingRowStyleFirst.'>Received</td>
								<td width="50%" bgcolor="#00FF00" '.$RowStyleOther.'>&nbsp;</td>
							</tr>
							<tr>
								<td width="50%" '.$HeadingRowStyleFirst.'>Late Recd.</td>
								<td width="50%" bgcolor="yellow" '.$RowStyleOther.'>&nbsp;</td>
							</tr>
							<tr>
								<td width="50%" '.$HeadingRowStyleFirst.'>Nil Recd.</td>
								<td width="50%" bgcolor="blue" '.$RowStyleOther.'>&nbsp;</td>
							</tr>
							<tr>
								<td width="50%" '.$HeadingRowStyleFirst.'>Not Recd</td>
								<td width="50%" bgcolor="red" '.$RowStyleOther.'>&nbsp;</td>										
							</tr>
						</table>
					</td>
				</tr>
			</table>';

	if($rowcnt > 0 ){
		$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($summary_html);
	}


	$phone_count = 0;

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

	foreach($listData as $curRow){
		$CustomerCd = $curRow['CustomerCd'];
		$str = "AND pc.CustomerCd='".$CustomerCd."'";
		
		$phone_details = $classSpecial->pdfreportPhoneCall($DTS, $DTE, $CallType, $str ,$where_str);

	if(!empty($phone_details) &&  sizeof($phone_details) > 0){
		$prevCustomerCd = 0;
		$prevCallType = '';
		$html_str = '';
		$phone_data_array = array();	
		$phone_count = 0;
		foreach($phone_details AS $key => $value) {
			$phone_count++;
			$currentCustomerCd = $phone_details[$key]['CustomerCd'];
			if($currentCustomerCd != $prevCustomerCd){
				$prevCustomerCd = $currentCustomerCd;		
				$html_str .= '<table id="tblHeading" width="100%" cellspacing="0" cellpadding="6" border="0" style="background-color:#B1E0E0;">
								<tr id="tblHeadingTR">
								<td id="tblHeadingTD">Phone Daily Report</td>
								<td align="right">'.($DTS==$DTE ? ' As On ' . $DTS : ' For ' . $DTS . ' To ' . $DTE).'</td>
							</tr>
						</table>
						<table border="0" cellspacing="0" cellpadding="2" style="width:100%;border:1px solid;" id="vtblBreakDealer">
							<tr>
								<td align="center" colspan="9" style="background-color: #A1C299; font-size: 16px; font-weight: bold;">Phone Daily Report By -&nbsp;'. strtoupper($phone_details[$key]['CustomerName']).'</td>
							</tr>';
			}
			$currentCallType = $phone_details[$key]['call_type_cd'];
			if($currentCallType != $prevCallType){
				$Sr_No = 0;
				$prevCallType = $currentCallType;
				$html_str .= '<tr>
							<td colspan="2" align="center" style="background-color: #F6C0A7;"><div width="250px" style="font-size: 16px;font-weight: bold; text-align: center; ">'.$phone_details[$key]['call_type_name'].'</div></td>
							<td colspan="7">&nbsp;</td>
						</tr>

						<tr style="background-color: #AFAFAF;border-top: 1px solid grey;">
							<th align="center" width="1%" '.$HeadingRowStyleFirst.'>Sr.No</th>
							<th align="center" width="20%" '.$HeadingRowStyleOther.'>Firm Name</th>
							<th align="center" width="%" '.$HeadingRowStyleOther.'>ST</th>
							<th align="center" width="%" '.$HeadingRowStyleOther.'>Person</th>
							<th align="center" width="%" '.$HeadingRowStyleOther.'>Time</th>
							<th align="center" width="%" '.$HeadingRowStyleOther.'>L/M</th>
							<th align="center" width="%" '.$HeadingRowStyleOther.'>Contact By</th>
							<th align="center" width="%" '.$HeadingRowStyleOther.'>Date</th>
							<th align="center" width="80%" '.$HeadingRowStyleOther.'>Message</th>
						</tr>';
			}

			$Sr_No++;
			$style = '';
			$state_cd = '';
			$msg_style = 'style="background-color:#ffffff; font-size:16px;border-right: 1px solid grey;border-bottom: 1px solid grey;"';
			if($phone_details[$key]['priority_flag'] == '1'){
				$msg_style = 'style="background-color:#ffffff; color:red;border-right: 1px solid grey;border-bottom: 1px solid grey;font-size:16px;"';				
			}						
			$state_cd = $phone_details[$key]['StateCd'];
							
			$html_str .='<tr><td '.$Rowfirst.' width="1%">'. $Sr_No.'</td>
			<td '.$Row.' width="15%">'. $phone_details[$key]['SubDealerName'].'</td>
			<td '.$Row.' align="center" width="1%">'. $state_cd.'</td>
			<td '.$Row.' align="center" width="8%">'. $phone_details[$key]['Person'].'</td>
			<td '.$Row.' align="center" width="1%">'. date('H:i',strtotime($phone_details[$key]['call_time'])).'</td>
			<td '.$Row.' align="center" width="1%">'. $phone_details[$key]['contact_medium'].'</td>
			<td '.$Row.' align="center" width="1%">'. $phone_details[$key]['ContactBy'].'</td>
			<td '.$Row.' width="%">'. date('d-m-Y' ,strtotime($phone_details[$key]['date_added'])).'</td>
			<td '.$msg_style.' align="justify" width="75%">'. $phone_details[$key]['message'].'</td>
			</tr>';
			}
			$html_str .='</table>';	
			
			if($phone_count > 0){
			$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);
			}
		}
	}
		


	if($phone_count == 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('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_str);
	}
	$filename = "phone_report_";
}
$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('Daily Dealer 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('atul.gupta1112@gmail.com' => 'Atul Gupta')) // 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,'D');
	exit;
}


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