<?php

/* 	
 * 	Author 	: Dhidhik
 * 	Tanggal : 15-10-2015	
 * 	Revisi tgl	: 24-11-2015
 *      Desc            : tambah menu account list & bank book
 */

class M_index_cash_bank extends CI_Model {
    
    public $tbl_cpayment    = "at_cashpayment";
    public $dtl_cpayment    = "at_cpaydetail";
    public $jrn_cpayment    = "at_cpayjournal";
    public $tbl_cdeposit    = "at_cashdeposit";
    public $jrn_cdeposit    = "at_cdepjournal";
    public $tbl_bsweep      = "at_banksweep";
    public $jrn_bsweep      = "at_bsweepjournal";
    public $tbl_afiliasiDebt= "at_afiliasidebt";
    public $tbl_afiliasiInst= "at_afiliasiinstallment";   
    public $tbl_employeeDebt= "at_employeedebt"; 
    public $tbl_employeeInst= "at_employeeinstallment";  
    public $tbl_transfercash= "at_transfercash";
    public $jrn_tcashjournal= "at_tcashjournal"; 
    public $tbl_ibank       = "at_interbanktransfer";
    public $jrn_ibankjournal= "at_ibanktransferjournal"; 
    public $tbl_oincome     = "at_otherincome";
    public $tbl_opayment    = "at_otherpayment";
    
    public $branch          = "am_branch";
    public $account         = "at_compaccountgl";
    public $accountgl       = "am_accountgl";
    public $subtype         = "am_accountsubtype";

    function pre_get_all_payment() {
        $CompCode       = ($this->input->post('cmb_company')!=null)?$this->input->post('cmb_company'):'';
        $BranchCode     = ($this->input->post('cmb_branch')!=null)?$this->input->post('cmb_branch'):'';
        $DateFrom       = ($this->input->post('txt_DateFrom')!=null)?$this->input->post('txt_DateFrom'):'';
        $DateTo         = ($this->input->post('txt_DateTo')!=null)?$this->input->post('txt_DateTo'):'';
        $PaymentFrom    = ($this->input->post('cmb_paymentFrom')!=null)?$this->input->post('cmb_paymentFrom'):'';
        $TransStatus    = ($this->input->post('chk_TransStatus')!=null)?$this->input->post('chk_TransStatus'):'';
        $txtTable       = ($this->input->post('txtTable')!=null)?$this->input->post('txtTable'):'';
        $txtField       = ($this->input->post('txtField')!=null)?$this->input->post('txtField'):'';
        $typeShorting   = ($this->input->post('typeShorting')!=null)?$this->input->post('typeShorting'):'';
        $lenCompCode    = strlen($CompCode);
        
        $Pages          = ($this->input->post('pages')!=null)?$this->input->post('pages'):'';
        $limitStart     = ($Pages['limit'] * $Pages['pages']) - $Pages['limit'];
        /* =================================END post dari find di kiri==================================================== */
           
        /* ==========================================by session======================================================= */
        $sess_company = $this->session->userdata('company');
        $sess_branch = $this->session->userdata('branch');
        $sess_branch_status = $this->session->userdata('branch_status');
        /* =========================================================================================================== */
    
        $this->db->select("at_cashpayment.CPayNumber as code");
        $this->db->from('at_cashpayment');
        $this->db->join('am_branch', 'at_cashpayment.BranchCode=am_branch.BranchCode');
        $this->db->join('at_cpayjournal', 'at_cashpayment.CpayNumber=at_cpayjournal.CpayNumber');
        $this->db->join('at_compaccountgl', 'at_cpayjournal.CPayAccountNumber=at_compaccountgl.AccountNumber');
        $this->db->join('am_accountgl', 'at_compaccountgl.AccountNumber=am_accountgl.AccountNumber');
        $this->db->join('am_accountsubtype', 'am_accountgl.AccountSubTypeID=am_accountsubtype.AccountSubTypeID');        
        $this->db->where('SUBSTRING(at_cashpayment.BranchCode,1,'.$lenCompCode.')', $CompCode);
        /* ========================jika kondisi tidak sama dengan kosong di find lakukan proses di if================= */
        if ($PaymentFrom != '' && $PaymentFrom!='All') {
            $this->db->where_in('at_cashpayment.BankPayment', $PaymentFrom);
        }
        if ($TransStatus != '') {
            $this->db->where_in('at_cashpayment.TransStatus', $TransStatus);
        } else {
            /*default adalah 2,6. Finished dan Need Approval*/
            $this->db->where_in('at_cashpayment.TransStatus', array(2, 6));
        }

        /* ===================================find by branch code========================================================= */
        
        if ($BranchCode != "" && $BranchCode != "All" ) {
            $this->db->where('at_cashpayment.BranchCode', $BranchCode);
        } 
        
        //========where sesuai Company========================//
        if ($CompCode != "" && $CompCode != "All") {
            $this->db->where('at_compaccountgl.CompanyCode', $CompCode);
        }
        //-----------------------end-------------------------------------------------//
        
        //=====================cek data tanggal jika tanggal awal lebih dari tanggal akhir=============//
        if (($DateFrom != '') && ($DateTo != '')) {
            if ($DateFrom <= $DateTo) {
                $this->db->where('at_cashpayment.CPayDate >=', date("Y-m-d", strtotime($DateFrom))); //change
                $this->db->where('at_cashpayment.CPayDate <=', date("Y-m-d", strtotime($DateTo))); //change
            }
        }
        //=====================END cek data tanggal jika tanggal awal lebih dari tanggal akhir=============//
        
        $this->db->group_by("at_cashpayment.CPayNumber");
        /* ==============================================SHORTING TABLE========================================================== */
        if($txtTable=="" && $txtField==""){
            $this->db->order_by($this->tbl_cpayment . '.CPayNumber', 'ASC');
            $this->db->order_by($this->jrn_cpayment . '.CPaySaldoDebet', 'DESC');
        }
        else if($txtField=="CPayDate"){
            $this->db->order_by($this->$txtTable . '.'.$txtField, $typeShorting);
            $this->db->order_by($this->tbl_cpayment . '.CPayNumber', '');
            $this->db->order_by($this->jrn_cpayment . '.CPaySaldoDebet', 'DESC');
        }
        else if($txtField=="CPayNumber"){
            $this->db->order_by($this->$txtTable . '.'.$txtField, $typeShorting);
            $this->db->order_by($this->jrn_cpayment . '.CPaySaldoDebet', 'DESC');
        }
        else if($txtField=="CPaySaldoDebet"){
            $this->db->order_by($this->tbl_cpayment . '.CPayNumber', '');
            $this->db->order_by($this->$txtTable . '.'.$txtField, $typeShorting);
        }
        else if($txtField=="CPaySaldoCredit"){
            $this->db->order_by($this->tbl_cpayment . '.CPayNumber', '');
            $this->db->order_by($this->$txtTable . '.'.$txtField, $typeShorting);
        }
        else if($txtField=="TransStatus"){
            $this->db->order_by($this->$txtTable . '.'.$txtField, $typeShorting);
            $this->db->order_by($this->tbl_cpayment . '.CPayNumber', '');
            $this->db->order_by($this->jrn_cpayment . '.CPaySaldoDebet', 'DESC');
        }else{
            $this->db->order_by($this->tbl_cpayment . '.CPayNumber', 'ASC');
            $this->db->order_by($this->jrn_cpayment . '.CPaySaldoDebet', 'DESC');
        }             
        /* ==============================================END SHORTING TABLE========================================================== */        
        $this->db->limit($Pages['limit'], $limitStart);
        
        $query = $this->db->get();
        //echo "<hr>".$this->db->last_query()."<hr>";
        $result = $query->result();
        return $result;
    }
    function get_all_payment() {
        $CompCode       = ($this->input->post('cmb_company')!=null)?$this->input->post('cmb_company'):'';
        $BranchCode     = ($this->input->post('cmb_branch')!=null)?$this->input->post('cmb_branch'):'';
        $DateFrom       = ($this->input->post('txt_DateFrom')!=null)?$this->input->post('txt_DateFrom'):'';
        $DateTo         = ($this->input->post('txt_DateTo')!=null)?$this->input->post('txt_DateTo'):'';
        $PaymentFrom    = ($this->input->post('cmb_paymentFrom')!=null)?$this->input->post('cmb_paymentFrom'):'';
        $TransStatus    = ($this->input->post('chk_TransStatus')!=null)?$this->input->post('chk_TransStatus'):'';
        $txtTable       = ($this->input->post('txtTable')!=null)?$this->input->post('txtTable'):'';
        $txtField       = ($this->input->post('txtField')!=null)?$this->input->post('txtField'):'';
        $typeShorting   = ($this->input->post('typeShorting')!=null)?$this->input->post('typeShorting'):'';
        $lenCompCode    = strlen($CompCode);
        
        $PreData        = ($this->input->post('predata')!=[null])?$this->input->post('predata'):'';
        /* =================================END post dari find di kiri==================================================== */
           
        /* ==========================================by session======================================================= */
        $sess_company = $this->session->userdata('company');
        $sess_branch = $this->session->userdata('branch');
        $sess_branch_status = $this->session->userdata('branch_status');
        /* =========================================================================================================== */
    
        $this->db->select('distinct(at_cashpayment.CPayNumber)CPayNumber,am_branch.BranchName, at_cashpayment.CPayDate, at_compaccountgl.AccountNumber, am_accountgl.AccountName, at_cpayjournal.CPaySaldoDebet, at_cpayjournal.CPaySaldoCredit, at_cashpayment.TransStatus');
        $this->db->from('at_cashpayment');
        $this->db->join('am_branch', 'at_cashpayment.BranchCode=am_branch.BranchCode');
        $this->db->join('at_cpayjournal', 'at_cashpayment.CpayNumber=at_cpayjournal.CpayNumber');
        $this->db->join('at_compaccountgl', 'at_cpayjournal.CPayAccountNumber=at_compaccountgl.AccountNumber');
        $this->db->join('am_accountgl', 'at_compaccountgl.AccountNumber=am_accountgl.AccountNumber');
        $this->db->join('am_accountsubtype', 'am_accountgl.AccountSubTypeID=am_accountsubtype.AccountSubTypeID');        
        $this->db->where('SUBSTRING(at_cashpayment.BranchCode,1,'.$lenCompCode.')', $CompCode);
        
        $this->db->where_in('at_cashpayment.CPayNumber', $PreData);
        /* ========================jika kondisi tidak sama dengan kosong di find lakukan proses di if================= */
        if ($PaymentFrom != '' && $PaymentFrom!='All') {
            $this->db->where_in('at_cashpayment.BankPayment', $PaymentFrom);
        }
        if ($TransStatus != '') {
            $this->db->where_in('at_cashpayment.TransStatus', $TransStatus);
        } else {
            /*default adalah 2,6. Finished dan Need Approval*/
            $this->db->where_in('at_cashpayment.TransStatus', array(2, 6));
        }

        /* ===================================find by branch code========================================================= */
        /* get data dari  combobox find di view */
        //echo "Branch John = {$BranchCode} <br>";
        if ($BranchCode != "" && $BranchCode != "All" ) {
            $this->db->where('at_cashpayment.BranchCode', $BranchCode);
        } 
//        else {
//            /*
//              inisialisasi untuk loading pertama page branch info
//             */
//            //----------------kondisi by company-------------------------------------//
//            /*
//              branch status 0 = HO
//              branch status 1 = depo/branch
//              jangan lupa sesion data di atas untuk setiap kondisi
//              jangan lupa tambahin case when trans status
//             */
//            $this->db->where('at_cashpayment.BranchCode', $sess_branch);
//            //-----------------------end-------------------------------------------------//
//        }
        /* =======================================================end======================================================== */
        
        //========where sesuai Company========================//
        if ($CompCode != "" && $CompCode != "All") {
            $this->db->where('at_compaccountgl.CompanyCode', $CompCode);
        }
        //-----------------------end-------------------------------------------------//
        
        //=====================cek data tanggal jika tanggal awal lebih dari tanggal akhir=============//
        if ((($DateFrom != '') && ($DateTo != ''))) {
            if ($DateFrom <= $DateTo) {
                $this->db->where('at_cashpayment.CPayDate >=', date("Y-m-d", strtotime($DateFrom))); //change
                $this->db->where('at_cashpayment.CPayDate <=', date("Y-m-d", strtotime($DateTo))); //change
            }
        }
        //=====================END cek data tanggal jika tanggal awal lebih dari tanggal akhir=============//
        
        /* ==============================================SHORTING TABLE========================================================== */
        if($txtTable=="" && $txtField==""){
            $this->db->order_by($this->tbl_cpayment . '.CPayNumber', 'ASC');
            $this->db->order_by($this->jrn_cpayment . '.CPaySaldoDebet', 'DESC');
        }
        else if($txtField=="CPayDate"){
            $this->db->order_by($this->$txtTable . '.'.$txtField, $typeShorting);
            $this->db->order_by($this->tbl_cpayment . '.CPayNumber', '');
            $this->db->order_by($this->jrn_cpayment . '.CPaySaldoDebet', 'DESC');
        }
        else if($txtField=="CPayNumber"){
            $this->db->order_by($this->$txtTable . '.'.$txtField, $typeShorting);
            $this->db->order_by($this->jrn_cpayment . '.CPaySaldoDebet', 'DESC');
        }
        else if($txtField=="CPaySaldoDebet"){
            $this->db->order_by($this->tbl_cpayment . '.CPayNumber', '');
            $this->db->order_by($this->$txtTable . '.'.$txtField, $typeShorting);
        }
        else if($txtField=="CPaySaldoCredit"){
            $this->db->order_by($this->tbl_cpayment . '.CPayNumber', '');
            $this->db->order_by($this->$txtTable . '.'.$txtField, $typeShorting);
        }
        else if($txtField=="TransStatus"){
            $this->db->order_by($this->$txtTable . '.'.$txtField, $typeShorting);
            $this->db->order_by($this->tbl_cpayment . '.CPayNumber', '');
            $this->db->order_by($this->jrn_cpayment . '.CPaySaldoDebet', 'DESC');
        }else{
            $this->db->order_by($this->tbl_cpayment . '.CPayNumber', 'ASC');
            $this->db->order_by($this->jrn_cpayment . '.CPaySaldoDebet', 'DESC');
        }             
        /* ==============================================END SHORTING TABLE========================================================== */        
        
        $query = $this->db->get();
//        echo "<hr>".$this->db->last_query()."<hr>";
        $result = $query->result();
        return $result;
    }

