<?php
require_once("report.php");
class Summary_inventoryitem extends Report
{
	function __construct()
	{
		parent::__construct();
	}

	public function getDataColumns()
	{
		return array($this->lang->line('reports_date'), $this->lang->line('reports_subtotal'), $this->lang->line('reports_total'), $this->lang->line('reports_tax'), $this->lang->line('reports_profit'));
	}

	public function getData(array $inputs)
	{
		$this->db->select('sale_date, sum(subtotal) as subtotal, sum(total) as total, sum(tax) as tax,sum(profit) as profit');
		$this->db->from('sales_items_temp');
		if ($inputs['sale_type'] == 'sales')
		{
			$this->db->where('quantity_purchased > 0');
		}
		elseif ($inputs['sale_type'] == 'returns')
		{
			$this->db->where('quantity_purchased < 0');
		}

		$this->db->group_by('sale_date');
		$this->db->having('sale_date BETWEEN "'. $inputs['start_date']. '" and "'. $inputs['end_date'].'"');
		$this->db->order_by('sale_date');
		return $this->db->get()->result_array();
	}

	public function getData_inventory_summaryitem($location_code)
	{
		$query = $this->db->query("SELECT b.description,c.description AS cat_description,b.gender,
SUM(a.qty) AS total_qty, SUM(a.qty*a.price) AS total_price
,SUM(a.qty*a.standard_cost) AS total_standard_cost
FROM stock_moves AS a
LEFT JOIN stock_master AS b ON a.stock_id=b.stock_id
LEFT JOIN stock_category AS c ON b.category_id=c.category_id
WHERE a.loc_code='".$location_code."' AND b.no_sale=0
GROUP BY a.stock_id,a.lot_no,a.price");
		
		foreach ($query->result() as $row) {
			$sales_data[] = $row;
		}

		return $sales_data;
	}
	
	public function getData_inventory_summary($location_code)
	{
		$query = $this->db->query("SELECT
a.stock_id,
a.description AS description,
b.description AS cat_description,
a.category_id,
a.gender,
c.lot_no,
SUM(c.qty) AS qty,
c.price

FROM stock_master AS a 
LEFT JOIN stock_category AS b ON a.category_id=b.category_id
LEFT JOIN stock_moves AS c ON a.stock_id=c.stock_id
WHERE c.loc_code='".$location_code."' AND a.no_sale=0
GROUP BY c.stock_id,c.lot_no,c.price");
		
		foreach ($query->result() as $row) {
			$sales_data[] = $row;
		}

		return $sales_data;
	}
	
	public function getData_inventory_details($location_code)
	{
		$query = $this->db->query("SELECT
a.stock_id,
IF(a.item_size_id> 0, CONCAT(a.stock_id,' - ',a.description,' - ',p.size), CONCAT(a.stock_id,' - ',a.description)) AS description,
b.description AS cat_description,
a.category_id,
a.gender,
c.lot_no,
SUM(c.qty) AS qty,
c.price

FROM stock_master AS a 
LEFT JOIN stock_category AS b ON a.category_id=b.category_id
LEFT JOIN pos_item_sizes AS p ON a.item_size_id=p.id
LEFT JOIN stock_moves AS c ON a.stock_id=c.stock_id
WHERE c.loc_code='".$location_code."'  AND a.no_sale=0
GROUP BY c.stock_id,c.lot_no,c.price");
		
		foreach ($query->result() as $row) {
			$sales_data[] = $row;
		}

		return $sales_data;
	}
	
	public function getData_inventory_details_lot($location_code)
	{
		$query = $this->db->query("SELECT
a.stock_id,
IF(a.item_size_id> 0, CONCAT(a.stock_id,' - ',a.description,' - ',p.size), CONCAT(a.stock_id,' - ',a.description)) AS description,
b.description AS cat_description,
a.category_id,
a.gender,
c.lot_no,
SUM(c.qty) AS qty,
c.price

FROM stock_master AS a 
LEFT JOIN stock_category AS b ON a.category_id=b.category_id
LEFT JOIN pos_item_sizes AS p ON a.item_size_id=p.id
LEFT JOIN stock_moves AS c ON a.stock_id=c.stock_id
WHERE c.loc_code='".$location_code."'  AND a.no_sale=0
GROUP BY c.stock_id,c.lot_no,c.price");
		
		foreach ($query->result() as $row) {
			$sales_data[] = $row;
		}

		return $sales_data;
	}
	



	public function getSummaryData(array $inputs)
	{
		$this->db->select('sum(subtotal) as subtotal, sum(total) as total, sum(tax) as tax,sum(profit) as profit');
		$this->db->from('sales_items_temp');
		$this->db->where('sale_date BETWEEN "'. $inputs['start_date']. '" and "'. $inputs['end_date'].'"');
		if ($inputs['sale_type'] == 'sales')
		{
			$this->db->where('quantity_purchased > 0');
		}
		elseif ($inputs['sale_type'] == 'returns')
		{
			$this->db->where('quantity_purchased < 0');
		}
		return $this->db->get()->row_array();
	}

}
?>