<?php
class M_coa extends CI_Model {
    // deklarasi nama tabel
    public $company         = 'am_company';
    public $table           = "am_accountgl";
    public $accountsubtype  = 'am_accountsubtype';
	public $bankbook  		= 'at_bankbook';
	public $companygl  		= 'at_compaccountgl';
	public $av_all_transaction = 'av_all_transaction';
		
    function get_all() {
        $this->db->_protect_identifiers = FALSE;
        $this->db->select("AccountNumber, AccountName, AccountLevel, AccountChild, AccountTypeName, ItemCategory");
        $this->db->from($this->table);
        $this->db->join('am_accountsubtype st','am_accountgl.AccountSubTypeID = st.AccountSubTypeID', 'LEFT');
//        $this->db->where('TransStatus', 2);
//        $this->db->where('AppLevel1Status', 1);
//        $this->db->where('AppLevel2Status', 1);	
//        $this->db->where('AccountChild', 0);	
        
//        if($account_sub_type == 0 || $account_sub_type = ''){
//            $this->db->where('st.AccountSubTypeID', $account_sub_type_id);            
//        }
        
        $this->db->order_by('AccountNumber','ASC');		
        $query=$this->db->get();	
        $this->db->_protect_identifiers = TRUE;		
        
        return $query->result();						
    }
	
	function get_company_coa(){
        $this->db->select('am_company.CompanyCode as CompanyCode, CompanyName, am_city.CityName');
        $this->db->from($this->company);
        $this->db->join('am_city', 'am_city.CityCode = am_company.CompanyCity', 'INNER');
		
        $this->db->order_by('am_company.CompanyCode', 'ASC');
        $query = $this->db->get();
        
        return $query->result();
    }
	
    function get_company_branch(){
        $this->db->select('am_company.CompanyCode as CompanyCode, CompanyName, CompBranchStatus, BranchCode, BranchName, BranchStatus');
        $this->db->from($this->company);
        $this->db->join('am_branch', 'am_branch.CompanyCode = am_company.CompanyCode', 'INNER');
        $this->db->where('am_branch.BranchStatus', 0);
        $this->db->order_by('am_company.CompanyCode', 'ASC');
        $this->db->order_by('am_branch.BranchCode', 'ASC');
        $query = $this->db->get();
        
        return $query->result();
    }
    
	/*===============function ini untuk company gl akun jika dia di selectd==========================================================*/
	/*function get_company_branch_grid(){
        $this->db->select('am_company.CompanyCode as CompanyCode, CompanyName, CompBranchStatus, BranchCode, BranchName, BranchStatus');
        $this->db->from($this->company);
        $this->db->join('am_branch', 'am_branch.CompanyCode = am_company.CompanyCode', 'INNER');
        //$this->db->where('am_branch.BranchStatus', 0);
        $this->db->order_by('am_company.CompanyCode', 'ASC');
        $this->db->order_by('am_branch.BranchCode', 'ASC');
        $query = $this->db->get();
        
        return $query->result();
    }*/
	/*======================================end======================================================================================*/
	
    function get_all_num_rows() {
        $query = $this->db->get($this->table);
        return $query->result();						
    }
    
    function get_by_id($account_number) {
        $query = $this->db->get_where($this->table, array('AccountNumber' => $account_number));
        
        if($query->num_rows() > 0){
            return $query->row();
        }
        else{
            return false;
        }
    }
	
    function get_account_type() {
        $this->db->order_by('AccountTypeName', 'ASC');
        $query = $this->db->get('am_accountsubtype');

        if($query->num_rows()>0){
            return $query->row();
        }
        else{
            return false;
        }
    }

    function get_account_subtype_wo_bank(){
        $this->db->where('AccountSubTypeID != ', 7);
        $this->db->order_by('AccountTypeName', 'Asc');                
        $query = $this->db->get($this->accountsubtype);
        
        return $query->result();
    }
    
