<?php
require_once("report.php");
class Detailed_pos extends Report
{
	function __construct()
	{
		parent::__construct();
	}
	
	public function getDataColumns()
	{
		return array('summary' => array(array('data'=>'PO ID', 'align'=>'left'),array('data'=>'Mro ID', 'align'=>'left'),array('data'=>'Supplier', 'align'=>'left'),array('data'=>'Request Location', 'align'=>'left'),array('data'=>'Location', 'align'=>'left'),array('data'=>'PO Date', 'align'=>'left'), array('data'=>'Order Quantity', 'align'=>'left'), array('data'=>'Request By', 'align'=>'left'), array('data'=>'TERMS & CONDITIONS', 'align'=>'left'), array('data'=>lang('reports_comments'), 'align'=>'left')),
					'details' => array(array('data'=>lang('reports_name'), 'align'=>'left'),array('data'=>lang('items_product_id'), 'align'=> 'left'), array('data'=>lang('reports_category'), 'align'=>'left'), array('data'=>'Request Qty', 'align'=>'left'))
		);		
	}
	
	public function getData()
	{
		$data = array();
		$data['summary'] = array();
		$data['details'] = array();
	
		$this->db->select('po_id,mro_id, location_id,req_location_id, supplier_id, po_purpos, po_date, sum(quantity_purchased) as items_purchased, CONCAT(employee.first_name," ",employee.last_name) as employee_name, sum(total) as total, sum(profit) as profit, comment', false);
		$this->db->from('po_items_temp');
		$this->db->join('people as employee', 'po_items_temp.employee_id = employee.person_id');
			$this->db->where('quantity_purchased > 0');
		
		$this->db->where('deleted', 0);
		$this->db->group_by('po_id');
		$this->db->order_by('po_date');

			//If we are exporting NOT exporting to excel make sure to use offset and limit
		if (isset($this->params['export_excel']) && !$this->params['export_excel'])
		{
			$this->db->limit($this->report_limit);
			$this->db->offset($this->params['offset']);
		}		
		
		foreach($this->db->get()->result_array() as $receiving_summary_row)
		{
			$data['summary'][$receiving_summary_row['po_id']] = $receiving_summary_row; 
		}
		$receiving_ids = array();
		
		foreach($data['summary'] as $receiving_row)
		{
			$receiving_ids[] = $receiving_row['po_id'];
		}

		$this->db->select('name,po_id, mro_id, location_id, po_purpos, po_date, items.category, quantity_purchased, serialnumber,total, discount_percent,items.product_id', false);
		$this->db->from('po_items_temp');
		$this->db->join('items', 'po_items_temp.item_id = items.item_id');
		//$this->db->join('locations', 'receivings_items_temp.new_location_id = locations.locations_id', 'left');

		if (!empty($receiving_ids))
		{
			$this->db->where_in('po_id', $receiving_ids);
		}
		else
		{
			$this->db->where('1', '2', FALSE);		
		}
		foreach($this->db->get()->result_array() as $receiving_item_row)
		{
			$data['details'][$receiving_item_row['po_id']][] = $receiving_item_row;
		}
		

		return $data;
	}
	
	public function getTotalRows()
	{		
		$this->db->select("COUNT(DISTINCT(po_id)) as po_count");
		$this->db->from('po_items_temp');
		
			$this->db->where('quantity_purchased > 0');
		
		$this->db->where('po_items_temp.deleted', 0);
		$ret = $this->db->get()->row_array();
		//echo $this->db->last_query();
		return $ret['po_count'];

	}
	
	public function getSummaryData()
	{
		$this->db->select('sum(total) as total', false);
		$this->db->from('po_items_temp');
		if ($this->params['sale_type'] == 'sales')
		{
			$this->db->where('quantity_purchased > 0');
		}
		elseif ($this->params['sale_type'] == 'returns')
		{
			$this->db->where('quantity_purchased < 0');
		}
		$this->db->where('deleted', 0);
		return $this->db->get()->row_array();
	}
}
?>