<?php

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

class Loan_funding extends CI_Controller {

    //initial 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 $name = "Loan Funding";
    public $trans_data = array();
    //initial menu id dari tabel m_menu
    public $menu_id = 387;

    public function __construct() {
        parent::__construct();
        $this->load->model('m_loan_funding');
        $this->load->model('m_account_journal');
        $this->load->helper('message');
        $this->load->helper('status');
        $this->load->helper('convert_date');
        $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');
            /*
              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($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');
                    // get link from tbl_menu 
                    $data['menu_link'] = $this->m_menu->get_menulink($this->menu_id);

                    // set data
                    $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/loan_funding/v_loan_funding', $data);
                    $this->load->view('v_footer');
                    //----------------------end--------------------------------------------------------		
                }
            } else {
                redirect(base_url() . 'error', 'refresh');
            }
        } else {
            redirect(base_url() . 'login', 'refresh');
        }
    }

    //Function Menampilkan Data Pada Data Grid View
    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');
        $Number = isset($param['txt_IDNumber']) ? $param['txt_IDNumber'] : '';
        $Desc = isset($param['txt_Description']) ? $param['txt_Description'] : '';

        $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============================================================ */
        /* ========================jika kondisi tidak sama dengan kosong di find lakukan proses di if================= */
        if ($BranchCode != "") {
            $this->db->where('BranchCode', $BranchCode);
        } else {
            $this->db->where('BranchCode', $this->session->userdata('branch'));
        }

        if ($Number != "") {
            $this->db->like('LFNumber', $Number);
        }
        if ($Desc != "") {
            $this->db->like('LFDesc', $Desc);
        }
        if ($TransStatus != '') {
            $this->db->where_in('TransStatus', $TransStatus);
        } else {
            /*
              default adalah 2,6. Finished dan Need Approval
             */
            $this->db->where_in('TransStatus', array(0, 1, 4, 5, 6, 7, 8));
        }


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

        $count_data = $this->db->count_all_results('at_loanfunding');

        $offset = $count_data - ($rows * $page);

        if ($offset > 0) {
            $rows = $rows;
        } else {
            $rows = $count_data - ($rows * ($page - 1));
        }
        /* ========================end======================================= */
        $result = $this->m_loan_funding->get_all();

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

            $hasil[] = array(
                'TransNumber' => $result[$i]->LFNumber,
                'LFDate' => $result[$i]->LFDate,
                'LoanAccount' => $result[$i]->Loan_Account,
                'ReceiptAccount' => $result[$i]->Receipt_Account,
                'LFAmount' => $result[$i]->LFAmount,
                'LFDesc' => $result[$i]->LFDesc,
                'TransStatus' => $TransStatus,
                'CreatedBy' => $result[$i]->created_name
            );
        }


