<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*	
 *	Author 	: Wahyu / Tanggal : 22-07-2014	
 *	Revisi 	: Ferry / Tanggal : 15-12-2014 / Penambahan Account G/L
 */
 
class Preference_item extends CI_Controller {
    public $menu_id = 90;

    public function __construct() {
        parent::__construct();
        $this->load->model('m_menu');
        $this->load->model('m_preference');
        $this->load->model('list/m_coa');
		$this->load->model('list/m_items');
    }
	
	function item_category(){
		$get_data_item_category = $this->m_preference->get_all_item_category();
		echo json_encode($get_data_item_category);
	}
	
	function akun_sales(){
		$company				 		= $this->session->userdata('company');	
		$akun_sales 					= array('Revenue','Other Revenue');
		
		$get_data_akun_sales = $this->m_preference->get_akun_sub_type_name($company, $akun_sales);
		echo json_encode($get_data_akun_sales);
	}
	
	function akun_cogs(){
		$company				 		= $this->session->userdata('company');	
		$akun_cogs 						= array('COGS / COGM (Cost Of Goods Made)');
		
		$get_data_akun_cogs = $this->m_preference->get_akun_sub_type_name($company, $akun_cogs);
		echo json_encode($get_data_akun_cogs);
	}
	
	function akun_inventory(){
		$company				 		= $this->session->userdata('company');	
		$akun_inventory 				= array('Inventory','Fixed Asset');
		
		$get_data_akun_inventory = $this->m_preference->get_akun_by_type_name($akun_inventory);
		echo json_encode($get_data_akun_inventory);
	}
	
	function akun_taxable_pph22(){
		$company				 		= $this->session->userdata('company');	
		$akun_taxable_pph22 			= array('Pajak Dibayar Di Muka');
		
		$get_data_akun_taxable_pph22 = $this->m_preference->get_akun_sub_type_name($company, $akun_taxable_pph22);
		echo json_encode($get_data_akun_taxable_pph22);
	}
	
	function akun_sr(){
		$company				 		= $this->session->userdata('company');	
		$akun_sr 						= array('Retur Penjualan');
		
		$get_data_akun_inventory = $this->m_preference->get_akun_sub_type_name($company, $akun_sr);
		echo json_encode($get_data_akun_inventory);
	}
	
	function akun_pr(){
		$company				 		= $this->session->userdata('company');	
		$akun_pr 						= array('Retur Pembelian');
		
		$get_data_akun_pr = $this->m_preference->get_akun_sub_type_name($company, $akun_pr);
		echo json_encode($get_data_akun_pr);
	}
	