    function pre_get_all_deposit() {
        $CompCode       = ($this->input->post('cmb_company')!=null)?$this->input->post('cmb_company'):'';
        $BranchCode     = ($this->input->post('cmb_branch')!=null)?$this->input->post('cmb_branch'):'';
        $BranchDepFrom  = ($this->input->post('branchDepositFrom')!=null)?$this->input->post('branchDepositFrom'):'';
        $BranchDepTo    = ($this->input->post('branchDepositTo')!=null)?$this->input->post('branchDepositTo'):'';
        $DateFrom       = ($this->input->post('txt_DateFrom')!=null)?$this->input->post('txt_DateFrom'):'';
        $DateTo         = ($this->input->post('txt_DateTo')!=null)?$this->input->post('txt_DateTo'):'';
        $DepositFrom    = ($this->input->post('cmb_deposit_from')!=null)?$this->input->post('cmb_deposit_from'):'';
        $DepositTo      = ($this->input->post('cmb_deposit_to')!=null)?$this->input->post('cmb_deposit_to'):'';
        $TransStatus    = ($this->input->post('chk_TransStatus')!=null)?$this->input->post('chk_TransStatus'):'';
        $txtTable       = ($this->input->post('txtTable')!=null)?$this->input->post('txtTable'):'';
        $txtField       = ($this->input->post('txtField')!=null)?$this->input->post('txtField'):'';
        $typeShorting   = ($this->input->post('typeShorting')!=null)?$this->input->post('typeShorting'):'';
        
        $Pages          = ($this->input->post('pages')!=null)?$this->input->post('pages'):'';
        $limitStart     = ($Pages['limit'] * $Pages['pages']) - $Pages['limit'];
        /* =================================END post dari find di kiri==================================================== */
           
        /* ==========================================by session======================================================= */
        $sess_company       = $this->session->userdata('company');
        $sess_branch        = $this->session->userdata('branch');
        $sess_branch_status = $this->session->userdata('branch_status');
        /* =========================================================================================================== */
    
        $this->db->select("at_cashdeposit.CDepNumber as code");
        $this->db->from('at_cashdeposit');
        $this->db->join('am_accountgl as acBankPayment', 'at_cashdeposit.BankPayment=acBankPayment.AccountNumber');
        $this->db->join('am_accountgl as acBankReceipt', 'at_cashdeposit.BankReceipt=acBankReceipt.AccountNumber');
        $this->db->join('am_branch', 'at_cashdeposit.BranchCode=am_branch.BranchCode');
        $this->db->join('at_compaccountgl as compaccBankPayment', 'at_cashdeposit.BankPayment=compaccBankPayment.AccountNumber');
        $this->db->join('at_compaccountgl as compaccBankReceipt', 'at_cashdeposit.BankReceipt=compaccBankReceipt.AccountNumber');
        $this->db->where_in('compaccBankPayment.TransStatus', 2);
        $this->db->where_in('compaccBankReceipt.TransStatus', 2);
        /* ========================jika kondisi tidak sama dengan kosong di find lakukan proses di if================= */
        if ($DepositFrom != '' && $DepositFrom!='All') {
            $this->db->where_in('at_cashdeposit.BankPayment', $DepositFrom);
        }
        if ($DepositTo != '' && $DepositTo!='All') {
            $this->db->where_in('at_cashdeposit.BankReceipt', $DepositTo);
        }
        if ($TransStatus != '') {
            $this->db->where_in('at_cashdeposit.TransStatus', $TransStatus);
        } else {
            /*default adalah 2,6. Finished dan Need Approval*/
            $this->db->where_in('at_cashdeposit.TransStatus', array(2, 6));
        }

        /* ===================================find by branch code========================================================= */
        if ($BranchCode != "" && $BranchCode != "All" ) {
            $this->db->where('at_cashdeposit.BranchCode', $BranchCode);
//            $this->db->where('compaccBankPayment.BranchCode', $BranchCode);
//            $this->db->where('compaccBankReceipt.BranchCode', $BranchCode);
        } 
        /* ==================================end find by branch code===================================================== */
        
        /* ===================================find by branch DepositFrom code========================================================= */
        if ($BranchDepFrom != "" && $BranchDepFrom != "All" ) {
            $this->db->where('at_cashdeposit.BranchCode', $BranchDepFrom);
            $this->db->where('compaccBankPayment.BranchCode', $BranchDepFrom);
//            $this->db->where('compaccBankReceipt.BranchCode', $BranchDepFrom);
        } 
        /* ==================================end find by branch DepositFrom code===================================================== */
        /* ===================================find by branch DepositTo code========================================================= */
        if ($BranchDepTo != "" && $BranchDepTo != "All" ) {
            $this->db->where('at_cashdeposit.BranchCode', $BranchDepTo);
//            $this->db->where('compaccBankPayment.BranchCode', $BranchDepTo);
            $this->db->where('compaccBankReceipt.BranchCode', $BranchDepTo);
        } 
        /* ==================================end find by branch DepositTo code===================================================== */
        //========where sesuai Company========================//
        if ($CompCode != "" && $CompCode != "All" ) {
            $this->db->where('compaccBankPayment.CompanyCode', $CompCode);
            $this->db->where('compaccBankReceipt.CompanyCode', $CompCode);
        } else {
            $this->db->where('compaccBankPayment.CompanyCode',$sess_company);
            $this->db->where('compaccBankReceipt.CompanyCode',$sess_company);
        }
        //========end where sesuai Company========================//
        
        //=====================cek data tanggal jika tanggal awal lebih dari tanggal akhir=============//
        if ((($DateFrom != '') && ($DateTo != ''))) {
            if ($DateFrom <= $DateTo) {
                $this->db->where('at_cashdeposit.CDepDate >=', date("Y-m-d", strtotime($DateFrom))); //change
                $this->db->where('at_cashdeposit.CDepDate <=', date("Y-m-d", strtotime($DateTo))); //change
            }
        }
        //=====================END cek data tanggal jika tanggal awal lebih dari tanggal akhir=============//
        
        $this->db->group_by("at_cashdeposit.CDepNumber");
        /* ==============================================SHORTING TABLE========================================================== */
        if($txtTable=="" && $txtField==""){
            $this->db->order_by($this->tbl_cdeposit . '.CDepNumber', 'ASC');
        }else if($txtTable=="acBankPayment" || $txtTable=="acBankReceipt"){
            $this->db->order_by($txtTable . '.'.$txtField, $typeShorting);
        }
        else{
            $this->db->order_by($this->$txtTable . '.'.$txtField, $typeShorting);
        }
        /* ==============================================END SHORTING TABLE========================================================== */        
        
        $this->db->limit($Pages['limit'], $limitStart);
        $query = $this->db->get();
        //echo "<hr>".$this->db->last_query()."<hr>";
        $result = $query->result();
        return $result;
    }
    function get_all_deposit() {
        $CompCode       = ($this->input->post('cmb_company')!=null)?$this->input->post('cmb_company'):'';
        $BranchCode     = ($this->input->post('cmb_branch')!=null)?$this->input->post('cmb_branch'):'';
        $BranchDepFrom  = ($this->input->post('branchDepositFrom')!=null)?$this->input->post('branchDepositFrom'):'';
        $BranchDepTo    = ($this->input->post('branchDepositTo')!=null)?$this->input->post('branchDepositTo'):'';
        $DateFrom       = ($this->input->post('txt_DateFrom')!=null)?$this->input->post('txt_DateFrom'):'';
        $DateTo         = ($this->input->post('txt_DateTo')!=null)?$this->input->post('txt_DateTo'):'';
        $DepositFrom    = ($this->input->post('cmb_deposit_from')!=null)?$this->input->post('cmb_deposit_from'):'';
        $DepositTo      = ($this->input->post('cmb_deposit_to')!=null)?$this->input->post('cmb_deposit_to'):'';
        $TransStatus    = ($this->input->post('chk_TransStatus')!=null)?$this->input->post('chk_TransStatus'):'';
        $txtTable       = ($this->input->post('txtTable')!=null)?$this->input->post('txtTable'):'';
        $txtField       = ($this->input->post('txtField')!=null)?$this->input->post('txtField'):'';
        $typeShorting   = ($this->input->post('typeShorting')!=null)?$this->input->post('typeShorting'):'';
        
        $PreData        = ($this->input->post('predata')!=[null])?$this->input->post('predata'):'';
        /* =================================END post dari find di kiri==================================================== */
           
        /* ==========================================by session======================================================= */
        $sess_company       = $this->session->userdata('company');
        $sess_branch        = $this->session->userdata('branch');
        $sess_branch_status = $this->session->userdata('branch_status');
        /* =========================================================================================================== */
    
        $this->db->select("distinct(at_cashdeposit.CDepNumber)CDepNumber"
                . ", at_cashdeposit.CDepDate"
                . ", acBankPayment.AccountName as BankPaymentName"
                . ", acBankReceipt.AccountName as BankReceiptName"
                . ", if(at_cashdeposit.CurrencyCode_to <> 'IDR', at_cashdeposit.CDepTotalAmount , concat('-')) as notIDR"
                . ", if(at_cashdeposit.CurrencyCode_to <> 'IDR', at_cashdeposit.CurrencyRate,concat('-')) as CurrencyRate"
                . ", if(at_cashdeposit.CurrencyCode_to <> 'IDR',at_cashdeposit.CDepTotalAmount * at_cashdeposit.CurrencyRate, at_cashdeposit.CDepTotalAmount ) as CDepTotalAmount"
                . ", at_cashdeposit.TransStatus"
                . ", am_branch.BranchName");
        $this->db->from('at_cashdeposit');
        $this->db->join('am_accountgl as acBankPayment', 'at_cashdeposit.BankPayment=acBankPayment.AccountNumber');
        $this->db->join('am_accountgl as acBankReceipt', 'at_cashdeposit.BankReceipt=acBankReceipt.AccountNumber');
        $this->db->join('am_branch', 'at_cashdeposit.BranchCode=am_branch.BranchCode');
        $this->db->join('at_compaccountgl as compaccBankPayment', 'at_cashdeposit.BankPayment=compaccBankPayment.AccountNumber');
        $this->db->join('at_compaccountgl as compaccBankReceipt', 'at_cashdeposit.BankReceipt=compaccBankReceipt.AccountNumber');
        $this->db->where_in('compaccBankPayment.TransStatus', 2);
        $this->db->where_in('compaccBankReceipt.TransStatus', 2);

        $this->db->where_in('at_cashdeposit.CDepNumber', $PreData);
        /* ========================jika kondisi tidak sama dengan kosong di find lakukan proses di if================= */
        if ($DepositFrom != '' && $DepositFrom!='All') {
            $this->db->where_in('at_cashdeposit.BankPayment', $DepositFrom);
        }
        if ($DepositTo != '' && $DepositTo!='All') {
            $this->db->where_in('at_cashdeposit.BankReceipt', $DepositTo);
        }
        if ($TransStatus != '') {
            $this->db->where_in('at_cashdeposit.TransStatus', $TransStatus);
        } else {
            /*default adalah 2,6. Finished dan Need Approval*/
            $this->db->where_in('at_cashdeposit.TransStatus', array(2, 6));
        }

        /* ===================================find by branch code========================================================= */
        if ($BranchCode != "" && $BranchCode != "All" ) {
            $this->db->where('at_cashdeposit.BranchCode', $BranchCode);
//            $this->db->where('compaccBankPayment.BranchCode', $BranchCode);
//            $this->db->where('compaccBankReceipt.BranchCode', $BranchCode);
        } 
        /* ==================================end find by branch code===================================================== */
        
        /* ===================================find by branch DepositFrom code========================================================= */
        if ($BranchDepFrom != "" && $BranchDepFrom != "All" ) {
            $this->db->where('at_cashdeposit.BranchCode', $BranchDepFrom);
            $this->db->where('compaccBankPayment.BranchCode', $BranchDepFrom);
//            $this->db->where('compaccBankReceipt.BranchCode', $BranchDepFrom);
        } 
        /* ==================================end find by branch DepositFrom code===================================================== */
        /* ===================================find by branch DepositTo code========================================================= */
        if ($BranchDepTo != "" && $BranchDepTo != "All" ) {
            $this->db->where('at_cashdeposit.BranchCode', $BranchDepTo);
//            $this->db->where('compaccBankPayment.BranchCode', $BranchDepTo);
            $this->db->where('compaccBankReceipt.BranchCode', $BranchDepTo);
        } 
        /* ==================================end find by branch DepositTo code===================================================== */
        //========where sesuai Company========================//
        if ($CompCode != "" && $CompCode != "All" ) {
            $this->db->where('compaccBankPayment.CompanyCode', $CompCode);
            $this->db->where('compaccBankReceipt.CompanyCode', $CompCode);
        } else {
            $this->db->where('compaccBankPayment.CompanyCode',$sess_company);
            $this->db->where('compaccBankReceipt.CompanyCode',$sess_company);
        }
        //========end where sesuai Company========================//
        
        //=====================cek data tanggal jika tanggal awal lebih dari tanggal akhir=============//
        if ((($DateFrom != '') && ($DateTo != ''))) {
            if ($DateFrom <= $DateTo) {
                $this->db->where('at_cashdeposit.CDepDate >=', date("Y-m-d", strtotime($DateFrom))); //change
                $this->db->where('at_cashdeposit.CDepDate <=', date("Y-m-d", strtotime($DateTo))); //change
            }
        }
        //=====================END cek data tanggal jika tanggal awal lebih dari tanggal akhir=============//
        
        /* ==============================================SHORTING TABLE========================================================== */
        if($txtTable=="" && $txtField==""){
            $this->db->order_by($this->tbl_cdeposit . '.CDepNumber', 'ASC');
        }else if($txtTable=="acBankPayment" || $txtTable=="acBankReceipt"){
            $this->db->order_by($txtTable . '.'.$txtField, $typeShorting);
        }
        else{
            $this->db->order_by($this->$txtTable . '.'.$txtField, $typeShorting);
        }
        /* ==============================================END SHORTING TABLE========================================================== */        
        
        $query = $this->db->get();
        //echo "<hr>".$this->db->last_query()."<hr>";
        $result = $query->result();
        //$data = array('datefrom'=>$DateFrom,'dateend'=>$DateTo,"data"=>$result);
        return $result;
    }
    
    function get_deposit_from($CompanyCode,$BranchCode){
        $this->db->select('at_compaccountgl.CompanyCode, 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_accountsubtype','am_accountgl.AccountSubTypeID=am_accountsubtype.AccountSubTypeID');
        if($CompanyCode!="All" && $CompanyCode!=""){
            $this->db->where($this->account.'.CompanyCode', $CompanyCode);
        }
        if($BranchCode!="All" && $BranchCode!=""){
            $this->db->where($this->account.'.BranchCode', $BranchCode);
        }
        $this->db->where($this->account.'.TransStatus', 2);
        $this->db->where('am_accountgl.AccountChild', 1);
        $this->db->where($this->subtype.'.AccountTypeName', 'Petty Cash');
        
        $this->db->order_by($this->account.'.BranchCode','ASC');
        
        $query = $this->db->get();
        $result=$query->result();
        return json_encode($result);
    }
    
    function get_deposit_to($CompanyCode,$BranchCode){
        $this->db->select('at_compaccountgl.CompanyCode, 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_accountsubtype','am_accountgl.AccountSubTypeID=am_accountsubtype.AccountSubTypeID');
        if($CompanyCode!="All" && $CompanyCode!=""){
            $this->db->where($this->account.'.CompanyCode', $CompanyCode);
        }
        if($BranchCode!="All" && $BranchCode!=""){
            $this->db->where($this->account.'.BranchCode', $BranchCode);
        }
        $this->db->where($this->account.'.TransStatus', 2);
        $this->db->where('am_accountgl.AccountChild', 1);
        $this->db->where($this->subtype.'.AccountTypeName', 'Bank');
        
        $this->db->order_by($this->account.'.BranchCode','ASC');
        
        $query = $this->db->get();
        $result=$query->result();
        return json_encode($result);
    }
    
