<?php
$path_to_root="../../../";

include($path_to_root.'/application/config/config.php');

$data = unserialize(base64_decode($_GET['data']));
$title = unserialize(base64_decode($_GET['title']));
$subtitle = unserialize(base64_decode($_GET['subtitle']));
$payment_data = unserialize(base64_decode($_GET['payment_data']));

$dates_array_st=explode("-",$subtitle);

$start_date_array=explode("/",$dates_array_st[0]);

$start_date=$start_date_array[2]."-".$start_date_array[1]."-".$start_date_array[0];

$end_date_array=explode("/",$dates_array_st[1]);
$end_date=$end_date_array[2]."-".$end_date_array[1]."-".$end_date_array[0];

include_once($path_to_root . "/pdf_report/mpdf60/print/custom_function.php");

$header = '
<table width="100%" style="border-bottom: 1px solid #000000;">
	<tr>
		<td colspan=3 width="100%" align="center"><img src="logo_grameenshamogree.png" width="126px" /></td>
	</tr>
	<tr>
		<td width="100%" colspan=3 align="center" style="font-size: 17px; color:#000000;">'.LOCATION_NAME.'</td>
	</tr>
	<tr>
		<td  width ="33%" align="left" style="font-size: 17px; color:#000000;"><b>Date Range : </b> '.$subtitle.'</td>
		<td width ="33%" align="center" style="font-size: 17px; color:#000000;">'.$title.'</td>
		<td width ="33%" align="right" style="font-size: 17px; color:#000000;"><b>Print Date : </b>'.date("d/m/Y").'</td>
	</tr>
</table>
';


$total=count($employee_code)+2;

$per_space=(90/$total)."%";

include('application/config/config.php');

$html='<table width="100%" style="color: #000000;border: 1px solid #000000; border-collapse: collapse;">
    <thead>
        <tr>
		<th width="100px" rowspan="2" style="background-color:#CBC8C8;color: #000000;border-top: 1px solid black; border-left: 1px solid black; border-bottom: 1px solid black; border-right: 1px solid black; border-collapse: collapse; text-align:center;font-size: 12pt;">
		 Date 
		</th>';
foreach ($employee_code as $key => $value) {
	$html.='<th width="'.$per_space.'" colspan ="2" style="background-color:#CBC8C8;color: #000000;border-top: 1px solid black; border-left: 1px solid black; border-bottom: 0px solid black; border-right: 1px solid black; border-collapse: collapse; text-align:center;font-size: 12pt;">'.
	$key.'
		</th>';
}


$html.='<th colspan ="2" rowspan="2"  width="'.$per_space.'" style="background-color:#CBC8C8;color: #000000;border-top: 1px solid black; border-left: 1px solid black; border-bottom: 1px solid black; border-right: 1px solid black; border-collapse: collapse; text-align:center;font-size: 12pt;">
		 Customer Own  
		</th>
		<th colspan="2" rowspan="2" width="'.$per_space.'" style="background-color:#CBC8C8;color: #000000;border-top: 1px solid black; border-left: 1px solid black; border-bottom: 1px solid black; border-right: 1px solid black; border-collapse: collapse; text-align:center;font-size: 12pt;">
		 Day Total  
		</th>
	</tr>
	<tr>';
foreach ($employee_code as $key => $value) {
	$html.='<th colspan ="2" style="background-color:#CBC8C8;color: #000000;border-top: 0px solid black; border-left: 1px solid black; border-bottom: 1px solid black; border-right: 1px solid black; border-collapse: collapse; text-align:center;font-size: 12pt;">'.
	$value["full_name"].'
		</th>';
}
$html.='
	</tr>
	
    </thead>
    <tbody>';

$root_serial=1;
ksort($data);



function date_range($first, $last, $step = '+1 day', $output_format = 'd/m/Y' ) {
	$dates = array();
	$current = strtotime($first);
	$last = strtotime($last);
	while( $current <= $last ) {
		$dates[date("Y-m-d", $current)] = date($output_format, $current);
		$current = strtotime($step, $current);
	}
	return $dates;
}

