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

class Bank_model extends CI_Model{

	public function bank_all(){
		$this->db->where('is_delete', 0);
		$this->db->where('companyID', element('compID', $this->session->userdata('logged_in')));
		$query = $this->db->get('comp_bank');
		if($query->num_rows() >= 1){
			return $query->result();
		}else{
			return FALSE;
		}
	}

	//Get bank list 
	// same for all companies
	//akmal 20160610 //i remove the condition same for all companies because different company will use different bank. 
	public function bank_all_withoutparent(){
		$this->db->select('a.*')
				->where('a.is_delete', 0);
		$this->db->where(array(	
							'a.companyID'=> element('compID', $this->session->userdata('logged_in')),  
							'b.parentID >' => 0
						));
		$this->db->join('comp_chart_of_acct c','c.ID=a.linkAccountID','left');
		$this->db->join('_ref_chart_of_acct b','b.acctCode=c.acctCode','left');
		$query = $this->db->get('comp_bank a');
		if($query->num_rows() >= 1){
			return $query->result();
		}else{
			return FALSE;
		}
	}

	public function bank_all_withAcctCode(){
		$query = $this->db
						->select('ca.*,ca.ID caID,b.*,b.ID bankID')
						->where('ca.acctCode >', 220000000)
						//->where('b.is_delete', 0)
						->where('b.companyID', element('compID', $this->session->userdata('logged_in')))
						->join('comp_chart_of_acct ca','ca.ID = b.linkAccountID','LEFT')
						->get('comp_bank b');

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

	public function bank_report(){
		$this->db->select("concat(bankName,'<br>',line1,'<br>',line2,'<br>',line3,'<br>',PostCode,' ',city) as bankName, branch, bankAccountNo,phoneNumber1,fax, email",false);
		$this->db->where('is_delete', 0);
		$this->db->where('companyID', element('compID', $this->session->userdata('logged_in')));
		$query = $this->db->get('comp_bank');

		if($query->num_rows() >= 1){
			return $query->result();
		}else{
			return FALSE;
		}
	}
	public function bank_insert($newbank){
		$sql = $this->db->insert_string('comp_bank', $newbank);
		$query = $this->db->query($sql);
		if($query === TRUE){
			return TRUE;
		} else {
			$last_query = $this->db->last_query();
			return $last_query;
		}
	}
	public function bank_edit($updatebank, $ID){
		$this->db->where('ID', $ID);
		$query = $this->db->update('comp_bank', $updatebank);
		if($query === TRUE){
			return TRUE;
		} else {
			return FALSE;
		}
	}
	
	public function bank_edit2($updatebank, $ID){
		$this->db->where('ID', $ID);
		$query = $this->db->update('comp_chart_of_acct', $updatebank);
		if($query === TRUE){
			return TRUE;
		} else {
			return FALSE;
		}
	}

	public function bank_delete($deletebank, $ID){
		$this->db->where('ID', $ID);
		$query = $this->db->update('comp_bank', $deletebank);
		if($query === TRUE){
			return TRUE;
		} else {
			return FALSE;
		}
	}

	public function bank_id($ID){
	    $this->db->where('ID', $ID);
		$query = $this->db->get('comp_bank', 1);
		//echo $this->db->last_query();
		if($query->num_rows() == 1){
			return $query->result();
		}else{
			return false;
		}
	}

	public function bank_linkaccountid($id){
		$query = $this->db
						->where('linkAccountID',$id)
						->where('is_delete', 0)
						->where('companyID', element('compID', $this->session->userdata('logged_in')))
						->get("comp_bank")->result();
		return $query;
	}

		//public function bank_allopt(){
		//$this->db->select('ID');
		//$this->db->select('bankName');
	   // $this->db->where('companyID', element('compID', $this->session->userdata('logged_in')));
		//$this->db->where('is_delete', 0);
		//$this->db->order_by('bankName');
		//$query = $this->db->get('comp_bank');
    	//if($query->result()){
       		//$result = $query->result();
        	//return $result;
    	//}
	//}
}
