<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
/* 	
 * 	Author 	: Wahyu
 * 	Tanggal : 01-09-2014	
 * 	Revisi 	: 
 */

/* 	
 * 	Author 	: Tagor
 * 	Tanggal : 08-10-2014	
 * 	Revisi 	: 
 */
/*
  Revisi : Fachri

  Revisi : Daswan
 */

class Payment_afiliasi extends CI_Controller {

    // inisialisasi manual
    public $menu_id = 254;
    public $menu_model = 'm_payment_afiliasi';
    public $name = 'Payment Afiliasi';
    public $trans_data = array();
    // inisialisasi otomatis
    public $add = "add";
    public $edit = "edit";
    public $delete = "delete";
    public $to_preview = "to_preview";
    public $to_pdf = "to_pdf";
    public $to_excel = "to_excel";

    public function __construct() {
        parent::__construct();
        $this->load->model('m_menu');
        $this->load->model('m_payment_afiliasi');
        $this->load->model('m_piutang_afiliasi');
        $this->load->model('m_account_journal');
        $this->load->helper('message_helper');
        $this->load->helper('status');
        $this->load->helper('convert_date_helper');
        $this->load->helper('custom_string_helper');
        $this->load->model('login/m_login');
        $is_login = $this->m_login->get_session_id($this->session->userdata('session_id'),$this->session->userdata('username'));
        if($is_login){

        }else{
            $username       = $this->session->userdata('username');
            $session_id     = $this->session->userdata('session_id');
            $this->m_login->delete_session($username,$session_id);
            $this->session->sess_destroy();

            $uri_func = $this->uri->segment(3);
            if(($uri_func === FALSE) || ($uri_func =='add') || ($uri_func =='edit')){
                redirect(base_url() . 'login', 'refresh');
            }else if(in_array($uri_func,array('approve_data','approved_mgr','approved_dir','approve','approve_transsaction','reverse_data','reversed_data','reverse_transsaction','save','save_data','insert','update','update_data','suspend','suspend_data','suspend_transsaction','suspend_mgr','suspend_dir'))){
                $result['status']   = 'session';
                $result['message']  = get_notification('session_destroy',0);
                echo json_encode($result);
                die();
            }
        }
    }

    // Function Index	
    public function index() {
        if ($this->session->userdata("login") == TRUE) {
            $group_menu = $this->session->userdata('group_menu');
            $arr_group_id = $this->m_menu->get_menu_by_group($group_menu, $this->menu_id);
            if (!empty($arr_group_id)) {
                $data['available_menu'] = array_unique($arr_group_id);
                if (!in_array($this->menu_id, $data['available_menu'])) {
                    redirect(base_url() . 'error', 'refresh');
                } else {
                    $group_menu = $this->session->userdata('group_menu');
                    $data['menu_link'] = $this->m_menu->get_menulink($this->menu_id);
                    $data['menu_label'] = $this->m_menu->get_menulabel($this->menu_id);

                    //panggil model hak akses untuk autoriti
                    $data['group_user'] = $this->m_menu->group_user_autority($this->menu_id, $group_menu);

                    $this->load->view('v_header');
                    $this->load->view('cash_and_bank/payment_afiliasi/v_paymentafiliasi', $data);
                    $this->load->view('v_footer');
                }
            } else {
                redirect(base_url() . 'error', 'refresh');
            }
        } else {
            redirect(base_url() . 'login', 'refresh');
        }
    }

    public function ajax() {

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

        $hasil = array();
        /* ===========rows*page tidak boleh melebihi dari jumlah data======= */

        /* =================================post dari find di kiri==================================================== */
        $param = $this->input->post('param');
        $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============================================================ */
        $this->db->join('am_branch', 'am_branch.BranchCode = at_afiliasiinstallment.BranchCode', 'INNER');
        $this->db->join('am_company as FromCompany', 'FromCompany.CompanyCode = at_afiliasiinstallment.CompTransCode', 'INNER');
        $this->db->join('am_company as ToCompany', 'ToCompany.CompanyCode = at_afiliasiinstallment.CompRecpCode', 'INNER');
        $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->join('at_ainsdetail', 'at_ainsdetail.AInsNumber = at_afiliasiinstallment.AInsNumber', 'INNER');
        $this->db->join('am_employee', 'at_afiliasiinstallment.CreatedBy=am_employee.EmployeeCode', 'LEFT');
        /* ========================jika kondisi tidak sama dengan kosong di find lakukan proses di if================= */
        if ($BranchCode != "") {
            $this->db->where('at_afiliasiinstallment.BranchCode', $BranchCode);
        } else {
            $this->db->where('at_afiliasiinstallment.BranchCode', $this->session->userdata('branch'));
        }

        /* ===================================find by trasnfer from dan transfer to========================================================= */
        // if($CompanyNameFrom != ''){ $this->db->like('FromCompany.CompanyName', $CompanyNameFrom); }
        // if($CompanyNameTo != ''){ $this->db->like('ToCompany.CompanyName', $CompanyNameTo); }
        /* =============================================end=================================================================== */
        if ($TransStatus != '') {
            $this->db->where_in('at_afiliasiinstallment.TransStatus', $TransStatus);
        } else {
            /*
              default adalah 2,6. Finished dan Need Approval
             */
            $this->db->where_in('at_afiliasiinstallment.TransStatus', array(0, 1, 4, 5, 6, 7, 8));
        }


        if ((($DateFrom != '') && ($DateTo != ''))) {
            if ($DateFrom <= $DateTo) {
                $this->db->where('at_afiliasiinstallment.AInsDate >=', date("Y-m-d", strtotime($DateFrom))); //change
                $this->db->where('at_afiliasiinstallment.AInsDate <=', date("Y-m-d", strtotime($DateTo))); //change
            }
        }

        $this->db->where('Payment_Type', 0);
        /* ==========================end================================================================= */

        $count_data = $this->db->count_all_results('at_afiliasiinstallment'); //change
        $offset = $count_data - ($rows * $page);
        if ($offset > 0) {
            $rows = $rows;
        } else {
            $rows = $count_data - ($rows * ($page - 1));
        }
        /* ========================end======================================= */
        $result = $this->m_payment_afiliasi->get_all(); //change

        for ($i = 0; $i <= ($rows - 1); $i++) {
            // kalau master ambil dari get_status_master(), kalau transaksi get_status()
            $status = $result[$i]->status;
            $TransStatus = get_status($status);

            $hasil[] = array(
                'CompTransName' => $result[$i]->CompTransName,
                'CompRecptName' => $result[$i]->CompRecptName,
                'TransNumber' => $result[$i]->AInsNumber,
                'ADebtNumber' => $result[$i]->ADebtNumber,
                'AInsDate' => $result[$i]->AInsDate,
                'APayAmount' => $result[$i]->APayAmount,
                'APayDesc' => $result[$i]->APayDesc,
                'LinkNumber' => $result[$i]->LinkNumber,
                'PaymentPeriod' => $result[$i]->PaymentPeriod,
                'TransStatus' => $TransStatus,
                'BranchName' => $result[$i]->BranchName,
                'BankFrom' => $result[$i]->BankFrom,
                'BankTo' => $result[$i]->BankTo,
                'CreatedBy' => $result[$i]->Created
            );
        }

        if (empty($hasil)) {
            $emptyVar = array();
            echo json_encode($emptyVar);
        } else {
            $result = array();
            $result['total'] = $count_data;
            $result['rows'] = $hasil;
            echo json_encode($result);
        }
    }