    function get_coa_wo_bank($value,$field, $sub_type_parent_id) {
        $this->db->_protect_identifiers = true;
		
		$this->db->select('ag.AccountNumber, ag.AccountName, ag.AccountLevel, ag.AccountChild, st.AccountTypeName, st.AccountSubTypeID');
        $this->db->from('am_accountgl ag');
		
        $this->db->join('am_accountsubtype st', 'st.AccountSubTypeID = ag.AccountSubTypeID', 'INNER');
        $this->db->where('ag.TransStatus', 2);
		$this->db->like('ag.'.$field, $value);
		
		if($sub_type_parent_id != null){
			//$this->db->where('ag.AccountChild', 0);
			$this->db->where("(st.SubTypeParentID = $sub_type_parent_id OR st.AccountSubTypeID = $sub_type_parent_id)", NULL, FALSE);
			//$this->db->where('st.SubTypeParentID', $sub_type_parent_id);
			//$this->db->or_where('st.AccountSubTypeID', $sub_type_parent_id);
		}
		else if($sub_type_parent_id != 7){
			$this->db->where("(ag.AccountChild <> 1 OR st.AccountTypeName <> 'Bank')", NULL, FALSE);
		}
		else{
			$this->db->order_by('ag.AccountNumber', 'ASC');
		}
		
        $query = $this->db->get();
        
		if($query->num_rows() > 0){
            return $query->result();
        }
        else{
            return false;
        }
		/*
		
        $select_bank =  "SELECT ag.AccountNumber, ag.AccountName, ag.AccountLevel, ag.AccountChild, st.AccountTypeName, st.AccountSubTypeID
                         FROM am_accountgl ag
                         INNER JOIN am_accountsubtype st ON st.AccountSubTypeID = ag.AccountSubTypeID
                         WHERE ag.TransStatus = 2"; 

        if($sub_type_parent_id != null){
            $select_bank = $select_bank . " AND ag.AccountChild = 0
                                           AND (st.SubTypeParentID = " . $sub_type_parent_id . " OR st.AccountSubTypeID = " . $sub_type_parent_id . ")";            
        }
		else if($sub_type_parent_id != 7){
            $select_bank = $select_bank . " AND (ag.AccountChild <> 1 OR st.AccountTypeName <> 'Bank') "; 
            $select_bank = $select_bank . "ORDER BY AccountNumber ASC";
        }
        else {
            $select_bank = $select_bank . "ORDER BY AccountNumber ASC";
        }
        
        $query = $this->db->query($select_bank);
        $this->db->_protect_identifiers = false;

        return $query->result();        */
    }

	function get_coa_wo_bank_empty_new($sub_type_parent_id) {
        $this->db->_protect_identifiers = true;
		
		$this->db->select('ag.AccountNumber, ag.AccountName, ag.AccountLevel, ag.AccountChild, st.AccountTypeName, st.AccountSubTypeID');
        $this->db->from('am_accountgl ag');
		
        $this->db->join('am_accountsubtype st', 'st.AccountSubTypeID = ag.AccountSubTypeID', 'INNER');
        $this->db->where('ag.TransStatus', 2);
		
		if($sub_type_parent_id != null){
			//$this->db->where('ag.AccountChild', 0);
			$this->db->where("(st.SubTypeParentID = $sub_type_parent_id OR st.AccountSubTypeID = $sub_type_parent_id)", NULL, FALSE);
			//$this->db->where('st.SubTypeParentID', $sub_type_parent_id);
			//$this->db->or_where('st.AccountSubTypeID', $sub_type_parent_id);
		}
		else if($sub_type_parent_id != 7){
			$this->db->where("(ag.AccountChild <> 1 OR st.AccountTypeName <> 'Bank')", NULL, FALSE);
		}
		else{
			//$this->db->order_by('ag.AccountNumber', 'ASC');
		}
		
        $query = $this->db->get();
        
		if($query->num_rows() > 0){
            return $query->result();
        }
        else{
            return false;
        }
    }
	
	function get_coa_wo_bank_empty($sub_type_parent_id) {
        $this->db->_protect_identifiers = true;
		
		$this->db->select('ag.AccountNumber, ag.AccountName, ag.AccountLevel, ag.AccountChild, st.AccountTypeName, st.AccountSubTypeID');
        $this->db->from('am_accountgl ag');
		
        $this->db->join('am_accountsubtype st', 'st.AccountSubTypeID = ag.AccountSubTypeID', 'INNER');
        $this->db->where('ag.TransStatus', 2);
		
		if($sub_type_parent_id != null){
			//$this->db->where('ag.AccountChild', 0);
			$this->db->where("(st.SubTypeParentID = $sub_type_parent_id OR st.AccountSubTypeID = $sub_type_parent_id)", NULL, FALSE);
			//$this->db->where('st.SubTypeParentID', $sub_type_parent_id);
			//$this->db->or_where('st.AccountSubTypeID', $sub_type_parent_id);
		}
		else if($sub_type_parent_id != 7){
			$this->db->where("(ag.AccountChild <> 1 OR st.AccountTypeName <> 'Bank')", NULL, FALSE);
		}
		else{
			$this->db->order_by('ag.AccountNumber', 'ASC');
		}
		
        $query = $this->db->get();
        
		if($query->num_rows() > 0){
            return $query->result();
        }
        else{
            return false;
        }
    }
	
