<?php

/* 	
 * 	Author 	: Tagor
 * 	Tanggal : 06-10-2014	
 * 	Revisi 	: 
 */

/*
  Author : Fachri
  Tanggal : 23-01-2015
 */

class M_payment_afiliasi_journal extends CI_Model {

    // deklarasi nama table
    public $table = "at_afiliasiinstallment_journal";
    public $table_payment = "at_afiliasiinstallment";
    public $detail = "at_ainjdetail";
    public $detail_payment = "at_ainsdetail";
    public $journal = "at_ainjjournal";
    public $journal_payment = "at_ainsjournal";
    public $table_piutang = "at_afiliasidebt";
    public $branch = "am_branch";
    public $company = "am_company";
    public $reconcile = "at_bankreconcille";
    public $period = "at_periodend";
    public $comp_gl = "at_compaccountgl";
    public $acc_gl = "am_accountgl";
    public $sub_type = "am_accountsubtype";
    public $acc_type = "am_accounttype";
    public $sys_other = "sys_otheraccount";
    public $sys_cat = "sys_othercategory";
    public $employee = "am_employee";
    public $department = "am_department";
    public $HCurrency = 'IDR';

    function check_payment_to_piutang_no($ADebtNumber, $BranchCode) {
        $this->db->select('SUM(APayAmount) as total_payment');
        $this->db->from($this->table_payment);
        $this->db->where('at_afiliasiinstallment.ADebtNumber', $ADebtNumber);
        // $this->db->where('at_afiliasiinstallment.CreditStatus', 1);
        $this->db->where('at_afiliasiinstallment.TransStatus !=', 3);
        $this->db->where('at_afiliasiinstallment.BranchCode', $BranchCode);
        $query = $this->db->get();
        return $query->row();
    }

    function get_sum_paymen($txt_payment_for) {
        $this->db->select('SUM(APayAmount) as total_piutang');
        $this->db->from($this->table);
        $this->db->where('at_afiliasiinstallment.ADebtNumber', $txt_payment_for);
        // $this->db->where('at_afiliasiinstallment.CreditStatus', 1);
        $query = $this->db->get();
        return $query->row();
    }