    public function get_payment_afiliasi_number() {
        $preg_date = $this->input->post('Payment_af_date') == '' ? date('d-m-Y') : $this->input->post('Payment_af_date');
        $payment_af_number = $this->m_payment_afiliasi->create_payment_afiliasi_number($preg_date);

//        print_r($data['sq_number1']);

        $result['status'] = true;
        $result['number'] = $payment_af_number;
        echo json_encode($result);
    }

    function get_company($comp_code) {
        $result = $this->m_payment_afiliasi->get_company_bycompanycode($comp_code);

        echo json_encode($result);
    }

    function get_bank($comp_code) {
        $result = $this->m_payment_afiliasi->get_bank_bycompany($comp_code);
        echo json_encode($result);
    }

    //function get last status
    function get_status_transaki() {
        $trans_number = $this->input->post('number');
        $get_number = $this->m_payment_afiliasi->get_all_by_no($trans_number);

        foreach ($get_number as $row) {
            $payafiliasi_number[] = $row->TransStatus;
        }

        $data = array(
            'payafiliasi_number' => $payafiliasi_number
        );
        echo json_encode($data);
    }

    public function list_piutangafiliasi() {
        $sess_company = $this->session->userdata('company');
        $sess_branch = $this->session->userdata('branch');
        $sess_branch_status = $this->session->userdata('branch_status');

        $total_piutang = 0;
        $amount_piutang = 0;
        $hasil = "";
        $result = $this->m_payment_afiliasi->list_piutangafiliasi($sess_company, $sess_branch, $sess_branch_status);
        //echo $this->db->last_query();
        foreach ($result as $row) {
            $amount_piutang = $row->ADebtAmount;
            $journal_voucher_balance = $row->JVoucherBalance;
            $check_payment_to_piutang_no = $this->m_payment_afiliasi->check_payment_to_piutang_no($row->ADebtNumber, $sess_branch);

            if (!empty($check_payment_to_piutang_no)) {
                $total_payment = $check_payment_to_piutang_no->total_payment;
                $BalanceAmount = $amount_piutang - ($total_payment + $journal_voucher_balance);
            } else {
                $BalanceAmount = 0;
            }

            if ($row->AdebtIsLock == 1) {
                $IsLock = "Locked";
            } else {
                $IsLock = "Un Locked";
            }

            $hasil[] = array(
                'BalanceAmount' => $BalanceAmount,
                'ADebtAmount' => $row->ADebtAmount,
                'CompFrom' => $row->CompFrom,
                'BankFrom' => $row->BankFrom,
                'CompTo' => $row->CompTo,
                'BankTo' => $row->BankTo,
                'ADebtNumber' => $row->ADebtNumber,
                'CompFromCode' => $row->CompFromCode,
                'CompToCode' => $row->CompToCode,
                'BankFromCode' => $row->BankFromCode,
                'BankToCode' => $row->BankToCode,
                'ChequeNumber' => $row->ChequeNumber,
                'IsLock' => $IsLock,
                'CqTermStatus' => $row->CqTermStatus,
                'ADebtDesc' => $row->ADebtDesc
            );
        }
        if (!empty($hasil)) {
            echo json_encode($hasil);
        } else {
            $emptyVar = array($hasil);
            echo json_encode($emptyVar);
        }
    }

    //function datafrid from piutang afiliasi
    function grid_bypiutang_no() {
        $piutang_no = $this->uri->segment(4);
        $piutang1 = str_replace('.', '/', $piutang_no);
        $piutang2 = str_replace('.', '/', $piutang1);
        $result = $this->m_payment_afiliasi->get_by_piutang($piutang2);
        echo json_encode($result);
    }

    public function add($no_piutang = null) {
        if ($this->session->userdata("login") == TRUE) {
            $group_menu = $this->session->userdata('group_menu');
            /*
              untuk di index/list = get_menu_by_group,
              untuk di add = get_menu_by_group_add
              untuk di edit = get_menu_by_group_edit
             */
            $arr_group_id = $this->m_menu->get_menu_by_group_add($group_menu, $this->menu_id);
            //--------------------------end-----------------------------------------------------------
            if (!empty($arr_group_id)) {
                $data['available_menu'] = array_unique($arr_group_id);
                if (!in_array($this->menu_id, $data['available_menu'])) {
                    redirect(base_url() . 'error', 'refresh');
                } else {
                    /*
                      modul untuk akses add masing masing modul berebda
                      jangan lupa copy $data nya (jika dia bener langsung ke add)
                     */
                    $group_menu = $this->session->userdata('group_menu');
                    $data['menu_link'] = $this->m_menu->get_menulink($this->menu_id);
                    $data['menu_label'] = $this->m_menu->get_menulabel($this->menu_id);

                    $data['payment_afiliasi_number'] = $this->m_payment_afiliasi->create_payment_afiliasi_number(date('d-m-Y'), $this->session->userdata('branch'));
                    $data['date_now'] = date('d-m-Y');

                    /* ======================= open langsung dari piutang jika  ====================== */
                    if ($no_piutang != null) {

                        $no_piutang = str_replace($data['menu_link'] . '/add/', "", $this->uri->uri_string());

                        $data['no_piutang'] = $this->m_payment_afiliasi->get_adebtnumber_branch_hutang($no_piutang);
                    } else {

                        $data['no_piutang'] = '';
                    }

                    //panggil model hak akses untuk autoriti
                    $data['group_user'] = $this->m_menu->group_user_autority($this->menu_id, $group_menu);

                    $this->load->view('v_header');
                    $this->load->view('list/cash_and_bank/payment_afiliasi/v_new_paymentafiliasi', $data);
                    $this->load->view('v_footer');
                    //----------------------end--------------------------------------------------------		
                }
            } else {
                redirect(base_url() . 'error', 'refresh');
            }
        } else {
            redirect(base_url() . 'login', 'refresh');
        }
    }