    function get_by_name($account_name) {
        $query = $this->db->get_where($this->table, array('AccountName' => $account_name));
    
        if($query->num_rows()>0){
            $data=$query->row();
            return $data->AccountNumber;
        }
        else{
            return "";
        }
    }
	
	function get_account_name($account_name) {
        $query = $this->db->get_where($this->table, array('AccountName' => $account_name));
    
        if($query->num_rows()>0){
            return $query->row();
        }
        else{
            return false;
        }
    }
	
    function get_count_child($account_parent) {
        $query = $this->db->get_where($this->table, array('AccountParent' => $account_parent));
        return $query->num_rows();
    }

    function get_type_id($AccountTypeName) {
        $this->db->select('AccountTypeID');
        $this->db->from('am_accounttype');
        $this->db->where('AccountTypeName',$AccountTypeName);
        $query = $this->db->get();

        if($query->num_rows()>0){
            return $query->row();
        }
        else{
            return false;
        }		
    }
	
    function get_subtype_id($AccountSubTypeName) {
        $this->db->select('AccountSubTypeID');
        $this->db->from('am_accountsubtype');
        $this->db->where('AccountTypeName', $AccountSubTypeName);
        $query = $this->db->get();
    
        if($query->num_rows()>0){
            return $query->row();
        }
        else{
            return false;
        }		
    }

    function get_auto_status(){
        $this->db->select('am_accountsubtype.*, am_accountgl.AccountNumber as AccNumber');
        $this->db->from($this->accountsubtype);
		$this->db->join('am_accountgl', 'am_accountgl.AccountSubTypeID = am_accountsubtype.AccountSubTypeID', 'INNER');
        $this->db->where('am_accountsubtype.AccountAutoStatus', 1);
		 $this->db->where('am_accountgl.AccountChild', 0);
        $this->db->order_by('AccountSubTypeID', 'ASC');
        $query = $this->db->get();
        
        return $query->result(); 
    }

    function check_auto_status($account_sub_type_id){
        $this->db->select('*');
        $this->db->from($this->accountsubtype);
        $this->db->where('AccountSubTypeID', $account_sub_type_id);
        $this->db->where('AccountAutoStatus', 1);
        $query = $this->db->get();
        
        if($query->num_rows() > 0){
            return true;            
        }
        else{
            return false;
        }
    }
    
    function get_account_by_parent($account_parent, $company_code){
        $this->db->_protect_identifiers = FALSE;
		$this->db->distinct();
        $this->db->select('am_accountgl.AccountNumber, at_compaccountgl.TransStatus, (substr(trim(AccountName), 1, length(trim(am_accountgl.AccountName)) - 4)) as AccountName');
        $this->db->from($this->table);
		$this->db->join('at_compaccountgl', 'at_compaccountgl.AccountNumber = am_accountgl.AccountNumber', 'INNER');
        $this->db->where('AccountParent', $account_parent);
        $this->db->where('substr(trim(AccountName), -3) = ', 'IDR');
        $this->db->where('CurrencyCode', 'IDR');
        $this->db->where('CompanyCode', $company_code);
        $query = $this->db->get();
        $this->db->_protect_identifiers = TRUE;
        
		if($query->num_rows() > 0){
             return $query->result();        
        }
        else{
            return false;
        }
       
    } 
	
	function get_account_by_auto_status($AccountName, $company_code, $branch_code){
        $this->db->_protect_identifiers = FALSE;
        $this->db->select('sum(at_compaccountgl.GLSaldoOpening) as GLOpening, at_compaccountgl.TransStatus, sum(at_compaccountgl.GLSaldoBalance'.CURRENT_PERIOD.') as GLSaldoBalance, sum(at_compaccountgl.GLSaldoBSheet) as GLSBsheet');
        $this->db->from($this->table);
		$this->db->join('at_compaccountgl', 'at_compaccountgl.AccountNumber = am_accountgl.AccountNumber', 'INNER');
        $this->db->like('am_accountgl.AccountName', $AccountName);
		$this->db->where('at_compaccountgl.CompanyCode', $company_code);
        $this->db->where('at_compaccountgl.TransStatus', 2);
        $query = $this->db->get();
        $this->db->_protect_identifiers = TRUE;
        
		if($query->num_rows() > 0){
             return $query->row();        
        }
        else{
            return false;
        }
       
    } 
 
