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




$start_date=$data['start_date'];
$end_date=$data['end_date'];
$payment_data_reprint_show=$data['payment_data_reprint_show'];


define("BASEPATH",'../../../');
require_once (BASEPATH.'application/config/database.php');


function getData_shawpon_date_range(array $inputs,array $db_config)
{
	define("SERVICE_HOST",$db_config['default']['hostname']);
	define("SERVICE_USER",$db_config['default']['username']);
	define("SERVICE_PASSWORD",$db_config['default']['password']);
	define("SERVICE_DB",$db_config['default']['database']);

	$db = mysql_connect(SERVICE_HOST, SERVICE_USER, SERVICE_PASSWORD);
	mysql_select_db(SERVICE_DB, $db);

	$sql = "SELECT
b.stock_id,
c.description,
d.description AS cat_description,
c.category_id,
c.gender,
SUM(b.quantity_purchased) AS sales_qty,
SUM(b.item_unit_price) AS sale_amount,
SUM(b.total_sale_amount) AS product_amount,
SUM(b.vat_enable_price_after_discount) AS product_amount_for_vat,
SUM(b.vat_enable_price) AS product_vat_enable_price,
SUM(IF(b.discount_percent> 0,b.total_discount,0)) AS discount_amount,
SUM(IF(b.discount_percent= 0,b.total_discount,0)) AS adjustment_amount,
SUM(b.vat_enable_price) AS vat_enable_price,
SUM(b.vat_amount) AS vat_amount

FROM ospos_sales AS a
LEFT JOIN ospos_sales_items AS b ON a.sale_id=b.sale_id
LEFT JOIN stock_master AS c ON b.stock_id=c.stock_id
LEFT JOIN stock_category AS d ON c.category_id=d.category_id

WHERE DATE_FORMAT(a.sale_time,'%Y-%m-%d') BETWEEN '".$inputs['start_date']."' AND '".$inputs['end_date']."'
AND b.quantity_purchased>0 AND a.location='".LOCATION_CODE."' AND a.is_global='0'  AND a.acc_location='".LOCATION_CODE."'
GROUP BY c.category_id,c.description,c.gender ORDER BY c.description ASC ";

	$result = mysql_query($sql, $db);
	$i = 0;
	while ($row = mysql_fetch_object($result)) {
		$sales_data[] = (object)$row;
	}
	return $sales_data;
}


function getData_shawpon_return_date_range(array $inputs,array $db_config)
{


	define("SERVICE_HOST",$db_config['default']['hostname']);
	define("SERVICE_USER",$db_config['default']['username']);
	define("SERVICE_PASSWORD",$db_config['default']['password']);
	define("SERVICE_DB",$db_config['default']['database']);


	$db = mysql_connect(SERVICE_HOST, SERVICE_USER, SERVICE_PASSWORD);
	mysql_select_db(SERVICE_DB, $db);

	$sql = "SELECT
b.stock_id,
c.description,
d.description AS cat_description,
c.category_id,
c.gender,
SUM(b.quantity_purchased) AS sales_qty,
SUM(b.item_unit_price) AS sale_amount,
SUM(b.total_sale_amount) AS product_amount,
SUM(b.vat_enable_price_after_discount) AS product_amount_for_vat,
SUM(b.vat_enable_price) AS product_vat_enable_price,
SUM(IF(b.discount_percent> 0,b.total_discount,0)) AS discount_amount,
SUM(IF(b.discount_percent= 0,b.total_discount,0)) AS adjustment_amount,
SUM(b.vat_enable_price) AS vat_enable_price,
SUM(b.vat_amount) AS vat_amount

FROM ospos_sales AS a
LEFT JOIN ospos_sales_items AS b ON a.sale_id=b.sale_id
LEFT JOIN stock_master AS c ON b.stock_id=c.stock_id
LEFT JOIN stock_category AS d ON c.category_id=d.category_id

WHERE DATE_FORMAT(a.sale_time,'%Y-%m-%d') BETWEEN '".$inputs['start_date']."' AND '".$inputs['end_date']."'
AND b.quantity_purchased<0 AND a.location='".LOCATION_CODE."' AND a.acc_location='".LOCATION_CODE."'
GROUP BY c.category_id,c.description,c.gender ORDER BY c.description ASC ";



	$result = mysql_query($sql, $db);
	$i = 0;
	while ($row = mysql_fetch_object($result)) {
		$sales_data[] = $row;
	}
	return $sales_data;

}