    public function proses_tutup() {
        $this->db->trans_rollback();
        $hasil = 0;
        $result['status'] = false;
        $result['message'] = get_notification('proses_tutup', $hasil);
        echo json_encode($result);
        die();
    }

    public function insert_group_transaksi($BranchCode, $txt_payment_afiliasi_date, $ainsnomer, $txt_payment_for, $cmb_fromcompany, $cmb_bankfrom, $cmb_tocompany, $cmb_bankto, $txt_cheque_no, $txt_cheque_date, $txa_memo, $txt_amount, $link_number) {

        $data = array(
            'BranchCode' => $BranchCode,
            'AInsDate' => date('Y-m-d', strtotime($txt_payment_afiliasi_date)),
            'AInsNumber' => $ainsnomer,
            'ADebtNumber' => $txt_payment_for,
            'CompTransCode' => $cmb_fromcompany,
            'BankTransfer' => $cmb_bankfrom,
            'CompRecpCode' => $cmb_tocompany,
            'BankReceipt' => $cmb_bankto,
            'ChequeNumber' => $txt_cheque_no,
            'ChequeDate' => date('Y-m-d', strtotime($txt_cheque_date)),
            'APayDesc' => $txa_memo,
            'APayAmount' => $txt_amount,
            'Payment_Type' => 0,
            'TransStatus' => 5,
            'CreditStatus' => 1,
            'LinkNumber' => $link_number,
            'CreatedBy' => $this->session->userdata("username"),
            'CreatedAt' => date('Y-m-d H:i:s'),
            'LastUser' => $this->session->userdata("username")
        );

        $this->trans_data['group_comp_piutang']['data'] = $data;
    }

    public function insert_group_receipt($BranchCode, $txt_payment_afiliasi_date, $payment_afiliasi_no, $txt_payment_for, $cmb_fromcompany, $cmb_bankfrom, $cmb_tocompany, $cmb_bankto, $txt_cheque_no, $txt_cheque_date, $txa_memo, $txt_amount, $link_number) {
        $data_from = array(
            'BranchCode' => $BranchCode, //BrchCodeCompTo->BranchCode,
            'AInsDate' => date('Y-m-d', strtotime($txt_payment_afiliasi_date)),
            'AInsNumber' => $payment_afiliasi_no,
            'ADebtNumber' => $txt_payment_for,
            'CompTransCode' => $cmb_fromcompany,
            'BankTransfer' => $cmb_bankfrom,
            'CompRecpCode' => $cmb_tocompany,
            'BankReceipt' => $cmb_bankto,
            'ChequeNumber' => $txt_cheque_no,
            'ChequeDate' => date('Y-m-d', strtotime($txt_cheque_date)),
            'APayDesc' => $txa_memo,
            'APayAmount' => $txt_amount,
            'Payment_Type' => 0,
            'TransStatus' => 5,
            'CreditStatus' => 0,
            'LinkNumber' => $link_number,
            'CreatedBy' => $this->session->userdata("username"),
            'CreatedAt' => date('Y-m-d H:i:s'),
            'LastUser' => $this->session->userdata("username")
        );

        $this->trans_data['group_comp_hutang']['data'] = $data_from;
    }

    public function insert_detail($payment_afiliasi_no, $txt_payment_for, $loan_payment, $period, $payment_per_period, $payment_balance) {
        $detail_data = array(
            'AInsNumber' => $payment_afiliasi_no,
            'ADebtNumber' => $txt_payment_for,
            'LoanPayment' => $loan_payment,
            'PaymentPeriod' => $period,
            'PaymentPerPeriod' => $payment_per_period,
            'PaymentBalance' => $payment_balance,
            'LastUser' => $this->session->userdata('username')
        );

        $this->trans_data['insert_detail']['data'][] = $detail_data;
    }

    public function insert_journal($txt_payment_afiliasi_date, $payment_afiliasi_no_from, $account_hutang_afiliasi, $cmb_bankfrom, $payment_afiliasi_no_to, $cmb_bankto, $account_piutang_afiliasi, $txt_amount, $txa_memo, $link_number) {
        $data_journal_paymen_afiliasi = array(
            'AInsDate' => date('Y-m-d', strtotime($txt_payment_afiliasi_date)),
            'AInsNumber' => $payment_afiliasi_no_to,
            'AInsAccountNumber' => $cmb_bankto,
            'AInsSaldoDebet' => $txt_amount,
            'AInsSaldoCredit' => 0,
            'AInsDesc' => $txa_memo,
            'AInsSignStatus' => 0,
            'LinkNumber' => $link_number,
            'AinsStatus' => 0,
            'LastUser' => $this->session->userdata("username")
        );

        $this->trans_data['insert_journal']['data'][] = $data_journal_paymen_afiliasi;

        $data_journal_transfer_credit = array(
            'AInsDate' => date('Y-m-d', strtotime($txt_payment_afiliasi_date)),
            'AInsNumber' => $payment_afiliasi_no_to,
            'AInsAccountNumber' => $account_piutang_afiliasi,
            'AInsSaldoDebet' => 0,
            'AInsSaldoCredit' => $txt_amount,
            'AInsDesc' => $txa_memo,
            'AInsSignStatus' => 1,
            'LinkNumber' => $link_number,
            'AinsStatus' => 1,
            'LastUser' => $this->session->userdata("username")
        );

        $this->trans_data['insert_journal']['data'][] = $data_journal_transfer_credit;

        $data_journal_transfer_debet = array(
            'AInsDate' => date('Y-m-d', strtotime($txt_payment_afiliasi_date)),
            'AInsNumber' => $payment_afiliasi_no_from,
            'AInsAccountNumber' => $account_hutang_afiliasi,
            'AInsSaldoDebet' => $txt_amount,
            'AInsSaldoCredit' => 0,
            'AInsDesc' => $txa_memo,
            'AInsSignStatus' => 1,
            'LinkNumber' => $link_number,
            'AinsStatus' => 2,
            'LastUser' => $this->session->userdata("username")
        );

        $this->trans_data['insert_journal']['data'][] = $data_journal_transfer_debet;

        $data_journal_recip_afiliasi = array(
            'AInsDate' => date('Y-m-d', strtotime($txt_payment_afiliasi_date)),
            'AInsNumber' => $payment_afiliasi_no_from,
            'AInsAccountNumber' => $cmb_bankfrom,
            'AInsSaldoDebet' => 0,
            'AInsSaldoCredit' => $txt_amount,
            'AInsDesc' => $txa_memo,
            'AInsSignStatus' => 1,
            'AinsStatus' => 3,
            'LinkNumber' => $link_number,
            'LastUser' => $this->session->userdata("username")
        );

        $this->trans_data['insert_journal']['data'][] = $data_journal_recip_afiliasi;
    }

