<?php
class M_expensecashbon_payment extends CI_Model {
    
	public $table = "at_expensepayment";
	public $detail = "at_epaydetail";
	public $journal = "at_eprealjournal";
	public $crv_table = "at_epaymentreal";
	/* public $department = "am_department";
	public $employee = "am_employee"; */
	public $account = "at_compaccountgl";
	
	function count_payment_cashbon_number($bulan, $cek_tahun, $BranchCode){
	    $this->db->select('*');
		$this->db->from('at_expensepayment');
		$this->db->where('MONTH(EPayDate)', $bulan);
		$this->db->where('YEAR(EPayDate)', $cek_tahun);
		$this->db->where('BranchCode', $BranchCode);
		return $this->db->get()->num_rows();
	}
	
	function create_po_number($date){
        $branch_code    = $this->session->userdata('branch');
        $month          = date('m', strtotime($date));
        $year           = date('y', strtotime($date));
        
        $exp_number    = $branch_code."/".$month.$year."EP/";
	
        $this->db->like('EPayNumber', $exp_number);
        $num    = $this->db->get($this->table)->num_rows()+1;
        $long   = strlen($num);
	
        if($long == 1){
            $number = '00'.$num;
        }
        elseif($long == 2){
            $number = '0'.$num;
        }
        else{
            $number = $num;
        }
		
        return  $pord_number.$number;
    }
	
	function get_last_payment_cashbon_number($BranchCode){
		$qty="SELECT MAX(EPayNumber) as EPayNumber FROM at_expensepayment WHERE BranchCode='$BranchCode'";
		return $this->db->query($qty)->result();
	}
	
	
	function get_list_cashbon($EmpCode){
		$this->db->select('*, at_ebondetail.EBonAccountNumber');
		$this->db->from('at_expensecashbon');
		$this->db->join('at_ebondetail','at_expensecashbon.EBonNumber=at_ebondetail.EBonNumber','INNER');
		$this->db->where('at_expensecashbon.EmployeeCode', $EmpCode);
		$this->db->where('at_expensecashbon.TransStatus', 1);
		//$this->db->where('at_expensecashbon.TransStatus', 0);
		/* $this->db->where('at_expensecashbon.AppLevel1Status', 1);
		$this->db->where('at_expensecashbon.AppLevel2Status', 1); */
		$this->db->order_by('at_expensecashbon.EBonNumber', 'ASC');
		
		$query = $this->db->get();
		return $query->result();
	}
	
	function get_list_expense_group($replace_akun){
	    $company_code = $this->session->userdata('company');
		$this->db->select('*');
		$this->db->from('sys_expensecategory');
		$this->db->join('sys_expenseaccount','sys_expensecategory.ExpCategoryID=sys_expenseaccount.ExpCategoryID','INNER');
		$this->db->where('sys_expensecategory.CompanyCode', $company_code);
		$this->db->where('sys_expenseaccount.AccountNumber', $replace_akun);
		
		$this->db->order_by('sys_expensecategory.ExpCategoryID', 'ASC');
		$query = $this->db->get();
		
		$result=$query->result();
		return json_encode($result);
	}
	
	function get_list_item_expense_group($Groupid){
		$this->db->select('*');
		$this->db->from('sys_expensedetail');
		$this->db->where('ExpCategoryID', $Groupid);
		$this->db->order_by('ExpDetailID', 'ASC');
		$query = $this->db->get();
		
		$result=$query->result();
		return json_encode($result);
	}
	
	function get_am_akun_gl($replace_akun, $brannch){
		$this->db->select('am_accountgl.*');
		$this->db->from('am_accountgl');
		$this->db->join('at_compaccountgl','am_accountgl.AccountNumber=at_compaccountgl.AccountNumber');
		$this->db->where('am_accountgl.AccountNumber', $replace_akun);
		$this->db->where('at_compaccountgl.BranchCode', $brannch);	
		$this->db->where('am_accountgl.TransStatus', 2);
		$this->db->where('at_compaccountgl.GLSaldoYears', date('Y'));
		$query = $this->db->get();
		
		$result=$query->row();
		return $result;
	}
	
	function insert_payment_cashbon($data_payment){
		$this->db->insert($this->table, $data_payment);
		
		if ($this->db->trans_status() === FALSE) {
			return 0;
		}
		else {
			return 1;
		}
	}
	
	function insert_detail_payment_cashbon($detail_payment){
		$this->db->insert($this->detail, $detail_payment);
		
		if ($this->db->trans_status() === FALSE) {
			return 0;
		}
		else {
			return 1;
		}
	}
	