function getData_global_sale_shawpon_date_range(array $inputs,array $db_config)
{


	define("SERVICE_HOST",$db_config['default']['hostname']);
	define("SERVICE_USER",$db_config['default']['username']);
	define("SERVICE_PASSWORD",$db_config['default']['password']);
	define("SERVICE_DB",$db_config['default']['database']);


	$db = mysql_connect(SERVICE_HOST, SERVICE_USER, SERVICE_PASSWORD);
	mysql_select_db(SERVICE_DB, $db);



	$sql = "SELECT
b.stock_id,
c.description,
d.description AS cat_description,
c.category_id,
c.gender,
SUM(b.quantity_purchased) AS sales_qty,
SUM(b.item_unit_price) AS sale_amount,
SUM(b.total_sale_amount) AS product_amount,
SUM(b.vat_enable_price_after_discount) AS product_amount_for_vat,
SUM(b.vat_enable_price) AS product_vat_enable_price,
SUM(IF(b.discount_percent> 0,b.total_discount,0)) AS discount_amount,
SUM(IF(b.discount_percent= 0,b.total_discount,0)) AS adjustment_amount,
SUM(b.vat_enable_price) AS vat_enable_price,
SUM(b.vat_amount) AS vat_amount

FROM ospos_sales AS a
LEFT JOIN ospos_sales_items AS b ON a.sale_id=b.sale_id
LEFT JOIN stock_master AS c ON b.stock_id=c.stock_id
LEFT JOIN stock_category AS d ON c.category_id=d.category_id

WHERE DATE_FORMAT(a.sale_time,'%Y-%m-%d') BETWEEN '".$inputs['start_date']."' AND '".$inputs['end_date']."'
AND b.quantity_purchased>0 AND a.is_global='1'  AND a.acc_location='".LOCATION_CODE."' AND a.receive_location!='".LOCATION_CODE."'
GROUP BY c.category_id,c.description,c.gender ORDER BY c.description ASC ";

	$result = mysql_query($sql, $db);
	$i = 0;
	while ($row = mysql_fetch_object($result)) {
		$sales_data[] = $row;
	}
	return $sales_data;

}


function getData_global_delivery_shawpon_date_range(array $inputs,array $db_config)
{


	define("SERVICE_HOST",$db_config['default']['hostname']);
	define("SERVICE_USER",$db_config['default']['username']);
	define("SERVICE_PASSWORD",$db_config['default']['password']);
	define("SERVICE_DB",$db_config['default']['database']);


	$db = mysql_connect(SERVICE_HOST, SERVICE_USER, SERVICE_PASSWORD);
	mysql_select_db(SERVICE_DB, $db);


	$sql = "SELECT
b.stock_id,
c.description,
d.description AS cat_description,
c.category_id,
c.gender,
SUM(b.quantity_purchased) AS sales_qty,
SUM(b.item_unit_price) AS sale_amount,
SUM(b.total_sale_amount) AS product_amount,
SUM(b.vat_enable_price_after_discount) AS product_amount_for_vat,
SUM(b.vat_enable_price) AS product_vat_enable_price,
SUM(IF(b.discount_percent> 0,b.total_discount,0)) AS discount_amount,
SUM(IF(b.discount_percent= 0,b.total_discount,0)) AS adjustment_amount,
SUM(b.vat_enable_price) AS vat_enable_price,
SUM(b.vat_amount) AS vat_amount

FROM ospos_sales AS a
LEFT JOIN ospos_sales_items AS b ON a.sale_id=b.sale_id
LEFT JOIN stock_master AS c ON b.stock_id=c.stock_id
LEFT JOIN stock_category AS d ON c.category_id=d.category_id

WHERE DATE_FORMAT(a.sale_time,'%Y-%m-%d') BETWEEN '".$inputs['start_date']."' AND '".$inputs['end_date']."'
AND b.quantity_purchased>0 AND a.is_global='1'  AND a.receive_location='".LOCATION_CODE."' AND a.acc_location!='".LOCATION_CODE."'
GROUP BY c.category_id,c.description,c.gender ORDER BY c.description ASC ";

	$result = mysql_query($sql, $db);
	$i = 0;
	while ($row = mysql_fetch_object($result)) {
		$sales_data[] = $row;
	}

	return $sales_data;
}