    public function insert() {

        $txt_payment_for = $this->input->post('txt_payment_for');
        $txt_payment_afiliasi_date = $this->input->post('txt_payment_afiliasi_date');

        $cmb_fromcompany = $this->input->post('cmb_fromcompany');
        $cmb_bankfrom = $this->input->post('cmb_bankfrom');
        $cmb_tocompany = $this->input->post('cmb_tocompany');
        $cmb_bankto = $this->input->post('cmb_bankto');
        $txt_cheque_no = $this->input->post('txt_cheque_no');
        $txt_cheque_date = $this->input->post('txt_cheque_date');
        $txa_memo = $this->input->post('txa_memo');
        $txt_amount = $this->input->post('txt_amount');

        $payment_balance = $this->input->post('payment_balance');


        $loan_payment = $this->input->post('loan_payment');
        $payment_per_period = $txt_amount;
        $result_insert = 0;

        /* ==================================== get branch from n branch to ================================= */
        $BrchCodeCompFrom = $this->m_payment_afiliasi->branch_code_comp_to($cmb_fromcompany);
        $BrchCodeCompTo = $this->session->userdata('branch');

        $arr_company = array($cmb_fromcompany, $cmb_tocompany);
        $arr_branch = array($BrchCodeCompFrom, $BrchCodeCompTo);
        $arr_type_afiliasi = array('PaymentAfiliasi', 'ReceiptAfiliasi');

        /* ===================================== Fungsi create afiliasi no ============================== */
        $payment_afiliasi_no_from = $this->m_payment_afiliasi->create_payment_afiliasi_number($txt_payment_afiliasi_date, $BrchCodeCompFrom);
        $payment_afiliasi_no_to = $this->m_payment_afiliasi->create_payment_afiliasi_number($txt_payment_afiliasi_date, $BrchCodeCompTo);

        /* ================================= Function get payment last ============================================ */
        $max_payment_period = $this->m_payment_afiliasi->get_max_payment_period($txt_payment_for);
        $payment_period = $this->m_payment_afiliasi->get_period_payment($payment_afiliasi_no_to);

        if (($max_payment_period == 0) or ( empty($max_payment_period))) {
            $period = 1;
        } else {
            $period = $max_payment_period + 1;
        }

        /* ========================= get link number piutang afiliasi ======================================== */
        $link_number_adeb = $this->m_payment_afiliasi->cek_at_adebt_number($txt_payment_for);

        $data_islock_status = array(
            'AdebtIsLock' => 1
        );

        $link_number = $payment_afiliasi_no_to;

        /* =================================== get Account Number Payment Afiliasi ======================================== */
        $cek_sys_otheraccount_paymen_afiliasi = $this->m_payment_afiliasi->get_sys_otheraccount_afiliasi($arr_company, $arr_branch, $arr_type_afiliasi);

        /* ==================================== get account hutang payment afiliasi ==================================================== */
        $account_hutang_afiliasi = $this->m_payment_afiliasi->get_sys_otheraccount_payment_receipt_afiliasi($cmb_tocompany, $BrchCodeCompTo, 'PaymentAfiliasi');

        /* ==================================== get account piutang payment afiliasi ==================================================== */
        $account_piutang_afiliasi = $this->m_payment_afiliasi->get_sys_otheraccount_payment_receipt_afiliasi($cmb_fromcompany, $BrchCodeCompFrom, 'ReceiptAfiliasi');


        if (count($cek_sys_otheraccount_paymen_afiliasi) < 8) {
            $result['status'] = false;
            $result['message'] = get_notification('preference', 0);
            echo json_encode($result);
        } else {

            /* ======================================================== Fungsi Insert group payment ===================================================== */
            $this->insert_group_transaksi($BrchCodeCompFrom, $txt_payment_afiliasi_date, $payment_afiliasi_no_from, $txt_payment_for, $cmb_fromcompany, $cmb_bankfrom, $cmb_tocompany, $cmb_bankto, $txt_cheque_no, $txt_cheque_date, $txa_memo, $txt_amount, $link_number);

            /* ======================================================== Fungsi Insert group receipt ===================================================== */
            $this->insert_group_receipt($BrchCodeCompTo, $txt_payment_afiliasi_date, $payment_afiliasi_no_to, $txt_payment_for, $cmb_fromcompany, $cmb_bankfrom, $cmb_tocompany, $cmb_bankto, $txt_cheque_no, $txt_cheque_date, $txa_memo, $txt_amount, $link_number);

            /* ============================================== update piutang afiliasi to lock ================================== */
            $this->trans_data['islock_status']['data'] = $data_islock_status;
            $this->trans_data['islock_status']['link_number'] = $link_number_adeb;

            /* ============================================= insert group detail =============================================== */
            $this->insert_detail($payment_afiliasi_no_to, $txt_payment_for, $loan_payment, $period, $payment_per_period, $payment_balance);

            /* ============================================ insert journal======================================================================== */
            $this->insert_journal($txt_payment_afiliasi_date, $payment_afiliasi_no_from, $account_hutang_afiliasi, $cmb_bankfrom, $payment_afiliasi_no_to, $cmb_bankto, $account_piutang_afiliasi, $txt_amount, $txa_memo, $link_number);

            $result_return = $this->m_payment_afiliasi->insert_transsaction($this->trans_data);

            $this->trans_data = [];

            if ($result_return) {
                $result['status'] = TRUE;
                $result['message'] = get_notification_transaksi($this->name, 'insert', 1, $payment_afiliasi_no_from);
            } else {
                $result['status'] = FALSE;
                $result['message'] = get_notification_transaksi($this->name, 'insert', 0, $payment_afiliasi_no_from);
            }

            echo json_encode($result);
        }
    }