$dates=date_range($start_date,$end_date);
$i=0;

$grand_total=array();

foreach ($dates as $date_key => $date_value) {
	if($i%2==0){
		$bg_color="#EFECEC";
	}else{
		$bg_color="#FFFFFF";
	}
	$i++;

	$html.='<tr>';
	$html.='<td style="background-color:'.$bg_color.';color: #000000;border-top: 1px solid black; border-left: 1px solid black; border-bottom: 1px solid black; border-right: 1px solid black; border-collapse: collapse; text-align:center;font-size: 12pt; " >&nbsp;'.$date_value.'&nbsp;</td>';



	$day_total=0;
	$day_total_qty=0;
	foreach ($employee_code as $emp_key => $emp_value) {

		$amount_individual=$data[$date_key][$emp_key]['individual_sales'];
		$amount_individual_sale=$data[$date_key][$emp_key]['individual_sales_qty'];
		$day_total+=$amount_individual;
		$day_total_qty+=$amount_individual_sale;

		$grand_total[$emp_key]+=$amount_individual;
		$grand_total_qty[$emp_key]+=$amount_individual_sale;

		$html.='<td style="background-color:'.$bg_color.';color: #000000;border-top: 1px solid black; border-left: 1px solid black; border-bottom: 1px solid black; border-right: 1px solid black; border-collapse: collapse; text-align:right;font-size: 12pt; " >&nbsp;'.number_format($amount_individual_sale,2,'.',',').'&nbsp;</td>';
		$html.='<td style="background-color:'.$bg_color.';color: #000000;border-top: 1px solid black; border-left: 1px solid black; border-bottom: 1px solid black; border-right: 1px solid black; border-collapse: collapse; text-align:right;font-size: 12pt; " >&nbsp;'.number_format($amount_individual,2,'.',',').'&nbsp;</td>';

	}



	$customer_own=$data[$date_key]['']['individual_sales'];
	$customer_own_qty=$data[$date_key]['']['individual_sales_qty'];
	$day_total+=$customer_own;
	$day_total_qty+=$customer_own_qty;
	$grand_total['customer_own']+=$customer_own;
	$grand_total['customer_own_qty']+=$customer_own_qty;


	$html.='<td style="background-color:'.$bg_color.';color: #000000;border-top: 1px solid black; border-left: 1px solid black; border-bottom: 1px solid black; border-right: 1px solid black; border-collapse: collapse; text-align:right;font-size: 12pt; " >&nbsp;'.number_format($customer_own_qty,2,'.',',').'&nbsp;</td>';
	$html.='<td style="background-color:'.$bg_color.';color: #000000;border-top: 1px solid black; border-left: 1px solid black; border-bottom: 1px solid black; border-right: 1px solid black; border-collapse: collapse; text-align:right;font-size: 12pt; " >&nbsp;'.number_format($customer_own,2,'.',',').'&nbsp;</td>';
	$html.='<td style="background-color:'.$bg_color.';color: #000000;border-top: 1px solid black; border-left: 1px solid black; border-bottom: 1px solid black; border-right: 1px solid black; border-collapse: collapse; text-align:right;font-size: 12pt; " >&nbsp;'.number_format($day_total_qty,2,'.',',').'&nbsp;</td>';
	$html.='<td style="background-color:'.$bg_color.';color: #000000;border-top: 1px solid black; border-left: 1px solid black; border-bottom: 1px solid black; border-right: 1px solid black; border-collapse: collapse; text-align:right;font-size: 12pt; " >&nbsp;'.number_format($day_total,2,'.',',').'&nbsp;</td>';
	$html.='</tr>';
}