function getData_receive_shawpon_date_range(array $inputs,array $db_config)
{

	define("SERVICE_HOST",$db_config['default']['hostname']);
	define("SERVICE_USER",$db_config['default']['username']);
	define("SERVICE_PASSWORD",$db_config['default']['password']);
	define("SERVICE_DB",$db_config['default']['database']);


	$db = mysql_connect(SERVICE_HOST, SERVICE_USER, SERVICE_PASSWORD);
	mysql_select_db(SERVICE_DB, $db);


	$sql = "SELECT a.stock_id,a.description,a.category_id,a.gender,c.description AS cat_description,SUM(b.qty) AS qty,b.price
FROM stock_moves AS b
LEFT JOIN stock_master AS a ON b.stock_id=a.stock_id
LEFT JOIN stock_category AS c ON a.category_id=c.category_id
WHERE b.loc_code='".LOCATION_CODE."' AND b.tran_date>='".$inputs['start_date']."' AND b.tran_date<='".$inputs['end_date']."'  AND b.receive_qty>0
GROUP BY b.stock_id,b.lot_no,b.price";

	$result = mysql_query($sql, $db);
	$i = 0;
	while ($row = mysql_fetch_object($result)) {
		$sales_data[] = $row;
	}

	return $sales_data;
}


function getData_shawpon_opening_range($start_date,$unique_items_cat,$unique_items_gender,$unique_items_description,array $db_config)
{


	define("SERVICE_HOST",$db_config['default']['hostname']);
	define("SERVICE_USER",$db_config['default']['username']);
	define("SERVICE_PASSWORD",$db_config['default']['password']);
	define("SERVICE_DB",$db_config['default']['database']);


	$db = mysql_connect(SERVICE_HOST, SERVICE_USER, SERVICE_PASSWORD);
	mysql_select_db(SERVICE_DB, $db);


	$unique_items_cat_str="'".implode("','",$unique_items_cat)."'";
	$unique_items_gender_str="'".implode("','",$unique_items_gender)."'";
	$unique_items_description_str="'".implode("','",$unique_items_description)."'";



	$sql = "SELECT a.stock_id,a.description,a.category_id,a.gender,c.description AS cat_description,
SUM(b.qty) AS qty,b.price

FROM stock_master AS a
LEFT JOIN stock_moves AS b ON a.stock_id=b.stock_id
LEFT JOIN stock_category AS c ON a.category_id=c.category_id
WHERE a.category_id IN (".$unique_items_cat_str.") 
AND a.gender IN (".$unique_items_gender_str.") 
AND a.description IN (".$unique_items_description_str.")
AND b.loc_code='".LOCATION_CODE."' AND b.tran_date<'".$start_date."'
GROUP BY b.stock_id,b.lot_no,b.price";


	$result = mysql_query($sql, $db);
	$i = 0;
	while ($row = mysql_fetch_object($result)) {
		$sales_data[] = $row;
	}

	return $sales_data;
}