    function edit() {
        if ($this->session->userdata("login") == TRUE) {
            $group_menu = $this->session->userdata('group_menu');
            /*
              untuk di index/list = get_menu_by_group,
              untuk di add = get_menu_by_group_add
              untuk di edit = get_menu_by_group_edit
             */
            $arr_group_id = $this->m_menu->get_menu_by_group_edit($group_menu, $this->menu_id);
            //--------------------------end-----------------------------------------------------------
            if (!empty($arr_group_id)) {
                $data['available_menu'] = array_unique($arr_group_id);
                if (!in_array($this->menu_id, $data['available_menu'])) {
                    redirect(base_url() . 'error', 'refresh');
                } else {
                    /*
                      modul untuk akses add masing masing modul berebda
                      jangan lupa copy $data nya (jika dia bener langsung ke add)
                     */
                    $group_menu = $this->session->userdata('group_menu');
                    $data['menu_label'] = $this->m_menu->get_menulabel($this->menu_id);
                    $data['menu_link'] = $this->m_menu->get_menulink($this->menu_id);
                    $pay_afil_numb = str_replace($data['menu_link'] . '/edit/', "", $this->uri->uri_string());

                    $data['payment'] = $this->m_payment_afiliasi->get_payment($pay_afil_numb);

                    //panggil model hak akses untuk autoriti
                    $data['group_user'] = $this->m_menu->group_user_autority($this->menu_id, $group_menu);

                    $this->load->view('v_header');
                    $this->load->view('list/cash_and_bank/payment_afiliasi/v_edit_paymentafiliasi', $data);
                    $this->load->view('v_footer');
                    //----------------------end--------------------------------------------------------		
                }
            } else {
                redirect(base_url() . 'error', 'refresh');
            }
        } else {
            redirect(base_url() . 'login', 'refresh');
        }
    }

    public function update_group($txt_payment_afiliasi_date, $cmb_bankfrom, $cmb_bankto, $txt_cheque_no, $txt_cheque_date, $txa_memo, $txt_amount, $link_number) {
        $data_group = array(
            'AInsDate' => date('Y-m-d', strtotime($txt_payment_afiliasi_date)),
            'BankTransfer' => $cmb_bankfrom,
            'BankReceipt' => $cmb_bankto,
            'ChequeNumber' => $txt_cheque_no,
            'ChequeDate' => date('Y-m-d', strtotime($txt_cheque_date)),
            'APayDesc' => $txa_memo,
            'APayAmount' => $txt_amount,
            'LastUser' => $this->session->userdata("username")
        );

        $this->trans_data['update_group']['data'] = $data_group;
        $this->trans_data['update_group']['link_number'] = $link_number;
    }

    public function update_detail($detail, $payment_afiliasi_no, $txt_payment_for) {
        if ($detail) {
            foreach ($detail as $row_2) {
                $LoanPayment = $row_2->LoanPayment;
                $PaymentPeriod = $row_2->PaymentPeriod;
                $PaymentPerPeriod = $row_2->PaymentPerPeriod;
                $PaymentBalance = $row_2->PaymentBalance;

                $detail_data = array(
                    'PaymentPerPeriod' => $PaymentPerPeriod,
                    'PaymentBalance' => $PaymentBalance
                );

                /* ================================== Update detail =========================== */
                $this->trans_data['update_detail']['data'][] = $detail_data;
                $this->trans_data['update_detail']['TransNumber'][] = $payment_afiliasi_no;
            }
        }
    }

    function update() {
        $branch_code_hidden = $this->input->post('branch_code_hidden');
        $txt_payment_for = $this->input->post('txt_payment_for');
        $payment_afiliasi_no_to = $this->input->post('payment_afiliasi_no');
        $cmb_bankfrom = $this->input->post('cmb_bankfrom');
        $com_tran = $this->input->post('CompTransCode');
        $com_rec = $this->input->post('CompRecpCode');
        $txt_payment_afiliasi_date = $this->input->post('txt_payment_afiliasi_date');
        $cmb_bankto = $this->input->post('cmb_bankto');
        $txt_cheque_no = $this->input->post('txt_cheque_no');
        $txt_cheque_date = $this->input->post('txt_cheque_date');
        $txa_memo = $this->input->post('txa_memo');
        $bank_from = $this->input->post('BankFrom');
        $bank_to = $this->input->post('BankTo');
        $link_number = $this->input->post('link_id');
        $txt_amount = $this->input->post('txt_amount');
        $pay_period = $this->input->post('payment_period');
        $pay_balance = $this->input->post('payment_balance');


        $detail = $this->input->post('detail');
        $detail = json_decode($detail);

        $cmb_fromcompany = $this->input->post('cmb_fromcompany');
        $BrchCodeCompFrom = $this->m_payment_afiliasi->branch_code_comp_to($cmb_fromcompany);
        $cmb_tocompany = $this->session->userdata('company');
        $BrchCodeCompTo = $this->session->userdata('branch');
        $sess_branch_status = $this->session->userdata('branch_status');

        $arr_company = array($cmb_fromcompany, $cmb_tocompany);
        $arr_branch = array($BrchCodeCompFrom, $BrchCodeCompTo);
        $arr_type_afiliasi = array('PaymentAfiliasi', 'ReceiptAfiliasi');
        $arr_company_gl = array();

        $get_sum_paymen = $this->m_payment_afiliasi->get_sum_paymen($txt_payment_for);
        $total = 0;

        $payment_afiliasi_no_from = $this->m_payment_afiliasi->get_payment_number($link_number);

        /* =================================== get Account Number Payment Afiliasi ======================================== */
        $cek_sys_otheraccount_paymen_afiliasi = $this->m_payment_afiliasi->get_sys_otheraccount_afiliasi($arr_company, $arr_branch, $arr_type_afiliasi);

        /* ==================================== get account hutang payment afiliasi ==================================================== */
        $account_hutang_afiliasi = $this->m_payment_afiliasi->get_sys_otheraccount_payment_receipt_afiliasi($cmb_tocompany, $BrchCodeCompTo, 'PaymentAfiliasi');

        /* ==================================== get account piutang payment afiliasi ==================================================== */
        $account_piutang_afiliasi = $this->m_payment_afiliasi->get_sys_otheraccount_payment_receipt_afiliasi($cmb_fromcompany, $BrchCodeCompFrom, 'ReceiptAfiliasi');

        /* ================================= 8 adalah 4 total piutang dari company a dan b n 4 total hutang dari company a dan b ====================== */
        if (count($cek_sys_otheraccount_paymen_afiliasi) < 8) {
            $result['status'] = false;
            $result['message'] = get_notification('preference', 0);
            echo json_encode($result);
        } else {

            /* ======================================== Update Group ================================== */
            $this->update_group($txt_payment_afiliasi_date, $cmb_bankfrom, $cmb_bankto, $txt_cheque_no, $txt_cheque_date, $txa_memo, $txt_amount, $link_number);

            /* ======================================= Update detail ================================== */
            $this->update_detail($detail, $payment_afiliasi_no_to, $txt_payment_for);

            /* ======================================= Delete Journal ================================= */
            $this->trans_data['delete_journal']['link_number'] = $link_number;

            /* ============================================ insert journal======================================================================== */
            $this->insert_journal($txt_payment_afiliasi_date, $payment_afiliasi_no_from, $account_hutang_afiliasi, $cmb_bankfrom, $payment_afiliasi_no_to, $cmb_bankto, $account_piutang_afiliasi, $txt_amount, $txa_memo, $link_number);

            $result_return = $this->m_payment_afiliasi->update_transsaction($this->trans_data);

            $this->trans_data = [];

            if ($result_return) {
                $result['status'] = true;
                $result['message'] = get_notification_transaksi($this->name, 'update', 1, $payment_afiliasi_no_from);
            } else {
                $result['status'] = false;
                $result['message'] = get_notification_transaksi($this->name, 'update', 0, $payment_afiliasi_no_from);
            }

            echo json_encode($result);
        }
    }

