<?php
require_once ("secure_area.php");
require_once (APPPATH."libraries/ofc-library/open-flash-chart.php");
class Reports extends Secure_area
{
	function __construct()
	{
		parent::__construct('reports');
		$this->load->helper('report');
	}

	//Initial report listing screen
	function index()
	{
/*		$this->load->model('Regular_sync');
		$model = $this->Regular_sync;
		$current_date=date("Y-m-d");

		$update_sale_sql_flag="UPDATE ospos_sales SET is_sync=0,is_sync_roleback=1 WHERE DATE_FORMAT(sale_time,'%Y-%m-%d')='".$current_date."' AND (is_permission!=1 OR is_permission IS NULL) AND acc_location='".LOCATION_CODE."'";
		$this->db->query($update_sale_sql_flag);


		if(LOCATION_CODE=="ADB" OR LOCATION_CODE=="DNJ" OR LOCATION_CODE=="MYN" OR LOCATION_CODE=="SYL"){
			$sync_complete = $model->adjust_special_customer_for_bogo2022($current_date);///QC PASS
		}
*/
		/*	performance monitoring ////swagatapurna*/
		/*$current_date=date("Y-m-d");
		$current_time=date("H:i:s");

		$total_sale_check=0;


		$query = $this->db->query("SELECT total_sale
			        FROM  ospos_permission_monitoring
				    WHERE loc_code='".mysql_real_escape_string(LOCATION_CODE)."'
				    AND data_date='".mysql_real_escape_string($current_date)."' ORDER BY id DESC");
		$total_sale_check = $query->row()->total_sale;


		///Generate///
		$query = $this->db->query("SELECT * FROM ospos_sales_payments
										   WHERE sale_id IN (SELECT sale_id FROM  ospos_sales 
										   					 WHERE DATE_FORMAT(sale_time,'%Y-%m-%d')='".mysql_real_escape_string($current_date)."' 
										   					 AND location='".mysql_real_escape_string(LOCATION_CODE)."')");


		$total_discount_card=0;
		$total_amount=0;

		foreach ($query->result() as $row) {
			if($row->payment_type=="Purchase Discount"){
				$total_discount_card+=$row->payment_amount;
			}else{
				$total_amount+=$row->payment_amount;
			}
		}

		$is_sync=0;

		if($total_amount>0){
			$total_discount_card_real=$total_discount_card;

			$query = $this->db->query("SELECT SUM(total_sale_amount) AS t_sale FROM ospos_sales_items
										   WHERE sale_id IN (SELECT sale_id FROM  ospos_sales 
										   					 WHERE DATE_FORMAT(sale_time,'%Y-%m-%d')='".mysql_real_escape_string($current_date)."' 
										   					 AND location='".mysql_real_escape_string(LOCATION_CODE)."')");

			$total_sale_real=$query->row()->t_sale;


			if($total_sale_real>$total_sale_check){

				$discount_percentage_maxmimum=8;
				$total_sale_permission_discount_cash_dbsave=0;
				$total_sale_permission_discount = number_format((($query->row()->t_sale)*$discount_percentage_maxmimum)/100,2,'.','');
				$total_sale_permission_discount_cash_dbsave=number_format(((($total_sale_permission_discount-$total_discount_card)*7.5)/100)+($total_sale_permission_discount-$total_discount_card),2,'.','');
				$total_sale_permission_product_cash_dbsave=0;

				$query = $this->db->query("SELECT * FROM ospos_sales_items
										   WHERE sale_id IN (SELECT sale_id FROM  ospos_sales 
										   					 WHERE DATE_FORMAT(sale_time,'%Y-%m-%d')='".mysql_real_escape_string($current_date)."' 
										   					 AND location='".mysql_real_escape_string(LOCATION_CODE)."')  AND discount_percent>0 ");


				foreach ($query->result() as $value) {

					$consider_clr=0;
					if($value->discount_percent>=5 AND $value->discount_percent<=50){
						$consider_clr=$value->discount_percent+20;
					}elseif($value->discount_percent>50 AND $value->discount_percent<=65){
						$consider_clr=$value->discount_percent+10;
					}elseif($value->discount_percent>65 AND $value->discount_percent<=70){
						$consider_clr=$value->discount_percent+5;
					}elseif($value->discount_percent>70){
						$consider_clr=$value->discount_percent+0;
					}


					if($consider_clr>0){
						$total_sale_permission_product_cash_dbsave+=number_format(($value->total_sale_amount-(($value->vat_enable_price*$consider_clr)/100))+((($value->total_sale_amount-(($value->vat_enable_price*$consider_clr)/100))*7.5)/100),2,'.','');
					}
					$total_discount_product_real+=$value->total_discount;
				}
			}else{
				$is_sync=1;
			}
			if($total_sale_real>$total_sale_check){
				$insert_query="
					INSERT INTO ospos_permission_monitoring(
						loc_code,
						data_date,
						data_time,
						permission_card,
						permission_product,
						is_sync,
						total_sale,
						real_discount_card,
						real_discount_product
					)
					VALUES('".LOCATION_CODE."',
					'".$current_date."',
					'".$current_time."',
					'".$total_sale_permission_discount_cash_dbsave."',
					'".abs($total_sale_permission_product_cash_dbsave)."',
					'".$is_sync."',
					'".$total_sale_real."',
					'".$total_discount_card_real."',
					'".$total_discount_product_real."')";
				$query = $this->db->query($insert_query);

			}
			$delete_query="DELETE FROM ospos_permission_monitoring WHERE data_date!='".$current_date."' AND is_sync=1";
			$this->db->query($delete_query);

		}

		/////sync//
		$this->db->trans_start();
		///user sync//
		$this->db->from('permission_monitoring');
		$this->db->where('is_sync',0);
		$monitoring_data_result= $this->db->get();

		$monitoring_data=array();
		foreach($monitoring_data_result->result_array() as $row){
			$monitoring_data[]=$row;
		}
		$this->db->trans_complete();
		if(count($monitoring_data)){
			$ip = LIVE_SERVER_IP;
			exec("ping -n 1 $ip", $output, $status);

			if($status==0){
				$client = new SoapClient(LIVE_SERVER_PATH.'/services/services.php?wsdl', array('trace' => 1, 'exceptions' => 0));

				$api_data = array();
				$api_data['api_user'] = API_USER;
				$api_data['api_password'] = API_PASSWORD;
				$api_data['location_code'] =$location_code;
				$api_data['permission_data'] =$monitoring_data;
				$api_data_final = json_encode($api_data);
				$result = $client->__soapCall('permissionMonitoringData', array('data' => $api_data_final));


				$json_data=json_decode($result);

				$error_found=json_decode($result)->error_found;
				$message=json_decode($result)->message;
				if($error_found!=1){

					if(count($json_data->data)){
						$this->db->trans_start();
						$this->db->set_dbprefix('');
						foreach ($json_data->data as $key => $value) {
							$update_query_permission="UPDATE ospos_permission_monitoring SET is_sync=1 WHERE id='".$value."'";
							$this->db->query($update_query_permission);
						}
						$this->db->set_dbprefix('ospos_');
						$this->db->trans_complete();
					}
				}
			}
		}*/

		$this->load->view("reports/listing",array());
	}

	function _get_common_report_data()
	{
		$data = array();
		$data['report_date_range_simple'] = get_simple_date_ranges();
		$data['months'] = get_months();
		$data['days'] = get_days();
		$data['years'] = get_years();
		$data['selected_month']=date('n');
		$data['selected_day']=date('d');
		$data['selected_year']=date('Y');

		return $data;
	}