    function get_account_by_subtypeid($sub_type_id, $child_status){
        $this->db->_protect_identifiers = FALSE;
        $this->db->select('AccountNumber, AccountName, AccountLevel');
        $this->db->from($this->table);
        $this->db->where('AccountSubTypeID', $sub_type_id);
        $this->db->where('AccountChild', $child_status);
        $this->db->where('CurrencyCode', 'IDR');
        $this->db->where('TransStatus', 2);
        $query = $this->db->get();
        $this->db->_protect_identifiers = TRUE;
        
        if($query->num_rows() > 0){
            return $query->result();
        }
        else{
            return FALSE;
        }
    }
        
    function get_level($account_parent){
//        $data = $this->get_by_id($account_parent);
//        if($data[0]->AccountLevel > 0){

        if($data = $this->get_by_id($account_parent)){
            return $data->AccountLevel + 1;
        }
        else{
            return 0;
        }
    }
	
	function get_level_parrent($account_sub_type_id){
 		$this->db->_protect_identifiers = FALSE;
        $this->db->select('AccountNumber');
        $this->db->from($this->table);
        $this->db->where('AccountSubTypeID', $account_sub_type_id);
        $this->db->where('AccountChild', 0);
        $this->db->where('TransStatus', 2);
        $query = $this->db->get();
        $this->db->_protect_identifiers = TRUE;
        
        if($query->num_rows() > 0){
            return $query->row();
        }
        else{
            return FALSE;
        }
    }
    
    function insert($data){		
        $this->db->insert($this->table, $data);
        $this->update_parent($data['AccountParent']);
    }
	
	//cek bank book by fahri
	function insert_bank_book($data, $id){
		$cek = $this->get_by_id($id);
		
        if($cek->AppLevel1Status == 1 && $cek->AppLevel2Status == 1){
            $this->db->insert($this->bankbook, $data);
        }
    }	
	//end	
    
    function update_parent($accountParent, $AccountSubTypeName=''){
        if($this->get_count_child($accountParent) > 0){
            $int_account_child = 0;
        }
        else{
            $int_account_child = 1;
            //ini katanya di rubah langsung dari database untuk yang bank
            /*if($AccountSubTypeName == "Bank"){
                    $int_account_child = 0;
            }
            else{
                    $int_account_child = 1;
            }*/
        }

        $data_parent = array('AccountChild' => $int_account_child, 'LastUser' => $this->session->userdata("username"));
        $this->db->update($this->table, $data_parent, array('AccountNumber' => $accountParent));
    }
    
    function update($data, $account_number) {
		$this->db->update($this->table, $data, array('AccountNumber' => $account_number));
    }
	
	function update_company_gl($data, $account_number, $branch_code) {
        $this->db->update($this->companygl, $data, array('AccountNumber' => $account_number, 'BranchCode' => $branch_code));
    }
	
	/*
		sudah tidak dipakai buang (fahri)
	*/
	function update_bank_book($data, $account_number) {
		$cek = $this->get_by_id($account_number);
		
		if($cek->AppLevel1Status == 1 && $cek->AppLevel2Status == 1){
        	$this->db->update($this->bankbook, $data, array('BankAccount' => $account_number));
		}
    }
        
    public function delete($account_number) {
        $cek_appoval = $this->get_by_id($account_number);		
        
        if($cek_appoval->AppLevel1Status=='1' && $cek_appoval->AppLevel2Status=='1'){
            $result['status'] = false;
            $result['message']= "This Account can't be delete, because this Account has been Approved";
        }
        else{
            $this->db->delete($this->table, array('AccountNumber' => $account_number));
            $result['status'] = true;
            $result['message']= "AccountNumber has been deleted by ".$this->session->userdata("username");
        }		
        return $result;
    }
     
	function get_account_gl_maseter_by_name($account_name) {
		$this->db->_protect_identifiers = FALSE;
        $this->db->select($this->table.'.AccountNumber');
        $this->db->from($this->table);
		$this->db->like($this->table.'.AccountName', $account_name);
		
		$this->db->_protect_identifiers = TRUE;
		
        $query = $this->db->get();
        return $query->result();
    }
	
	function get_am_account_gl_by_name($account_name, $company_code) {
		$this->db->_protect_identifiers = FALSE;
        $this->db->select($this->table.'.AccountNumber, at_compaccountgl.BranchCode');
        $this->db->from($this->table);
		$this->db->join('at_compaccountgl', 'at_compaccountgl.AccountNumber = am_accountgl.AccountNumber', 'INNER');
		$this->db->join('am_company', 'am_company.CompanyCode = at_compaccountgl.CompanyCode', 'INNER');
		
		$this->db->like($this->table.'.AccountName', $account_name);
		$this->db->where('at_compaccountgl.CompanyCode', $company_code);
		$this->db->where('at_compaccountgl.GLSaldoYears', date('Y'));
		
		$this->db->_protect_identifiers = TRUE;
		
        $query = $this->db->get();
        return $query->result();
    }
	   
