<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Saleschart_model extends CI_Model{
	
	public function Sales_all_date_byCustomer($startDate, $endDate){
		$this->db->select('a.*');
		$this->db->select('b.customerName');
		$this->db->select('b.attention');
		$this->db->select('b.phoneNumber1');
		$this->db->select('sum(c.amount * a.exchangeRate) as customerTotal');
		$this->db->where('a.companyID', element('compID', $this->session->userdata('logged_in')));
		$this->db->where('a.deliveryOrderDate >=', $startDate);
		$this->db->where('a.deliveryOrderDate <=', $endDate);
		$this->db->order_by('a.companyID');
		$this->db->order_by('a.customerID');
		$this->db->group_by('a.customerID');
		$this->db->from('comp_delivery_order a');
		$this->db->join('customer b', 'b.ID = a.customerID');	
		$this->db->join('comp_delivery_detail c', 'c.deliveryID = a.ID', 'left outer');	
//		$this->db->join('comp_sales_order b', 'b.customerID = a.ID AND salesOrderDate >='.$startDate.' AND salesOrderDate >='.$endDate);	
		$query = $this->db->get();
		if($query->num_rows() >= 1){
			$total = 0;
			$i = 0;
			$dataexpenses = '';
			foreach($query->result() as $row){
				$total =  $total + $row->customerTotal;
			}
			foreach($query->result() as $row){
				$dataexpenses[$i] = array(
					'label' => $row->customerName,
					'data' => $row->customerTotal / $total,
					'customerTotal' => $row->customerTotal,
					'customerName' => $row->ID
					);
				$i++;
			}
			return $dataexpenses;
		}else{
			$dataexpenses = NULL;
			return $dataexpenses;
		}
	}
	public function Sales_all_date_byItem($startDate, $endDate){
		$this->db->select('a.ID');
		$this->db->select('a.customerName');
		$this->db->select('a.attention');
		$this->db->select('a.phoneNumber1');
		$this->db->select('c.fldname');
		$this->db->select('d.fldcountry');
		$this->db->select('sum(b.totalAmount * exchangeRate) as customerTotal');
		$this->db->where('a.companyID', element('compID', $this->session->userdata('logged_in')));
		$this->db->order_by('a.customerName');
		$this->db->group_by('a.customerName');
		$this->db->from('customer a');
		$this->db->join('comp_sales_order b', 'b.customerID = a.ID AND b.salesOrderDate >='.$startDate.' AND b.salesOrderDate >='.$endDate);	
		$this->db->join('_ref_state c', 'c.fldid = a.customerStateID');	
		$this->db->join('_ref_country d', 'd.fldid = a.customerCountryID');	
//		$this->db->join('comp_sales_order b', 'b.customerID = a.ID AND salesOrderDate >='.$startDate.' AND salesOrderDate >='.$endDate);	
		$query = $this->db->get();
		if($query->num_rows() >= 1){
			$total = 0;
			$i = 0;
			$dataexpenses = '';
			foreach($query->result() as $row){
				$total =  $total + $row->customerTotal;
			}
			foreach($query->result() as $row){
				$dataexpenses[$i] = array(
					'label' => $row->customerName,
					'data' => $row->customerTotal / $total,
					'customerTotal' => $row->customerTotal,
					'customerName' => $row->ID
					);
				$i++;
			}
			return $dataexpenses;
		}else{
			$dataexpenses = NULL;
			return $dataexpenses;
		}
	}

}
/* End of file glchart.php */
/* Location: ./application/model/compAcctDet.php */
/* Created By : Yahaya Abdollah */