        $result = array();
        $result['total'] = $count_data;
        $result['rows'] = $hasil;
        echo json_encode($result);
    }

    public function add() {
        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');
                    // get link from tbl_menu 
                    $data['menu_link'] = $this->m_menu->get_menulink($this->menu_id);
                    // set data
                    $data['menu_label'] = $this->m_menu->get_menulabel($this->menu_id);

                    $data['date_now'] = date('d-m-Y');

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

                    // set for view
                    $this->load->view('v_header');
                    $this->load->view('cash_and_bank/loan_funding/v_new_loan_funding', $data);
                    $this->load->view('v_footer');
                    //----------------------end--------------------------------------------------------		
                }
            } else {
                redirect(base_url() . 'error', 'refresh');
            }
        } else {
            redirect(base_url() . 'login', 'refresh');
        }
    }

    public function to_preview_list() {
        // set data
        $data['list'] = $this->m_loan_funding->get_all();

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

    public function to_print_list() {
        // set data
        $data['list'] = $this->m_loan_funding->get_all();

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

    //Function view print data
    public function to_preview($obj_string = null) {
        $data['company'] = $this->m_loan_funding->get_company_by_branch($this->session->userdata('branch'));
        //$data['list'] 		= $this->m_loan_funding->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_loan_funding->get_all_to_print($data_filter);
        
        $this->load->view('v_header');
        $this->load->view('cash_and_bank/loan_funding/v_print_loan_funding', $data);
        $this->load->view('v_footer');
    }
    public function to_preview_LAMA($data_grid = null) {
        $data['company'] = $this->m_loan_funding->get_company_by_branch($this->session->userdata('branch'));
        //$data['list'] 		= $this->m_loan_funding->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('cash_and_bank/loan_funding/v_print_loan_funding', $data);
        $this->load->view('v_footer');
    }

    public function get_loan_funding_number() {
        $preg_date = $this->input->post('loan_funding_date') == '' ? date('d-m-Y') : $this->input->post('loan_funding_date');

        $karyawan_number = $this->m_loan_funding->create_loan_funding_number($preg_date);

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

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

    public function get_company_edit($branch) {
        $branch_explode = str_replace('_', '-', $branch);
        $list_company = $this->m_loan_funding->get_company_edit($branch_explode);
        echo"$list_company";
    }

    public function get_akun_bank_edit($BranchCode) {
        $branch_explode = str_replace('_', '-', $BranchCode);
        $list_akun = $this->m_loan_funding->get_akun_bank_edit($branch_explode);
        echo $list_akun;
    }

    public function get_karyawan_edit($BranchCode) {
        $branch_explode = str_replace('_', '-', $BranchCode);
        $list_karyawan = $this->m_loan_funding->get_karyawan_bycompany_edit($branch_explode);
        echo $list_karyawan;
    }

    public function get_loan_account() {

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

        $loan_account = $this->m_loan_funding->m_get_loan_account($branch);

        echo "$loan_account";
    }

    public function get_receipt_account() {

        $branch = $this->session->userdata('branch');
        $CurrencyCode = ($this->input->post('CurrencyCode')) ? $this->input->post('CurrencyCode') : '';

        $receipt_account = $this->m_loan_funding->m_get_receipt_account($branch, $CurrencyCode);

        echo "$receipt_account";
    }

    public function get_akun_bank($BranchCode) {
        $list_akun = $this->m_loan_funding->get_akun_bank($BranchCode);
        echo $list_akun;
    }

    public function get_karyawan($BranchCode) {
        $company_perusahaan = $this->session->userdata('company');
        $branch = $this->session->userdata('branch');
        $list_karyawan = $this->m_loan_funding->get_karyawan_bycompany($branch, $company_perusahaan);
        echo $list_karyawan;
    }

    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($branch, $txt_date, $LFNumber, $loan_account, $receipt_account, $txt_desc, $txt_amount, $txt_rate, $currency_code,$cheque_number) {

        $data_piutang = array(
            'BranchCode' => $branch,
            'LFDate' => $txt_date,
            'LFNumber' => $LFNumber,
            'LFAccount' => $loan_account,
            'ReceiptAccount' => $receipt_account,
            'LFDesc' => $txt_desc,
            'LFAmount' => $txt_amount,
            'CurrencyRate' => $txt_rate,
            'CurrencyCode' => $currency_code,
            'ChequeNumber' => $cheque_number,
            'TransStatus' => 5,
            'LFIsLock' => 0,
            'CreatedBy' => $this->session->userdata('username'),
            'CreatedAt' => date('Y-m-d H:i:s'),
            'LastUser' => $this->session->userdata('username')
        );

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

    public function insert_journal($txt_date, $LFNumber, $loan_account, $receipt_account, $txt_amount, $txt_desc, $txt_rate) {

        $journal_debet = array(
            'LFDate' => $txt_date,
            'LFNumber' => $LFNumber,
            'LFAccountNumber' => $receipt_account,
            'LFSaldoDebet' => $txt_amount * $txt_rate,
            'LFSaldoCredit' => 0,
            'AmountCurrency' => $txt_amount,
            'LFDesc' => $txt_desc,
            'LFSignStatus' => 0,
            'LastUser' => $this->session->userdata('username')
        );

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

        $journal_credit = array(
            'LFDate' => $txt_date,
            'LFNumber' => $LFNumber,
            'LFAccountNumber' => $loan_account,
            'LFSaldoDebet' => 0,
            'LFSaldoCredit' => $txt_amount * $txt_rate,
            'AmountCurrency' => $txt_amount,
            'LFDesc' => $txt_desc,
            'LFSignStatus' => 0,
            'LastUser' => $this->session->userdata('username')
        );

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

    public function insert() {

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

        $loan_account = $this->input->post('cmb_loan_account');
        $receipt_account = $this->input->post('cmb_receipt_account');
        $txt_date = date("Y-m-d", strtotime($this->input->post('txt_loan_date')));

        $txt_desc = $this->input->post('txa_description');
        $txt_amount = $this->input->post('txt_amount');
        $txt_rate = $this->input->post('txt_rate');
        $cheque_number = $this->input->post('cheque_number');
        $currency_code = $this->input->post('currency_code');

        /* =============================== Create number loan payment ============================= */
        $LFNumber = $this->m_loan_funding->create_loan_funding_number($txt_date);

        /* ================================================= set insert group =================================================== */
        $this->insert_group($branch, $txt_date, $LFNumber, $loan_account, $receipt_account, $txt_desc, $txt_amount, $txt_rate, $currency_code,$cheque_number);

        /* ===================================== set insert journal ========================================= */
        $this->insert_journal($txt_date, $LFNumber, $loan_account, $receipt_account, $txt_amount, $txt_desc, $txt_rate);

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

        $this->trans_data = [];

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

        echo json_encode($result);
    }

    public 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');
                    // get link from tbl_menu 
                    $data['menu_link'] = $this->m_menu->get_menulink($this->menu_id);
                    // set data
                    $CompanyCode = $this->session->userdata('company');
                    $data['menu_label'] = $this->m_menu->get_menulabel($this->menu_id);

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

                    $data['detail'] = $this->m_loan_funding->get_detail($id);

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

                    // set for view
                    $this->load->view('v_header');
                    $this->load->view('cash_and_bank/loan_funding/v_edit_loan_funding', $data);
                    $this->load->view('v_footer');
                    //----------------------end--------------------------------------------------------		
                }
            } else {
                redirect(base_url() . 'error', 'refresh');
            }
        } else {
            redirect(base_url() . 'login', 'refresh');
        }
    }

    public function update_group($branch, $txt_date, $LFNumber, $loan_account, $receipt_account, $txt_desc, $txt_amount, $txt_rate, $currency_code,$cheque_number) {
        $data_loan = array(
            'BranchCode' => $branch,
            'LFAccount' => $loan_account,
            'ReceiptAccount' => $receipt_account,
            'LFDesc' => $txt_desc,
            'LFAmount' => $txt_amount,
            'CurrencyRate' => $txt_rate,
            'CurrencyCode' => $currency_code,
            'ChequeNumber' => $cheque_number,
            'LastUser' => $this->session->userdata('username')
        );

        $this->trans_data['update_group']['data'] = $data_loan;
        $this->trans_data['update_group']['TransNumber'] = $LFNumber;
    }

    public function update() {
        $branch = $this->session->userdata('branch');

        $loan_account = $this->input->post('cmb_loan_account');
        $receipt_account = $this->input->post('cmb_receipt_account');
        $txt_date = date("Y-m-d", strtotime($this->input->post('txt_loan_date')));

        $txt_desc = $this->input->post('txa_description');
        $txt_amount = $this->input->post('txt_amount');
        $txt_rate = $this->input->post('txt_rate');
        $currency_code = $this->input->post('currency_code');
        $LFNumber = $this->input->post('LFNumber');
        $cheque_number = $this->input->post('cheque_number');

        /* ========================================= set update group ================================ */
        $this->update_group($branch, $txt_date, $LFNumber, $loan_account, $receipt_account, $txt_desc, $txt_amount, $txt_rate, $currency_code,$cheque_number);

        /* ============================================ set delete journal ================================= */
        $this->trans_data['delete_journal']['TransNumber'] = $LFNumber;

        /* ============================================set insert journal =================================== */
        $this->insert_journal($txt_date, $LFNumber, $loan_account, $receipt_account, $txt_amount, $txt_desc, $txt_rate);

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

        $this->trans_data = [];

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

        echo json_encode($result);
    }

    /* =================================Approv Manager ============================================ */

    function approved_dir() {
        $LFNumber = $this->input->post('LFNumber');
        $data = array(
            "TransStatus" => 1,
            "AppFinished" => 1,
            "AppLevel2Status" => 1,
            "AppLevel2Code" => $this->session->userdata('username'),
            "AppLevel2Date" => date('Y-m-d'),
            "LFIsLock" => 0,
            "LastUser" => $this->session->userdata('username')
        );

        /* =============================Fungsi Update payment =================================== */
        $this->trans_data['approve_data']['data'] = $data;
        $this->trans_data['approve_data']['TransNumber'] = $LFNumber;

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

        $this->trans_data = [];

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

        echo json_encode($result);
    }

    public function reversed_data() {
        /* ===================================Deklarasi Variabel untuk proses insert atau update=================================== */
        $LFNumber = $this->input->post('id');

        $arr_no = explode("/", $LFNumber);
        $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;

        $data_loan = array(
            "TransStatus" => 5,
            "AppFinished" => 0,
            "AppLevel2Status" => 0,
            "LFIsLock" => 1
        );

        /* ==================================== cek payment karyawan ================================= */
        $cek_loan_payment = $this->m_loan_funding->m_cek_loan_payment($LFNumber);
        $cek_loan_payment_journal = $this->m_loan_funding->m_cek_loan_payment_journal($LFNumber);

        if ($cek_loan_payment) {

            /* =================================== get no payment ==================================================== */
            $no_loan_payment = $this->m_loan_funding->get_no_loan_payment($LFNumber);

            $result['status'] = false;
            $result['message'] = get_notification('reverse_transaksi', 0) . ' ' . $no_loan_payment;
            echo json_encode($result);
        } else if ($cek_loan_payment_journal) {
            /* =================================== get no payment ==================================================== */
            $no_loan_payment_journal = $this->m_loan_funding->get_no_loan_payment_journal($LFNumber);

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

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

            if ($check_period == false) {

                /* =================== set update piutang karyawan/ reverse =================================== */
                $this->trans_data['reverse_data']['data'] = $data_loan;
                $this->trans_data['reverse_data']['TransNumber'] = $LFNumber;

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

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

                $this->trans_data = [];

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

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

    public function to_journal() {
        $CompanyCode = $this->session->userdata('company');
        $BranchCode = $this->session->userdata('branch');

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

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


        $group_journal = $this->m_loan_funding->get_date_journal_loan($LFNumber);
        $list_journal = $this->m_loan_funding->get_all_journal_loan($LFNumber, $BranchCode);

        $data['group_journal'] = json_decode($group_journal);
        $data['list_journal'] = json_decode($list_journal);

        $this->load->view('v_header');
        $this->load->view('cash_and_bank/loan_funding/v_print_journal_loan_funding', $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');

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

        if ($piutang_no != "") {

            $data['list_transaksi'] = $this->m_loan_funding->get_detail_item_print($piutang_no, $BranchCode, '=');
            // $data['list_receipt'] 		= $this->m_piutang_afiliasi->get_detail_item_print($piutang_no,$BranchCode,'!='); 
            // echo $this->db->last_query();

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

    public function suspend_data() {
        $LFNumber = $this->input->post('id');
        //$status 	= $this->input->post('trans_status');

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

        /* ==================== update suspend data ================== */
        $this->trans_data['suspend_data']['data'] = $data_suspend;
        $this->trans_data['suspend_data']['TransNumber'] = $LFNumber;

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

        $this->trans_data = [];

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

        echo json_encode($result);
    }

}