    function check_name_exist($account_name) {
        $this->db->select('*');
        $this->db->from($this->table);
        $this->db->where('AccountName', $account_name);
        $query = $this->db->get();
        
        if ($query->num_rows() > 0) {
            return TRUE;
        }
        else {
            return FALSE;
        }	
    }
    
    function check_code_exist($account_number) {
        $query = $this->db->get_where($this->table, array('AccountNumber' => $account_number), 1, 0);

        if ($query->num_rows() > 0) {
            return TRUE;
        }
        else {
            return FALSE;
        }	
    }
	
	function check_sub_type($sub_type_id, $arr_sub_type_name) {
        $this->db->select('am_accountsubtype.AccountTypeName, am_accountsubtype.AccountSubTypeID');
        $this->db->from($this->accountsubtype);
		$this->db->where_in('am_accountsubtype.AccountSubTypeID', $sub_type_id);
		
        $query = $this->db->get();
        
        if ($query->num_rows() > 0) {
			$data_number = $query->row();
			if($data_number->AccountSubTypeID == $sub_type_id){
				return TRUE;	
			}
			else{
				return FALSE;	
			}
        }
        else {
            return FALSE;
        }	
    }
	
	function check_bank_book($bank_book, $branch_code) {
        $this->db->select('BankAccount, SaldoDebet, SaldoCredit, SaldoBalance');
        $this->db->from($this->bankbook);
        $this->db->where('BankAccount', $bank_book);
		$this->db->where('BranchCode', $branch_code);
		$this->db->order_by('TransDate', 'ASC');
        $query = $this->db->get();
        
        if ($query->num_rows() > 0) {
            return $query->row();
        }
        else {
            return FALSE;
        }	
    }
	
	function check_sub_type_master($sub_type_id) {
        $this->db->select('am_accountsubtype.AccountTypeName, am_accountsubtype.AccountSubTypeID');
        $this->db->from($this->accountsubtype);
		$this->db->where_in('am_accountsubtype.AccountSubTypeID', $sub_type_id);
		
        $query = $this->db->get();
        
        if ($query->num_rows() > 0) {
			return $query->row();
        }
        else {
            return FALSE;
        }	
    }
	
    function create_account_number($account_parent){
        $len                = array(1,1,2,3,2);
        $SpaceChar          = '-';
        $account_level      = $this->get_level($account_parent);		
        
        $num                = $this->db->get_where($this->table, array('AccountParent'=>$account_parent))->num_rows() + 1;			
        $splite             = explode($SpaceChar, $account_parent);
        $current_max_len    = $len[$account_level];

        foreach($len as $key => $val){
            if($key > $account_level){
                $nol = '';
                
                for($i=0; $i<$val; $i++){
                    $nol = '0' . $nol;					
                }
                
                $splite[$key] = $nol;
            }					
        }

        $current_len = strlen($num);
	
        while($current_len < $current_max_len){
            $num            = '0'.$num;
            $current_len    = strlen($num);
        } 
	
        $splite[$account_level] = $num;
        $account_number         = implode('-', $splite);
	
        while($this->check_code_exist($account_number)){
            $num++;
            $current_len = strlen($num);
            
            while($current_len<$current_max_len){
                $num            = '0'.$num;
                $current_len    = strlen($num);
            } 
            
            $splite[$account_level]  = $num;
            $account_number          = implode('-',$splite);
        }

        return $account_number;	
    }
    
    function set_finish($account_number, $txt_branch_code) {
        $cek = $this->get_by_id($account_number);
        $trans_status = 0;
        
        $trans_status 	= 2;
		$PLStatus 		= 1;

        $data = array (							
                'TransStatus'   => $trans_status,					
                'LastUser'      => $this->session->userdata("username")
                );
		$this->update($data, $account_number);
				
		$data_company = array (	
				'PLStatus' 		=> $PLStatus,						
                'TransStatus'   => $trans_status,					
                'LastEditedOP'  => $this->session->userdata("username")
                );
		$this->update_company_gl($data_company, $account_number, $txt_branch_code);
    }  
	