function getData_payment_shawpon_date_range(array $inputs,array $db_config)
{


	define("SERVICE_HOST",$db_config['default']['hostname']);
	define("SERVICE_USER",$db_config['default']['username']);
	define("SERVICE_PASSWORD",$db_config['default']['password']);
	define("SERVICE_DB",$db_config['default']['database']);


	$db = mysql_connect(SERVICE_HOST, SERVICE_USER, SERVICE_PASSWORD);
	mysql_select_db(SERVICE_DB, $db);


	$sql = "SELECT
b.payment_type,SUM(b.payment_amount) AS payment_amoount
FROM ospos_sales AS a
LEFT JOIN ospos_sales_payments AS b ON a.sale_id=b.sale_id
WHERE DATE_FORMAT(a.sale_time,'%Y-%m-%d') BETWEEN '".$inputs['start_date']."' AND '".$inputs['end_date']."'
AND a.acc_location='".LOCATION_CODE."'
GROUP BY b.payment_type";

	$result = mysql_query($sql, $db);
	$i = 0;
	while ($row = mysql_fetch_object($result)) {
		$sales_data[] = $row;
	}

	return $sales_data;

}

function getData_payment_shawpon_date_range_reprint(array $inputs,array $db_config)
{


	define("SERVICE_HOST",$db_config['default']['hostname']);
	define("SERVICE_USER",$db_config['default']['username']);
	define("SERVICE_PASSWORD",$db_config['default']['password']);
	define("SERVICE_DB",$db_config['default']['database']);


	$db = mysql_connect(SERVICE_HOST, SERVICE_USER, SERVICE_PASSWORD);
	mysql_select_db(SERVICE_DB, $db);


	$sql = "SELECT
b.payment_type,SUM(b.payment_amount) AS payment_amoount
FROM ospos_sales_reprint AS a
LEFT JOIN ospos_sales_payments_reprint AS b ON a.sale_id=b.sale_id
WHERE DATE_FORMAT(a.sale_time,'%Y-%m-%d') BETWEEN '".$inputs['start_date']."' AND '".$inputs['end_date']."'
AND a.acc_location='".LOCATION_CODE."'
GROUP BY b.payment_type";

	$result = mysql_query($sql, $db);
	$i = 0;
	while ($row = mysql_fetch_object($result)) {
		$sales_data[] = $row;
	}

	return $sales_data;

}


$report_data_current_sale = getData_shawpon_date_range(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => 'sales'),$db);
$report_data_current_return = getData_shawpon_return_date_range(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => 'sales'),$db);
$report_data_global_sale = getData_global_sale_shawpon_date_range(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => 'sales'),$db);
$report_data_global_delivery = getData_global_delivery_shawpon_date_range(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => 'sales'),$db);
$report_data_current_receive = getData_receive_shawpon_date_range(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => 'sales'),$db);




$stock_items_gender = array(
'fabric' => _("Fabric"),
'male' => _("Male"),
'female' => _("Female"),
'boy' => _("Boy"),
'girl' => _("Girl"),
'newlook' => _("New Look"),
'others' => _("Others"),
'' => _("Miscellaneous"),
'school' => _("School Fabric"),
);


$unique_items=array();
$unique_items_cat=array();
$unique_items_description=array();
$unique_items_gender=array();

