<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*	
 *	Author 	: Wahyu / 22-07-2014	
 *	Revisi 	: Ferry / December 2014
 */
 
class Preference_purchases extends CI_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->model('m_preference');
		$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_payable(){
		$company				 		= $this->session->userdata('company');	
		$akun_payable 						= array(47,37,36,38,43,35);
		
		$get_data_akun_payable = $this->m_preference->get_akun_advance($company, $akun_payable);
		echo json_encode($get_data_akun_payable);
	}
	
	function akun_advance_purchase(){
		$company				 		= $this->session->userdata('company');	
		$akun_advance_purchase 				= array(28);
		
		$get_data_akun_advance_purchase = $this->m_preference->get_akun_advance($company, $akun_advance_purchase);
		echo json_encode($get_data_akun_advance_purchase);
	}
	
	function akun_discount_invoice(){
		$company				 		= $this->session->userdata('company');	
		$akun_discount_invoice 			= 'Potongan Pembelian';
		
		$get_data_akun_discount_invoice = $this->m_preference->get_coa_account($akun_discount_invoice, 2);
		echo json_encode($get_data_akun_discount_invoice);
	}
	
	function akun_discount_payment(){
		$company				 		= $this->session->userdata('company');	
		$akun_discount_payment 				= array('Retur Pembelian');
		
		$get_data_akun_discount_payment = $this->m_preference->get_akun_sub_type_name($company, $akun_discount_payment);
		echo json_encode($get_data_akun_discount_payment);
	}
	
	function akun_taxable_purchase(){
		$company				 		= $this->session->userdata('company');	
		$akun_taxable_purchase 				= array('PPN Masukan');
		
		$get_data_akun_taxable_purchase = $this->m_preference->get_coa_account($akun_taxable_purchase, 4);
		echo json_encode($get_data_akun_taxable_purchase);
	}
	
	function akun_purchase_freight(){
		$company				 		= $this->session->userdata('company');	
		$akun_purchase_freight 				= array('Biaya Freight In');
		
		$get_data_akun_purchase_freight = $this->m_preference->get_akun_sub_type_name($company, $akun_purchase_freight);
		echo json_encode($get_data_akun_purchase_freight);
	}
	
	function akun_purchase_realized(){
		$company				 		= $this->session->userdata('company');	
		$akun_purchase_realized 			= array('Selisih Kurs Transaksi');
		
		$get_data_akun_purchase_realized = $this->m_preference->get_akun_sub_type_name($company, $akun_purchase_realized);
		echo json_encode($get_data_akun_purchase_realized);
	}
	
	function akun_purchase_unrealized(){
		$company				 		= $this->session->userdata('company');	
		$akun_purchase_unrealized 			= array('Selisih Kurs Akhir Periode');
		
		$get_data_akun_purchase_unrealized = $this->m_preference->get_akun_sub_type_name($company, $akun_purchase_unrealized);
		echo json_encode($get_data_akun_purchase_unrealized);
	}
	
    public function index(){
        $company = $this->session->userdata('company');		
        //$data['item_category']              = $this->m_preference->get_all_item_category();
        
		//$akun_payable 						= array(47,37,36,38,43,35);
		//$akun_advance_purchase 				= array(28);
													
		//$akun_discount_invoice 				= 'Potongan Pembelian';
		//$akun_discount_payment 				= array('Retur Pembelian');
		//$akun_taxable_purchase 				= array('PPN Masukan');
		//$akun_purchase_freight 				= array('Biaya Freight In');
		//$akun_purchase_realized 			= array('Selisih Kurs Transaksi');
		//$akun_purchase_unrealized 			= array('Selisih Kurs Akhir Periode');
		
        //$data['akun_payable']               = $this->m_preference->get_akun_advance($company, $akun_payable);
        //$data['akun_advance_purchase']      = $this->m_preference->get_akun_advance($company, $akun_advance_purchase);
       
		//$data['akun_discount_invoice']      = $this->m_preference->get_coa_account($akun_discount_invoice, 2);
		
        //$data['akun_discount_payment']      = $this->m_preference->get_akun_sub_type_name($company, $akun_discount_payment);
       // $data['akun_taxable_purchase']      = $this->m_preference->get_coa_account($akun_taxable_purchase, 4);
		
//        $data['akun_taxable_purchase']      = $this->m_preference->get_akun($company, $id='32');
       // $data['akun_purchase_freight']      = $this->m_preference->get_akun_sub_type_name($company, $akun_purchase_freight);
       // $data['akun_purchase_realized']     = $this->m_preference->get_akun_sub_type_name($company, $akun_purchase_realized);
       // $data['akun_purchase_unrealized']   = $this->m_preference->get_akun_sub_type_name($company, $akun_purchase_unrealized);
		$data['jobtitle'] = $this->session->userdata('jobtitle');	
        $this->load->view('v_header');
        $this->load->view('preference/view_preference_purchases', $data);
        $this->load->view('v_footer');
    }
		
    //PROCESS POST DATA
    function proses_post_data_prefernce_purchase_account(){
        try{
            $dataSend = [];
            $dataSend['data_purchasecategoryid']                = [];
            $dataSend['data_sysaccount_purchase_insert']        = [];
            $dataSend['data_purchase_account_payable_update']   = [];
            $dataSend['data_advance_purchase_account_update']   = [];
            $dataSend['data_discount_invoice_account_update']   = [];
            $dataSend['data_discount_payment_account_update']   = [];
            $dataSend['data_taxable_purchase_account_update']   = [];
            $dataSend['data_purchase_freight_update']           = [];
            $dataSend['data_realized_gained_account_update']    = [];
            $dataSend['data_unrealized_gained_account_update']  = [];
            
            $dataSend['ItemCategoryID'] = $this->input->post('select_item_category');           
            
            //============== set sys_itemcategory ============================//
            $CompanyCode = $this->session->userdata('company');	
            $ItemCategoryID = $this->input->post('select_item_category');
            $sys_purchasecategoryid = $this->m_preference->get_purchase_categori_id($ItemCategoryID);
            //============== END set sys_itemcategory ========================//
            
            //============== 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 Pemakaian Item yang status gantung ==============//

            if(empty($sys_purchasecategoryid)){
                $dataSend['data_purchasecategoryid'][] = array(
                    'CompanyCode' => $CompanyCode, 
                    'ItemCategoryID' => $ItemCategoryID, 
                    'LastUser' => $this->session->userdata('username')
                );
//                $this->m_preference->insert_sys_purchasecategory($data_purchasecategoryid);
            }
            $syspurchasecategoryid = $this->m_preference->get_purchase_categori_id($ItemCategoryID);//dipakai juga di model,untuk dapet syspurchasecategoryid, karena pakai proses simpan array dulu

            //============== set sys purchase account ========================//
            $code_payable_akun              = 'Payable';
            $code_advance_purchase_akun     = 'AdvancePurchase';
            $code_discount_invoice_akun     = 'InvoiceDiscount';
            $code_discount_payment_akun     = 'PaymentDiscount';
            $code_taxable_purchase_akun     = 'PurchaseTaxable';
            $code_purchase_freight_akun     = 'PurchaseFreight';
            $code_realized_purchase_akun    = 'RealizedGainedLoss';
            $code_unrealized_purchase_akun  = 'UnRealizedGainedLoss';
            //============== END set sys purchase account ====================//

            //=============== Purchase Account Payable =======================//
            $cmb_akun_payable = $this->input->post('select_account_pi_payablee');
            $cek_akun = $this->m_preference->cek_kode_on_purchase_account($code_payable_akun, $syspurchasecategoryid);
            if(empty($cek_akun)){
                $dataSend['data_sysaccount_purchase_insert'][] = array(
                    'SysPurchaseCategoryID'     => $syspurchasecategoryid, 
                    'AccountCode'               => $code_payable_akun,
                    'AccountNumber'             => ($cmb_akun_payable == "")? NULL:nl2br($cmb_akun_payable),
                    'LastUser'                  => $this->session->userdata('username')
                );
//                $this->m_preference->insert_sysaccount_purchase($data_sysaccount_purchase);//masuk ke model
            }else {
                $dataSend['data_purchase_account_payable_update'][] = array(
                    'AccountCode'               => $code_payable_akun,
                    'AccountNumber'             => ($cmb_akun_payable == "")? NULL:nl2br($cmb_akun_payable),
                    'LastUser'                  => $this->session->userdata('username'),
                    
                    'code_payable_akun'         =>$code_payable_akun // untuk parameter update di model
                );
//                $this->m_preference->update_sysaccount_purchase($data_sysaccount_purchase, $syspurchasecategoryid, $code_payable_akun);//masuk ke model
            }
            //===============END Purchase Account Payable ====================//

            //=============== Advance Purchase Account =======================//
            $cmb_akun_advance_purchase = $this->input->post('select_account_advance_purchase');
            $cek_akun = $this->m_preference->cek_kode_on_purchase_account($code_advance_purchase_akun, $syspurchasecategoryid);
            if(empty($cek_akun)){
                $dataSend['data_sysaccount_purchase_insert'][] = array(
                    'SysPurchaseCategoryID'     => $syspurchasecategoryid, 
                    'AccountCode'               => $code_advance_purchase_akun,
                    'AccountNumber'             => ($cmb_akun_advance_purchase == "")? NULL:nl2br($cmb_akun_advance_purchase),
                    'LastUser'                  => $this->session->userdata('username')
                );
//                $this->m_preference->insert_sysaccount_purchase($data_sysaccount_purchase);//masuk ke model
            }else {
                $dataSend['data_advance_purchase_account_update'][] = array(
                    'AccountCode'               => $code_advance_purchase_akun,
                    'AccountNumber'             => ($cmb_akun_advance_purchase == "")? NULL:nl2br($cmb_akun_advance_purchase),
                    'LastUser'                  => $this->session->userdata('username'),
                    
                    'code_advance_purchase_akun'=> $code_advance_purchase_akun //untuk parameter update di model
                );
//                $this->m_preference->update_sysaccount_purchase($data_sysaccount_purchase, $syspurchasecategoryid, $code_advance_purchase_akun);//masuk ke model
            }
            //=============== END Advance Purchase Account ===================//

            //=============== Discount Invoice Account =======================//
            $cmb_akun_discount_invoice = $this->input->post('select_account_discount_invoice');
            $cek_akun = $this->m_preference->cek_kode_on_purchase_account($code_discount_invoice_akun, $syspurchasecategoryid);
            if(empty($cek_akun)){
                $dataSend['data_sysaccount_purchase_insert'][] = array(
                    'SysPurchaseCategoryID'     => $syspurchasecategoryid, 
                    'AccountCode'               => $code_discount_invoice_akun,
                    'AccountNumber'             => ($cmb_akun_discount_invoice == "")? NULL:nl2br($cmb_akun_discount_invoice),
                    'LastUser'                  => $this->session->userdata('username')
                );
//                $this->m_preference->insert_sysaccount_purchase($data_sysaccount_purchase); //masuk ke model
            }else {
                $dataSend['data_discount_invoice_account_update'][] = array(
                    'AccountCode'               => $code_discount_invoice_akun,
                    'AccountNumber'             => ($cmb_akun_discount_invoice == "")? NULL:nl2br($cmb_akun_discount_invoice),
                    'LastUser'                  => $this->session->userdata('username'),
                    
                    'code_discount_invoice_akun'=> $code_discount_invoice_akun //untuk parameter update di model
                );
//                $this->m_preference->update_sysaccount_purchase($data_sysaccount_purchase, $syspurchasecategoryid, $code_discount_invoice_akun); //masuk ke model
            }   
            //=============== End Discount Invoice Account ===================//

            //=============== Discount  Payment Account ======================//
            $cmb_akun_discount_payment = $this->input->post('select_account_discount_payment');
            $cek_akun = $this->m_preference->cek_kode_on_purchase_account($code_discount_payment_akun, $syspurchasecategoryid);
            if(empty($cek_akun)){
                $dataSend['data_sysaccount_purchase_insert'][] = array(
                    'SysPurchaseCategoryID'     => $syspurchasecategoryid, 
                    'AccountCode'               => $code_discount_payment_akun,
                    'AccountNumber'             => ($cmb_akun_discount_payment == "")? NULL:nl2br($cmb_akun_discount_payment),
                    'LastUser'                  => $this->session->userdata('username')
                );
//                $this->m_preference->insert_sysaccount_purchase($data_sysaccount_purchase);
            }else {
                $dataSend['data_discount_payment_account_update'][] = array(
                    'AccountCode'               => $code_discount_payment_akun,
                    'AccountNumber'             => ($cmb_akun_discount_payment == "")? NULL:nl2br($cmb_akun_discount_payment),
                    'LastUser'                  => $this->session->userdata('username'),
                    
                    'code_discount_payment_akun'=> $code_discount_payment_akun //untuk parameter update di model
                );
//                $this->m_preference->update_sysaccount_purchase($data_sysaccount_purchase, $syspurchasecategoryid, $code_discount_payment_akun);//masuk ke model
            }
            //=============== End Discount  Payment Account ==================//

            //=============== Taxable Purchase Account =======================//
            $cmb_akun_taxable_purchase = $this->input->post('select_account_taxable_purchase');
            $cek_akun = $this->m_preference->cek_kode_on_purchase_account($code_taxable_purchase_akun, $syspurchasecategoryid);
            if(empty($cek_akun)){
                $dataSend['data_sysaccount_purchase_insert'][] = array(
                    'SysPurchaseCategoryID'     => $syspurchasecategoryid, 
                    'AccountCode'               => $code_taxable_purchase_akun,
                    'AccountNumber'             => ($cmb_akun_taxable_purchase == "")? NULL:nl2br($cmb_akun_taxable_purchase),
                    'LastUser'                  => $this->session->userdata('username')
                );
//                $this->m_preference->insert_sysaccount_purchase($data_sysaccount_purchase); //masuk ke model
            }else {
                $dataSend['data_taxable_purchase_account_update'][] = array(
                    'AccountCode'               => $code_taxable_purchase_akun,
                    'AccountNumber'             => ($cmb_akun_taxable_purchase == "")? NULL:nl2br($cmb_akun_taxable_purchase),
                    'LastUser'                  => $this->session->userdata('username'),
                    
                    'code_taxable_purchase_akun'=> $code_taxable_purchase_akun //untuk parameter update di model
                );
//                $this->m_preference->update_sysaccount_purchase($data_sysaccount_purchase, $syspurchasecategoryid, $code_taxable_purchase_akun);//masuk ke model
            }
            //===============End  Taxable Purchase Account ===================//

            //=============== Purchase Freight ===============================//
            $cmb_akun_purchase_freight = $this->input->post('select_account_purchase_freight');
            $cek_akun = $this->m_preference->cek_kode_on_purchase_account($code_purchase_freight_akun, $syspurchasecategoryid);
            if(empty($cek_akun)){
                $dataSend['data_sysaccount_purchase_insert'][] = array(
                    'SysPurchaseCategoryID'     => $syspurchasecategoryid, 
                    'AccountCode'               => $code_purchase_freight_akun,
                    'AccountNumber'             => ($cmb_akun_purchase_freight == "")? NULL:nl2br($cmb_akun_purchase_freight),
                    'LastUser'                  => $this->session->userdata('username')
                );
//                $this->m_preference->insert_sysaccount_purchase($data_sysaccount_purchase); //masuk ke model
            }else {
                $dataSend['data_purchase_freight_update'][] = array(
                    'AccountCode'               => $code_purchase_freight_akun,
                    'AccountNumber'             => ($cmb_akun_purchase_freight == "")? NULL:nl2br($cmb_akun_purchase_freight),
                    'LastUser'                  => $this->session->userdata('username'),
                    
                    'code_purchase_freight_akun'=> $code_purchase_freight_akun //untuk parameter update di model
                );
//                $this->m_preference->update_sysaccount_purchase($data_sysaccount_purchase, $syspurchasecategoryid, $code_purchase_freight_akun); //masuk ke model
            }
            //=============== End Purchase Freight ===========================//

            //=============== Realized Gained Account ========================//
            $cmb_akun_realized = $this->input->post('select_account_realized');
            $cek_akun = $this->m_preference->cek_kode_on_purchase_account($code_realized_purchase_akun, $syspurchasecategoryid);
            if(empty($cek_akun)){
                $dataSend['data_sysaccount_purchase_insert'][] = array(
                    'SysPurchaseCategoryID'     => $syspurchasecategoryid, 
                    'AccountCode'               => $code_realized_purchase_akun,
                    'AccountNumber'             => ($cmb_akun_realized == "")? NULL:nl2br($cmb_akun_realized),
                    'LastUser'                  => $this->session->userdata('username')
                );
//                $this->m_preference->insert_sysaccount_purchase($data_sysaccount_purchase); //masuk ke model
            }else {
                $dataSend['data_realized_gained_account_update'][] = array(
                    'AccountCode'               => $code_realized_purchase_akun,
                    'AccountNumber'             => ($cmb_akun_realized == "")? NULL:nl2br($cmb_akun_realized),
                    'LastUser'                  => $this->session->userdata('username'),
                    
                    'code_realized_purchase_akun'=> $code_realized_purchase_akun //untuk parameter update di model
                );
//                $this->m_preference->update_sysaccount_purchase($data_sysaccount_purchase, $syspurchasecategoryid, $code_realized_purchase_akun); //masuk ke model
            }
            //=============== END Realized Gained Account ====================//

            //=============== Unrealized Gained Account ======================//
            $cmb_akun_unrealized = $this->input->post('select_account_unrealized');
            $cek_akun = $this->m_preference->cek_kode_on_purchase_account($code_unrealized_purchase_akun, $syspurchasecategoryid);
            if(empty($cek_akun)){
                $dataSend['data_sysaccount_purchase_insert'][] = array(
                    'SysPurchaseCategoryID'     => $syspurchasecategoryid, 
                    'AccountCode'               => $code_unrealized_purchase_akun,
                    'AccountNumber'             => ($cmb_akun_unrealized == "")? NULL:nl2br($cmb_akun_unrealized),
                    'LastUser'                  => $this->session->userdata('username')
                );
//                $this->m_preference->insert_sysaccount_purchase($data_sysaccount_purchase); //MASUK KE MODEL
            }else {
                $dataSend['data_unrealized_gained_account_update'][] = array(
                    'AccountCode'               => $code_unrealized_purchase_akun,
                    'AccountNumber'             => ($cmb_akun_unrealized == "")? NULL:nl2br($cmb_akun_unrealized),
                    'LastUser'                  => $this->session->userdata('username'),
                    
                    'code_unrealized_purchase_akun' => $code_unrealized_purchase_akun //untuk parameter update di model
                );
//                $this->m_preference->update_sysaccount_purchase($data_sysaccount_purchase, $syspurchasecategoryid, $code_unrealized_purchase_akun);//masuk ke model
            }
            //=============== END Unrealized Gained Account ======================//
            
            $rstDataSend = $this->m_preference->data_send_preference_purchase($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_purchase_account_LAMA(){
        //set sys_itemcategory
        $CompanyCode = $this->session->userdata('company');	
        $ItemCategoryID = $this->input->post('select_item_category');
		
        $sys_purchasecategoryid = $this->m_preference->get_purchase_categori_id($ItemCategoryID);
		
		/*
			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===========================*/
		
        if(empty($sys_purchasecategoryid)){
            $data_purchasecategoryid = array(
                'CompanyCode' => $CompanyCode, 
                'ItemCategoryID' => $ItemCategoryID, 
                'LastUser' => $this->session->userdata('username')
            );

            $this->m_preference->insert_sys_purchasecategory($data_purchasecategoryid);
        }
		
        $syspurchasecategoryid = $this->m_preference->get_purchase_categori_id($ItemCategoryID);
		
        //set sys purchase account
        $code_payable_akun              = 'Payable';
        $code_advance_purchase_akun     = 'AdvancePurchase';
        $code_discount_invoice_akun     = 'InvoiceDiscount';
        $code_discount_payment_akun     = 'PaymentDiscount';
        $code_taxable_purchase_akun     = 'PurchaseTaxable';
        $code_purchase_freight_akun     = 'PurchaseFreight';
        $code_realized_purchase_akun    = 'RealizedGainedLoss';
        $code_unrealized_purchase_akun  = 'UnRealizedGainedLoss';
        
        //Purchase Account Payable
        $cmb_akun_payable = $this->input->post('select_account_pi_payablee');
        $cek_akun = $this->m_preference->cek_kode_on_purchase_account($code_payable_akun, $syspurchasecategoryid);
        
        if(empty($cek_akun)){
            $data_sysaccount_purchase = array(
                                            'SysPurchaseCategoryID'     => $syspurchasecategoryid, 
                                            'AccountCode'               => $code_payable_akun,
                                            'AccountNumber'             => ($cmb_akun_payable == "")? NULL:nl2br($cmb_akun_payable),
                                            'LastUser'                  => $this->session->userdata('username')
                                            );

            $this->m_preference->insert_sysaccount_purchase($data_sysaccount_purchase);
        } 
        else {
            $data_sysaccount_purchase = array(
                                            'AccountCode'               => $code_payable_akun,
                                            'AccountNumber'             => ($cmb_akun_payable == "")? NULL:nl2br($cmb_akun_payable),
                                            'LastUser'                  => $this->session->userdata('username')
                                            );

            $this->m_preference->update_sysaccount_purchase($data_sysaccount_purchase, $syspurchasecategoryid, $code_payable_akun);
        }
		
        //Advance Purchase Account
        $cmb_akun_advance_purchase = $this->input->post('select_account_advance_purchase');
        $cek_akun = $this->m_preference->cek_kode_on_purchase_account($code_advance_purchase_akun, $syspurchasecategoryid);

        if(empty($cek_akun)){
            $data_sysaccount_purchase = array(
                                            'SysPurchaseCategoryID'     => $syspurchasecategoryid, 
                                            'AccountCode'               => $code_advance_purchase_akun,
                                            'AccountNumber'             => ($cmb_akun_advance_purchase == "")? NULL:nl2br($cmb_akun_advance_purchase),
                                            'LastUser'                  => $this->session->userdata('username')
                                            );

            $this->m_preference->insert_sysaccount_purchase($data_sysaccount_purchase);
        } 
        else {
            $data_sysaccount_purchase = array(
                                            'AccountCode'               => $code_advance_purchase_akun,
                                            'AccountNumber'             => ($cmb_akun_advance_purchase == "")? NULL:nl2br($cmb_akun_advance_purchase),
                                            'LastUser'                  => $this->session->userdata('username')
                                            );

            $this->m_preference->update_sysaccount_purchase($data_sysaccount_purchase, $syspurchasecategoryid, $code_advance_purchase_akun);
        }
		
        //Discount Invoice Account
        $cmb_akun_discount_invoice = $this->input->post('select_account_discount_invoice');
        $cek_akun = $this->m_preference->cek_kode_on_purchase_account($code_discount_invoice_akun, $syspurchasecategoryid);

        if(empty($cek_akun)){
            $data_sysaccount_purchase = array(
                                            'SysPurchaseCategoryID'     => $syspurchasecategoryid, 
                                            'AccountCode'               => $code_discount_invoice_akun,
                                            'AccountNumber'             => ($cmb_akun_discount_invoice == "")? NULL:nl2br($cmb_akun_discount_invoice),
                                            'LastUser'                  => $this->session->userdata('username')
                                            );

            $this->m_preference->insert_sysaccount_purchase($data_sysaccount_purchase);
        } 
        else {
            $data_sysaccount_purchase = array(
                                            'AccountCode'               => $code_discount_invoice_akun,
                                            'AccountNumber'             => ($cmb_akun_discount_invoice == "")? NULL:nl2br($cmb_akun_discount_invoice),
                                            'LastUser'                  => $this->session->userdata('username')
                                            );

            $this->m_preference->update_sysaccount_purchase($data_sysaccount_purchase, $syspurchasecategoryid, $code_discount_invoice_akun);
        }   
		
        //Discount  Payment Account
        $cmb_akun_discount_payment = $this->input->post('select_account_discount_payment');
        $cek_akun = $this->m_preference->cek_kode_on_purchase_account($code_discount_payment_akun, $syspurchasecategoryid);

        if(empty($cek_akun)){
            $data_sysaccount_purchase = array(
                                            'SysPurchaseCategoryID'     => $syspurchasecategoryid, 
                                            'AccountCode'               => $code_discount_payment_akun,
                                            'AccountNumber'             => ($cmb_akun_discount_payment == "")? NULL:nl2br($cmb_akun_discount_payment),
                                            'LastUser'                  => $this->session->userdata('username')
                                            );

            $this->m_preference->insert_sysaccount_purchase($data_sysaccount_purchase);
        } 
        else {
            $data_sysaccount_purchase = array(
                                            'AccountCode'               => $code_discount_payment_akun,
                                            'AccountNumber'             => ($cmb_akun_discount_payment == "")? NULL:nl2br($cmb_akun_discount_payment),
                                            'LastUser'                  => $this->session->userdata('username')
                                            );

            $this->m_preference->update_sysaccount_purchase($data_sysaccount_purchase, $syspurchasecategoryid, $code_discount_payment_akun);
        }
		
        //Taxable Purchase Account
        $cmb_akun_taxable_purchase = $this->input->post('select_account_taxable_purchase');
        $cek_akun = $this->m_preference->cek_kode_on_purchase_account($code_taxable_purchase_akun, $syspurchasecategoryid);

        if(empty($cek_akun)){
            $data_sysaccount_purchase = array(
                                            'SysPurchaseCategoryID'     => $syspurchasecategoryid, 
                                            'AccountCode'               => $code_taxable_purchase_akun,
                                            'AccountNumber'             => ($cmb_akun_taxable_purchase == "")? NULL:nl2br($cmb_akun_taxable_purchase),
                                            'LastUser'                  => $this->session->userdata('username')
                                            );

            $this->m_preference->insert_sysaccount_purchase($data_sysaccount_purchase);
        } 
        else {
            $data_sysaccount_purchase = array(
                                            'AccountCode'               => $code_taxable_purchase_akun,
                                            'AccountNumber'             => ($cmb_akun_taxable_purchase == "")? NULL:nl2br($cmb_akun_taxable_purchase),
                                            'LastUser'                  => $this->session->userdata('username')
                                            );

            $this->m_preference->update_sysaccount_purchase($data_sysaccount_purchase, $syspurchasecategoryid, $code_taxable_purchase_akun);
        }

        //Purchase Freight
        $cmb_akun_purchase_freight = $this->input->post('select_account_purchase_freight');
        $cek_akun = $this->m_preference->cek_kode_on_purchase_account($code_purchase_freight_akun, $syspurchasecategoryid);

        if(empty($cek_akun)){
            $data_sysaccount_purchase = array(
                                            'SysPurchaseCategoryID'     => $syspurchasecategoryid, 
                                            'AccountCode'               => $code_purchase_freight_akun,
                                            'AccountNumber'             => ($cmb_akun_purchase_freight == "")? NULL:nl2br($cmb_akun_purchase_freight),
                                            'LastUser'                  => $this->session->userdata('username')
                                        );

            $this->m_preference->insert_sysaccount_purchase($data_sysaccount_purchase);
        } 
        else {
            $data_sysaccount_purchase = array(
                                            'AccountCode'               => $code_purchase_freight_akun,
                                            'AccountNumber'             => ($cmb_akun_purchase_freight == "")? NULL:nl2br($cmb_akun_purchase_freight),
                                            'LastUser'                  => $this->session->userdata('username')
                                        );

            $this->m_preference->update_sysaccount_purchase($data_sysaccount_purchase, $syspurchasecategoryid, $code_purchase_freight_akun);
        }
        
        //Realized Gained Account
        $cmb_akun_realized = $this->input->post('select_account_realized');
        $cek_akun = $this->m_preference->cek_kode_on_purchase_account($code_realized_purchase_akun, $syspurchasecategoryid);

        if(empty($cek_akun)){
            $data_sysaccount_purchase = array(
                                            'SysPurchaseCategoryID'     => $syspurchasecategoryid, 
                                            'AccountCode'               => $code_realized_purchase_akun,
                                            'AccountNumber'             => ($cmb_akun_realized == "")? NULL:nl2br($cmb_akun_realized),
                                            'LastUser'                  => $this->session->userdata('username')
                                            );

            $this->m_preference->insert_sysaccount_purchase($data_sysaccount_purchase);
        } 
        else {
            $data_sysaccount_purchase = array(
                                            'AccountCode'               => $code_realized_purchase_akun,
                                            'AccountNumber'             => ($cmb_akun_realized == "")? NULL:nl2br($cmb_akun_realized),
                                            'LastUser'                  => $this->session->userdata('username')
                                            );

            $this->m_preference->update_sysaccount_purchase($data_sysaccount_purchase, $syspurchasecategoryid, $code_realized_purchase_akun);
        }
		
        //Unrealized Gained Account
        $cmb_akun_unrealized = $this->input->post('select_account_unrealized');
        $cek_akun = $this->m_preference->cek_kode_on_purchase_account($code_unrealized_purchase_akun, $syspurchasecategoryid);

        if(empty($cek_akun)){
            $data_sysaccount_purchase = array(
                                            'SysPurchaseCategoryID'     => $syspurchasecategoryid, 
                                            'AccountCode'               => $code_unrealized_purchase_akun,
                                            'AccountNumber'             => ($cmb_akun_unrealized == "")? NULL:nl2br($cmb_akun_unrealized),
                                            'LastUser'                  => $this->session->userdata('username')
                                            );

            $this->m_preference->insert_sysaccount_purchase($data_sysaccount_purchase);
        } 
        else {
            $data_sysaccount_purchase = array(
                                            'AccountCode'               => $code_unrealized_purchase_akun,
                                            'AccountNumber'             => ($cmb_akun_unrealized == "")? NULL:nl2br($cmb_akun_unrealized),
                                            'LastUser'                  => $this->session->userdata('username')
            );

            $this->m_preference->update_sysaccount_purchase($data_sysaccount_purchase, $syspurchasecategoryid, $code_unrealized_purchase_akun);
        }
		
        //approval
//        $jobtitlecode = $this->input->post('jobtitle');
//        $cek_approval = $this->m_preference->cek_approval_on_purchase_account($ItemCategoryID);
//
//        $date_approval = date('Y-m-d'); 
//		
//        if($jobtitlecode == 'MGR'){
//            $data_sysaccount_purchase = array(
//                                            'AppLevel1Status'           => 1,
//                                            'AppLevel1Code'             => $jobtitlecode,
//                                            'AppLevel1Date'             => $date_approval
//                                            );
//
//            $this->m_preference->update_approval_purchase($data_sysaccount_purchase, $syspurchasecategoryid);
//        }
		
        //Set Data
        $result['status'] 	= true;
        $result['message']	= "Preferences Berhasil Disimpan";
        echo json_encode($result);
    }
	
    //get value akun payable
    function combo_list_payable(){
        $item_id = $this->input->post('item_id');
        $akun_value_payable = $this->m_preference->get_akun_purchase_payable($item_id);

        echo"$akun_value_payable";
    }
	
    //get value akun advance
    function combo_list_advance_purchase(){
        $item_id = $this->input->post('item_id');
        $akun_value_advance = $this->m_preference->get_akun_purchase_advance($item_id);

        echo"$akun_value_advance";
    }
	
    //get value akun discount
    function combo_list_discount_invoice(){
        $item_id = $this->input->post('item_id');
        $akun_value_discount = $this->m_preference->get_invoice_discount($item_id);

        echo"$akun_value_discount";
    }
	
    //get value akun diskon payable
    function combo_list_discount_payment(){
        $item_id = $this->input->post('item_id');
        $akun_value_payment = $this->m_preference->get_akun_payment_discount($item_id);

        echo"$akun_value_payment";
    }
	
    //get value akun taxable
    function combo_list_taxable_purchase(){
        $item_id = $this->input->post('item_id');
        $akun_value_taxable = $this->m_preference->get_akun_purchase_taxable($item_id);

        echo"$akun_value_taxable";
    }

    //get value akun freight
    function combo_list_purchase_freight(){
        $item_id = $this->input->post('item_id');
        $akun_value_freight = $this->m_preference->get_akun_purchase_freight($item_id);

        echo "$akun_value_freight";
    }
    
    //get value purchase realized
    function combo_list_purchase_realized(){
        $item_id = $this->input->post('item_id');
        $akun_value_realized = $this->m_preference->get_akun_purchase_realized($item_id);

        echo"$akun_value_realized";
    }
	
    //get value purchase unrealized
    function combo_list_purchase_unrealized(){
        $item_id = $this->input->post('item_id');
        $akun_value_unrealized = $this->m_preference->get_akun_purchase_unrealized($item_id);

        echo"$akun_value_unrealized";
    }
	
    //cek approval purchase
    function cek_app_purchase(){
        $item_id = $this->input->post('item_id');
        $approval_list = $this->m_preference->get_cek_app_purchase($item_id);

        echo"$approval_list";
    }
}

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