    //function get piutang list
    function list_piutangafiliasi($sess_company, $sess_branch, $sess_branch_status) {
        $this->db->select('at_afiliasidebt.ADebtNumber,FromCompany.CompanyName as CompFrom,FromCompany.CompanyCode as CompFromCode,
							ToCompany.CompanyName as CompTo,ToCompany.CompanyCode as CompToCode,FromBank.AccountName as BankFrom,FromBank.AccountNumber as BankFromCode,
							ToBank.AccountName as BankTo,ToBank.AccountNumber as BankToCode,at_afiliasidebt.ADebtAmount,at_afiliasidebt.ChequeNumber,
							at_afiliasidebt.CqTermStatus,at_afiliasidebt.ADebtDesc, at_afiliasidebt.AdebtIsLock,at_afiliasidebt.JVoucherBalance');
        $this->db->from('at_afiliasidebt');
        $this->db->join('am_company as FromCompany', 'FromCompany.CompanyCode = at_afiliasidebt.CompTransCode');
        $this->db->join('am_company as ToCompany', 'ToCompany.CompanyCode = at_afiliasidebt.CompRecpCode');
        $this->db->join('am_accountgl as FromBank', 'FromBank.AccountNumber = at_afiliasidebt.BankTransfer');
        $this->db->join('am_accountgl as ToBank', 'ToBank.AccountNumber = at_afiliasidebt.BankReceipt');
        $this->db->join('am_branch', 'am_branch.BranchCode = at_afiliasidebt.BranchCode', 'inner');

        //if($this->session->userdata('jobtitle') != "DIR"){
        if ($sess_branch_status == 0) {
            $this->db->where('at_afiliasidebt.CompTransCode', $sess_company);
            $this->db->where('at_afiliasidebt.CreditStatus', 1);
            $this->db->where_in('at_afiliasidebt.TransStatus', array(1));
        } else {
            $this->db->where('at_afiliasidebt.CompTransCode', $sess_company);
            $this->db->where('at_afiliasidebt.BranchCode', $sess_branch);
            $this->db->where('at_afiliasidebt.CreditStatus', 1);
            $this->db->where_in('at_afiliasidebt.TransStatus', array(1));
        }
        //}
        $this->db->order_by('at_afiliasidebt.ADebtDate', 'ASC');

        $query = $this->db->get();
        return $query->result();
    }

    function branch_code_comp_to($cmb_tocompany) {
        $this->db->select('am_branch.BranchCode');
        $this->db->from('am_branch');
        $this->db->join('am_company', 'am_branch.CompanyCode = am_company.CompanyCode', 'inner');
        $this->db->where('am_branch.CompanyCode', $cmb_tocompany);
        $this->db->where('am_branch.BranchStatus', 0);
        $query = $this->db->get()->result_array();
        return $query[0]['BranchCode'];
    }

    /* ======================penomeran transaksi new======================================================= */

    function check_code_exist_1($AInsNumber) {
        $query = $this->db->get_where($this->table, array('AInjNumber' => $AInsNumber), 1, 0);

        if ($query->num_rows() > 0) {
            return TRUE;
        } else {
            return FALSE;
        }
    }

    function create_payment_afiliasi_number($date, $branch) {

        if ($date != '') {
            $month = date('m', strtotime($date));
            $year = date('y', strtotime($date));
        } else {
            $month = date('m');
            $year = date('y');
        }

        $pa_number = $branch . "/" . $month . $year . "AJ/";

        $this->db->_protect_identifiers = false;
        $this->db->select('(CASE 
                              WHEN MAX(CONVERT(SUBSTR(AInjNumber, -3),signed)) IS NULL then 1
                              ELSE MAX(CONVERT(SUBSTR(AInjNumber, -3),signed)) + 1 END) as New_Number');
        $this->db->like('AInjNumber', $pa_number);
        $query = $this->db->get($this->table);
        $this->db->_protect_identifiers = true;

        $num = $query->row()->New_Number;
        $long = strlen($num);

        if ($long == 1) {
            $number = '00' . $num;
        } elseif ($long == 2) {
            $number = '0' . $num;
        } else {
            $number = $num;
        }

        $pa_new_number = $pa_number . $number;

        while ($this->check_code_exist_1($pa_new_number)) {
            $num++;
            $long = strlen($num);

            if ($long == 1) {
                $number = '00' . $num;
            } elseif ($long == 2) {
                $number = '0' . $num;
            } else {
                $number = $num;
            }

            $pa_new_number = $pa_number . $number;
        }

        return $pa_new_number;
    }

    function branch_code_comp($company_code) {
        $this->db->select('br.BranchCode');
        $this->db->from($this->branch . ' as br');
        $this->db->where('br.CompanyCode', $company_code);
        $this->db->where('br.BranchStatus', 0);
        $query = $this->db->get();
        $row = $query->row();

        return $row->BranchCode;
    }

    function get_other_kategori($Company_code) {
        $this->db->select('SysOtherCategoryID');
        $this->db->from('sys_othercategory');
        $this->db->where('CompanyCode', $this->session->userdata('company'));

        //get SysItemCategoryID
        $query = $this->db->get();
        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $SysOtherCategoryID = $row->SysOtherCategoryID;
            }

            return $SysOtherCategoryID;
        } else {
            return FALSE;
        }
    }

    function get_other_account($Other_kategori) {
        $this->db->select('AccountNumber');
        $this->db->from('sys_otheraccount');
        $this->db->where('SysOtherCategoryID', $Other_kategori);

        //get SysItemCategoryID
        $query = $this->db->get();
        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $AccountNumber = $row->AccountNumber;
            }

            return $AccountNumber;
        } else {
            return FALSE;
        }
    }

    public function get_all() {
        /* =========================lemparan parah dari custom.js untuk searching_data_transaksi===================== */
        $param = $this->input->post('param');
        /* ====================================end=================================================================== */

        /* =================================post dari find di kiri==================================================== */
        $CompanyNameFrom = isset($param['txt_TransferFrom']) ? mysql_real_escape_string($param['txt_TransferFrom']) : '';
        $CompanyNameTo = isset($param['txt_TransferTo']) ? mysql_real_escape_string($param['txt_TransferTo']) : '';

        $DateFrom = isset($param['txt_DateFrom']) ? $param['txt_DateFrom'] : '';
        $DateTo = isset($param['txt_DateTo']) ? $param['txt_DateTo'] : '';
        $TransStatus = isset($param['chk_TransStatus']) ? $param['chk_TransStatus'] : '';
        $BranchCode = isset($param['cmb_branch']) ? $param['cmb_branch'] : '';
        /* ============================================end============================================================ */

        /* ======================================scrol view 1-150======================================= */
        $page = ($this->input->post('page')) ? intval($this->input->post('page')) : 1;
        $rows = ($this->input->post('rows')) ? intval($this->input->post('rows')) : 150;

        $beginning = ($page - 1) * $rows;
        $end = $page * $rows;
        /* ============================================================================================== */

        /* ==========================================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('FromCompany.CompanyCode,ToCompany.CompanyCode,FromCompany.CompanyName as CompTransName,ToCompany.CompanyName as CompRecptName,
							GLBankFrom.AccountName as BankFrom,at_afiliasiinstallment_journal.CompTransCode, 
							at_afiliasiinstallment_journal.AInjNumber,at_afiliasiinstallment_journal.ADebtNumber,at_afiliasiinstallment_journal.AInjDate,
							at_afiliasiinstallment_journal.APayAmount,at_afiliasiinstallment_journal.APayDesc,at_afiliasiinstallment_journal.TransStatus as status,am_branch.BranchName, at_afiliasiinstallment_journal.LinkNumber,am_employee.EmployeeName as Created');
        $this->db->from($this->table);
        $this->db->join('am_branch', 'am_branch.BranchCode = at_afiliasiinstallment_journal.BranchCode', 'INNER');
        $this->db->join('am_company as FromCompany', 'FromCompany.CompanyCode = at_afiliasiinstallment_journal.CompTransCode', 'INNER');
        $this->db->join('am_company as ToCompany', 'ToCompany.CompanyCode = at_afiliasiinstallment_journal.CompRecpCode', 'INNER');
        $this->db->join('am_accountgl as GLBankFrom', 'GLBankFrom.AccountNumber = at_afiliasiinstallment_journal.BankTransfer', 'INNER');
        $this->db->join('am_employee', 'at_afiliasiinstallment_journal.CreatedBy=am_employee.EmployeeCode', 'LEFT');

        /* ===================================find by branch code========================================================= */
        /*
          get data dari  combobox find di view
         */
        if ($BranchCode != "") {
            $this->db->where('at_afiliasiinstallment_journal.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_afiliasiinstallment_journal.BranchCode', $sess_branch);
            //-----------------------end-------------------------------------------------//
        }
        /* =======================================================end======================================================== */

        /* ===================================find by trasnfer from dan transfer to========================================================= */
        // if($CompanyNameFrom != ''){ $this->db->like('FromCompany.CompanyName', $CompanyNameFrom); }
        // if($CompanyNameTo != ''){ $this->db->like('ToCompany.CompanyName', $CompanyNameTo); }
        /* =============================================end=================================================================== */

        /* =====================================================find by date================================================== */
        if ((($DateFrom != '') && ($DateTo != ''))) {
            if ($DateFrom <= $DateTo) {
                $this->db->where('at_afiliasiinstallment_journal.AInjDate >=', date("Y-m-d", strtotime($DateFrom)));
                $this->db->where('at_afiliasiinstallment_journal.AInjDate <=', date("Y-m-d", strtotime($DateTo)));
            }
        }
        /* ======================================================end=========================================================== */

        /* =====================================================find by status================================================== */
        if ($TransStatus != '') {
            $this->db->where_in('at_afiliasiinstallment_journal.TransStatus', $TransStatus);
        } else {
            /*
              trans status 0,1 adalah aktif
             */
            $this->db->where_in('at_afiliasiinstallment_journal.TransStatus', array(0, 1, 4, 5, 6, 7, 8));
        }

        /* =======================================================end======================================================== */

        /* ==============================================sort tabel asc========================================================== */
        $this->db->order_by($this->table . '.AInjNumber', 'ASC');
        $this->db->order_by($this->table . '.TransStatus', 'DESC');
        /* ===============================end================================================================================= */
        $query = $this->db->limit($end, $beginning);
        $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->where('am_branch.BranchCode', $sess_branch);
        $query = $this->db->get();
        return $query->row();
    }

    function get_sys_otheraccount_afiliasi($arr_company, $arr_branch, $arr_type_afiliasi) {

        $this->db->select('so.AccountNumber');
        $this->db->from($this->sys_other . ' as so');
        $this->db->join($this->sys_cat . ' as sc', 'sc.SysOtherCategoryID = so.SysOtherCategoryID', 'inner');
        $this->db->join($this->comp_gl . ' as ac', 'ac.AccountNumber = so.AccountNumber', 'inner');
        $this->db->where_in('so.AccountCode', $arr_type_afiliasi);
        $this->db->where_in('sc.CompanyCode', $arr_company);
        $this->db->where_in('ac.BranchCode', $arr_branch);
        $this->db->where('ac.GLSaldoYears', date('Y'));
        $this->db->where('ac.TransStatus', 2);
        $query = $this->db->get();
        return $query->result_array();
    }

    function get_sys_otheraccount_payment_receipt_afiliasi($company, $branch, $tipe_afiliasi) {

        $this->db->select('so.AccountNumber');
        $this->db->from($this->sys_other . ' as so');
        $this->db->join($this->sys_cat . ' as sc', 'sc.SysOtherCategoryID = so.SysOtherCategoryID', 'inner');
        $this->db->join($this->comp_gl . ' as ac', 'ac.AccountNumber = so.AccountNumber', 'inner');
        $this->db->where('so.AccountCode', $tipe_afiliasi);
        $this->db->where('sc.CompanyCode', $company);
        $this->db->where('ac.BranchCode', $branch);
        $this->db->where('ac.GLSaldoYears', date('Y'));
        $this->db->where('ac.TransStatus', 2);
        $query = $this->db->get()->result_array();
        return $query[0]['AccountNumber'];
    }

    function get_account_in_companygl($arr_company_gl, $arr_company) {
        $this->db->select('ac.AccountNumber');
        $this->db->from($this->comp_gl . ' as ac');
        $this->db->where_in('ac.AccountNumber', $arr_company_gl);
        $this->db->where_in('ac.Br', $arr_company_gl);
    }

    function cek_jurnal_at_companyaccgl($nomor_akun, $branch) {
        $this->db->select('at_compaccountgl.AccountNumber');
        $this->db->from('at_compaccountgl');
        $this->db->where('at_compaccountgl.GLSaldoYears', date('Y'));
        $this->db->where('at_compaccountgl.AccountNumber', $nomor_akun);
        $this->db->where('at_compaccountgl.BranchCode', $branch);
        $query = $this->db->get();
        return $query->result();
    }

    function get_by_payment_number($payment_no) {
        $this->db->select('FromCompany.CompanyCode,ToCompany.CompanyCode,FromCompany.CompanyName as CompTransName,ToCompany.CompanyName as CompRecptName,
								GLBankFrom.AccountName as BankFrom,GLBankTo.AccountName as BankTo,
								at_afiliasiinstallment.AInsNumber,at_afiliasiinstallment.ADebtNumber,at_afiliasiinstallment.AInsDate,
								at_afiliasiinstallment.APayAmount,at_afiliasiinstallment.APayDesc,at_afiliasiinstallment.TransStatus,am_branch.BranchName');
        $this->db->from($this->table);
        $this->db->join('am_branch', 'am_branch.BranchCode = at_afiliasiinstallment.BranchCode');
        $this->db->join('am_company as FromCompany', 'FromCompany.CompanyCode = at_afiliasiinstallment.CompTransCode');
        $this->db->join('am_company as ToCompany', 'ToCompany.CompanyCode = at_afiliasiinstallment.CompRecpCode');
        $this->db->join('am_accountgl as GLBankFrom', 'GLBankFrom.AccountNumber = at_afiliasiinstallment.BankTransfer', 'INNER');
        $this->db->join('am_accountgl as GLBankTo', 'GLBankTo.AccountNumber = at_afiliasiinstallment.BankReceipt', 'INNER');
        $this->db->where('at_afiliasiinstallment.AInsNumber', $payment_no);
        $query = $this->db->get();
        return $query->result();
    }

    //function get latest number afiliasi
    function get_latest_payment_afiliasi_number($sess_branch) {
        $this->db->select_max('AInsNumber');
        $query = $this->db->get_where($this->table, array('BranchCode' => $sess_branch));

        return $query->result();
    }

    // Function Join
    function get_accountgl() {
        $this->db->select('*');
        $this->db->from('at_compaccountgl');
        $this->db->join('am_accountgl', 'am_accountgl.AccountNumber = at_compaccountgl.AccountNumber');
        $this->db->join('am_accounttype', 'am_accounttype.AccountTypeID = am_accountgl.AccountTypeID');
        $this->db->like('am_accounttype.AccountTypeName', 'Cash / Bank', 'both');
        //$this->db->or_like('am_accounttype.AccountTypeName', 'cash','both');
        //$this->db->or_like('am_accounttype.AccountTypeNamess', 'bank','both');
        $query = $this->db->get();
        return $query->result();
    }

    function get_payment($TransNumber) {
        $this->db->select('gr.BranchCode,gr.ADebtNumber,gr.BankTransfer as BankFrom,ag.AccountName as Loss_Account,
							gr.AInjNumber,FromCompany.CompanyCode as FromCompCode,gr.CreatedBy,
							ToCompany.CompanyCode as ToCompCode,ag.AccountNumber as AccountBankFrom,gr.AInjDate,
							gr.APayDesc,
							gr.APayAmount,gr.TransStatus as status,gr.AppLevel2Status,gr.LinkNumber,ad.ADebtAmount,
							ad.TransStatus as piutang_status,em.DepartmentCode');
        $this->db->from($this->table . ' as gr');
        $this->db->join($this->company . ' as FromCompany', 'FromCompany.CompanyCode = gr.CompTransCode');
        $this->db->join($this->company . ' as ToCompany', 'ToCompany.CompanyCode = gr.CompRecpCode');
        $this->db->join($this->acc_gl . ' as ag', 'ag.AccountNumber = gr.BankTransfer');
        $this->db->join($this->table_piutang . ' as ad', 'ad.ADebtNumber = gr.ADebtNumber');
        $this->db->join($this->employee . ' as em', 'em.EmployeeCode=gr.CreatedBy', 'inner');
        $this->db->where('gr.AInjNumber', $TransNumber);
        $query = $this->db->get();
        return $query->row();
    }

    function get_payment_detail($AInjNumber) {
        $this->db->select('dt.AInjDetailID,dt.AInjAmount as PaymentAmount,ag.AccountName as GainAccount,dt.AInjMemo as Memo,cp.CompanyName as PaymentCompany');
        $this->db->from($this->detail . ' as dt');
        $this->db->join($this->table . ' as gr', 'gr.AInjNumber =dt.AInjNumber', 'inner');
        $this->db->join($this->company . ' as cp', 'cp.CompanyCode=gr.CompRecpCode', 'inner');
        $this->db->join($this->acc_gl . ' as ag', 'ag.AccountNumber=dt.AInjAccountNumber', 'inner');
        $this->db->where('dt.AInjNumber', $AInjNumber);
        $this->db->order_by('dt.AInjDetailID', 'ASC');
        $query = $this->db->get();
        return $query->result();
    }

    // Function check number exsist	
    function check_code_exist($id) {
        $query = $this->db->get_where($this->table, array('AInsNumber' => $id), 1, 0);

        if ($query->num_rows() > 0) {
            return TRUE;
        } else {
            return FALSE;
        }
    }

    // Function Insert
    function insert_group($data) {
        $this->db->insert($this->table, $data);
    }

    function insert_detail($detail_data) {
        $this->db->insert($this->detail, $detail_data);
    }

    function insert_journal($data_journal) {
        $this->db->insert($this->journal, $data_journal);
    }

    function update_group($data_group, $link_number) {
        $this->db->update($this->table, $data_group, array('LinkNumber' => $link_number));
    }

    function update_payment_afiliasi_no($data, $link_number) {
        $this->db->update($this->table, $data, array('LinkNumber' => $link_number));
    }

    function update_detail($detail_data, $AInjDetailID) {
        $this->db->update($this->detail, $detail_data, array('AInjDetailID' => $AInjDetailID));
    }

    function suspend_data($data_suspend, $link_number) {
        $this->db->update($this->table, $data_suspend, array('LinkNumber' => $link_number));
    }

    function delete_detail($AInjDetailID) {
        $this->db->delete($this->detail, array('AInjDetailID' => $AInjDetailID));
    }

    function get_bank_bycompany($company_code) {
        $this->db->select('ag.AccountName,ag.AccountNumber,ag.CurrencyCode');
        $this->db->from($this->comp_gl . ' as ac');
        $this->db->join($this->acc_gl . ' as ag', 'ag.AccountNumber = ac.AccountNumber', 'INNER');
        $this->db->join($this->branch . ' as br', 'br.BranchCode = ac.BranchCode', 'INNER');
        $this->db->join($this->sub_type . ' as sub', 'sub.AccountSubTypeID = ag.AccountSubTypeID', 'INNER');
        $this->db->where('ac.CompanyCode', $company_code);
        $this->db->where('ac.GLSaldoYears', date('Y'));
        $this->db->where('sub.AccountTypeName', 'Bank');
        $this->db->where('ag.AccountChild !=', 0);
        $this->db->where('ac.TransStatus =', 2);
        $this->db->where('ag.CurrencyCode =', 'IDR');
        $this->db->where('br.BranchStatus', 0);
        $query = $this->db->get();

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

        return $query->result();
    }

    function get_company_bycompanycode($comp_code) {
        $this->db->select('CompanyCode,CompanyName');
        $this->db->from('am_company');
        $this->db->where('CompanyCode', $comp_code);
        $query = $this->db->get();

        return $query->result();
    }

    function get_branch_by_comp_and_accnumb() {
        $this->db->select('*');
        $this->db->from('at_compaccountgl');
        $this->db->join('am_company', 'am_company.CompanyCode = at_compaccountgl.CompanyCode');
        $this->db->join('am_branch', 'am_branch.CompanyCode = am_company.CompanyCode');
        $query = $this->db->get();
        return $query->result();
    }

    function get_all_by_no($payafiliasi_number) {
        $this->db->select('TransStatus');
        $this->db->from($this->table);
        $this->db->where('at_afiliasiinstallment.AInsNumber', $payafiliasi_number);
        $query = $this->db->get();
        return $query->result();
    }

    function get_by_piutang($piutang_no) {
        $this->db->select('at_ainsdetail.LoanPayment,at_ainsdetail.PaymentPeriod,at_ainsdetail.PaymentPerPeriod,at_ainsdetail.PaymentBalance, at_ainsdetail.AInsNumber');
        $this->db->from($this->detail);
        $this->db->join($this->table, $this->table . '.AInsNumber = ' . $this->detail . '.AInsNumber');
        $this->db->where($this->table . '.ADebtNumber', $piutang_no);
        $this->db->where($this->table . '.TransStatus !=', 3);
        $this->db->order_by('PaymentPeriod', 'ASC');
        $query = $this->db->get();
        return $query->result();
    }

    function get_max_payment_period($piutang_no) {
        $this->db->select('max(PaymentPeriod) as max_period');
        $this->db->from($this->detail . ' as dt');
        $this->db->join($this->table . ' as tb', 'dt.AInsNumber = tb.AInsNumber', 'INNER');
        $this->db->where('tb.ADebtNumber', $piutang_no);
        $this->db->where('tb.TransStatus !=', 3);
        $query = $this->db->get()->result_array();

        return $query[0]['max_period'];
    }

    function get_period_payment($payment_no) {
        $this->db->select('count(ADebtNumber) as total_payment');
        $this->db->from($this->table . ' as tb');
        $this->db->where('tb.AInsNumber', $payment_no);
        $query = $this->db->get()->result_array();

        return $query[0]['total_payment'];
    }

    function get_link_number_suspen($id) {
        $this->db->select('ADebtNumber');
        $this->db->from($this->table);
        $this->db->where($this->table . '.LinkNumber', $id);
        $query = $this->db->get();
        return $query->row();
    }

    function cek_at_adebt_number($ADebtNumber) {
        $this->db->select('LinkNumber');
        $this->db->from('at_afiliasidebt');
        $this->db->where('at_afiliasidebt.ADebtNumber', $ADebtNumber);
        $query = $this->db->get()->result_array();
        if (count($query) > 0) {
            return $query[0]['LinkNumber'];
        } else {
            return 0;
        }
    }

    function get_link_numberafiliasijournal($AInjNumber){
        $this->db->select('gr.LinkNumber');
        $this->db->from($this->table.' as gr');
        $this->db->where('gr.AInjNumber',$AInjNumber);

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

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

    function update_piutang_afiliasi_by_no($data_piutang, $link_number_adeb) {
        $this->db->update('at_afiliasidebt', $data_piutang, array('LinkNumber' => $link_number_adeb));
    }

    function update_is_lock($data_islock_status, $link_number_adeb_2) {
        $this->db->update('at_afiliasidebt', $data_islock_status, array('LinkNumber' => $link_number_adeb_2));
    }

    ##== function to Journal ==##

    function akun_payment_afiliasi($company_code) {

        $this->db->select('at_compaccountgl.AccountNumber, am_accountgl.AccountName');
        $this->db->from('sys_otheraccount');
        $this->db->join('sys_othercategory', 'sys_othercategory.SysOtherCategoryID = sys_otheraccount.SysOtherCategoryID', 'inner');
        $this->db->join('at_compaccountgl', 'at_compaccountgl.AccountNumber = sys_otheraccount.AccountNumber', 'inner');
        $this->db->join('am_accountgl', 'am_accountgl.AccountNumber = at_compaccountgl.AccountNumber', 'inner');
        $this->db->where('sys_otheraccount.AccountCode', 'PaymentAfiliasi');
        $this->db->where('sys_othercategory.CompanyCode', $company_code);
        $this->db->where('at_compaccountgl.CompanyCode', $company_code);
        $this->db->where('at_compaccountgl.GLSaldoYears', date('Y'));
        $this->db->where('am_accountgl.TransStatus', 2);
        $query = $this->db->get();
        return $query->row();

        /* $this->db->select('sys_otheraccount.AccountNumber,am_accountgl.AccountName');
          $this->db->from('sys_otheraccount');
          $this->db->join('sys_othercategory','sys_othercategory.SysOtherCategoryID = sys_otheraccount.SysOtherCategoryID');
          $this->db->join('am_accountgl','am_accountgl.AccountNumber = sys_otheraccount.AccountNumber');
          $this->db->where('sys_otheraccount.AccountCode', 'PaymentAfiliasi');
          $this->db->where('sys_othercategory.OtherCategoryID', 1);
          $this->db->where('sys_othercategory.CompanyCode', $company_code);
          $query = $this->db->get();
          return $query->row(); */
    }

    function akun_penerimaan_afiliasi($company_code) {

        $this->db->select('at_compaccountgl.AccountNumber, am_accountgl.AccountName');
        $this->db->from('sys_otheraccount');
        $this->db->join('sys_othercategory', 'sys_othercategory.SysOtherCategoryID = sys_otheraccount.SysOtherCategoryID', 'inner');
        $this->db->join('at_compaccountgl', 'at_compaccountgl.AccountNumber = sys_otheraccount.AccountNumber', 'inner');
        $this->db->join('am_accountgl', 'am_accountgl.AccountNumber = at_compaccountgl.AccountNumber', 'inner');
        $this->db->where('sys_otheraccount.AccountCode', 'ReceiptAfiliasi');
        $this->db->where('sys_othercategory.CompanyCode', $company_code);
        $this->db->where('at_compaccountgl.CompanyCode', $company_code);
        $this->db->where('at_compaccountgl.GLSaldoYears', date('Y'));
        $this->db->where('am_accountgl.TransStatus', 2);
        $query = $this->db->get();
        return $query->row();

        /* $this->db->select('sys_otheraccount.AccountNumber,am_accountgl.AccountName');
          $this->db->from('sys_otheraccount');
          $this->db->join('sys_othercategory','sys_othercategory.SysOtherCategoryID = sys_otheraccount.SysOtherCategoryID');
          $this->db->join('am_accountgl','am_accountgl.AccountNumber = sys_otheraccount.AccountNumber');
          $this->db->where('sys_otheraccount.AccountCode', 'ReceiptAfiliasi');
          $this->db->where('sys_othercategory.OtherCategoryID', 1);
          $this->db->where('sys_othercategory.CompanyCode', $company_code);
          $query = $this->db->get();
          return $query->row(); */
    }

    function akun_hutang_afiliasi() {
        $this->db->select('AccountNumber,AccountName');
        $this->db->from('am_accountgl');
        $this->db->where('am_accountgl.AccountName', 'Hutang Afiliasi');
        $query = $this->db->get();
        return $query->row();
    }

    function akun_piutang_afiliasi() {
        $this->db->select('AccountNumber,AccountName');
        $this->db->from('am_accountgl');
        $this->db->where('am_accountgl.AccountName', 'Piutang Afiliasi');
        $query = $this->db->get();
        return $query->row();
    }

    function get_company_from_n_to($payment_no) {
        $this->db->select('CompTransCode,CompRecpCode');
        $this->db->from($this->table);
        $this->db->where('AInjNumber', $payment_no);
        $query = $this->db->get();
        return $query->row();
    }

    function branch_code_by_company_transfer($company_code) {
        $this->db->select('am_branch.BranchCode,am_company.CompanyName');
        $this->db->from('am_branch');
        $this->db->join('am_company', 'am_company.CompanyCode = am_branch.CompanyCode');
        $this->db->where('am_company.CompanyCode', $company_code);
        $this->db->where('am_branch.BranchStatus', 0);
        $query = $this->db->get();
        return $query->row();
    }

    function detail_to_journal($link_number) {
        $this->db->select("jr.*,ag.AccountName,cf.CompanyName as CompFrom,ct.CompanyName as CompTo,jr.AInjNumber,
							(CASE WHEN jr.AInjNumber='$link_number' THEN gr.CompTransCode ELSE gr.CompRecpCode END) as company_code");
        $this->db->from($this->journal . ' as jr');
        $this->db->join($this->table . ' as gr', 'gr.AInjNumber=jr.AInjNumber', 'inner');
        $this->db->join($this->acc_gl . ' as ag', 'ag.AccountNumber = jr.AInjAccountNumber', 'inner');
        $this->db->join($this->company . ' as cf', 'cf.CompanyCode=gr.CompTransCode', 'inner');
        $this->db->join($this->company . ' as ct', 'ct.CompanyCode=gr.CompRecpCode', 'inner');
        $this->db->where('jr.LinkNumber', $link_number);
        $query = $this->db->get();
        return $query->result();
    }

    ##== End function to Journal ==##

    public function get_all_to_print($data_filter) {
        /* =================================post dari find di kiri==================================================== */
        $CompanyNameFrom    = isset($data_filter['txt_TransferFrom']) ? mysql_real_escape_string($data_filter['txt_TransferFrom']) : '';
        $CompanyNameTo      = isset($data_filter['txt_TransferTo']) ? mysql_real_escape_string($data_filter['txt_TransferTo']) : '';
        $BranchCode         = $data_filter['cmb_branch'];
        $DateFrom           = $data_filter['txt_DateFrom'];
        $DateTo             = $data_filter['txt_DateTo'];
        $TransStatus        = $data_filter['chk_TransStatus'];
        /* ============================================end============================================================ */

        /* ==========================================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('FromCompany.CompanyCode,ToCompany.CompanyCode,FromCompany.CompanyName as CompTransName,ToCompany.CompanyName as CompRecptName,
							GLBankFrom.AccountName as BankFrom,at_afiliasiinstallment_journal.CompTransCode, 
							at_afiliasiinstallment_journal.AInjNumber,at_afiliasiinstallment_journal.ADebtNumber,at_afiliasiinstallment_journal.AInjDate,
							at_afiliasiinstallment_journal.APayAmount,at_afiliasiinstallment_journal.APayDesc,at_afiliasiinstallment_journal.TransStatus as status,am_branch.BranchName, at_afiliasiinstallment_journal.LinkNumber,am_employee.EmployeeName as Created');
        $this->db->from($this->table);
        $this->db->join('am_branch', 'am_branch.BranchCode = at_afiliasiinstallment_journal.BranchCode', 'INNER');
        $this->db->join('am_company as FromCompany', 'FromCompany.CompanyCode = at_afiliasiinstallment_journal.CompTransCode', 'INNER');
        $this->db->join('am_company as ToCompany', 'ToCompany.CompanyCode = at_afiliasiinstallment_journal.CompRecpCode', 'INNER');
        $this->db->join('am_accountgl as GLBankFrom', 'GLBankFrom.AccountNumber = at_afiliasiinstallment_journal.BankTransfer', 'INNER');
        $this->db->join('am_employee', 'at_afiliasiinstallment_journal.CreatedBy=am_employee.EmployeeCode', 'LEFT');

        /* ===================================find by branch code========================================================= */
        /*
          get data dari  combobox find di view
         */
        if ($BranchCode != "") {
            $this->db->where('at_afiliasiinstallment_journal.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_afiliasiinstallment_journal.BranchCode', $sess_branch);
            //-----------------------end-------------------------------------------------//
        }
        /* =======================================================end======================================================== */

        /* ===================================find by trasnfer from dan transfer to========================================================= */
        // if($CompanyNameFrom != ''){ $this->db->like('FromCompany.CompanyName', $CompanyNameFrom); }
        // if($CompanyNameTo != ''){ $this->db->like('ToCompany.CompanyName', $CompanyNameTo); }
        /* =============================================end=================================================================== */

        /* =====================================================find by date================================================== */
        if ((($DateFrom != '') && ($DateTo != ''))) {
            if ($DateFrom <= $DateTo) {
                $this->db->where('at_afiliasiinstallment_journal.AInjDate >=', date("Y-m-d", strtotime($DateFrom)));
                $this->db->where('at_afiliasiinstallment_journal.AInjDate <=', date("Y-m-d", strtotime($DateTo)));
            }
        }
        /* ======================================================end=========================================================== */

        /* =====================================================find by status================================================== */
        if (!empty($TransStatus)) {
            $this->db->where_in('at_afiliasiinstallment_journal.TransStatus', $TransStatus);
        } else {
            /*
              trans status 0,1 adalah aktif
             */
            $this->db->where_in('at_afiliasiinstallment_journal.TransStatus', array(0, 1, 4, 5, 6, 7, 8));
        }

        /* =======================================================end======================================================== */

        /* ==============================================sort tabel asc========================================================== */
        $this->db->order_by($this->table . '.AInjNumber', 'ASC');
        $this->db->order_by($this->table . '.TransStatus', 'DESC');
        /* ===============================end================================================================================= */
        $query = $this->db->get();
        return $query->result();
    }

    function get_all_to_print_LAMA($sess_branch) {
        $this->db->select('FromCompany.CompanyCode,ToCompany.CompanyCode,FromCompany.CompanyName as CompTransName,ToCompany.CompanyName as CompRecptName,
							GLBankFrom.AccountName as BankFrom,
							gr.AInjNumber,gr.ADebtNumber,gr.AInjDate,
							gr.APayAmount,gr.APayDesc,gr.TransStatus,am_branch.BranchName');
        $this->db->from($this->table . ' as gr');
        $this->db->join('am_branch', 'am_branch.BranchCode = gr.BranchCode');
        $this->db->join('am_company as FromCompany', 'FromCompany.CompanyCode = gr.CompTransCode');
        $this->db->join('am_company as ToCompany', 'ToCompany.CompanyCode = gr.CompRecpCode');
        $this->db->join('am_accountgl as GLBankFrom', 'GLBankFrom.AccountNumber = gr.BankTransfer', 'INNER');
        $this->db->where('gr.BranchCode', $sess_branch);

        $query = $this->db->get();
        return $query->result();
    }

    function m_delete_journal($link_number) {
        $this->db->where('LinkNumber', $link_number);
        $this->db->delete($this->journal);
    }

    function update_status_approval($link_number, $data_approval) {
        $this->db->where('LinkNumber', $link_number);
        $this->db->update($this->table, $data_approval);
    }

    function update_to_reverse_payment($link_number_payment, $data_reversed_payment) {
        $this->db->where('LinkNumber', $link_number_payment);
        $this->db->update($this->table, $data_reversed_payment);
    }

    function update_to_reverse_piutang($link_number_piutang, $data_reversed_piutang) {
        $this->db->where('LinkNumber', $link_number_piutang);
        $this->db->update('at_afiliasidebt', $data_reversed_piutang);
    }

    function get_detail_item_print($link_number, $BranchCode, $operasi) {
        $this->db->select('gr.*,b_transfer.AccountName as Bank_transfer,branch.BranchName,branch.BranchAddress,am_prepared.EmployeeName as nm_prepared,am_dir.EmployeeName as nm_direksi');
        $this->db->from($this->table . ' as gr');
        $this->db->join($this->acc_gl . ' as b_transfer', 'b_transfer.AccountNumber = gr.BankTransfer', 'INNER');
        $this->db->join($this->branch . ' as branch', 'branch.BranchCode = gr.BranchCode', 'INNER');
        $this->db->join($this->employee . ' as am_prepared', 'gr.CreatedBy=am_prepared.EmployeeCode', 'LEFT');
        $this->db->join($this->employee . ' as am_dir', 'gr.AppLevel2Code=am_dir.EmployeeCode', 'LEFT');
        $this->db->where('gr.LinkNumber', $link_number);
        $this->db->where('gr.BranchCode ' . $operasi, $BranchCode);
        $query = $this->db->get();
        return $query->row();
    }

    function get_group_item_print_journal($link_number, $BranchCode) {
        $this->db->select('gr.ADebtNumber,gr.AInjNumber,gr.AInjDate,gr.APayDesc,gr.APayAmount,gr.TransStatus,ag.AccountName,br_from.BranchName,br_from.BranchAddress,br_to.BranchName,br_to.BranchAddress,am_prepared.EmployeeName as nm_prepared,am_dir.EmployeeName as nm_direksi');
        $this->db->from($this->table . ' as gr');
        $this->db->join($this->acc_gl . ' as ag', 'ag.AccountNumber=gr.BankTransfer', 'inner');
        $this->db->join($this->branch . ' as br_from', 'br_from.BranchCode = gr.BranchCode', 'INNER');
        $this->db->join($this->branch . ' as br_to', 'br_to.BranchCode = gr.BranchCode', 'INNER');
        $this->db->join($this->employee . ' as am_prepared', 'gr.CreatedBy=am_prepared.EmployeeCode', 'LEFT');
        $this->db->join($this->employee . ' as am_dir', 'gr.AppLevel2Code=am_dir.EmployeeCode', 'LEFT');
        $this->db->where('gr.AInjNumber', $link_number);

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

        return $query->row();
    }

    function get_detail_item_print_journal($link_number, $branch) {
        $query = $this->db->query("SELECT dt.AInjNumber AS TransNumber,dt.AInjAmount AS Amount,dt.AInjMemo AS Memo,
		(SELECT br.BranchName 
		FROM at_afiliasiinstallment_journal AS gr 
		INNER JOIN am_branch AS br ON br.BranchCode=gr.BranchCode 
		WHERE gr.LinkNumber= '$link_number' AND gr.BranchCode='$branch') AS TransFrom,
		(SELECT br.BranchName 
		FROM at_afiliasiinstallment_journal AS gr 
		INNER JOIN am_branch AS br ON br.BranchCode=gr.BranchCode 
		WHERE gr.LinkNumber= '$link_number' AND gr.BranchCode!='$branch') AS TransTo,
		`ag`.`AccountName` AS Account_Name
		FROM (`at_ainjdetail` AS dt) 
		INNER JOIN `am_accountgl` AS ag ON `ag`.`AccountNumber` = `dt`.`AInjAccountNumber`
		WHERE `dt`.`AInjNumber` = '$link_number'");

        // UNION 
        // SELECT tb.AInjNumber AS TransNumber,tb.APayAmount AS Amount,tb.APayDesc AS Memo,
        // (SELECT br.BranchName 
        // FROM at_afiliasiinstallment_journal AS gr 
        // INNER JOIN am_branch AS br ON br.BranchCode=gr.BranchCode 
        // WHERE gr.LinkNumber= '$link_number' AND gr.BranchCode='$branch') AS TransFrom,
        // (SELECT br.BranchName 
        // FROM at_afiliasiinstallment_journal AS gr 
        // INNER JOIN am_branch AS br ON br.BranchCode=gr.BranchCode 
        // WHERE gr.LinkNumber= '$link_number' AND gr.BranchCode!='$branch') AS TransTo,
        // ag.`AccountName` AS Account_Name
        // FROM at_afiliasiinstallment_journal AS tb
        // INNER JOIN `am_accountgl` AS ag ON `ag`.`AccountNumber` = `tb`.`BankTransfer`
        // WHERE `tb`.`AInjNumber` = '$link_number'");

        return $query->result_array();
    }

    function get_link_number($no_afiliasi) {
        $this->db->where('AInjNumber', $no_afiliasi);
        $query = $this->db->get($this->table)->result_array();
        return $query[0]['LinkNumber'];
    }

    function check_status_piutang_afiliasi($no_piutang_afiliasi) {
        $this->db->select('TransStatus');
        $this->db->where('ADebtNumber', $no_piutang_afiliasi);
        $query = $this->db->get('at_afiliasidebt')->result_array();
        if ($query[0]['TransStatus'] == 1) {
            return true;
        } else {
            return false;
        }
    }

    function m_get_total_piutang($ADebtNumber) {
        $this->db->select('ADebtAmount,JVoucherBalance');
        $this->db->from('at_afiliasidebt');
        $this->db->where('ADebtNumber', $ADebtNumber);

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

        if (count($query) > 0) {
            return $query[0];
        } else {
            return false;
        }
    }

    function m_get_total_payment($ADebtNumber, $branch) {

        $TransStatus = array(2, 5);

        $this->db->select('sum(APayAmount) as total_payment');
        $this->db->from($this->table);
        $this->db->where('ADebtNumber', $ADebtNumber);
        $this->db->where('BranchCode', $branch);
        $this->db->where_in('TransStatus', $TransStatus);

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

        if (count($query) > 0) {
            return $query[0]['total_payment'];
        } else {
            return false;
        }
    }

    function get_payment_number($link_number) {

        $branch = $this->session->userdata('branch');

        $this->db->select('AInjNumber');
        $this->db->where('LinkNumber', $link_number);
        $this->db->where('BranchCode !=', $branch);

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

        if (count($query) > 0) {
            return $query[0]['AInjNumber'];
        } else {
            return false;
        }
    }

    function delete_reconcile($TransNumber) {
        $this->db->where('TransNumber', $TransNumber);
        $this->db->delete($this->reconcile);
    }

    function check_period_end($periodend) {
        $this->db->select('PeriodEnd');
        $this->db->from($this->period);
        $this->db->where('PeriodEnd', $periodend);

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

        if (count($query) > 0) {
            return true;
        } else {
            return false;
        }
    }

    function m_get_other_expense($comp_code) {
        $this->db->select('ag.AccountName,ag.AccountNumber,ag.CurrencyCode,acc.AccountTypeName');
        $this->db->from($this->comp_gl . ' as ac');
        $this->db->join($this->acc_gl . ' as ag', 'ag.AccountNumber = ac.AccountNumber', 'INNER');
        $this->db->join($this->branch . ' as br', 'br.BranchCode = ac.BranchCode', 'INNER');
        $this->db->join($this->sub_type . ' as sub', 'sub.AccountSubTypeID = ag.AccountSubTypeID', 'INNER');
        $this->db->join($this->acc_type . ' as acc', 'acc.AccountTypeID = sub.AccountTypeID', 'INNER');

        $this->db->where('ac.CompanyCode', $comp_code);
        $this->db->where('ac.GLSaldoYears', date('Y'));
        //$this->db->where_in('acc.AccountTypeName', array('Revenue','Other Revenue','Expense','Other Expense'));
        $this->db->where('ag.AccountChild !=', 0);
        $this->db->where('ac.TransStatus =', 2);
        $this->db->where('br.BranchStatus', 0);
        $query = $this->db->get()->result();

        $return = array();

        foreach($query as $row){
            if(in_array($row->AccountTypeName,array('Revenue','Expense','COGS / COGM (Cost Of Goods Made)','Other Revenue','Other Expense'))){
                $return[] = $row;
            }else{
                if($row->CurrencyCode == $this->HCurrency){
                    $return[] = $row;
                }
            }
        }

        return $return;
    }

    function m_get_other_revenue($comp_code) {
        $this->db->select('ag.AccountName,ag.AccountNumber,ag.CurrencyCode,acc.AccountTypeName');
        $this->db->from($this->comp_gl . ' as ac');
        $this->db->join($this->acc_gl . ' as ag', 'ag.AccountNumber = ac.AccountNumber', 'INNER');
        $this->db->join($this->branch . ' as br', 'br.BranchCode = ac.BranchCode', 'INNER');
        $this->db->join($this->sub_type . ' as sub', 'sub.AccountSubTypeID = ag.AccountSubTypeID', 'INNER');
        $this->db->join($this->acc_type . ' as acc', 'acc.AccountTypeID = sub.AccountTypeID', 'INNER');

        $this->db->where('ac.CompanyCode', $comp_code);
        $this->db->where('ac.GLSaldoYears', date('Y'));
        //$this->db->where_in('acc.AccountTypeName', array('Revenue','Other Revenue','Expense','Other Expense'));
        $this->db->where('ag.AccountChild !=', 0);
        $this->db->where('ac.TransStatus =', 2);
        $this->db->where('br.BranchStatus', 0);
        $query = $this->db->get()->result();

        $return = array();

        foreach($query as $row){
            if(in_array($row->AccountTypeName,array('Revenue','Expense','COGS / COGM (Cost Of Goods Made)','Other Revenue','Other Expense'))){
                $return[] = $row;
            }else{
                if($row->CurrencyCode == $this->HCurrency){
                    $return[] = $row;
                }
            }
        }

        return $return;
    }

    function get_max_period($no_piutang) {
        $this->db->select('max(PaymentPeriod) as period');
        $this->db->from($this->detail . ' as dt');
        $this->db->join($this->table . ' as gr', 'gr.AInsNumber=dt.AInsNumber', 'inner');
        $this->db->where('dt.ADebtNumber', $no_piutang);
        $this->db->where_not_in('gr.TransStatus', array(3));

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

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

    function get_payment_period($AInsNumber) {
        $this->db->select('dt.PaymentPeriod');
        $this->db->from($this->detail . ' as dt');
        $this->db->where('dt.AInsNumber', $AInsNumber);

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

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

    function get_payment_afiliasi_number($branch, $link_number) {
        $this->db->select('AInjNumber');
        $this->db->from($this->table . ' as gr');
        $this->db->where('gr.LinkNumber', $link_number);
        $this->db->where('gr.BranchCode', $branch);

        $query = $this->db->get()->result_array();
        return $query[0]['AInjNumber'];
    }

    function insert_transsaction($trans_data) {

        $this->db->trans_start();

        /* ======================= insert group transaction ============================ */
        $this->insert_group($trans_data['group_comp_piutang']['data']);
        $this->insert_group($trans_data['group_comp_hutang']['data']);
        /* ====================== end group transaction =============================== */

        /* =========================== update is lock piutang ========================= */
        $this->update_is_lock($trans_data['islock_status']['data'], $trans_data['islock_status']['link_number']);
        /* =========================== end update is lock piutang ===================== */

        /* =========================== insert detail ============================ */
        for ($x = 0; $x < count($trans_data['insert_detail']['data']); $x++) {
            $this->insert_detail($trans_data['insert_detail']['data'][$x]);
        }
        /* =========================== end insert detail ========================== */

        /* ===================== insert journal ================================ */
        for ($x = 0; $x < count($trans_data['insert_journal']['data']); $x++) {
            $this->insert_journal($trans_data['insert_journal']['data'][$x]);
        }
        /* ===================== end insert journal =============================== */

        $this->db->trans_complete();

        if ($this->db->trans_status() === FALSE) {
            return false;
        } else {
            return true;
        }
    }

    function update_transsaction($trans_data) {

        $this->db->trans_start();

        /* ====================== update group ==================== */
        $this->update_group($trans_data['update_group']['data'], $trans_data['update_group']['link_number']);

        /* ======================== delete detail ======================= */
        if ($trans_data['delete_detail']['total'] > 0) {
            for ($x = 0; $x < $trans_data['delete_detail']['total']; $x++) {
                $this->delete_detail($trans_data['delete_detail']['AInjDetailID'][$x]);
            }
        }

        /* ======================= update detail ============================== */
        for ($x = 0; $x < count($trans_data['update_detail']['data']); $x++) {
            $this->update_detail($trans_data['update_detail']['data'][$x], $trans_data['update_detail']['AInjDetailID'][$x]);
        }

        /* ========================= delete journal =============================== */
        $this->m_delete_journal($trans_data['delete_journal']['link_number']);

        /* ===================== insert journal ================================ */
        for ($x = 0; $x < count($trans_data['insert_journal']['data']); $x++) {
            $this->insert_journal($trans_data['insert_journal']['data'][$x]);
        }

        /* ===================== end insert journal =============================== */
        $this->db->trans_complete();

        if ($this->db->trans_status() === FALSE) {
            return false;
        } else {
            return true;
        }
    }

    function approve_transsaction($trans_data) {

        $this->db->trans_start();

        /* ============================================= approval transaction =================================================== */
        $this->update_status_approval($trans_data['approve_data']['link_number'], $trans_data['approve_data']['data']);

        /* =============================================== update_piutang_afiliasi_by_no ========================================= */
        $this->update_piutang_afiliasi_by_no($trans_data['update_piutang']['data'], $trans_data['update_piutang']['link_number']);

        $this->db->trans_complete();

        if ($this->db->trans_status() === FALSE) {
            return false;
        } else {
            return true;
        }
    }

    function reverse_transsaction($trans_data) {

        $this->db->trans_start();

        /* ====================================================== reverse payment ==================================================== */
        $this->update_to_reverse_payment($trans_data['reverse_payment']['link_number'], $trans_data['reverse_payment']['data']);

        /* ====================================================== reverse piutang ==================================================== */
        $this->update_to_reverse_piutang($trans_data['reverse_piutang']['link_number'], $trans_data['reverse_piutang']['data']);

        /* ====================================================== delete reconcile ============================================= */
        $this->delete_reconcile($trans_data['delete_reconcile']['TransNumber']);

        $this->db->trans_complete();

        if ($this->db->trans_status() === FALSE) {
            return false;
        } else {
            return true;
        }
    }

    function suspend_transsaction($trans_data) {

        $this->db->trans_start();

        /* ===================================== suspend payment ============================================ */
        $this->suspend_data($trans_data['suspend_data']['data'], $trans_data['suspend_data']['link_number']);

        /* ===================================== update piutang ============================================ */
        $this->update_is_lock($trans_data['update_piutang']['data'], $trans_data['update_piutang']['link_number']);

        $this->db->trans_complete();

        if ($this->db->trans_status() === FALSE) {
            return false;
        } else {
            return true;
        }
    }

}
