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

class Customer_model extends CI_Model{
	
  	public function customermaster_all(){
		$this->db->where('is_delete', 0);
		$this->db->where('companyID', element('compID', $this->session->userdata('logged_in')));
		$query = $this->db->get('customer');
		if($query->num_rows() >= 1){
			return $query->result();
		}else{
			return FALSE;
		}
	}
	
	public function customermaster_all_state(){
		$this->db->select('customer.*');
		$this->db->select('_ref_state.fldname');
	    $this->db->where('is_delete', 0);
	    $this->db->where('companyID', element('compID', $this->session->userdata('logged_in')));
		$this->db->from('customer');
		$this->db->join('_ref_state', '_ref_state.fldid = customer.customerStateID');
		$query = $this->db->get();
		if($query->num_rows() >= 1){
			return $query->result();
		}else{
			return false;
		}
	}
	
public function customermaster_id($ID){
	    $this->db->where('companyID', element('compID', $this->session->userdata('logged_in')));
	    $this->db->where('ID', $ID);
		$query = $this->db->get('customer', 1);		
		if($query->num_rows() == 1){
			return $query->result();
		}else{
			return false;
		}
	}
	
	
	public function customermaster_insert($newuser){
		$sql = $this->db->insert_string('customer', $newuser);
		$query = $this->db->query($sql);
		if($query === TRUE){
			return TRUE;
		} else {
			$last_query = $this->db->last_query();
			return $last_query;
		}
	}
	public function customermaster_edit($updateuser, $ID){
		$this->db->where('ID', $ID);
		$query = $this->db->update('customer', $updateuser); 
		if($query === TRUE){
			return TRUE;
		} else {
			return FALSE;
		}
	}

	public function customermaster_delete($deletuser, $ID){
		$this->db->where('ID', $ID);
		$query = $this->db->update('customer', $ID); 
		if($query === TRUE){
			return TRUE;
		} else {
			return FALSE;
		}
	}
	
	
		public function customermaster_allopt(){
		$this->db->select('ID');
		$this->db->select('customerName');
	    $this->db->where('companyID', element('compID', $this->session->userdata('logged_in')));
		$this->db->where('is_delete', 0);
		$query = $this->db->get('customer');
    	if($query->result()){
       		$result = $query->result();
        	return $result;
    	} 	
	}
	public function customerAging_byID($id){
		$this->db->where('customerID', $id);
		$query = $this->db->get('view_customer_aging'); 
		if($query == TRUE){
			return $query->result();
		} else {
			return FALSE;
		}
	}
	public function customerStatement_byID($id){
		$this->db->where('customerID', $id);
		$this->db->order_by('parent, seq, transDate');
		$query = $this->db->get('view_customer_statement');
		if($query == TRUE){
			return $query->result();
		} else {
			return FALSE;
		}
	}
	public function loadCustomerDefaultValue($id){
		$sql='SELECT cu.fldcurr_code As currencyWord, comp_currency_conv.rateConv as exchangeRate, cus.currencyID '
                        . 'FROM customer cus, _ref_currency cu LEFT OUTER JOIN comp_currency_conv  ON comp_currency_conv.currencyTo=cu.fldid '
                        . ' WHERE  comp_currency_conv.startDate<="'.date('Y-m-d').'" And comp_currency_conv.endDate >="'.date('Y-m-d').'" '
                        . ' And cus.ID = '.$id.' AND comp_currency_conv.currencyTo= cu.fldid ';
		$query = $this->db->query($sql);

		if($query->num_rows() >=1){
			return $query->result();
		}else{
			return FALSE;
		}
	}
	
}