    public function approved_dir() {

        $branch = $this->session->userdata('branch');
        $link_number = $this->input->post('link_id');

        /* =========================== piutang number =========================== */
        $txt_payment_for = $this->input->post('txt_payment_for');

        $pay_balance = $this->input->post('payment_balance');
        $app_level2_status = 1;
        $app_level2_code = $this->session->userdata('username');
        $app_level2_date = date('Y-m-d');

        $data_approval = array(
            'TransStatus' => 2,
            'AppLevel2Status' => $app_level2_status,
            'AppLevel2Code' => $app_level2_code,
            'AppLevel2Date' => $app_level2_date,
            'AppFinished' => 1
        );

        /* ================================== set data to update adebtIsLock to 1 ======================= */
        $data_islock_status = array(
            'AdebtIsLock' => 0
        );

        /* ================================== get link number from piutang afiliasi ================ */
        $link_number_piutang = $this->m_payment_afiliasi->cek_at_adebt_number($txt_payment_for);

        /* ================================= piutang ============================================= */
        $piutang = $this->m_payment_afiliasi->m_get_total_piutang($txt_payment_for);

        /* ================================= get total payment ========================================== */
        $total_payment = $this->m_payment_afiliasi->m_get_total_payment($txt_payment_for, $branch);

        $cek_payment = $piutang['ADebtAmount'] - ($total_payment + $piutang['JVoucherBalance']);

        /* ================================== check status piutang afiliais =============================== */
        /* ================================== true = status 1, false = status bukan 1 =========================== */
        $check_status_piutang_afiliasi = $this->m_payment_afiliasi->check_status_piutang_afiliasi($txt_payment_for);

        if ($check_status_piutang_afiliasi == false) {

            $result['status'] = false;
            $result['message'] = get_notification('approved_transaksi', 0);
            echo json_encode($result);
        } elseif ($cek_payment < 0) {

            $result['status'] = false;
            $result['message'] = get_notification('JVB', 0);
            echo json_encode($result);
        } else {

            /* ========================= Fungsi Approval data direktur =============================== */
            $this->trans_data['approve_data']['data'] = $data_approval;
            $this->trans_data['approve_data']['link_number'] = $link_number;

            /* ========================= Fungsi Update IsLock piutang afiliasi to un lock =================== */
            $this->trans_data['islock_status']['data'] = $data_islock_status;
            $this->trans_data['islock_status']['link_number'] = $link_number_piutang;

            /* ======================== update piutang to finished ==================== */
            $this->trans_data['update_piutang']['total'] = false;
            if ($pay_balance == 0) {
                $this->trans_data['update_piutang']['total'] = true;
                $data_piutang = array(
                    'TransStatus' => 2
                );
                $this->trans_data['update_piutang']['data'] = $data_piutang;
                $this->trans_data['update_piutang']['link_number'] = $link_number_piutang;
            }

            $result_return = $this->m_payment_afiliasi->approve_transsaction($this->trans_data);

            $this->trans_data = [];


            if ($result_return) {
                $result['status'] = true;
                $result['message'] = get_notification_transaksi($this->name, 'approve', 1, $link_number);
            } else {
                $result['status'] = false;
                $result['message'] = get_notification_transaksi($this->name, 'approve', 0, $link_number);
            }

            echo json_encode($result);
        }
    }

    /* ===================================Fungsi reverse transaksi========================================== */

