<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/* 	
 * 	Author 	: Herpan
 * 	Tanggal : 20-07-2014	
 * 	Revisi 	: 
 */

class Transaksi_antar_kas extends CI_Controller {

    public $menu_id = 239;
    public $menu_model = 'm_transfer_to_petty_cash';
    public $name = 'Transfer To Petty Cash';
    public $trans_data = array();
    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('login/m_login');
        $this->load->model('m_transfer_to_petty_cash');
        $this->load->model('m_account_journal');
        $this->load->helper('regex');
        $this->load->helper('message');
        $this->load->helper('status');
        $this->load->helper('convert_date_helper');
        $this->load->helper('custom_string_helper');

        $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();
            }
        }
    }

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

    //Function Menampilkan Data Pada Datagrid
    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->like('BranchCode', $BranchCode);
        } else {
            $this->db->where('BranchCode', $this->session->userdata('branch'));
        }

        if ($Number != "") {
            $this->db->like('TCashNumber', $Number);
        }
        if ($Desc != "") {
            $this->db->like('TCashDesc', $Desc);
        }

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

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

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

        for ($i = 0; $i < count($result); $i++) {

            $TransStatus = get_status($result[$i]['TransCashStatus']);

            $hasil[] = array(
                'TransNumber' => $result[$i]['TCashNumber'],
                'BankFrom' => $result[$i]['BankFrom'],
                'BankTo' => $result[$i]['BankTo'],
                'TCashDate' => $result[$i]['TCashDate'],
                'TransAmount' => $result[$i]['TransAmount'],
                'TCashDesc' => $result[$i]['TCashDesc'],
                'TransStatus' => $TransStatus,
                '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_bank_bycompany($branch_code) {
        $str_replace_branch = str_replace('_', '-', $branch_code);
        $result = $this->m_transfer_to_petty_cash->get_bank_bycompany($str_replace_branch);
        //echo $this->db->last_query();
        echo json_encode($result);
    }

    public function get_petty_cash_bycompany($branch_code, $currency_idr, $currency_code) {
        $str_replace_branch = str_replace('_', '-', $branch_code);
        $result = $this->m_transfer_to_petty_cash->get_petty_cash_bycompany($str_replace_branch, $currency_idr, $currency_code);
        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');
                    $data['menu_link'] = $this->m_menu->get_menulink($this->menu_id);
                    $data['menu_label'] = $this->m_menu->get_menulabel($this->menu_id);
                    $sess_brach = $this->session->userdata('branch');

                    //$data['transaksi_antar_kas']  = $this->m_transfer_to_petty_cash->create_paty_cash_number(date('d-m-Y'));
                    $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);

                    $this->load->view('v_header');
                    $this->load->view('list/cash_and_bank/transaksi_antar_kas/v_new_transaksi_antar_kas', $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 get_transaksi_number() {
        $preg_date = $this->input->post('Patycash_date') == '' ? date('d-m-Y') : $this->input->post('Patycash_date');

        $paty_cash_number = $this->m_transfer_to_petty_cash->create_paty_cash_number($preg_date);

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

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

    public function insert() {
        $txt_jv_date = $this->input->post('txt_jv_date');
        $cmb_bank_accountfrom = $this->input->post('cmb_bank_accountfrom');
        $cmb_bank_accountto = $this->input->post('cmb_bank_accountto');

        $txa_memo = $this->input->post('txa_memo');
        $txt_rate = $this->input->post('txt_rate');
        $txt_amount = $this->input->post('txt_amount');
        $cheque_number = $this->input->post('cheque_number');
        $currency_code = $this->input->post('currency_code');
        $currency_code_to = $this->input->post('currency_code_to');
        $result_insert = 0;
        $branch = $this->session->userdata('branch');

        /* ======================= Create number pettycash ======================================== */
        $trans_to_petty_cash_no = $this->m_transfer_to_petty_cash->create_tcash_number($txt_jv_date);

        /* ===================================Fungsi Hitung Currency ======================================= */
        $total_currency = $this->count_currency($currency_code, $currency_code_to, $txt_amount, $txt_rate);

        /* ============================================ Fungsi insert ke table TransferCash========================================================================================= */
        $this->insert_group($branch, $txt_jv_date, $trans_to_petty_cash_no, $cmb_bank_accountfrom, $cmb_bank_accountto, $txa_memo, $currency_code, $currency_code_to, $txt_rate, $txt_amount,$cheque_number);

        /* ============================================ Fungsi insert ke table journal    ======================================== */
        $this->insert_journal($txt_jv_date, $trans_to_petty_cash_no, $cmb_bank_accountfrom, $cmb_bank_accountto, $total_currency['total'], $total_currency['total_debet'], $total_currency['total_credit'], $txa_memo);

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

        $this->trans_data = [];

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

        echo json_encode($result);
    }

    public function count_currency($currency_code, $currency_code_to, $txt_amount, $txt_rate) {
        ///perhitungan usd to usd, idr to idr, idr to usd, sud to idr
        if ($currency_code == $currency_code_to) {
            $total = $txt_amount * $txt_rate;

            $total_debet = $txt_amount;
            $total_credit = $txt_amount;
        } else {
            if ($currency_code_to == "IDR") {
                $total = $txt_amount;
                $total_debet = $txt_amount;
                $total_credit = $txt_amount / $txt_rate;
            } else {
                $total = $txt_amount * $txt_rate;
                $total_debet = $txt_amount;
                $total_credit = $txt_amount * $txt_rate;
            }
        }
        //end////////////////////////////////////////////////

        $return['total'] = $total;
        $return['total_debet'] = $total_debet;
        $return['total_credit'] = $total_credit;
        return $return;
    }

    public function insert_group($branch, $txt_jv_date, $trans_to_petty_cash_no, $cmb_bank_accountfrom, $cmb_bank_accountto, $txa_memo, $currency_code, $currency_code_to, $txt_rate, $txt_amount,$cheque_number) {
        $data_group = array(
            'BranchCode' => $branch,
            'TCashDate' => date('Y-m-d', strtotime($txt_jv_date)),
            'TCashNumber' => $trans_to_petty_cash_no,
            'BankTransfer' => $cmb_bank_accountfrom,
            'BankReceipt' => $cmb_bank_accountto,
            'ChequeNumber' => $cheque_number,
            'TCashDesc' => $txa_memo,
            'CurrencyCode' => $currency_code,
            'CurrencyCode_to' => $currency_code_to,
            'CurrencyRate' => $txt_rate,
            'TransAmount' => $txt_amount,
            'TransStatus' => 4,
            '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_group;
    }

    public function insert_journal($txt_jv_date, $trans_to_petty_cash_no, $cmb_bank_accountfrom, $cmb_bank_accountto, $total, $total_debet, $total_credit, $txa_memo) {
        //inisialiasi to journal
        $data_to_journal_debet = array(
            'TCashDate' => date('Y-m-d', strtotime($txt_jv_date)),
            'TCashNumber' => $trans_to_petty_cash_no,
            'TCashAccountNumber' => $cmb_bank_accountto,
            'TCashSaldoDebet' => $total,
            'TCashSaldoCredit' => '',
            'TCashDesc' => $txa_memo,
            'AmountCurrency' => $total_debet,
            'TCashSignStatus' => 0,
            'LastUser' => $this->session->userdata('username')
        );

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

        $data_to_journal_credit = array(
            'TCashDate' => date('Y-m-d', strtotime($txt_jv_date)),
            'TCashNumber' => $trans_to_petty_cash_no,
            'TCashAccountNumber' => $cmb_bank_accountfrom,
            'TCashSaldoDebet' => '',
            'TCashSaldoCredit' => $total,
            'TCashDesc' => $txa_memo,
            'AmountCurrency' => $total_credit,
            'TCashSignStatus' => 1,
            'LastUser' => $this->session->userdata('username')
        );

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

    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');
                    $data['menu_label'] = $this->m_menu->get_menulabel($this->menu_id);
                    $data['menu_link'] = $this->m_menu->get_menulink($this->menu_id);

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

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

                    $data['transaksi_antar_kas'] = $this->m_transfer_to_petty_cash->get_transaksi($tcash_number);
                    $data['id_journal'] = $this->m_transfer_to_petty_cash->get_id_journal($tcash_number);

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

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

        $branch_code_hidden = $this->input->post('branch_code_hidden');
        $cmb_bank_accountfrom = $this->input->post('cmb_bank_accountfrom');
        $trans_to_petty_cash_no = $this->input->post('trans_to_petty_cash_no');
        $cmb_bank_accountto = $this->input->post('cmb_bank_accountto');
        $txt_jv_date = $this->input->post('txt_jv_date');
        $txa_memo = $this->input->post('txa_memo');
        $txt_amount = $this->input->post('txt_amount');
        $chk_suspend = $this->input->post('copy_suspend');
        $code_appmgrspv = $this->input->post('code_appmgrspv');
        $txt_rate = $this->input->post('txt_rate');
        $currency_code_to = $this->input->post('currency_code_to');
        $currency_code = $this->input->post('currency_code');
        $id_journal = $this->input->post('id_journnal');
        $cheque_number = $this->input->post('cheque_number');

        $app_level_stat = null;
        $app_level_code = null;
        $app_level_date = null;
        $app_finished = null;
        $result_insert = 0;
        /* ===================================Fungsi Hitung Currency ======================================= */
        $total_currency = $this->count_currency($currency_code, $currency_code_to, $txt_amount, $txt_rate);

        /* ========================================= Fungsi update group ========================================================== */
        $this->update_group($branch_code_hidden, $txt_jv_date, $trans_to_petty_cash_no, $cmb_bank_accountfrom, $cmb_bank_accountto, $txa_memo, $txt_amount, $currency_code, $currency_code_to, $txt_rate,$cheque_number);

        $this->trans_data['delete_journal']['TransNumber'] = $trans_to_petty_cash_no;

        /* ============================================ Fungsi insert ke table journal    ======================================== */
        $this->insert_journal($txt_jv_date, $trans_to_petty_cash_no, $cmb_bank_accountfrom, $cmb_bank_accountto, $total_currency['total'], $total_currency['total_debet'], $total_currency['total_credit'], $txa_memo);

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

        $this->trans_data = [];

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

        echo json_encode($result);
    }

    public function update_group($branch_code_hidden, $txt_jv_date, $trans_to_petty_cash_no, $cmb_bank_accountfrom, $cmb_bank_accountto, $txa_memo, $txt_amount, $currency_code, $currency_code_to, $txt_rate,$cheque_number) {

        $data_group = array(
            'BranchCode' => $branch_code_hidden,
            'TCashDate' => date('Y-m-d', strtotime($txt_jv_date)),
            'BankTransfer' => $cmb_bank_accountfrom,
            'BankReceipt' => $cmb_bank_accountto,
            'TCashDesc' => $txa_memo,
            'ChequeNumber' => $cheque_number,
            'TransAmount' => $txt_amount,
            'CurrencyCode' => $currency_code,
            'CurrencyCode_to' => $currency_code_to,
            'CurrencyRate' => $txt_rate,
            'LastUser' => $this->session->userdata('username')
        );

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

    public function approved_mgr() {

        $trans_to_petty_cash_no = $this->input->post('trans_to_petty_cash_no');
        $data_approved = array(
            'AppLevel1Status' => 1,
            'TransStatus' => 2,
            'AppLevel1Code' => $this->session->userdata("username"),
            'AppLevel1Date' => date("Y-m-d"),
            'AppFinished' => 1,
            'LastUser' => $this->session->userdata("username")
        );

        $this->trans_data['approve_data']['data'] = $data_approved;
        $this->trans_data['approve_data']['TransNumber'] = trim($trans_to_petty_cash_no);

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

        $this->trans_data = [];

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

        echo json_encode($result);
    }

    public function suspend() {
        $id = $this->input->post('id');
        $hasil = 0;
        $data = array(
            'TransStatus' => 3,
            'LastUser' => $this->session->userdata('username')
        );
        $this->db->trans_begin();
        $hasil = $this->m_transfer_to_petty_cash->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() {
        $transaction_no = $this->input->post('id');
        $hasil = 0;

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

        $this->trans_data['suspend_data']['data'] = $data;
        $this->trans_data['suspend_data']['TransNumber'] = trim($transaction_no);

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

        $this->trans_data = [];

        if ($result_return) {
            $result['status'] = true;
            $result['message'] = get_notification('suspend', 1);
        } else {
            $result['status'] = false;
            $result['message'] = get_notification('suspend', 0);
        }

        echo json_encode($result);
    }

    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_compay = $this->session->userdata('company');
        $sess_branch = $this->session->userdata('branch');

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

        $data['company'] = $this->m_transfer_to_petty_cash->get_company_by_branch($sess_branch);
        $data['list'] = $this->m_transfer_to_petty_cash->get_datatojournal($tcash_number);
        //echo $this->db->last_query();

        $get_at_transfercash = $this->m_transfer_to_petty_cash->get_datatojournal($tcash_number);
        foreach ($get_at_transfercash as $row_data) {
            $branch_code = $row_data->BranchCode;
        }

        $list_detail = $this->m_transfer_to_petty_cash->get_datatojournal_detail($tcash_number, $sess_branch);
        $data['list_detail'] = $list_detail;
        //echo $this->db->last_query();
        //print_r($list_detail);

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

    // Function Print

    public function to_preview($obj_string = null) {
        $data['company'] = $this->m_transfer_to_petty_cash->get_company_by_branch($this->session->userdata('branch'));
        //$data['list'] 		= $this->m_transfer_to_petty_cash->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_transfer_to_petty_cash->get_all_to_print($data_filter);

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

    public function to_preview_LAMA($data_grid = null) {
        $data['company'] = $this->m_transfer_to_petty_cash->get_company_by_branch($this->session->userdata('branch'));
        //$data['list'] 		= $this->m_transfer_to_petty_cash->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/transaksi_antar_kas/v_print_transaksi_antar_kas', $data);
        $this->load->view('v_footer');
    }

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

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

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

        //$data['company'] 	= $this->m_transfer_to_petty_cash->get_company_by_branch($sess_branch);
        $data['list'] = $this->m_transfer_to_petty_cash->get_to_print($tcash_number);
        //echo $this->db->last_query();

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

    function get_status_transaki() {
        $trans_number = $this->input->post('number');
        $get_number = $this->m_transfer_to_petty_cash->get_all_by_no($trans_number);

        foreach ($get_number as $row) {
            $cash_number[] = $row->trans_cash_stat;
        }

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

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

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

        $arr_no = explode("/", $trans_to_petty_cash_no);
        $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_petty_cash = array(
            "TransStatus" => 4,
            "AppFinished" => 0
        );

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

        if ($check_period == false) {

            $this->trans_data['reverse_data']['data'] = $data_petty_cash;
            $this->trans_data['reverse_data']['TransNumber'] = trim($trans_to_petty_cash_no);

            $this->trans_data['delete_reconcile']['TransNumber'] = trim($trans_to_petty_cash_no);

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

            $this->trans_data = [];

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

    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);
    }

}
