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

	public function getDataColumns()
	{
		/////Select Maximum Col/////

		$this->db->select('DISTINCT(payment_type)', false);
		$this->db->from('sales_payments');
		$this->db->order_by('payment_type','ASC');
		$col_data = array();
		$col_data = $this->db->get()->result_array();

		$col_data_final=array();

		$col_data_final[]="Date Time";
		foreach ($col_data as $key => $value) {
			$col_data_final[]=trim($value['payment_type']);
		}
		//  return array($this->lang->line('reports_customer'), "Phone", "Email Address", "Advance Amount", "Due Amount");
		return $col_data_final;
	}

	public function getData(array $inputs)
	{

		/////Select Maximum Col/////

		$this->db->select('DISTINCT(payment_type)', false);
		$this->db->from('sales_payments');
		$this->db->order_by('payment_type','ASC');
		$col_data = array();
		$col_data = $this->db->get()->result_array();

		$col_data_final=array();

		$col_data_final['sale_time']="";
		foreach ($col_data as $key => $value) {
			$col_data_final[trim($value['payment_type'])]="";
		}

		$this->db->select('sales.sale_time,sales_payments.payment_amount,sales_payments.payment_type', false);
		$this->db->from('sales');
		$this->db->join('sales_payments', 'sales_payments.sale_id = sales.sale_id');
		$this->db->where('sale_time BETWEEN "'. $inputs['start_date']. ' 00:00:00" and "'. $inputs['end_date'].' 23:59:59" and customer_id='.$inputs['customer_id']);

		$this->db->order_by('sale_time','DESC');

		$data = array();
		$data= $this->db->get()->result_array();
		$final_data=array();
		foreach ($data as $data_key => $data_value) {
			$temp_col_data=array();
			$temp_col_data=$col_data_final;
			foreach ($data_value as $d_key => $d_value) {
				if($d_key=="sale_time"){
					$temp_col_data['sale_time']=$d_value;
				}elseif ($d_key=="payment_type"){
					foreach ($col_data_final as $c_key => $c_value) {
						if($c_key==$d_value){
							$temp_col_data[$d_value]=to_currency($data_value['payment_amount']);
						}
					}
				}
			}
			$final_data[$data_value['sale_time']][]=$temp_col_data;
		}

		//////final////////

		$final_final_data=array();


		if(count($final_data)){
			foreach ($final_data as $final_key => $final_value) {
				$final_final_data_temp=$col_data_final;
				foreach ($final_value as $f_key => $f_value) {
					foreach ($f_value as $ff_key => $ff_value) {
						if(trim($ff_value)){
							$final_final_data_temp[$ff_key]=$ff_value;
						}
					}
				}
				$final_final_data[]=$final_final_data_temp;
			}
		}

		return $final_final_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'].'" and customer_id='.$inputs['customer_id']);
		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();
	}
}
?>