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

class customerstatement_model extends CI_Model{
	
public function customerstatement_all(){
	    $this->db->where('is_delete', 0);
	    //$this->db->where('companyID', element('compID', $this->session->userdata('logged_in')));
		$query = $this->db->get('view_customer_statement');
		if($query->num_rows() >= 1){
			return $query->result();
		}else{
			return FALSE;
		}
	}
	public function customer_statement_id($ID){
		$this->db->where('ID', $ID);
	    $this->db->where('is_delete', 0);
	   // $this->db->where('companyID', element('compID', $this->session->userdata('logged_in')));
		$query = $this->db->get('view_customer_statement');
		if($query->num_rows() >= 1){
			return $query->result();
		}else{
			return FALSE;
		}
	}
	

	public function getOpening($dateFrom = '', $dateTo = '', $ID = ''){
		$yearMonth = date('Ym',strtotime($dateFrom));
		$query = $this->db->select('balance')
						->from('comp_customer_closing')
						->where(array('companyID' => element('compID', $this->session->userdata('logged_in')),'supplierID'=>$ID,'yearMonth'=>$yearMonth))
						->get();
		if($query->num_rows() >=1 ){
			$result = $query->result();
			return $result[0]->balance;
		}else{
			return 0;			
		}		
						
	}
	
	public function customerstatementPrintDetails_ID($ID){
		$this->db->select('a.*');
		$this->db->select('b.*');
		$this->db->select('a.formNo as customerStatementNo', false);
		$this->db->where('a.ID', $ID);
		$this->db->where('a.companyID', element('compID', $this->session->userdata('logged_in')));
		$this->db->from('view_customer_statement a');
		$this->db->join('customer b', 'b.ID = a.customerID');
		$query = $this->db->get();
		if($query->num_rows() >= 1){
			return $query->result();
		}else{
			return false;
		}
	}

	public function customerstatementList_date($dateFrom, $dateTo){
	    //$this->db->where('is_delete', 0);
		$this->db->where('view_customer_statement.companyID', element('compID', $this->session->userdata('logged_in')));
		$this->db->where('transDate between  "'.$dateFrom.'" and "'.$dateTo.'"');
		$this->db->select('sum(dr) as dr');
		$this->db->select('sum(cr) as cr');
		$this->db->from('view_customer_statement');
		$this->db->select('customer.*');
//		$this->db->select('supplier.supplierCode as supplierCode');
		$this->db->join('customer', 'customer.ID = view_customer_statement.customerID');
		$this->db->order_by('transDate desc');
	    //$this->db->where('companyID', element('compID', $this->session->userdata('logged_in')));
		$query = $this->db->get();
		//echo $this->db->last_query();
		if($query->num_rows() >= 1){
			return $query->result();
		}else{
			return FALSE;
		}
	}
		
	public function customerstatement_date($dateFrom, $dateTo){
	    //$this->db->where('is_delete', 0);
		$this->db->where('view_customer_statement.companyID', element('compID', $this->session->userdata('logged_in')));
		$this->db->where('transDate between  "'.$dateFrom.'" and "'.$dateTo.'"');
		$this->db->select('view_customer_statement.*');
		$this->db->from('view_customer_statement');
		$this->db->select('customer.customerName as customerName');
		$this->db->select('customer.customerCode as customerCode');
		$this->db->join('customer', 'customer.ID = view_customer_statement.customerID');
		$this->db->order_by('transDate desc');
	    //$this->db->where('companyID', element('compID', $this->session->userdata('logged_in')));
		$query = $this->db->get();
		if($query->num_rows() >= 1){
			return $query->result();
		}else{
			return FALSE;
		}
	}
	
	public function customerstatement_name($dateFrom, $dateTo, $ID){
	    //$this->db->where('is_delete', 0);
		$this->db->where('view_customer_statement.companyID', element('compID', $this->session->userdata('logged_in')));
		$this->db->where('transDate between  "'.$dateFrom.'" and "'.$dateTo.'"');
		$this->db->select('view_customer_statement.*');
		$this->db->from('view_customer_statement');
		$this->db->select('customer.customerName as customerName');
		$this->db->where('customer.ID', $ID);
		$this->db->select('customer.customerCode as customerCode');
		$this->db->select('customer.customerName');
		$this->db->join('customer', 'customer.ID = view_customer_statement.customerID');
		$this->db->order_by('transDate ASC');
		
		$query = $this->db->get();
		if($query->num_rows() >= 1){
			return $query->result();
		}else{
			return FALSE;
		}
	}
	
	public function customerStat_byID($ID){
		$this->db->where('customerID', $ID);
		$query = $this->db->get('view_customer_statement');
		if($query->num_rows() >= 1){
			return $query->result();
		}else{
			return false;
		}
	 }
	
/* //statement should no update/delete -akmal
	public function supplier_statement_edit($updatestat, $ID){
		$this->db->where('ID', $ID);
		$query = $this->db->update('comp_supplier_statement', $updatestat); 
		if($query === TRUE){
			return TRUE;
		} else {
			return FALSE;
		}
	}
	
	public function supplier_statement_insert($newstatement){
		$sql = $this->db->insert_string('comp_supplier_statement', $newstatement);
		$query = $this->db->query($sql);
		if($query === TRUE){
			return TRUE;
		} else {
			$last_query = $this->db->last_query();
			return $last_query;
		}
	}
	public function suppplier_statement_delete($deletepstat, $ID){
		$this->db->where('ID', $ID);
		$query = $this->db->update('comp_supplier_statement', $deletestat); 
		if($query === TRUE){
			return TRUE;
		} else {
			return FALSE;
		}
	}
	*/
}