	function check_data_coa_by_company($account_number, $company_code, $branch_code, $status_branch_company) {
		$this->db->_protect_identifiers = FALSE;
        $this->db->select('at_compaccountgl.GLSaldoOpening, at_compaccountgl.TransStatus, at_compaccountgl.GLSaldoBalance'.CURRENT_PERIOD.' as GLSaldoBalance, at_compaccountgl.GLSaldoBSheet');
        $this->db->from($this->companygl);
		$this->db->join('am_branch', 'am_branch.BranchCode = at_compaccountgl.BranchCode', 'INNER');
		$this->db->join('am_company', 'am_branch.CompanyCode = am_company.CompanyCode', 'INNER');
		
		$this->db->where('at_compaccountgl.AccountNumber', $account_number);
		$this->db->where('at_compaccountgl.CompanyCode', $company_code);
		$this->db->where('at_compaccountgl.GLSaldoYears', date('Y'));
		
		if($status_branch_company == 0){
			$this->db->where('at_compaccountgl.BranchCode', $branch_code);
			$this->db->where('am_company.CompanyCode', $company_code);
			$this->db->where('am_branch.BranchStatus', 0);
		}
		
		$this->db->_protect_identifiers = TRUE;
		
        $query = $this->db->get();
        if($query->num_rows() > 0){
            return $query->row();
        }
        else{
            return false;
        }
    }
	
	function check_data_by_company_from_coa($account_number, $company_code, $branch_code) {
		$this->db->_protect_identifiers = FALSE;
        $this->db->select('sum(at_compaccountgl.GLSaldoOpening) as GLOpening, at_compaccountgl.TransStatus, sum(at_compaccountgl.GLSaldoBalance'.CURRENT_PERIOD.') as GLSaldoBalance, sum(at_compaccountgl.GLSaldoBSheet) as GLBSheet');
        $this->db->from($this->companygl);
		$this->db->join('am_branch', 'am_branch.BranchCode = at_compaccountgl.BranchCode', 'INNER');
		$this->db->join('am_company', 'am_branch.CompanyCode = am_company.CompanyCode', 'INNER');
		
		$this->db->where('at_compaccountgl.AccountNumber', $account_number);
		$this->db->where('at_compaccountgl.CompanyCode', $company_code);
		$this->db->where('at_compaccountgl.GLSaldoYears', date('Y'));
		
		$this->db->_protect_identifiers = TRUE;
		
        $query = $this->db->get();
        if($query->num_rows() > 0){
            return $query->row();
        }
        else{
            return false;
        }
    }
	
	function cek_all_transaction($account_number, $company_code, $branch_code) {
        $this->db->select('av_all_transaction.TransStatus, av_all_transaction.Journal_Number');
        $this->db->from($this->av_all_transaction);
		
		$this->db->join('am_branch', 'am_branch.BranchCode = av_all_transaction.Branch_Code', 'INNER');
		$this->db->join('am_company', 'am_company.CompanyCode = am_branch.CompanyCode', 'INNER');
		
		$this->db->where('av_all_transaction.Account_Number', $account_number);
		$this->db->where('am_company.CompanyCode', $company_code);
		$this->db->where('am_branch.BranchCode', $branch_code);
		
        $query = $this->db->get();
        if($query->num_rows() > 0){
            return $query->result();
        }
        else{
            return false;
        }
    } 
	
	/*=======================================function untuk cek sys item pas suspen coa=========================================*/
	function get_sys_item_account($id, $company_code){
		$this->db->select('sys_itemaccount.AccountNumber, sys_itemcategory.SysItemCategoryID');
        $this->db->from('sys_itemaccount');
		
		$this->db->join('sys_itemcategory', 'sys_itemcategory.SysItemCategoryID = sys_itemaccount.SysItemCategoryID', 'INNER');
		$this->db->join('am_company', 'am_company.CompanyCode = sys_itemcategory.CompanyCode', 'INNER');
		
		$this->db->where('sys_itemcategory.CompanyCode', $company_code);
		$this->db->where('sys_itemaccount.AccountNumber', $id);
        $query = $this->db->get();
        if($query->num_rows() > 0){
             return $query->result();
        }
        else{
            return false;
        }
	}
	
	function delete_sys_item_account($AccountNumber, $SysItemCategoryID) {
        $this->db->delete('sys_itemaccount', array('AccountNumber' => $AccountNumber, 'SysItemCategoryID'=> $SysItemCategoryID));
    }
	/*=========================================end========================================================================*/
	
	/*=======================================function untuk cek sys sales pas suspen coa=========================================*/
	function get_sys_sales_account($id, $company_code){
		$this->db->select('sys_salesaccount.AccountNumber, sys_salescategory.SysSalesCategoryID');
        $this->db->from('sys_salesaccount');
		
		$this->db->join('sys_salescategory', 'sys_salescategory.SysSalesCategoryID = sys_salesaccount.SysSalesCategoryID', 'INNER');
		$this->db->join('am_company', 'am_company.CompanyCode = sys_salescategory.CompanyCode', 'INNER');
		
		$this->db->where('sys_salescategory.CompanyCode', $company_code);
		$this->db->where('sys_salesaccount.AccountNumber', $id);
        $query = $this->db->get();
        if($query->num_rows() > 0){
             return $query->result();
        }
        else{
            return false;
        }
	}
	