    function pre_get_all_sweep_bank() {
        $CompCode       = ($this->input->post('cmb_company')!=null)?$this->input->post('cmb_company'):'';
        $BranchCode     = ($this->input->post('cmb_branch')!=null)?$this->input->post('cmb_branch'):'';
        $BranchBankTo   = ($this->input->post('branchSweepBankTo')!=null)?$this->input->post('branchSweepBankTo'):'';
        $DateFrom       = ($this->input->post('txt_DateFrom')!=null)?$this->input->post('txt_DateFrom'):'';
        $DateTo         = ($this->input->post('txt_DateTo')!=null)?$this->input->post('txt_DateTo'):'';
        $SweepBankTo    = ($this->input->post('cmb_sweep_bank_to')!=null)?$this->input->post('cmb_sweep_bank_to'):'';
        $TransStatus    = ($this->input->post('chk_TransStatus')!=null)?$this->input->post('chk_TransStatus'):'';
        $txtTable       = ($this->input->post('txtTable')!=null)?$this->input->post('txtTable'):'';
        $txtField       = ($this->input->post('txtField')!=null)?$this->input->post('txtField'):'';
        $typeShorting   = ($this->input->post('typeShorting')!=null)?$this->input->post('typeShorting'):'';
        
        $Pages          = ($this->input->post('pages')!=null)?$this->input->post('pages'):'';
        $limitStart     = ($Pages['limit'] * $Pages['pages']) - $Pages['limit'];
        /* =================================END post dari find di kiri==================================================== */
           
        /* ==========================================by session======================================================= */
        $sess_company       = $this->session->userdata('company');
        $sess_branch        = $this->session->userdata('branch');
        $sess_branch_status = $this->session->userdata('branch_status');
        /* =========================================================================================================== */
    
        $this->db->select('at_banksweep.BSweepNumber as code');
        $this->db->from('at_banksweep');
        $this->db->join('am_accountgl as acBankTransfer', 'at_banksweep.BankTransfer = acBankTransfer.AccountNumber');
        $this->db->join('am_accountgl as acBankReceipt', 'at_banksweep.BankReceipt = acBankReceipt.AccountNumber');
        $this->db->join('am_branch', 'at_banksweep.BranchCode = am_branch.BranchCode');
        $this->db->join('at_compaccountgl as compaccBankTransfer', 'at_banksweep.BankTransfer = compaccBankTransfer.AccountNumber');
        $this->db->join('at_compaccountgl as compaccBankReceipt', 'at_banksweep.BankReceipt = compaccBankReceipt.AccountNumber');
        
        $this->db->where_in('compaccBankTransfer.TransStatus', 2);
        $this->db->where_in('compaccBankReceipt.TransStatus', 2);
        /* ========================jika kondisi tidak sama dengan kosong di find lakukan proses di if================= */
        
        if ($SweepBankTo != '' && $SweepBankTo!='All') {
            $this->db->where_in('at_banksweep.BankReceipt', $SweepBankTo);
        }
        
        if ($TransStatus != '') {
            $this->db->where_in('at_banksweep.TransStatus', $TransStatus);
        } else {
            /*default adalah 2,6. Finished dan Need Approval*/
            $this->db->where_in('at_banksweep.TransStatus', array(2, 6));
        }

        /* ===================================find by branch code========================================================= */
        if ($BranchCode != "" && $BranchCode != "All" ) {
            $this->db->where('at_banksweep.BranchCode', $BranchCode);
        } 
        /* ==================================end find by branch code===================================================== */
        
        /* ===================================find by branch DepositTo code========================================================= */
//        if ($BranchSBankTo != "" && $BranchSBankTo != "All" ) {
//            $this->db->where('at_cashdeposit.BranchCode', $BranchDepTo);
//            $this->db->where('compaccBankReceipt.BranchCode', $BranchDepTo);
//        } 
        /* ==================================end find by branch DepositTo code===================================================== */
        //========where sesuai Company========================//
        if ($CompCode != "" && $CompCode != "All" ) {
            $this->db->where('compaccBankTransfer.CompanyCode', $CompCode);
            $this->db->where('compaccBankReceipt.CompanyCode', $CompCode);
        } else {
            $this->db->where('compaccBankTransfer.CompanyCode',$sess_company);
            $this->db->where('compaccBankReceipt.CompanyCode',$sess_company);
        }
        //========end where sesuai Company========================//
        
        //=====================cek data tanggal jika tanggal awal lebih dari tanggal akhir=============//
        if ((($DateFrom != '') && ($DateTo != ''))) {
            if ($DateFrom <= $DateTo) {
                $this->db->where('at_banksweep.BSweepDate >=', date("Y-m-d", strtotime($DateFrom))); //change
                $this->db->where('at_banksweep.BSweepDate <=', date("Y-m-d", strtotime($DateTo))); //change
            }
        }
        //=====================END cek data tanggal jika tanggal awal lebih dari tanggal akhir=============//
        
        $this->db->group_by("at_banksweep.BSweepNumber");
        /* ==============================================SHORTING TABLE========================================================== */
        if($txtTable=="" && $txtField==""){
            $this->db->order_by($this->tbl_bsweep . '.BSweepNumber', 'ASC');
        }else if($txtTable=="acBankTransfer" || $txtTable=="acBankReceipt"){
            $this->db->order_by($txtTable . '.'.$txtField, $typeShorting);
        }
        else{
            $this->db->order_by($this->$txtTable . '.'.$txtField, $typeShorting);
        }
        /* ==============================================END SHORTING TABLE========================================================== */        
        
        $this->db->limit($Pages['limit'], $limitStart);
        $query = $this->db->get();
        //echo "<hr>".$this->db->last_query()."<hr>";
        $result = $query->result();
        return $result;
    }
    function get_all_sweep_bank() {
        $CompCode       = ($this->input->post('cmb_company')!=null)?$this->input->post('cmb_company'):'';
        $BranchCode     = ($this->input->post('cmb_branch')!=null)?$this->input->post('cmb_branch'):'';
        $BranchBankTo   = ($this->input->post('branchSweepBankTo')!=null)?$this->input->post('branchSweepBankTo'):'';
        $DateFrom       = ($this->input->post('txt_DateFrom')!=null)?$this->input->post('txt_DateFrom'):'';
        $DateTo         = ($this->input->post('txt_DateTo')!=null)?$this->input->post('txt_DateTo'):'';
        $SweepBankTo    = ($this->input->post('cmb_sweep_bank_to')!=null)?$this->input->post('cmb_sweep_bank_to'):'';
        $TransStatus    = ($this->input->post('chk_TransStatus')!=null)?$this->input->post('chk_TransStatus'):'';
        $txtTable       = ($this->input->post('txtTable')!=null)?$this->input->post('txtTable'):'';
        $txtField       = ($this->input->post('txtField')!=null)?$this->input->post('txtField'):'';
        $typeShorting   = ($this->input->post('typeShorting')!=null)?$this->input->post('typeShorting'):'';
        
        $PreData        = ($this->input->post('predata')!=[null])?$this->input->post('predata'):'';
        /* =================================END post dari find di kiri==================================================== */
           
        /* ==========================================by session======================================================= */
        $sess_company       = $this->session->userdata('company');
        $sess_branch        = $this->session->userdata('branch');
        $sess_branch_status = $this->session->userdata('branch_status');
        /* =========================================================================================================== */
    
        $this->db->select('distinct(at_banksweep.BSweepNumber)BSweepNumber'
                . ', at_banksweep.BSweepDate'
                . ', acBankTransfer.AccountName as BankTransferName'
                . ', acBankReceipt.AccountName as BankReceiptName'
                . ', at_banksweep.BSweepAmount'
                . ', at_banksweep.TransStatus'
                . ', am_branch.BranchName');
        
        $this->db->from('at_banksweep');
        $this->db->join('am_accountgl as acBankTransfer', 'at_banksweep.BankTransfer = acBankTransfer.AccountNumber');
        $this->db->join('am_accountgl as acBankReceipt', 'at_banksweep.BankReceipt = acBankReceipt.AccountNumber');
        $this->db->join('am_branch', 'at_banksweep.BranchCode = am_branch.BranchCode');
        $this->db->join('at_compaccountgl as compaccBankTransfer', 'at_banksweep.BankTransfer = compaccBankTransfer.AccountNumber');
        $this->db->join('at_compaccountgl as compaccBankReceipt', 'at_banksweep.BankReceipt = compaccBankReceipt.AccountNumber');
        
        $this->db->where_in('compaccBankTransfer.TransStatus', 2);
        $this->db->where_in('compaccBankReceipt.TransStatus', 2);
        $this->db->where_in('at_banksweep.BSweepNumber', $PreData);
        /* ========================jika kondisi tidak sama dengan kosong di find lakukan proses di if================= */
        
        if ($SweepBankTo != '' && $SweepBankTo!='All') {
            $this->db->where_in('at_banksweep.BankReceipt', $SweepBankTo);
        }
        
        if ($TransStatus != '') {
            $this->db->where_in('at_banksweep.TransStatus', $TransStatus);
        } else {
            /*default adalah 2,6. Finished dan Need Approval*/
            $this->db->where_in('at_banksweep.TransStatus', array(2, 6));
        }

        /* ===================================find by branch code========================================================= */
        if ($BranchCode != "" && $BranchCode != "All" ) {
            $this->db->where('at_banksweep.BranchCode', $BranchCode);
        } 
        /* ==================================end find by branch code===================================================== */
        
        /* ===================================find by branch DepositTo code========================================================= */
//        if ($BranchSBankTo != "" && $BranchSBankTo != "All" ) {
//            $this->db->where('at_cashdeposit.BranchCode', $BranchDepTo);
//            $this->db->where('compaccBankReceipt.BranchCode', $BranchDepTo);
//        } 
        /* ==================================end find by branch DepositTo code===================================================== */
        //========where sesuai Company========================//
        if ($CompCode != "" && $CompCode != "All" ) {
            $this->db->where('compaccBankTransfer.CompanyCode', $CompCode);
            $this->db->where('compaccBankReceipt.CompanyCode', $CompCode);
        } else {
            $this->db->where('compaccBankTransfer.CompanyCode',$sess_company);
            $this->db->where('compaccBankReceipt.CompanyCode',$sess_company);
        }
        //========end where sesuai Company========================//
        
        //=====================cek data tanggal jika tanggal awal lebih dari tanggal akhir=============//
        if ((($DateFrom != '') && ($DateTo != ''))) {
            if ($DateFrom <= $DateTo) {
                $this->db->where('at_banksweep.BSweepDate >=', date("Y-m-d", strtotime($DateFrom))); //change
                $this->db->where('at_banksweep.BSweepDate <=', date("Y-m-d", strtotime($DateTo))); //change
            }
        }
        //=====================END cek data tanggal jika tanggal awal lebih dari tanggal akhir=============//
        
        /* ==============================================SHORTING TABLE========================================================== */
        if($txtTable=="" && $txtField==""){
            $this->db->order_by($this->tbl_bsweep . '.BSweepNumber', 'ASC');
        }else if($txtTable=="acBankTransfer" || $txtTable=="acBankReceipt"){
            $this->db->order_by($txtTable . '.'.$txtField, $typeShorting);
        }
        else{
            $this->db->order_by($this->$txtTable . '.'.$txtField, $typeShorting);
        }
        /* ==============================================END SHORTING TABLE========================================================== */        
        
        $query = $this->db->get();
        //echo "<hr>".$this->db->last_query()."<hr>";
        $result = $query->result();
        return $result;
    }
    
    function get_sweep_bank_to($CompanyCode){
        $this->db->select('am_accountgl.AccountNumber, am_accountgl.AccountName, am_accountgl.CurrencyCode,am_branch.BranchCode');
        $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);
        $this->db->where('at_compaccountgl.GLSaldoYears', date('Y'));	
        $this->db->where('am_branch.BranchStatus', 0);

        $this->db->where('am_accountgl.CurrencyCode', "IDR");
		$this->db->where('am_accountgl.AccountSubTypeID', 7);
        $this->db->where('am_accountgl.AccountChild', 1);
        $this->db->where('am_accountgl.TransStatus', 2);

        $query = $this->db->get();