    public function reversed_data() {

        /* =================================== LINK NUMBER = AInsNumber branch yang sedang login ================================== */
        /* ===================================Deklarasi Variabel untuk proses insert atau update=================================== */
        /* =================================== $id berisi link_number dan payment for yang di pisah dengan koma ========================== */
        $id = $this->input->post('id');

        $arr_id = explode(',', $id);

        /* ======================= link number payment afiliasi ============================== */
        $link_number = $arr_id[0];

        /* ============================== no piutang afiliasi =============================== */
        $txt_payment_for = $arr_id[1];

        $data_reversed_payment = array(
            'AppLevel2Status' => 0,
            'AppLevel2Code' => null,
            'AppLevel2Date' => 0,
            'TransStatus' => 5,
            'AppFinished' => 0,
        );

        $data_reversed_piutang = array(
            'TransStatus' => 1,
            'AdebtIsLock' => 1
        );


        /* ================================== get link number from piutang afiliasi ================ */
        $link_number_piutang = $this->m_payment_afiliasi->cek_at_adebt_number($txt_payment_for);

        /* =============================== get max period ========================================= */
        $max_period = $this->m_payment_afiliasi->get_max_period($txt_payment_for);


        /* ============ LINK NUMBER = AInsNumber branch yang sedang login ============= */
        $payment_period = $this->m_payment_afiliasi->get_payment_period($link_number);

        /* ================================== payment number ======================================= */
        $no_payment = $this->m_payment_afiliasi->get_payment_number($link_number);

        $arr_no = explode("/", $no_payment);
        $no_transaction = substr($arr_no[1], 0, 4);
        $month_transaction = (int) substr($no_transaction, 0, 2);
        $year_transaction = ((int) substr($no_transaction, 2, 2)) + 2000;

        $date_transaction = $year_transaction . $month_transaction;

        /* ============================== check period end ======================================== */
        $check_period = $this->m_payment_afiliasi->check_period_end($date_transaction);

        /* ============================== check payment journal =================================== */
        $cek_payment_journal = $this->m_payment_afiliasi->check_payment_afiliasi_journal($txt_payment_for);
    
        if ($check_period == FALSE) {

            if ($payment_period == $max_period) {

                if ($cek_payment_journal == FALSE) {

                    /* =============================update at_afiliasiinstallment/ reverse =================================== */
                    $this->trans_data['reverse_payment']['data'] = $data_reversed_payment;
                    $this->trans_data['reverse_payment']['link_number'] = $link_number;

                    /* =============================update adebt/ reverse =================================== */
                    $this->trans_data['reverse_piutang']['data'] = $data_reversed_piutang;
                    $this->trans_data['reverse_piutang']['link_number'] = $link_number_piutang;

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

                    $result_return = $this->m_payment_afiliasi->reverse_transsaction($this->trans_data);

                    $this->trans_data = [];

                    if ($result_return) {
                        $result['status'] = true;
                        $result['message'] = get_notification_transaksi($this->name, 'reverse', 1, $link_number);
                    } else {
                        $result['status'] = false;
                        $result['message'] = get_notification_transaksi($this->name, 'reverse', 0, $link_number);
                    }

                    echo json_encode($result);
                } else {

                    /* =================================== get no payment afiliasi ==================================================== */
                    $sess_branch            = $this->session->userdata('branch');
                    $no_payment_afiliasi    = $this->m_payment_afiliasi->get_no_payment_afiliasi_journal($txt_payment_for, $sess_branch);
                    $result['status']       = false;
                    $result['message']      = get_notification('reverse_transaksi', 0) . ' ' . $no_payment_afiliasi;
                    echo json_encode($result);
                }
            } else {

                $result['status'] = false;
                $result['message'] = get_notification('max_period', 0);
                echo json_encode($result);
            }
        } else {
            $result['status'] = false;
            $result['message'] = get_notification('reverse_in_period', 0);
            echo json_encode($result);
        }
    }

    function payment_detail() {

        $uri4 = $this->uri->segment(4);
        $uri5 = $this->uri->segment(5);
        $uri6 = $this->uri->segment(6);
        $uri7 = $this->uri->segment(7); //payment_period
        $uri8 = $this->uri->segment(8);
        $uri9 = $this->uri->segment(9);
        $uri10 = $this->uri->segment(10);
        $uri11 = $this->uri->segment(11);

        $ADebtNumber = $uri4 . "/" . $uri5 . "/" . $uri6;
        $ainsnumber = $uri9 . "/" . $uri10 . "/" . $uri11;

        $payment_detail = $this->m_payment_afiliasi->get_payment_detail($ADebtNumber, $uri7, $uri8, $ainsnumber);
        //echo $this->db->last_query();

        echo json_encode($payment_detail);
    }

    public function suspend() {
        $id = $this->input->post('id');

        $hasil = 0;
        $data = array(
            'TransStatus' => 3,
            'LastUser' => $this->session->userdata('username')
        );

        $get_link_number_suspend = $this->m_payment_afiliasi->get_link_number_suspen($id);
        $txt_payment_for = $get_link_number_suspend->ADebtNumber;

        $cek_adeb_transstatus_2 = $this->m_payment_afiliasi->cek_at_adebt_number($txt_payment_for);
        $link_number_adeb_2 = $cek_adeb_transstatus_2;

        $data_islock_status = array(
            'AdebtIsLock' => 0
        );

        $this->db->trans_begin();
        $hasil = $this->m_payment_afiliasi->update_is_lock($data_islock_status, $link_number_adeb_2);
        if ($hasil == 0) {
            $this->proses_tutup();
        }

        $hasil = $this->m_payment_afiliasi->suspend($data, $id);
        if ($hasil == 0) {
            $this->proses_tutup();
        }

        if ($hasil == 0) {
            $this->db->trans_rollback();
            $result['status'] = false;
            $result['message'] = get_notification('suspend', $hasil);
        } else {
            $this->db->trans_commit();
            $result['status'] = true;
            $result['message'] = get_notification('suspend', $hasil);
        }
        echo json_encode($result);
    }

    public function suspend_data() {
        $link_number = $this->input->post('id');
        $hasil = 0;

        $data_suspend = array(
            'TransStatus' => 3,
            'LastUser' => $this->session->userdata('username')
        );

        /* =========================== get number afiliasi piutang ================================= */
        $get_afiliasi_number_suspend = $this->m_payment_afiliasi->get_link_number_suspen($link_number);
        $txt_payment_for = $get_afiliasi_number_suspend->ADebtNumber;

        /* =========================== get link number piutang afiliasi ============================= */
        $cek_adeb_transstatus_2 = $this->m_payment_afiliasi->cek_at_adebt_number($txt_payment_for);
        $link_number_adeb_2 = $cek_adeb_transstatus_2;

        $data_islock_status = array(
            'AdebtIsLock' => 0
        );

        $this->trans_data['suspend_data']['data'] = $data_suspend;
        $this->trans_data['suspend_data']['link_number'] = trim($link_number);

        $this->trans_data['update_piutang']['data'] = $data_islock_status;
        $this->trans_data['update_piutang']['link_number'] = trim($link_number_adeb_2);

        $result_return = $this->m_payment_afiliasi->suspend_transsaction($this->trans_data);

        $this->trans_data = [];

        if ($result_return) {
            $result['status'] = true;
            $result['message'] = get_notification_transaksi($this->name, 'suspend', 1, $link_number);
        } else {
            $result['status'] = false;
            $result['message'] = get_notification_transaksi($this->name, 'suspend', 0, $link_number);
        }

        echo json_encode($result);
    }

