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

ini_set('max_execution_time', 300);

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, '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 = '';

$calltype = '';
$phone_details = '';
$phone_count = 0;
$DTS	= date("d-m-Y");
$DTE    = date("d-m-Y");

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 = "dealer_phone_report_";
$Date = date('d-m-Y');
$filename = $filename.$Date;

$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('Auto Mail 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', 'k.bhadada@solargroup.com' => 'K.Bhadadaji',  'kcn@solargroup.com' => 'kcn@solargroup.com',  'bhl1@solargroup.com'=>'Abhishek', 'websupport@solargroup.com' => 'Web Support')) // Array of people to send to 
			->setTo(array( 'websupport@solargroup.com' => 'Web Support', 'rajesh.watkar@solargroup.com' =>'Rajesh Sir')) // 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>";
}

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