<?php

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

class Deposit extends CI_Controller {

    public $menu_id = 158;
    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 = "Deposit";
    public $trans_data = array();

    public function __construct() {
        parent::__construct();
        $this->load->model('login/m_login');
        $this->load->model('m_deposit');
        $this->load->model('m_account_journal');
        $this->load->helper('message');
        $this->load->helper('status');
        $this->load->model('m_menu');
        $this->load->helper('convert_date');
        $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();
            }
        }
    }

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

    // Function Index	
    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['dep_number'] = $this->m_deposit->create_dep_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);
                    // set for view
                    $this->load->view('v_header');
                    $this->load->view('cash_and_bank/deposit/v_new_deposit', $data);
                    $this->load->view('v_footer');
                    //----------------------end--------------------------------------------------------		
                }
            } 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');
        $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'] : '';

        $beginning = ($page - 1) * $rows;
        $end = $page * $rows;
        /* ============================================end============================================================ */
        /* ========================jika kondisi tidak sama dengan kosong di find lakukan proses di if================= */
        $this->db->join('am_branch', 'at_cashdeposit.BranchCode=am_branch.BranchCode', 'INNER');
        $this->db->join('am_employee', 'at_cashdeposit.CreatedBy=am_employee.EmployeeCode', 'LEFT');

        if ($BranchCode != "") {
            $this->db->where('at_cashdeposit.BranchCode', $BranchCode);
        } else {
            $this->db->where('at_cashdeposit.BranchCode', $this->session->userdata('branch'));
        }

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


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

        /* ==========================end================================================================= */
        // $this->db->limit($end, $beginning);

        $count_data = $this->db->count_all_results('at_cashdeposit'); //change
        //echo $this->db->last_query();

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

        $result = $this->m_deposit->get_all(); //change
        //echo $this->db->last_query();

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

            $hasil[] = array(
                'TransNumber' => $result[$i]->CDepNumber, //change
                'CDepDate' => $result[$i]->CDepDate, //change
                'CDepTotalAmount' => $result[$i]->CDepTotalAmount, //change
                'CDepDesc' => $result[$i]->CDepDesc, //change
                'CreatedBy' => $result[$i]->CreatedBy, //change
                'TransStatus' => $TransStatus
            );
        }

        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 to_preview($obj_string = null) {

        $branch = $this->session->userdata('branch');
        $data['company'] = $this->m_deposit->get_company_by_branch($branch);
        //$data['list'] 		= $this->m_deposit->get_all();
        
        $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_deposit->get_all_to_print($data_filter);
        
        $this->load->view('v_header');
        $this->load->view('cash_and_bank/deposit/v_preview_deposit', $data);
        $this->load->view('v_footer');
    }
    public function to_preview_LAMA($data_grid = null) {

        $branch = $this->session->userdata('branch');
        $data['company'] = $this->m_deposit->get_company_by_branch($branch);
        //$data['list'] 		= $this->m_deposit->get_all();

        $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/deposit/v_preview_deposit', $data);
        $this->load->view('v_footer');
    }

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

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

    public function get_akun_bank_add($branch_code, $currency_idr, $currency_code) {
        $CompanyCode = $this->session->userdata('company');
        $str_replace_branch = str_replace('_', '-', $branch_code);
        $list_bank = $this->m_deposit->get_coa_bank_add($CompanyCode, $str_replace_branch, $currency_idr, $currency_code);
        //echo $this->db->last_query();
        echo"$list_bank";
    }

    public function get_akun_bank($brach_code, $currency_idr, $currency_code) {
        $replace_branch = str_replace('_', '-', $brach_code);

        $CompanyCode = $this->session->userdata('company');
        //$BranchCode 	= $this->session->userdata('branch');
        $list_bank = $this->m_deposit->get_coa_bank($replace_branch, $currency_idr, $currency_code);
        echo"$list_bank";
    }

    public function get_petty_cash_add() {
        $CompanyCode = $this->session->userdata('company');
        $BranchCode = $this->session->userdata('branch');
        $list_petty_cash = $this->m_deposit->get_petty_cash_add($CompanyCode, $BranchCode);
        echo"$list_petty_cash";
    }

    public function get_petty_cash($brach_code) {
        $replace_branch = str_replace('_', '-', $brach_code);

        $CompanyCode = $this->session->userdata('company');
        //$BranchCode 	= $this->session->userdata('branch');
        $list_petty_cash = $this->m_deposit->get_petty_cash($CompanyCode, $replace_branch);
        //echo $this->db->last_query();
        echo"$list_petty_cash";
    }

    public function get_dep_number() {
        $preg_date = $this->input->post('Dep_date') == '' ? date('d-m-Y') : $this->input->post('Dep_date');
        $dep_number = $this->m_deposit->create_dep_number($preg_date);

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

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

    public function proses_tutup() {
        $this->db->trans_rollback();
        die();
    }

    // Function Insert
    public function insert() {
        $BranchCode = $this->session->userdata('branch');

        $bulan = date('m');
        $tahun = date('y');
        $cek_tahun = date('Y');
        $kode = "CD";
        $result_insert = 0;


        $txt_deposit_date = date("Y-m-d", strtotime($this->input->post('txt_deposit_date')));
        /* ===================================Create deposit number================== */
        $txt_deposit_no = $this->m_deposit->create_dep_number($txt_deposit_date);
        /* ===================================end =================================== */

        $cmb_bank_from = $this->input->post('cmb_bank_from');
        $cmb_bank = $this->input->post('cmb_bank');
        $txa_memo = $this->input->post('txa_memo');
        $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');
        $txt_rate = $this->input->post('txt_rate');

        $count_currency = $this->count_currency($currency_code, $currency_code_to, $txt_rate, $txt_amount);

        /* ===================================fungsi insert to at_cashdeposit table================== */
        $this->insert_group($BranchCode, $txt_deposit_date, $txt_deposit_no, $cmb_bank_from, $cmb_bank, $txa_memo, $txt_amount, $currency_code, $currency_code_to, $txt_rate,$cheque_number);
        /* =================================== end ================================================== */

        /* ===================================fungsi insert to at_cdepjournal================== */
        $this->insert_journal($txt_deposit_date, $txt_deposit_no, $cmb_bank, $cmb_bank_from, $count_currency['total'], $count_currency['total_debet'], $count_currency['total_credit'], $txa_memo);
        /* =================================== end ================================================== */

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

        $this->trans_data = [];

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

        echo json_encode($result);
    }

    public function count_currency($currency_code, $currency_code_to, $txt_rate, $txt_amount) {

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

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

        return $return;
    }

    public function insert_group($BranchCode, $txt_deposit_date, $txt_deposit_no, $cmb_bank_from, $cmb_bank, $txa_memo, $txt_amount, $currency_code, $currency_code_to, $txt_rate,$cheque_number) {
        $data_deposit = array(
            'BranchCode' => $BranchCode,
            'CDepDate' => $txt_deposit_date,
            'CDepNumber' => $txt_deposit_no,
            'BankPayment' => $cmb_bank_from,
            'BankReceipt' => $cmb_bank,
            'CqTermStatus' => NULL,
            'CDepDesc' => $txa_memo,
            'CDepTotalAmount' => $txt_amount,
            'CurrencyCode' => $currency_code,
            'CurrencyCode_to' => $currency_code_to,
            'CurrencyRate' => $txt_rate,
            'ChequeNumber' => $cheque_number,
            '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_deposit;
    }

    public function insert_journal($txt_deposit_date, $txt_deposit_no, $cmb_bank, $cmb_bank_from, $total, $total_debet, $total_credit, $txa_memo) {
        $journal_debet = array(
            'CDepDate' => $txt_deposit_date,
            'CDepNumber' => $txt_deposit_no,
            'CDepAccountNumber' => $cmb_bank,
            'CDepSaldoDebet' => $total,
            'CDepSaldoCredit' => 0,
            'AmountCurrency' => $total_debet,
            'CDepDesc' => $txa_memo,
            'CDepSignStatus' => 0,
            'LastUser' => $this->session->userdata('username')
        );

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

        $journal_credit = array(
            'CDepDate' => $txt_deposit_date,
            'CDepNumber' => $txt_deposit_no,
            'CDepAccountNumber' => $cmb_bank_from,
            'CDepSaldoDebet' => 0,
            'CDepSaldoCredit' => $total,
            'AmountCurrency' => $total_credit,
            'CDepDesc' => $txa_memo,
            'CDepSignStatus' => 1,
            'LastUser' => $this->session->userdata('username')
        );

        $this->trans_data['insert_journal']['data'][] = $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');
                    // get link from tbl_menu 
                    $data['menu_link'] = $this->m_menu->get_menulink($this->menu_id);
                    $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_deposit->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/deposit/v_edit_deposit', $data);
                    $this->load->view('v_footer');
                    //----------------------end--------------------------------------------------------		
                }
            } else {
                redirect(base_url() . 'error', 'refresh');
            }
        } else {
            redirect(base_url() . 'login', 'refresh');
        }
    }

    public function update_group($txt_deposit_no, $cmb_bank_from, $cmb_bank, $txa_memo, $currency_code, $currency_code_to, $txt_rate, $txt_amount,$cheque_number) {
        $data_deposit = array(
            'BankPayment' => $cmb_bank_from,
            'BankReceipt' => $cmb_bank,
            'CqTermStatus' => NULL,
            'CDepDesc' => $txa_memo,
            'CurrencyCode' => $currency_code,
            'CurrencyCode_to' => $currency_code_to,
            'CurrencyRate' => $txt_rate,
            'ChequeNumber' => $cheque_number,
            'TransStatus' => 4,
            'CDepTotalAmount' => $txt_amount,
            'LastUser' => $this->session->userdata('username')
        );

        $this->trans_data['update_group']['data'] = $data_deposit;
        $this->trans_data['update_group']['TransNumber'] = $txt_deposit_no;
    }

    // Function Update
    public function update() {
        $BranchCode = $this->session->userdata('branch');
        $txt_deposit_no = $this->input->post('txt_deposit_no');
        $txt_deposit_date = date("Y-m-d", strtotime($this->input->post('txt_deposit_date')));
        $cmb_bank_from = $this->input->post('cmb_bank_from');
        $cmb_bank = $this->input->post('cmb_bank');
        $txa_memo = $this->input->post('txa_memo');
        $cheque_number = $this->input->post('cheque_number');
        $txt_amount = $this->input->post('txt_amount');
        $chk_suspend = $this->input->post('chk_suspend');
        $txt_rate = $this->input->post('txt_rate');
        $currency_code_to = $this->input->post('currency_code_to');
        $currency_code = $this->input->post('currency_code');
        $result_update = 0;

        $count_currency = $this->count_currency($currency_code, $currency_code_to, $txt_rate, $txt_amount);

        /* ===================================fungsi update group=============================================================== */
        $this->update_group($txt_deposit_no, $cmb_bank_from, $cmb_bank, $txa_memo, $currency_code, $currency_code_to, $txt_rate, $txt_amount,$cheque_number);
        /* ============================================end====================================================================== */

        /* ==========================fungsi delete journal=============================================== */
        $this->trans_data['delete_journal']['TransNumber'] = $txt_deposit_no;
        /* ============================================end=============================================== */

        /* ===================================fungsi insert to at_cdepjournal================== */
        $this->insert_journal($txt_deposit_date, $txt_deposit_no, $cmb_bank, $cmb_bank_from, $count_currency['total'], $count_currency['total_debet'], $count_currency['total_credit'], $txa_memo);
        /* =================================== end ================================================== */

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

        $this->trans_data = [];

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

        echo json_encode($result);
    }

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

    public function reversed_data() {

        /* ===================================Deklarasi Variabel untuk proses insert atau update=================================== */
        $txt_deposit_no = $this->input->post('id');

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

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

        if ($check_period == false) {

            /* =============================update at_cashpayment/ reverse =================================== */
            $this->trans_data['reverse_data']['data'] = $data_deposit;
            $this->trans_data['reverse_data']['TransNumber'] = $txt_deposit_no;

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

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

            $this->trans_data = [];

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

    public function suspend() {
        // inisialisasi additional parameter
        $deposit_number = $this->input->post('id');
        $id = str_replace(".", "/", $deposit_number);
        $hasil = 0;

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

        $this->db->trans_begin();
        $cek_status = $this->m_deposit->cek_trans_status($id);

        if ($cek_status == 6 || $cek_status == 4) {
            $hasil = $this->m_deposit->pembatalan_transaksi($id, $data);
            if ($hasil == 0) {
                $this->proses_tutup();
            }

            if ($hasil == 0) {
                $this->db->trans_rollback();
                $hasil = 0;
            } else {
                $this->db->trans_commit();
                $hasil = 1;
            }

            if ($hasil) {
                echo json_encode(array('status' => true, 'message' => 'Deposit berhasil dbatalkan'));
            } else {
                echo json_encode(array('status' => false, 'message' => 'Pembatalan Deposit Gagal'));
            }
        } else {
            echo json_encode(array('status' => false, 'message' => 'Deposit sudah tidak bisa dbatalkan'));
        }
    }

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

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

        /* ==================== update suspend data ================== */
        $this->trans_data['suspend_data']['data'] = $data;
        $this->trans_data['suspend_data']['TransNumber'] = trim($id);

        $result_return = $this->m_deposit->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($id) {
        $data['menu_link'] = $this->m_menu->get_menulink($this->menu_id);

        $CompanyCode = $this->session->userdata('company');

        $data['menu_label'] = $this->m_menu->get_menulabel($this->menu_id);
        $BranchCode = $this->session->userdata('branch');
        $deposit_number = str_replace($data['menu_link'] . '/to_journal/', "", $this->uri->uri_string());

        $data['date_journal'] = $this->m_deposit->get_date_journal_deposit($deposit_number);

        $ambil_dari_cashdeposit = $this->m_deposit->get_branch($deposit_number);
        $branch = $ambil_dari_cashdeposit->BranchCode;

        $data['list_jurnal'] = $this->m_deposit->get_all_journal_deposit($deposit_number, $branch);

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

    public function to_print($CDepNumber) {

        $id = str_replace(".", "/", $CDepNumber);
        $detail = $this->m_deposit->get_detail($id);

        $data['BankPayment'] = $this->m_deposit->get_account_name_bank_payment($detail->BankPayment);
        $data['BankReceipt'] = $this->m_deposit->get_account_name_bank_receipt($detail->BankReceipt);
        $data['CDepNumber'] = $detail->CDepNumber;
        $data['CDepDate'] = $detail->CDepDate;
        $data['CDepDesc'] = $detail->CDepDesc;
        $data['CDepTotalAmount'] = $detail->CDepTotalAmount;
        $data['AppLevel1Code'] = $detail->AppLevel1Code;
        $data['AppLevel1Date'] = $detail->AppLevel1Date;

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

    public function approval2() {
        if ($this->session->userdata("jobtitle_status") == 1) {

            $deposit_number = $this->input->post('txt_deposit_no');

            $data = 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;
            $this->trans_data['approve_data']['TransNumber'] = $deposit_number;

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

            $this->trans_data = [];

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

            echo json_encode($result);
        }
    }

    //Daswan Fungsi untuk print detail
    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);
        $companyCode = $this->session->userdata('company');

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

        $detail = $this->m_deposit->get_detail($id);

        $data['BankPayment'] = $this->m_deposit->get_account_name_bank_payment($detail->BankPayment);
        $data['BankReceipt'] = $this->m_deposit->get_account_name_bank_receipt($detail->BankReceipt);
        $data['nm_employee'] = $this->m_deposit->get_nm_created($id);
        $data['Branch'] = $this->m_deposit->get_barchname($detail->BranchCode);
        $data['CDepNumber'] = $detail->CDepNumber;
        $data['CDepDate'] = $detail->CDepDate;
        $data['CDepDesc'] = $detail->CDepDesc;
        $data['CurrencyCode'] = $detail->CurrencyCode;
        $data['CurrencyCode_to'] = $detail->CurrencyCode_to;
        $data['CurrencyRate'] = $detail->CurrencyRate;
        $data['CDepTotalAmount'] = $detail->CDepTotalAmount;
        $data['AppLevel1Code'] = $detail->AppLevel1Code;
        $data['AppLevel1Date'] = $detail->AppLevel1Date;
        $data['TransStatus'] = $detail->TransStatus;

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

}
