<?php

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

class Piutang_karyawan 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 = "Piutang Karyawan";
    public $trans_data = array();
    //initial menu id dari tabel m_menu
    public $menu_id = 255;

    public function __construct() {
        parent::__construct();
        $this->load->model('m_piutang_karyawan');
        $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/piutang_karyawan/v_piutang_karyawan', $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('EDebtNumber', $Number);
        }
        if ($Desc != "") {
            $this->db->like('EDebtDesc', $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_employeedebt.EDebtDate >=', date("Y-m-d", strtotime($DateFrom)));
                $this->db->where('at_employeedebt.EDebtDate <=', date("Y-m-d", strtotime($DateTo)));
            }
        }
        /* ==========================end================================================================= */

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

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

        if ($offset > 0) {
            $rows = $rows;
        } else {
            $rows = $count_data - ($rows * ($page - 1));
        }
        /* ========================end======================================= */
        $result = $this->m_piutang_karyawan->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]->EDebtNumber,
                'EDebtDate' => $result[$i]->EDebtDate,
                'CompTransCode' => $result[$i]->CompTransCode,
                'EmployeeName' => $result[$i]->EmployeeName,
                'EDebtAmount' => $result[$i]->EDebtAmount,
                'EDebtDesc' => $result[$i]->EDebtDesc,
                '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['piutang_karyawan'] = $this->m_piutang_karyawan->create_piutang_karyawan_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/piutang_karyawan/v_new_piutangkaryawan', $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_piutang_karyawan->get_all();

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

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

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

    //Function view print data
    public function to_preview($obj_string = null) {
        $data['company'] = $this->m_piutang_karyawan->get_company_by_branch($this->session->userdata('branch'));
        //$data['list']	 = $this->m_piutang_karyawan->get_all_to_print($this->session->userdata('branch'));
        //$data['list'] 		= $this->m_expense_cashbon->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['company_code']   = $data_filter['cmb_company'];
        $data['branch_code']    = $data_filter['cmb_branch'];
        $data['list']           = $this->m_piutang_karyawan->get_all_to_print($data_filter);
        
        $this->load->view('v_header');
        $this->load->view('cash_and_bank/piutang_karyawan/v_print_piutang_karyawan', $data);
        $this->load->view('v_footer');
    }

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

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

        $karyawan_number = $this->m_piutang_karyawan->create_piutang_karyawan_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_piutang_karyawan->get_company_edit($branch_explode);
        echo"$list_company";
    }

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

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

    public function get_company() {
        $CompanyCode = $this->session->userdata('company');
        $branch = $this->session->userdata('branch');
        $list_company = $this->m_piutang_karyawan->get_company($CompanyCode, $branch);
        echo"$list_company";
    }

    public function get_akun_bank($BranchCode) {
        $list_akun = $this->m_piutang_karyawan->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_piutang_karyawan->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($com_transaksi, $txt_date, $txt_piutang_no, $company, $bank_from, $cmb_karyawan, $rekening_karyawan, $cheq_number, $cheq_date, $txt_desc, $txt_amount) {

        $data_piutang = array(
            'BranchCode' => $com_transaksi,
            'EDebtDate' => $txt_date,
            'EDebtNumber' => $txt_piutang_no,
            'CompTransCode' => $company,
            'BankTransfer' => $bank_from,
            'EmplRecpCode' => $cmb_karyawan,
            'BankReceipt' => $rekening_karyawan,
            'ChequeNumber' => $cheq_number,
            'ChequeDate' => $cheq_date,
            'EDebtDesc' => $txt_desc,
            'TransStatus' => 4,
            'EdebtIsLock' => 1,
            'EDebtAmount' => $txt_amount,
            '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, $txt_piutang_no, $bank_from, $get_coa_piutang_karyawan, $txt_amount, $txt_desc) {

        $journal_debet = array(
            'EDebtDate' => $txt_date,
            'EDebtNumber' => $txt_piutang_no,
            'EDebtAccountNumber' => $get_coa_piutang_karyawan,
            'EDebtSaldoDebet' => $txt_amount,
            'EDebtSaldoCredit' => 0,
            'EDebtDesc' => $txt_desc,
            'EDebtSignStatus' => 0,
            'LastUser' => $this->session->userdata('username')
        );

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

        $journal_credit = array(
            'EDebtDate' => $txt_date,
            'EDebtNumber' => $txt_piutang_no,
            'EDebtAccountNumber' => $bank_from,
            'EDebtSaldoDebet' => 0,
            'EDebtSaldoCredit' => $txt_amount,
            'EDebtDesc' => $txt_desc,
            'EDebtSignStatus' => 1,
            'LastUser' => $this->session->userdata('username')
        );

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

    public function insert() {

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

        $bulan = date('m');
        $tahun = date('y');
        $cek_tahun = date('Y');

        $com_transaksi = $this->input->post('cmb_from_company');
        $txt_date = date("Y-m-d", strtotime($this->input->post('txt_payment_date')));
        $bank_from = $this->input->post('cmb_from_bank');
        $cmb_karyawan = $this->input->post('cmb_karyawan');
        $rekening_karyawan = $this->input->post('txt_rekening_karyawan');
        $txt_desc = $this->input->post('txa_description');
        $txt_amount = $this->input->post('txt_amount');
        $checque_no = $this->input->post('txt_cheque_no');

        /* =============================== Create number piutang karyawan ============================= */
        $txt_piutang_no = $this->m_piutang_karyawan->create_piutang_karyawan_number($txt_date);

        if (!empty($checque_no)) {
            $cheq_number = $this->input->post('txt_cheque_no');
            $cheq_date = date("Y-m-d", strtotime($this->input->post('txt_cheque_date')));
        } else {
            $cheq_number = "";
            $cheq_date = "NULL";
        }

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

        //$ExpCategoryID 				= $this->m_piutang_karyawan->get_expt_piutang($CompanyCode);

        $get_coa_piutang_karyawan = $this->m_piutang_karyawan->get_coa_piutang_karyawan($company, $branch_code);

        //$PiutangAccount = '1-3-04-004-00';
        if ($get_coa_piutang_karyawan == "") {
            $result['status'] = false;
            $result['message'] = get_notification('coa', 0);

            echo json_encode($result);
            die;
        } else {

            /* ================================================= insert group =================================================== */
            $this->insert_group($com_transaksi, $txt_date, $txt_piutang_no, $company, $bank_from, $cmb_karyawan, $rekening_karyawan, $cheq_number, $cheq_date, $txt_desc, $txt_amount);

            /* ===================================== insert journal ========================================= */
            $this->insert_journal($txt_date, $txt_piutang_no, $bank_from, $get_coa_piutang_karyawan, $txt_amount, $txt_desc);

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

            $this->trans_data = [];

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

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

    public function update_group($txt_piutang_no, $bank_from, $cmb_karyawan, $rekening_karyawan, $cheq_number, $cheq_date, $txt_desc, $txt_amount) {
        $data_piutang = array(
            'BankTransfer' => $bank_from,
            'EmplRecpCode' => $cmb_karyawan,
            'BankReceipt' => $rekening_karyawan,
            'ChequeNumber' => $cheq_number,
            'ChequeDate' => $cheq_date,
            'EDebtDesc' => $txt_desc,
            'EDebtAmount' => $txt_amount,
            'LastUser' => $this->session->userdata('username')
        );

        //$this->m_piutang_karyawan->update_piutang_karyawan($data_piutang, $txt_piutang_no);
        $this->trans_data['update_group']['data'] = $data_piutang;
        $this->trans_data['update_group']['TransNumber'] = $txt_piutang_no;
    }

    public function update() {
        $BranchCode = $this->session->userdata('branch');
        $EDebtNumber = $this->input->post('txt_piutang_no');
        $result_insert = 0;
        $com_trans = $this->input->post('cmb_from_company');

        $id = $EDebtNumber;
        $txt_piutang_no = $EDebtNumber;
        // $cek_app_status_mgr = $this->m_piutang_karyawan->cek_status_app_mgr($piutang_no);
        // $cek_app_status_dir = $this->m_piutang_karyawan->cek_status_app_dir($piutang_no);
        // $cek_status 		= $this->m_piutang_karyawan->cek_trans_status($id);
        $chk_suspend = $this->input->post('chk_suspend');

        $bank_from = $this->input->post('cmb_from_bank');
        $cmb_karyawan = $this->input->post('cmb_karyawan');
        $rekening_karyawan = $this->input->post('txt_rekening_karyawan');
        $cheq_date = date("Y-m-d", strtotime($this->input->post('txt_cheque_date')));
        $txt_desc = $this->input->post('txa_description');
        $txt_amount = $this->input->post('txt_amount');
        $txt_date = date("Y-m-d", strtotime($this->input->post('txt_payment_date')));


        $checque_no = $this->input->post('txt_cheque_no');
        if (!empty($checque_no)) {
            $cheq_number = $this->input->post('txt_cheque_no');
            $cheq_date = date("Y-m-d", strtotime($this->input->post('txt_cheque_date')));
        } else {
            $cheq_number = "";
            $cheq_date = "NULL";
        }

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

        //$ExpCategoryID 	= $this->m_piutang_karyawan->get_expt_piutang($CompanyCode);

        /* ================================= get coa piutang karyawan ==================================== */
        $get_coa_piutang_karyawan = $this->m_piutang_karyawan->get_coa_piutang_karyawan_edit($com_trans);

        //$PiutangAccount = '1-3-04-004-00';
        if ($get_coa_piutang_karyawan == "") {

            $result['status'] = false;
            $result['message'] = get_notification('coa', 0);

            echo json_encode($result);
            die;
        } else {

            /* ========================================= update group ================================ */
            $this->update_group($txt_piutang_no, $bank_from, $cmb_karyawan, $rekening_karyawan, $cheq_number, $cheq_date, $txt_desc, $txt_amount);

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

            /* ============================================ insert journal =================================== */
            $this->insert_journal($txt_date, $txt_piutang_no, $bank_from, $get_coa_piutang_karyawan, $txt_amount, $txt_desc);
            
            $result_return = $this->m_piutang_karyawan->update_transsaction($this->trans_data);

            $this->trans_data = [];

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

            echo json_encode($result);
        }
    }

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

    function approved_mgr() {
        $txt_piutang_no = $this->input->post('txt_piutang_no');
        $data = array(
            "TransStatus" => 1,
            "AppFinished" => 1,
            "AppLevel1Status" => 1,
            "AppLevel1Code" => $this->session->userdata('username'),
            "AppLevel1Date" => date('Y-m-d'),
            "EdebtIsLock" => 0,
            "LastUser" => $this->session->userdata('username')
        );

        /* =============================Fungsi Update payment =================================== */
        //$this->m_piutang_karyawan->update_approved_data($data,$txt_piutang_no);

        $this->trans_data['approve_data']['data'] = $data;
        $this->trans_data['approve_data']['TransNumber'] = $txt_piutang_no;

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

        $this->trans_data = [];

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

        echo json_encode($result);
    }

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

        $arr_no = explode("/", $txt_piutang_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_piutang = array(
            "TransStatus" => 4,
            "AppFinished" => 0,
            "AppLevel1Status" => 0,
            "EdebtIsLock" => 1
        );

        /* ==================================== cek payment karyawan ================================= */
        $cek_payment_karyawan = $this->m_piutang_karyawan->m_cek_payment_karyawan($txt_piutang_no);
        $cek_payment_karyawan_journal = $this->m_piutang_karyawan->m_cek_payment_karyawan_journal($txt_piutang_no);

        if ($cek_payment_karyawan) {
            /* =================================== get no payment ==================================================== */
            $no_payment_karyawan = $this->m_piutang_karyawan->get_no_payment_karyawan($txt_piutang_no);

            $result['status'] = false;
            $result['message'] = get_notification('reverse_transaksi', 0) . ' ' . $no_payment_karyawan;
            echo json_encode($result);
        } else if ($cek_payment_karyawan_journal) {
            /* =================================== get no payment ==================================================== */
            $no_payment_karyawan_journal = $this->m_piutang_karyawan->get_no_payment_karyawan_journal($txt_piutang_no);

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

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

            if ($check_period == false) {

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

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

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

                $this->trans_data = [];

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

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

        $piutang_number = str_replace($data['menu_link'] . '/to_journal/', "", $this->uri->uri_string());
        $pecah = explode("/", $piutang_number);
        $branch_explode = $pecah[0];

        $data['date_journal'] = $this->m_piutang_karyawan->get_date_journal_piutang($piutang_number);
        $data['list_jurnal'] = $this->m_piutang_karyawan->get_all_journal_piutang($piutang_number, $branch_explode);

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

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

        $BranchCode = $this->m_piutang_karyawan->get_branch_transaction($piutang_no);

        if ($piutang_no != "") {

            $data['list_transaksi'] = $this->m_piutang_karyawan->get_detail_item_print($piutang_no, $BranchCode, '=');

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

    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->m_piutang_karyawan->suspend($data, $id);

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

        $result_return = $this->m_piutang_karyawan->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);
    }

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

}
