<?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']));

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




$html='<table width="100%" style="color: #000000;border: 1px solid #000000; border-collapse: collapse;">
    <thead>
        <tr>
		<th  rowspan="2" width="16%" 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: 12px;">
		 Item 
		</th>
		<th colspan="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: 12px;">
		 Opening 
		</th>
		<th colspan="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: 12px;">
		 Receive 
		</th>
		<th colspan="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: 12px;">
		 Local Sale 
		</th>
		<th colspan="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: 12px;">
		 Network Sale 
		</th>
		<th colspan="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: 12px;">
		 Network Delivery 
		</th>
		<th colspan="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: 12px;">
		 Return
		</th>
		<th colspan="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: 12px;">
		 Closing
		</th>
	</tr>
	<tr>
		<th 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: 12px;">
		 Qty 
		</th>
		<th 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: 12px;">
		 Price 
		</th>
		
		<th 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: 12px;">
		 Qty 
		</th>
		<th 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: 12px;">
		 Price 
		</th>
		
		<th 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: 12px;">
		 Qty 
		</th>
		<th 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: 12px;">
		 Price 
		</th>
		
		<th 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: 12px;">
		 Qty 
		</th>
		<th 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: 12px;">
		 Price 
		</th>
		
		<th 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: 12px;">
		 Qty 
		</th>
		<th 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: 12px;">
		 Price 
		</th>
		
		<th 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: 12px;">
		 Qty 
		</th>
		<th 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: 12px;">
		 Price 
		</th>
		
		<th 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: 12px;">
		 Qty 
		</th>
		<th 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: 12px;">
		 Price 
		</th>
		
	</tr>
    </thead>
    <tbody>';

$root_serial=1;
ksort($data);



$cat_total_total_qty_opening=0;
$cat_total_total_amount_opening=0;

$cat_total_total_qty_receive=0;
$cat_total_total_amount_receive=0;

$cat_total_total_qty_local_sale=0;
$cat_total_total_amount_local_sale=0;

$cat_total_total_qty_network_sale=0;
$cat_total_total_amount_network_sale=0;

$cat_total_total_qty_network_delivery=0;
$cat_total_total_amount_network_delivery=0;

$cat_total_total_qty_network_return=0;
$cat_total_total_amount_network_return=0;

$cat_total_total_qty_network_closing=0;
$cat_total_total_amount_network_closing=0;

$grand_total_discount=0;
$grand_total_adjustment=0;
$grand_total_vatable_price=0;
$grand_total_vat=0;