$vat_enable_price_for_vat=0;

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

	$unique_items[$stock_items_gender[$value->gender]][$value->cat_description][$value->description]['sales_qty']=$unique_items[$stock_items_gender[$value->gender]][$value->cat_description][$value->description]['sales_qty']+$value->sales_qty;
	$unique_items[$stock_items_gender[$value->gender]][$value->cat_description][$value->description]['product_amount']=$unique_items[$stock_items_gender[$value->gender]][$value->cat_description][$value->description]['product_amount']+$value->product_vat_enable_price;
	$unique_items[$stock_items_gender[$value->gender]][$value->cat_description][$value->description]['discount_amount']=$unique_items[$stock_items_gender[$value->gender]][$value->cat_description][$value->description]['discount_amount']+$value->discount_amount;
	$unique_items[$stock_items_gender[$value->gender]][$value->cat_description][$value->description]['adjustment_amount']=$unique_items[$stock_items_gender[$value->gender]][$value->cat_description][$value->description]['adjustment_amount']+$value->adjustment_amount;
	$unique_items[$stock_items_gender[$value->gender]][$value->cat_description][$value->description]['vat_enable_price']=$unique_items[$stock_items_gender[$value->gender]][$value->cat_description][$value->description]['vat_enable_price']+$value->vat_enable_price;
	$vat_enable_price_for_vat=$vat_enable_price_for_vat+$value->product_amount_for_vat;
	$unique_items[$stock_items_gender[$value->gender]][$value->cat_description][$value->description]['vat_amount']=$unique_items[$stock_items_gender[$value->gender]][$value->cat_description][$value->description]['vat_amount']+$value->vat_amount;

	$unique_items_cat[$value->category_id]=$value->category_id;
	$unique_items_description[$value->description]=$value->description;
	$unique_items_gender[$value->gender]=$value->gender;
}




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

	$unique_items[$stock_items_gender[$value->gender]][$value->cat_description][$value->description]['return_sales_qty']=$unique_items[$stock_items_gender[$value->gender]][$value->cat_description][$value->description]['return_sales_qty']+abs($value->sales_qty);
	$unique_items[$stock_items_gender[$value->gender]][$value->cat_description][$value->description]['return_product_amount']=$unique_items[$stock_items_gender[$value->gender]][$value->cat_description][$value->description]['return_product_amount']+$value->product_vat_enable_price;
	$unique_items[$stock_items_gender[$value->gender]][$value->cat_description][$value->description]['return_discount_amount']=$unique_items[$stock_items_gender[$value->gender]][$value->cat_description][$value->description]['return_discount_amount']+$value->discount_amount;
	$unique_items[$stock_items_gender[$value->gender]][$value->cat_description][$value->description]['return_adjustment_amount']=$unique_items[$stock_items_gender[$value->gender]][$value->cat_description][$value->description]['return_adjustment_amount']+$value->adjustment_amount;
	$unique_items[$stock_items_gender[$value->gender]][$value->cat_description][$value->description]['return_vat_enable_price']=$unique_items[$stock_items_gender[$value->gender]][$value->cat_description][$value->description]['return_vat_enable_price']+$value->vat_enable_price;
	$vat_enable_price_for_vat=$vat_enable_price_for_vat+$value->product_amount_for_vat;
	$unique_items[$stock_items_gender[$value->gender]][$value->cat_description][$value->description]['return_vat_amount']=$unique_items[$stock_items_gender[$value->gender]][$value->cat_description][$value->description]['return_vat_amount']+$value->vat_amount;
	$unique_items_cat[$value->category_id]=$value->category_id;
	$unique_items_description[$value->description]=$value->description;
	$unique_items_gender[$value->gender]=$value->gender;
}




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

	$unique_items[$stock_items_gender[$value->gender]][$value->cat_description][$value->description]['global_sales_qty']=$unique_items[$stock_items_gender[$value->gender]][$value->cat_description][$value->description]['global_sales_qty']+abs($value->sales_qty);
	$unique_items[$stock_items_gender[$value->gender]][$value->cat_description][$value->description]['global_product_amount']=$unique_items[$stock_items_gender[$value->gender]][$value->cat_description][$value->description]['global_product_amount']+$value->product_vat_enable_price;
	$unique_items[$stock_items_gender[$value->gender]][$value->cat_description][$value->description]['global_discount_amount']=$unique_items[$stock_items_gender[$value->gender]][$value->cat_description][$value->description]['global_discount_amount']+$value->discount_amount;
	$unique_items[$stock_items_gender[$value->gender]][$value->cat_description][$value->description]['global_adjustment_amount']=$unique_items[$stock_items_gender[$value->gender]][$value->cat_description][$value->description]['global_adjustment_amount']+$value->adjustment_amount;
	$unique_items[$stock_items_gender[$value->gender]][$value->cat_description][$value->description]['global_vat_enable_price']=$unique_items[$stock_items_gender[$value->gender]][$value->cat_description][$value->description]['global_vat_enable_price']+$value->vat_enable_price;
	$vat_enable_price_for_vat=$vat_enable_price_for_vat+$value->product_amount_for_vat;
	$unique_items[$stock_items_gender[$value->gender]][$value->cat_description][$value->description]['global_vat_amount']=$unique_items[$stock_items_gender[$value->gender]][$value->cat_description][$value->description]['global_vat_amount']+$value->vat_amount;
	$unique_items_cat[$value->category_id]=$value->category_id;
	$unique_items_description[$value->description]=$value->description;
	$unique_items_gender[$value->gender]=$value->gender;
}


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

	$unique_items[$stock_items_gender[$value->gender]][$value->cat_description][$value->description]['global_delivery_sales_qty']=$unique_items[$stock_items_gender[$value->gender]][$value->cat_description][$value->description]['global_delivery_sales_qty']+abs($value->sales_qty);
	$unique_items[$stock_items_gender[$value->gender]][$value->cat_description][$value->description]['global_delivery_product_amount']=$unique_items[$stock_items_gender[$value->gender]][$value->cat_description][$value->description]['global_delivery_product_amount']+$value->product_vat_enable_price;
	$unique_items[$stock_items_gender[$value->gender]][$value->cat_description][$value->description]['global_delivery_discount_amount']=$unique_items[$stock_items_gender[$value->gender]][$value->cat_description][$value->description]['global_delivery_discount_amount']+$value->discount_amount;
	$unique_items[$stock_items_gender[$value->gender]][$value->cat_description][$value->description]['global_delivery_adjustment_amount']=$unique_items[$stock_items_gender[$value->gender]][$value->cat_description][$value->description]['global_delivery_adjustment_amount']+$value->adjustment_amount;
	$unique_items[$stock_items_gender[$value->gender]][$value->cat_description][$value->description]['global_delivery_vat_enable_price']=$unique_items[$stock_items_gender[$value->gender]][$value->cat_description][$value->description]['global_delivery_vat_enable_price']+$value->vat_enable_price;
	$vat_enable_price_for_vat=$vat_enable_price_for_vat+$value->product_amount_for_vat;
	$unique_items_cat[$value->category_id]=$value->category_id;
	$unique_items_description[$value->description]=$value->description;
	$unique_items_gender[$value->gender]=$value->gender;
}

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

	$unique_items[$stock_items_gender[$value->gender]][$value->cat_description][$value->description]['receive_qty']=$unique_items[$stock_items_gender[$value->gender]][$value->cat_description][$value->description]['receive_qty']+$value->qty;
	$unique_items[$stock_items_gender[$value->gender]][$value->cat_description][$value->description]['receive_qty_price']=$unique_items[$stock_items_gender[$value->gender]][$value->cat_description][$value->description]['receive_qty_price']+($value->price*$value->qty);

	$unique_items_cat[$value->category_id]=$value->category_id;
	$unique_items_description[$value->description]=$value->description;
	$unique_items_gender[$value->gender]=$value->gender;
}





