<?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->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;
	}

	//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();
		$report_data_current_inventory = $model->getData_inventory_summary(LOCATION_CODE);


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


		$unique_items=array();

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

			$unique_items[$stock_items_gender[$value->gender]][trim($value->cat_description)][trim($value->description)]['qty_total']=$unique_items[$stock_items_gender[$value->gender]][trim($value->cat_description)][trim($value->description)]['qty_total']+$value->qty;
			$unique_items[$stock_items_gender[$value->gender]][trim($value->cat_description)][trim($value->description)]['price_total']=$unique_items[$stock_items_gender[$value->gender]][trim($value->cat_description)][trim($value->description)]['price_total']+($value->qty*$value->price);
		}


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

		$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();
		$report_data_current_inventory = $model->getData_inventory_summary(LOCATION_CODE);

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


		$unique_items=array();

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

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

		}


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

		$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();
		$report_data_current_inventory = $model->getData_inventory_summaryitem(LOCATION_CODE);


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


		$unique_items=array();

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

			$unique_items[$stock_items_gender[trim($value->gender)]][trim($value->description)]['qty_total']=$unique_items[$stock_items_gender[trim($value->gender)]][trim($value->description)]['qty_total']+$value->total_qty;
			$unique_items[$stock_items_gender[trim($value->gender)]][trim($value->description)]['price_total']=$unique_items[$stock_items_gender[trim($value->gender)]][trim($value->description)]['price_total']+$value->total_price;

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

		$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();
		$report_data_current_inventory = $model->getData_inventory_details(LOCATION_CODE);


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


		$unique_items=array();

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

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


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

		$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();
		$report_data_current_inventory = $model->getData_inventory_details_lot(LOCATION_CODE);


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


		$unique_items=array();

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

			$unique_items[$stock_items_gender[$value->gender]][$value->cat_description][$value->description][$value->lot_no]['qty_total']=$unique_items[$stock_items_gender[$value->gender]][$value->cat_description][$value->description][$value->lot_no]['qty_total']+$value->qty;
			$unique_items[$stock_items_gender[$value->gender]][$value->cat_description][$value->description][$value->lot_no]['price_total']=$unique_items[$stock_items_gender[$value->gender]][$value->cat_description][$value->description][$value->lot_no]['price_total']+($value->qty*$value->price);
		}


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

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


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

		}
		$report_data_current_payment = $model->getData_payment_shawpon_date_range(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => 'sales'));


		$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,
		"start_date" => $start_date,
		"end_date" => $end_date
		);

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


	function summary_stock_transactions($start_date, $end_date, $sale_type='sales', $export_excel=0)
	{
		$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"),
		);


		$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_amount;
			$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_amount;
			$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_amount;
			$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_amount;
			$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 => $value) {

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

		}

		$report_data_current_payment=array();

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

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


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

		}
		$report_data_current_payment = $model->getData_payment_shawpon_date_range(array('start_date'=>$start_date, 'end_date'=>$end_date, 'sale_type' => 'sales'));


		$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,
		"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)
	{

		$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"),
		);


		$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_amount;
			$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_amount;
			$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_amount;
			$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_amount;
			$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 => $value) {

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

		}

		$report_data_current_payment=array();

		$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
		);
		$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()
	{
		$config['base_url'] = site_url('/suppliers/index');
		$config['total_rows'] = $this->Supplier->count_all();
		$config['per_page'] = '1000';
		$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( $config['per_page'], $this->uri->segment( $config['uri_segment'] ) ), $this );

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

	function get_form_width()
	{
		return 360;
	}
	
	function product_search(){
		$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"),
		);

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

}
?>