<?php
require_once("report.php");
class Detailed_expences extends Report
{
	function __construct()
	{
		parent::__construct();
	}
	
	public function getDataColumns()
	{
		return array('summary' => array(array('data'=>'Expense ID', 'align'=>'left'),array('data'=>'Ref #', 'align'=>'left'),array('data'=>'Paid To', 'align'=>'left'),array('data'=>'Location', 'align'=>'left'),array('data'=>'Expense Date', 'align'=>'left'),array('data'=>"Expense Type", 'align'=>'left'), array('data'=>"Title", 'align'=>'left'), array('data'=>'Created By ', 'align'=>'left'), array('data'=>'Description', 'align'=>'left'), array('data'=>'Amount', 'align'=>'left'))
		);		
	}
	
	public function getData()
	{
		$data = array();
		$data['summary'] = array();
		$data['details'] = array();
	
		$this->db->select('expence_id, location_id, title, expence_time,expence_type,ref_no,paid_to,  CONCAT(first_name," ",last_name) as employee_name, sum(amount) as total, comments', false);
		$this->db->from('expences_items_temp');
		
		$this->db->group_by('expence_id');
		$this->db->order_by('expence_time');

			//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 $expence_summary_row)
		{
			$data['summary'][$expence_summary_row['expence_id']] = $expence_summary_row; 
		}
		
		$expence_ids = array();
		
		foreach($data['summary'] as $expence_row)
		{
			$expence_ids[] = $expence_row['expence_id'];
		}
		return $data;
	}
	
	public function getTotalRows()
	{		
		$this->db->select("COUNT(DISTINCT(expence_id)) as expences_count");
		$this->db->from('expences_items_temp');
		
		
		$this->db->where('expences_items_temp.deleted', 0);
		$ret = $this->db->get()->row_array();
		//echo $this->db->last_query();
		return $ret['expences_count'];

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