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

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

/*shawpon modify for online*/
$title = unserialize(base64_decode($_GET['title']));
$subtitle = unserialize(base64_decode($_GET['subtitle']));

/*shawpon modify for online*/
//$data = unserialize(base64_decode($_GET['data']));
if(substr(LOCAL_PATH, -1)=="/"){
	$filename = LOCAL_PATH.unserialize(base64_decode($_GET['data']));
}else{
	$filename = LOCAL_PATH."/".unserialize(base64_decode($_GET['data']));
}
$data=array();
$string_data = file_get_contents($filename);
$data = unserialize($string_data);

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_rn_healthcare_pharma.png" width="250px" /></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: 12px; color:#000000;"><b>Upto : </b> '.$subtitle.'</td>
		<td width ="33%" align="center" style="font-size: 13px; color:#000000;font-weight:bold;">'.$title.'</td>
		<td width ="33%" align="right" style="font-size: 12px; color:#000000;"><b>Print Date : </b>'.date("d/m/Y h:m:i A").'</td>
	</tr>
</table>
';

$html='<table width="100%" style="color: #000000;border: 1px solid #000000; border-collapse: collapse;">
    <thead>
        <tr>
		<th style="background-color:#ACA6A6;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: 13px;">
		 Item 
		</th>
		<th style="background-color:#ACA6A6;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: 13px;">
		 Quantity 
		</th>
		<th style="background-color:#ACA6A6;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: 13px;">
		 Total Price 
		</th>
	</tr>
    </thead>
    <tbody>';

$root_serial=1;
ksort($data);
$cat_grand_total_qty_opening=0;
$cat_grand_total_amount_opening=0;
$gender_key=array();
foreach ($data as $key_gender => $value_gender) {
	$gender_key[]=$key_gender;

	$html.='<tr>';
	$html.='<td  colspan=3 style="background-color:#A6A6A6;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:left;font-size: 13px;font-weight:bold; " >&nbsp;'.$root_serial.'&nbsp;'.$key_gender.'&nbsp;</td>';
	$html.='</tr>';
	$root_serial_cat_conmpany=1;
	$cat_total_qty_opening=0;
	$cat_total_amount_opening=0;
	ksort($value_gender);
	foreach ($value_gender as $key_comapny => $value_company) {

		$html.='<tr>';
		$html.='<td  colspan=3 style="background-color:#E1E0E0;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:left;font-size: 11px;font-weight:bold;" >&nbsp;'.$root_serial.".".str_pad($root_serial_cat_conmpany, 3, "0", STR_PAD_LEFT).'&nbsp;'.$key_comapny.'&nbsp;</td>';
		$html.='</tr>';

		ksort($value_company);
		$item_serial=1;

		$comapny_total_qty=0;
		$comapny_total_price=0;

		foreach ($value_company as $item_key => $item_value) {

			$html.='<tr>';
			$html.='<td style="background-color:#FFFFFF;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:left;font-size: 10px;" >&nbsp;'.str_pad($root_serial, 1, "0", STR_PAD_LEFT).'.'.str_pad($root_serial_cat_conmpany, 3, "0", STR_PAD_LEFT).'.'.str_pad($item_serial, 3, "0", STR_PAD_LEFT).' &nbsp;'.$item_key.'&nbsp;</td>';

			$html.='<td style="background-color:#FFFFFF;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: 10px; " >';
			if($item_value['qty_total']){
				$html.=$item_value['qty_total'];
			}else{
				$html.="-";
			}
			$html.='</td>';


			$html.='<td style="background-color:#FFFFFF;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: 10px; " >';
			if($item_value['price_total']){
				$html.=number_format($item_value['price_total'],2,'.','');
			}else{
				$html.="-";
			}
			$html.='</td>';


			$comapny_total_qty+=$item_value['qty_total'];
			$comapny_total_price+=number_format($item_value['price_total'],2,'.','');

			$cat_total_qty_opening+=$item_value['qty_total'];
			$cat_total_amount_opening+=number_format($item_value['price_total'],2,'.','');

			$html.='</tr>';
			$item_serial++;
		}

		$html.='<tr>';
		$html.='<td style="background-color:#E1E0E0;color: #000000;font-weight:bold;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: 10px;" >Sub-Total ('.$key_comapny.')&nbsp;</td>';

		$html.='<td style="background-color:#E1E0E0;color: #000000;font-weight:bold;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: 10px; " >'.$comapny_total_qty.'</td>';
		$html.='<td style="background-color:#E1E0E0;color: #000000;font-weight:bold;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: 10px;" >'.number_format($comapny_total_price,2,'.','').'</td>';

		$html.='</tr>';

		$comapny_total_qty=0;
		$comapny_total_price=0;
		$root_serial_cat_conmpany++;
	}

	$cat_grand_total_qty_opening+=$cat_total_qty_opening;
	$cat_grand_total_amount_opening+=$cat_total_amount_opening;


	$html.='<tr>';
	$html.='<td style="background-color:#CECDCD;color: #000000;font-weight:bold;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: 13px;" >Total ('.$key_gender.')&nbsp;</td>';

	$html.='<td style="background-color:#CECDCD;color: #000000;font-weight:bold;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: 13px; " >'.$cat_total_qty_opening.'</td>';
	$html.='<td style="background-color:#CECDCD;color: #000000;font-weight:bold;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: 13px;" >'.number_format($cat_total_amount_opening,2,'.','').'</td>';

	$html.='</tr>';

	$root_serial++;
}
 $gender_key_str=implode(",",$gender_key);

$html.='<tr>';
$html.='<td style="background-color:#ACA6A6;color: #000000;font-weight:bold;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: 14px;" >Grand Total ('.$gender_key_str.')&nbsp;</td>';

$html.='<td style="background-color:#ACA6A6;color: #000000;font-weight:bold;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: 14px; " >'.$cat_grand_total_qty_opening.'</td>';
$html.='<td style="background-color:#ACA6A6;color: #000000;font-weight:bold;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: 14px;" >'.number_format($cat_grand_total_amount_opening,2,'.','').'</td>';

$html.='</tr>';

$html.='</tbody></table>';

$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',0,'',10,10,32,20);

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

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

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

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

$mpdf->WriteHTML($html);

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

exit;
?>