	function delete_sys_sales_account($AccountNumber, $SysSalesCategoryID) {
        $this->db->delete('sys_salesaccount', array('AccountNumber' => $AccountNumber, 'SysSalesCategoryID'=> $SysSalesCategoryID));
    }
	/*=========================================end========================================================================*/
	
	/*=======================================function untuk cek sys sales pas suspen coa=========================================*/
	function get_sys_purchase_account($id, $company_code){
		$this->db->select('sys_purchaseaccount.AccountNumber, sys_purchasecategory.SysPurchaseCategoryID');
        $this->db->from('sys_purchaseaccount');
		
		$this->db->join('sys_purchasecategory', 'sys_purchasecategory.SysPurchaseCategoryID = sys_purchaseaccount.SysPurchaseCategoryID', 'INNER');
		$this->db->join('am_company', 'am_company.CompanyCode = sys_purchasecategory.CompanyCode', 'INNER');
		
		$this->db->where('sys_purchasecategory.CompanyCode', $company_code);
		$this->db->where('sys_purchaseaccount.AccountNumber', $id);
        $query = $this->db->get();
        if($query->num_rows() > 0){
             return $query->result();
        }
        else{
            return false;
        }
	}
	
	function delete_sys_purchase_account($AccountNumber, $SysPurchaseCategoryID) {
        $this->db->delete('delete_sys_purchase_account', array('AccountNumber' => $AccountNumber, 'SysPurchaseCategoryID'=> $SysPurchaseCategoryID));
    }
	/*=========================================end========================================================================*/
	
	function check_data_from_master_gl() {
        $this->db->select('AccountNumber');
        $this->db->from($this->table);
        $query = $this->db->get();
        if($query->num_rows() > 0){
             return true;
        }
        else{
            return false;
        }						
    }
	
	function get_master_coa_sub_bank($acc_name) {
        $this->db->select('AccountName, BankID, TransStatus');
        $this->db->from($this->table);
		
		$this->db->where('AccountName', $acc_name);
		
        $query = $this->db->get();
        if($query->num_rows() > 0){
             return $query->row();
        }
        else{
            return false;
        }						
    }
	
