<?php
require_once("report.php");
class Detailed_mros extends Report
{
	function __construct()
	{
		parent::__construct();
	}
	
	public function getDataColumns()
	{
		return array('summary' => array(array('data'=>'Mro ID', 'align'=>'left'),array('data'=>'Location', 'align'=>'left'),array('data'=>'Request Date', 'align'=>'left'), array('data'=>'Total Item REQUEST', 'align'=>'left'), array('data'=>'Request By', 'align'=>'left'), array('data'=>'Purpose', '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('mro_id, location_id, mro_purpos, mro_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('mros_items_temp');
		$this->db->join('people as employee', 'mros_items_temp.employee_id = employee.person_id');
			$this->db->where('quantity_purchased > 0');
		
		$this->db->where('deleted', 0);
		$this->db->group_by('mro_id');
		$this->db->order_by('mro_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['mro_id']] = $receiving_summary_row; 
		}
		$receiving_ids = array();
		
		foreach($data['summary'] as $receiving_row)
		{
			$receiving_ids[] = $receiving_row['mro_id'];
		}

		$this->db->select('name, mro_id, location_id, mro_purpos, mro_date, items.category, quantity_purchased, serialnumber,total, discount_percent,items.product_id', false);
		$this->db->from('mros_items_temp');
		$this->db->join('items', 'mros_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('mro_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['mro_id']][] = $receiving_item_row;
		}
		

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

	}
	
	public function getSummaryData()
	{
		$this->db->select('sum(total) as total', false);
		$this->db->from('mros_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();
	}
}
?>