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

class systemgeneral_model extends CI_Model{
	
    public function sysgetall_bycompanyID(){
//default general setup
			$sql = 'insert into comp_control_acct_setup (refID, groupDisplay, companyID, accountCode,accountID, orderNo,createdby)  select ID, groupDisplay, '.element('compID', $this->session->userdata('logged_in')).',name, (select id from comp_chart_of_acct where companyid='.element('compID', $this->session->userdata('logged_in')).' and acctcode=_ref_control_acct_setup.defaultAcctCode) as accountID, orderNo,1 from _ref_control_acct_setup where name not in (select accountCode from comp_control_acct_setup where companyID = '.element('compID', $this->session->userdata('logged_in')).') ';
			$this->db->query($sql);

//default tax master
			$sql='insert into tax_master (companyid, code,name,taxtype,taxpercentage,taxClass,taxgroup,description,taxaccount,status,createdby) select '.element('compID', $this->session->userdata('logged_in')).',code,name,taxtype,taxpercentage,taxClass,taxgroup,description,(select id from comp_chart_of_acct where companyid='.element('compID', $this->session->userdata('logged_in')).' and acctcode= _ref_tax_master.defaultTaxAccount) as taxaccount,status,createdby from _ref_tax_master where code not in (select code from tax_master where companyid ='.element('compID', $this->session->userdata('logged_in')).')';
			$this->db->query($sql);
//			echo $this->db->last_query(); 

		//update record to readonly if transaction exist in journal 
		$sql = 'update comp_control_acct_setup set updateable = 0 where companyID = '. element('compID', $this->session->userdata('logged_in'));
		$sql .= ' and updateable = 1 ';
		$sql .= ' and accountID > 0 '; 
		$sql .= ' and accountID in (select distinct acctCode from comp_journal_detail where companyid = '.element('compID', $this->session->userdata('logged_in'));
		$sql .= ' )';
		$this->db->query($sql); 
		$this->db->select('groupDisplay,ID,accountCode,updateable,accountID');
		$this->db->order_by('groupDisplay, orderNo');
	     $this->db->where('is_delete', 0);
		 $this->db->where('companyID', element('compID', $this->session->userdata('logged_in')));
		 $query = $this->db->get('comp_control_acct_setup');
//		 echo $this->db->last_query(); 
		if($query->num_rows() >= 1){
			return $query->result();
		}
		
/*		else{
			//no data. insert from system default ref. 
			$sql = 'insert into comp_control_acct_setup (companyID, accountCode,orderNo,createdby)  select '.element('compID', $this->session->userdata('logged_in')).',name,orderNo,1 from master_code where masterID = 21 and accountCode not in (select accountCode from comp_control_acct_setup where companyID = '.element('compID', $this->session->userdata('logged_in')).') ';
			$this->db->query($sql);
//			echo $this->db->last_query(); 
			$this->db->where('companyID', element('compID', $this->session->userdata('logged_in')));
			$query = $this->db->get('comp_control_acct_setup');
			
			return $query->result();
		}
*/	}
	
	public function get_byCode($Code){
	  $this->db->where('companyID', element('compID', $this->session->userdata('logged_in')));
	    $this->db->where('accountCode', $Code);
		$query = $this->db->get('comp_control_acct_setup', 1);		
		if($query->num_rows() == 1){
			return $query->result();
		}else{
			return false;
		}
	}
	
	//Function to check if transaction exist.
	public function get_if_transactin_exist($accountID){ 
	  $this->db->where('companyID', element('compID', $this->session->userdata('logged_in')));
		$this->db->where('acctID', $accountID);
		$query = $this->db->get('comp_acc_det', 1);		
		if($query->num_rows() > 0)
		{
			return true;
		}else{
			return false;
		}
	}
	
	//Function to check if record exist.
	public function get_if_record_exist($accountCode){ 
	  $this->db->where('companyID', element('compID', $this->session->userdata('logged_in')));
		$this->db->where('accountID', 0);
		$this->db->where_or('accountID', isnull);
		$query = $this->db->get('comp_control_acct_setup', 1);		
//		echo $this->db->last_query();
		if($query->num_rows() > 0)
		{
			return true;
		}else{
			return false;
		}
	}
	
	public function sysupdate_byID($ID, $data){
		$this->db->where('ID', $ID);
		$query = $this->db->update('comp_control_acct_setup', $data); 
		if($query === TRUE){
			return TRUE;
		} else {
			return FALSE;
		}
	}
	
	public function updateAccountCode($dataUpdate) {
//		var_dump($dataUpdate);
		$this->db->set('accountID',$dataUpdate[0]);
		$this->db->where('ID',$dataUpdate[1]);
		$this->db->update('comp_control_acct_setup');
		echo '<br>'.$this->db->last_query(); 
		return;
	}
	
	public function updateAccountCode1($id, $val) {
		$this->db->set('accountID',$val);
		$this->db->where('ID',$id);
		$this->db->where('updateable',1);
		$this->db->update('comp_control_acct_setup');
		echo '<br>'.$this->db->last_query(); 
		return;
	}

	public function syssave($dataupdate){
		//check if record exist = update else insert
		$exist = $this->get_if_record_exist($accountCode);
		if ($exist) {
			$this->db->where('ID', $ID);
			$query = $this->db->update('comp_control_acct_setup', $dataupdate); 
		} else{
			$sql = $this->db->insert_string('comp_control_acct_setup', $dataupdate);
			$query = $this->db->query($sql);		
		}		
		if($query === TRUE){
			return TRUE;
		} else {
			return FALSE;
		}
	}
	
	public function complete_setup_bycompanyID() {
		$this->db->where('companyID', element('compID', $this->session->userdata('logged_in')));
		$this->db->where('accountID',0);
		$query = $this->db->get('comp_control_acct_setup');		
		//echo $this->db->last_query(); 
		if($query->num_rows() > 0)
		{
			return TRUE;
		}else{
			return FALSE;
		}
	}
}