<?php

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

/*
 * Author   : Tagor // Tanggal : 11-08-2014
 * Revisi   : Dhidhik, 23 maret 2016, array in modul system
 */

class fixed_asset_type extends CI_Controller {

    public $menu_id = 181;
    public $code = "Fixed Asset Type Code";
    public $name = "Fixed Asset Type Name";

    public function __construct() {
        parent::__construct();
        $this->load->helper('regex');
        $this->load->helper('message');
        $this->load->helper('status');
        $this->load->helper('custom_string_helper');

        $this->load->model('m_fixed_asset_type');
        $this->load->model('m_item_categories');
        $this->load->model('m_items');
		
		$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();
            }
        }
    }

    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 {
                    //untuk ambil data dari session dan hak akses
                    $group_menu = $this->session->userdata('group_menu');

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

                    //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('fixed_asset_type/v_fixed_asset_type', $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)
                     */
                    //untuk ambil data dari session dan hak akses
                    $group_menu = $this->session->userdata('group_menu');

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

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

    // Function Insert    
    public function insert() {
        try {
            $txt_fixedassettype_name = trim($this->input->post('txt_fixedassettype_name'));
            $cmb_depreciate_method = $this->input->post('cmb_depreciate_method');
            $txt_desc = trim($this->input->post('txt_desc'));
            $txt_estimate_life = $this->input->post('txt_estimate_life');
            $txt_depreciation_rate = $this->input->post('txt_depreciation_rate');
            $cmb_akun_penyusutan = $this->input->post('cmb_akun_penyusutan');
            $cmb_biaya_penyusutan = $this->input->post('cmb_biaya_penyusutan');
            $txt_name_initial = $this->input->post('txt_name_initial');
            $dataSend = [];
            $dataSend['data_fixedassettype_insert'] = [];

            // cek suspended
            $chk_suspended = $this->input->post('chk_suspended');
            if ($chk_suspended == 1) {
                $chk_suspended = 3;
            } else {
                $chk_suspended = 5;
            }

            if ($this->m_fixed_asset_type->check_code_exist($txt_fixedassettype_name) != FALSE) {
                // set nilai untuk ke form
                throw new Exception(msg_already_used($this->name));
            } else if ($this->m_fixed_asset_type->check_code_initial_exist($txt_name_initial) != FALSE) {
                // set nilai untuk ke form
                throw new Exception("Initial Name Category sudah digunakan silahkan mengganti dengan yang lain");
            } else {
                // inisialisasi array
                $dataSend['data_fixedassettype_insert'] = array(
                    'FixedAssetType' => strtoupper($txt_fixedassettype_name),
                    'FixedAssetDesc' => $txt_desc,
                    'DepMethodID' => $cmb_depreciate_method,
                    'EstimatedLife' => $txt_estimate_life,
                    'DepriciateRate' => $txt_depreciation_rate,
                    'AccumAccountNumber' => $cmb_akun_penyusutan,
                    'ExpenseAccountNumber' => $cmb_biaya_penyusutan,
                    'InitialCategory' => strtoupper($txt_name_initial),
                    'TransStatus' => $chk_suspended,
                    'LastUser' => $this->session->userdata("username")
                );

//                // insert database
//                $this->m_fixed_asset_type->insert($data);

                $rstDataSend = $this->m_fixed_asset_type->datasend_fixedassettype_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_LAMA() {
        $txt_fixedassettype_name = trim($this->input->post('txt_fixedassettype_name'));
        $cmb_depreciate_method = $this->input->post('cmb_depreciate_method');
        $txt_desc = trim($this->input->post('txt_desc'));
        $txt_estimate_life = $this->input->post('txt_estimate_life');
        $txt_depreciation_rate = $this->input->post('txt_depreciation_rate');
        $cmb_akun_penyusutan = $this->input->post('cmb_akun_penyusutan');
        $cmb_biaya_penyusutan = $this->input->post('cmb_biaya_penyusutan');
        $txt_name_initial = $this->input->post('txt_name_initial');

        // cek suspended
        $chk_suspended = $this->input->post('chk_suspended');
        if ($chk_suspended == 1) {
            $chk_suspended = 3;
        } else {
            $chk_suspended = 5;
        }
        $this->db->trans_begin();

        /* if(!regex_name($txt_fixedassettype_name)) {
          // set nilai untuk ke form
          $result['status'] = false;
          $result['message'] = msg_invalid_code($this->name);
          }
          else { */
        // cek apakah kode sudah digunakan
        if ($this->m_fixed_asset_type->check_code_exist($txt_fixedassettype_name) == TRUE) {
            // set nilai untuk ke form
            $result['status'] = false;
            $result['message'] = msg_already_used($this->name);
        } else if ($this->m_fixed_asset_type->check_code_initial_exist($txt_name_initial) == TRUE) {
            // set nilai untuk ke form
            $result['status'] = false;
            $result['message'] = "Initial Name Category sudah digunakan silahkan mengganti dengan yang lain";
        } else {
            // inisialisasi array
            $data = array(
                'FixedAssetType' => strtoupper($txt_fixedassettype_name),
                'FixedAssetDesc' => $txt_desc,
                'DepMethodID' => $cmb_depreciate_method,
                'EstimatedLife' => $txt_estimate_life,
                'DepriciateRate' => $txt_depreciation_rate,
                'AccumAccountNumber' => $cmb_akun_penyusutan,
                'ExpenseAccountNumber' => $cmb_biaya_penyusutan,
                'InitialCategory' => strtoupper($txt_name_initial),
                'TransStatus' => $chk_suspended,
                'LastUser' => $this->session->userdata("username")
            );

            // insert database
            $this->m_fixed_asset_type->insert($data);
            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);
            }
        }
        //}	

        echo json_encode($result);
    }

    // Function Edit
    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)
                     */
                    //untuk ambil data dari session dan hak akses
                    $group_menu = $this->session->userdata('group_menu');

                    // inisialisasi additional parameter
                    $id = $this->uri->segment(4);
                    $data['detail'] = $this->m_fixed_asset_type->get_by_id($id);

                    // set data
                    $data['menu_id'] = $this->menu_id;

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

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

    // Function Update
    public function update() {
        try {
            $id = $this->input->post('id');
            $txt_fixedassettype_name = trim($this->input->post('txt_fixedassettype_name'));
            $cmb_depreciate_method = $this->input->post('cmb_depreciate_method');
            $txt_desc = trim($this->input->post('txt_desc'));
            $txt_estimate_life = $this->input->post('txt_estimate_life');
            $txt_depreciation_rate = $this->input->post('txt_depreciation_rate');
            $txt_asset_type_hidden = $this->input->post('txt_asset_type_hidden');
            $cmb_akun_penyusutan = $this->input->post('cmb_akun_penyusutan');
            $cmb_biaya_penyusutan = $this->input->post('cmb_biaya_penyusutan');
            $txt_name_initial = trim($this->input->post('txt_name_initial'));
            $text_trans_status = trim($this->input->post('text_trans_status'));
            $dataSend = [];
            $dataSend['data_fixedassettype_update'] = [];
            $dataSend['data_category_update'] = [];
            $dataSend['idcode'] = $id;

            // inisialisasi array
            $dataSend['data_fixedassettype_update'] = array(
                'FixedAssetType' => strtoupper($txt_fixedassettype_name),
                'FixedAssetDesc' => $txt_desc,
                'DepMethodID' => $cmb_depreciate_method,
                'EstimatedLife' => $txt_estimate_life,
                'DepriciateRate' => $txt_depreciation_rate,
                'AccumAccountNumber' => $cmb_akun_penyusutan,
                'ExpenseAccountNumber' => $cmb_biaya_penyusutan,
                'InitialCategory' => strtoupper($txt_name_initial),
                'LastUser' => $this->session->userdata("username")
            );

            if ($text_trans_status == 1) {
                $dataSend['data_category_update'][] = array(
                    'ItemCategoryName' => strtoupper($txt_fixedassettype_name),
                    'ItemCategoryDesc' => $txt_desc,
                    'InitialCategory' => strtoupper($txt_name_initial),
                    'LastUser' => $this->session->userdata("username"),
                    'idcode' => $txt_asset_type_hidden //untuk parameter update di models
                );
//                $this->m_item_categories->update_from_aset($data_category, $txt_asset_type_hidden);
            }
            // update database
//            $this->m_fixed_asset_type->update($data, $id);
            $rstDataSend = $this->m_fixed_asset_type->datasend_fixedassettype_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() {
        $id = $this->input->post('id');
        $txt_fixedassettype_name = trim($this->input->post('txt_fixedassettype_name'));
        $cmb_depreciate_method = $this->input->post('cmb_depreciate_method');
        $txt_desc = trim($this->input->post('txt_desc'));
        $txt_estimate_life = $this->input->post('txt_estimate_life');
        $txt_depreciation_rate = $this->input->post('txt_depreciation_rate');
        $txt_asset_type_hidden = $this->input->post('txt_asset_type_hidden');
        $cmb_akun_penyusutan = $this->input->post('cmb_akun_penyusutan');
        $cmb_biaya_penyusutan = $this->input->post('cmb_biaya_penyusutan');
        $txt_name_initial = trim($this->input->post('txt_name_initial'));
        $text_trans_status = trim($this->input->post('text_trans_status'));

        // inisialisasi array
        $this->db->trans_begin();
        $data = array(
            'FixedAssetType' => strtoupper($txt_fixedassettype_name),
            'FixedAssetDesc' => $txt_desc,
            'DepMethodID' => $cmb_depreciate_method,
            'EstimatedLife' => $txt_estimate_life,
            'DepriciateRate' => $txt_depreciation_rate,
            'AccumAccountNumber' => $cmb_akun_penyusutan,
            'ExpenseAccountNumber' => $cmb_biaya_penyusutan,
            'InitialCategory' => strtoupper($txt_name_initial),
            'LastUser' => $this->session->userdata("username")
        );

        if ($text_trans_status == 1) {
            $data_category = array(
                'ItemCategoryName' => strtoupper($txt_fixedassettype_name),
                'ItemCategoryDesc' => $txt_desc,
                'LastUser' => $this->session->userdata("username")
            );
            $this->m_item_categories->update_from_aset($data_category, $txt_asset_type_hidden);
        }
        // update database
        $this->m_fixed_asset_type->update($data, $id);
        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);
        }

        echo json_encode($result);
    }

    // Function Delete        
    public function suspend_data() {
        try {
            $id = $this->input->post('id');
            $fix_asset_type_by_id = $this->m_fixed_asset_type->get_by_id_for_suspend($id);
            $branch = $this->session->userdata("branch");
            $dataSend = [];
            $dataSend['idcode'] = $id;
            $dataSend['byname'] = "";
            //================== Cek trans status ============================//
            if ($fix_asset_type_by_id->TransStatus == 1) {
                $cek_item_master = $this->m_items->get_item_by_categori_fix_asset($id);
                if ($cek_item_master == 1) {
                    //========== Cek item by trans status ================//
                    $cek_item_master_transstatus = $this->m_items->get_item_by_categori_by_row($id);
                    if ($cek_item_master_transstatus->TransStatus == 3) {
                        //============= cek item akumulasi ===============//
                        $get_inven_detail = $this->m_fixed_asset_type->get_inven_detail($id, $branch);
                        if ($get_inven_detail->PaymentAmount != $get_inven_detail->ItemAverage) {
                            throw new Exception("Gagal di suspend karena masih ada item fix asset yang belum selesai di akumulasi");
                        } else {
                            //========== tidak ada item ==================//
                            $name = $this->m_fixed_asset_type->get_name_by_id($id);
                            $dataSend['byname'] = $name;
//                            $this->m_item_categories->suspended_by_name($name);   //masuk ke model
//                            $action = $this->m_fixed_asset_type->suspended($id);  //masuk ke model
                            //========== end tidak ada item ==================//
                        }
                        //============= end cek item akumulasi ===========//
                    } else {
                        throw new Exception("Gagal di suspend karena masih ada item di master belum disuspend");
                    }
                    //========== end Cek item by trans status ================//
                } else {
                    //========== Tidak ada item ==============================//
                    $name = $this->m_fixed_asset_type->get_name_by_id($id);
                    $dataSend['byname'] = $name;
//                    $this->m_item_categories->suspended_by_name($name);   //masuk ke model
//                    $action = $this->m_fixed_asset_type->suspended($id);  //masuk ke model
                    //========== End Tidak ada item ==========================//
                }
            } else {
                //============== Tidak ada item ==============================//
                $name = $this->m_fixed_asset_type->get_name_by_id($id);
                $dataSend['byname'] = $name;
//                $this->m_item_categories->suspended_by_name($name);   //masuk ke model
//                $action = $this->m_fixed_asset_type->suspended($id);  //masuk ke model
                //========== End Tidak ada item ==============================//
            }
            //================== End Cek trans status ============================//

            $rstDataSend = $this->m_fixed_asset_type->datasend_fixedassettype_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');
        $fix_asset_type_by_id = $this->m_fixed_asset_type->get_by_id_for_suspend($id);
        $branch = $this->session->userdata("branch");
        $this->db->trans_begin();
        /*
          Cek trans status
         */
        if ($fix_asset_type_by_id->TransStatus == 1) {
            $cek_item_master = $this->m_items->get_item_by_categori_fix_asset($id);
            if ($cek_item_master == 1) {
                /*
                  Cek item by trans status
                 */
                $cek_item_master_transstatus = $this->m_items->get_item_by_categori_by_row($id);
                if ($cek_item_master_transstatus->TransStatus == 3) {
                    /*
                      cek item akumulasi
                     */
                    $get_inven_detail = $this->m_fixed_asset_type->get_inven_detail($id, $branch);

                    if ($get_inven_detail->PaymentAmount != $get_inven_detail->ItemAverage) {
                        $result['status'] = false;
                        $result['message'] = "Gagal di suspend karena masih ada item fix asset yang belum selesai di akumulasi";
                        echo json_encode($result);
                        die();
                    } else {
                        /*
                          Tidak ada item
                         */
                        $name = $this->m_fixed_asset_type->get_name_by_id($id);
                        $this->m_item_categories->suspended_by_name($name);
                        $action = $this->m_fixed_asset_type->suspended($id);
                        /* ==============End================= */
                    }
                    /* ===================End===================== */
                } else {
                    $result['status'] = false;
                    $result['message'] = "Gagal di suspend karena masih ada item di master belum disuspend";
                    echo json_encode($result);
                    die();
                }
            } else {
                /*
                  Tidak ada item
                 */
                $name = $this->m_fixed_asset_type->get_name_by_id($id);
                $this->m_item_categories->suspended_by_name($name);
                $action = $this->m_fixed_asset_type->suspended($id);
                /* ==============End================= */
            }
        } else {
            /*
              Tidak ada item
             */
            $name = $this->m_fixed_asset_type->get_name_by_id($id);
            $this->m_item_categories->suspended_by_name($name);
            $action = $this->m_fixed_asset_type->suspended($id);
            /* ==============End================= */
        }
        /* =================End========================= */

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

    //Function preview data
    public function to_preview($obj_string = null) {
        // set data
        $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['list'] = $this->m_fixed_asset_type->get_all();

        $data_filter = arrayParamInUrlToStr($obj_string);
        $data['company_code'] = $data_filter['cmb_company'];
        $data['branch_code'] = $data_filter['cmb_branch'];
        $data['list'] = $this->m_fixed_asset_type->get_all_to_print($data_filter);

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

    //Function To Print
    public function to_print() {
        // set data
        $data['menu_id'] = $this->menu_id;
        $data['menu_label'] = $this->m_menu->get_menulabel($this->menu_id);

        $data['list'] = $this->m_fixed_asset_type->get_all();

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

    /* public function ajax(){

      echo json_encode($this->m_fixed_asset_type->get_all());
      } */

    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('FAssetCode') ? $this->input->post('FAssetCode') : '';
        $Desc = $this->input->post('FAssetDesc') ? $this->input->post('FAssetDesc') : '';

        $TransStatus = $this->input->post('TransStatus') ? $this->input->post('TransStatus') : '';
        /* ========================================end================================================== */

        /* ========================jika kondisi tidak sama dengan kosong di find lakukan proses di if============ */
        if ($Code != "") {
            $this->db->like('FixedAssetType', $Code);
        }
        if ($Desc != "") {
            $this->db->like('FixedAssetDesc', $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));
        }
        /* ==========================end================================================================= */
        $count_data = $this->db->count_all_results('am_fixedassettype');

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

        $result = $this->m_fixed_asset_type->get_all();

        for ($i = 0; $i <= ($rows - 1); $i++) {
            $TransStatus = get_status_master($result[$i]->TransStatus);
            $DepMethodID = get_depreciate_method($result[$i]->DepMethodID);

            $hasil[] = array(
                'TransNumber' => $result[$i]->FixedAssetTypeID,
                'FixedAssetType' => $result[$i]->FixedAssetType,
                'FixedAssetDesc' => $result[$i]->FixedAssetDesc,
                'EstimatedLife' => $result[$i]->EstimatedLife,
                'DepriciateRate' => $result[$i]->DepriciateRate,
                'DepMethodID' => $DepMethodID,
                'TransStatus' => $TransStatus
            );
        }


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

    function approval_dir() {
        try {
            $txt_fixedassettype_name = trim($this->input->post('txt_fixedassettype_name'));
            $txt_desc = trim($this->input->post('txt_desc'));
            $txt_name_initial = $this->input->post('txt_name_initial');
            $id = $this->input->post('id');
            $dataSend = [];
            $dataSend['data_category'] = [];
            $dataSend['data_fixedassettype_app'] = [];
            $dataSend['idcode'] = $id;

            $dataSend['data_category'] = array(
                'ItemCategoryName' => strtoupper($txt_fixedassettype_name),
                'ItemCategoryDesc' => $txt_desc,
                'AssetStatus' => 1,
                'TransStatus' => 1,
                'InitialCategory' => strtoupper($txt_name_initial),
                'LastUser' => $this->session->userdata("username")
            );
//            $this->m_item_categories->insert($data_category);

            $dataSend['data_fixedassettype_app'] = array(
                'TransStatus' => 1,
                'LastUser' => $this->session->userdata("username")
            );

            // update database approval
//		$action = $this->m_fixed_asset_type->update($data, $id);

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

    function approval_dir_LAMA() {

        $txt_fixedassettype_name = trim($this->input->post('txt_fixedassettype_name'));
        $txt_desc = trim($this->input->post('txt_desc'));
        $txt_name_initial = $this->input->post('txt_name_initial');
        $id = $this->input->post('id');
        $this->db->trans_begin();

        $data_category = array(
            'ItemCategoryName' => strtoupper($txt_fixedassettype_name),
            'ItemCategoryDesc' => $txt_desc,
            'AssetStatus' => 1,
            'TransStatus' => 1,
            'InitialCategory' => strtoupper($txt_name_initial),
            'LastUser' => $this->session->userdata("username")
        );
        $this->m_item_categories->insert($data_category);

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

        // update database
        $action = $this->m_fixed_asset_type->update($data, $id);

        if ($this->db->trans_status() === FALSE) {
            $this->db->trans_rollback();
            $result['status'] = FALSE;
            $result['message'] = get_notification('approval', 0);
        } else {
            $this->db->trans_commit();
            $result['status'] = TRUE;
            $result['message'] = get_notification('approval', 1);
        }

        echo json_encode($result);
    }

    function akun_penyusutan() {
        $list_akun = $this->m_fixed_asset_type->get_akun_penyusutan();
        
		$hasil[] = array(
			'AccountNumber' 	=>	" ",
			'AccountName' 	=>	" "
		);
			
		foreach($list_akun  as $row_detail){
			$hasil[] = array(
				'AccountNumber' => $row_detail->AccountNumber,
				'AccountName' 	=> $row_detail->AccountName
			);
		}
		
		echo json_encode($hasil);
    }

    function biaya_penyusutan() {
        $list_biaya = $this->m_fixed_asset_type->get_biaya_penyusutan();
        $hasil[] = array(
			'AccountNumber' 	=>	" ",
			'AccountName' 	=>	" "
		);
			
		foreach($list_biaya  as $row_detail){
			$hasil[] = array(
				'AccountNumber' => $row_detail->AccountNumber,
				'AccountName' 	=> $row_detail->AccountName
			);
		}
		
		echo json_encode($hasil);
		
		//echo "$list_biaya";
    }

}