	function akun_unbilled(){
		$company				 		= $this->session->userdata('company');	
		$akun_unbilled 					= array('Hutang Yang Belum DiTagih');
		
		$get_data_akun_unbilled = $this->m_preference->get_akun_sub_type_name($company, $akun_unbilled);
		echo json_encode($get_data_akun_unbilled);
	}
	
    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 {	
                    $company				 		= $this->session->userdata('company');	
            		
            		//$akun_sales 					= array('Revenue','Other Revenue');
            		//$akun_cogs 						= array('COGS / COGM (Cost Of Goods Made)');
            		//$akun_inventory 				= array('Inventory','Fixed Asset');
            		//$akun_taxable_pph22 			= array('Pajak Dibayar Di Muka');
            		//$akun_sr 						= array('Retur Penjualan');
            		//$akun_pr 						= array('Retur Pembelian');
            		//$akun_unbilled 					= array('Hutang Yang Belum DiTagih');
            		 
                   // $data['item_category']          = $this->m_preference->get_all_item_category();
                    //$data['akun_sales']             = $this->m_preference->get_akun_sub_type_name($company, $akun_sales);
                    //$data['akun_cogs']              = $this->m_preference->get_akun_sub_type_name($company, $akun_cogs);
                    //$data['akun_inventory']         = $this->m_preference->get_akun_by_type_name($akun_inventory);
                    
                    //$data['akun_taxable_pph22']     = $this->m_preference->get_akun_sub_type_name($company, $akun_taxable_pph22);
                    //$data['akun_sr']                = $this->m_preference->get_akun_sub_type_name($company, $akun_sr);
                    //$data['akun_pr']                = $this->m_preference->get_akun_sub_type_name($company, $akun_pr);
            //        $data['akun_expense']           = $this->m_preference->get_all_akun($company);
                   // $data['akun_unbilled']          = $this->m_preference->get_akun_sub_type_name($company, $akun_unbilled);

                    $data['jobtitle'] = $this->session->userdata('jobtitle');	
                    $this->load->view('v_header');
                    $this->load->view('preference/view_preference_item', $data);
                    $this->load->view('v_footer');
                }
            }else{
                redirect(base_url() . 'error', 'refresh');
            }
        }else{
           redirect(base_url() . 'login', 'refresh'); 
        }
    }
	
    //PROCESS POST DATA
    function proses_post_data_prefernce_item_account(){
        try{
            $dataSend = [];
            $dataSend['data_sysitemcategory']   = [];
            $dataSend['data_sysitemaccount_insert'] = [];
//            $dataSend['data_sales_akun_insert'] = [];
            $dataSend['data_sales_akun_update'] = [];
//            $dataSend['data_cogs_akun_insert']  = [];
            $dataSend['data_cogs_akun_update']  = [];
//            $dataSend['data_inventory_insert']  = [];
            $dataSend['data_inventory_update']  = [];
            $dataSend['data_coa_update']        = [];
//            $dataSend['data_taxable22_insert']  = [];
            $dataSend['data_taxable22_update']  = [];
//            $dataSend['data_sr_insert']         = [];
            $dataSend['data_sr_update']         = [];
//            $dataSend['data_pr_insert']         = [];
            $dataSend['data_pr_update']         = [];
//            $dataSend['data_unbilled_insert']   = [];
            $dataSend['data_unbilled_update']   = [];
            $dataSend['ItemCategoryID']         = $this->input->post('select_item_category');
            
            //================ set sys_itemcategory ============================//
            $CompanyCode    = $this->session->userdata('company');	
            $ItemCategoryID = $this->input->post('select_item_category');

            /*========== Pemakaian Item yang status gantung */
            $get_inventory = $this->m_items->get_all_item_on_transaction_by_categories($ItemCategoryID, $CompanyCode);
            if(!empty($get_inventory->item_code)){
                throw new Exception('Maaf penggantian preference gagal, masih ada transaksi di '.$get_inventory->trans_type.' untuk company '.$get_inventory->company_code.' yang gantung untuk kategori ini, No Transaksi : '.$get_inventory->no_transaksi.'');
            }
            //================END set sys_itemcategory ============================//

            $cek_sysitemcategoryid = $this->m_preference->get_sysitem_categori_id($ItemCategoryID);
            if(empty($cek_sysitemcategoryid)){
                $dataSend['data_sysitemcategory'][] = array(
                    'CompanyCode'       => $CompanyCode, 
                    'ItemCategoryID'    => $ItemCategoryID, 
                    'LastUser'          => $this->session->userdata('username')
                );
//                $this->m_preference->insert_sys_itemcategory($data_sysitemcategory); //<-- masuk ke model
            }

            $sysitemcategoryid = $this->m_preference->get_sysitem_categori_id($ItemCategoryID);
            $code_sales_akun                = 'Sales';
            $code_cogs_akun                 = 'COGS';
            $code_inventory_akun            = 'Inventory';
            $code_taxable22_akun            = 'TaxablePPH22';
            $code_sr_akun                   = 'SalesReturn';
            $code_pr_akun                   = 'PurchaseReturn';
            $code_unbilled_akun             = 'Unbilled';
            
            //========= Sales akun ==================//
            $cmb_akun_sales = $this->input->post('select_item_sales_account');
            $cek_akun       = $this->m_preference->cek_kode_on_preference($code_sales_akun, $sysitemcategoryid);
            //throw new Exception("ItemCategoryID = ".$ItemCategoryID."; sysitemcategoryid = ".$sysitemcategoryid."; cek_akun=".$cek_akun);
            if(empty($cek_akun)){
//                $dataSend['data_sales_akun_insert'][] = array(
                $dataSend['data_sysitemaccount_insert'][] = array(
                    'SysItemCategoryID' => $sysitemcategoryid, 
                    'AccountCode'       => $code_sales_akun,
                    'AccountNumber'     => ($cmb_akun_sales == "")? NULL:nl2br($cmb_akun_sales), 
                    'LastUser'          => $this->session->userdata('username')
                );
//                $this->m_preference->insert_sysitemaccount($data_sysitemaccount);//<-- masuk ke model
            }else {
                $dataSend['data_sales_akun_update'][] = array(
                    'AccountCode'       => $code_sales_akun,
                    'AccountNumber'     => ($cmb_akun_sales == "")? NULL:nl2br($cmb_akun_sales), 
                    'LastUser'          => $this->session->userdata('username'),
                     
                    'sysitemcategoryid' => $sysitemcategoryid, //untuk parameter di model
                    'code_sales_akun'   => $code_sales_akun //untuk parameter di model
                );
//                $this->m_preference->update_sysitemaccount($data_sysitemaccount, $sysitemcategoryid, $code_sales_akun);//<-- masuk ke model
            }
            //=========END Sales akun ==================//

            //======== COGS Akun =======================//
            $cmb_akun_cogs  = $this->input->post('select_item_cogs_account');
            $cek_akun       = $this->m_preference->cek_kode_on_preference($code_cogs_akun, $sysitemcategoryid);
            if(empty($cek_akun)){
//                $dataSend['data_cogs_akun_insert'][] = array(
                $dataSend['data_sysitemaccount_insert'][] = array(
                    'SysItemCategoryID' => $sysitemcategoryid, 
                    'AccountCode'       => $code_cogs_akun,
                    'AccountNumber'     => ($cmb_akun_cogs == "")? NULL:nl2br($cmb_akun_cogs), 
                    'LastUser'          => $this->session->userdata('username')
                );
//                $this->m_preference->insert_sysitemaccount($data_sysitemaccount); // <-- masuk model
            }else{
                $dataSend['data_cogs_akun_update'][] = array(
                    'AccountCode'       => $code_cogs_akun,
                    'AccountNumber'     => ($cmb_akun_cogs == "")? NULL:nl2br($cmb_akun_cogs),
                    'LastUser'          => $this->session->userdata('username'),
                        
                    'sysitemcategoryid' => $sysitemcategoryid,  // untuk parameter di model
                    'code_cogs_akun'    => $code_cogs_akun      // untuk parameter di model
                );
//                $this->m_preference->update_sysitemaccount($data_sysitemaccount, $sysitemcategoryid, $code_cogs_akun);
            }
            //======== END COGS Akun =======================//

            //======== Inventory ===========================//
            $cmb_akun_inventory = $this->input->post('select_item_inventory_account');
            $cek_akun           = $this->m_preference->cek_kode_on_preference($code_inventory_akun, $sysitemcategoryid);
            if(empty($cek_akun)){
//                $dataSend['data_inventory_insert'][] = array(
                $dataSend['data_sysitemaccount_insert'][] = array(
                    'SysItemCategoryID' => $sysitemcategoryid, 
                    'AccountCode'       => $code_inventory_akun,
                    'AccountNumber'     => ($cmb_akun_inventory == "")? NULL:nl2br($cmb_akun_inventory),
                    'LastUser'          => $this->session->userdata('username')
                );
//                $this->m_preference->insert_sysitemaccount($data_sysitemaccount); // <-- masuk ke model
            }else{
                $dataSend['data_inventory_update'][] = array(
                    'SysItemCategoryID' => $sysitemcategoryid, 
                    'AccountCode'       => $code_inventory_akun,
                    'AccountNumber'     => ($cmb_akun_inventory == "")? NULL:nl2br($cmb_akun_inventory),
                    'LastUser'          => $this->session->userdata('username'),
                    
                    'sysitemcategoryid'  => $sysitemcategoryid,     //untuk parameter update di model
                    'code_inventory_akun'=> $code_inventory_akun    //untuk parameter update du model
                );
//                $this->m_preference->update_sysitemaccount($data_sysitemaccount, $sysitemcategoryid, $code_inventory_akun); // <-- masuk ke model
            }

            $coa_itemcategory   = $this->m_coa->get_by_id($cmb_akun_inventory);
            $item_category      = empty($coa_itemcategory) ? '' : $coa_itemcategory->ItemCategory;
            if($item_category != ''){
                $item_category = explode(',', $item_category);
                if(!in_array($ItemCategoryID, $item_category)){
                    array_push($item_category, $ItemCategoryID);
                    $item_category = implode(',', $item_category);
                    $dataSend['data_coa_update'][] = array(
                        'ItemCategory'      => $item_category,
                        'LastUser'          => $this->session->userdata('username'),
                        
                        'cmb_akun_inventory'=> $cmb_akun_inventory // untup parameter update di model
                    );
//                    $this->m_coa->update($data_coa, $cmb_akun_inventory); // masuk ke model
                }
            }else {
                $dataSend['data_coa_update'][] = array(
                    'ItemCategory'      => $ItemCategoryID,
                    'LastUser'          => $this->session->userdata('username'),
                        
                    'cmb_akun_inventory'=> $cmb_akun_inventory // untup parameter update di model
                );
//                $this->m_coa->update($data_coa, $cmb_akun_inventory);
            }
            //======== END Inventory ===========================//

            //======== Taxable22 ===============================//
            $cmb_akun_taxable22 = $this->input->post('select_item_taxable22');
            $cek_akun           = $this->m_preference->cek_kode_on_preference($code_taxable22_akun, $sysitemcategoryid);
            if(empty($cek_akun)){
//                $dataSend['data_taxable22_insert'][] = array(
                $dataSend['data_sysitemaccount_insert'][] = array(
                    'SysItemCategoryID' => $sysitemcategoryid, 
                    'AccountCode'       => $code_taxable22_akun,
                    'AccountNumber'     => ($cmb_akun_taxable22 == "")? NULL:nl2br($cmb_akun_taxable22),
                    'LastUser'          => $this->session->userdata('username')
                );
//                $this->m_preference->insert_sysitemaccount($data_sysitemaccount);
            }else{
                $dataSend['data_taxable22_update'][] = array(
                    'AccountCode'       => $code_taxable22_akun,
                    'AccountNumber'     => ($cmb_akun_taxable22 == "")? NULL:nl2br($cmb_akun_taxable22),
                    'LastUser'          => $this->session->userdata('username'),
                    
                    'sysitemcategoryid'     => $sysitemcategoryid,  // untuk parameter update di model
                    'code_taxable22_akun'   => $code_taxable22_akun // untuk parameter update di model                   
                );
//                $this->m_preference->update_sysitemaccount($data_sysitemaccount, $sysitemcategoryid, $code_taxable22_akun);// masuk ke model
            }
            //======== End Taxable22 ===============================//

            //========= SR =========================================//
            $cmb_akun_sr    = $this->input->post('select_item_account_sr');
            $cek_akun       = $this->m_preference->cek_kode_on_preference($code_sr_akun, $sysitemcategoryid);
            if(empty($cek_akun)){
//                $dataSend['data_sr_insert'][] = array(
                $dataSend['data_sysitemaccount_insert'][] = array(
                    'SysItemCategoryID' => $sysitemcategoryid, 
                    'AccountCode'       => $code_sr_akun,
                    'AccountNumber'     => ($cmb_akun_sr == "")? NULL:nl2br($cmb_akun_sr),
                    'LastUser'          => $this->session->userdata('username')
                );
//                $this->m_preference->insert_sysitemaccount($data_sysitemaccount); // masuk ke model
            }else{
                $dataSend['data_sr_update'][] = array(
                    'AccountCode'       => $code_sr_akun,
                    'AccountNumber'     => ($cmb_akun_sr == "")? NULL:nl2br($cmb_akun_sr),
                    'LastUser'          => $this->session->userdata('username'),
                    
                    'sysitemcategoryid' => $sysitemcategoryid,
                    'code_sr_akun'      => $code_sr_akun
                );
//                $this->m_preference->update_sysitemaccount($data_sysitemaccount, $sysitemcategoryid, $code_sr_akun);
            }
            //========= END SR =========================================//

            //========= PR =============================================//
            $cmb_akun_pr    = $this->input->post('select_item_account_pr');
            $cek_akun       = $this->m_preference->cek_kode_on_preference($code_pr_akun, $sysitemcategoryid);
            if(empty($cek_akun)){
//                $dataSend['data_pr_insert'][] = array(
                $dataSend['data_sysitemaccount_insert'][] = array(
                    'SysItemCategoryID' => $sysitemcategoryid, 
                    'AccountCode'       => $code_pr_akun,
                    'AccountNumber'     => ($cmb_akun_pr == "")? NULL:nl2br($cmb_akun_pr),
                    'LastUser'          => $this->session->userdata('username')
                );
//                $this->m_preference->insert_sysitemaccount($data_sysitemaccount);
            }else{
                $dataSend['data_pr_update'][] = array(
                    'AccountCode'       => $code_pr_akun,
                    'AccountNumber'     => ($cmb_akun_pr == "")? NULL:nl2br($cmb_akun_pr),
                    'LastUser'          => $this->session->userdata('username'),
                    
                    'sysitemcategoryid' => $sysitemcategoryid,  // untuk parameter update di model
                    'code_pr_akun'      => $code_pr_akun        // untuk parameter update di model
                );
//                $this->m_preference->update_sysitemaccount($data_sysitemaccount, $sysitemcategoryid, $code_pr_akun);
            }
            //========= END PR =============================================//

            //========= unbilled ==========================================//
            $cmb_akun_unbilled  = $this->input->post('select_account_unbilled');
            $cek_akun           = $this->m_preference->cek_kode_on_preference($code_unbilled_akun, $sysitemcategoryid);
            if(empty($cek_akun)){
//                $dataSend['data_unbilled_insert'][] = array(
                $dataSend['data_sysitemaccount_insert'][] = array(
                    'SysItemCategoryID' => $sysitemcategoryid, 
                    'AccountCode'       => $code_unbilled_akun,
                    'AccountNumber'     => ($cmb_akun_unbilled == "")? NULL:nl2br($cmb_akun_unbilled),
                    'LastUser'          => $this->session->userdata('username')
                );
//                $this->m_preference->insert_sysitemaccount($data_sysitemaccount);
            }else{
                $dataSend['data_unbilled_update'][] = array(
                    'AccountCode'       => $code_unbilled_akun,
                    'AccountNumber'     => ($cmb_akun_unbilled == "")? NULL:nl2br($cmb_akun_unbilled),
                    'LastUser'          => $this->session->userdata('username'),
                    
                    'sysitemcategoryid' => $sysitemcategoryid,  // untuk parameter update di model
                    'code_unbilled_akun'=> $code_unbilled_akun  // untuk parameter update di model
                );
//                $this->m_preference->update_sysitemaccount($data_sysitemaccount, $sysitemcategoryid, $code_unbilled_akun);
            }
            //========= END unbilled ==========================================//
            
            $rstDataSend = $this->m_preference->data_send_preference_item($dataSend);
            if($rstDataSend){
                $result['status'] 	= true;
                $result['message']	= "Preferences Berhasil Disimpan";                
            }else{
                $result['status'] 	= false;
                $result['message']	= "Preferences Gagal Disimpan";
            }
            echo json_encode($result);
        }catch (Exception $e){            
            $result['status']   = false;
            $result['message']  = $e->getMessage();
            echo json_encode($result);
        }
    }
    function proses_post_data_prefernce_item_account_LAMA(){
        //set sys_itemcategory
        $CompanyCode    = $this->session->userdata('company');	
        $ItemCategoryID = $this->input->post('select_item_category');
		
		/*
			Pemakaian Item yang status gantung
		*/
		$get_inventory = $this->m_items->get_all_item_on_transaction_by_categories($ItemCategoryID, $CompanyCode);
		if(!empty($get_inventory->item_code)){
			$result['status'] = false;
            $result['message'] = 'Maaf penggantian preference gagal, masih ada transaksi di '.$get_inventory->trans_type.' untuk company '.$get_inventory->company_code.' yang gantung untuk kategori ini, No Transaksi : '.$get_inventory->no_transaksi.'';
			echo json_encode($result);
			die();
		}
		/*======================End===========================*/
		
        $cek_sysitemcategoryid = $this->m_preference->get_sysitem_categori_id($ItemCategoryID);
		
        if(empty($cek_sysitemcategoryid)){
            $data_sysitemcategory = array(
                                        'CompanyCode'       => $CompanyCode, 
                                        'ItemCategoryID'    => $ItemCategoryID, 
                                        'LastUser'          => $this->session->userdata('username')
                                    );
			
            $this->m_preference->insert_sys_itemcategory($data_sysitemcategory);
        }
		
        $sysitemcategoryid = $this->m_preference->get_sysitem_categori_id($ItemCategoryID);
		
        $code_sales_akun                = 'Sales';
        $code_cogs_akun                 = 'COGS';
        $code_inventory_akun            = 'Inventory';
        $code_taxable22_akun            = 'TaxablePPH22';
        $code_sr_akun                   = 'SalesReturn';
        $code_pr_akun                   = 'PurchaseReturn';
        $code_unbilled_akun             = 'Unbilled';
		
        //Sales akun
        $cmb_akun_sales = $this->input->post('select_item_sales_account');
        $cek_akun       = $this->m_preference->cek_kode_on_preference($code_sales_akun, $sysitemcategoryid);
	
        if(empty($cek_akun)){
            $data_sysitemaccount = array(
                                    'SysItemCategoryID' => $sysitemcategoryid, 
                                    'AccountCode'       => $code_sales_akun,
                                    'AccountNumber'     => ($cmb_akun_sales == "")? NULL:nl2br($cmb_akun_sales), 
                                    'LastUser'          => $this->session->userdata('username')
                                    );
			
            $this->m_preference->insert_sysitemaccount($data_sysitemaccount);
        } 
        else {
            $data_sysitemaccount = array(
                                    'AccountCode'       => $code_sales_akun,
                                    'AccountNumber'     => ($cmb_akun_sales == "")? NULL:nl2br($cmb_akun_sales), 
                                    'LastUser'          => $this->session->userdata('username')
                                    );
			
            $this->m_preference->update_sysitemaccount($data_sysitemaccount, $sysitemcategoryid, $code_sales_akun);
        }
        //--------------------------------------------------------------------------------------------------------------//

        //COGS Akun
        $cmb_akun_cogs  = $this->input->post('select_item_cogs_account');
        $cek_akun       = $this->m_preference->cek_kode_on_preference($code_cogs_akun, $sysitemcategoryid);

        if(empty($cek_akun)){
            $data_sysitemaccount = array(
                                    'SysItemCategoryID' => $sysitemcategoryid, 
                                    'AccountCode'       => $code_cogs_akun,
                                    'AccountNumber'     => ($cmb_akun_cogs == "")? NULL:nl2br($cmb_akun_cogs), 
                                    'LastUser'          => $this->session->userdata('username')
                                    );
		
            $this->m_preference->insert_sysitemaccount($data_sysitemaccount);
        }
        else{
            $data_sysitemaccount = array(
                                    'AccountCode'       => $code_cogs_akun,
                                    'AccountNumber'     => ($cmb_akun_cogs == "")? NULL:nl2br($cmb_akun_cogs),
                                    'LastUser'          => $this->session->userdata('username')
                                    );
		
            $this->m_preference->update_sysitemaccount($data_sysitemaccount, $sysitemcategoryid, $code_cogs_akun);
        }
        //--------------------------------------------------------------------------------------------------------------//
		
        //Inventory
        $cmb_akun_inventory = $this->input->post('select_item_inventory_account');
        $cek_akun           = $this->m_preference->cek_kode_on_preference($code_inventory_akun, $sysitemcategoryid);

        if(empty($cek_akun)){
            $data_sysitemaccount = array(
                                    'SysItemCategoryID' => $sysitemcategoryid, 
                                    'AccountCode'       => $code_inventory_akun,
                                    'AccountNumber'     => ($cmb_akun_inventory == "")? NULL:nl2br($cmb_akun_inventory),
                                    'LastUser'          => $this->session->userdata('username')
                                    );
			
            $this->m_preference->insert_sysitemaccount($data_sysitemaccount);
        }
        else{
            $data_sysitemaccount = array(
                                    'SysItemCategoryID' => $sysitemcategoryid, 
                                    'AccountCode'       => $code_inventory_akun,
                                    'AccountNumber'     => ($cmb_akun_inventory == "")? NULL:nl2br($cmb_akun_inventory),
                                    'LastUser'          => $this->session->userdata('username')
                                    );
			
            $this->m_preference->update_sysitemaccount($data_sysitemaccount, $sysitemcategoryid, $code_inventory_akun);
        }

        $coa_itemcategory   = $this->m_coa->get_by_id($cmb_akun_inventory);
        $item_category      = empty($coa_itemcategory) ? '' : $coa_itemcategory->ItemCategory;
        
        if($item_category != ''){
            $item_category = explode(',', $item_category);

            if(!in_array($ItemCategoryID, $item_category)){
                array_push($item_category, $ItemCategoryID);
                $item_category = implode(',', $item_category);
                
                $data_coa = array(
                                'ItemCategory'      => $item_category,
                                'LastUser'          => $this->session->userdata('username')
                            );
                
                $this->m_coa->update($data_coa, $cmb_akun_inventory);
            }
        }
        else {
            $data_coa = array(
                            'ItemCategory'      => $ItemCategoryID,
                            'LastUser'          => $this->session->userdata('username')
                        );

            $this->m_coa->update($data_coa, $cmb_akun_inventory);
        }
        //--------------------------------------------------------------------------------------------------------------//
        
        //Taxable22
        $cmb_akun_taxable22 = $this->input->post('select_item_taxable22');
        $cek_akun           = $this->m_preference->cek_kode_on_preference($code_taxable22_akun, $sysitemcategoryid);
	
        if(empty($cek_akun)){
            $data_sysitemaccount = array(
                                    'SysItemCategoryID' => $sysitemcategoryid, 
                                    'AccountCode'       => $code_taxable22_akun,
                                    'AccountNumber'     => ($cmb_akun_taxable22 == "")? NULL:nl2br($cmb_akun_taxable22),
                                    'LastUser'          => $this->session->userdata('username')
                                    );
			
            $this->m_preference->insert_sysitemaccount($data_sysitemaccount);
        }
        else{
            $data_sysitemaccount = array(
                                    'AccountCode'       => $code_taxable22_akun,
                                    'AccountNumber'     => ($cmb_akun_taxable22 == "")? NULL:nl2br($cmb_akun_taxable22),
                                    'LastUser'          => $this->session->userdata('username')
                                    );
			
            $this->m_preference->update_sysitemaccount($data_sysitemaccount, $sysitemcategoryid, $code_taxable22_akun);
        }
        //--------------------------------------------------------------------------------------------------------------//
		
        //SR
        $cmb_akun_sr    = $this->input->post('select_item_account_sr');
        $cek_akun       = $this->m_preference->cek_kode_on_preference($code_sr_akun, $sysitemcategoryid);
	
        if(empty($cek_akun)){
            $data_sysitemaccount = array(
                                    'SysItemCategoryID' => $sysitemcategoryid, 
                                    'AccountCode'       => $code_sr_akun,
                                    'AccountNumber'     => ($cmb_akun_sr == "")? NULL:nl2br($cmb_akun_sr),
                                    'LastUser'          => $this->session->userdata('username')
                                    );
			
            $this->m_preference->insert_sysitemaccount($data_sysitemaccount);
        }
        else{
            $data_sysitemaccount = array(
                                    'AccountCode'       => $code_sr_akun,
                                    'AccountNumber'     => ($cmb_akun_sr == "")? NULL:nl2br($cmb_akun_sr),
                                    'LastUser'          => $this->session->userdata('username')
                                    );
			
            $this->m_preference->update_sysitemaccount($data_sysitemaccount, $sysitemcategoryid, $code_sr_akun);
        }
        //--------------------------------------------------------------------------------------------------------------//
		
        //PR
        $cmb_akun_pr    = $this->input->post('select_item_account_pr');
        $cek_akun       = $this->m_preference->cek_kode_on_preference($code_pr_akun, $sysitemcategoryid);

        if(empty($cek_akun)){
            $data_sysitemaccount = array(
                                    'SysItemCategoryID' => $sysitemcategoryid, 
                                    'AccountCode'       => $code_pr_akun,
                                    'AccountNumber'     => ($cmb_akun_pr == "")? NULL:nl2br($cmb_akun_pr),
                                    'LastUser'          => $this->session->userdata('username')
                                    );
			
            $this->m_preference->insert_sysitemaccount($data_sysitemaccount);
        }
        else{
            $data_sysitemaccount = array(
                                    'AccountCode'       => $code_pr_akun,
                                    'AccountNumber'     => ($cmb_akun_pr == "")? NULL:nl2br($cmb_akun_pr),
                                    'LastUser'          => $this->session->userdata('username')
                                    );
			
            $this->m_preference->update_sysitemaccount($data_sysitemaccount, $sysitemcategoryid, $code_pr_akun);
        }
        //--------------------------------------------------------------------------------------------------------------//
        
        //unbilled
        $cmb_akun_unbilled  = $this->input->post('select_account_unbilled');
        $cek_akun           = $this->m_preference->cek_kode_on_preference($code_unbilled_akun, $sysitemcategoryid);

        if(empty($cek_akun)){
            $data_sysitemaccount = array(
                                    'SysItemCategoryID' => $sysitemcategoryid, 
                                    'AccountCode'       => $code_unbilled_akun,
                                    'AccountNumber'     => ($cmb_akun_unbilled == "")? NULL:nl2br($cmb_akun_unbilled),
                                    'LastUser'          => $this->session->userdata('username')
                                    );

            $this->m_preference->insert_sysitemaccount($data_sysitemaccount);
        }
        else{
            $data_sysitemaccount = array(
                                    'AccountCode'       => $code_unbilled_akun,
                                    'AccountNumber'     => ($cmb_akun_unbilled == "")? NULL:nl2br($cmb_akun_unbilled),
                                    'LastUser'          => $this->session->userdata('username')
                                    );

            $this->m_preference->update_sysitemaccount($data_sysitemaccount, $sysitemcategoryid, $code_unbilled_akun);
        }
        //--------------------------------------------------------------------------------------------------------------//

        $result['status'] 	= true;
        $result['message']	= "Preferences Berhasil Disimpan";
        echo json_encode($result);		
    }
	
    //get value akun sales
    function combo_list_sales_item_account(){
        $item_id                = $this->input->post('item_id');
        $akun_value_sales       = $this->m_preference->get_akun_sales_value($item_id);

        echo "$akun_value_sales";
    }
	
    //get value akun cogs
    function combo_list_cogs_item_account(){
        $item_id                = $this->input->post('item_id');
        $akun_value_cogs        = $this->m_preference->get_akun_cogs_value($item_id);

        echo "$akun_value_cogs";
    }
	
    //get value akun inventory
    function combo_list_inventory_item_account(){
        $item_id                = $this->input->post('item_id');
        $akun_value_inventory   = $this->m_preference->get_akun_inventory_value($item_id);
        
        echo "$akun_value_inventory";
    }
		
    //get value akun taxable22
    function combo_list_taxable22_item_account(){
        $item_id                = $this->input->post('item_id');
        $akun_value_taxable22   = $this->m_preference->get_akun_taxable22_value($item_id);
        
        echo "$akun_value_taxable22";
    }
		
    //get value akun sr
    function combo_list_sr_item_account(){
        $item_id                = $this->input->post('item_id');
        $akun_value_sr          = $this->m_preference->get_akun_sr_value($item_id);
    
        echo "$akun_value_sr";
    }
	
    //get value akun pr
    function combo_list_pr_item_account(){
        $item_id                = $this->input->post('item_id');
        $akun_value_sr          = $this->m_preference->get_akun_pr_value($item_id);
        
        echo "$akun_value_sr";
    }
	
    //get value akun unbilled
    function combo_list_unbilled_item_account(){
        $item_id                = $this->input->post('item_id');
        $akun_value_unbilled    = $this->m_preference->get_akun_unbilled_value($item_id);
    
        echo "$akun_value_unbilled";
    }
}

/* End of file preference.php */
/* Location: ./application/controllers/preference.php */