        $result=$query->result();
        $result[]=array("AccountNumber"=>"All", "AccountName"=>"All", "CurrencyCode"=>"All", "BranchCode"=>"All");
        return json_encode($result);
    }
    
    function pre_get_all_afiliasi() {
        $CompCode       = ($this->input->post('cmb_company')!=null)?$this->input->post('cmb_company'):'';
        $DateFrom       = ($this->input->post('txt_DateFrom')!=null)?$this->input->post('txt_DateFrom'):'';
        $DateTo         = ($this->input->post('txt_DateTo')!=null)?$this->input->post('txt_DateTo'):'';
        $TransStatus    = ($this->input->post('chk_TransStatus')!=null)?$this->input->post('chk_TransStatus'):'';
        $txtTable       = ($this->input->post('txtTable')!=null)?$this->input->post('txtTable'):'';
        $txtField       = ($this->input->post('txtField')!=null)?$this->input->post('txtField'):'';
        $typeShorting   = ($this->input->post('typeShorting')!=null)?$this->input->post('typeShorting'):'';
        $lenCompCode    = strlen($CompCode);
        
        $Pages          = ($this->input->post('pages')!=null)?$this->input->post('pages'):'';
        $limitStart     = ($Pages['limit'] * $Pages['pages']) - $Pages['limit'];
        /* =================================END post dari find di kiri==================================================== */
           
        /* ==========================================by session======================================================= */
        $sess_company       = $this->session->userdata('company');
        $sess_branch        = $this->session->userdata('branch');
        $sess_branch_status = $this->session->userdata('branch_status');
        /* =========================================================================================================== */

        $this->db->select(' at_afiliasidebt.LinkNumber as code');
        
        $this->db->from('at_afiliasidebt');        
        $this->db->join('at_afiliasiinstallment', ' at_afiliasiinstallment.ADebtNumber = at_afiliasidebt.ADebtNumber and SUBSTRING(at_afiliasiinstallment.AInsNumber,1,LENGTH(at_afiliasiinstallment.CompTransCode))<>at_afiliasiinstallment.CompTransCode and at_afiliasiinstallment.TransStatus=2 ',"left");
        $this->db->join('am_accountgl as accTransferPay', 'at_afiliasiinstallment.BankTransfer = accTransferPay.AccountNumber',"left");
        $this->db->join('am_accountgl as accReceiptPay', 'at_afiliasiinstallment.BankReceipt = accReceiptPay.AccountNumber',"left"); 
        $this->db->join('am_company as compReceiptPay', 'at_afiliasiinstallment.CompRecpCode = compReceiptPay.CompanyCode',"left"); 
        $this->db->join('am_branch', 'at_afiliasidebt.BranchCode = am_branch.BranchCode',"inner");
        $this->db->join('am_accountgl as accTransfer', 'at_afiliasidebt.BankTransfer = accTransfer.AccountNumber',"inner");
        $this->db->join('am_accountgl as accReceipt', 'at_afiliasidebt.BankReceipt = accReceipt.AccountNumber',"inner");
        $this->db->join('am_company as compReceipt', 'at_afiliasidebt.CompRecpCode = compReceipt.CompanyCode',"inner");    
        $this->db->where("SUBSTRING(at_afiliasidebt.ADebtNumber,1,LENGTH(at_afiliasidebt.CompRecpCode))=at_afiliasidebt.CompRecpCode", null); 
        //$this->db->where("at_afiliasiinstallment.TransStatus", "2");                    
        /* ========================jika kondisi tidak sama dengan kosong di find lakukan proses di if================= */
        
        //========where sesuai Company========================//
        if ($CompCode != "" && $CompCode != "All" ) {
            $this->db->where('at_afiliasidebt.CompTransCode', $CompCode);
        } 
        //========end where sesuai Company========================//
        
        if ($TransStatus != '') {
            $this->db->where_in('at_afiliasidebt.TransStatus', $TransStatus);
        } else {
            /*default adalah 2,6. Finished dan Need Approval*/
            $this->db->where_in('at_afiliasidebt.TransStatus', array(2, 6));
        }
        
        //=====================cek data tanggal jika tanggal awal lebih dari tanggal akhir=============//
        if ((($DateFrom != '') && ($DateTo != ''))) {
            if ($DateFrom <= $DateTo) {
                $this->db->where('at_afiliasidebt.ADebtDate >=', date("Y-m-d", strtotime($DateFrom))); //change
                $this->db->where('at_afiliasidebt.ADebtDate <=', date("Y-m-d", strtotime($DateTo))); //change
            }
        }
        //=====================END cek data tanggal jika tanggal awal lebih dari tanggal akhir=============//
        
        $this->db->group_by("at_afiliasidebt.LinkNumber");
        /* ==============================================SHORTING TABLE========================================================== */
        if($txtTable=="" && $txtField==""){
            $this->db->order_by($this->tbl_afiliasiDebt . '.BranchCode', '');
            $this->db->order_by('at_afiliasiinstallment.AInsNumber', 'ASC');
            $this->db->order_by($this->tbl_afiliasiDebt . '.LinkNumber', '');
        }else{
            $this->db->order_by($this->$txtTable . '.'.$txtField, $typeShorting);
        }
        /* ==============================================END SHORTING TABLE========================================================== */        
        
        $this->db->limit($Pages['limit'], $limitStart);
        $query = $this->db->get();
        //echo "<hr>".$this->db->last_query()."<hr>";
        $result = $query->result();
        //$data = array('datefrom'=>$DateFrom,'dateend'=>$DateTo,"data"=>$result);
        return $result;
    }
    function get_all_afiliasi() {
        $CompCode       = ($this->input->post('cmb_company')!=null)?$this->input->post('cmb_company'):'';
        $DateFrom       = ($this->input->post('txt_DateFrom')!=null)?$this->input->post('txt_DateFrom'):'';
        $DateTo         = ($this->input->post('txt_DateTo')!=null)?$this->input->post('txt_DateTo'):'';
        $TransStatus    = ($this->input->post('chk_TransStatus')!=null)?$this->input->post('chk_TransStatus'):'';
        $txtTable       = ($this->input->post('txtTable')!=null)?$this->input->post('txtTable'):'';
        $txtField       = ($this->input->post('txtField')!=null)?$this->input->post('txtField'):'';
        $typeShorting   = ($this->input->post('typeShorting')!=null)?$this->input->post('typeShorting'):'';
        $lenCompCode    = strlen($CompCode);
        
        $PreData        = ($this->input->post('predata')!=[null])?$this->input->post('predata'):'';
        /* =================================END post dari find di kiri==================================================== */
           
        /* ==========================================by session======================================================= */
        $sess_company       = $this->session->userdata('company');
        $sess_branch        = $this->session->userdata('branch');
        $sess_branch_status = $this->session->userdata('branch_status');
        /* =========================================================================================================== */

        $this->db->select(' distinct(at_afiliasidebt.LinkNumber) LinkNumber_Debt'
                . ', at_afiliasidebt.ADebtNumber    as ADebtNumber_Debt'
                . ', at_afiliasidebt.ADebtDate      as ADebtDate_Debt'
                . ', at_afiliasidebt.BranchCode     as BranchCode_Debt'
                . ', am_branch.BranchName           as BranchName_Debt'
                . ', at_afiliasidebt.CompTransCode  as CompTransCode_Debt'      //company pemberi pinjaman
                . ', accTransfer.AccountName        as AccountNameTrans_Debt'   //nama bank transfer
                . ', at_afiliasidebt.CompRecpCode   as CompRecpCode_Debt'       //kode company pemberi pinjaman
                . ', compReceipt.CompanyName        as CompanyNameRecp_Debt'    //nama company penerima pinjaman
                . ', accReceipt.AccountName         as AccountNameRecp_Dept'  
                . ', at_afiliasidebt.ADebtAmount    as ADebtAmount_Debt'
                . ', at_afiliasidebt.TransStatus    as TransStatus_Debt'
                
                . ', at_afiliasiinstallment.AInsDate        as AInsDate_Pay'
                . ', at_afiliasiinstallment.AInsNumber      as AInsNumber_Pay'
                . ', at_afiliasiinstallment.ADebtNumber     as ADebtNumber_Pay'
                . ', at_afiliasiinstallment.BankTransfer    as BankTransfer_Pay'
                . ', accTransferPay.AccountName             as AccountNameTrans_Pay'
                . ', at_afiliasiinstallment.BankReceipt     as BankReceipt_Pay'
                . ', accReceiptPay.AccountName              as AccountNameRecp_Pay'
                . ', compReceiptPay.CompanyName             as CompanyNameRecp_Pay'  
                . ', at_afiliasiinstallment.APayAmount      as APayAmount_Pay'
                . ', at_afiliasiinstallment.TransStatus     as TransStatus_Pay'
                );
        
        $this->db->from('at_afiliasidebt');        
        $this->db->join('at_afiliasiinstallment', ' at_afiliasiinstallment.ADebtNumber = at_afiliasidebt.ADebtNumber and SUBSTRING(at_afiliasiinstallment.AInsNumber,1,LENGTH(at_afiliasiinstallment.CompTransCode))<>at_afiliasiinstallment.CompTransCode and at_afiliasiinstallment.TransStatus=2 ',"left");
        $this->db->join('am_accountgl as accTransferPay', 'at_afiliasiinstallment.BankTransfer = accTransferPay.AccountNumber',"left");
        $this->db->join('am_accountgl as accReceiptPay', 'at_afiliasiinstallment.BankReceipt = accReceiptPay.AccountNumber',"left"); 
        $this->db->join('am_company as compReceiptPay', 'at_afiliasiinstallment.CompRecpCode = compReceiptPay.CompanyCode',"left"); 
        $this->db->join('am_branch', 'at_afiliasidebt.BranchCode = am_branch.BranchCode',"inner");
        $this->db->join('am_accountgl as accTransfer', 'at_afiliasidebt.BankTransfer = accTransfer.AccountNumber',"inner");
        $this->db->join('am_accountgl as accReceipt', 'at_afiliasidebt.BankReceipt = accReceipt.AccountNumber',"inner");
        $this->db->join('am_company as compReceipt', 'at_afiliasidebt.CompRecpCode = compReceipt.CompanyCode',"inner");    
        $this->db->where("SUBSTRING(at_afiliasidebt.ADebtNumber,1,LENGTH(at_afiliasidebt.CompRecpCode))=at_afiliasidebt.CompRecpCode", null); 
        
        $this->db->where_in('at_afiliasidebt.LinkNumber', $PreData);
        //$this->db->where("at_afiliasiinstallment.TransStatus", "2");                    
        /* ========================jika kondisi tidak sama dengan kosong di find lakukan proses di if================= */
        
        //========where sesuai Company========================//
        if ($CompCode != "" && $CompCode != "All" ) {
            $this->db->where('at_afiliasidebt.CompTransCode', $CompCode);
        } 
        //========end where sesuai Company========================//
        
        if ($TransStatus != '') {
            $this->db->where_in('at_afiliasidebt.TransStatus', $TransStatus);
        } else {
            /*default adalah 2,6. Finished dan Need Approval*/
            $this->db->where_in('at_afiliasidebt.TransStatus', array(2, 6));
        }
        
        //=====================cek data tanggal jika tanggal awal lebih dari tanggal akhir=============//
        if ((($DateFrom != '') && ($DateTo != ''))) {
            if ($DateFrom <= $DateTo) {
                $this->db->where('at_afiliasidebt.ADebtDate >=', date("Y-m-d", strtotime($DateFrom))); //change
                $this->db->where('at_afiliasidebt.ADebtDate <=', date("Y-m-d", strtotime($DateTo))); //change
            }
        }
        //=====================END cek data tanggal jika tanggal awal lebih dari tanggal akhir=============//
        
        /* ==============================================SHORTING TABLE========================================================== */
        
        if($txtTable=="" && $txtField==""){
            $this->db->order_by($this->tbl_afiliasiDebt . '.BranchCode', '');
            $this->db->order_by('at_afiliasiinstallment.AInsNumber', 'ASC');
            $this->db->order_by($this->tbl_afiliasiDebt . '.LinkNumber', '');
        }
//        else if($txtTable=="acBankTransfer" || $txtTable=="acBankReceipt"){
//            $this->db->order_by($txtTable . '.'.$txtField, $typeShorting);
//        }
        else{
            //$this->db->order_by($this->tbl_afiliasiDebt . '.BranchCode', 'ASC');
            //$this->db->order_by($this->tbl_afiliasiDebt . '.LinkNumber', '');
            $this->db->order_by($this->$txtTable . '.'.$txtField, $typeShorting);
        }
        /* ==============================================END SHORTING TABLE========================================================== */        
        
        $query = $this->db->get();
        //echo "<hr>".$this->db->last_query()."<hr>";
        $result = $query->result();
        return $result;
    }
            
    function pre_get_all_piutang_karyawan() {
        $CompCode       = ($this->input->post('cmb_company')!=null)?$this->input->post('cmb_company'):'';
        $BranchCode     = ($this->input->post('cmb_branch')!=null)?$this->input->post('cmb_branch'):'';
        $DateFrom       = ($this->input->post('txt_DateFrom')!=null)?$this->input->post('txt_DateFrom'):'';
        $DateTo         = ($this->input->post('txt_DateTo')!=null)?$this->input->post('txt_DateTo'):'';
        $TransStatus    = ($this->input->post('chk_TransStatus')!=null)?$this->input->post('chk_TransStatus'):'';
        $txtTable       = ($this->input->post('txtTable')!=null)?$this->input->post('txtTable'):'';
        $txtField       = ($this->input->post('txtField')!=null)?$this->input->post('txtField'):'';
        $typeShorting   = ($this->input->post('typeShorting')!=null)?$this->input->post('typeShorting'):'';
        
        $Pages          = ($this->input->post('pages')!=null)?$this->input->post('pages'):'';
        $limitStart     = ($Pages['limit'] * $Pages['pages']) - $Pages['limit'];
        /* =================================END post dari find di kiri==================================================== */
           
        /* ==========================================by session======================================================= */
        $sess_company       = $this->session->userdata('company');
        $sess_branch        = $this->session->userdata('branch');
        $sess_branch_status = $this->session->userdata('branch_status');
        /* =========================================================================================================== */

        $this->db->select("at_employeedebt.EmplRecpCode     as code");
        $this->db->from('at_employeedebt');        
        $this->db->join('at_employeeinstallment', 'at_employeeinstallment.EDebtNumber = at_employeedebt.EDebtNumber and at_employeeinstallment.TransStatus=2',"left");
        $this->db->join('am_branch', 'am_branch.BranchCode = at_employeedebt.BranchCode',"inner");
        $this->db->join('am_accountgl', 'am_accountgl.AccountNumber = at_employeedebt.BankTransfer',"inner");
        $this->db->join('am_employee', 'am_employee.EmployeeCode = at_employeedebt.EmplRecpCode',"inner");
       /* ========================jika kondisi tidak sama dengan kosong di find lakukan proses di if================= */
        
        //========where sesuai Company========================//
        if ($CompCode != "" && $CompCode != "All" ) {
            $this->db->where('at_employeedebt.CompTransCode', $CompCode);
        } 
        //========end where sesuai Company========================//
        
        if ($TransStatus != '') {
            $this->db->where_in('at_employeedebt.TransStatus', $TransStatus);
        } else {
            /*default adalah 2,6. Finished dan Need Approval*/
            $this->db->where_in('at_employeedebt.TransStatus', array(2, 6));
        }
        
        //=====================cek data tanggal jika tanggal awal lebih dari tanggal akhir=============//
        if ((($DateFrom != '') && ($DateTo != ''))) {
            if ($DateFrom <= $DateTo) {
                $this->db->where('at_employeedebt.EDebtDate >=', date("Y-m-d", strtotime($DateFrom))); //change
                $this->db->where('at_employeedebt.EDebtDate <=', date("Y-m-d", strtotime($DateTo))); //change
            }
        }
        //=====================END cek data tanggal jika tanggal awal lebih dari tanggal akhir=============//
        
        $this->db->group_by("at_employeedebt.EmplRecpCode");
        /* ==============================================SHORTING TABLE========================================================== */
        if($txtTable=="" && $txtField==""){
            $this->db->order_by($this->tbl_employeeDebt . '.EDebtNumber', 'ASC');
            $this->db->order_by($this->tbl_employeeInst . '.EInsNumber', 'ASC');
        }
        else if($txtTable=="accglTransPay" || $txtTable=="accglRecptPay" || $txtTable=="am_employee"){
            $this->db->order_by($txtTable . '.'.$txtField, $typeShorting);
        }
        else{
            $this->db->order_by($this->$txtTable . '.'.$txtField, $typeShorting);
        }
        /* ==============================================END SHORTING TABLE========================================================== */        
        $this->db->limit($Pages['limit'], $limitStart);
        
        $query = $this->db->get();
        //echo "<hr>".$this->db->last_query()."<hr>";
        $result = $query->result();
        return $result;
    }
    function get_all_piutang_karyawan() {
        $CompCode       = ($this->input->post('cmb_company')!=null)?$this->input->post('cmb_company'):'';
        $BranchCode     = ($this->input->post('cmb_branch')!=null)?$this->input->post('cmb_branch'):'';
        $DateFrom       = ($this->input->post('txt_DateFrom')!=null)?$this->input->post('txt_DateFrom'):'';
        $DateTo         = ($this->input->post('txt_DateTo')!=null)?$this->input->post('txt_DateTo'):'';
        $TransStatus    = ($this->input->post('chk_TransStatus')!=null)?$this->input->post('chk_TransStatus'):'';
        $txtTable       = ($this->input->post('txtTable')!=null)?$this->input->post('txtTable'):'';
        $txtField       = ($this->input->post('txtField')!=null)?$this->input->post('txtField'):'';
        $typeShorting   = ($this->input->post('typeShorting')!=null)?$this->input->post('typeShorting'):'';
        
        $PreData        = ($this->input->post('predata')!=[null])?$this->input->post('predata'):'';
        /* =================================END post dari find di kiri==================================================== */
           
        /* ==========================================by session======================================================= */
        $sess_company       = $this->session->userdata('company');
        $sess_branch        = $this->session->userdata('branch');
        $sess_branch_status = $this->session->userdata('branch_status');
        /* =========================================================================================================== */

        $this->db->select("at_employeedebt.BranchCode as BranchCode_Debt"
                . ", at_employeedebt.EDebtDate        as EDebtDate_Debt"
                . ", at_employeedebt.EDebtNumber      as EDebtNumber_Debt"
                . ", at_employeedebt.CompTransCode    as CompTransCode_Debt"
                . ", at_employeedebt.BankTransfer     as BankTransfer_Debt"
                . ", at_employeedebt.EmplRecpCode     as EmplRecpCode_Debt"
                . ", at_employeedebt.BankReceipt      as BankReceipt_Debt"
                . ", at_employeedebt.EDebtAmount      as EDebtAmount_Debt"
                . ", at_employeedebt.TransStatus      as TransStatus_Debt"
                . ", am_branch.BranchName             as BranchName_Debt"
                . ", am_accountgl.AccountName         as BankTransferName_Debt"
                . ", am_employee.EmployeeName         as EmployeeName_Debt"
                
                . ", at_employeeinstallment.EInsDate        as EInsDate_Pay" 
                . ", at_employeeinstallment.EInsNumber      as EInsNumber_Pay" 
                . ", at_employeeinstallment.BankTransfer    as BankTransfer_Pay" 
                . ", at_employeeinstallment.BankReceipt     as BankReceipt_Pay" 
                . ", at_employeeinstallment.EInsAmount      as EInsAmount_Pay" 
                . ", at_employeeinstallment.TransStatus     as TransStatus_Pay"   
                . ", accglTransPay.AccountName              as BankTransferName_Pay"   
                . ", accglRecptPay.AccountName              as BankReceiptName_Pay"                
                );

        $this->db->from('at_employeedebt');        
        $this->db->join('at_employeeinstallment', 'at_employeeinstallment.EDebtNumber = at_employeedebt.EDebtNumber and at_employeeinstallment.TransStatus=2',"left");
        $this->db->join('am_branch', 'am_branch.BranchCode = at_employeedebt.BranchCode',"inner");
        $this->db->join('am_accountgl', 'am_accountgl.AccountNumber = at_employeedebt.BankTransfer',"inner");
        $this->db->join('am_employee', 'am_employee.EmployeeCode = at_employeedebt.EmplRecpCode',"inner");
        $this->db->join('am_accountgl as accglTransPay', 'accglTransPay.AccountNumber = at_employeeinstallment.BankTransfer',"left");
        $this->db->join('am_accountgl as accglRecptPay', 'accglRecptPay.AccountNumber = at_employeeinstallment.BankReceipt',"left");
        
        $this->db->where_in('at_employeedebt.EmplRecpCode', $PreData);
       /* ========================jika kondisi tidak sama dengan kosong di find lakukan proses di if================= */
        
        //========where sesuai Company========================//
        if ($CompCode != "" && $CompCode != "All" ) {
            $this->db->where('at_employeedebt.CompTransCode', $CompCode);
        } 
        //========end where sesuai Company========================//
        
        if ($TransStatus != '') {
            $this->db->where_in('at_employeedebt.TransStatus', $TransStatus);
        } else {
            /*default adalah 2,6. Finished dan Need Approval*/
            $this->db->where_in('at_employeedebt.TransStatus', array(2, 6));
        }
        
        //=====================cek data tanggal jika tanggal awal lebih dari tanggal akhir=============//
        if ((($DateFrom != '') && ($DateTo != ''))) {
            if ($DateFrom <= $DateTo) {
                $this->db->where('at_employeedebt.EDebtDate >=', date("Y-m-d", strtotime($DateFrom))); //change
                $this->db->where('at_employeedebt.EDebtDate <=', date("Y-m-d", strtotime($DateTo))); //change
            }
        }
        //=====================END cek data tanggal jika tanggal awal lebih dari tanggal akhir=============//
        
        /* ==============================================SHORTING TABLE========================================================== */
        
        if($txtTable=="" && $txtField==""){
            $this->db->order_by($this->tbl_employeeDebt . '.EDebtNumber', 'ASC');
            $this->db->order_by($this->tbl_employeeInst . '.EInsNumber', 'ASC');
        }
        else if($txtTable=="accglTransPay" || $txtTable=="accglRecptPay" || $txtTable=="am_employee"){
            $this->db->order_by($txtTable . '.'.$txtField, $typeShorting);
        }
        else{
            $this->db->order_by($this->$txtTable . '.'.$txtField, $typeShorting);
        }
        /* ==============================================END SHORTING TABLE========================================================== */        
        
        $query = $this->db->get();
        //echo "<hr>".$this->db->last_query()."<hr>";
        $result = $query->result();
        return $result;
    }
        
    function pre_get_all_transfer_to_petty_cash() {
        $CompCode       = ($this->input->post('cmb_company')!=null)?$this->input->post('cmb_company'):'';
        $BranchCode     = ($this->input->post('cmb_branch')!=null)?$this->input->post('cmb_branch'):'';
        $BranchPettyCashTo  = ($this->input->post('branchPettyCashTo')!=null)?$this->input->post('branchPettyCashTo'):'';
        $DateFrom       = ($this->input->post('txt_DateFrom')!=null)?$this->input->post('txt_DateFrom'):'';
        $DateTo         = ($this->input->post('txt_DateTo')!=null)?$this->input->post('txt_DateTo'):'';
        $PettyCashTo    = ($this->input->post('cmb_petty_cash_to')!=null)?$this->input->post('cmb_petty_cash_to'):'';
        $TransStatus    = ($this->input->post('chk_TransStatus')!=null)?$this->input->post('chk_TransStatus'):'';
        $txtTable       = ($this->input->post('txtTable')!=null)?$this->input->post('txtTable'):'';
        $txtField       = ($this->input->post('txtField')!=null)?$this->input->post('txtField'):'';
        $typeShorting   = ($this->input->post('typeShorting')!=null)?$this->input->post('typeShorting'):'';
        
        $Pages          = ($this->input->post('pages')!=null)?$this->input->post('pages'):'';
        $limitStart     = ($Pages['limit'] * $Pages['pages']) - $Pages['limit'];
        /* =================================END post dari find di kiri==================================================== */
           
        /* ==========================================by session======================================================= */
        $sess_company       = $this->session->userdata('company');
        $sess_branch        = $this->session->userdata('branch');
        $sess_branch_status = $this->session->userdata('branch_status');
        /* =========================================================================================================== */
    
        $this->db->select("at_transfercash.TCashNumber as code" );
        $this->db->from('at_transfercash');
        $this->db->join('am_accountgl as acBankTransfer', 'at_transfercash.BankTransfer=acBankTransfer.AccountNumber');
        $this->db->join('am_accountgl as acBankReceipt', 'at_transfercash.BankReceipt=acBankReceipt.AccountNumber');
        $this->db->join('am_branch', 'at_transfercash.BranchCode=am_branch.BranchCode');
        $this->db->join('at_compaccountgl as compaccBankPayment', 'at_transfercash.BankTransfer=compaccBankPayment.AccountNumber');
        $this->db->join('at_compaccountgl as compaccBankReceipt', 'at_transfercash.BankReceipt=compaccBankReceipt.AccountNumber');
        $this->db->where_in('compaccBankPayment.TransStatus', 2);
        $this->db->where_in('compaccBankReceipt.TransStatus', 2);
        /* ========================jika kondisi tidak sama dengan kosong di find lakukan proses di if================= */
        if ($PettyCashTo != '' && $PettyCashTo!='All') {
            $this->db->where_in('at_transfercash.BankReceipt', $PettyCashTo);
        }
        
        if ($TransStatus != '') {
            $this->db->where_in('at_transfercash.TransStatus', $TransStatus);
        } else {
            /*default adalah 2,6. Finished dan Need Approval*/
            $this->db->where_in('at_transfercash.TransStatus', array(2, 6));
        }

        /* ===================================find by branch code========================================================= */
        if ($BranchCode != "" && $BranchCode != "All" ) {
            $this->db->where('at_transfercash.BranchCode', $BranchCode);
//            $this->db->where('compaccBankPayment.BranchCode', $BranchCode);
//            $this->db->where('compaccBankReceipt.BranchCode', $BranchCode);
        } 
        /* ==================================end find by branch code===================================================== */
        
        /* ===================================find by branch transfercash petty to code========================================================= */
        if ($BranchPettyCashTo != "" && $BranchPettyCashTo != "All" ) {
            $this->db->where('at_transfercash.BranchCode', $BranchPettyCashTo);
            $this->db->where('compaccBankPayment.BranchCode', $BranchPettyCashTo);
        } 
        /* ==================================end find by branch transfercash petty to code===================================================== */
        
        //========where sesuai Company========================//
        if ($CompCode != "" && $CompCode != "All" ) {
            $this->db->where('compaccBankPayment.CompanyCode', $CompCode);
            $this->db->where('compaccBankReceipt.CompanyCode', $CompCode);
        } else {
            $this->db->where('compaccBankPayment.CompanyCode',$sess_company);
            $this->db->where('compaccBankReceipt.CompanyCode',$sess_company);
        }
        //========end where sesuai Company========================//
        
        //=====================cek data tanggal jika tanggal awal lebih dari tanggal akhir=============//
        if ((($DateFrom != '') && ($DateTo != ''))) {
            if ($DateFrom <= $DateTo) {
                $this->db->where('at_transfercash.TCashDate >=', date("Y-m-d", strtotime($DateFrom))); //change
                $this->db->where('at_transfercash.TCashDate <=', date("Y-m-d", strtotime($DateTo))); //change
            }
        }
        //=====================END cek data tanggal jika tanggal awal lebih dari tanggal akhir=============//
        
        $this->db->group_by("at_transfercash.TCashNumber");
        /* ==============================================SHORTING TABLE========================================================== */
        if($txtTable=="" && $txtField==""){
            $this->db->order_by($this->tbl_transfercash . '.TCashNumber', 'ASC');
        }else if($txtTable=="acBankPayment" || $txtTable=="acBankReceipt"){
            $this->db->order_by($txtTable . '.'.$txtField, $typeShorting);
        }
        else{
            $this->db->order_by($this->$txtTable . '.'.$txtField, $typeShorting);
        }
        /* ==============================================END SHORTING TABLE========================================================== */        
        $this->db->limit($Pages['limit'], $limitStart);
        
        $query = $this->db->get();
        //echo "<hr>".$this->db->last_query()."<hr>";
        $result = $query->result();
        return $result;
    }
    function get_all_transfer_to_petty_cash() {
        $CompCode       = ($this->input->post('cmb_company')!=null)?$this->input->post('cmb_company'):'';
        $BranchCode     = ($this->input->post('cmb_branch')!=null)?$this->input->post('cmb_branch'):'';
        $BranchPettyCashTo  = ($this->input->post('branchPettyCashTo')!=null)?$this->input->post('branchPettyCashTo'):'';
        $DateFrom       = ($this->input->post('txt_DateFrom')!=null)?$this->input->post('txt_DateFrom'):'';
        $DateTo         = ($this->input->post('txt_DateTo')!=null)?$this->input->post('txt_DateTo'):'';
        $PettyCashTo    = ($this->input->post('cmb_petty_cash_to')!=null)?$this->input->post('cmb_petty_cash_to'):'';
        $TransStatus    = ($this->input->post('chk_TransStatus')!=null)?$this->input->post('chk_TransStatus'):'';
        $txtTable       = ($this->input->post('txtTable')!=null)?$this->input->post('txtTable'):'';
        $txtField       = ($this->input->post('txtField')!=null)?$this->input->post('txtField'):'';
        $typeShorting   = ($this->input->post('typeShorting')!=null)?$this->input->post('typeShorting'):'';
        
        $PreData        = ($this->input->post('predata')!=[null])?$this->input->post('predata'):'';
        /* =================================END post dari find di kiri==================================================== */
           
        /* ==========================================by session======================================================= */
        $sess_company       = $this->session->userdata('company');
        $sess_branch        = $this->session->userdata('branch');
        $sess_branch_status = $this->session->userdata('branch_status');
        /* =========================================================================================================== */
    
        $this->db->select("distinct(at_transfercash.TCashNumber)TCashNumber"
                . ", at_transfercash.TCashDate"
                . ", at_transfercash.BranchCode"
                . ", acBankTransfer.AccountName as BankTransferName"
                . ", acBankReceipt.AccountName as BankReceiptName"
                . ", if(at_transfercash.CurrencyCode_to <> 'IDR', at_transfercash.TransAmount , concat('-')) as notIDR"
                . ", if(at_transfercash.CurrencyCode_to <> 'IDR', at_transfercash.CurrencyRate,concat('-')) as CurrencyRate"
                . ", if(at_transfercash.CurrencyCode_to <> 'IDR',at_transfercash.TransAmount * at_transfercash.CurrencyRate, at_transfercash.TransAmount ) as TransAmount"
                . ", at_transfercash.TransStatus"
                . ", am_branch.BranchName");
        $this->db->from('at_transfercash');
        $this->db->join('am_accountgl as acBankTransfer', 'at_transfercash.BankTransfer=acBankTransfer.AccountNumber');
        $this->db->join('am_accountgl as acBankReceipt', 'at_transfercash.BankReceipt=acBankReceipt.AccountNumber');
        $this->db->join('am_branch', 'at_transfercash.BranchCode=am_branch.BranchCode');
        $this->db->join('at_compaccountgl as compaccBankPayment', 'at_transfercash.BankTransfer=compaccBankPayment.AccountNumber');
        $this->db->join('at_compaccountgl as compaccBankReceipt', 'at_transfercash.BankReceipt=compaccBankReceipt.AccountNumber');
        $this->db->where_in('compaccBankPayment.TransStatus', 2);
        $this->db->where_in('compaccBankReceipt.TransStatus', 2);
        
        $this->db->where_in('at_transfercash.TCashNumber', $PreData);
        /* ========================jika kondisi tidak sama dengan kosong di find lakukan proses di if================= */
        if ($PettyCashTo != '' && $PettyCashTo!='All') {
            $this->db->where_in('at_transfercash.BankReceipt', $PettyCashTo);
        }
        
        if ($TransStatus != '') {
            $this->db->where_in('at_transfercash.TransStatus', $TransStatus);
        } else {
            /*default adalah 2,6. Finished dan Need Approval*/
            $this->db->where_in('at_transfercash.TransStatus', array(2, 6));
        }

        /* ===================================find by branch code========================================================= */
        if ($BranchCode != "" && $BranchCode != "All" ) {
            $this->db->where('at_transfercash.BranchCode', $BranchCode);
//            $this->db->where('compaccBankPayment.BranchCode', $BranchCode);
//            $this->db->where('compaccBankReceipt.BranchCode', $BranchCode);
        } 
        /* ==================================end find by branch code===================================================== */
        
        /* ===================================find by branch transfercash petty to code========================================================= */
        if ($BranchPettyCashTo != "" && $BranchPettyCashTo != "All" ) {
            $this->db->where('at_transfercash.BranchCode', $BranchPettyCashTo);
            $this->db->where('compaccBankPayment.BranchCode', $BranchPettyCashTo);
        } 
        /* ==================================end find by branch transfercash petty to code===================================================== */
        
        //========where sesuai Company========================//
        if ($CompCode != "" && $CompCode != "All" ) {
            $this->db->where('compaccBankPayment.CompanyCode', $CompCode);
            $this->db->where('compaccBankReceipt.CompanyCode', $CompCode);
        } else {
            $this->db->where('compaccBankPayment.CompanyCode',$sess_company);
            $this->db->where('compaccBankReceipt.CompanyCode',$sess_company);
        }
        //========end where sesuai Company========================//
        
        //=====================cek data tanggal jika tanggal awal lebih dari tanggal akhir=============//
        if ((($DateFrom != '') && ($DateTo != ''))) {
            if ($DateFrom <= $DateTo) {
                $this->db->where('at_transfercash.TCashDate >=', date("Y-m-d", strtotime($DateFrom))); //change
                $this->db->where('at_transfercash.TCashDate <=', date("Y-m-d", strtotime($DateTo))); //change
            }
        }
        //=====================END cek data tanggal jika tanggal awal lebih dari tanggal akhir=============//
        
        /* ==============================================SHORTING TABLE========================================================== */
        if($txtTable=="" && $txtField==""){
            $this->db->order_by($this->tbl_transfercash . '.TCashNumber', 'ASC');
        }else if($txtTable=="acBankPayment" || $txtTable=="acBankReceipt"){
            $this->db->order_by($txtTable . '.'.$txtField, $typeShorting);
        }
        else{
            $this->db->order_by($this->$txtTable . '.'.$txtField, $typeShorting);
        }
        /* ==============================================END SHORTING TABLE========================================================== */        
        
        $query = $this->db->get();
        //echo "<hr>".$this->db->last_query()."<hr>";
        $result = $query->result();
        //$data = array('datefrom'=>$DateFrom,'dateend'=>$DateTo,"data"=>$result);
        return $result;
    }
        
    function pre_get_all_inter_bank_transfer() {
        $CompCode       = ($this->input->post('cmb_company')!=null)?$this->input->post('cmb_company'):'';
        $BranchCode     = ($this->input->post('cmb_branch')!=null)?$this->input->post('cmb_branch'):'';
        $BranchIbankFrom= ($this->input->post('branchIbankFrom')!=null)?$this->input->post('branchIbankFrom'):'';
        $BranchIbankTo  = ($this->input->post('branchIbankTo')!=null)?$this->input->post('branchIbankTo'):'';
        $DateFrom       = ($this->input->post('txt_DateFrom')!=null)?$this->input->post('txt_DateFrom'):'';
        $DateTo         = ($this->input->post('txt_DateTo')!=null)?$this->input->post('txt_DateTo'):'';
        $IbankFrom      = ($this->input->post('cmb_ibank_from')!=null)?$this->input->post('cmb_ibank_from'):'';
        $IbankTo        = ($this->input->post('cmb_ibank_to')!=null)?$this->input->post('cmb_ibank_to'):'';
        $TransStatus    = ($this->input->post('chk_TransStatus')!=null)?$this->input->post('chk_TransStatus'):'';
        $txtTable       = ($this->input->post('txtTable')!=null)?$this->input->post('txtTable'):'';
        $txtField       = ($this->input->post('txtField')!=null)?$this->input->post('txtField'):'';
        $typeShorting   = ($this->input->post('typeShorting')!=null)?$this->input->post('typeShorting'):'';
        
        $Pages          = ($this->input->post('pages')!=null)?$this->input->post('pages'):'';
        $limitStart     = ($Pages['limit'] * $Pages['pages']) - $Pages['limit'];
        /* =================================END post dari find di kiri==================================================== */
           
        /* ==========================================by session======================================================= */
        $sess_company       = $this->session->userdata('company');
        $sess_branch        = $this->session->userdata('branch');
        $sess_branch_status = $this->session->userdata('branch_status');
        /* =========================================================================================================== */
    
        $this->db->select("at_interbanktransfer.IBankNumber as code");
        $this->db->from('at_interbanktransfer');
        $this->db->join('am_accountgl as acBankTransfer', 'at_interbanktransfer.BankTransfer=acBankTransfer.AccountNumber');
        $this->db->join('am_accountgl as acBankReceipt', 'at_interbanktransfer.BankReceipt=acBankReceipt.AccountNumber');
        $this->db->join('am_branch', 'at_interbanktransfer.BranchCode=am_branch.BranchCode');
        $this->db->join('at_compaccountgl as compaccBankTransfer', 'at_interbanktransfer.BankTransfer=compaccBankTransfer.AccountNumber');
        $this->db->join('at_compaccountgl as compaccBankReceipt', 'at_interbanktransfer.BankReceipt=compaccBankReceipt.AccountNumber');
        $this->db->where_in('compaccBankTransfer.TransStatus', 2);
        //$this->db->where_in('compaccBankReceipt.TransStatus', 2);
        /* ========================jika kondisi tidak sama dengan kosong di find lakukan proses di if================= */
        if ($IbankFrom != '' && $IbankFrom!='All') {
            $this->db->where_in('at_interbanktransfer.BankTransfer', $IbankFrom);
        }
        if ($IbankTo != '' && $IbankTo!='All') {
            $this->db->where_in('at_interbanktransfer.BankReceipt', $IbankTo);
        }
        if ($TransStatus != '') {
            $this->db->where_in('at_interbanktransfer.TransStatus', $TransStatus);
        } else {
            /*default adalah 2,6. Finished dan Need Approval*/
            $this->db->where_in('at_interbanktransfer.TransStatus', array(2, 6));
        }

        /* ===================================find by branch code========================================================= */
        if ($BranchCode != "" && $BranchCode != "All" ) {
            $this->db->where('at_interbanktransfer.BranchCode', $BranchCode);
        } 
        /* ==================================end find by branch code===================================================== */
        
        /* ===================================find by branch ibankfrom code========================================================= */
        if ($BranchIbankFrom != "" && $BranchIbankFrom != "All" ) {
            $this->db->where('at_interbanktransfer.BranchCode', $BranchIbankFrom);
            $this->db->where('compaccBankTransfer.BranchCode', $BranchIbankFrom);
        } 
        /* ==================================end find by branch ibankFrom code===================================================== */
        /* ===================================find by branch ibankTo code========================================================= */
        if ($BranchIbankTo != "" && $BranchIbankTo != "All" ) {
            $this->db->where('at_interbanktransfer.BranchCode', $BranchIbankTo);
            $this->db->where('compaccBankReceipt.BranchCode', $BranchIbankTo);
        } 
        /* ==================================end find by branch ibankTo code===================================================== */
        //========where sesuai Company========================//
        if ($CompCode != "" && $CompCode != "All" ) {
            $this->db->where('compaccBankTransfer.CompanyCode', $CompCode);
            $this->db->where('compaccBankReceipt.CompanyCode', $CompCode);
        } else {
            $this->db->where('compaccBankTransfer.CompanyCode',$sess_company);
            $this->db->where('compaccBankReceipt.CompanyCode',$sess_company);
        }
        //========end where sesuai Company========================//
        
        //=====================cek data tanggal jika tanggal awal lebih dari tanggal akhir=============//
        if ((($DateFrom != '') && ($DateTo != ''))) {
            if ($DateFrom <= $DateTo) {
                $this->db->where('at_interbanktransfer.IBankDate >=', date("Y-m-d", strtotime($DateFrom))); //change
                $this->db->where('at_interbanktransfer.IBankDate <=', date("Y-m-d", strtotime($DateTo))); //change
            }
        }
        //=====================END cek data tanggal jika tanggal awal lebih dari tanggal akhir=============//
        
        $this->db->group_by("at_interbanktransfer.IBankNumber");
        /* ==============================================SHORTING TABLE========================================================== */
        if($txtTable=="" && $txtField==""){
            $this->db->order_by($this->tbl_ibank . '.IBankNumber', 'ASC');
        }else if($txtTable=="acBankTransfer" || $txtTable=="acBankReceipt"){
            $this->db->order_by($txtTable . '.'.$txtField, $typeShorting);
        }
        else{
            $this->db->order_by($this->$txtTable . '.'.$txtField, $typeShorting);
        }
        /* ==============================================END SHORTING TABLE========================================================== */        
        
        $this->db->limit($Pages['limit'], $limitStart);
        $query = $this->db->get();
        //echo "<hr>".$this->db->last_query()."<hr>";
        $result = $query->result();
        return $result;
    }
    function get_all_inter_bank_transfer() {
        $CompCode       = ($this->input->post('cmb_company')!=null)?$this->input->post('cmb_company'):'';
        $BranchCode     = ($this->input->post('cmb_branch')!=null)?$this->input->post('cmb_branch'):'';
        $BranchIbankFrom= ($this->input->post('branchIbankFrom')!=null)?$this->input->post('branchIbankFrom'):'';
        $BranchIbankTo  = ($this->input->post('branchIbankTo')!=null)?$this->input->post('branchIbankTo'):'';
        $DateFrom       = ($this->input->post('txt_DateFrom')!=null)?$this->input->post('txt_DateFrom'):'';
        $DateTo         = ($this->input->post('txt_DateTo')!=null)?$this->input->post('txt_DateTo'):'';
        $IbankFrom      = ($this->input->post('cmb_ibank_from')!=null)?$this->input->post('cmb_ibank_from'):'';
        $IbankTo        = ($this->input->post('cmb_ibank_to')!=null)?$this->input->post('cmb_ibank_to'):'';
        $TransStatus    = ($this->input->post('chk_TransStatus')!=null)?$this->input->post('chk_TransStatus'):'';
        $txtTable       = ($this->input->post('txtTable')!=null)?$this->input->post('txtTable'):'';
        $txtField       = ($this->input->post('txtField')!=null)?$this->input->post('txtField'):'';
        $typeShorting   = ($this->input->post('typeShorting')!=null)?$this->input->post('typeShorting'):'';
        
        $PreData        = ($this->input->post('predata')!=[null])?$this->input->post('predata'):'';
        /* =================================END post dari find di kiri==================================================== */
           
        /* ==========================================by session======================================================= */
        $sess_company       = $this->session->userdata('company');
        $sess_branch        = $this->session->userdata('branch');
        $sess_branch_status = $this->session->userdata('branch_status');
        /* =========================================================================================================== */
    
        $this->db->select("distinct(at_interbanktransfer.IBankNumber)IBankNumber"
                . ", at_interbanktransfer.IBankDate"
                . ", at_interbanktransfer.BranchCode"
                . ", acBankTransfer.AccountName as BankTransferName"
                . ", acBankReceipt.AccountName as BankReceiptName"
                . ", if(at_interbanktransfer.CurrencyCode_to <> 'IDR', at_interbanktransfer.IBankAmount , concat('-')) as notIDR"
                . ", if(at_interbanktransfer.CurrencyCode_to <> 'IDR', at_interbanktransfer.CurrencyRate,concat('-')) as CurrencyRate"
                . ", if(at_interbanktransfer.CurrencyCode_to <> 'IDR',at_interbanktransfer.IBankAmount * at_interbanktransfer.CurrencyRate, at_interbanktransfer.IBankAmount ) as IBankAmount"
                . ", at_interbanktransfer.TransStatus"
                . ", am_branch.BranchName");
        $this->db->from('at_interbanktransfer');
        $this->db->join('am_accountgl as acBankTransfer', 'at_interbanktransfer.BankTransfer=acBankTransfer.AccountNumber');
        $this->db->join('am_accountgl as acBankReceipt', 'at_interbanktransfer.BankReceipt=acBankReceipt.AccountNumber');
        $this->db->join('am_branch', 'at_interbanktransfer.BranchCode=am_branch.BranchCode');
        $this->db->join('at_compaccountgl as compaccBankTransfer', 'at_interbanktransfer.BankTransfer=compaccBankTransfer.AccountNumber');
        $this->db->join('at_compaccountgl as compaccBankReceipt', 'at_interbanktransfer.BankReceipt=compaccBankReceipt.AccountNumber');
        $this->db->where_in('compaccBankTransfer.TransStatus', 2);
        $this->db->where_in('at_interbanktransfer.IBankNumber', $PreData);
        //$this->db->where_in('compaccBankReceipt.TransStatus', 2);
        /* ========================jika kondisi tidak sama dengan kosong di find lakukan proses di if================= */
        if ($IbankFrom != '' && $IbankFrom!='All') {
            $this->db->where_in('at_interbanktransfer.BankTransfer', $IbankFrom);
        }
        if ($IbankTo != '' && $IbankTo!='All') {
            $this->db->where_in('at_interbanktransfer.BankReceipt', $IbankTo);
        }
        if ($TransStatus != '') {
            $this->db->where_in('at_interbanktransfer.TransStatus', $TransStatus);
        } else {
            /*default adalah 2,6. Finished dan Need Approval*/
            $this->db->where_in('at_interbanktransfer.TransStatus', array(2, 6));
        }

        /* ===================================find by branch code========================================================= */
        if ($BranchCode != "" && $BranchCode != "All" ) {
            $this->db->where('at_interbanktransfer.BranchCode', $BranchCode);
//            $this->db->where('compaccBankTransfer.BranchCode', $BranchCode);
//            $this->db->where('compaccBankReceipt.BranchCode', $BranchCode);
        } 
        /* ==================================end find by branch code===================================================== */
        
        /* ===================================find by branch ibankfrom code========================================================= */
        if ($BranchIbankFrom != "" && $BranchIbankFrom != "All" ) {
            $this->db->where('at_interbanktransfer.BranchCode', $BranchIbankFrom);
            $this->db->where('compaccBankTransfer.BranchCode', $BranchIbankFrom);
        } 
        /* ==================================end find by branch ibankFrom code===================================================== */
        /* ===================================find by branch ibankTo code========================================================= */
        if ($BranchIbankTo != "" && $BranchIbankTo != "All" ) {
            $this->db->where('at_interbanktransfer.BranchCode', $BranchIbankTo);
            $this->db->where('compaccBankReceipt.BranchCode', $BranchIbankTo);
        } 
        /* ==================================end find by branch ibankTo code===================================================== */
        //========where sesuai Company========================//
        if ($CompCode != "" && $CompCode != "All" ) {
            $this->db->where('compaccBankTransfer.CompanyCode', $CompCode);
            $this->db->where('compaccBankReceipt.CompanyCode', $CompCode);
        } else {
            $this->db->where('compaccBankTransfer.CompanyCode',$sess_company);
            $this->db->where('compaccBankReceipt.CompanyCode',$sess_company);
        }
        //========end where sesuai Company========================//
        
        //=====================cek data tanggal jika tanggal awal lebih dari tanggal akhir=============//
        if ((($DateFrom != '') && ($DateTo != ''))) {
            if ($DateFrom <= $DateTo) {
                $this->db->where('at_interbanktransfer.IBankDate >=', date("Y-m-d", strtotime($DateFrom))); //change
                $this->db->where('at_interbanktransfer.IBankDate <=', date("Y-m-d", strtotime($DateTo))); //change
            }
        }
        //=====================END cek data tanggal jika tanggal awal lebih dari tanggal akhir=============//
        
        /* ==============================================SHORTING TABLE========================================================== */
        if($txtTable=="" && $txtField==""){
            $this->db->order_by($this->tbl_ibank . '.IBankNumber', 'ASC');
        }else if($txtTable=="acBankTransfer" || $txtTable=="acBankReceipt"){
            $this->db->order_by($txtTable . '.'.$txtField, $typeShorting);
        }
        else{
            $this->db->order_by($this->$txtTable . '.'.$txtField, $typeShorting);
        }
        /* ==============================================END SHORTING TABLE========================================================== */        
        
        $query = $this->db->get();
        //echo "<hr>".$this->db->last_query()."<hr>";
        $result = $query->result();
        //$data = array('datefrom'=>$DateFrom,'dateend'=>$DateTo,"data"=>$result);
        return $result;
    }
    
    function get_ibank_from($BranchCode){
        $jobtitle = $this->session->userdata("jobtitle");
        $this->db->select('am_accountgl.AccountNumber, am_accountgl.AccountName, am_accountgl.CurrencyCode, am_branch.BranchName, am_branch.BranchCode');
        $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.BranchCode', $BranchCode);
        $this->db->where('at_compaccountgl.GLSaldoYears', date('Y'));

        $this->db->where('am_branch.BranchStatus', 0);
        /* $this->db->where('at_compaccountgl.CompanyCode', $CompanyCode);
        $this->db->where('am_branch.BranchStatus', 1);	 */
        $this->db->where('am_accountgl.AccountSubTypeID', 7);
        $this->db->where('am_accountgl.AccountChild', 1);
        $this->db->where('am_accountgl.TransStatus', 2);

        $query = $this->db->get();

        $result=$query->result();
        return json_encode($result);
    }
    
    function pre_get_all_other_income() {
        $CompCode       = ($this->input->post('cmb_company')!=null)?$this->input->post('cmb_company'):'';
        $BranchCode     = ($this->input->post('cmb_branch')!=null)?$this->input->post('cmb_branch'):'';
        $BranchOIncomeTo= ($this->input->post('branchOIncomeTo')!=null)?$this->input->post('branchOIncomeTo'):'';
        $DateFrom       = ($this->input->post('txt_DateFrom')!=null)?$this->input->post('txt_DateFrom'):'';
        $DateTo         = ($this->input->post('txt_DateTo')!=null)?$this->input->post('txt_DateTo'):'';
        $OIncomeTo      = ($this->input->post('cmb_oincome_to')!=null)?$this->input->post('cmb_oincome_to'):'';
        $TransStatus    = ($this->input->post('chk_TransStatus')!=null)?$this->input->post('chk_TransStatus'):'';
        $txtTable       = ($this->input->post('txtTable')!=null)?$this->input->post('txtTable'):'';
        $txtField       = ($this->input->post('txtField')!=null)?$this->input->post('txtField'):'';
        $typeShorting   = ($this->input->post('typeShorting')!=null)?$this->input->post('typeShorting'):'';
        
        $Pages          = ($this->input->post('pages')!=null)?$this->input->post('pages'):'';
        $limitStart     = ($Pages['limit'] * $Pages['pages']) - $Pages['limit'];
        /* =================================END post dari find di kiri==================================================== */
           
        /* ==========================================by session======================================================= */
        $sess_company       = $this->session->userdata('company');
        $sess_branch        = $this->session->userdata('branch');
        $sess_branch_status = $this->session->userdata('branch_status');
        /* =========================================================================================================== */
    
        $this->db->select("at_otherincome.OIncomeNumber as code" );
        $this->db->from('at_otherincome');
        $this->db->join('am_accountgl as acBankPayment', 'at_otherincome.BankPayment=acBankPayment.AccountNumber');
        $this->db->join('am_branch', 'at_otherincome.BranchCode=am_branch.BranchCode');
        $this->db->join('at_compaccountgl as compaccBankPayment', 'at_otherincome.BankPayment=compaccBankPayment.AccountNumber');
        $this->db->where_in('compaccBankPayment.TransStatus', 2);
        //$this->db->where_in('compaccBankReceipt.TransStatus', 2);
        /* ========================jika kondisi tidak sama dengan kosong di find lakukan proses di if================= */
        if ($OIncomeTo != '' && $OIncomeTo!='All') {
            $this->db->where_in('at_otherincome.BankPayment', $OIncomeTo);
        }
        if ($TransStatus != '') {
            $this->db->where_in('at_otherincome.TransStatus', $TransStatus);
        } else {
            /*default adalah 2,6. Finished dan Need Approval*/
            $this->db->where_in('at_otherincome.TransStatus', array(2, 6));
        }

        /* ===================================find by branch code========================================================= */
        if ($BranchCode != "" && $BranchCode != "All" ) {
            $this->db->where('at_otherincome.BranchCode', $BranchCode);
//            $this->db->where('compaccBankTransfer.BranchCode', $BranchCode);
//            $this->db->where('compaccBankReceipt.BranchCode', $BranchCode);
        } 
        /* ==================================end find by branch code===================================================== */
        
        /* ===================================find by branch ibankTo code========================================================= */
        if ($BranchOIncomeTo != "" && $BranchOIncomeTo != "All" ) {
            $this->db->where('at_otherincome.BranchCode', $BranchOIncomeTo);
            $this->db->where('compaccBankPayment.BranchCode', $BranchOIncomeTo);
        } 
        /* ==================================end find by branch ibankTo code===================================================== */
        //========where sesuai Company========================//
        if ($CompCode != "" && $CompCode != "All" ) {
            $this->db->where('compaccBankPayment.CompanyCode', $CompCode);
        } else {
            $this->db->where('compaccBankPayment.CompanyCode',$sess_company);
        }
        //========end where sesuai Company========================//
        
        //=====================cek data tanggal jika tanggal awal lebih dari tanggal akhir=============//
        if ((($DateFrom != '') && ($DateTo != ''))) {
            if ($DateFrom <= $DateTo) {
                $this->db->where('at_otherincome.OIncomeDate >=', date("Y-m-d", strtotime($DateFrom))); //change
                $this->db->where('at_otherincome.OIncomeDate <=', date("Y-m-d", strtotime($DateTo))); //change
            }
        }
        //=====================END cek data tanggal jika tanggal awal lebih dari tanggal akhir=============//
        
        $this->db->group_by("at_otherincome.OIncomeNumber");
        /* ==============================================SHORTING TABLE========================================================== */
        if($txtTable=="" && $txtField==""){
            $this->db->order_by($this->tbl_oincome . '.OIncomeNumber', 'ASC');
        }else if($txtTable=="acBankPayment"){
            $this->db->order_by($txtTable . '.'.$txtField, $typeShorting);
        }
        else{
            $this->db->order_by($this->$txtTable . '.'.$txtField, $typeShorting);
        }
        /* ==============================================END SHORTING TABLE========================================================== */        
        
        $this->db->limit($Pages['limit'], $limitStart);
        $query = $this->db->get();
        //echo "<hr>".$this->db->last_query()."<hr>";
        $result = $query->result();
        return $result;
    }
    function get_all_other_income() {
        $CompCode       = ($this->input->post('cmb_company')!=null)?$this->input->post('cmb_company'):'';
        $BranchCode     = ($this->input->post('cmb_branch')!=null)?$this->input->post('cmb_branch'):'';
        $BranchOIncomeTo= ($this->input->post('branchOIncomeTo')!=null)?$this->input->post('branchOIncomeTo'):'';
        $DateFrom       = ($this->input->post('txt_DateFrom')!=null)?$this->input->post('txt_DateFrom'):'';
        $DateTo         = ($this->input->post('txt_DateTo')!=null)?$this->input->post('txt_DateTo'):'';
        $OIncomeTo      = ($this->input->post('cmb_oincome_to')!=null)?$this->input->post('cmb_oincome_to'):'';
        $TransStatus    = ($this->input->post('chk_TransStatus')!=null)?$this->input->post('chk_TransStatus'):'';
        $txtTable       = ($this->input->post('txtTable')!=null)?$this->input->post('txtTable'):'';
        $txtField       = ($this->input->post('txtField')!=null)?$this->input->post('txtField'):'';
        $typeShorting   = ($this->input->post('typeShorting')!=null)?$this->input->post('typeShorting'):'';
        
        $PreData        = ($this->input->post('predata')!=[null])?$this->input->post('predata'):'';
        /* =================================END post dari find di kiri==================================================== */
           
        /* ==========================================by session======================================================= */
        $sess_company       = $this->session->userdata('company');
        $sess_branch        = $this->session->userdata('branch');
        $sess_branch_status = $this->session->userdata('branch_status');
        /* =========================================================================================================== */
    
        $this->db->select("distinct(at_otherincome.OIncomeNumber)OIncomeNumber"
                . ", at_otherincome.OIncomeDate"
                . ", at_otherincome.BranchCode"
                . ", acBankPayment.AccountName as BankPaymentName"
                . ", if(at_otherincome.CurrencyCode <> 'IDR', at_otherincome.OInTotalAmount , concat('-')) as notIDR"
                . ", if(at_otherincome.CurrencyCode <> 'IDR', at_otherincome.CurrencyRate, concat('-')) as CurrencyRate"
                . ", if(at_otherincome.CurrencyCode <> 'IDR', at_otherincome.OInTotalAmount * at_otherincome.CurrencyRate, at_otherincome.OInTotalAmount ) as OInTotalAmount"
                . ", at_otherincome.TransStatus"
                . ", am_branch.BranchName");
        $this->db->from('at_otherincome');
        $this->db->join('am_accountgl as acBankPayment', 'at_otherincome.BankPayment=acBankPayment.AccountNumber');
        $this->db->join('am_branch', 'at_otherincome.BranchCode=am_branch.BranchCode');
        $this->db->join('at_compaccountgl as compaccBankPayment', 'at_otherincome.BankPayment=compaccBankPayment.AccountNumber');
        $this->db->where_in('compaccBankPayment.TransStatus', 2);
        
        $this->db->where_in('at_otherincome.OIncomeNumber', $PreData);
        //$this->db->where_in('compaccBankReceipt.TransStatus', 2);
        /* ========================jika kondisi tidak sama dengan kosong di find lakukan proses di if================= */
        if ($OIncomeTo != '' && $OIncomeTo!='All') {
            $this->db->where_in('at_otherincome.BankPayment', $OIncomeTo);
        }
        if ($TransStatus != '') {
            $this->db->where_in('at_otherincome.TransStatus', $TransStatus);
        } else {
            /*default adalah 2,6. Finished dan Need Approval*/
            $this->db->where_in('at_otherincome.TransStatus', array(2, 6));
        }

        /* ===================================find by branch code========================================================= */
        if ($BranchCode != "" && $BranchCode != "All" ) {
            $this->db->where('at_otherincome.BranchCode', $BranchCode);
//            $this->db->where('compaccBankTransfer.BranchCode', $BranchCode);
//            $this->db->where('compaccBankReceipt.BranchCode', $BranchCode);
        } 
        /* ==================================end find by branch code===================================================== */
        
        /* ===================================find by branch ibankTo code========================================================= */
        if ($BranchOIncomeTo != "" && $BranchOIncomeTo != "All" ) {
            $this->db->where('at_otherincome.BranchCode', $BranchOIncomeTo);
            $this->db->where('compaccBankPayment.BranchCode', $BranchOIncomeTo);
        } 
        /* ==================================end find by branch ibankTo code===================================================== */
        //========where sesuai Company========================//
        if ($CompCode != "" && $CompCode != "All" ) {
            $this->db->where('compaccBankPayment.CompanyCode', $CompCode);
        } else {
            $this->db->where('compaccBankPayment.CompanyCode',$sess_company);
        }
        //========end where sesuai Company========================//
        
        //=====================cek data tanggal jika tanggal awal lebih dari tanggal akhir=============//
        if ((($DateFrom != '') && ($DateTo != ''))) {
            if ($DateFrom <= $DateTo) {
                $this->db->where('at_otherincome.OIncomeDate >=', date("Y-m-d", strtotime($DateFrom))); //change
                $this->db->where('at_otherincome.OIncomeDate <=', date("Y-m-d", strtotime($DateTo))); //change
            }
        }
        //=====================END cek data tanggal jika tanggal awal lebih dari tanggal akhir=============//
        
        /* ==============================================SHORTING TABLE========================================================== */
        if($txtTable=="" && $txtField==""){
            $this->db->order_by($this->tbl_oincome . '.OIncomeNumber', 'ASC');
        }else if($txtTable=="acBankPayment"){
            $this->db->order_by($txtTable . '.'.$txtField, $typeShorting);
        }
        else{
            $this->db->order_by($this->$txtTable . '.'.$txtField, $typeShorting);
        }
        /* ==============================================END SHORTING TABLE========================================================== */        
        
        $query = $this->db->get();
        //echo "<hr>".$this->db->last_query()."<hr>";
        $result = $query->result();
        //$data = array('datefrom'=>$DateFrom,'dateend'=>$DateTo,"data"=>$result);
        return $result;
    }
    function get_oincome_to($CompanyCode,$BranchCode){     
        $this->db->select('at_compaccountgl.AccountNumber,at_compaccountgl.BranchCode,am_accountgl.AccountName,am_bank.BankOutCode,am_bank.BankInCode, 
			am_currency.CurrencyCode,am_currency.CurrencyName,am_currency.CurrencyRate');        
        $this->db->from('at_compaccountgl');
        $this->db->join('am_accountgl','at_compaccountgl.AccountNumber=am_accountgl.AccountNumber','INNER');
        $this->db->join('am_bank','am_bank.BankID=am_accountgl.BankID','INNER');
        $this->db->join('am_currency','am_accountgl.CurrencyCode=am_currency.CurrencyCode','INNER');
        
        $this->db->where('am_currency.CompanyCode', $CompanyCode);
        $this->db->where('at_compaccountgl.BranchCode', $BranchCode);
        $this->db->where('at_compaccountgl.GLSaldoYears', date('Y'));
        $this->db->where('am_accountgl.AccountSubTypeID',7);
        $this->db->where('am_accountgl.AccountChild',1);
        $this->db->where('am_accountgl.TransStatus',2);		
        return $this->db->get()->result();
    }
        
    function pre_get_all_other_payment() {        
        $CompCode       = ($this->input->post('cmb_company')!=null)?$this->input->post('cmb_company'):'';
        $BranchCode     = ($this->input->post('cmb_branch')!=null)?$this->input->post('cmb_branch'):'';
        $BranchOPaymenFrom= ($this->input->post('branchOPaymenFrom')!=null)?$this->input->post('branchOPaymenFrom'):'';
        $DateFrom       = ($this->input->post('txt_DateFrom')!=null)?$this->input->post('txt_DateFrom'):'';
        $DateTo         = ($this->input->post('txt_DateTo')!=null)?$this->input->post('txt_DateTo'):'';
        $OPaymentFrom   = ($this->input->post('cmb_opayment_from')!=null)?$this->input->post('cmb_opayment_from'):'';
        $TransStatus    = ($this->input->post('chk_TransStatus')!=null)?$this->input->post('chk_TransStatus'):'';
        $txtTable       = ($this->input->post('txtTable')!=null)?$this->input->post('txtTable'):'';
        $txtField       = ($this->input->post('txtField')!=null)?$this->input->post('txtField'):'';
        $typeShorting   = ($this->input->post('typeShorting')!=null)?$this->input->post('typeShorting'):'';
        
        $Pages          = ($this->input->post('pages')!=null)?$this->input->post('pages'):'';
        $limitStart     = ($Pages['limit'] * $Pages['pages']) - $Pages['limit'];
        /* =================================END post dari find di kiri==================================================== */
           
        /* ==========================================by session======================================================= */
        $sess_company       = $this->session->userdata('company');
        $sess_branch        = $this->session->userdata('branch');
        $sess_branch_status = $this->session->userdata('branch_status');
        /* =========================================================================================================== */
    
        $this->db->select("at_otherpayment.CPMoreNumber as code");
        $this->db->from('at_otherpayment');
        $this->db->join('am_accountgl as acBankPayment', 'at_otherpayment.BankPayment=acBankPayment.AccountNumber');
        $this->db->join('am_branch', 'at_otherpayment.BranchCode=am_branch.BranchCode');
        $this->db->join('at_compaccountgl as compaccBankPayment', 'at_otherpayment.BankPayment=compaccBankPayment.AccountNumber');
        $this->db->where_in('compaccBankPayment.TransStatus', 2);
        //$this->db->where_in('compaccBankReceipt.TransStatus', 2);
        /* ========================jika kondisi tidak sama dengan kosong di find lakukan proses di if================= */
        if ($OPaymentFrom != '' && $OPaymentFrom!='All') {
            $this->db->where_in('at_otherpayment.BankPayment', $OPaymentFrom);
        }
        if ($TransStatus != '') {
            $this->db->where_in('at_otherpayment.TransStatus', $TransStatus);
        } else {
            /*default adalah 2,6. Finished dan Need Approval*/
            $this->db->where_in('at_otherpayment.TransStatus', array(2, 6));
        }

        /* ===================================find by branch code========================================================= */
        if ($BranchCode != "" && $BranchCode != "All" ) {
            $this->db->where('at_otherpayment.BranchCode', $BranchCode);
//            $this->db->where('compaccBankTransfer.BranchCode', $BranchCode);
//            $this->db->where('compaccBankReceipt.BranchCode', $BranchCode);
        } 
        /* ==================================end find by branch code===================================================== */
        
        /* ===================================find by branch ibankTo code========================================================= */
        if ($BranchOPaymenFrom != "" && $BranchOPaymenFrom != "All" ) {
            $this->db->where('at_otherpayment.BranchCode', $BranchOPaymenFrom);
        } 
        /* ==================================end find by branch ibankTo code===================================================== */
        //========where sesuai Company========================//
        if ($CompCode != "" && $CompCode != "All" ) {
            $this->db->where('compaccBankPayment.CompanyCode', $CompCode);
        } else {
            $this->db->where('compaccBankPayment.CompanyCode',$sess_company);
        }
        //========end where sesuai Company========================//
        
        //=====================cek data tanggal jika tanggal awal lebih dari tanggal akhir=============//
        if ((($DateFrom != '') && ($DateTo != ''))) {
            if ($DateFrom <= $DateTo) {
                $this->db->where('at_otherpayment.CPMoreDate >=', date("Y-m-d", strtotime($DateFrom))); //change
                $this->db->where('at_otherpayment.CPMoreDate <=', date("Y-m-d", strtotime($DateTo))); //change
            }
        }
        //=====================END cek data tanggal jika tanggal awal lebih dari tanggal akhir=============//
        
        $this->db->group_by("at_otherpayment.CPMoreNumber");
        /* ==============================================SHORTING TABLE========================================================== */
        if($txtTable=="" && $txtField==""){
            $this->db->order_by($this->tbl_opayment . '.CPMoreNumber', 'ASC');
        }else if($txtTable=="acBankPayment" || $txtTable=="acBankReceipt"){
            $this->db->order_by($txtTable . '.'.$txtField, $typeShorting);
        }
        else{
            $this->db->order_by($this->$txtTable . '.'.$txtField, $typeShorting);
        }
        /* ==============================================END SHORTING TABLE========================================================== */        
        
        $this->db->limit($Pages['limit'], $limitStart);
        $query = $this->db->get();
        //echo "<hr>".$this->db->last_query()."<hr>";
        $result = $query->result();
        return $result;
    }
    function get_all_other_payment() {     
        $CompCode       = ($this->input->post('cmb_company')!=null)?$this->input->post('cmb_company'):'';
        $BranchCode     = ($this->input->post('cmb_branch')!=null)?$this->input->post('cmb_branch'):'';
        $BranchOPaymenFrom= ($this->input->post('branchOPaymenFrom')!=null)?$this->input->post('branchOPaymenFrom'):'';
        $DateFrom       = ($this->input->post('txt_DateFrom')!=null)?$this->input->post('txt_DateFrom'):'';
        $DateTo         = ($this->input->post('txt_DateTo')!=null)?$this->input->post('txt_DateTo'):'';
        $OPaymentFrom   = ($this->input->post('cmb_opayment_from')!=null)?$this->input->post('cmb_opayment_from'):'';
        $TransStatus    = ($this->input->post('chk_TransStatus')!=null)?$this->input->post('chk_TransStatus'):'';
        $txtTable       = ($this->input->post('txtTable')!=null)?$this->input->post('txtTable'):'';
        $txtField       = ($this->input->post('txtField')!=null)?$this->input->post('txtField'):'';
        $typeShorting   = ($this->input->post('typeShorting')!=null)?$this->input->post('typeShorting'):'';
        
        $PreData        = ($this->input->post('predata')!=[null])?$this->input->post('predata'):'';
        /* =================================END post dari find di kiri==================================================== */
           
        /* ==========================================by session======================================================= */
        $sess_company       = $this->session->userdata('company');
        $sess_branch        = $this->session->userdata('branch');
        $sess_branch_status = $this->session->userdata('branch_status');
        /* =========================================================================================================== */
    
        $this->db->select("distinct(at_otherpayment.CPMoreNumber)CPMoreNumber"
                . ", at_otherpayment.CPMoreDate"
                . ", acBankPayment.AccountName as BankPaymentName"
                . ", at_otherpayment.CPMoreTotalAmount"
                . ", at_otherpayment.TransStatus"
                . ", am_branch.BranchName");
        $this->db->from('at_otherpayment');
        $this->db->join('am_accountgl as acBankPayment', 'at_otherpayment.BankPayment=acBankPayment.AccountNumber');
        $this->db->join('am_branch', 'at_otherpayment.BranchCode=am_branch.BranchCode');
        $this->db->join('at_compaccountgl as compaccBankPayment', 'at_otherpayment.BankPayment=compaccBankPayment.AccountNumber');
        $this->db->where_in('compaccBankPayment.TransStatus', 2);
        
        $this->db->where_in('at_otherpayment.CPMoreNumber', $PreData);
        //$this->db->where_in('compaccBankReceipt.TransStatus', 2);
        /* ========================jika kondisi tidak sama dengan kosong di find lakukan proses di if================= */
        if ($OPaymentFrom != '' && $OPaymentFrom!='All') {
            $this->db->where_in('at_otherpayment.BankPayment', $OPaymentFrom);
        }
        if ($TransStatus != '') {
            $this->db->where_in('at_otherpayment.TransStatus', $TransStatus);
        } else {
            /*default adalah 2,6. Finished dan Need Approval*/
            $this->db->where_in('at_otherpayment.TransStatus', array(2, 6));
        }

        /* ===================================find by branch code========================================================= */
        if ($BranchCode != "" && $BranchCode != "All" ) {
            $this->db->where('at_otherpayment.BranchCode', $BranchCode);
//            $this->db->where('compaccBankTransfer.BranchCode', $BranchCode);
//            $this->db->where('compaccBankReceipt.BranchCode', $BranchCode);
        } 
        /* ==================================end find by branch code===================================================== */
        
        /* ===================================find by branch ibankTo code========================================================= */
        if ($BranchOPaymenFrom != "" && $BranchOPaymenFrom != "All" ) {
            $this->db->where('at_otherpayment.BranchCode', $BranchOPaymenFrom);
        } 
        /* ==================================end find by branch ibankTo code===================================================== */
        //========where sesuai Company========================//
        if ($CompCode != "" && $CompCode != "All" ) {
            $this->db->where('compaccBankPayment.CompanyCode', $CompCode);
        } else {
            $this->db->where('compaccBankPayment.CompanyCode',$sess_company);
        }
        //========end where sesuai Company========================//
        
        //=====================cek data tanggal jika tanggal awal lebih dari tanggal akhir=============//
        if ((($DateFrom != '') && ($DateTo != ''))) {
            if ($DateFrom <= $DateTo) {
                $this->db->where('at_otherpayment.CPMoreDate >=', date("Y-m-d", strtotime($DateFrom))); //change
                $this->db->where('at_otherpayment.CPMoreDate <=', date("Y-m-d", strtotime($DateTo))); //change
            }
        }
        //=====================END cek data tanggal jika tanggal awal lebih dari tanggal akhir=============//
        
        /* ==============================================SHORTING TABLE========================================================== */
        if($txtTable=="" && $txtField==""){
            $this->db->order_by($this->tbl_opayment . '.CPMoreNumber', 'ASC');
        }else if($txtTable=="acBankPayment" || $txtTable=="acBankReceipt"){
            $this->db->order_by($txtTable . '.'.$txtField, $typeShorting);
        }
        else{
            $this->db->order_by($this->$txtTable . '.'.$txtField, $typeShorting);
        }
        /* ==============================================END SHORTING TABLE========================================================== */        
        
        $query = $this->db->get();
        //echo "<hr>".$this->db->last_query()."<hr>";
        $result = $query->result();
        //$data = array('datefrom'=>$DateFrom,'dateend'=>$DateTo,"data"=>$result);
        return $result;
    }
        
    
    function pre_get_all_bank_book() {        
        $CompCode       = ($this->input->post('cmb_company')!=null)?$this->input->post('cmb_company'):'';
        $BranchCode     = ($this->input->post('cmb_branch')!=null)?$this->input->post('cmb_branch'):'';
        $BankNoAccount  = ($this->input->post('cmb_bank')!=null)?$this->input->post('cmb_bank'):'';
        $DateFrom       = ($this->input->post('txt_DateFrom')!=null)?convert_to_ymd($this->input->post('txt_DateFrom')):'';
        $DateTo         = ($this->input->post('txt_DateTo')!=null)?convert_to_ymd($this->input->post('txt_DateTo')):'';
        $TransStatus    = ($this->input->post('chk_TransStatus')!=null)?$this->input->post('chk_TransStatus'):'';
        $txtTable       = ($this->input->post('txtTable')!=null)?$this->input->post('txtTable'):'';
        $txtField       = ($this->input->post('txtField')!=null)?$this->input->post('txtField'):'';
        $typeShorting   = ($this->input->post('typeShorting')!=null)?$this->input->post('typeShorting'):'';
        
        $Pages          = ($this->input->post('pages')!=null)?$this->input->post('pages'):'';
        $limitStart     = ($Pages['limit'] * $Pages['pages']) - $Pages['limit'];
        /* =================================END post dari find di kiri==================================================== */
           
        /* ==========================================by session======================================================= */
        $sess_company       = $this->session->userdata('company');
        $sess_branch        = $this->session->userdata('branch');
        $sess_branch_status = $this->session->userdata('branch_status');
        /* =========================================================================================================== */
    
        $this->db->select("av1.Journal_Number as code");
        $this->db->from('av_all_transaction_for_report av1');
//        $this->db->join('am_branch', 'av1.Branch_Code=am_branch.BranchCode');
        $this->db->where('av1.Account_Number', $BankNoAccount);
        $this->db->where('av1.Acc_Type_Name', 'Cash / Bank');
        //$this->db->where_in('compaccBankReceipt.TransStatus', 2);
        /* ========================jika kondisi tidak sama dengan kosong di find lakukan proses di if================= */
        
        if ($TransStatus != '') {
            $this->db->where_in('av1.TransStatus', $TransStatus);
        } else {
            /*default adalah 2,6. Finished dan Need Approval*/
            $this->db->where_in('av1.TransStatus', array(2));
        }

        /* ===================================find by branch code========================================================= */
        if ($BranchCode != "" && $BranchCode != "All" ) {
            $this->db->where('av1.Branch_Code', $BranchCode);
//            $this->db->where('compaccBankTransfer.BranchCode', $BranchCode);
//            $this->db->where('compaccBankReceipt.BranchCode', $BranchCode);
        } 
        /* ==================================end find by branch code===================================================== */
        
        //========where sesuai Company========================//
        if ($CompCode != "" && $CompCode != "All" ) {
            $this->db->where('av1.CompanyCode', $CompCode);
        } else {
            $this->db->where('av1.CompanyCode',$sess_company);
        }
        //========end where sesuai Company========================//
        
        //=====================cek data tanggal jika tanggal awal lebih dari tanggal akhir=============//
        if ((($DateFrom != '') && ($DateTo != ''))) {
            if ($DateFrom <= $DateTo) {
                $this->db->where('av1.Journal_Date >=', date("Y-m-d", strtotime($DateFrom))); //change
                $this->db->where('av1.Journal_Date <=', date("Y-m-d", strtotime($DateTo))); //change
            }
        }
        //=====================END cek data tanggal jika tanggal awal lebih dari tanggal akhir=============//
        
        $this->db->group_by("av1.Journal_Number");
        $this->db->order_by("av1.Journal_Date", 'ASC');
        $this->db->order_by("av1.Journal_ID", 'ASC');
        $this->db->order_by("av1.Trans_Type", 'ASC');
        /* ==============================================SHORTING TABLE========================================================== */
        /*if($txtTable=="" && $txtField==""){
            $this->db->order_by("av1.Journal_Date", 'ASC');
        }else{
            $this->db->order_by($this->$txtTable . '.'.$txtField, $typeShorting);
        }*/
        /* ==============================================END SHORTING TABLE========================================================== */        
        
        $this->db->limit($Pages['limit'], $limitStart);
        $query = $this->db->get();
//        echo "DateFrom = $DateFrom <hr> DateTo = $DateTo";
//        echo "<hr>".$this->db->last_query()."<hr>";
        $result = $query->result();
        return $result;
    }
    function get_all_bank_book() {        
        $CompCode       = ($this->input->post('cmb_company')!=null)?$this->input->post('cmb_company'):'';
        $BranchCode     = ($this->input->post('cmb_branch')!=null)?$this->input->post('cmb_branch'):'';
        $BankNoAccount  = ($this->input->post('cmb_bank')!=null)?$this->input->post('cmb_bank'):'';
        $DateFrom       = ($this->input->post('txt_DateFrom')!=null)?$this->input->post('txt_DateFrom'):'';
        $DateTo         = ($this->input->post('txt_DateTo')!=null)?$this->input->post('txt_DateTo'):'';
        $TransStatus    = ($this->input->post('chk_TransStatus')!=null)?$this->input->post('chk_TransStatus'):'';
        $txtTable       = ($this->input->post('txtTable')!=null)?$this->input->post('txtTable'):'';
        $txtField       = ($this->input->post('txtField')!=null)?$this->input->post('txtField'):'';
        $typeShorting   = ($this->input->post('typeShorting')!=null)?$this->input->post('typeShorting'):'';
        
        $PreData        = ($this->input->post('predata')!=[null])?$this->input->post('predata'):'';
        /* =================================END post dari find di kiri==================================================== */
           
        /* ==========================================by session======================================================= */
        $sess_company       = $this->session->userdata('company');
        $sess_branch        = $this->session->userdata('branch');
        $sess_branch_status = $this->session->userdata('branch_status');
        /* =========================================================================================================== */
    
        $this->db->_protect_identifiers = FALSE;
        $this->db->select("av1.Journal_Date, av1.Journal_Desc,av1.Journal_Number"
//                . ",av1.Saldo_Debet,av1.Saldo_Credit"
                . ",IF(av1.Sign_Status=0,av1.Amount_Currency,0) as Saldo_Debet"
                . ",IF(av1.Sign_Status=1,av1.Amount_Currency,0) as Saldo_Credit"
                . ", concat(am_bank.BankCode,'-',am_bank.CurrencyCode) as BankName, am_bank.BankCode");
        $this->db->from('av_all_transaction_for_report av1');
        $this->db->join('am_branch', 'av1.Branch_Code=am_branch.BranchCode',"INNER");
        $this->db->join('am_accountgl', 'am_accountgl.AccountNumber = av1.Account_Number', 'INNER');
        $this->db->join('am_bank', 'am_bank.BankCode = am_accountgl.AccountName and am_bank.CurrencyCode=av1.CurrencyCode',"INNER");
        $this->db->where_in('av1.Journal_Number', $PreData);
        $this->db->where('av1.Account_Number', $BankNoAccount);
        $this->db->where('av1.Acc_Type_Name', 'Cash / Bank');
        //$this->db->where_in('compaccBankReceipt.TransStatus', 2);
        /* ========================jika kondisi tidak sama dengan kosong di find lakukan proses di if================= */
        
        if ($TransStatus != '') {
            $this->db->where_in('av1.TransStatus', $TransStatus);
        } else {
            /*default adalah 2,6. Finished dan Need Approval*/
            $this->db->where_in('av1.TransStatus', array(2));
        }

        /* ===================================find by branch code========================================================= */
        if ($BranchCode != "" && $BranchCode != "All" ) {
            $this->db->where('av1.Branch_Code', $BranchCode);
//            $this->db->where('compaccBankTransfer.BranchCode', $BranchCode);
//            $this->db->where('compaccBankReceipt.BranchCode', $BranchCode);
        } 
        /* ==================================end find by branch code===================================================== */
        
        //========where sesuai Company========================//
        if ($CompCode != "" && $CompCode != "All" ) {
            $this->db->where('av1.CompanyCode', $CompCode);
        } else {
            $this->db->where('av1.CompanyCode',$sess_company);
        }
        //========end where sesuai Company========================//
        
        //=====================cek data tanggal jika tanggal awal lebih dari tanggal akhir=============//
        if ((($DateFrom != '') && ($DateTo != ''))) {
            if ($DateFrom <= $DateTo) {
                $this->db->where('av1.Journal_Date >=', date("Y-m-d", strtotime($DateFrom))); //change
                $this->db->where('av1.Journal_Date <=', date("Y-m-d", strtotime($DateTo))); //change
            }
        }
        //=====================END cek data tanggal jika tanggal awal lebih dari tanggal akhir=============//
        
        /* ==============================================SHORTING TABLE========================================================== */
        
        $this->db->order_by("av1.Journal_Date", 'ASC');
        $this->db->order_by("av1.Journal_ID", 'ASC');
        $this->db->order_by("av1.Trans_Type", 'ASC');
        /*if($txtTable=="" && $txtField==""){
            $this->db->order_by("av1.Journal_Date", 'ASC');
        }else{
            $this->db->order_by($this->$txtTable . '.'.$txtField, $typeShorting);
        }*/
        /* ==============================================END SHORTING TABLE========================================================== */        
        $query = $this->db->get();
        $this->db->_protect_identifiers = TRUE;
        //echo "<hr>".$this->db->last_query()."<hr>";
        $result = $query->result();
        return $result;
    }
    function cek_start_period($company, $branch,$bankAccountNo, $periodEnd){  
        $this->db->_protect_identifiers = FALSE;	
        $this->db->select("(select pe2.PeriodEnd from at_periodend as pe2"
                . " where pe2.PeriodEnd='$periodEnd' and pe2.CompanyCode='$company') as PeriodEnd_search  "
                . ",(select av_1.Journal_Date from av_all_transaction_for_report av_1 "
                . " where av_1.Branch_Code='$branch' and av_1.CompanyCode='$company' and av_1.Account_Number='$bankAccountNo' order by av_1.Journal_Date ASC limit 1"
                . " ) as oldDateTransaction"
                . ", (select am_currency.CurrencySymbol from am_bank inner join am_currency on(am_bank.CurrencyCode=am_currency.CurrencyCode  AND am_bank.CompanyCode=am_currency.CompanyCode) "
                . " inner join am_accountgl on(am_bank.BankCode = am_accountgl.AccountName) where am_bank.CompanyCode='$company' "
                . " and am_bank.BranchCode='$branch' and (am_bank.TransStatus='0' OR am_bank.TransStatus='1') "
                . " and am_accountgl.AccountNumber='$bankAccountNo'"
                . ") as CurrencySymbol"
                . ", (select am_bank.BankCode from am_bank "
                . " inner join am_accountgl on(am_bank.BankCode = am_accountgl.AccountName) where am_bank.CompanyCode='$company' "
                . " and am_bank.BranchCode='$branch' and (am_bank.TransStatus='0' OR am_bank.TransStatus='1') "
                . " and am_accountgl.AccountNumber='$bankAccountNo'"
                . ") as BankCode"
                . ", pe1.PeriodEnd");
        $this->db->from('at_periodend pe1');
        $this->db->where('pe1.CompanyCode', $company);
        $this->db->order_by("pe1.PeriodEnd","DESC");
        $this->db->limit(1, 0);
        $query = $this->db->get();
        $this->db->_protect_identifiers = TRUE;	
//        echo "<hr>".$this->db->last_query()."<hr>";
        $result = $query->result();
        return $result;
    }
    function get_balance_period($data){  
        $this->db->_protect_identifiers = FALSE;
        $strSumTransaction ="(Select sum(IF(avall_1.Sign_Status=0,avall_1.Amount_Currency,0)) - sum(IF(avall_1.Sign_Status=1,avall_1.Amount_Currency,0)) "
                    . "from av_all_transaction_for_report as avall_1 "
                    . "where avall_1.CompanyCode = '".$data['CompanyCode']."' "
                    . "and avall_1.Branch_Code = '".$data['BranchCode']."' "
                    . "AND avall_1.Account_Number='".$data['BankAccountNo']."' "
                    . "and avall_1.Acc_Type_Name = 'Cash / Bank' "
                    . "and avall_1.TransStatus = '2' "
                    . "and (avall_1.Journal_Date >= '".$data['dateStartSum']."' "
                    . "and avall_1.Journal_Date <= '".$data['dateEndSum']."') "
                    . "group by avall_1.Account_Number)as sumTransaction ";
        if($data['transactionOnly']=="yes"){
            $this->db->select("0 as ".$data['columnName']." , ". $strSumTransaction);
        }else if($data['dateStartSum'] <= $data['dateEndSum']){            
            $this->db->select("at_compaccountgl.".$data['columnName']." , ". $strSumTransaction);
        }else{
            $this->db->select("at_compaccountgl.".$data['columnName'].",0 as sumTransaction");
        }       
        $this->db->from('at_compaccountgl');             
        $this->db->where('at_compaccountgl.CompanyCode', $data['CompanyCode']);
        $this->db->where('at_compaccountgl.BranchCode', $data['BranchCode']);
        $this->db->where('at_compaccountgl.GLSaldoYears', $data['v_year']);
        $this->db->where('at_compaccountgl.AccountNumber', $data['BankAccountNo']);
        $this->db->limit(1, 0);
        $query = $this->db->get();
        $this->db->_protect_identifiers = TRUE;	
//        echo "<hr>".$this->db->last_query()."<hr>";
//        echo "data = ".json_encode($data);
        $result = $query->result();
        return $result;
    }
    
    //get all account list
    public function get_all_account_list() {
        $CompCode       = ($this->input->post('cmb_company')!=null)?$this->input->post('cmb_company'):'';
        $BranchCode     = ($this->input->post('cmb_branch')!=null)?$this->input->post('cmb_branch'):'';
        $Vendor       = ($this->input->post('txt_vendor')!=null)?$this->input->post('txt_vendor'):'';
        $DateTo         = ($this->input->post('txt_DateTo')!=null)?$this->input->post('txt_DateTo'):'';
        $TransStatus    = ($this->input->post('chk_TransStatus')!=null)?$this->input->post('chk_TransStatus'):'';
        $txtTable       = ($this->input->post('txtTable')!=null)?$this->input->post('txtTable'):'';
        $txtField       = ($this->input->post('txtField')!=null)?$this->input->post('txtField'):'';
        $typeShorting   = ($this->input->post('typeShorting')!=null)?$this->input->post('typeShorting'):'';
        /* =================================END post dari find di kiri==================================================== */
           
        /* ==========================================by session======================================================= */
        $sess_company       = $this->session->userdata('company');
        $sess_branch        = $this->session->userdata('branch');
        $sess_branch_status = $this->session->userdata('branch_status');
        /* =========================================================================================================== */
    
        $this->db->select("* "
                );
        
        $this->db->from('av_all_transaction');
        //==========================================================================================================//
     
        /* ===================================find by VendorName code========================================================= */
        if ($Vendor != "" && $Vendor != "All" ) {
            $this->db->like('am_vendor.VendorName', $Vendor);
        } 
        /* ==================================end find by VendorName code===================================================== */
        
        /* ===================================find by branch code========================================================= */
        if ($BranchCode != "" && $BranchCode != "All" ) {
            $this->db->where('at_invoicepayment.BranchCode', $BranchCode);
        } 
        /* ==================================end find by branch code===================================================== */
        
        //========where sesuai Company========================//
        if ($CompCode != "" && $CompCode != "All" ) {
            $this->db->where("SUBSTRING(at_invoicepayment.BranchCode,1,LENGTH('".$CompCode."'))='".$CompCode."'", null); 
        } else {
            $this->db->where("SUBSTRING(at_invoicepayment.BranchCode,1,LENGTH('".$sess_company."'))='".$sess_company."'", null); 
        }
        //========end where sesuai Company========================//
        
        //=====================cek data tanggal =============//
        if ($DateTo != '') {
            $this->db->where('at_invoicepayment.InvoiceDate <=', date("Y-m-d", strtotime($DateTo))); //change
        }
        //=====================END cek data tanggal ============//
        
        $this->db->group_by("at_invoicepayment.VendorCode");
        /* ==============================================SHORTING TABLE========================================================== */
        if($txtTable=="" && $txtField==""){
            $this->db->order_by('at_invoicepayment.InvoiceDate', 'ASC');
        }else{
            $this->db->order_by($txtTable . '.'.$txtField, $typeShorting);
        }
        /* ==============================================END SHORTING TABLE========================================================== */        
        
        $query = $this->db->get();
        //echo "<hr>".$this->db->last_query()."<hr>";
        $result = $query->result();
        //$data = array('datefrom'=>$DateFrom,'dateend'=>$DateTo,"data"=>$result);
        return $result;
    }
    
    //=========== function filter =======================================//
    function get_opayment_from($companyCode, $branchCode){
        $this->db->select('am_accountgl.AccountNumber,am_accountgl.AccountName');
        $this->db->from('at_compaccountgl');
        $this->db->join('am_accountgl','at_compaccountgl.AccountNumber = am_accountgl.AccountNumber');
        //if($this->session->userdata('jobtitle') != "DIR"){
                $this->db->where('at_compaccountgl.CompanyCode', $companyCode);
                $this->db->where('at_compaccountgl.BranchCode', $branchCode );
        //}
        $this->db->where('at_compaccountgl.GLSaldoYears', date('Y'));
        $this->db->where('am_accountgl.AccountSubTypeID', 7);
        $this->db->where('am_accountgl.CurrencyCode', 'IDR');
        $this->db->where('am_accountgl.AccountChild', 1);
        $this->db->where('am_accountgl.TransStatus', 2);
        $query = $this->db->get();

        return $query->result();
    }
    
    function get_petyy_cash($company, $branch) {
        $this->db->select('am_accountgl.AccountNumber,am_accountgl.AccountName');
        $this->db->from('at_compaccountgl');
        $this->db->join('am_accountgl', 'at_compaccountgl.AccountNumber = am_accountgl.AccountNumber');
        //if($this->session->userdata('jobtitle') != "DIR"){
        $this->db->where('at_compaccountgl.CompanyCode', $company);
        if($branch!="" && $branch!="All"){
            $this->db->where('at_compaccountgl.BranchCode', $branch);
        }
        //}
        $this->db->where('at_compaccountgl.GLSaldoYears', date('Y'));
        $this->db->where('am_accountgl.AccountSubTypeID', 5);
        $this->db->where('am_accountgl.CurrencyCode', 'IDR');
        $this->db->where('am_accountgl.AccountChild', 1);
        $this->db->where('am_accountgl.TransStatus', 2);
        
        $this->db->group_by('am_accountgl.AccountNumber');
        $query = $this->db->get();

        return $query->result();
    }
    
    function get_company_by_branch($sess_branch){
            $this->db->select('CompanyName,BranchName');
            $this->db->from('am_company');
            $this->db->join('am_branch','am_branch.CompanyCode = am_company.CompanyCode','inner');
//            $this->db->join('at_cashpayment','at_cashpayment.BranchCode = am_branch.BranchCode','inner');
//            $this->db->where('at_cashpayment.BranchCode', $sess_branch);
            $this->db->where('am_branch.BranchCode', $sess_branch);
            $query = $this->db->get();
            return $query->row();
    }

    function get_company_name($company){
            $this->db->select('CompanyName');
            $this->db->from('am_company');
            $this->db->where('am_company.CompanyCode', $company);
            $query = $this->db->get();
            return $query->row();
    }   
    
    function get_list_bank($company,$branch) {
        $TransStatus = array(0,1);
        /* ==========================================by session========================================== */
        $sess_company = $this->session->userdata('company');
        $sess_branch = $this->session->userdata('branch');
        $sess_branch_status = $this->session->userdata('branch_status');
        /* =============================================================================================== */

        $this->db->_protect_identifiers = FALSE;
        $this->db->select("am_bank.BankCode, am_accountgl.AccountNumber");
        $this->db->from('am_bank');
        $this->db->join('am_accountgl', 'am_bank.BankCode = am_accountgl.AccountName', 'INNER');

        ($company != "")?$this->db->where('am_bank.CompanyCode', $company):$this->db->where('am_bank.CompanyCode', $sess_company);
        ($branch != "")?$this->db->where('am_bank.BranchCode', $branch):$this->db->where('am_bank.BranchCode', $sess_branch);
        ($TransStatus != '')?$this->db->where_in('am_bank.TransStatus', $TransStatus):$this->db->where_in('am_bank.TransStatus', array(0, 1));
        /* ======================= sort =============================================== */
        $this->db->order_by('am_bank.BranchCode', 'ASC');
        $this->db->order_by('am_bank.TransStatus', 'ASC');
        $query = $this->db->get();
        $this->db->_protect_identifiers = FALSE;
        return $query->result();
    }
        
}