	function get_bank_id($AccountNumber){
		$this->db->select('BankID');
		$this->db->from('am_accountgl');
		$this->db->join('at_compaccountgl','am_accountgl.AccountNumber=am_accountgl.AccountNumber','INNER');
		$this->db->where('am_accountgl.AccountNumber', $AccountNumber);
		$query = $this->db->get();

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $BankID = $row->BankID;
            }

            return $BankID;
        }
        else {
            return FALSE;
        }
	}
	
	function get_bank_code_masuk($bank_id){
		$this->db->select('BankInCode');
		$this->db->from('am_bank');
		$this->db->where('BankID', $bank_id);
		$query = $this->db->get();

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $BankInCode = $row->BankInCode;
            }

            return $BankInCode;
        }
        else {
            return FALSE;
        }
	}
	
	function get_akun($CompanyCode, $replace_akun, $currency_code, $cpv_crv){
	    $jobtitle = $this->session->userdata("jobtitle");
		$BranchCode 	= $this->session->userdata('branch');
		$BranchStatus 	= $this->session->userdata('branch_status');
		
		$this->db->select('at_compaccountgl.BranchCode, am_accountgl.AccountNumber,  am_accountgl.AccountName , am_accountgl.CurrencyCode');
		$this->db->from($this->account);
		$this->db->join('am_accountgl','at_compaccountgl.AccountNumber=am_accountgl.AccountNumber');		
		$this->db->join('am_branch','at_compaccountgl.BranchCode=am_branch.BranchCode');		
		
		$this->db->where('at_compaccountgl.CompanyCode', $CompanyCode);
		$this->db->where('at_compaccountgl.BranchCode', $BranchCode);	
		
		if($cpv_crv == "CPV"){
			$this->db->where_in('am_accountgl.AccountSubTypeID', $replace_akun);
		}
		else{
			$this->db->where('am_accountgl.AccountNumber', $replace_akun);
		}
		//		
		
		//$a = array(5,6);
		
		$this->db->where('am_accountgl.AccountChild', 1);
		$this->db->where('am_accountgl.CurrencyCode', $currency_code);
		$this->db->where('am_accountgl.TransStatus', 2);
		$this->db->where('at_compaccountgl.GLSaldoYears', date('Y'));
		
		$query = $this->db->get();
		
		$result=$query->result();
		return $result;
	}
	
	function get_bank_code_keluar($bank_id){
		$this->db->select('BankOutCode');
		$this->db->from('am_bank');
		$this->db->where('BankID', $bank_id);
		$query = $this->db->get();

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $BankOutCode = $row->BankOutCode;
            }

            return $BankOutCode;
        }
        else {
            return FALSE;
        }
	}
	
	function count_crv_number($bulan, $cek_tahun){
	    $this->db->select('*');
		$this->db->from('at_epaymentreal');
		$this->db->where('MONTH(EPayDate)', $bulan);
		$this->db->where('YEAR(EPayDate)', $cek_tahun);
		$this->db->where('EPRealTransType', 1);
		return $this->db->get()->num_rows();
	}
	
	function get_last_crv_number(){
		$qty="SELECT MAX(EPRealNumber) as EPRealNumber FROM at_epaymentreal WHERE EPRealTransType ='1'";
		return $this->db->query($qty)->result();
	}
	
	function count_cpv_number($bulan, $cek_tahun){
	    $this->db->select('*');
		$this->db->from('at_epaymentreal');
		$this->db->where('MONTH(EPayDate)', $bulan);
		$this->db->where('YEAR(EPayDate)', $cek_tahun);
		$this->db->where('EPRealTransType', 2);
		return $this->db->get()->num_rows();
	}
	
	function get_last_cpv_number(){
		$qty="SELECT MAX(EPRealNumber) as EPRealNumber FROM at_epaymentreal WHERE EPRealTransType ='2'";
		return $this->db->query($qty)->result();
	}
	
	function insert_detail_crv($detail_ppaycrv){
		$this->db->trans_begin();
		$this->db->insert('at_epaymentreal', $detail_ppaycrv);
		
		if ($this->db->trans_status() === FALSE) {
			$this->db->trans_rollback();
			return 0;
		}
		else {
			$this->db->trans_commit();
			return 1;
		}
	}
	
	function insert_detail_cpv($detail_ppaycpv){
		$this->db->trans_begin();
		$this->db->insert('at_epaymentreal', $detail_ppaycpv);
		
		if ($this->db->trans_status() === FALSE) {
			$this->db->trans_rollback();
			return 0;
		}
		else {
			$this->db->trans_commit();
			return 1;
		}
	}
	
	function get_account_exp_payable($expCategoryID/*, $curren_code*/){
		$this->db->select('sys_expa.AccountNumber');
		$this->db->from('sys_expenseaccount as sys_expa');
		
		$this->db->join('at_compaccountgl as cg', 'cg.AccountNumber = sys_expa.AccountNumber', 'INNER');
		$this->db->join('am_accountgl as ag', 'ag.AccountNumber = cg.AccountNumber', 'INNER');
		$this->db->join('am_branch as br', 'br.BranchCode = cg.BranchCode', 'INNER');
		$this->db->where('cg.CompanyCode', $this->session->userdata('company'));
		$this->db->where('cg.BranchCode', $this->session->userdata('branch'));            
		$this->db->where('(case when br.BranchStatus = 0 then cg.PLStatus in (0, 1) else cg.PLStatus = 1 end)');            
		$this->db->where('cg.GLSaldoYears', date('Y'));                
		
		/*if($curren_code == 'IDR'){
			$this->db->where('ag.CurrencyCode', 'IDR');
		}
		else{
			$this->db->where('ag.CurrencyCode', $curren_code);
		}*/
		
		$this->db->where('sys_expa.ExpCategoryID', $expCategoryID);
		$this->db->where('sys_expa.AccountCode', 'Payable');
			
		$query = $this->db->get();

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $AccountNumber = $row->AccountNumber;
            }

            return $AccountNumber;
        }
        else {
            return FALSE;
        }
	}
	
	function get_account_exp_relize($expCategoryID/*, $curren_code*/){
		$this->db->select('sys_expa.AccountNumber');
		$this->db->from('sys_expenseaccount as sys_expa');
		
		$this->db->join('at_compaccountgl as cg', 'cg.AccountNumber = sys_expa.AccountNumber', 'INNER');
		$this->db->join('am_accountgl as ag', 'ag.AccountNumber = cg.AccountNumber', 'INNER');
		$this->db->join('am_branch as br', 'br.BranchCode = cg.BranchCode', 'INNER');
		$this->db->where('cg.CompanyCode', $this->session->userdata('company'));
		$this->db->where('cg.BranchCode', $this->session->userdata('branch'));            
		$this->db->where('(case when br.BranchStatus = 0 then cg.PLStatus in (0, 1) else cg.PLStatus = 1 end)');            
		$this->db->where('cg.GLSaldoYears', date('Y'));                
		
		/*if($curren_code == 'IDR'){
			$this->db->where('ag.CurrencyCode', 'IDR');
		}
		else{
			$this->db->where('ag.CurrencyCode', $curren_code);
		}*/
		
		$this->db->where('sys_expa.ExpCategoryID', $expCategoryID);
		$this->db->where('sys_expa.AccountCode', 'Realized');
			
		$query = $this->db->get();

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $AccountNumber = $row->AccountNumber;
            }

            return $AccountNumber;
        }
        else {
            return FALSE;
        }
	}
	
	function get_account_advance_cashbon($expCategoryID/*, $curren_code*/){
		$this->db->select('sys_expa.AccountNumber');
		$this->db->from('sys_expenseaccount as sys_expa');
		
		$this->db->join('at_compaccountgl as cg', 'cg.AccountNumber = sys_expa.AccountNumber', 'INNER');
		$this->db->join('am_accountgl as ag', 'ag.AccountNumber = cg.AccountNumber', 'INNER');
		$this->db->join('am_branch as br', 'br.BranchCode = cg.BranchCode', 'INNER');
		$this->db->where('cg.CompanyCode', $this->session->userdata('company'));
		$this->db->where('cg.BranchCode', $this->session->userdata('branch'));            
		$this->db->where('(case when br.BranchStatus = 0 then cg.PLStatus in (0, 1) else cg.PLStatus = 1 end)');            
		$this->db->where('cg.GLSaldoYears', date('Y'));                
		
		/*if($curren_code == 'IDR'){
			$this->db->where('ag.CurrencyCode', 'IDR');
		}
		else{
			$this->db->where('ag.CurrencyCode', $curren_code);
		}*/
		
		$this->db->where('sys_expa.ExpCategoryID', $expCategoryID);
		$this->db->where('sys_expa.AccountCode', 'Expense');
			
		$query = $this->db->get();

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $AccountNumber = $row->AccountNumber;
            }

            return $AccountNumber;
        }
        else {
            return FALSE;
        }
	}
	
	/*function get_account_advance_cashbon($expCategoryID){
		$company = $this->session->userdata('company');
		$this->db->select('sys_expenseaccount.AccountNumber');
		//$this->db->from('at_compaccountgl');
		$this->db->from('sys_expenseaccount');
		$this->db->join('sys_expensecategory, sys_expensecategory.ExpCategoryID = sys_expenseaccount.ExpCategoryID');
		$this->db->where('sys_expenseaccount.ExpCategoryID', $expCategoryID);
		$this->db->where('sys_expenseaccount.AccountCode', 'Expense'); 
		$this->db->where('sys_expenseaccount.CompanyCode',$company);
		$query = $this->db->get();

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $AccountNumber = $row->AccountNumber;
            }

            return $AccountNumber;
        }
        else {
            return FALSE;
        }
	}*/
	
	function get_account_bank_payment($EBonNumber){
		$this->db->select('BankPayment');
		$this->db->from('at_expensecashbon');
		$this->db->where('EBonNumber', $EBonNumber);
		$query = $this->db->get();

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $BankPayment = $row->BankPayment;
            }

            return $BankPayment;
        }
        else {
            return FALSE;
        }
	}
	
	function insert_journal_expense_payment_debit($data_journal_exp_debit){
		$this->db->trans_begin();
		$this->db->insert('at_eprealjournal', $data_journal_exp_debit);
		
		if ($this->db->trans_status() === FALSE) {
			$this->db->trans_rollback();
			return 0;
		}
		else {
			$this->db->trans_commit();
			return 1;
		}
	}
	
	function insert_journal_expense_payment_credit($data_journal_exp_credit){
		$this->db->insert('at_eprealjournal', $data_journal_exp_credit);
		
		if ($this->db->trans_status() === FALSE) {
			return 0;
		}
		else {
			return 1;
		}
	}
	
	function insert_journal_expense_crv_debit($data_journal_exp_crv_debit){
		$this->db->insert('at_eprealjournal', $data_journal_exp_crv_debit);
		
		if ($this->db->trans_status() === FALSE) {
			return 0;
		}
		else {
			return 1;
		}
	}
	
	function insert_journal_bank_crv_debit($data_journal_bank_crv_debit){
		$this->db->insert('at_eprealjournal', $data_journal_bank_crv_debit);
		
		if ($this->db->trans_status() === FALSE) {
			return 0;
		}
		else {
			return 1;
		}
	}
	
	function insert_journal_expense_crv_credit($data_journal_crv_credit){
		$this->db->insert('at_eprealjournal', $data_journal_crv_credit);
		
		if ($this->db->trans_status() === FALSE) {
			return 0;
		}
		else {
			return 1;
		}
	}
	
	function insert_journal_expense_relize_credit($data_journal_relize_credit){
		$this->db->insert('at_eprealjournal', $data_journal_relize_credit);
		
		if ($this->db->trans_status() === FALSE) {
			return 0;
		}
		else {
			return 1;
		}
	}
	
	function insert_journal_expense_cpv_debit($data_journal_exp_cpv_debit){
		$this->db->insert('at_eprealjournal', $data_journal_exp_cpv_debit);
		
		if ($this->db->trans_status() === FALSE) {
			return 0;
		}
		else {
			return 1;
		}
	}
	
	function insert_journal_bank_cpv_credit($data_journal_bank_cpv_credit){
		$this->db->insert('at_eprealjournal', $data_journal_bank_cpv_credit);
		
		if ($this->db->trans_status() === FALSE) {
			return 0;
		}
		else {
			return 1;
		}
	}
	
	function insert_journal_expense_cpv_credit($data_journal_cpv_credit){
		$this->db->insert('at_eprealjournal', $data_journal_cpv_credit);
		
		if ($this->db->trans_status() === FALSE) {
			return 0;
		}
		else {
			return 1;
		}
	}
	
	function get_detail($id){
		$query = $this->db->join('at_epaymentreal','at_expensepayment.EPayNumber = at_epaymentreal.EPayNumber', 'INNER');
        $query = $this->db->get_where($this->table, array('at_expensepayment.EPayNumber' => $id));
		if($query->num_rows()>0){
			return $query->row();
		}
		else{
			return false;
		}      
    }	
	
	function get_cashbon_number($id){
		$this->db->select('EBonNumber');
		$this->db->from('at_expensepayment');
		$this->db->where('EPayNumber', $id);
		$query = $this->db->get();

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $EBonNumber = $row->EBonNumber;
            }

            return $EBonNumber;
        }
        else {
            return FALSE;
        }
	}
	
	function get_amount_cashbon($ebonNumber){
		$this->db->select('EBonAmount');
		$this->db->from('at_ebondetail');
		$this->db->where('EBonNumber', $ebonNumber);
		$query = $this->db->get();

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $EBonAmount = $row->EBonAmount;
            }

            return $EBonAmount;
        }
        else {
            return FALSE;
        }
	}
	
	function get_date_cashbon($ebonNumber){
		$this->db->select('EBonDate');
		$this->db->from('at_expensecashbon');
		$this->db->where('EBonNumber', $ebonNumber);
		$query = $this->db->get();

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $EBonDate = $row->EBonDate;
            }

            return $EBonDate;
        }
        else {
            return FALSE;
        }
	}
	
	function get_rate_cashbon($ebonNumber){
		$this->db->select('CurrencyRate');
		$this->db->from('at_expensecashbon');
		$this->db->where('EBonNumber', $ebonNumber);
		$query = $this->db->get();

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $CurrencyRate = $row->CurrencyRate;
            }

            return $CurrencyRate;
        }
        else {
            return FALSE;
        }
	}
	
	function get_bank_cashbon($ebonNumber){
		$this->db->select('BankPayment');
		$this->db->from('at_expensecashbon');
		$this->db->where('EBonNumber', $ebonNumber);
		$query = $this->db->get();

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $BankPayment = $row->BankPayment;
            }

            return $BankPayment;
        }
        else {
            return FALSE;
        }
	}
	
	function get_amount_expense_payment($id){
		$this->db->select('sum(ExpenseAmount) as ExpenseAmount');
		$this->db->from('at_epaydetail');
		$this->db->where('EPayNumber', $id);
		$query = $this->db->get();

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $ExpenseAmount = $row->ExpenseAmount;
            }

            return $ExpenseAmount;
        }
        else {
            return FALSE;
        }
	}
	
	function get_detail_item($number){
	    $this->db->_protect_identifiers = FALSE;
	    $this->db->select("ExpCategoryID as ExpDetailID, ExpCategoryName as ExpDetailName, ExpenseDesc as ExpDetailDesc, ExpenseAmount as ItemAmount, EPayDetailID");
        $query = $this->db->get_where($this->detail, array('EPayNumber' => $number));
		$this->db->_protect_identifiers = TRUE;
        $result=$query->result();
		return json_encode($result);
    }
	
	function get_detail_actual($number){
	    $this->db->select("
							BankReceipt as AccountNumber, EPRealDate  as CrCvDate, 
							EPRealAmount  as AmountCRV, at_expensepayment.EPayAmount, 
							at_epaymentreal.EPRealTransType, at_expensecashbon.EBonAmount
						");
	    $this->db->_protect_identifiers = FALSE;
		$this->db->join('at_expensepayment','at_epaymentreal.EPayNumber=at_expensepayment.EPayNumber');
		$this->db->join('at_expensecashbon','at_expensepayment.EBonNumber=at_expensecashbon.EBonNumber');
		//$this->db->join('am_accountgl','at_compaccountgl.AccountNumber=am_accountgl.AccountNumber');
        $query = $this->db->get_where('at_epaymentreal', array('at_epaymentreal.EPayNumber' => $number));
        $this->db->_protect_identifiers = TRUE;   
		return $query->result();
    }
	
	function get_acc_name($acc_no){
		$this->db->select('AccountName');
		$this->db->from('am_accountgl');
		$this->db->where('AccountNumber', $acc_no);
		$query = $this->db->get();
		
        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $AccountName = $row->AccountName;
            }

            return $AccountName;
        }
        else {
            return FALSE;
        }
	}
	
	function get_crv_number_detail($EPayNumber){
		$this->db->select('EPRealNumber');
		$this->db->from('at_epaymentreal');
		$this->db->where('EPayNumber', $EPayNumber);
		$this->db->where('EPRealTransType', 1);
		$query = $this->db->get();

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $EPRealNumber = $row->EPRealNumber;
            }

            return $EPRealNumber;
        }
        else {
            return FALSE;
        }
	}
	
	function get_cpv_number_detail($EPayNumber){
		$this->db->select('EPRealNumber');
		$this->db->from('at_epaymentreal');
		$this->db->where('EPayNumber', $EPayNumber);
		$this->db->where('EPRealTransType', 2);
		$query = $this->db->get();

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $EPRealNumber = $row->EPRealNumber;
            }

            return $EPRealNumber;
        }
        else {
            return FALSE;
        }
	}
	
	function get_journal($EPayNumber){
		$this->db->select('EPRealAccountNumber');
		$this->db->from('at_eprealjournal');
		$this->db->where('EPRealNumber_BOut', $EPayNumber);
		//$this->db->where('EPRealAccountNumber', $account_exp_relize);
		$query = $this->db->get();

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $EPRealAccountNumber = $row->EPRealAccountNumber;
            }

            return $EPRealAccountNumber;
        }
        else {
            return FALSE;
        }
	}
	
	function update_payment_cashbon($data, $id){
		$this->db->update($this->table, $data, array('EPayNumber' => $id));

        if ($this->db->trans_status() == FALSE) {
            return 0;
        }
        else {
            return 1;
        }
	}
	
	function update_detail_payment_cashbon($data , $id){
		$this->db->update($this->detail, $data, array('EPayDetailID' => $id));
		
		if ($this->db->trans_status() === FALSE) {
			return 0;
		}
		else {
			return 1;
		}
	}
	
	function update_detail_crv($data, $id){
		$this->db->update('at_epaymentreal', $data, array('EPayNumber' => $id));
		
		if ($this->db->trans_status() === FALSE) {
			return 0;
		}
		else {
			return 1;
		}
	}
	
	function update_journal_expense_crv_debit($data_journal_exp_crv_debit, $id, $account_exp_payable){
		$this->db->update('at_eprealjournal', $data_journal_exp_crv_debit, array('EPRealNumber_BOut' => $id, 'EPRealAccountNumber' => $account_exp_payable));
		
		if ($this->db->trans_status() === FALSE) {
			return 0;
		}
		else {
			return 1;
		}
	}
	
	function update_journal_bank_crv_debit($data_journal_bank_crv_debit, $id,  $AccountNumber){
		$this->db->update('at_eprealjournal', $data_journal_bank_crv_debit, array('EPRealNumber_BOut' => $id, 'EPRealAccountNumber' => $AccountNumber));
		
		if ($this->db->trans_status() === FALSE) {
			return 0;
		}
		else {
			return 1;
		}
	}
	
	function update_journal_expense_crv_credit($data_journal_crv_credit, $id, $account_adv_cashbon){
		$this->db->update('at_eprealjournal', $data_journal_crv_credit, array('EPRealNumber_BOut' => $id, 'EPRealAccountNumber' => $account_adv_cashbon));
		
		if ($this->db->trans_status() === FALSE) {
			return 0;
		}
		else {
			return 1;
		}
	}
	
	function update_detail_cpv($data, $id){
		$this->db->update('at_epaymentreal', $data, array('EPayNumber' => $id));

		if ($this->db->trans_status() === FALSE) {
			return 0;
		}
		else {
			return 1;
		}
	}
	
	function update_journal_expense_cpv_debit($data, $id, $account_exp_payable){
		$this->db->update('at_eprealjournal', $data, array('EPRealNumber_BOut' => $id, 'EPRealAccountNumber' => $account_exp_payable));
		
		if ($this->db->trans_status() === FALSE) {
			return 0;
		}
		else {
			return 1;
		}
	}
	
	function update_journal_bank_cpv_credit($data, $id, $AccountNumber){
		$this->db->update('at_eprealjournal', $data, array('EPRealNumber_BOut' => $id, 'EPRealAccountNumber' => $AccountNumber));
		
		if ($this->db->trans_status() === FALSE) {
			return 0;
		}
		else {
			return 1;
		}
	}
	
	function update_journal_expense_cpv_credit($data, $id, $account_adv_cashbon){
		$this->db->update('at_eprealjournal', $data, array('EPRealNumber_BOut' => $id, 'EPRealAccountNumber' => $account_adv_cashbon));
		
		if ($this->db->trans_status() === FALSE) {
			return 0;
		}
		else {
			return 1;
		}
	}
	
	function update_journal_expense_payment_debit($data, $id){
		$this->db->update('at_eprealjournal', $data, array('EPRealNumber_BOut' => $id));
		
		if ($this->db->trans_status() === FALSE) {
			return 0;
		}
		else {
			return 1;
		}
	}
	
	function update_journal_expense_payment_credit($data, $id){
		$this->db->update('at_eprealjournal', $data, array('EPRealNumber_BOut' => $id));
		
		if ($this->db->trans_status() === FALSE) {
			return 0;
		}
		else {
			return 1;
		}
	}
	
	function update_journal_relize_credit($data, $id, $account_exp_relize){
		$this->db->update('at_eprealjournal', $data, array('EPRealNumber_BOut' => $id, 'EPRealAccountNumber' => $account_exp_relize));

		if ($this->db->trans_status() === FALSE) {
			return 0;
		}
		else {
			return 1;
		}
	}
	
	function get_status_journal($EPayNumber){
		$this->db->select('EPRealTransType');
		$this->db->from('at_epaymentreal');
		$this->db->where('EPayNumber', $EPayNumber);
		$query = $this->db->get();

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $EPRealTransType = $row->EPRealTransType;
            }

            return $EPRealTransType;
        }
        else {
            return FALSE;
        }
	}
	
	function get_actual_number($payment_number){
		$this->db->select('EPRealNumber');
		$this->db->from('at_epaymentreal');
		$this->db->where('EPayNumber', $payment_number);
		$query = $this->db->get();

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $EPRealNumber = $row->EPRealNumber;
            }

            return $EPRealNumber;
        }
        else {
            return FALSE;
        }
	}
	
	function get_branch_code($payment_number){
		$this->db->select('BranchCode');
		$this->db->from('at_expensepayment');
		$this->db->where('EPayNumber', $payment_number);
		$query = $this->db->get();

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $BranchCode = $row->BranchCode;
            }

            return $BranchCode;
        }
        else {
            return FALSE;
        }
	}
	
	function get_date_journal_payment($payment_number){
		$this->db->select('*');
		$this->db->from('at_expensepayment');
		$this->db->where('EPayNumber', $payment_number);
		$query = $this->db->get();
		
		$result=$query->result();
		return json_encode($result);
	}
	
	function get_detail_payment_description($payment_number){
		$this->db->select('ExpenseDesc');
		$this->db->from('at_epaydetail');
		$this->db->where('EPayNumber', $payment_number);
		$query = $this->db->get();
		
		$result=$query->result();
		return json_encode($result);
	}
	
	function get_journal_payment($get_journal_payment, $BranchCode){
		$this->db->select('am_accountgl.AccountNumber, am_accountgl.AccountName, at_eprealjournal.EPRealSaldoCredit, 
						   at_eprealjournal.EPRealSaldoDebet, at_eprealjournal.EPRealNumber');
		$this->db->from('at_compaccountgl');
		$this->db->join('at_eprealjournal','at_compaccountgl.AccountNumber=at_eprealjournal.EPRealAccountNumber');
		$this->db->join('am_accountgl','at_compaccountgl.AccountNumber=am_accountgl.AccountNumber');
		$this->db->where('at_compaccountgl.BranchCode', $BranchCode);
		$this->db->where('at_compaccountgl.GLSaldoYears', date('Y'));
		$this->db->where('at_eprealjournal.EPRealNumber_BOut', $get_journal_payment);
		$query = $this->db->get();
		
		$result=$query->result();
		return json_encode($result);
	}
	
	function update_status_cashbon($EBonNumber, $status_cashbon){
		$this->db->where('EBonNumber', $EBonNumber);
		return $this->db->update('at_expensecashbon', $status_cashbon);
	}
	
	function update_status_payment($EPayNumber, $status_payment){
		$this->db->where('EPayNumber', $EPayNumber);
		return $this->db->update('at_expensepayment', $status_payment);
	}
	
	function status_finished($app_finished, $payment_number){
		$this->db->where('EPayNumber', $payment_number);
		return $this->db->update('at_expensepayment', $app_finished);
	}
	
	function delete_payment_cashbon($EPayNumber){
		$this->db->where('EPayNumber', $EPayNumber);
		return $this->db->delete($this->table);
	}
	
	function delete_detail_payment_cashbon($EPayNumber){
		$this->db->where('EPayNumber', $EPayNumber);
		return $this->db->delete($this->detail);
	}
	
	function delete_detail_crv($EPayNumber){
		$this->db->where('EPayNumber', $EPayNumber);
		return $this->db->delete('at_epaymentreal');
	}
	
	function delete_journal_expense_crv($EPayNumber){
		$this->db->where('EPRealNumber_BOut', $EPayNumber);
		return $this->db->delete('at_eprealjournal');
	}
	
	function delete_detail_cpv($EPayNumber){
		$this->db->where('EPayNumber', $EPayNumber);
		return $this->db->delete('at_epaymentreal');
	}
	
	function delete_journal_cpv($EPayNumber){
		$this->db->where('EPRealNumber_BOut', $EPayNumber);
		return $this->db->delete('at_eprealjournal');
	}
	
	function delete_journal_payment($EPayNumber){
		$this->db->where('EPRealNumber_BOut', $EPayNumber);
		return $this->db->delete('at_eprealjournal');
	}
	
	function update_trans_status($payment_number, $data){
		$this->db->where('EPayNumber', $payment_number);
		return $this->db->update('at_expensepayment', $data);
	}
	
	function cek_status_by_number_payment($payment_number){
		$this->db->select('TransStatus');
		$this->db->from('at_expensepayment');
		$this->db->where('EPayNumber', $payment_number);
		$query = $this->db->get();

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $TransStatus = $row->TransStatus;
            }

            return $TransStatus;
        }
        else {
            return FALSE;
        }	
	}
	
	function approval_status($data,$payment_number){
		$this->db->where('EPayNumber', $payment_number);
		return $this->db->update($this->table, $data);
	}
	
	function cek_app($payment_number){
		$this->db->select('AppLevel1Status, AppLevel2Status');
		$this->db->from('at_expensepayment');
		$this->db->where('EPayNumber', $payment_number);
		$this->db->where('AppLevel1Status', 1);
		$this->db->where('AppLevel2Status', 1);
		$query = $this->db->get();

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $app = 1;
            }
            return $app;
        }
        else {
		    $app = 0;
            return $app;
        }	
	}
	
	function cek_status_app_mgr($payment_number){
		$this->db->select('AppLevel1Status');
		$this->db->from('at_expensepayment');
		$this->db->where('EPayNumber', $payment_number);
		$this->db->where('AppLevel1Status', 1);
		$query = $this->db->get();

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $AppLevel1Status = $row->AppLevel1Status;
            }
            return $AppLevel1Status;
        }
        else {
		    return FALSE;
        }	
	}
	
	function cek_status_app_dir($payment_number){
		$this->db->select('AppLevel2Status');
		$this->db->from('at_expensepayment');
		$this->db->where('EPayNumber', $payment_number);
		$this->db->where('AppLevel2Status', 1);
		$query = $this->db->get();

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $AppLevel2Status = $row->AppLevel2Status;
            }
            return $AppLevel2Status;
        }
        else {
		    return FALSE;
        }	
	}
	
	function get_detail_pembiayaan($payment_number){
		$this->db->select('*');
		$this->db->from('at_epaydetail');
		$this->db->where('EPayNumber', $payment_number);
		$query = $this->db->get();
		
		$result=$query->result();
		return json_encode($result);
	}
	
	function get_at_expensepayment($payment_number){
		$this->db->select('*');
		$this->db->from('at_expensepayment');
		$this->db->where('EPayNumber', $payment_number);
		$query = $this->db->get();
		
		$result=$query->row();
		return $result;
	}
	
	function create_epay_number($date/*, $BankOutCode*/){
		$branch     = $this->session->userdata('branch');
        $month      = date('m', strtotime($date));
        $year       = date('y', strtotime($date));
        $epay_number  = $branch."/".$month.$year."EP/";

        $this->db->like('EPayNumber', $epay_number);
        $num    = $this->db->get($this->table)->num_rows()+1;
        $long   = strlen($num);
		
        if($long == 1){
            $number='00'.$num;
        }
        elseif($long == 2){
            $number='0'.$num;
        }
        else{
            $number=$num;
        }
	
        $new_epay_number = $epay_number.$number;
        
        while($this->check_code_exist($new_epay_number)){
            $num++;
            $long = strlen($num);
            
            if($long == 1){
                $number = '00'.$num;
            }
            elseif($long == 2){
                $number='0'.$num;
            }
            else{
                $number=$num;
            }
            
            $new_epay_number = $epay_number.$number;		
        }
    
        return $new_epay_number;
	}
	
	function check_code_exist($Number) {
        $query = $this->db->get_where($this->table, array('EPayNumber' => $Number), 1, 0);
		
        if ($query->num_rows() > 0) {
            return TRUE;
        }
        else {
            return FALSE;
        }	
    }
	
	function cek_kode($crv_number) {
        $query = $this->db->get_where($this->crv_table, array('EPRealNumber' => $crv_number), 1, 0);
		
        if ($query->num_rows() > 0) {
            return TRUE;
        }
        else {
            return FALSE;
        }	
    }
	
	function create_crv_number($bank_code_masuk){
        $branch_code    = $this->session->userdata('branch');
		
        $month	= date('m');
        $year	= date('y');
        
        $crv_number    = $branch_code . "/" . $month . $year . "ER/".$bank_code_masuk;
        $this->db->like('EPRealNumber', $crv_number);
        
        $num    = $this->db->get($this->crv_table)->num_rows() + 1;
        $long   = strlen($num);
	
        if($long == 1){
            $number = '00'. $num;
        }
        elseif($long == 2){
            $number = '0'. $num;
        }
        else{
            $number = $num;
        }
	
        $new_crv_number = $crv_number . $number;
        
        while($this->cek_kode($new_crv_number)){
            $num++;
            $long = strlen($num);
	
            if($long == 1){
                $number = '00' . $num;
            }
            elseif($long == 2){
                $number = '0' . $num;
            }
            else{
                $number = $num;
            }
            
            $new_crv_number = $crv_number . $number;		
        }
        return $new_crv_number;
    }
	
	function get_detail_item_for_payment($ExpDetailID){
        $this->db->_protect_identifiers = false;
        $this->db->select("*, '0' as ItemAmount");
        $this->db->where_in('ExpDetailID', $ExpDetailID);
        $query = $this->db->get('sys_expensedetail');
        $this->db->_protect_identifiers = true;
        
        return $query->result();
    }
}
	