    //Function view print data
    public function to_preview($obj_string = null) {
        $data['company'] = $this->m_payment_afiliasi->get_company_by_branch($this->session->userdata('branch'));
        //$data['list']		= $this->m_payment_afiliasi->get_all_to_print($this->session->userdata('branch'));
        $data['menu_id']        = $this->menu_id;
        $data['menu_label']     = $this->m_menu->get_menulabel($this->menu_id);
        $data['menu_link']      = $this->m_menu->get_menulink($this->menu_id);
        
        $data_filter            = arrayParamInUrlToStr($obj_string);
        $data['list']           = $this->m_payment_afiliasi->get_all_to_print($data_filter);
        
        $this->load->view('v_header');
        $this->load->view('list/cash_and_bank/payment_afiliasi/v_print_paymentafiliasi', $data);
        $this->load->view('v_footer');
    }
    public function to_preview_LAMA($data_grid = null) {
        $data['company'] = $this->m_payment_afiliasi->get_company_by_branch($this->session->userdata('branch'));
        //$data['list']		= $this->m_payment_afiliasi->get_all_to_print($this->session->userdata('branch'));
        $data_grid = str_replace('__', '/', $data_grid);
        $data_grid = str_replace('_', '-', $data_grid);
        $data_grid = str_replace('%20', ' ', $data_grid);
        $arr_grid_lv1 = explode('linebaru', $data_grid);

        $obj_string = '[';

        for ($x = 0; $x < count($arr_grid_lv1) - 1; $x++) {
            $arr_data_lv1 = $arr_grid_lv1[$x];
            $arr_grid_lv2 = explode('spasi', $arr_data_lv1);

            $obj_string .= '{';
            for ($i = 0; $i < count($arr_grid_lv2); $i++) {
                $arr_grid_lv3 = explode(':', $arr_grid_lv2[$i]);
                if ($i < count($arr_grid_lv2) - 1) {
                    $obj_string .= '"' . $arr_grid_lv3[0] . '":"' . $arr_grid_lv3[1] . '",';
                } else {
                    $obj_string .= '"' . $arr_grid_lv3[0] . '":"' . $arr_grid_lv3[1] . '"';
                }
            }

            if ($x < count($arr_grid_lv1) - 2) {
                $obj_string .= '},';
            } else {
                $obj_string .= '}';
            }
        }

        $obj_string .= ']';

        $data['list'] = json_decode($obj_string);
        $this->load->view('v_header');
        $this->load->view('list/cash_and_bank/payment_afiliasi/v_print_paymentafiliasi', $data);
        $this->load->view('v_footer');
    }

    //Function view print data by afiliasi number
    public function to_preview_by_payment_number() {
        //$id = $this->input->post('id');
        $uri4 = $this->uri->segment(4);
        $uri5 = $this->uri->segment(5);
        $uri6 = $this->uri->segment(6);
        $id = $uri4 . '/' . $uri5 . '/' . $uri6;

        $data['company'] = $this->m_payment_afiliasi->get_company_by_branch($this->session->userdata('branch'));
        $data['list'] = $this->m_payment_afiliasi->get_by_payment_number($id);
        $this->load->view('v_header');
        $this->load->view('list/cash_and_bank/payment_afiliasi/v_print_paymentafiliasi', $data);
        $this->load->view('v_footer');
    }

    public function to_journal() {
        $data['menu_label'] = $this->m_menu->get_menulabel($this->menu_id);
        $data['menu_link'] = $this->m_menu->get_menulink($this->menu_id);

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

        $payment_number = str_replace($data['menu_link'] . '/to_journal/', "", $this->uri->uri_string());

        $get_company_from_n_to = $this->m_payment_afiliasi->get_company_from_n_to($payment_number);

        $data['akun_payment_from'] = $this->m_payment_afiliasi->akun_payment_afiliasi($get_company_from_n_to->CompRecpCode);

        $data['akun_payment_to'] = $this->m_payment_afiliasi->akun_penerimaan_afiliasi($get_company_from_n_to->CompTransCode);

        $data['branch_by_company_from'] = $this->m_payment_afiliasi->branch_code_by_company_transfer($get_company_from_n_to->CompRecpCode);
        $data['branch_by_company_to'] = $this->m_payment_afiliasi->branch_code_by_company_transfer($get_company_from_n_to->CompTransCode);

        $data['detail_to_journal'] = $this->m_payment_afiliasi->detail_to_journal($payment_number, $sess_company, $sess_branch);

        $this->load->view('v_header');
        $this->load->view('cash_and_bank/payment_afiliasi/v_journal_paymentafiliasi', $data);
        $this->load->view('v_footer');
    }

    public function to_print_detail() {

        $data['menu_link'] = $this->m_menu->get_menulink($this->menu_id);
        $data['menu_label'] = $this->m_menu->get_menulabel($this->menu_id);

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

        $id = str_replace($data['menu_link'] . '/to_print_detail/', "", $this->uri->uri_string());

        if ($id != "") {

            /* ===================== fungsi get link number ================================== */
            $link_number = $this->m_payment_afiliasi->get_link_number($id);

            $data['list_transaksi'] = $this->m_payment_afiliasi->get_detail_item_print($link_number, $BranchCode, '=');
            $data['list_receipt'] = $this->m_payment_afiliasi->get_detail_item_print($link_number, $BranchCode, '!=');

            // echo $this->db->last_query();

            $this->load->view('v_header');
            $this->load->view('cash_and_bank/payment_afiliasi/v_print_detail_payment_afiliasi', $data);
            $this->load->view('v_footer');
        }
    }

    function get_bank_balance() {
        $AccountNumber = $this->input->post('AccountNumber');
        $branch = $this->session->userdata('branch');

        $bank_balance = $this->m_account_journal->m_get_bank_balance($branch, $AccountNumber);

        if ($bank_balance == false) {
            $return = array('message' => false, 'value' => 0);
        } else {
            $return = array('message' => true, 'value' => $bank_balance);
        }

        echo json_encode($return);
    }

}