$html.='<tr>';
$html.='<td style="background-color:#ECAD2E;color: #000000;border-top: 1px solid black; border-left: 1px solid black; border-bottom: 1px solid black; border-right: 1px solid black; border-collapse: collapse; text-align:right;font-size: 12pt; " >&nbsp;Grand Total&nbsp;</td>';

$grand_day_total=0;
$grand_day_total_qty=0;
foreach ($employee_code as $emp_key => $emp_value) {
	$grand_day_total+=$grand_total[$emp_key];
	$grand_day_total_qty+=$grand_total_qty[$emp_key];
	
	$html.='<td style="background-color:#ECAD2E;color: #000000;border-top: 1px solid black; border-left: 1px solid black; border-bottom: 1px solid black; border-right: 1px solid black; border-collapse: collapse; text-align:right;font-size: 12pt; " >&nbsp;'.number_format($grand_total_qty[$emp_key],2,'.',',').'&nbsp;</td>';
	$html.='<td style="background-color:#ECAD2E;color: #000000;border-top: 1px solid black; border-left: 1px solid black; border-bottom: 1px solid black; border-right: 1px solid black; border-collapse: collapse; text-align:right;font-size: 12pt; " >&nbsp;'.number_format($grand_total[$emp_key],2,'.',',').'&nbsp;</td>';
}
$grand_day_total+=$grand_total['customer_own'];
$grand_day_total_qty+=$grand_total['customer_own_qty'];

$html.='<td style="background-color:#ECAD2E;color: #000000;border-top: 1px solid black; border-left: 1px solid black; border-bottom: 1px solid black; border-right: 1px solid black; border-collapse: collapse; text-align:right;font-size: 12pt; " >&nbsp;'.number_format($grand_total['customer_own_qty'],2,'.',',').'&nbsp;</td>';
$html.='<td style="background-color:#ECAD2E;color: #000000;border-top: 1px solid black; border-left: 1px solid black; border-bottom: 1px solid black; border-right: 1px solid black; border-collapse: collapse; text-align:right;font-size: 12pt; " >&nbsp;'.number_format($grand_total['customer_own'],2,'.',',').'&nbsp;</td>';
$html.='<td style="background-color:#ECAD2E;color: #000000;border-top: 1px solid black; border-left: 1px solid black; border-bottom: 1px solid black; border-right: 1px solid black; border-collapse: collapse; text-align:right;font-size: 12pt; " >&nbsp;'.number_format($grand_day_total_qty,2,'.',',').'&nbsp;</td>';
$html.='<td style="background-color:#ECAD2E;color: #000000;border-top: 1px solid black; border-left: 1px solid black; border-bottom: 1px solid black; border-right: 1px solid black; border-collapse: collapse; text-align:right;font-size: 12pt; " >&nbsp;'.number_format($grand_day_total,2,'.',',').'&nbsp;</td>';


$html.='</tr>';

$html.='</tbody></table><div>&nbsp;</div>';

$footer = '<p align="center" style="border-top: 1px solid #000000;">'.REPORT_FOOTER_LINE_1.'<br />'.REPORT_FOOTER_LINE_2.'&nbsp;&nbsp;&nbsp;&nbsp;<font style="font-style: italic;font-weight:bold;font-size:11px;">Page {PAGENO} of {nb}</font></p>';

include("../mpdf.php");

$mpdf=new mPDF('utf-8', 'A4-L',0,'',10,10,35,20);

$mpdf->SetHTMLHeader($header);
$mpdf->SetHTMLFooter($footer);

$mpdf->autoScriptToLang = true;
$mpdf->baseScript = 1;
$mpdf->autoVietnamese = true;
$mpdf->autoArabic = true;

$mpdf->SetWatermarkImage('grameen_shamogree_wm.png', 0.10, 'D');

$mpdf->showWatermarkImage = true;
$mpdf->autoLangToFont = true;

$mpdf->WriteHTML($html);

$mpdf->Output('stock_transaction_summary_report_'.LOCATION_CODE.'_'.$subtitle.'.pdf','I');

exit;
?>