<?php

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

class Currency extends CI_Controller {

    public $menu_id = 93;
    public $menu_model = array('m_menu', 'm_currency', 'm_coa', 'm_compaccountgl', 'setup/m_branchinfo', 'file/m_new_company','login/m_login');
    public $company = 'm_company';

    public function __construct() {
        parent::__construct();

        $this->load->helper('convert_date');
        $this->load->model($this->menu_model);
        $this->load->helper('regex');
        $this->load->helper('message');
        $this->load->helper('status');
        $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');

                    // get link from tbl_menu 
                    $data['menu_label'] = $this->m_menu->get_menulabel($this->menu_id);
                    $data['menu_link'] = $this->m_menu->get_menulink($this->menu_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('currency/v_currency', $data);
                    $this->load->view('v_footer');
                }
            } else {
                redirect(base_url() . 'error', 'refresh');
            }
        } else {
            redirect(base_url() . 'login', 'refresh');
        }
    }

    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_label'] = $this->m_menu->get_menulabel($this->menu_id);
                    $data['menu_link'] = $this->m_menu->get_menulink($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('currency/v_new_currency', $data);
                    $this->load->view('v_footer');
                    //----------------------end--------------------------------------------------------		
                }
            } else {
                redirect(base_url() . 'error', 'refresh');
            }
        } else {
            redirect(base_url() . 'login', 'refresh');
        }
    }

    public function insert() {
        try {
            $currency_code = $this->input->post('txt_currency_code');
            $currency_name = $this->input->post('txt_currency_name');
            $currency_rate = $this->input->post('txt_currency_rate_nilai');
            $currency_desc = $this->input->post('txt_currency_desc');
            $cmb_country = $this->input->post('cmb_country');
            $currency_symbol = $this->input->post('txt_currency_symbol');

            $dataSend = [];
            $dataSend['data_currency_insert'] = [];
            $dataSend['data_coa_insert'] = [];
            $dataSend['branch_info'] = [];

            if (!regex_name($currency_code)) {
                throw new Exception(msg_invalid_code($currency_code));
            } else {
                if ($this->m_currency->check_code_exist($currency_code) == TRUE) {
                    throw new Exception(msg_already_used($currency_code));
                } else {
                    //========== 1. Insert Currency into am_currency =========//
                    $get_company_percurrency = $this->m_new_company->get_company_currency();
                    foreach ($get_company_percurrency as $row_company) {
                        $Code_company = $row_company->CompanyCode;
                        $dataSend['data_currency_insert'][] = array(
                            'CurrencyCode' => strtoupper($currency_code),
                            'CurrencyName' => $currency_name,
                            'CompanyCode' => $Code_company,
                            'CurrencyRate' => $currency_rate,
                            'CurrencyDesc' => $currency_desc,
                            'CurrencyCountry' => $cmb_country,
                            'CurrencySymbol' => $currency_symbol,
                            'TransStatus' => 1,
                            'LastUser' => $this->session->userdata("username")
                        );
                        //$this->m_currency->insert($data); // masuk ke model
                    }
                    //========== End 1. Insert Currency into am_currency =====//
                    //========== Create COA Account based on new entry currency code by automaticly ==========================//
                    $branch_info = $this->m_branchinfo->get_all_branch_code_company($this->session->userdata("company"));
                    $sub_type_auto = $this->m_coa->get_auto_status();
                    $trans_status = '';
                    $dataSend['branch_info'] = $branch_info;

                    foreach ($sub_type_auto as $row_subtype) {
                        $account_subtype_id = $row_subtype->AccountSubTypeID;
                        $account_subtype_number = $row_subtype->AccNumber;

                        $account_level = $this->m_coa->get_level($account_subtype_number);
                        $account_gl = $this->m_coa->get_account_by_parent($account_subtype_number, $this->session->userdata("company"));

                        if ($account_gl != "") {
                            foreach ($account_gl as $row_account) {
                                //$account_number     = $this->m_coa->create_account_number($account_subtype_number);//masuk di model
                                $trans_status = $row_account->TransStatus;

                                //======= 2. Insert Account G/L into am_accountgl ======//
                                $dataSend['data_coa_insert'][] = array(
                                    'AccountSubTypeID' => $account_subtype_id,
                                    'AccountNumber' => " ", //$account_number
                                    'AccountName' => $row_account->AccountName . ' ' . strtoupper($currency_code),
                                    'CurrencyCode' => strtoupper($currency_code),
                                    'AccountParent' => $account_subtype_number,
                                    'AccountLevel' => $account_level,
                                    'BankID' => Null,
                                    'AccountChild' => 1,
                                    'AppLevel1Status' => 1,
                                    'AppLevel2Status' => 1,
                                    'TransStatus' => 2,
                                    'LastUser' => $this->session->userdata("username"),
                                    'trans_status' => $trans_status //untuk loop si accountCompanyGL
                                );
                                //                            $this->m_coa->insert($data_coa);//masuk ke model
                                //======= END 2. Insert Account G/L into am_accountgl ======//
                                //======= 3. Insert Account G/L into at_compaccountgl for each branch ==========// masuk ke model smua
                                //                            foreach($branch_info as $row_branch){
                                //                                $dataSend['data_glcompany_insert'][] = array(
                                //                                    'CompanyCode'       => $row_branch->CompanyCode,
                                //                                    'BranchCode'        => $row_branch->BranchCode,
                                //                                    'AccountNumber'     => $account_number,
                                //                                    'TransStatus'       => $trans_status,
                                //                                    'PLStatus'          => 1,// change PL status 1 ()
                                //                                    'GLSaldoYears'      => date('Y')
                                //                                );
                                ////                                $this->m_compaccountgl->insert($glcompany_data); //masuk ke model                 
                                //                            }                    
                                //======= End 3. Insert Account G/L into at_compaccountgl for each branch =======//
                            }
                        }
                    }
                    //========== End Create COA Account based on new entry currency code by automaticly ======================//

                    $rstDataSend = $this->m_currency->data_send_currency_insert($dataSend);
                    if ($rstDataSend) {
                        $result['status'] = true;
                        $result['message'] = get_notification('insert', 1);
                    } else {
                        $result['status'] = false;
                        $result['message'] = get_notification('insert', 0);
                    }
                    echo json_encode($result);
                }
            }
        } catch (Exception $e) {
            $result['status'] = false;
            $result['message'] = $e->getMessage();
            echo json_encode($result);
        }
    }

    public function insert_FACHRI() {
        $currency_code = $this->input->post('txt_currency_code');
        $currency_name = $this->input->post('txt_currency_name');
        $currency_rate = $this->input->post('txt_currency_rate_nilai');
        $currency_desc = $this->input->post('txt_currency_desc');
        $cmb_country = $this->input->post('cmb_country');
        $currency_symbol = $this->input->post('txt_currency_symbol');

        if (!regex_name($currency_code)) {
            $result['status'] = false;
            $result['message'] = msg_invalid_code($currency_code);
        } else {
            if ($this->m_currency->check_code_exist($currency_code) == TRUE) {
                $result['status'] = false;
                $result['message'] = msg_already_used($currency_code);
            } else {
                $this->db->trans_begin();

                $get_company_percurrency = $this->m_new_company->get_company_currency();
                foreach ($get_company_percurrency as $row_company) {
                    $Code_company = $row_company->CompanyCode;
                    $data = array(
                        'CurrencyCode' => strtoupper($currency_code),
                        'CurrencyName' => $currency_name,
                        'CompanyCode' => $Code_company,
                        'CurrencyRate' => $currency_rate,
                        'CurrencyDesc' => $currency_desc,
                        'CurrencyCountry' => $cmb_country,
                        'CurrencySymbol' => $currency_symbol,
                        'TransStatus' => 1,
                        'LastUser' => $this->session->userdata("username")
                    );

                    //1. Insert Currency into am_currency
                    //$this->transaction['insert_group']['data'][] = $data;

                    $this->m_currency->insert($data);
                }
                //-----------------------------------------------------------------------------------------------------//
                //Create COA Account based on new entry currency code by automaticly
                $branch_info = $this->m_branchinfo->get_all_branch_code_company($this->session->userdata("company"));
                $sub_type_auto = $this->m_coa->get_auto_status();
                $trans_status = '';

                foreach ($sub_type_auto as $row_subtype) {
                    $account_subtype_id = $row_subtype->AccountSubTypeID;
                    $account_subtype_number = $row_subtype->AccNumber;

                    $account_level = $this->m_coa->get_level($account_subtype_number);
                    $account_gl = $this->m_coa->get_account_by_parent($account_subtype_number, $this->session->userdata("company"));

                    foreach ($account_gl as $row_account) {
                        $account_number = $this->m_coa->create_account_number($account_subtype_number);
                        $trans_status = $row_account->TransStatus;


                        $data_coa = array(
                            'AccountSubTypeID' => $account_subtype_id,
                            'AccountNumber' => $account_number,
                            'AccountName' => $row_account->AccountName . ' ' . strtoupper($currency_code),
                            'CurrencyCode' => strtoupper($currency_code),
                            'AccountParent' => $account_subtype_number,
                            'AccountLevel' => $account_level,
                            'BankID' => Null,
                            'AccountChild' => 1,
                            'AppLevel1Status' => 1,
                            'AppLevel2Status' => 1,
                            'TransStatus' => 2,
                            'LastUser' => $this->session->userdata("username")
                        );

                        //2. Insert Account G/L into am_accountgl
                        //  $this->transaction['insert_coa']['data'][] = $data_coa;
                        $this->m_coa->insert($data_coa);
                        //------------------------------------------------------------------------------------------------//
                        //3. Insert Account G/L into at_compaccountgl for each branch
                        foreach ($branch_info as $row_branch) {
                            $glcompany_data = array(
                                'CompanyCode' => $row_branch->CompanyCode,
                                'BranchCode' => $row_branch->BranchCode,
                                'AccountNumber' => $account_number,
                                'TransStatus' => $trans_status,
                                'PLStatus' => 1, // change PL status 1 ()
                                'GLSaldoYears' => date('Y')
                            );
                            //$this->transaction['insert_coa_company']['data'][] = $glcompany_data;

                            $this->m_compaccountgl->insert($glcompany_data);
                            //----------------------------------------------------------------------------------------//                    
                        }
                    }
                }
                //$result_insert 		= $this->m_currency->insert_transaction($this->transaction);
                //$this->transaction 	= [];
                /* ===============================trans rol back============================================================ */
                if ($this->db->trans_status() == FALSE) {
                    $this->db->trans_rollback();
                    $result['status'] = false;
                    $result['message'] = get_notification('insert', 0);
                } else {
                    $this->db->trans_commit();
                    $result['status'] = true;
                    $result['message'] = get_notification('insert', 1);
                }
                /* ========================================end=============================================================== */
            }
        }

        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)
                     */
                    $id = $this->uri->segment(4);

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

                    $data['detail'] = $this->m_currency->get_detail_new($id);
                    $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('currency/v_edit_currency', $data);
                    $this->load->view('v_footer');
                    //----------------------end--------------------------------------------------------		
                }
            } else {
                redirect(base_url() . 'error', 'refresh');
            }
        } else {
            redirect(base_url() . 'login', 'refresh');
        }
    }

    public function update() {
        try {
            $currency_id = $this->input->post('txt_currency_id');
            $currency_code = $this->input->post('txt_currency_code');
            $currency_name = $this->input->post('txt_currency_name');
            $currency_rate = $this->input->post('txt_currency_rate_nilai');
            $currency_desc = $this->input->post('txt_currency_desc');
            $cmb_country = $this->input->post('cmb_country');
            $currency_symbol = $this->input->post('txt_currency_symbol');
            $company_code = $this->input->post('txt_company_code');

            $dataSend = [];
            $dataSend['data_currency_update'] = [];
            $dataSend['currency_id'] = $currency_id;
            $dataSend['company_code'] = $company_code;

            $dataSend['data_currency_update'] = array(
                'CurrencyCode' => strtoupper($currency_code),
                'CurrencyName' => $currency_name,
                'CurrencyRate' => $currency_rate,
                'CurrencyDesc' => $currency_desc,
                'CurrencyCountry' => $cmb_country,
                'CurrencySymbol' => $currency_symbol,
                'TransStatus' => 1,
                'LastUser' => $this->session->userdata("username")
            );
//            $this->m_currency->update($data, $currency_id, $company_code);

            $rstDataSend = $this->m_currency->data_send_currency_update($dataSend);
            if ($rstDataSend) {
                $result['status'] = true;
                $result['message'] = get_notification('update', 1);
            } else {
                $result['status'] = false;
                $result['message'] = get_notification('update', 0);
            }
            echo json_encode($result);
        } catch (Exception $e) {
            $result['status'] = false;
            $result['message'] = $e->getMessage();
            echo json_encode($result);
        }
    }

    public function update_LAMA() {
        $currency_id = $this->input->post('txt_currency_id');
        $currency_code = $this->input->post('txt_currency_code');
        $currency_name = $this->input->post('txt_currency_name');
        $currency_rate = $this->input->post('txt_currency_rate_nilai');
        $currency_desc = $this->input->post('txt_currency_desc');
        $cmb_country = $this->input->post('cmb_country');
        $currency_symbol = $this->input->post('txt_currency_symbol');
        $company_code = $this->input->post('txt_company_code');

        $data = array(
            'CurrencyCode' => strtoupper($currency_code),
            'CurrencyName' => $currency_name,
            'CurrencyRate' => $currency_rate,
            'CurrencyDesc' => $currency_desc,
            'CurrencyCountry' => $cmb_country,
            'CurrencySymbol' => $currency_symbol,
            'TransStatus' => 1,
            'LastUser' => $this->session->userdata("username")
        );

        $this->db->trans_begin();
        $this->m_currency->update($data, $currency_id, $company_code);

        /* ===============================trans rol back============================================================ */
        if ($this->db->trans_status() == FALSE) {
            $this->db->trans_rollback();
            $result['status'] = false;
            $result['message'] = get_notification('update', 0);
        } else {
            $this->db->trans_commit();
            $result['status'] = true;
            $result['message'] = get_notification('update', 1);
        }
        /* ========================================end=============================================================== */
        echo json_encode($result);
    }

    public function update_coa_currency() {
        $branch_info = $this->m_branchinfo->get_all_branch_code();
        $currency_code = $this->input->post('CurrencyCode');

        $sub_type_auto = $this->m_coa->get_auto_status();
        $this->db->trans_begin();

        foreach ($sub_type_auto as $row_subtype) {
            $subtype_id = $row_subtype->AccountSubTypeID;
            $get_parent = $this->m_coa->get_account_by_subtypeid($subtype_id, 0);

            if ($get_parent) {
                $parent_acc_number = $get_parent[0]->AccountNumber;
                $account_gl = $this->m_coa->get_account_by_subtypeid($subtype_id, 1);

                if ($account_gl) {
                    foreach ($account_gl as $row_account) {
                        $name_account = $row_account->AccountName;
                        $account_name = substr($name_account, 0, strlen($name_account) - 4) . ' ' . $currency_code;
                        $check_exist = $this->m_coa->check_name_exist($account_name);

                        if ($check_exist == false) {
                            $account_number = $this->m_coa->create_account_number($parent_acc_number);
                            $account_level = $row_account->AccountLevel;
                            $pl_status = 0;

                            $data_coa = array(
                                'AccountSubTypeID' => $subtype_id,
                                'AccountNumber' => $account_number,
                                'AccountName' => $account_name,
                                'CurrencyCode' => $currency_code,
                                'AccountParent' => $parent_acc_number,
                                'AccountLevel' => $account_level,
                                'BankID' => Null,
                                'AccountChild' => 1,
                                'AppLevel1Status' => 1,
                                'AppLevel2Status' => 1,
                                'TransStatus' => 2,
                                'LastUser' => $this->session->userdata("username")
                            );

                            //1. Insert Account G/L into am_accountgl
                            $this->m_coa->insert($data_coa);
                            //------------------------------------------------------------------------------------------------//
                        } else {
                            $account_number = $this->m_coa->get_by_name($account_name);
                            $pl_status = $this->m_compaccountgl->check_pl_status(1) == true ? 1 : 0;
                        }

                        //2. Insert Account G/L into at_compaccountgl for each branch
                        foreach ($branch_info as $row_branch) {
                            $company_code = $row_branch->CompanyCode;
                            $branch_code = $row_branch->BranchCode;
                            $check_exist = $this->m_compaccountgl->check_if_exist($account_number, $company_code, $branch_code, date('Y'));

                            if ($check_exist == false) {
                                $branch_gl = array(
                                    'CompanyCode' => $company_code,
                                    'BranchCode' => $branch_code,
                                    'AccountNumber' => $account_number,
                                    'GLSaldoYears' => date('Y'),
                                    'PLStatus' => 1,
                                    'LastUserPL' => $this->session->userdata("username")
                                );

                                $this->m_compaccountgl->insert($branch_gl);
                            } else {
                                $branch_gl = array(
                                    'PLStatus' => 1,
                                    'LastUserPL' => $this->session->userdata("username")
                                );

                                $this->m_compaccountgl->update($branch_gl, $account_number, $company_code);
                            }

                            //----------------------------------------------------------------------------------------//                    
                        }
                    }
                } else {
                    $result['status'] = false;
                    $result['message'] = "Update data tidak berhasil, silahkan cek kembali form transaksi ini";
                    echo json_encode($result);
                    die();
                }
            } else {
                echo $get_parent[0]->AccountName;
            }
        }

        /* ===============================trans rol back============================================================ */
        if ($this->db->trans_status() == FALSE) {
            $this->db->trans_rollback();
            $result['status'] = false;
            $result['message'] = get_notification('edit', 0);
        } else {
            $this->db->trans_commit();
            $result['status'] = true;
            $result['message'] = get_notification('edit', 1);
        }
        /* ========================================end=============================================================== */

        echo json_encode($result);
    }

    public function suspend_data() {
        try {
            $id = $this->input->post('id');
            $dataSend = [];
            $dataSend['data_insert'] = [];
            $dataSend['idcode'] = $id;
            $dataSend['company'] = $this->session->userdata("company");
            //update table for status = 3
            $dataSend['data_insert'] = array(
                'TransStatus' => 3,
                'LastUser' => $this->session->userdata("username")
            );

//            $this->m_currency->update($data, $id, $this->session->userdata("company"));

            $rstDataSend = $this->m_currency->data_send_currency_suspend($dataSend);
            if ($rstDataSend) {
                $result['status'] = true;
                $result['message'] = get_notification('suspend', 1);
            } else {
                $result['status'] = false;
                $result['message'] = get_notification('suspend', 0);
            }
            echo json_encode($result);
        } catch (Exception $e) {
            $result['status'] = false;
            $result['message'] = $e->getMessage();
            echo json_encode($result);
        }
    }

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

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

        //update table for status = 3
        $this->db->trans_begin();
        $this->m_currency->update($data, $id, $this->session->userdata("company"));

        /* ===============================trans rol back============================================================ */
        if ($this->db->trans_status() == FALSE) {
            $this->db->trans_rollback();
            $result['status'] = false;
            $result['message'] = get_notification('suspend', 0);
        } else {
            $this->db->trans_commit();
            $result['status'] = true;
            $result['message'] = get_notification('suspend', 1);
        }
        /* ========================================end=============================================================== */

        echo json_encode($result);
    }

    //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======= */

        /* ===========================declar post by find================================================ */
        $Code = ($this->input->post('CurrencyCode')) ? $this->input->post('CurrencyCode') : '';
        $Name = ($this->input->post('CurrencyName')) ? $this->input->post('CurrencyName') : '';

        $company_code = ($this->input->post('CompanyCode')) ? $this->input->post('CompanyCode') : '';
        $TransStatus = ($this->input->post('TransStatus')) ? $this->input->post('TransStatus') : '';
        /* ========================================end================================================== */
        /* ========================jika kondisi tidak sama dengan kosong di find========================= */
        if ($company_code != "") {
            $this->db->where('CompanyCode', $company_code);
        } else {
            $this->db->where('CompanyCode', $this->session->userdata('company'));
        }

        if ($Code != "") {
            $this->db->like('CurrencyCode', $Code);
        }
        if ($Name != "") {
            $this->db->like('CurrencyName', $Name);
        }
        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));
        }

        /* ==========================end================================================================= */

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

        $offset = $count_data - ($rows * $page);
        if ($offset > 0) {
            $rows = $rows;
        } else if ($offset < 0) {
            $rows = $count_data - ($rows * ($page - 1));
        }
        /* ========================end======================================= */
        $result = $this->m_currency->join_all();
        for ($i = 0; $i <= ($rows - 1); $i++) {
            $TransStatus = get_status_master($result[$i]->TransStatus);

            $hasil[] = array(
                'TransNumber' => $result[$i]->CurrencyId,
                'CurrencyCode' => $result[$i]->CurrencyCode,
                'CurrencyName' => $result[$i]->CurrencyName,
                'CurrencyRate' => $result[$i]->CurrencyRate,
                'CurrencyDesc' => $result[$i]->CurrencyDesc,
                'CurrencyCountry' => $result[$i]->CountryName,
                'CurrencySymbol' => $result[$i]->CurrencySymbol,
                'CompanyCode' => $result[$i]->CompanyCode,
                'TransStatus' => $TransStatus
            );
        }

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

    public function to_preview($obj_string=null) {
//        $data['list'] = $this->m_currency->get_all_data($CompanyCode);

        $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_currency->get_all_to_print($data_filter);
        
        $this->load->view('v_header');
        $this->load->view('currency/v_preview_currency', $data);
        $this->load->view('v_footer');
    }
    public function to_preview_LAMA() {

        $CompanyCode = $this->uri->segment(4);
        $BranchCode = $this->uri->segment(5);
        // set data
        $data['company_code'] = $CompanyCode;
        $data['branch_code'] = $BranchCode;

        $data['list'] = $this->m_currency->get_all_data($CompanyCode);

        $this->load->view('v_header');
        $this->load->view('currency/v_preview_currency', $data);
        $this->load->view('v_footer');
    }

    public function to_print() {
        $data['list'] = $this->m_currency->get_all_data();

        $this->load->view('v_header');
        $this->load->view('currency/v_print_currency', $data);
        $this->load->view('v_footer');
    }

    public function trans_rollback($modus) {
        $this->db->trans_rollback();
        $result['status'] = false;

        if ($modus == 'journal') {
            $result['message'] = 'Pengaturan Preference Journal Template belum lengkap, silahkan atur ulang';
        } else {
            $result['message'] = get_notification_transaksi($this->name, $modus, 0);
        }

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

}