foreach ($data as $key => $value) {

	$html.='<tr>';
	$html.='<td  colspan=15 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: 12pt; " >&nbsp;'.$root_serial.'&nbsp;'.$key.'&nbsp;</td>';
	$html.='</tr>';

	$root_serial_cat=1;
	foreach ($value as $cat_key => $cat_value) {

		$html.='<tr>';
		$html.='<td  colspan=15 style="background-color:#E0DBDB;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: 11pt; " >&nbsp;'.$root_serial.'.'.str_pad($root_serial_cat, 2, "0", STR_PAD_LEFT).'&nbsp;'.$cat_key.'</td>';
		$html.='</tr>';

		$serial_item=1;

		$cat_total_qty_opening=0;
		$cat_total_amount_opening=0;

		$cat_total_qty_receive=0;
		$cat_total_amount_receive=0;

		$cat_total_qty_local_sale=0;
		$cat_total_amount_local_sale=0;

		$cat_total_qty_network_sale=0;
		$cat_total_amount_network_sale=0;

		$cat_total_qty_network_delivery=0;
		$cat_total_amount_network_delivery=0;

		$cat_total_qty_network_return=0;
		$cat_total_amount_network_return=0;

		$cat_total_qty_network_closing=0;
		$cat_total_amount_network_closing=0;


		foreach ($cat_value as $d_key => $d_value) {

			$closing_qty=0;
			$closing_amount=0;


			$grand_total_discount+=$d_value['discount_amount'];
			$grand_total_discount-=abs($d_value['return_discount_amount']);
			$grand_total_discount+=$d_value['global_discount_amount'];


			$grand_total_adjustment+=$d_value['adjustment_amount'];
			$grand_total_adjustment-=abs($d_value['return_adjustment_amount']);
			$grand_total_adjustment+=$d_value['global_adjustment_amount'];


			$grand_total_vatable_price+=$d_value['vat_enable_price'];
			$grand_total_vatable_price-=abs($d_value['return_vat_enable_price']);
			$grand_total_vatable_price+=$d_value['global_vat_enable_price'];


			$grand_total_vat+=$d_value['vat_amount'];
			$grand_total_vat-=abs($d_value['return_vat_amount']);
			$grand_total_vat+=$d_value['global_vat_amount'];


			$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: 10pt;" >&nbsp;'.$root_serial.'.'.str_pad($root_serial_cat, 2, "0", STR_PAD_LEFT).'.'.str_pad($serial_item, 2, "0", STR_PAD_LEFT).' &nbsp;'.$d_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: 10pt; " >';
			if($d_value['opening_qty']){
				$html.=$d_value['opening_qty'];
			}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: 10pt; " >';
			if($d_value['opening_price']){
				$html.=number_format($d_value['opening_price'],2,'.','');
			}else{
				$html.="-";
			}
			$html.='</td>';

			$closing_qty+=$d_value['opening_qty'];
			$closing_amount+=number_format($d_value['opening_price'],2,'.','');



			$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: 10pt; " >';
			if($d_value['receive_qty']){
				$html.=$d_value['receive_qty'];
			}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: 10pt; " >';
			if($d_value['receive_qty_price']){
				$html.=number_format($d_value['receive_qty_price'],2,'.','');
			}else{
				$html.="-";
			}
			$html.='</td>';


			$closing_qty+=$d_value['receive_qty'];
			$closing_amount+=number_format($d_value['receive_qty_price'],2,'.','');



			$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: 10pt; " >';
			if($d_value['sales_qty']){
				$html.=$d_value['sales_qty'];
			}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: 10pt; " >';
			if($d_value['product_amount']){
				$html.=number_format($d_value['product_amount'],2,'.','');
			}else{
				$html.="-";
			}
			$html.='</td>';

			$closing_qty-=$d_value['sales_qty'];
			$closing_amount-=number_format($d_value['product_amount'],2,'.','');



			$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: 10pt; " >';
			if($d_value['global_sales_qty']){
				$html.=$d_value['global_sales_qty'];
			}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: 10pt; " >';
			if($d_value['global_product_amount']){
				$html.=number_format($d_value['global_product_amount'],2,'.','');
			}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:center;font-size: 10pt; " >';
			if($d_value['global_delivery_sales_qty']){
				$html.=$d_value['global_delivery_sales_qty'];
			}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: 10pt; " >';
			if($d_value['global_delivery_product_amount']){
				$html.=number_format($d_value['global_delivery_product_amount'],2,'.','');
			}else{
				$html.="-";
			}
			$html.='</td>';

			$closing_qty-=$d_value['global_delivery_sales_qty'];
			$closing_amount-=number_format($d_value['global_delivery_product_amount'],2,'.','');

			$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: 10pt; " >';
			if($d_value['return_sales_qty']){
				$html.=$d_value['return_sales_qty'];
			}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: 10pt; " >';
			if($d_value['return_product_amount']){
				$html.=number_format($d_value['return_product_amount'],2,'.','');
			}else{
				$html.="-";
			}
			$html.='</td>';

			$closing_qty+=$d_value['return_sales_qty'];
			$closing_amount+=number_format($d_value['return_product_amount'],2,'.','');


			$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: 10pt; " >';
			if($closing_qty){
				$html.=$closing_qty;
			}else{
				$html.="0";
			}
			$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: 10pt; " >';
			if($closing_amount){
				$html.=number_format($closing_amount,2,'.','');
			}else{
				$html.="0.00";
			}
			$html.='</td>';



			$cat_total_qty_opening+=$d_value['opening_qty'];
			$cat_total_amount_opening+=number_format($d_value['opening_price'],2,'.','');

			$cat_total_qty_receive+=$d_value['receive_qty'];
			$cat_total_amount_receive+=number_format($d_value['receive_qty_price'],2,'.','');

			$cat_total_qty_local_sale+=$d_value['sales_qty'];
			$cat_total_amount_local_sale+=number_format($d_value['product_amount'],2,'.','');

			$cat_total_qty_network_sale+=$d_value['global_sales_qty'];
			$cat_total_amount_network_sale+=number_format($d_value['global_product_amount'],2,'.','');

			$cat_total_qty_network_delivery+=$d_value['global_delivery_sales_qty'];
			$cat_total_amount_network_delivery+=number_format($d_value['global_delivery_product_amount'],2,'.','');

			$cat_total_qty_network_return+=$d_value['return_sales_qty'];
			$cat_total_amount_network_return+=number_format($d_value['return_product_amount'],2,'.','');

			$cat_total_qty_network_closing+=$closing_qty;
			$cat_total_amount_network_closing+=number_format($closing_amount,2,'.','');


			$html.='</tr>';

			$serial_item++;

		}

		$cat_total_total_qty_opening+=$cat_total_qty_opening;
		$cat_total_total_amount_opening+=$cat_total_amount_opening;

		$cat_total_total_qty_receive+=$cat_total_qty_receive;
		$cat_total_total_amount_receive+=$cat_total_amount_receive;

		$cat_total_total_qty_local_sale+=$cat_total_qty_local_sale;
		$cat_total_total_amount_local_sale+=$cat_total_amount_local_sale;

		$cat_total_total_qty_network_sale+=$cat_total_qty_network_sale;
		$cat_total_total_amount_network_sale+=$cat_total_amount_network_sale;

		$cat_total_total_qty_network_delivery+=$cat_total_qty_network_delivery;
		$cat_total_total_amount_network_delivery+=$cat_total_amount_network_delivery;

		$cat_total_total_qty_network_return+=$cat_total_qty_network_return;
		$cat_total_total_amount_network_return+=$cat_total_amount_network_return;

		$cat_total_total_qty_network_closing+=$cat_total_qty_network_closing;
		$cat_total_total_amount_network_closing+=$cat_total_amount_network_closing;



		$html.='<tr>';
		$html.='<td style="background-color:#E0DBDB;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: 10pt;" >Sub-Total ('.$cat_key.')&nbsp;</td>';

		$html.='<td style="background-color:#E0DBDB;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: 10pt; " >'.$cat_total_qty_opening.'</td>';
		$html.='<td style="background-color:#E0DBDB;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: 10pt;" >'.number_format($cat_total_amount_opening,2,'.','').'</td>';

		$html.='<td style="background-color:#E0DBDB;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: 10pt; " >'.$cat_total_qty_receive.'</td>';
		$html.='<td style="background-color:#E0DBDB;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: 10pt;" >'.number_format($cat_total_amount_receive,2,'.','').'</td>';

		$html.='<td style="background-color:#E0DBDB;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: 10pt; " >'.$cat_total_qty_local_sale.'</td>';
		$html.='<td style="background-color:#E0DBDB;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: 10pt;" >'.number_format($cat_total_amount_local_sale,2,'.','').'</td>';

		$html.='<td style="background-color:#E0DBDB;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: 10pt; " >'.$cat_total_qty_network_sale.'</td>';
		$html.='<td style="background-color:#E0DBDB;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: 10pt;" >'.number_format($cat_total_amount_network_sale,2,'.','').'</td>';

		$html.='<td style="background-color:#E0DBDB;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: 10pt; " >'.$cat_total_qty_network_delivery.'</td>';
		$html.='<td style="background-color:#E0DBDB;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: 10pt;" >'.number_format($cat_total_amount_network_delivery,2,'.','').'</td>';

		$html.='<td style="background-color:#E0DBDB;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: 10pt; " >'.$cat_total_qty_network_return.'</td>';
		$html.='<td style="background-color:#E0DBDB;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: 10pt;" >'.number_format($cat_total_amount_network_return,2,'.','').'</td>';

		$html.='<td style="background-color:#E0DBDB;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: 10pt; " >'.$cat_total_qty_network_closing.'</td>';
		$html.='<td style="background-color:#E0DBDB;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: 10pt;" >'.number_format($cat_total_amount_network_closing,2,'.','').'</td>';

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


	$cat_grand_total_qty_opening+=$cat_total_total_qty_opening;
	$cat_grand_total_amount_opening+=$cat_total_total_amount_opening;

	$cat_grand_total_qty_receive+=$cat_total_total_qty_receive;
	$cat_grand_total_amount_receive+=$cat_total_total_amount_receive;

	$cat_grand_total_qty_local_sale+=$cat_total_total_qty_local_sale;
	$cat_grand_total_amount_local_sale+=$cat_total_total_amount_local_sale;

	$cat_grand_total_qty_network_sale+=$cat_total_total_qty_network_sale;
	$cat_grand_total_amount_network_sale+=$cat_total_total_amount_network_sale;

	$cat_grand_total_qty_network_delivery+=$cat_total_total_qty_network_delivery;
	$cat_grand_total_amount_network_delivery+=$cat_total_total_amount_network_delivery;

	$cat_grand_total_qty_network_return+=$cat_total_total_qty_network_return;
	$cat_grand_total_amount_network_return+=$cat_total_total_amount_network_return;

	$cat_grand_total_qty_network_closing+=$cat_total_total_qty_network_closing;
	$cat_grand_total_amount_network_closing+=$cat_total_total_amount_network_closing;


	$cat_total_total_qty_opening=0;
	$cat_total_total_amount_opening=0;

	$cat_total_total_qty_receive=0;
	$cat_total_total_amount_receive=0;

	$cat_total_total_qty_local_sale=0;
	$cat_total_total_amount_local_sale=0;

	$cat_total_total_qty_network_sale=0;
	$cat_total_total_amount_network_sale=0;

	$cat_total_total_qty_network_delivery=0;
	$cat_total_total_amount_network_delivery=0;

	$cat_total_total_qty_network_return=0;
	$cat_total_total_amount_network_return=0;

	$cat_total_total_qty_network_closing=0;
	$cat_total_total_amount_network_closing=0;

	$root_serial++;
}


$html.='<tr>';
$html.='<td style="background-color:#CBC8C8;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: 10pt;" >Grand Total&nbsp;</td>';

$html.='<td style="background-color:#CBC8C8;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: 10pt; " >'.$cat_grand_total_qty_opening.'</td>';
$html.='<td style="background-color:#CBC8C8;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: 10pt;" >'.number_format($cat_grand_total_amount_opening,2,'.','').'</td>';

$html.='<td style="background-color:#CBC8C8;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: 10pt; " >'.$cat_grand_total_qty_receive.'</td>';
$html.='<td style="background-color:#CBC8C8;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: 10pt;" >'.number_format($cat_grand_total_amount_receive,2,'.','').'</td>';

$html.='<td style="background-color:#CBC8C8;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: 10pt; " >'.$cat_grand_total_qty_local_sale.'</td>';
$html.='<td style="background-color:#CBC8C8;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: 10pt;" >'.number_format($cat_grand_total_amount_local_sale,2,'.','').'</td>';

$html.='<td style="background-color:#CBC8C8;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: 10pt; " >'.$cat_grand_total_qty_network_sale.'</td>';
$html.='<td style="background-color:#CBC8C8;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: 10pt;" >'.number_format($cat_grand_total_amount_network_sale,2,'.','').'</td>';

$html.='<td style="background-color:#CBC8C8;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: 10pt; " >'.$cat_grand_total_qty_network_delivery.'</td>';
$html.='<td style="background-color:#CBC8C8;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: 10pt;" >'.number_format($cat_grand_total_amount_network_delivery,2,'.','').'</td>';

$html.='<td style="background-color:#CBC8C8;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: 10pt; " >'.$cat_grand_total_qty_network_return.'</td>';
$html.='<td style="background-color:#CBC8C8;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: 10pt;" >'.number_format($cat_grand_total_amount_network_return,2,'.','').'</td>';

$html.='<td style="background-color:#CBC8C8;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: 10pt; " >'.$cat_grand_total_qty_network_closing.'</td>';
$html.='<td style="background-color:#CBC8C8;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: 10pt;" >'.number_format($cat_grand_total_amount_network_closing,2,'.','').'</td>';

$html.='</tr>';

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

$total_credit=0;
$total_payment_shawpon=0;
foreach ($payment_data as $pay_key => $pay_value) {
	if($pay_value->payment_type=="Due"){
		$total_credit+=$pay_value->payment_amoount;
	}
	$total_payment_shawpon+=$pay_value->payment_amoount;
}
$grand_total_show=0;

$grand_total_show+=$cat_grand_total_amount_local_sale-$cat_grand_total_amount_network_return+$cat_grand_total_amount_network_sale;
// $grand_total_show-=$grand_total_discount;
$grand_total_show+=$grand_total_adjustment;
$grand_total_show+=$grand_total_vat;
$grand_total_show-=$total_credit;



$html.='<table width="100%" style="color: #000000;border: 0px solid #000000; border-collapse: collapse;">';

$html.='<tr>';

$html.='<td width="50%" align="left">';

$html.='<table width="60%" style="color: #000000;border: 1px solid #000000; border-collapse: collapse;">';
$html.='<tr>';

$html.='<td style="background-color:#CBC8C8;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: 10pt; " >Total Sale &nbsp;</td>';
$html.='<td style="background-color:#FFFFFF;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: 10pt;" >'.number_format($cat_grand_total_amount_local_sale-$cat_grand_total_amount_network_return+$cat_grand_total_amount_network_sale,2,'.','').'</td>';

$html.='</tr>';

$html.='<tr>';

$html.='<td style="background-color:#E0DBDB;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: 10pt; " >Total Discount &nbsp;</td>';
$html.='<td style="background-color:#FFFFFF;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: 10pt;" >'.number_format($grand_total_discount,2,'.','').'</td>';

$html.='</tr>';

$html.='<tr>';

$html.='<td style="background-color:#E0DBDB;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: 10pt; " >Total Adjustment &nbsp;</td>';
$html.='<td style="background-color:#FFFFFF;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: 10pt;" >'.number_format($total_payment_shawpon-$grand_total_show,2,'.','').'</td>';

$html.='</tr>';

$html.='<tr>';

$html.='<td style="background-color:#CBC8C8;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: 10pt; " >Total VAT &nbsp;</td>';
$html.='<td style="background-color:#FFFFFF;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: 10pt;" >'.number_format($grand_total_vat,2,'.','').'</td>';

$html.='</tr>';



$html.='<tr>';

$html.='<td style="background-color:#E0DBDB;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: 10pt; " >Total Credit &nbsp;</td>';
$html.='<td style="background-color:#FFFFFF;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: 10pt;" >'.number_format($total_credit,2,'.','').'</td>';

$html.='</tr>';


$html.='<tr>';

$html.='<td style="background-color:#CBC8C8;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: 10pt; " >Grand Total &nbsp;</td>';
$html.='<td style="background-color:#FFFFFF;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: 10pt;" >'.number_format($total_payment_shawpon,2,'.','').'</td>';

$html.='</tr>';

$html.='</table>';

$html.='</td>';

$html.='<td width="50%" align="right">';

$html.='<table width="60%" style="color: #000000;border: 1px solid #000000; border-collapse: collapse;">';
$html.='<tr>';
$html.='<td style="background-color:#CBC8C8;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: 10pt; " >Payment Type &nbsp;</td>';
$html.='<td style="background-color:#CBC8C8;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: 10pt;" >Amount</td>';
$html.='</tr>';

$total_payment=0;



foreach ($payment_data as $pay_key => $pay_value) {

	$html.='<tr>';
	$html.='<td style="background-color:#FFFFFF;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: 10pt; " >'.$pay_value->payment_type.'&nbsp;</td>';
	$html.='<td style="background-color:#FFFFFF;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: 10pt;" >'.number_format($pay_value->payment_amoount,2,'.','').'</td>';
	$html.='</tr>';

	$total_payment+=$pay_value->payment_amoount;
}
$html.='</table>';
$html.='</td>';
$html.='</tr>';

$html.='</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-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_details_report_'.LOCATION_CODE.'_'.$subtitle.'.pdf','I');

exit;
?>