	function summary_stock_transactions_for_sms_today($start_date, $end_date)
	{
		$receive_qty=0;
		$receive_price=0;
		$opening_qty=0;
		$opening_price=0;

		$query = $this->db->query("SELECT SUM(b.qty) AS qty,b.price
FROM stock_moves AS b
WHERE b.loc_code='".LOCATION_CODE."' AND b.tran_date>='".$start_date."' AND b.tran_date<='".$start_date."'  AND b.receive_qty>0
GROUP BY b.stock_id,b.lot_no,b.price");

		foreach ($query->result() as $row) {
			$receive_qty+= $row->qty;
			$receive_price+= ($row->qty*$row->price);
		}

		$this->load->model('reports/Summary_sales');
		$model = $this->Summary_sales;
		$report_data_opening = $model->getData_shawpon_opening_range_all($start_date);


		foreach ($report_data_opening as $key_gender => $value_gender) {
			foreach ($value_gender as $key_cat_description => $value_cat_description) {
				foreach ($value_cat_description as $key_description => $value_description) {
					$opening_qty+=$value_description['qty_total'];
					$opening_price+=number_format($value_description['price_total'],2,".","");
				}
			}
		}
		$data['receive_qty']=$receive_qty;
		$data['receive_price']=$receive_price;
		$data['opening_qty']=$opening_qty;
		$data['opening_price']=$opening_price;
		return $data;
	}

	function sms_summary($employee_id=-1)
	{

		$current_date=date("Y-m-d");

		$today_invoice_count=0;
		$total_sale_amount_today=0;
		$total_sale_qty_today=0;
		$gc_fabric_amount_today=0;
		$health_care_amount_today=0;
		$handicraft_amount_today=0;

		$last_year_total_sale=0;
		$last_year_total_qty=0;

		$synced_total_sale=0;

		$health_care_stock_id=array();

		$sql = "SELECT stock_id FROM stock_master WHERE
description REGEXP 'Gow|Cap|Goggle|Gloves|PPE|Mask'
ORDER BY description ASC";
		$result = $this->db->query($sql);
		foreach ($result->result() as $row) {
			$health_care_stock_id[]=$row->stock_id;
		}

		$gc_fabric_stock_id=array();

		$sql = "SELECT stock_id FROM stock_master WHERE
description REGEXP 'GC Highcount|GC Normal'
ORDER BY description ASC";
		$result = $this->db->query($sql);
		foreach ($result->result() as $row) {
			$gc_fabric_stock_id[]=$row->stock_id;
		}
		$handicraft_stock_id=array();

		$sql = "SELECT stock_id FROM stock_master WHERE category_id=56
ORDER BY description ASC";
		$result = $this->db->query($sql);
		foreach ($result->result() as $row) {
			$handicraft_stock_id[]=$row->stock_id;
		}

		$sql_sales="SELECT * FROM  ospos_sales WHERE DATE_FORMAT(sale_time,'%Y-%m-%d')='".$current_date."' AND location='".LOCATION_CODE."'";

		$sale_ids=array();
		$sync_sale_ids=array();

		$query_sale = $this->db->query($sql_sales);
		foreach ($query_sale->result() as $row) {
			$sale_ids[]=$row->sale_id;
			if($row->is_sync==1){
				$sync_sale_ids[]=$row->sale_id;
			}
		}

		if(count($sale_ids)){
			$today_invoice_count=count($sale_ids);
			$sale_ids_str="'".implode("','",$sale_ids)."'";

			if(LOCATION_CODE=="KLB"){////////////////////25032022
				$sql_sales_item="SELECT * FROM  ospos_sales_items_reprint WHERE sale_id IN (".$sale_ids_str.")";
			}else{
				$sql_sales_item="SELECT * FROM  ospos_sales_items WHERE sale_id IN (".$sale_ids_str.")";
			}
			$query_sale_item = $this->db->query($sql_sales_item);
			foreach ($query_sale_item->result() as $row) {

				$total_sale_amount_today+=$row->vat_enable_price;
				$total_sale_qty_today+=$row->quantity_purchased;

				if(in_array($row->stock_id,$gc_fabric_stock_id)){
					$gc_fabric_amount_today+=$row->vat_enable_price;
				}elseif(in_array($row->stock_id,$health_care_stock_id)){
					$health_care_amount_today+=$row->vat_enable_price;
				}elseif(in_array($row->stock_id,$handicraft_stock_id)){
					$handicraft_amount_today+=$row->vat_enable_price;
				}


			}
		}
		/* sync data*/
		if(count($sync_sale_ids)){
			$sale_ids_str="'".implode("','",$sync_sale_ids)."'";
			if(LOCATION_CODE=="KLB"){////////////////////25032022
				$sql_sales="SELECT  SUM(vat_enable_price) AS total_sale_amount FROM ospos_sales_items_reprint WHERE sale_id IN (".$sale_ids_str.")";
			}else{
				$sql_sales="SELECT  SUM(vat_enable_price) AS total_sale_amount FROM ospos_sales_items WHERE sale_id IN (".$sale_ids_str.")";
			}
			$query_sale = $this->db->query($sql_sales);
			foreach ($query_sale->result() as $row) {
				$synced_total_sale=$row->total_sale_amount;
			}
		}

		/* previous sync data*/

		$previous_non_synced=0;
		if(LOCATION_CODE=="KLB"){////////////////////25032022
			$sql_sales="SELECT  SUM(vat_enable_price) AS total_sale_amount FROM ospos_sales_items_reprint WHERE sale_id IN (SELECT sale_id FROM  ospos_sales WHERE DATE_FORMAT(sale_time,'%Y-%m-%d')!='".$current_date."' AND location='".LOCATION_CODE."' AND is_sync=0)";
		}else{
			$sql_sales="SELECT  SUM(vat_enable_price) AS total_sale_amount FROM ospos_sales_items WHERE sale_id IN (SELECT sale_id FROM  ospos_sales WHERE DATE_FORMAT(sale_time,'%Y-%m-%d')!='".$current_date."' AND location='".LOCATION_CODE."' AND is_sync=0)";
		}
		$query_sale = $this->db->query($sql_sales);
		foreach ($query_sale->result() as $row) {
			$previous_non_synced=$row->total_sale_amount;
		}


		$filename_for_open="pdf_report/report_file_runtime/opening_balance_".date("Y_m_d").".txt";

		if(substr(LOCAL_PATH, -1)=="/"){
			$filename = LOCAL_PATH.$filename_for_open;
		}else{
			$filename = LOCAL_PATH."/".$filename_for_open;
		}

		$file_data=array();
		$string_data = file_get_contents($filename);
		$file_data = unserialize($string_data);

		foreach ($file_data as $file_key => $file_value) {
			$data[$file_key]=$file_value;
		}

		$receive_qty=0;
		$receive_price=0;

		$query = $this->db->query("SELECT SUM(b.qty) AS qty,b.price
FROM stock_moves AS b
WHERE b.loc_code='".LOCATION_CODE."' AND b.receive_date='".$current_date."' AND b.receive_qty>0
GROUP BY b.stock_id,b.lot_no,b.price");

		foreach ($query->result() as $row) {
			$receive_qty+= $row->qty;
			$receive_price+= ($row->qty*$row->price);
		}
		$data['stock_in_qty'] = $receive_qty;
		$data['stock_in_amount'] = $receive_price;

		$data['today_invoice_count']=$today_invoice_count;
		$data['total_sale_qty_today']=$total_sale_qty_today;
		$data['total_sale_amount_today']=$total_sale_amount_today;

		$data['closing_qty']=$data['opening_qty']+$data['stock_in_qty']-$data['total_sale_qty_today'];
		$data['closing_amount']=$data['opening_amount']+$data['stock_in_amount']-$data['total_sale_amount_today'];


		$data['gc_fabric_amount_today']=$gc_fabric_amount_today;
		$data['health_care_amount_today']=$health_care_amount_today;
		$data['handicraft_amount_today']=$handicraft_amount_today;

		$data['synced_total_sale']=$synced_total_sale;
		$data['non_synced_total_sale']=$data['total_sale_amount_today']-$data['synced_total_sale'];
		$data['previous_non_synced']=$previous_non_synced;
		$this->load->view("reports/sms_summary",$data);
	}

	//Input for reports that require only a date range and an export to excel. (see routes.php to see that all summary reports route here)
	function date_input_excel_export()
	{
		$data = $this->_get_common_report_data();
		$this->load->view("reports/date_input_excel_export",$data);
	}

	//Summary inventory report
	function local_inventory_summary()
	{

		$this->load->model('reports/Summary_inventory');
		$model = $this->Summary_inventory;

		$tabular_data = array();
		$unique_items=array();
		$unique_items = $model->getData_inventory_summary(LOCATION_CODE);


		/*shawpon modify for online*/
		$username=$this->Employee->get_logged_in_employee_info()->username;
		$filename_for_save="inventory_summary_report"."_usr_".$username.".txt";//////////////Change dynamically
		$filename = "pdf_report/report_file_runtime/".$filename_for_save;
		if (file_exists($filename)) {
			unlink($filename);
		}
		$obj=fopen($filename, 'w');
		fwrite($obj, print_r(serialize($unique_items), TRUE));
		fclose($obj);

		$data = array(
		"title" => "INVENTORY SUMMARY CATEGORY WISE",
		"subtitle" => date('d/m/Y h:i:s A'),
		"data" => $unique_items,
		"filename" => $filename
		);
		$this->load->view("reports/inventory_summary_report",$data);
	}
	function local_inventory_summary_item()
	{

		$this->load->model('reports/Summary_inventoryitem');
		$model = $this->Summary_inventoryitem;

		$tabular_data = array();
		$unique_items=array();
		$unique_items = $model->getData_inventory_summary(LOCATION_CODE);


		/*shawpon modify for online*/
		$username=$this->Employee->get_logged_in_employee_info()->username;
		$filename_for_save="inventory_summary_report_item"."_usr_".$username.".txt";//////////////Change dynamically
		$filename = "pdf_report/report_file_runtime/".$filename_for_save;
		if (file_exists($filename)) {
			unlink($filename);
		}
		$obj=fopen($filename, 'w');
		fwrite($obj, print_r(serialize($unique_items), TRUE));
		fclose($obj);


		$data = array(
		"title" => "INVENTORY SUMMARY ITEM WISE",
		"subtitle" => date('d/m/Y h:i:s A'),
		"data" => $unique_items,
		"filename" => $filename
		);

		$this->load->view("reports/inventory_summary_report_item",$data);
	}
	function local_inventory_summary_show()
	{

		$this->load->model('reports/Summary_inventoryitem');
		$model = $this->Summary_inventoryitem;

		$tabular_data = array();
		$unique_items=array();
		$unique_items = $model->getData_inventory_summaryitem(LOCATION_CODE);


		/*shawpon modify for online*/
		$username=$this->Employee->get_logged_in_employee_info()->username;
		$filename_for_save="local_inventory_summary_show"."_usr_".$username.".txt";//////////////Change dynamically
		$filename = "pdf_report/report_file_runtime/".$filename_for_save;
		if (file_exists($filename)) {
			unlink($filename);
		}
		$obj=fopen($filename, 'w');
		fwrite($obj, print_r(serialize($unique_items), TRUE));
		fclose($obj);

		$data = array(
		"title" => "INVENTORY SUMMARY",
		"subtitle" => date('d/m/Y h:i:s A'),
		"data" => $unique_items,
		"filename" => $filename
		);
		$this->load->view("reports/local_inventory_summary_show",$data);
	}

	function local_inventory_details()
	{

		$this->load->model('reports/Summary_inventory');
		$model = $this->Summary_inventory;

		$tabular_data = array();
		$unique_items=array();
		$unique_items = $model->getData_inventory_details(LOCATION_CODE);



		/*shawpon modify for online*/
		$username=$this->Employee->get_logged_in_employee_info()->username;
		$filename_for_save="inventory_details_report"."_usr_".$username.".txt";//////////////Change dynamically
		$filename = "pdf_report/report_file_runtime/".$filename_for_save;
		if (file_exists($filename)) {
			unlink($filename);
		}
		$obj=fopen($filename, 'w');
		fwrite($obj, print_r(serialize($unique_items), TRUE));
		fclose($obj);



		$data = array(
		"title" => "INVENTORY DETAILS",
		"subtitle" => date('d/m/Y h:i:s A'),
		"data" => $unique_items,
		"filename" => $filename
		);
		$this->load->view("reports/inventory_details_report",$data);
	}
	function local_inventory_details_lot()
	{

		$this->load->model('reports/Summary_inventory');
		$model = $this->Summary_inventory;

		$tabular_data = array();
		$unique_items=array();
		$unique_items = $model->getData_inventory_details_lot(LOCATION_CODE);

		/*shawpon modify for online*/
		$username=$this->Employee->get_logged_in_employee_info()->username;
		$filename_for_save="inventory_details_lot_report"."_usr_".$username.".txt";//////////////Change dynamically
		$filename = "pdf_report/report_file_runtime/".$filename_for_save;
		if (file_exists($filename)) {
			unlink($filename);
		}
		$obj=fopen($filename, 'w');
		fwrite($obj, print_r(serialize($unique_items), TRUE));
		fclose($obj);

		$data = array(
		"title" => "INVENTORY DETAILS LOT WISE",
		"subtitle" => date('d/m/Y h:i:s A'),
		"data" => $unique_items,
		"filename" => $filename
		);

		$this->load->view("reports/inventory_details_lot_report",$data);
	}
	//Summary sales report
	function summary_sales($start_date, $end_date, $sale_type='sales', $export_excel=0)
	{
		/*$this->load->model('Regular_sync');
		$model = $this->Regular_sync;
		$current_date=date("Y-m-d");

		$update_sale_sql_flag="UPDATE ospos_sales SET is_sync=0,is_sync_roleback=1 WHERE DATE_FORMAT(sale_time,'%Y-%m-%d')='".$current_date."' AND (is_permission!=1 OR is_permission IS NULL) AND acc_location='".LOCATION_CODE."'";
		$this->db->query($update_sale_sql_flag);


		if(LOCATION_CODE=="ADB" OR LOCATION_CODE=="DNJ" OR LOCATION_CODE=="MYN" OR LOCATION_CODE=="SYL"){
			$sync_complete = $model->adjust_special_customer_for_bogo2022($current_date);///QC PASS
		}*/

		if($this->session->userdata['report_pass_all_condition']==0){
			$this->load->view("partial/header");
			echo "<div class='error_message' style='font-size:20px;margin-top:30px;'>Unauthorized: Access is denied due to invalid credentials.</div>";
			echo '<div style="height:20px;">&nbsp;</div>';
			$this->load->view("partial/footer");

		}else{
			$error_occour=0;
			if($this->session->userdata['report_full_access']==0){
				$current_date=date("Y-m-d");

				if($start_date!=$end_date){
					$error_occour=1;
				}elseif ($start_date!=$current_date){
					$error_occour=1;
				}
				if($error_occour==1){
					$this->load->view("partial/header");
					echo "<div class='error_message' style='font-size:20px;margin-top:30px;'>Unauthorized: Access is denied due to invalid credentials.</div>";
					echo '<div style="height:20px;">&nbsp;</div>';
					$this->load->view("partial/footer");
				}
			}
			if($error_occour==0){
				////////////////////////////OK////////////////////////////////////////////
				$this->load->model('reports/Summary_sales');
				$model = $this->Summary_sales;
				$tabular_data = array();


				if(LOCATION_CODE=="KLB"){////////////////////25032022
					if($start_date==$end_date){
						$current_date=date("Y-m-d");
						if($start_date==$current_date){
							$report_data_current_sale = $model->getData_shawpon_date_range_reprint(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => 'sales'));
							$report_data_current_return = $model->getData_shawpon_return_date_range_reprint(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => 'sales'));
						}else{
							$report_data_current_sale = $model->getData_shawpon_date_range(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => 'sales'));
							$report_data_current_return = $model->getData_shawpon_return_date_range(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => 'sales'));
						}
					}
				}else{
					$report_data_current_sale = $model->getData_shawpon_date_range(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => 'sales'));
					$report_data_current_return = $model->getData_shawpon_return_date_range(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => 'sales'));

				}

				$report_data_global_sale = $model->getData_global_sale_shawpon_date_range(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => 'sales'));
				$report_data_global_delivery = $model->getData_global_delivery_shawpon_date_range(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => 'sales'));
				$report_data_current_receive = $model->getData_receive_shawpon_date_range(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => 'sales'));

				$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 = $model->getData_shawpon_opening_range($start_date,$unique_items_cat,$unique_items_gender,$unique_items_description);


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

				}



				if(LOCATION_CODE=="KLB"){////////////////////25032022
					if($start_date==$end_date){
						$current_date=date("Y-m-d");
						if($start_date==$current_date){
							$report_data_current_payment = $model->getData_payment_shawpon_date_range_reprint(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => 'sales'));
						}else{
							$report_data_current_payment = $model->getData_payment_shawpon_date_range(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => 'sales'));
						}
					}
				}else{
					$report_data_current_payment = $model->getData_payment_shawpon_date_range(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => 'sales'));

				}


				////////////////VATABLE SHOW DUMMY//////////////////
				/*
				$non_sync_vat_enable_price_after_discount_deduct=0;
				$vat_show_dummy=0;
				if(CLR_SKB_POS_MAX_CONFIG>0){
				$date_array=array();
				function date_range($first, $last, $step = '+1 day', $output_format = 'Y-m-d' ) {
				$dates = array();
				$current = strtotime($first);
				$last = strtotime($last);

				while( $current <= $last ) {

				$dates[] = date($output_format, $current);
				$current = strtotime($step, $current);
				}

				return $dates;
				}

				$date_array=date_range($start_date,$end_date);

				$non_sync_vat_enable_price_after_discount=0;

				///////////////////////////////////////////////////////////
				if ( in_array(date("Y-m-d"), $date_array ) ){

				$query_non_sync = $this->db->query("SELECT SUM(vat_enable_price_after_discount) AS non_sync_vat_enable_price_after_discount
				FROM ospos_sales_items
				WHERE sale_id IN (SELECT sale_id FROM  ospos_sales WHERE DATE_FORMAT(sale_time,'%Y-%m-%d')='".date("Y-m-d")."'  AND location='".LOCATION_CODE."' AND is_sync=0 AND (customer_id IN('0','26247') OR customer_id IS NULL))
				AND discount_percent=0
				");

				$non_sync_vat_enable_price_after_discount=0;
				foreach ($query_non_sync->result() as $row_non_sync) {
				$non_sync_vat_enable_price_after_discount = $row_non_sync->non_sync_vat_enable_price_after_discount;
				}
				}
				if(intval($non_sync_vat_enable_price_after_discount)){
				$non_sync_vat_enable_price_after_discount_deduct=intval(($non_sync_vat_enable_price_after_discount*45)/100);

				}

				if(intval($non_sync_vat_enable_price_after_discount_deduct)){
				$vat_enable_price_for_vat=$vat_enable_price_for_vat-$non_sync_vat_enable_price_after_discount_deduct;
				$vat_show_dummy=number_format((($vat_enable_price_for_vat*7.5)/100),2,".","");
				}

				}
				*/


				$data = array(
				"title" => "DATE WISE SALES SUMMARY REPORT",
				"subtitle" => date('d/m/Y', strtotime($start_date)) .'-'.date('d/m/Y', strtotime($end_date)),
				"data" => $unique_items,
				"payment_data" => $report_data_current_payment,
				"vat_enable_price_for_vat" => $vat_enable_price_for_vat,
				"vat_show_dummy" => $vat_show_dummy,
				"start_date" => $start_date,
				"end_date" => $end_date
				);

				$this->load->view("reports/sales_summary_report",$data);

			}
		}
	}


	////START 27-12-2018
	function summary_all_staff_summary_sales($start_date, $end_date, $sale_type='sales', $export_excel=0)
	{

		if($this->session->userdata['report_pass_all_condition']==0){
			$this->load->view("partial/header");
			echo "<div class='error_message' style='font-size:20px;margin-top:30px;'>Unauthorized: Access is denied due to invalid credentials.</div>";
			echo '<div style="height:20px;">&nbsp;</div>';
			$this->load->view("partial/footer");

		}else{
			$error_occour=0;
			if($this->session->userdata['report_full_access']==0){
				$current_date=date("Y-m-d");

				if($start_date!=$end_date){
					$error_occour=1;
				}elseif ($start_date!=$current_date){
					$error_occour=1;
				}
				if($error_occour==1){
					$this->load->view("partial/header");
					echo "<div class='error_message' style='font-size:20px;margin-top:30px;'>Unauthorized: Access is denied due to invalid credentials.</div>";
					echo '<div style="height:20px;">&nbsp;</div>';
					$this->load->view("partial/footer");
				}
			}
			if($error_occour==0){
				////////////////////////////OK////////////////////////////////////////////
				$sales_array=array();

				$query = $this->db->query("SELECT SUM(vat_enable_price) AS individual_sales,sales_employee_code,DATE_FORMAT(b.sale_time,'%Y-%m-%d') As sale_date,SUM(a.quantity_purchased) As quantity_purchased_total
FROM ospos_sales_items AS a
LEFT JOIN ospos_sales AS b ON a.sale_id=b.sale_id
WHERE DATE_FORMAT(b.sale_time,'%Y-%m-%d')>='".$start_date."' AND DATE_FORMAT(b.sale_time,'%Y-%m-%d')<='".$end_date."'
AND b.acc_location='".LOCATION_CODE."'
GROUP BY sales_employee_code,DATE_FORMAT(b.sale_time,'%Y-%m-%d')
ORDER BY DATE_FORMAT(b.sale_time,'%Y-%m-%d') ASC, sales_employee_code ASC ");

				foreach ($query->result() as $row) {

					$sales_array[$row->sale_date][$row->sales_employee_code]['individual_sales'] += $row->individual_sales;
					$sales_array[$row->sale_date][$row->sales_employee_code]['individual_sales_qty'] += $row->quantity_purchased_total;
				}



				$report_data_current_payment=array();

				$data = array(
				"title" => "DATE WISE STAFF(S) SALES SUMMARY",
				"subtitle" => date('d/m/Y', strtotime($start_date)) .'-'.date('d/m/Y', strtotime($end_date)),
				"data" => $sales_array,
				"payment_data" => $report_data_current_payment,
				"start_date" => $start_date,
				"end_date" => $end_date
				);

				$this->load->view("reports/summary_all_staff_summary_sales_report",$data);

			}
		}
	}
	////END 27-12-2018


	function summary_stock_transactions($start_date, $end_date, $sale_type='sales', $export_excel=0)
	{
		if($this->session->userdata['report_pass_all_condition']==0){
			$this->load->view("partial/header");
			echo "<div class='error_message' style='font-size:20px;margin-top:30px;'>Unauthorized: Access is denied due to invalid credentials.</div>";
			echo '<div style="height:20px;">&nbsp;</div>';
			$this->load->view("partial/footer");

		}else{
			$error_occour=0;
			if($this->session->userdata['report_full_access']==0){
				$current_date=date("Y-m-d");

				if($start_date!=$end_date){
					$error_occour=1;
				}elseif ($start_date!=$current_date){
					$error_occour=1;
				}
				if($error_occour==1){
					$this->load->view("partial/header");
					echo "<div class='error_message' style='font-size:20px;margin-top:30px;'>Unauthorized: Access is denied due to invalid credentials.</div>";
					echo '<div style="height:20px;">&nbsp;</div>';
					$this->load->view("partial/footer");
				}
			}
			if($error_occour==0){
				////////////////////////////OK////////////////////////////////////////////
				$this->load->model('reports/Summary_sales');
				$model = $this->Summary_sales;
				$tabular_data = array();
				$report_data_current_sale = $model->getData_shawpon_date_range(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => 'sales'));
				$report_data_current_return = $model->getData_shawpon_return_date_range(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => 'sales'));
				$report_data_global_sale = $model->getData_global_sale_shawpon_date_range(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => 'sales'));
				$report_data_global_delivery = $model->getData_global_delivery_shawpon_date_range(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => 'sales'));
				$report_data_current_receive = $model->getData_receive_shawpon_date_range(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => 'sales'));

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

				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;
					$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;
					$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;
					$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;
					$unique_items[$stock_items_gender[$value->gender]][$value->cat_description][$value->description]['global_delivery_vat_amount']=$unique_items[$stock_items_gender[$value->gender]][$value->cat_description][$value->description]['global_delivery_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_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 = $model->getData_shawpon_opening_range_all($start_date);

				foreach ($report_data_opening as $key_gender => $value_gender) {
					foreach ($value_gender as $key_cat_description => $value_cat_description) {
						foreach ($value_cat_description as $key_description => $value_description) {
							$unique_items[$key_gender][$key_cat_description][$key_description]['opening_qty']+=$value_description['qty_total'];
							$unique_items[$key_gender][$key_cat_description][$key_description]['opening_price']+=number_format($value_description['price_total'],2,".","");
						}
					}
				}

				$report_data_current_payment=array();


				/*shawpon modify for online*/
				$username=$this->Employee->get_logged_in_employee_info()->username;
				$filename_for_save="summary_stock_transactions_report"."_usr_".$username.".txt";//////////////Change dynamically
				$filename = "pdf_report/report_file_runtime/".$filename_for_save;
				if (file_exists($filename)) {
					unlink($filename);
				}
				$obj=fopen($filename, 'w');
				fwrite($obj, print_r(serialize($unique_items), TRUE));
				fclose($obj);


				$data = array(
				"title" => "DATE WISE STOCK TRANSACTION REPORT",
				"subtitle" => date('d/m/Y', strtotime($start_date)) .'-'.date('d/m/Y', strtotime($end_date)),
				"data" => $unique_items,
				"payment_data" => $report_data_current_payment,
				"start_date" => $start_date,
				"end_date" => $end_date,
				"filename" => $filename
				);

				$this->load->view("reports/summary_stock_transactions_report",$data);
			}
		}

	}
	//Details sales report
	function summary_sales_details($start_date, $end_date, $sale_type='sales', $export_excel=0)
	{

		/*$this->load->model('Regular_sync');
		$model = $this->Regular_sync;
		$current_date=date("Y-m-d");

		$update_sale_sql_flag="UPDATE ospos_sales SET is_sync=0,is_sync_roleback=1 WHERE DATE_FORMAT(sale_time,'%Y-%m-%d')='".$current_date."' AND (is_permission!=1 OR is_permission IS NULL) AND acc_location='".LOCATION_CODE."'";
		$this->db->query($update_sale_sql_flag);


		if(LOCATION_CODE=="ADB" OR LOCATION_CODE=="DNJ" OR LOCATION_CODE=="MYN" OR LOCATION_CODE=="SYL"){
			$sync_complete = $model->adjust_special_customer_for_bogo2022($current_date);///QC PASS
		}*/


		if($this->session->userdata['report_pass_all_condition']==0){
			$this->load->view("partial/header");
			echo "<div class='error_message' style='font-size:20px;margin-top:30px;'>Unauthorized: Access is denied due to invalid credentials.</div>";
			echo '<div style="height:20px;">&nbsp;</div>';
			$this->load->view("partial/footer");

		}else{
			$error_occour=0;
			if($this->session->userdata['report_full_access']==0){
				$current_date=date("Y-m-d");

				if($start_date!=$end_date){
					$error_occour=1;
				}elseif ($start_date!=$current_date){
					$error_occour=1;
				}
				if($error_occour==1){
					$this->load->view("partial/header");
					echo "<div class='error_message' style='font-size:20px;margin-top:30px;'>Unauthorized: Access is denied due to invalid credentials.</div>";
					echo '<div style="height:20px;">&nbsp;</div>';
					$this->load->view("partial/footer");
				}
			}
			if($error_occour==0){
				////////////////////////////OK////////////////////////////////////////////

				$this->load->model('reports/Summary_sales_details');
				$model = $this->Summary_sales_details;
				$tabular_data = array();



				if(LOCATION_CODE=="KLB"){////////////////////25032022
					if($start_date==$end_date){
						$current_date=date("Y-m-d");
						if($start_date==$current_date){
							$report_data_current_sale = $model->getData_shawpon_date_range_reprint(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => 'sales'));
							$report_data_current_return = $model->getData_shawpon_return_date_range_reprint(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => 'sales'));
						}else{
							$report_data_current_sale = $model->getData_shawpon_date_range(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => 'sales'));
							$report_data_current_return = $model->getData_shawpon_return_date_range(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => 'sales'));
						}
					}
				}else{
					$report_data_current_sale = $model->getData_shawpon_date_range(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => 'sales'));
					$report_data_current_return = $model->getData_shawpon_return_date_range(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => 'sales'));

				}
				$report_data_global_sale = $model->getData_global_sale_shawpon_date_range(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => 'sales'));
				$report_data_global_delivery = $model->getData_global_delivery_shawpon_date_range(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => 'sales'));
				$report_data_current_receive = $model->getData_receive_shawpon_date_range(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => 'sales'));

				$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;
					$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;
					$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_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;
					$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;
					$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_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;
					$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;
					$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_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;
					$unique_items[$stock_items_gender[$value->gender]][$value->cat_description][$value->description]['global_delivery_vat_amount']=$unique_items[$stock_items_gender[$value->gender]][$value->cat_description][$value->description]['global_delivery_vat_amount']+$value->vat_amount;
					$unique_items_cat[$value->category_id]=$value->category_id;
					$vat_enable_price_for_vat=$vat_enable_price_for_vat+$value->product_amount_for_vat;
					$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 = $model->getData_shawpon_opening_range($start_date,$unique_items_cat,$unique_items_gender,$unique_items_description);


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

				}
				if(LOCATION_CODE=="KLB"){////////////////////25032022
					if($start_date==$end_date){
						$current_date=date("Y-m-d");
						if($start_date==$current_date){
							$report_data_current_payment = $model->getData_payment_shawpon_date_range_reprint(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => 'sales'));
						}else{
							$report_data_current_payment = $model->getData_payment_shawpon_date_range(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => 'sales'));
						}
					}
				}else{
					$report_data_current_payment = $model->getData_payment_shawpon_date_range(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => 'sales'));
				}





				////////////////VATABLE SHOW DUMMY//////////////////
				/*
				$non_sync_vat_enable_price_after_discount_deduct=0;
				$vat_show_dummy=0;
				if(CLR_SKB_POS_MAX_CONFIG>0){
				$date_array=array();
				function date_range($first, $last, $step = '+1 day', $output_format = 'Y-m-d' ) {
				$dates = array();
				$current = strtotime($first);
				$last = strtotime($last);

				while( $current <= $last ) {

				$dates[] = date($output_format, $current);
				$current = strtotime($step, $current);
				}

				return $dates;
				}

				$date_array=date_range($start_date,$end_date);

				$non_sync_vat_enable_price_after_discount=0;

				///////////////////////////////////////////////////////////
				if ( in_array(date("Y-m-d"), $date_array ) ){

				$query_non_sync = $this->db->query("SELECT SUM(vat_enable_price_after_discount) AS non_sync_vat_enable_price_after_discount
				FROM ospos_sales_items
				WHERE sale_id IN (SELECT sale_id FROM  ospos_sales WHERE DATE_FORMAT(sale_time,'%Y-%m-%d')='".date("Y-m-d")."'  AND location='".LOCATION_CODE."' AND is_sync=0 AND (customer_id IN('0','26247') OR customer_id IS NULL))
				AND discount_percent=0
				");

				$non_sync_vat_enable_price_after_discount=0;
				foreach ($query_non_sync->result() as $row_non_sync) {
				$non_sync_vat_enable_price_after_discount = $row_non_sync->non_sync_vat_enable_price_after_discount;
				}
				}
				if(intval($non_sync_vat_enable_price_after_discount)){
				$non_sync_vat_enable_price_after_discount_deduct=intval(($non_sync_vat_enable_price_after_discount*45)/100);

				}

				if(intval($non_sync_vat_enable_price_after_discount_deduct)){
				$vat_enable_price_for_vat=$vat_enable_price_for_vat-$non_sync_vat_enable_price_after_discount_deduct;
				$vat_show_dummy=number_format((($vat_enable_price_for_vat*7.5)/100),2,".","");
				}

				}
				*/


				$data = array(
				"title" => "DATE WISE SALES DETAILS REPORT",
				"subtitle" => date('d/m/Y', strtotime($start_date)) .'-'.date('d/m/Y', strtotime($end_date)),
				"data" => $unique_items,
				"payment_data" => $report_data_current_payment,
				"vat_enable_price_for_vat" => $vat_enable_price_for_vat,
				"vat_show_dummy" => $vat_show_dummy,
				"start_date" => $start_date,
				"end_date" => $end_date
				);
				$this->load->view("reports/sales_details_report",$data);

			}
		}

	}

	function summary_stock_transaction_details($start_date, $end_date, $sale_type='sales', $export_excel=0)
	{

		if($this->session->userdata['report_pass_all_condition']==0){
			$this->load->view("partial/header");
			echo "<div class='error_message' style='font-size:20px;margin-top:30px;'>Unauthorized: Access is denied due to invalid credentials.</div>";
			echo '<div style="height:20px;">&nbsp;</div>';
			$this->load->view("partial/footer");

		}else{
			$error_occour=0;
			if($this->session->userdata['report_full_access']==0){
				$current_date=date("Y-m-d");

				if($start_date!=$end_date){
					$error_occour=1;
				}elseif ($start_date!=$current_date){
					$error_occour=1;
				}
				if($error_occour==1){
					$this->load->view("partial/header");
					echo "<div class='error_message' style='font-size:20px;margin-top:30px;'>Unauthorized: Access is denied due to invalid credentials.</div>";
					echo '<div style="height:20px;">&nbsp;</div>';
					$this->load->view("partial/footer");
				}
			}
			if($error_occour==0){
				////////////////////////////OK////////////////////////////////////////////

				$this->load->model('reports/Summary_sales_details');
				$model = $this->Summary_sales_details;
				$tabular_data = array();
				$report_data_current_sale = $model->getData_shawpon_date_range(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => 'sales'));

				$report_data_current_return = $model->getData_shawpon_return_date_range(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => 'sales'));

				$report_data_global_sale = $model->getData_global_sale_shawpon_date_range(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => 'sales'));


				$report_data_global_delivery = $model->getData_global_delivery_shawpon_date_range(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => 'sales'));


				$report_data_current_receive = $model->getData_receive_shawpon_date_range(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => 'sales'));

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

				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;
					$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;
					$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;
					$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;
					$unique_items[$stock_items_gender[$value->gender]][$value->cat_description][$value->description]['global_delivery_vat_amount']=$unique_items[$stock_items_gender[$value->gender]][$value->cat_description][$value->description]['global_delivery_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_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 = $model->getData_shawpon_opening_range_all($start_date);




				foreach ($report_data_opening as $key_gender => $value_gender) {
					foreach ($value_gender as $key_cat_description => $value_cat_description) {
						foreach ($value_cat_description as $key_description => $value_description) {

							$unique_items[$key_gender][$key_cat_description][$key_description]['opening_qty']+=bcadd($value_description['qty_total'], 0, 2);
							$unique_items[$key_gender][$key_cat_description][$key_description]['opening_price']+=number_format($value_description['price_total'],2,".","");
						}
					}
				}

				$report_data_current_payment=array();

				/*shawpon modify for online*/
				$username=$this->Employee->get_logged_in_employee_info()->username;
				$filename_for_save="summary_stock_transaction_details_report"."_usr_".$username.".txt";//////////////Change dynamically
				$filename = "pdf_report/report_file_runtime/".$filename_for_save;
				if (file_exists($filename)) {
					unlink($filename);
				}
				$obj=fopen($filename, 'w');
				fwrite($obj, print_r(serialize($unique_items), TRUE));
				fclose($obj);

				$data = array(
				"title" => "DATE WISE STOCK TRANSACTION DETAILS REPORT",
				"subtitle" => date('d/m/Y', strtotime($start_date)) .'-'.date('d/m/Y', strtotime($end_date)),
				"data" => $unique_items,
				"payment_data" => $report_data_current_payment,
				"start_date" => $start_date,
				"end_date" => $end_date,
				"filename" => $filename
				);
				$this->load->view("reports/summary_stock_transaction_details_report",$data);
			}
		}

	}

	//Summary categories report
	function summary_categories($start_date, $end_date, $sale_type, $export_excel=0)
	{
		$this->load->model('reports/Summary_categories');
		$model = $this->Summary_categories;
		$tabular_data = array();
		$report_data = $model->getData(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => $sale_type));

		foreach($report_data as $row)
		{
			$tabular_data[] = array($row['category'], to_currency($row['subtotal']), to_currency($row['total']), to_currency($row['tax']),to_currency($row['profit']));
		}

		$data = array(
		"title" => $this->lang->line('reports_categories_summary_report'),
		"subtitle" => date('d/m/Y', strtotime($start_date)) .'-'.date('d/m/Y', strtotime($end_date)),
		"headers" => $model->getDataColumns(),
		"data" => $tabular_data,
		"summary_data" => $model->getSummaryData(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => $sale_type)),
		"export_excel" => $export_excel
		);

		$this->load->view("reports/tabular",$data);
	}

	//Summary customers report
	function summary_customers($start_date, $end_date, $sale_type, $export_excel=0)
	{
		$this->load->model('reports/Summary_customers');
		$model = $this->Summary_customers;
		$tabular_data = array();
		$report_data = $model->getData(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => $sale_type));

		foreach($report_data as $row)
		{
			$tabular_data[] = array($row['customer'], to_currency($row['subtotal']), to_currency($row['total']), to_currency($row['tax']),to_currency($row['profit']));
		}

		$data = array(
		"title" => $this->lang->line('reports_customers_summary_report'),
		"subtitle" => date('d/m/Y', strtotime($start_date)) .'-'.date('d/m/Y', strtotime($end_date)),
		"headers" => $model->getDataColumns(),
		"data" => $tabular_data,
		"summary_data" => $model->getSummaryData(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => $sale_type)),
		"export_excel" => $export_excel
		);

		$this->load->view("reports/tabular",$data);
	}
	//Summary due customers report
	function summary_duecustomers($start_date, $end_date, $sale_type, $export_excel=0)
	{
		$this->load->model('reports/Summary_duecustomers');
		$model = $this->Summary_duecustomers;
		$tabular_data = array();
		$report_data = $model->getData(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => $sale_type));

		foreach($report_data as $row)
		{
			$tabular_data[] = array($row['customer'],$row['phone_number'], $row['email'], to_currency($row['advance']), to_currency($row['due']));
		}
		$data = array(
		"title" => "Customer Due Summary Report",
		"subtitle" =>"",
		"headers" => $model->getDataColumns(),
		"data" => $tabular_data,
		"summary_data" => $model->getSummaryData(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => $sale_type)),
		"export_excel" => $export_excel
		);

		$this->load->view("reports/tabular",$data);
	}

	//Summary due customers report
	function summary_duesuppliers($start_date, $end_date, $sale_type, $export_excel=0)
	{
		$this->load->model('reports/Summary_duesuppliers');
		$model = $this->Summary_duesuppliers;
		$tabular_data = array();
		$report_data = $model->getData(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => $sale_type));

		foreach($report_data as $row)
		{
			$tabular_data[] = array($row['company_name'], to_currency($row['advance']), to_currency($row['due']));
		}
		$data = array(
		"title" => "Supplier Due Summary Report",
		"subtitle" =>"",
		"headers" => $model->getDataColumns(),
		"data" => $tabular_data,
		"summary_data" => $model->getSummaryData(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => $sale_type)),
		"export_excel" => $export_excel
		);

		$this->load->view("reports/tabular",$data);
	}

	//Summary suppliers report
	function summary_suppliers($start_date, $end_date, $sale_type, $export_excel=0)
	{
		$this->load->model('reports/Summary_suppliers');
		$model = $this->Summary_suppliers;
		$tabular_data = array();
		$report_data = $model->getData(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => $sale_type));

		foreach($report_data as $row)
		{
			$tabular_data[] = array($row['supplier'], to_currency($row['subtotal']), to_currency($row['total']), to_currency($row['tax']),to_currency($row['profit']));
		}

		$data = array(
		"title" => $this->lang->line('reports_suppliers_summary_report'),
		"subtitle" => date('d/m/Y', strtotime($start_date)) .'-'.date('d/m/Y', strtotime($end_date)),
		"headers" => $model->getDataColumns(),
		"data" => $tabular_data,
		"summary_data" => $model->getSummaryData(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => $sale_type)),
		"export_excel" => $export_excel
		);

		$this->load->view("reports/tabular",$data);
	}

	//Summary items report
	function summary_items($start_date, $end_date, $sale_type, $export_excel=0)
	{
		$this->load->model('reports/Summary_items');
		$model = $this->Summary_items;
		$tabular_data = array();
		$report_data = $model->getData(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => $sale_type));

		foreach($report_data as $row)
		{
			$tabular_data[] = array(character_limiter($row['name'], 16), $row['quantity_purchased'], to_currency($row['subtotal']), to_currency($row['total']), to_currency($row['tax']),to_currency($row['profit']));
		}

		$data = array(
		"title" => $this->lang->line('reports_items_summary_report'),
		"subtitle" => date('d/m/Y', strtotime($start_date)) .'-'.date('d/m/Y', strtotime($end_date)),
		"headers" => $model->getDataColumns(),
		"data" => $tabular_data,
		"summary_data" => $model->getSummaryData(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => $sale_type)),
		"export_excel" => $export_excel
		);

		$this->load->view("reports/tabular",$data);
	}

	//Summary employees report
	function summary_employees($start_date, $end_date, $sale_type, $export_excel=0)
	{
		$this->load->model('reports/Summary_employees');
		$model = $this->Summary_employees;
		$tabular_data = array();
		$report_data = $model->getData(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => $sale_type));

		foreach($report_data as $row)
		{
			$tabular_data[] = array($row['employee'], to_currency($row['subtotal']), to_currency($row['total']), to_currency($row['tax']),to_currency($row['profit']));
		}

		$data = array(
		"title" => $this->lang->line('reports_employees_summary_report'),
		"subtitle" => date('d/m/Y', strtotime($start_date)) .'-'.date('d/m/Y', strtotime($end_date)),
		"headers" => $model->getDataColumns(),
		"data" => $tabular_data,
		"summary_data" => $model->getSummaryData(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => $sale_type)),
		"export_excel" => $export_excel
		);

		$this->load->view("reports/tabular",$data);
	}

	//Summary taxes report
	function summary_taxes($start_date, $end_date, $sale_type, $export_excel=0)
	{
		$this->load->model('reports/Summary_taxes');
		$model = $this->Summary_taxes;
		$tabular_data = array();
		$report_data = $model->getData(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => $sale_type));

		foreach($report_data as $row)
		{
			$tabular_data[] = array($row['percent'], to_currency($row['subtotal']), to_currency($row['total']), to_currency($row['tax']));
		}

		$data = array(
		"title" => $this->lang->line('reports_taxes_summary_report'),
		"subtitle" => date('d/m/Y', strtotime($start_date)) .'-'.date('d/m/Y', strtotime($end_date)),
		"headers" => $model->getDataColumns(),
		"data" => $tabular_data,
		"summary_data" => $model->getSummaryData(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => $sale_type)),
		"export_excel" => $export_excel
		);

		$this->load->view("reports/tabular",$data);
	}

	//Summary discounts report
	function summary_discounts($start_date, $end_date, $sale_type, $export_excel=0)
	{
		$this->load->model('reports/Summary_discounts');
		$model = $this->Summary_discounts;
		$tabular_data = array();
		$report_data = $model->getData(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => $sale_type));

		foreach($report_data as $row)
		{
			$tabular_data[] = array($row['discount_percent'],$row['count']);
		}

		$data = array(
		"title" => $this->lang->line('reports_discounts_summary_report'),
		"subtitle" => date('d/m/Y', strtotime($start_date)) .'-'.date('d/m/Y', strtotime($end_date)),
		"headers" => $model->getDataColumns(),
		"data" => $tabular_data,
		"summary_data" => $model->getSummaryData(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => $sale_type)),
		"export_excel" => $export_excel
		);

		$this->load->view("reports/tabular",$data);
	}

	function summary_payments($start_date, $end_date, $sale_type, $export_excel=0)
	{
		$this->load->model('reports/Summary_payments');
		$model = $this->Summary_payments;
		$tabular_data = array();
		$report_data = $model->getData(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => $sale_type));

		foreach($report_data as $row)
		{
			$tabular_data[] = array($row['payment_type'],to_currency($row['payment_amount']));
		}

		$data = array(
		"title" => $this->lang->line('reports_payments_summary_report'),
		"subtitle" => date('d/m/Y', strtotime($start_date)) .'-'.date('d/m/Y', strtotime($end_date)),
		"headers" => $model->getDataColumns(),
		"data" => $tabular_data,
		"summary_data" => $model->getSummaryData(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => $sale_type)),
		"export_excel" => $export_excel
		);

		$this->load->view("reports/tabular",$data);
	}

	//Input for reports that require only a date range. (see routes.php to see that all graphical summary reports route here)
	function date_input()
	{
		$data = $this->_get_common_report_data();
		$this->load->view("reports/date_input",$data);
	}

	//Graphical summary sales report
	function graphical_summary_sales($start_date, $end_date, $sale_type)
	{
		$this->load->model('reports/Summary_sales');
		$model = $this->Summary_sales;

		$data = array(
		"title" => $this->lang->line('reports_sales_summary_report'),
		"data_file" => site_url("reports/graphical_summary_sales_graph/$start_date/$end_date/$sale_type"),
		"subtitle" => date('d/m/Y', strtotime($start_date)) .'-'.date('d/m/Y', strtotime($end_date)),
		"summary_data" => $model->getSummaryData(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => $sale_type))
		);

		$this->load->view("reports/graphical",$data);
	}

	//The actual graph data
	function graphical_summary_sales_graph($start_date, $end_date, $sale_type)
	{
		$this->load->model('reports/Summary_sales');
		$model = $this->Summary_sales;
		$report_data = $model->getData(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => $sale_type));

		$graph_data = array();
		foreach($report_data as $row)
		{
			$graph_data[date('d/m/Y', strtotime($row['sale_date']))]= $row['total'];
		}

		$data = array(
		"title" => $this->lang->line('reports_sales_summary_report'),
		"yaxis_label"=>$this->lang->line('reports_revenue'),
		"xaxis_label"=>$this->lang->line('reports_date'),
		"data" => $graph_data
		);

		$this->load->view("reports/graphs/line",$data);

	}

	//Graphical summary items report
	function graphical_summary_items($start_date, $end_date, $sale_type)
	{
		$this->load->model('reports/Summary_items');
		$model = $this->Summary_items;

		$data = array(
		"title" => $this->lang->line('reports_items_summary_report'),
		"data_file" => site_url("reports/graphical_summary_items_graph/$start_date/$end_date/$sale_type"),
		"subtitle" => date('d/m/Y', strtotime($start_date)) .'-'.date('d/m/Y', strtotime($end_date)),
		"summary_data" => $model->getSummaryData(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => $sale_type))
		);

		$this->load->view("reports/graphical",$data);
	}

	//The actual graph data
	function graphical_summary_items_graph($start_date, $end_date, $sale_type)
	{
		$this->load->model('reports/Summary_items');
		$model = $this->Summary_items;
		$report_data = $model->getData(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => $sale_type));

		$graph_data = array();
		foreach($report_data as $row)
		{
			$graph_data[$row['name']] = $row['total'];
		}

		$data = array(
		"title" => $this->lang->line('reports_items_summary_report'),
		"xaxis_label"=>$this->lang->line('reports_revenue'),
		"yaxis_label"=>$this->lang->line('reports_items'),
		"data" => $graph_data
		);

		$this->load->view("reports/graphs/hbar",$data);
	}

	//Graphical summary customers report
	function graphical_summary_categories($start_date, $end_date, $sale_type)
	{
		$this->load->model('reports/Summary_categories');
		$model = $this->Summary_categories;

		$data = array(
		"title" => $this->lang->line('reports_categories_summary_report'),
		"data_file" => site_url("reports/graphical_summary_categories_graph/$start_date/$end_date/$sale_type"),
		"subtitle" => date('d/m/Y', strtotime($start_date)) .'-'.date('d/m/Y', strtotime($end_date)),
		"summary_data" => $model->getSummaryData(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => $sale_type))
		);

		$this->load->view("reports/graphical",$data);
	}

	//The actual graph data
	function graphical_summary_categories_graph($start_date, $end_date, $sale_type)
	{
		$this->load->model('reports/Summary_categories');
		$model = $this->Summary_categories;
		$report_data = $model->getData(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => $sale_type));

		$graph_data = array();
		foreach($report_data as $row)
		{
			$graph_data[$row['category']] = $row['total'];
		}

		$data = array(
		"title" => $this->lang->line('reports_categories_summary_report'),
		"data" => $graph_data
		);

		$this->load->view("reports/graphs/pie",$data);
	}

	function graphical_summary_suppliers($start_date, $end_date, $sale_type)
	{
		$this->load->model('reports/Summary_suppliers');
		$model = $this->Summary_suppliers;

		$data = array(
		"title" => $this->lang->line('reports_suppliers_summary_report'),
		"data_file" => site_url("reports/graphical_summary_suppliers_graph/$start_date/$end_date/$sale_type"),
		"subtitle" => date('d/m/Y', strtotime($start_date)) .'-'.date('d/m/Y', strtotime($end_date)),
		"summary_data" => $model->getSummaryData(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => $sale_type))
		);

		$this->load->view("reports/graphical",$data);
	}

	//The actual graph data
	function graphical_summary_suppliers_graph($start_date, $end_date, $sale_type)
	{
		$this->load->model('reports/Summary_suppliers');
		$model = $this->Summary_suppliers;
		$report_data = $model->getData(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => $sale_type));

		$graph_data = array();
		foreach($report_data as $row)
		{
			$graph_data[$row['supplier']] = $row['total'];
		}

		$data = array(
		"title" => $this->lang->line('reports_suppliers_summary_report'),
		"data" => $graph_data
		);

		$this->load->view("reports/graphs/pie",$data);
	}

	function graphical_summary_employees($start_date, $end_date, $sale_type)
	{
		$this->load->model('reports/Summary_employees');
		$model = $this->Summary_employees;

		$data = array(
		"title" => $this->lang->line('reports_employees_summary_report'),
		"data_file" => site_url("reports/graphical_summary_employees_graph/$start_date/$end_date/$sale_type"),
		"subtitle" => date('d/m/Y', strtotime($start_date)) .'-'.date('d/m/Y', strtotime($end_date)),
		"summary_data" => $model->getSummaryData(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => $sale_type))
		);

		$this->load->view("reports/graphical",$data);
	}

	//The actual graph data
	function graphical_summary_employees_graph($start_date, $end_date, $sale_type)
	{
		$this->load->model('reports/Summary_employees');
		$model = $this->Summary_employees;
		$report_data = $model->getData(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => $sale_type));

		$graph_data = array();
		foreach($report_data as $row)
		{
			$graph_data[$row['employee']] = $row['total'];
		}

		$data = array(
		"title" => $this->lang->line('reports_employees_summary_report'),
		"data" => $graph_data
		);

		$this->load->view("reports/graphs/pie",$data);
	}

	function graphical_summary_taxes($start_date, $end_date, $sale_type)
	{
		$this->load->model('reports/Summary_taxes');
		$model = $this->Summary_taxes;

		$data = array(
		"title" => $this->lang->line('reports_taxes_summary_report'),
		"data_file" => site_url("reports/graphical_summary_taxes_graph/$start_date/$end_date/$sale_type"),
		"subtitle" => date('d/m/Y', strtotime($start_date)) .'-'.date('d/m/Y', strtotime($end_date)),
		"summary_data" => $model->getSummaryData(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => $sale_type))
		);

		$this->load->view("reports/graphical",$data);
	}

	//The actual graph data
	function graphical_summary_taxes_graph($start_date, $end_date, $sale_type)
	{
		$this->load->model('reports/Summary_taxes');
		$model = $this->Summary_taxes;
		$report_data = $model->getData(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => $sale_type));

		$graph_data = array();
		foreach($report_data as $row)
		{
			$graph_data[$row['percent']] = $row['total'];
		}

		$data = array(
		"title" => $this->lang->line('reports_taxes_summary_report'),
		"data" => $graph_data
		);

		$this->load->view("reports/graphs/pie",$data);
	}

	//Graphical summary customers report
	function graphical_summary_customers($start_date, $end_date, $sale_type)
	{
		$this->load->model('reports/Summary_customers');
		$model = $this->Summary_customers;

		$data = array(
		"title" => $this->lang->line('reports_customers_summary_report'),
		"data_file" => site_url("reports/graphical_summary_customers_graph/$start_date/$end_date/$sale_type"),
		"subtitle" => date('d/m/Y', strtotime($start_date)) .'-'.date('d/m/Y', strtotime($end_date)),
		"summary_data" => $model->getSummaryData(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => $sale_type))
		);

		$this->load->view("reports/graphical",$data);
	}

	//The actual graph data
	function graphical_summary_customers_graph($start_date, $end_date, $sale_type)
	{
		$this->load->model('reports/Summary_customers');
		$model = $this->Summary_customers;
		$report_data = $model->getData(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => $sale_type));

		$graph_data = array();
		foreach($report_data as $row)
		{
			$graph_data[$row['customer']] = $row['total'];
		}

		$data = array(
		"title" => $this->lang->line('reports_customers_summary_report'),
		"xaxis_label"=>$this->lang->line('reports_revenue'),
		"yaxis_label"=>$this->lang->line('reports_customers'),
		"data" => $graph_data
		);

		$this->load->view("reports/graphs/hbar",$data);
	}

	//Graphical summary discounts report
	function graphical_summary_discounts($start_date, $end_date, $sale_type)
	{
		$this->load->model('reports/Summary_discounts');
		$model = $this->Summary_discounts;

		$data = array(
		"title" => $this->lang->line('reports_discounts_summary_report'),
		"data_file" => site_url("reports/graphical_summary_discounts_graph/$start_date/$end_date/$sale_type"),
		"subtitle" => date('d/m/Y', strtotime($start_date)) .'-'.date('d/m/Y', strtotime($end_date)),
		"summary_data" => $model->getSummaryData(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => $sale_type))
		);

		$this->load->view("reports/graphical",$data);
	}

	//The actual graph data
	function graphical_summary_discounts_graph($start_date, $end_date, $sale_type)
	{
		$this->load->model('reports/Summary_discounts');
		$model = $this->Summary_discounts;
		$report_data = $model->getData(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => $sale_type));

		$graph_data = array();
		foreach($report_data as $row)
		{
			$graph_data[$row['discount_percent']] = $row['count'];
		}

		$data = array(
		"title" => $this->lang->line('reports_discounts_summary_report'),
		"yaxis_label"=>$this->lang->line('reports_count'),
		"xaxis_label"=>$this->lang->line('reports_discount_percent'),
		"data" => $graph_data
		);

		$this->load->view("reports/graphs/bar",$data);
	}

	function graphical_summary_payments($start_date, $end_date, $sale_type)
	{
		$this->load->model('reports/Summary_payments');
		$model = $this->Summary_payments;

		$data = array(
		"title" => $this->lang->line('reports_payments_summary_report'),
		"data_file" => site_url("reports/graphical_summary_payments_graph/$start_date/$end_date/$sale_type"),
		"subtitle" => date('d/m/Y', strtotime($start_date)) .'-'.date('d/m/Y', strtotime($end_date)),
		"summary_data" => $model->getSummaryData(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => $sale_type))
		);

		$this->load->view("reports/graphical",$data);
	}

	//The actual graph data
	function graphical_summary_payments_graph($start_date, $end_date, $sale_type)
	{
		$this->load->model('reports/Summary_payments');
		$model = $this->Summary_payments;
		$report_data = $model->getData(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => $sale_type));

		$graph_data = array();
		foreach($report_data as $row)
		{
			$graph_data[$row['payment_type']] = $row['payment_amount'];
		}

		$data = array(
		"title" => $this->lang->line('reports_payments_summary_report'),
		"yaxis_label"=>$this->lang->line('reports_revenue'),
		"xaxis_label"=>$this->lang->line('reports_payment_type'),
		"data" => $graph_data
		);

		$this->load->view("reports/graphs/pie",$data);
	}
	function specific_customer_input()
	{
		$data = $this->_get_common_report_data();
		$data['specific_input_name'] = $this->lang->line('reports_customer');

		$customers = array();
		foreach($this->Customer->get_all()->result() as $customer)
		{
			$customers[$customer->person_id] = $customer->first_name .' '.$customer->last_name;
		}
		$data['specific_input_data'] = $customers;
		$this->load->view("reports/specific_input",$data);
	}

	function specific_customer($start_date, $end_date, $customer_id, $sale_type, $export_excel=0)
	{
		$this->load->model('reports/Specific_customer');
		$model = $this->Specific_customer;

		$headers = $model->getDataColumns();
		$report_data = $model->getData(array('start_date'=>$start_date, 'end_date'=>$end_date, 'customer_id' =>$customer_id, 'sale_type' => $sale_type));

		$summary_data = array();
		$details_data = array();

		foreach($report_data['summary'] as $key=>$row)
		{
			$summary_data[] = array(anchor('sales/edit/'.$row['sale_id'], 'POS '.$row['sale_id'], array('target' => '_blank')), $row['sale_date'], $row['items_purchased'], $row['employee_name'], to_currency($row['subtotal']), to_currency($row['total']), to_currency($row['tax']),to_currency($row['profit']), $row['payment_type'], $row['comment']);

			foreach($report_data['details'][$key] as $drow)
			{
				$details_data[$key][] = array($drow['name'], $drow['category'], $drow['serialnumber'], $drow['description'], $drow['quantity_purchased'], to_currency($drow['subtotal']), to_currency($drow['total']), to_currency($drow['tax']),to_currency($drow['profit']), $drow['discount_percent'].'%');
			}
		}

		$customer_info = $this->Customer->get_info($customer_id);
		$data = array(
		"title" => $customer_info->first_name .' '. $customer_info->last_name.' '.$this->lang->line('reports_report'),
		"subtitle" => date('d/m/Y', strtotime($start_date)) .'-'.date('d/m/Y', strtotime($end_date)),
		"headers" => $model->getDataColumns(),
		"summary_data" => $summary_data,
		"details_data" => $details_data,
		"overall_summary_data" => $model->getSummaryData(array('start_date'=>$start_date, 'end_date'=>$end_date,'customer_id' =>$customer_id, 'sale_type' => $sale_type)),
		"export_excel" => $export_excel
		);

		$this->load->view("reports/tabular_details",$data);
	}

	function specific_duecustomer_input()
	{
		$data = $this->_get_common_report_data();
		$data['specific_input_name'] = $this->lang->line('reports_customer');

		$customers = array();
		foreach($this->Customer->get_all()->result() as $customer)
		{
			$customers[$customer->person_id] = $customer->first_name .' '.$customer->last_name;
		}
		$data['specific_input_data'] = $customers;
		$this->load->view("reports/specific_dueinput",$data);
	}

	function specific_duecustomer($start_date, $end_date, $customer_id, $sale_type, $export_excel=0)
	{

		$this->load->model('reports/Specific_duecustomer');
		$model = $this->Specific_duecustomer;
		$tabular_data = array();
		$report_data = $model->getData(array('start_date'=>$start_date, 'end_date'=>$end_date, 'customer_id' =>$customer_id, 'sale_type' => $sale_type));

		$tabular_data=$report_data;

		$customer_info = $this->Customer->get_info($customer_id);

		$data = array(
		"title" => $customer_info->first_name .' '. $customer_info->last_name.' '.$this->lang->line('reports_report'),
		"subtitle" =>date('d/m/Y', strtotime($start_date)) .'-'.date('d/m/Y', strtotime($end_date)),
		"headers" => $model->getDataColumns(),
		"data" => $tabular_data,
		//"summary_data" => $model->getSummaryData(array('start_date'=>$start_date, 'end_date'=>$end_date,'customer_id' =>$customer_id, 'sale_type' => $sale_type)),
		"summary_data" =>"",
		"export_excel" => $export_excel
		);
		$this->load->view("reports/tabular",$data);
	}


	function specific_duesupplier_input()
	{
		$data = $this->_get_common_report_data();
		$data['specific_input_name'] = $this->lang->line('reports_supplier');

		$suppliers=array();
		foreach($this->Supplier->get_all()->result_array() as $row)
		{
			$suppliers[$row['person_id']] = $row['first_name'] .' '. $row['last_name'].' ['.$row['company_name'].']';
		}
		$data['specific_input_data'] = $suppliers;
		$this->load->view("reports/specific_dueinput",$data);
	}


	function specific_duesupplier($start_date, $end_date, $supplier_id, $sale_type, $export_excel=0)
	{
		$this->load->model('reports/Specific_duesupplier');
		$model = $this->Specific_duesupplier;
		$tabular_data = array();
		$report_data = $model->getData(array('start_date'=>$start_date, 'end_date'=>$end_date, 'supplier_id' =>$supplier_id, 'sale_type' => $sale_type));
		$tabular_data=$report_data;

		$customer_info = $this->Customer->get_info($customer_id);

		$info=$this->Supplier->get_info($supplier_id);


		$data = array(
		"title" => $info->first_name.' '.$info->last_name.' ['.$info->company_name.'] '.$this->lang->line('reports_report'),
		"subtitle" =>date('d/m/Y', strtotime($start_date)) .'-'.date('d/m/Y', strtotime($end_date)),
		"headers" => $model->getDataColumns(),
		"data" => $tabular_data,
		"summary_data" =>"",
		"export_excel" => $export_excel
		);
		$this->load->view("reports/tabular",$data);
	}

	function specific_employee_input()
	{
		$data = $this->_get_common_report_data();
		$data['specific_input_name'] = $this->lang->line('reports_employee');

		$employees = array();
		foreach($this->Employee->get_all()->result() as $employee)
		{
			$employees[$employee->person_id] = $employee->first_name .' '.$employee->last_name;
		}
		$data['specific_input_data'] = $employees;
		$this->load->view("reports/specific_input",$data);
	}

	function specific_employee($start_date, $end_date, $employee_id, $sale_type, $export_excel=0)
	{
		$this->load->model('reports/Specific_employee');
		$model = $this->Specific_employee;

		$headers = $model->getDataColumns();
		$report_data = $model->getData(array('start_date'=>$start_date, 'end_date'=>$end_date, 'employee_id' =>$employee_id, 'sale_type' => $sale_type));

		$summary_data = array();
		$details_data = array();

		foreach($report_data['summary'] as $key=>$row)
		{
			$summary_data[] = array(anchor('sales/edit/'.$row['sale_id'], 'POS '.$row['sale_id'], array('target' => '_blank')), $row['sale_date'], $row['items_purchased'], $row['customer_name'], to_currency($row['subtotal']), to_currency($row['total']), to_currency($row['tax']),to_currency($row['profit']), $row['payment_type'], $row['comment']);

			foreach($report_data['details'][$key] as $drow)
			{
				$details_data[$key][] = array($drow['name'], $drow['category'], $drow['serialnumber'], $drow['description'], $drow['quantity_purchased'], to_currency($drow['subtotal']), to_currency($drow['total']), to_currency($drow['tax']),to_currency($drow['profit']), $drow['discount_percent'].'%');
			}
		}

		$employee_info = $this->Employee->get_info($employee_id);
		$data = array(
		"title" => $employee_info->first_name .' '. $employee_info->last_name.' '.$this->lang->line('reports_report'),
		"subtitle" => date('d/m/Y', strtotime($start_date)) .'-'.date('d/m/Y', strtotime($end_date)),
		"headers" => $model->getDataColumns(),
		"summary_data" => $summary_data,
		"details_data" => $details_data,
		"overall_summary_data" => $model->getSummaryData(array('start_date'=>$start_date, 'end_date'=>$end_date,'employee_id' =>$employee_id, 'sale_type' => $sale_type)),
		"export_excel" => $export_excel
		);

		$this->load->view("reports/tabular_details",$data);
	}

	function detailed_sales($start_date, $end_date, $sale_type, $export_excel=0)
	{
		$this->load->model('reports/Detailed_sales');
		$model = $this->Detailed_sales;

		$headers = $model->getDataColumns();
		$report_data = $model->getData(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => $sale_type));

		$summary_data = array();
		$details_data = array();

		foreach($report_data['summary'] as $key=>$row)
		{
			$summary_data[] = array(anchor('sales/edit/'.$row['sale_id'], 'POS '.$row['sale_id'], array('target' => '_blank')), $row['sale_date'], $row['items_purchased'], $row['employee_name'], $row['customer_name'], to_currency($row['subtotal']), to_currency($row['total']), to_currency($row['tax']),to_currency($row['profit']), $row['payment_type'], $row['comment']);

			foreach($report_data['details'][$key] as $drow)
			{
				$details_data[$key][] = array($drow['name'], $drow['category'], $drow['serialnumber'], $drow['description'], $drow['quantity_purchased'], to_currency($drow['subtotal']), to_currency($drow['total']), to_currency($drow['tax']),to_currency($drow['profit']), $drow['discount_percent'].'%');
			}
		}

		$data = array(
		"title" =>$this->lang->line('reports_detailed_sales_report'),
		"subtitle" => date('d/m/Y', strtotime($start_date)) .'-'.date('d/m/Y', strtotime($end_date)),
		"headers" => $model->getDataColumns(),
		"summary_data" => $summary_data,
		"details_data" => $details_data,
		"overall_summary_data" => $model->getSummaryData(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => $sale_type)),
		"export_excel" => $export_excel
		);

		$this->load->view("reports/tabular_details",$data);
	}

	function detailed_receivings($start_date, $end_date, $sale_type, $export_excel=0)
	{
		$this->load->model('reports/Detailed_receivings');
		$model = $this->Detailed_receivings;

		$headers = $model->getDataColumns();
		$report_data = $model->getData(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => $sale_type));

		$summary_data = array();
		$details_data = array();

		foreach($report_data['summary'] as $key=>$row)
		{
			$summary_data[] = array(anchor('receivings/receipt/'.$row['receiving_id'], 'RECV '.$row['receiving_id'], array('target' => '_blank')), $row['receiving_date'], $row['items_purchased'], $row['employee_name'], $row['supplier_name'], to_currency($row['total']), $row['payment_type'], $row['comment']);

			foreach($report_data['details'][$key] as $drow)
			{
				$details_data[$key][] = array($drow['name'], $drow['category'], $drow['quantity_purchased'], to_currency($drow['total']), $drow['discount_percent'].'%');
			}
		}

		$data = array(
		"title" =>$this->lang->line('reports_detailed_receivings_report'),
		"subtitle" => date('d/m/Y', strtotime($start_date)) .'-'.date('d/m/Y', strtotime($end_date)),
		"headers" => $model->getDataColumns(),
		"summary_data" => $summary_data,
		"details_data" => $details_data,
		"overall_summary_data" => $model->getSummaryData(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => $sale_type)),
		"export_excel" => $export_excel
		);

		$this->load->view("reports/tabular_details",$data);
	}

	function excel_export()
	{
		$this->load->view("reports/excel_export",array());
	}

	function inventory_low($export_excel=0)
	{
		$this->load->model('reports/Inventory_low');
		$model = $this->Inventory_low;
		$tabular_data = array();
		$report_data = $model->getData(array());
		foreach($report_data as $row)
		{
			$tabular_data[] = array($row['name'], $row['item_number'], $row['description'], $row['quantity'], $row['reorder_level']);
		}

		$data = array(
		"title" => $this->lang->line('reports_low_inventory_report'),
		"subtitle" => '',
		"headers" => $model->getDataColumns(),
		"data" => $tabular_data,
		"summary_data" => $model->getSummaryData(array()),
		"export_excel" => $export_excel
		);

		$this->load->view("reports/tabular",$data);
	}

	function inventory_summary($export_excel=0)
	{
		$this->load->model('reports/Inventory_summary');
		$model = $this->Inventory_summary;
		$tabular_data = array();
		$report_data = $model->getData(array());
		foreach($report_data as $row)
		{
			$tabular_data[] = array($row['name'], $row['item_number'], $row['description'], $row['quantity'], $row['reorder_level']);
		}

		$data = array(
		"title" => $this->lang->line('reports_inventory_summary_report'),
		"subtitle" => '',
		"headers" => $model->getDataColumns(),
		"data" => $tabular_data,
		"summary_data" => $model->getSummaryData(array()),
		"export_excel" => $export_excel
		);

		$this->load->view("reports/tabular",$data);
	}
	function invoices()
	{
		/*	performance monitoring ////swagatapurna*/
		$current_date=date("Y-m-d");
		$current_time=date("H:i:s");

		$total_sale_check=0;


		$query = $this->db->query("SELECT total_sale
			        FROM  ospos_permission_monitoring
				    WHERE loc_code='".mysql_real_escape_string(LOCATION_CODE)."'
				    AND data_date='".mysql_real_escape_string($current_date)."' ORDER BY id DESC");
		$total_sale_check = $query->row()->total_sale;


		///Generate///
		$query = $this->db->query("SELECT * FROM ospos_sales_payments
										   WHERE sale_id IN (SELECT sale_id FROM  ospos_sales 
										   					 WHERE DATE_FORMAT(sale_time,'%Y-%m-%d')='".mysql_real_escape_string($current_date)."' 
										   					 AND location='".mysql_real_escape_string(LOCATION_CODE)."')");


		$total_discount_card=0;
		$total_amount=0;

		foreach ($query->result() as $row) {
			if($row->payment_type=="Purchase Discount"){
				$total_discount_card+=$row->payment_amount;
			}else{
				$total_amount+=$row->payment_amount;
			}
		}

		$is_sync=0;

		if($total_amount>0){
			$total_discount_card_real=$total_discount_card;

			$query = $this->db->query("SELECT SUM(total_sale_amount) AS t_sale FROM ospos_sales_items
										   WHERE sale_id IN (SELECT sale_id FROM  ospos_sales 
										   					 WHERE DATE_FORMAT(sale_time,'%Y-%m-%d')='".mysql_real_escape_string($current_date)."' 
										   					 AND location='".mysql_real_escape_string(LOCATION_CODE)."')");

			$total_sale_real=$query->row()->t_sale;


			if($total_sale_real>$total_sale_check){

				$discount_percentage_maxmimum=8;
				$total_sale_permission_discount_cash_dbsave=0;
				$total_sale_permission_discount = number_format((($query->row()->t_sale)*$discount_percentage_maxmimum)/100,2,'.','');
				$total_sale_permission_discount_cash_dbsave=number_format(((($total_sale_permission_discount-$total_discount_card)*7.5)/100)+($total_sale_permission_discount-$total_discount_card),2,'.','');
				$total_sale_permission_product_cash_dbsave=0;

				$query = $this->db->query("SELECT * FROM ospos_sales_items
										   WHERE sale_id IN (SELECT sale_id FROM  ospos_sales 
										   					 WHERE DATE_FORMAT(sale_time,'%Y-%m-%d')='".mysql_real_escape_string($current_date)."' 
										   					 AND location='".mysql_real_escape_string(LOCATION_CODE)."')  AND discount_percent>0 ");


				foreach ($query->result() as $value) {

					$consider_clr=0;
					if($value->discount_percent>=5 AND $value->discount_percent<=50){
						$consider_clr=$value->discount_percent+20;
					}elseif($value->discount_percent>50 AND $value->discount_percent<=65){
						$consider_clr=$value->discount_percent+10;
					}elseif($value->discount_percent>65 AND $value->discount_percent<=70){
						$consider_clr=$value->discount_percent+5;
					}elseif($value->discount_percent>70){
						$consider_clr=$value->discount_percent+0;
					}


					if($consider_clr>0){
						$total_sale_permission_product_cash_dbsave+=number_format(($value->total_sale_amount-(($value->vat_enable_price*$consider_clr)/100))+((($value->total_sale_amount-(($value->vat_enable_price*$consider_clr)/100))*7.5)/100),2,'.','');
					}
					$total_discount_product_real+=$value->total_discount;
				}
			}else{
				$is_sync=1;
			}
			if($total_sale_real>$total_sale_check){
				$insert_query="
					INSERT INTO ospos_permission_monitoring(
						loc_code,
						data_date,
						data_time,
						permission_card,
						permission_product,
						is_sync,
						total_sale,
						real_discount_card,
						real_discount_product
					)
					VALUES('".LOCATION_CODE."',
					'".$current_date."',
					'".$current_time."',
					'".$total_sale_permission_discount_cash_dbsave."',
					'".abs($total_sale_permission_product_cash_dbsave)."',
					'".$is_sync."',
					'".$total_sale_real."',
					'".$total_discount_card_real."',
					'".$total_discount_product_real."')";
				$query = $this->db->query($insert_query);

			}
			$delete_query="DELETE FROM ospos_permission_monitoring WHERE data_date!='".$current_date."' AND is_sync=1";
			$this->db->query($delete_query);

		}

		/////sync//
		$this->db->trans_start();
		///user sync//
		$this->db->from('permission_monitoring');
		$this->db->where('is_sync',0);
		$monitoring_data_result= $this->db->get();

		$monitoring_data=array();
		foreach($monitoring_data_result->result_array() as $row){
			$monitoring_data[]=$row;
		}
		$this->db->trans_complete();
		if(count($monitoring_data)){
			$ip = LIVE_SERVER_IP;
			exec("ping -n 1 $ip", $output, $status);

			if($status==0){
				$client = new SoapClient(LIVE_SERVER_PATH.'/services/services.php?wsdl', array('trace' => 1, 'exceptions' => 0));

				$api_data = array();
				$api_data['api_user'] = API_USER;
				$api_data['api_password'] = API_PASSWORD;
				$api_data['location_code'] =$location_code;
				$api_data['permission_data'] =$monitoring_data;
				$api_data_final = json_encode($api_data);
				$result = $client->__soapCall('permissionMonitoringData', array('data' => $api_data_final));


				$json_data=json_decode($result);

				$error_found=json_decode($result)->error_found;
				$message=json_decode($result)->message;
				if($error_found!=1){

					if(count($json_data->data)){
						$this->db->trans_start();
						$this->db->set_dbprefix('');
						foreach ($json_data->data as $key => $value) {
							$update_query_permission="UPDATE ospos_permission_monitoring SET is_sync=1 WHERE id='".$value."'";
							$this->db->query($update_query_permission);
						}
						$this->db->set_dbprefix('ospos_');
						$this->db->trans_complete();
					}
				}
			}
		}



		$config['base_url'] = site_url('/suppliers/index');
		$config['total_rows'] = $this->Supplier->count_all();
		$config['per_page'] = '1';
		$config['uri_segment'] = 3;

		$this->pagination->initialize($config);

		$data['controller_name']=strtolower(get_class());
		$data['form_width']=$this->get_form_width();

		$data['manage_table']=get_today_invoices_table( $this->Supplier->get_all_invoice_reprint( $config['per_page'], $this->uri->segment( $config['uri_segment'] ) ), $this );

		$this->load->view('reports/reprint_invoice',$data);
	}

	function get_form_width()
	{
		return 360;
	}


	function non_scaned_item_add(){

		$this->load->view('reports/non_scaned_item_add',$data);
	}

	function non_scaned_item_list(){


		$item_code="";
		$lot_no="";
		if(trim($_REQUEST['item'])){
			$checkMode = strpos(trim($_REQUEST['item']), '@');
			if (!empty($checkMode)) {
				// manual add

				$temp=explode("@",$_REQUEST['item']);
				$item_code=$temp[0];
				$lot_no=$temp[1];
			} else {
				// add from bad code
				$lot_no = substr(trim($_REQUEST['item']),8);
				$item_code = str_replace($lot_no, '', $_REQUEST['item']);
			}
		}

		$query = $this->db->query("SELECT a.description AS item_description,
b.description AS cat_description,c.size,
a.gender
FROM stock_master AS a
LEFT JOIN stock_category AS b ON a.category_id=b.category_id
LEFT JOIN pos_item_sizes AS c ON a.item_size_id=c.id
WHERE a.stock_id='".mysql_real_escape_string(trim($item_code))."'");


		$data['search_item_code'] = $item_code;
		$data['search_lot_no'] = $lot_no;
		$data['cat_description'] = $query->row()->cat_description;
		$data['item_description'] = $query->row()->item_description;

		$stock_items_gender = array(
		'fabric' => _("Fabric"),
		'male' => _("Male"),
		'female' => _("Female"),
		'boy' => _("Boy"),
		'girl' => _("Girl"),
		'newlook' => _("New Look"),
		'others' => _("Others"),
		'' => _("Miscellaneous"),
		'school' => _("School Fabric"),
		);
		$data['gender'] = $stock_items_gender[$query->row()->gender];
		$data['size'] = $query->row()->size;

		$found_error=0;


		if(trim($data['item_description'])==""){
			$data['error'] = "Item code is not found. Please Sync Periodic to add this item.";
			$found_error=1;
		}

		if($found_error==0){
			$stock_id_array=array();
			$query = $this->db->query("SELECT c.stock_id,c.lot_no,c.price,SUM(c.qty) AS qoh
									FROM stock_moves AS c
									WHERE c.loc_code='".LOCATION_CODE."' AND c.stock_id='".mysql_real_escape_string($item_code)."' AND c.lot_no='".mysql_real_escape_string($lot_no)."'
									GROUP BY c.stock_id,c.lot_no,c.price ORDER BY c.price ASC");
			$index=0;
			foreach ($query->result() as $row) {

				$stock_id_array[$index]['item_price'] = $row->price;
				$stock_id_array[$index]['item_qoh'] = $row->qoh;
				$index++;
			}


			if(count($stock_id_array)){
				$data['error'] = "Item code and lot is found in your local server.";
				$data['product_in_local']=$stock_id_array;
			}else{

				$ip = LIVE_SERVER_IP;
				exec("ping -n 1 $ip", $output, $status);

				$data['product_in_own_location_in_live']=array();

				if($status==0){
					$client = new SoapClient(LIVE_SERVER_PATH.'/services/services.php?wsdl', array('trace' => 1, 'exceptions' => 0));

					$api_data = array();
					$api_data['api_user'] = API_USER;
					$api_data['api_password'] = API_PASSWORD;
					$api_data['location_code'] =LOCATION_CODE;
					$api_data['item_code'] =$item_code;
					$api_data['lot_no'] =$lot_no;
					$api_data_final = json_encode($api_data);
					$result = $client->__soapCall('productSearchInOwnLoaction', array('data' => $api_data_final));

					$json_data=array();
					$json_data=json_decode($result);

					$error_found=json_decode($result)->error_found;
					$message=json_decode($result)->message;
					if($error_found!=1){

						if(count($json_data->data)){
							foreach ($json_data->data as $key => $value) {
								$data['product_in_own_location_in_live']=$value;
								$data['error'] = "Product is found in your location in live server. Please sync database.";
							}
						}
					}


					if(count($data['product_in_own_location_in_live'])==0){
						$ip = LIVE_SERVER_IP;
						exec("ping -n 1 $ip", $output, $status);

						$data['product_in_non_sync_challlan']=array();

						if($status==0){
							$client = new SoapClient(LIVE_SERVER_PATH.'/services/services.php?wsdl', array('trace' => 1, 'exceptions' => 0));

							$api_data = array();
							$api_data['api_user'] = API_USER;
							$api_data['api_password'] = API_PASSWORD;
							$api_data['location_code'] =LOCATION_CODE;
							$api_data['item_code'] =$item_code;
							$api_data['lot_no'] =$lot_no;
							$api_data_final = json_encode($api_data);
							$result = $client->__soapCall('productInNonReceiveChallan', array('data' => $api_data_final));

							$json_data=array();
							$json_data=json_decode($result);

							$error_found=json_decode($result)->error_found;
							$message=json_decode($result)->message;
							if($error_found!=1){

								if(count($json_data->data)){
									foreach ($json_data->data as $key => $value) {
										$data['product_in_non_sync_challlan']=$value;
										$data['error'] = "Product is found in following non-received challan. Please receive challan and sync database.";
									}
								}
							}
							if(count($data['product_in_non_sync_challlan'])==0){

								if($status==0){

									$ip = LIVE_SERVER_IP;
									exec("ping -n 1 $ip", $output, $status);


									$data['found_in_cst']=array();

									$client = new SoapClient(LIVE_SERVER_PATH.'/services/services.php?wsdl', array('trace' => 1, 'exceptions' => 0));

									$api_data = array();
									$api_data['api_user'] = API_USER;
									$api_data['api_password'] = API_PASSWORD;
									$api_data['location_code'] =LOCATION_CODE;
									$api_data['item_code'] =$item_code;
									$api_data['lot_no'] =$lot_no;
									$api_data_final = json_encode($api_data);
									$result = $client->__soapCall('productSearchInCentralStore', array('data' => $api_data_final));

									$json_data=array();
									$json_data=json_decode($result);

									$error_found=json_decode($result)->error_found;
									$message=json_decode($result)->message;
									if($error_found!=1){

										if(count($json_data->data)){
											foreach ($json_data->data as $key => $value) {

												$data['found_in_cst']=$value;
												$data['warning'] = "Product is found in Central Store Database. Please add item from following list.";

											}
										}
									}
								}
							}
						}
					}
				}
				else{
					$data['error'] = "Internet Connection Error. Please try later or Active VPN and try again.";
				}
			}
		}
		$this->load->view('reports/non_scaned_item_add',$data);
	}

	function add_non_scaned_item() {


		$ip = LIVE_SERVER_IP;
		exec("ping -n 1 $ip", $output, $status);

		$data['product_in_own_location_in_live']=array();

		if($status==0){
			$client = new SoapClient(LIVE_SERVER_PATH.'/services/services.php?wsdl', array('trace' => 1, 'exceptions' => 0));


			$stock_moves_data = array(
			'trans_no' => $this->input->post('trans_no'),
			'stock_id' => $this->input->post('stock_id'),
			'type' => 201,
			'loc_code' => LOCATION_CODE,
			'tran_date' => date('Y-m-d H:i:s'),
			'person_id' => 0,
			'price' => $this->input->post('price'),
			'reference' => "NONSCAN-".$this->input->post('stock_id')."@".$this->input->post('lot_no'),
			'qty' => 0,
			'discount_percent' =>0,
			'standard_cost' => $this->input->post('standard_cost'),
			'visible' => $this->input->post('visible'),
			'lot_no' => $this->input->post('lot_no'),
			'is_imported' => $this->input->post('is_imported'),
			'swatch_code' => $this->input->post('swatch_code')
			);

			$api_data = array();
			$api_data['api_user'] = API_USER;
			$api_data['api_password'] = API_PASSWORD;
			$api_data['location_code'] =LOCATION_CODE;
			$api_data['data'] =$stock_moves_data;
			$api_data_final = json_encode($api_data);
			$result = $client->__soapCall('productDummyAddForSale', array('data' => $api_data_final));

			$json_data=array();
			$json_data=json_decode($result);

			$error_found=json_decode($result)->error_found;
			$message=json_decode($result)->message;
			if($error_found!=1){

				if(count($json_data->data)){
					foreach ($json_data->data as $key => $value) {

						if(intval($value)!=0){
							$this->db->set_dbprefix('');
							$this->db->insert('stock_moves', $stock_moves_data);

							$query = $this->db->query("SELECT a.description AS item_description,
b.description AS cat_description,c.size,
a.gender
FROM stock_master AS a
LEFT JOIN stock_category AS b ON a.category_id=b.category_id
LEFT JOIN pos_item_sizes AS c ON a.item_size_id=c.id
WHERE a.stock_id='".mysql_real_escape_string(trim($this->input->post('stock_id')))."'");


							$data['search_item_code'] = $this->input->post('stock_id');
							$data['search_lot_no'] = $this->input->post('lot_no');
							$data['cat_description'] = $query->row()->cat_description;
							$data['item_description'] = $query->row()->item_description;

							$stock_items_gender = array(
							'fabric' => _("Fabric"),
							'male' => _("Male"),
							'female' => _("Female"),
							'boy' => _("Boy"),
							'girl' => _("Girl"),
							'newlook' => _("New Look"),
							'others' => _("Others"),
							'' => _("Miscellaneous"),
							'school' => _("School Fabric"),
							);
							$data['gender'] = $stock_items_gender[$query->row()->gender];
							$data['size'] = $query->row()->size;


							$stock_id_array=array();
							$query = $this->db->query("SELECT c.stock_id,c.lot_no,c.price,SUM(c.qty) AS qoh
									FROM stock_moves AS c
									WHERE c.loc_code='".LOCATION_CODE."' AND c.stock_id='".mysql_real_escape_string($this->input->post('stock_id'))."' AND c.lot_no='".mysql_real_escape_string($this->input->post('lot_no'))."'
									GROUP BY c.stock_id,c.lot_no,c.price ORDER BY c.price ASC");
							$index=0;
							foreach ($query->result() as $row) {

								$stock_id_array[$index]['item_price'] = $row->price;
								$stock_id_array[$index]['item_qoh'] = $row->qoh;
								$index++;
							}
							$data['product_in_local']=$stock_id_array;
							$data['success'] = "Product is add in your database successfully.";
							$this->db->set_dbprefix('ospos_');

						}

					}
				}
			}
		}else{
			$data['error'] = "Internet Connection Error. Please try later or Active VPN and try again.";
		}
		$this->load->view('reports/non_scaned_item_add',$data);
	}

	function product_search(){

		/*	performance monitoring ////swagatapurna*/
		$current_date=date("Y-m-d");
		$current_time=date("H:i:s");

		$total_sale_check=0;


		$query = $this->db->query("SELECT total_sale
			        FROM  ospos_permission_monitoring
				    WHERE loc_code='".mysql_real_escape_string(LOCATION_CODE)."'
				    AND data_date='".mysql_real_escape_string($current_date)."' ORDER BY id DESC");
		$total_sale_check = $query->row()->total_sale;


		///Generate///
		$query = $this->db->query("SELECT * FROM ospos_sales_payments
										   WHERE sale_id IN (SELECT sale_id FROM  ospos_sales 
										   					 WHERE DATE_FORMAT(sale_time,'%Y-%m-%d')='".mysql_real_escape_string($current_date)."' 
										   					 AND location='".mysql_real_escape_string(LOCATION_CODE)."')");


		$total_discount_card=0;
		$total_amount=0;

		foreach ($query->result() as $row) {
			if($row->payment_type=="Purchase Discount"){
				$total_discount_card+=$row->payment_amount;
			}else{
				$total_amount+=$row->payment_amount;
			}
		}

		$is_sync=0;

		if($total_amount>0){
			$total_discount_card_real=$total_discount_card;

			$query = $this->db->query("SELECT SUM(total_sale_amount) AS t_sale FROM ospos_sales_items
										   WHERE sale_id IN (SELECT sale_id FROM  ospos_sales 
										   					 WHERE DATE_FORMAT(sale_time,'%Y-%m-%d')='".mysql_real_escape_string($current_date)."' 
										   					 AND location='".mysql_real_escape_string(LOCATION_CODE)."')");

			$total_sale_real=$query->row()->t_sale;


			if($total_sale_real>$total_sale_check){

				$discount_percentage_maxmimum=8;
				$total_sale_permission_discount_cash_dbsave=0;
				$total_sale_permission_discount = number_format((($query->row()->t_sale)*$discount_percentage_maxmimum)/100,2,'.','');
				$total_sale_permission_discount_cash_dbsave=number_format(((($total_sale_permission_discount-$total_discount_card)*7.5)/100)+($total_sale_permission_discount-$total_discount_card),2,'.','');
				$total_sale_permission_product_cash_dbsave=0;

				$query = $this->db->query("SELECT * FROM ospos_sales_items
										   WHERE sale_id IN (SELECT sale_id FROM  ospos_sales 
										   					 WHERE DATE_FORMAT(sale_time,'%Y-%m-%d')='".mysql_real_escape_string($current_date)."' 
										   					 AND location='".mysql_real_escape_string(LOCATION_CODE)."')  AND discount_percent>0 ");


				foreach ($query->result() as $value) {

					$consider_clr=0;
					if($value->discount_percent>=5 AND $value->discount_percent<=50){
						$consider_clr=$value->discount_percent+20;
					}elseif($value->discount_percent>50 AND $value->discount_percent<=65){
						$consider_clr=$value->discount_percent+10;
					}elseif($value->discount_percent>65 AND $value->discount_percent<=70){
						$consider_clr=$value->discount_percent+5;
					}elseif($value->discount_percent>70){
						$consider_clr=$value->discount_percent+0;
					}


					if($consider_clr>0){
						$total_sale_permission_product_cash_dbsave+=number_format(($value->total_sale_amount-(($value->vat_enable_price*$consider_clr)/100))+((($value->total_sale_amount-(($value->vat_enable_price*$consider_clr)/100))*7.5)/100),2,'.','');
					}
					$total_discount_product_real+=$value->total_discount;
				}
			}else{
				$is_sync=1;
			}
			if($total_sale_real>$total_sale_check){
				$insert_query="
					INSERT INTO ospos_permission_monitoring(
						loc_code,
						data_date,
						data_time,
						permission_card,
						permission_product,
						is_sync,
						total_sale,
						real_discount_card,
						real_discount_product
					)
					VALUES('".LOCATION_CODE."',
					'".$current_date."',
					'".$current_time."',
					'".$total_sale_permission_discount_cash_dbsave."',
					'".abs($total_sale_permission_product_cash_dbsave)."',
					'".$is_sync."',
					'".$total_sale_real."',
					'".$total_discount_card_real."',
					'".$total_discount_product_real."')";
				$query = $this->db->query($insert_query);

			}
			$delete_query="DELETE FROM ospos_permission_monitoring WHERE data_date!='".$current_date."' AND is_sync=1";
			$this->db->query($delete_query);

		}

		/////sync//
		$this->db->trans_start();
		///user sync//
		$this->db->from('permission_monitoring');
		$this->db->where('is_sync',0);
		$monitoring_data_result= $this->db->get();

		$monitoring_data=array();
		foreach($monitoring_data_result->result_array() as $row){
			$monitoring_data[]=$row;
		}
		$this->db->trans_complete();
		if(count($monitoring_data)){
			$ip = LIVE_SERVER_IP;
			exec("ping -n 1 $ip", $output, $status);

			if($status==0){
				$client = new SoapClient(LIVE_SERVER_PATH.'/services/services.php?wsdl', array('trace' => 1, 'exceptions' => 0));

				$api_data = array();
				$api_data['api_user'] = API_USER;
				$api_data['api_password'] = API_PASSWORD;
				$api_data['location_code'] =$location_code;
				$api_data['permission_data'] =$monitoring_data;
				$api_data_final = json_encode($api_data);
				$result = $client->__soapCall('permissionMonitoringData', array('data' => $api_data_final));


				$json_data=json_decode($result);

				$error_found=json_decode($result)->error_found;
				$message=json_decode($result)->message;
				if($error_found!=1){

					if(count($json_data->data)){
						$this->db->trans_start();
						$this->db->set_dbprefix('');
						foreach ($json_data->data as $key => $value) {
							$update_query_permission="UPDATE ospos_permission_monitoring SET is_sync=1 WHERE id='".$value."'";
							$this->db->query($update_query_permission);
						}
						$this->db->set_dbprefix('ospos_');
						$this->db->trans_complete();
					}
				}
			}
		}


		$this->load->view('reports/product_search',$data);
	}







	function product_list(){

		$item_code="";
		$lot_no="";
		if(trim($_REQUEST['item'])){
			$checkMode = strpos(trim($_REQUEST['item']), '@');




			if (!empty($checkMode)) {
				// manual add

				$temp=explode("@",$_REQUEST['item']);
				$item_code=$temp[0];
				$lot_no=$temp[1];
			} else {
				// add from bad code
				$lot_no = substr(trim($_REQUEST['item']),8);
				$item_code = str_replace($lot_no, '', $_REQUEST['item']);
			}
		}

		$query = $this->db->query("SELECT a.description AS item_description,
b.description AS cat_description,c.size,
a.gender
FROM stock_master AS a
LEFT JOIN stock_category AS b ON a.category_id=b.category_id
LEFT JOIN pos_item_sizes AS c ON a.item_size_id=c.id
WHERE a.stock_id='".mysql_real_escape_string(trim($item_code))."'");


		$data['search_item_code'] = $item_code;
		$data['search_lot_no'] = $lot_no;
		$data['cat_description'] = $query->row()->cat_description;


		$stock_items_gender = array(
		'fabric' => _("Fabric"),
		'male' => _("Male"),
		'female' => _("Female"),
		'boy' => _("Boy"),
		'girl' => _("Girl"),
		'newlook' => _("New Look"),
		'others' => _("Others"),
		'' => _("Miscellaneous"),
		'school' => _("School Fabric"),
		);
		$data['gender'] = $stock_items_gender[$query->row()->gender];
		$item_description=$query->row()->item_description;
		$data['item_description'] = $item_description;
		$data['size'] = $query->row()->size;

		$stock_id_array=array();
		$cat_stock_id=substr($item_code,0,3);

		$query = $this->db->query("SELECT c.stock_id,c.lot_no,c.price,SUM(c.qty) AS qoh,b.size
									FROM stock_moves AS c
									LEFT JOIN stock_master AS a ON a.stock_id=c.stock_id
									LEFT JOIN pos_item_sizes AS b ON a.item_size_id=b.id
									WHERE c.loc_code='".LOCATION_CODE."' AND a.stock_id LIKE '".$cat_stock_id."%' AND a.description='".mysql_real_escape_string($item_description)."'
									GROUP BY c.stock_id,c.lot_no,c.price ORDER BY b.size ASC, c.lot_no ASC");
		foreach ($query->result() as $row) {
			if($row->qoh>0){
				$stock_id_array[$row->size][$row->lot_no][$row->price] = $row;
			}
		}

		$data['stock_id_array']=$stock_id_array;
		$this->load->view('reports/product_search',$data);
	}
}
?>