$report_data_opening = getData_shawpon_opening_range($start_date,$unique_items_cat,$unique_items_gender,$unique_items_description,$db);


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

	$unique_items[$stock_items_gender[$value->gender]][$value->cat_description][$value->description]['opening_qty']=$unique_items[$stock_items_gender[$value->gender]][$value->cat_description][$value->description]['opening_qty']+$value->qty;
	$unique_items[$stock_items_gender[$value->gender]][$value->cat_description][$value->description]['opening_price']=$unique_items[$stock_items_gender[$value->gender]][$value->cat_description][$value->description]['opening_price']+($value->price*$value->qty);

}


$report_data_current_payment = getData_payment_shawpon_date_range(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => 'sales'),$db);
$report_data_current_payment_reprint = getData_payment_shawpon_date_range_reprint(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => 'sales'),$db);




$data = $unique_items;
$payment_data =$report_data_current_payment;
$payment_data_reprint =$report_data_current_payment_reprint;
$payment_data_real =$report_data_current_payment;

if($payment_data_reprint_show==1){
	$payment_data=$payment_data_reprint;
}

$subtitle_array=explode("#",$subtitle);
$subtitle=$subtitle_array[0];
$vat_enable_price_for_vat=$subtitle_array[1];


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+=$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']*-1),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']*-1),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>';