	function get_master_coa_sub_bank_id($acc_name) {
        $this->db->select('AccountName,AccountNumber, BankID, TransStatus');
        $this->db->from($this->table);
		
		$this->db->where('AccountName', $acc_name);
		
        $query = $this->db->get();
        if($query->num_rows() > 0){
             return $query->row();
        }
        else{
            return false;
        }						
    }
    
    
    //============= DHIDHIK, 21 Maret 2016 ===================================//    
    function datasend_coa_import($dataSend){         
        $this->db->trans_start();   
        foreach($dataSend['data_insert'] as $key){
            $AccountNumber          = $this->create_account_number($key['AccountParent']);
            $key['AccountNumber']   = $AccountNumber;
            $this->insert($key);//yang update AccountParent sudah masuk di function insert ternyata, jd ga perlu ditulisken kembali
        }
        $this->db->trans_complete();
        if($this->db->trans_status() == FALSE){
            $result  = false;
        }else{
            $result = true;
        }
        return $result;
    }    
    function datasend_coa_newinsert($dataSend){         
        $this->db->trans_start();  
        $this->insert($dataSend['data_accountgl']);
        foreach($dataSend['data_companygl'] as $key){
            $this->m_compaccountgl->insert($key);
        }
        $this->db->trans_complete();
        if($this->db->trans_status() == FALSE){
            $result  = false;
        }else{
            $result = true;
        }
        return $result;
    }    
    function datasend_coa_app_dir2($dataSend){         
        $this->db->trans_start();  
        
        $this->update($dataSend['data_app'],$dataSend['idcode']);
        $this->set_finish($dataSend['idcode'], $dataSend['branchcode']);
        
        foreach($dataSend['data_accountgl'] as $key){
            $account_number     = $this->create_account_number($key['AccountParent']);
            $key['AccountNumber'] = $account_number;
            $this->insert($key);
            
            if(!in_array($dataSend['SubTypeName'], array("Bank","Deposito"))){	
                $get_branch_by_company = $this->m_branchinfo->get_branch_by_company_all($dataSend['companycode']);
                foreach($get_branch_by_company as $row_branch_auto){
                    //======= insert masuk ke company acc gl by branch =======//
                    $glcompany_data = array(
                        'CompanyCode'       => $dataSend['companycode'],
                        'BranchCode'        => $row_branch_auto->BranchCode,
                        'AccountNumber'     => $account_number,
                        'TransStatus'       => 2,
                        'PLStatus'          => 1,// change PL status 1 ()
                        'GLSaldoYears'      => date('Y')
                    );
                    $this->m_compaccountgl->insert($glcompany_data);
                    //======= end insert masuk ke company acc gl by branch ===//
                }
            }
        }
        
        foreach($dataSend['data_companygl'] as $key){
            $this->m_compaccountgl->insert($key);
        }
        $this->db->trans_complete();
        if($this->db->trans_status() == FALSE){
            $result  = false;
        }else{
            $result = true;
        }
        return $result;
    }
    function datasend_coa_update($dataSend){         
        $this->db->trans_start();  
        if(($dataSend['account_subtype'] != 7)) {//masuk ke model
            $this->update($dataSend['data_coa_update'], $dataSend['account_number']);
        }
        $this->m_journalvoucher->insert($dataSend['data_group']);
        $this->m_journalvoucher->insert_detail($dataSend['data_detail_debet']);
        $this->m_journalvoucher->insert_detail($dataSend['data_detail_credit']);
        $this->m_journalvoucher->insert_journal($dataSend['journal_debet']);
        $this->m_journalvoucher->insert_journal($dataSend['journal_credit']);
        $this->m_journalvoucher->update($dataSend['data_group_update'], $dataSend['jv_number']);
        
        $this->db->trans_complete();
        if($this->db->trans_status() == FALSE){
            $result  = false;
        }else{
            $result = true;
        }
        return $result;
    }    
    function datasend_coa_suspend($dataSend){         
        $this->db->trans_start();   
        
        if($dataSend['if']==1){
            $dataSend = $dataSend['opening_balance'];            
            foreach($dataSend['data_company_gl'] as $key){//update comp akun gl
                $data_company_gl = array(                    
                    'TransStatus'   => $key['TransStatus'],
                    'PLStatus'      => $key['PLStatus']
                );
                $this->update_company_gl($data_company_gl, $key['AccountNumber_id'], $key['BranchCode']);
            }     
            
            foreach($dataSend['delete_sys_item'] as $key){//delete_sys_item_account
                $this->delete_sys_item_account($key['AccountNumber'], $key['SysItemCategoryID']);
            }
            
            foreach($dataSend['delete_sys_sales'] as $key){//delete_sys_sales_account
                $this->delete_sys_sales_account($key['AccountNumber'], $key['SysSalesCategoryID']);
            }
            
            foreach($dataSend['delete_sys_purchase'] as $key){//delete_sys_purchase_account
                $this->delete_sys_purchase_account($key['AccountNumber'], $key['SysItemCategoryID']);
            }
        }else{
            $dataSend = $dataSend['bank'];
            $this->update($dataSend['update_coa'],$dataSend['idcode']);
            $this->update_company_gl($dataSend['company_gl'],$dataSend['idcode'], $dataSend['branchcode']);
        }
        
        $this->db->trans_complete();
        if($this->db->trans_status() == FALSE){
            $result  = false;
        }else{
            $result = true;
        }
        return $result;
    }   
    function datasend_coa_companygl_insert($dataSend){         
        $this->db->trans_start();   
        
        foreach($dataSend['data_compaccountgl'] as $key){
            $this->m_compaccountgl->insert($key);
        }
        foreach($dataSend['update_compaccountgl'] as $key){
            $this->m_compaccountgl->update_company_coa($key['AccountNumber'], $key['BranchCode']);
        }
        
        $this->db->trans_complete();
        if($this->db->trans_status() == FALSE){
            $result  = false;
        }else{
            $result = true;
        }
        return $result;
    }     
    function datasend_coa_branchbank_insert($dataSend){         
        $this->db->trans_start();   
        
        if($dataSend['if']==1){
            $this->update($dataSend['branchbank_update'], $dataSend['AccountNumber']);
            $this->m_compaccountgl->update_branch($dataSend['compaccountgl_update'], $dataSend['AccountNumber'], $dataSend['BranchCode']);
        }else{
            $this->insert($dataSend['branchbank_insert']);
            $this->m_compaccountgl->insert($dataSend['compaccountgl_insert']);
        }
        
        $this->db->trans_complete();
        if($this->db->trans_status() == FALSE){
            $result  = false;
        }else{
            $result = true;
        }
        return $result;
    }    
    //============= End DHIDHIK, 21 Maret 2016 ===============================//
}