<?php
require_once("report.php");
class Summary_duecustomers extends Report
{
	function __construct()
	{
		parent::__construct();
	}
	
	public function getDataColumns()
	{
		return array($this->lang->line('reports_customer'), "Phone", "Email Address", "Advance Amount", "Due Amount");
	}
	
	public function getData(array $inputs)
	{
		$null_previous_due=0;
		$this->db->select('CONCAT(first_name, " ", last_name) AS customer, people.phone_number, people.email,(CASE WHEN previous_due < "0" THEN previous_due*(-1) ELSE 0 END) AS advance,(CASE WHEN previous_due > "0" THEN previous_due ELSE 0 END) AS due', false);
		$this->db->from('customers');
		$this->db->join('people', 'customers.person_id = people.person_id');
		$this->db->where('previous_due <> '.$null_previous_due);
		//$this->db->where('sale_date BETWEEN "'. $inputs['start_date']. '" and "'. $inputs['end_date'].'"');
		//$this->db->group_by('customer_id');
		$this->db->order_by('first_name');
		/*
		SELECT CONCAT(first_name, " ", last_name) AS customer,(CASE WHEN previous_due < '0' THEN previous_due*(-1) ELSE 0 END) AS advance,(CASE WHEN previous_due > '0' THEN previous_due ELSE 0 END) AS due


FROM (`ospos_customers`)
JOIN `ospos_people` ON `ospos_customers`.`person_id` = `ospos_people`.`person_id`
ORDER BY `last_name`
		
		
		
		echo "<pre>";
		print_r($this->db->get()->result_array());
		echo "</pre>";
		exit();
		*/
		return $this->db->get()->result_array();		
	}
	
	public function getSummaryData(array $inputs)
	{
		$null_previous_due=0;
		$this->db->select('person_id,(CASE WHEN previous_due < "0" THEN previous_due*(-1) ELSE 0 END) AS advance,(CASE WHEN previous_due > "0" THEN previous_due ELSE 0 END) AS due', false);
		$this->db->from('customers');
		$this->db->where('previous_due <> '.$null_previous_due);
		$data_array=$this->db->get()->result_array();	
		
		$total_customer=0;
		$total_advance=0;
		$total_due=0;
		
		if(count($data_array)){
			
			foreach ($data_array as $data_key => $data_value) {
				$total_customer=$total_customer+1;
				$total_advance=$total_advance+$data_value['advance'];
				$total_due=$total_due+$data_value['due'];
			}
		}
		$summary_array=array();
		
		$summary_array['total_customer']=$total_customer;
		$summary_array['total_advance']=$total_advance;
		$summary_array['total_due']=$total_due;
		return $summary_array;
	}
}
?>