if($payment_data_reprint_show==1){
	$total_payment_shawpon_mrp=0;
	foreach ($payment_data_real as $pay_key => $pay_value) {
		if($pay_value->payment_type=="Due"){
			$total_credit+=$pay_value->payment_amoount;
		}
		$total_payment_shawpon_mrp+=$pay_value->payment_amoount;
	}
	$grand_total_show_mrp=0;

	$grand_total_show_mrp+=$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_mrp+=$grand_total_adjustment;
	$grand_total_show_mrp+=$grand_total_vat;
	$grand_total_show_mrp-=$total_credit;
	$show_adjustment=$total_payment_shawpon_mrp-$grand_total_show_mrp+round($grand_total_discount);
}


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


if($payment_data_reprint_show==0){
	$show_adjustment=$total_payment_shawpon-$grand_total_show+round($grand_total_discount);
}



$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 VATable 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($vat_enable_price_for_vat,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 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($show_adjustment,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 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:#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 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:#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; " >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+round($grand_total_discount),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:right;font-size: 10pt; " >Payment/Discount 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;
$total_cash_and_others_payment=0;
$total_sales_discount=0;
$sales_discount_array=array();



foreach ($payment_data as $pay_key => $pay_value) {
	if(trim($pay_value->payment_type)!=""){
		$payment_type_show=$pay_value->payment_type;

		if($pay_value->payment_type=="Purchase Discount"){
			$payment_type_show="Sales Discount";

			$sales_discount_array[$pay_value->payment_type]['name']=$payment_type_show;
			$sales_discount_array[$pay_value->payment_type]['amount']=$pay_value->payment_amoount;

			$total_sales_discount+=$pay_value->payment_amoount;

		}elseif($pay_value->payment_type=="Purchase Bonus"){
			$payment_type_show="Special Sales Discount";

			$sales_discount_array[$pay_value->payment_type]['name']=$payment_type_show;
			$sales_discount_array[$pay_value->payment_type]['amount']=$pay_value->payment_amoount;
			$total_sales_discount+=$pay_value->payment_amoount;
		}else{
			$total_cash_and_others_payment+=$pay_value->payment_amoount;

			$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; " >'.$payment_type_show.'&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.='<tr>';
$html.='<td style="background-color:#DED2CD;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 Payment &nbsp;</td>';
$html.='<td style="background-color:#DED2CD;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_cash_and_others_payment,2,'.','').'</td>';
$html.='</tr>';


if(round($grand_total_discount)){
	$sales_discount_array["Dealer/Clearance Discount"]['name']="Dealer/Clearance Discount";
	$sales_discount_array["Dealer/Clearance Discount"]['amount']=round($grand_total_discount);
	$total_sales_discount+=round($grand_total_discount);
	$total_payment+=round($grand_total_discount);
}

foreach ($sales_discount_array as $key => $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; " >'.$value['name'].'&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($value['amount'],2,'.','').'</td>';
	$html.='</tr>';
}


$html.='<tr>';
$html.='<td style="background-color:#DED2CD;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:#DED2CD;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_sales_discount,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 Payment & Discount &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;" >'.number_format($total_payment,2,'.','').'</td>';
$html.='</tr>';

$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('sales_summary_report_'.LOCATION_CODE.'_'.$subtitle.'.pdf','I');

exit;
?>