<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*	
 *	Author 	: Wahyu / Tanggal : 22-07-2014	
 *	Revisi 	: Ferry / Tanggal : 23-03-2015
 *	Revisi 	: Dhidhik / Tanggal : 16 Maret 2016, editing function "proses_post_data_prefernce_sales_account" dibuat array model in insert/update
 */
 
class Preference_sales 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_sr_receiveble(){
		$company				 		= $this->session->userdata('company');	
		$akun_sr_receiveble 			= array('16','14','10','15');
		
		$get_data_akun_sr_receiveble = $this->m_preference->get_akun_advance($company, $akun_sr_receiveble);
		echo json_encode($get_data_akun_sr_receiveble);
	}
	
	function akun_advance_sales(){
		$company				 		= $this->session->userdata('company');	
		$akun_advance_sales 			= array('Uang Muka Penjualan');
		
		$get_data_akun_advance_sales = $this->m_preference->get_akun_sub_type_name($company, $akun_advance_sales);
		echo json_encode($get_data_akun_advance_sales);
	}
	
	function akun_sales_freight(){
		$company				 		= $this->session->userdata('company');	
		$akun_sales_freight 			= array('Biaya Freight Out');
		
		$get_data_akun_sales_freight = $this->m_preference->get_akun_sub_type_name($company, $akun_sales_freight);
		echo json_encode($get_data_akun_sales_freight);
	}
	
	function akun_realized(){
		$company				 		= $this->session->userdata('company');	
		$akun_realized 					= array('Selisih Kurs Transaksi');
		
		$get_data_akun_realized = $this->m_preference->get_akun_sub_type_name($company, $akun_realized);
		echo json_encode($get_data_akun_realized);
	}
	
	function akun_unrealized(){
		$company				 		= $this->session->userdata('company');	
		$akun_unrealized 				= array('Selisih Kurs Akhir Periode');
		
		$get_data_akun_unrealized = $this->m_preference->get_akun_sub_type_name($company, $akun_unrealized);
		echo json_encode($get_data_akun_unrealized);
	}
	
	function akun_invoice_discount(){
		$company				 		= $this->session->userdata('company');	
		//$akun_unrealized 				= array('Selisih Kurs Akhir Periode');
		
		$get_data_akun_invoice_discount = $this->m_preference->get_coa_account('Potongan Penjualan', 2);
		//echo $this->db->last_query();
		echo json_encode($get_data_akun_invoice_discount);
	}
	
	function akun_receive_discount(){
		$company				 		= $this->session->userdata('company');	
		//$akun_unrealized 				= array('Selisih Kurs Akhir Periode');
		
		$get_data_akun_receive_discount = $this->m_preference->get_coa_account('Diskon Penjualan', 2);
		echo json_encode($get_data_akun_receive_discount);
	}
	
	function akun_sales_taxable(){
		$company				 		= $this->session->userdata('company');	
		//$akun_unrealized 				= array('Selisih Kurs Akhir Periode');
		
		$get_data_akun_sales_taxable = $this->m_preference->get_coa_account('Hutang Pajak', 3);
		echo json_encode($get_data_akun_sales_taxable);
	}
	
    public function index(){	
        $company = $this->session->userdata('company');		

        //set daftar item category
        //$data['item_category']          = $this->m_preference->get_all_item_category();
        
		//$akun_sr_receiveble 			= array('16','14','10','15');
		//$akun_advance_sales 			= array('Uang Muka Penjualan');
		//$akun_sales_freight 			= array('Biaya Freight Out');
		//$akun_realized 					= array('Selisih Kurs Transaksi');
		//$akun_unrealized 				= array('Selisih Kurs Akhir Periode');
		
        //$data['akun_sr_receiveble']     = $this->m_preference->get_akun_advance($company, $akun_sr_receiveble);
        //$data['akun_advance_sales']     = $this->m_preference->get_akun_sub_type_name($company, $akun_advance_sales);
        
		//$data['akun_invoice_discount']  = $this->m_preference->get_coa_account('Potongan Penjualan', 2);
        //$data['akun_receive_discount']  = $this->m_preference->get_coa_account('Diskon Penjualan', 2);
        //$data['akun_sales_taxable']     = $this->m_preference->get_coa_account('Hutang Pajak', 3);
		
        //$data['akun_sales_freight']     = $this->m_preference->get_akun_sub_type_name($company, $akun_sales_freight);
        //$data['akun_realized']          = $this->m_preference->get_akun_sub_type_name($company, $akun_realized);
        //$data['akun_unrealized']        = $this->m_preference->get_akun_sub_type_name($company, $akun_unrealized);
		
		$data['jobtitle'] = $this->session->userdata('jobtitle');	
        $this->load->view('v_header');
        $this->load->view('preference/view_preference_sales', $data);
        $this->load->view('v_footer');
    }
	
    //PROCESS POST DATA    
    public function proses_post_data_prefernce_sales_account(){
        try{
            $dataSend = [];
            $dataSend['data_salescategoryid']                   = [];
            $dataSend['data_sysaccount_sales_insert']           = [];
            $dataSend['data_sales_account_receivable_update']   = [];
            $dataSend['data_advance_sales_update']              = [];
            $dataSend['data_sales_diskon_update']               = [];
            $dataSend['data_receive_diskon_update']             = [];
            $dataSend['data_sales_taxable_update']              = [];
            $dataSend['data_sales_freight_update']              = [];
            $dataSend['data_realized_gained_update']            = [];
            $dataSend['data_unrealized_gained_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_salescategoryid = $this->m_preference->get_sales_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_salescategoryid)){
                $dataSend['data_salescategoryid'][] = array(
                    'CompanyCode'       => $CompanyCode, 
                    'ItemCategoryID'    => $ItemCategoryID, 
                    'LastUser'          => 'admin'
                );
//                $this->m_preference->insert_sys_salescategory($data_salescategoryid);// masuk ke model
            }
            $syssalescategoryid = $this->m_preference->get_sales_categori_id($ItemCategoryID); // masuk ke model JUGA, klo ga,ntar ga dapet syssalescategoryid nya

            //========== set sys sales account ===============================//
            $code_sr_receiveble_akun        = 'SRAccountReceivable';
            $code_advance_sales_akun        = 'AdvanceSales';
            $code_invoice_discount_akun     = 'InvoiceDiscount';
            $code_receive_discount_akun     = 'ReceiveDiscount';
            $code_sales_taxable_akun        = 'SalesTaxable';
            $code_sales_freight_akun        = 'SalesFreight';
            $code_realized_akun             = 'RealizedGainedLoss';
            $code_unrealized_akun           = 'UnRealizedGainedLoss';
            //========== END set sys sales account ===========================//

            //========== Sales Account Receivable ============================//
            $cmb_akun_sr_receiveble = $this->input->post('select_account_receivable');
            $cek_akun = $this->m_preference->cek_kode_on_sales_account($code_sr_receiveble_akun, $syssalescategoryid);
            if(empty($cek_akun)){
                $dataSend['data_sysaccount_sales_insert'][] = array(
                    'SysSalesCategoryID'    => $syssalescategoryid, 
                    'AccountCode'           => $code_sr_receiveble_akun,
                    'AccountNumber'         => ($cmb_akun_sr_receiveble == "")? NULL:nl2br($cmb_akun_sr_receiveble),
                    'LastUser'              => $this->session->userdata('username')
                );
//                $this->m_preference->insert_sysaccount_sales($data_sysaccount_sales); // masuk ke model
            }else {
                $dataSend['data_sales_account_receivable_update'][] = array(
                    'AccountCode'           => $code_sr_receiveble_akun,
                    'AccountNumber'         => ($cmb_akun_sr_receiveble == "")? NULL:nl2br($cmb_akun_sr_receiveble),
                    'LastUser'              => $this->session->userdata('username'),
                    
                    'code_sr_receiveble_akun' => $code_sr_receiveble_akun //untuk parameter update di model
                );
//                $this->m_preference->update_sysaccount_sales($data_sysaccount_sales, $syssalescategoryid, $code_sr_receiveble_akun);// masuk ke model
            }
            //========== END Sales Account Receivable ========================//

            //========== Advance Sales =======================================//
            $cmb_akun_advance_sales = $this->input->post('select_account_advance_sales');
            $cek_akun = $this->m_preference->cek_kode_on_sales_account($code_advance_sales_akun, $syssalescategoryid);
            if(empty($cek_akun)){
                $dataSend['data_sysaccount_sales_insert'][] = array(
                    'SysSalesCategoryID'    => $syssalescategoryid, 
                    'AccountCode'           => $code_advance_sales_akun,
                    'AccountNumber'         => ($cmb_akun_advance_sales == "")? NULL:nl2br($cmb_akun_advance_sales),
                    'LastUser'              => $this->session->userdata('username')
                );
//                $this->m_preference->insert_sysaccount_sales($data_sysaccount_sales);//masuk ke model
            }else {
                $dataSend['data_advance_sales_update'][] = array(
                    'AccountCode'           => $code_advance_sales_akun,
                    'AccountNumber'         => ($cmb_akun_advance_sales == "")? NULL:nl2br($cmb_akun_advance_sales),
                    'LastUser'              => $this->session->userdata('username'),
                    
                    'code_advance_sales_akun'   => $code_advance_sales_akun //untuk parameter update di model
                );
//                $this->m_preference->update_sysaccount_sales($data_sysaccount_sales, $syssalescategoryid, $code_advance_sales_akun);//masuk ke model
            }
            //========== END Advance Sales ===================================//

            //========== Sales diskon ========================================//
            $cmb_akun_invoice_diskon = $this->input->post('select_account_invoice_discount');
            $cek_akun = $this->m_preference->cek_kode_on_sales_account($code_invoice_discount_akun, $syssalescategoryid);
            if(empty($cek_akun)){
                $dataSend['data_sysaccount_sales_insert'][] = array(
                    'SysSalesCategoryID'=> $syssalescategoryid, 
                    'AccountCode'       => $code_invoice_discount_akun,
                    'AccountNumber'     => ($cmb_akun_invoice_diskon == "")? NULL:nl2br($cmb_akun_invoice_diskon),
                    'LastUser'          => $this->session->userdata('username')
                );
//                $this->m_preference->insert_sysaccount_sales($data_sysaccount_sales); // masuk ke model
            }else {
                $dataSend['data_sales_diskon_update'][] = array(
                    'AccountCode' => $code_invoice_discount_akun,
                    'AccountNumber' => ($cmb_akun_invoice_diskon == "")? NULL:nl2br($cmb_akun_invoice_diskon),
                    'LastUser' => $this->session->userdata('username'),
                    
                    'code_invoice_discount_akun' => $code_invoice_discount_akun //untuk parameter update di model 
                );
//                $this->m_preference->update_sysaccount_sales($data_sysaccount_sales, $syssalescategoryid, $code_invoice_discount_akun);//masuk ke model
            }
            //========== END Sales diskon ====================================//

            //========== Receive diskon ======================================//
            $cmb_akun_receive_diskon = $this->input->post('select_account_receive_discount');
            $cek_akun = $this->m_preference->cek_kode_on_sales_account($code_receive_discount_akun, $syssalescategoryid);
            if(empty($cek_akun)){
                $dataSend['data_sysaccount_sales_insert'][] = array(
                    'SysSalesCategoryID' => $syssalescategoryid, 
                    'AccountCode' => $code_receive_discount_akun,
                    'AccountNumber' => ($cmb_akun_receive_diskon == "")? NULL:nl2br($cmb_akun_receive_diskon),
                    'LastUser' => $this->session->userdata('username')
                );
//                $this->m_preference->insert_sysaccount_sales($data_sysaccount_sales); // masuk di model
            }else {
                $dataSend['data_receive_diskon_update'][] = array(
                    'AccountCode' => $code_receive_discount_akun,
                    'AccountNumber' => ($cmb_akun_receive_diskon == "")? NULL:nl2br($cmb_akun_receive_diskon),
                    'LastUser' => $this->session->userdata('username'),
                    
                    'code_receive_discount_akun' => $code_receive_discount_akun  //untuk parameter update di model
                );
//                $this->m_preference->update_sysaccount_sales($data_sysaccount_sales, $syssalescategoryid, $code_receive_discount_akun); // masuk ke model
            }
            //========== END Receive diskon ==================================//

            //========== Sales taxable =======================================//
            $cmb_akun_sales_taxable = $this->input->post('select_account_sales_taxable');
            $cek_akun = $this->m_preference->cek_kode_on_sales_account($code_sales_taxable_akun, $syssalescategoryid);
            if(empty($cek_akun)){
                $dataSend['data_sysaccount_sales_insert'][] = array(
                    'SysSalesCategoryID'    => $syssalescategoryid, 
                    'AccountCode'           => $code_sales_taxable_akun,
                    'AccountNumber'         => ($cmb_akun_sales_taxable == "")? NULL:nl2br($cmb_akun_sales_taxable),
                    'LastUser'              => $this->session->userdata('username')
                );
//                $this->m_preference->insert_sysaccount_sales($data_sysaccount_sales);
            }else {
                $dataSend['data_sales_taxable_update'][] = array(
                    'AccountCode'           => $code_sales_taxable_akun,
                    'AccountNumber'         => ($cmb_akun_sales_taxable == "")? NULL:nl2br($cmb_akun_sales_taxable),
                    'LastUser'              => $this->session->userdata('username'),
                    
                    'code_sales_taxable_akun'   => $code_sales_taxable_akun // untuk parameter update di model
                );
//                $this->m_preference->update_sysaccount_sales($data_sysaccount_sales, $syssalescategoryid, $code_sales_taxable_akun); // masuk ke model
            }
            //========== END Sales taxable ===================================//

            //========== Sales Freight =======================================//
            $cmb_akun_sales_freight = $this->input->post('select_account_sales_freight');
            $cek_akun = $this->m_preference->cek_kode_on_sales_account($code_sales_freight_akun, $syssalescategoryid);
            if(empty($cek_akun)){
                $dataSend['data_sysaccount_sales_insert'][] = array(
                    'SysSalesCategoryID'    => $syssalescategoryid, 
                    'AccountCode'           => $code_sales_freight_akun,
                    'AccountNumber'         => ($cmb_akun_sales_freight == "")? NULL:nl2br($cmb_akun_sales_freight),
                    'LastUser'              => $this->session->userdata('username')
                );
//                $this->m_preference->insert_sysaccount_sales($data_sysaccount_sales); // masuk ke model
            }else {
                $dataSend['data_sales_freight_update'][] = array(
                    'AccountCode'           => $code_sales_freight_akun,
                    'AccountNumber'         => ($cmb_akun_sales_freight == "")? NULL:nl2br($cmb_akun_sales_freight),
                    'LastUser'              => $this->session->userdata('username'),
                    
                    'code_sales_freight_akun'   => $code_sales_freight_akun //untuk paramenter update di model
                );
//                $this->m_preference->update_sysaccount_sales($data_sysaccount_sales, $syssalescategoryid, $code_sales_freight_akun); //masuk ke model
            }
            //========== Sales Freight =======================================//

            //=========== realized gained ====================================//
            $cmb_akun_realized = $this->input->post('select_account_realized_gained');
            $cek_akun = $this->m_preference->cek_kode_on_sales_account($code_realized_akun, $syssalescategoryid);
            if(empty($cek_akun)){
                $dataSend['data_sysaccount_sales_insert'][] = array(
                    'SysSalesCategoryID'    => $syssalescategoryid, 
                    'AccountCode'           => $code_realized_akun,
                    'AccountNumber'         => ($cmb_akun_realized == "")? NULL:nl2br($cmb_akun_realized),
                    'LastUser'              => $this->session->userdata('username')
                );
//                $this->m_preference->insert_sysaccount_sales($data_sysaccount_sales); //masuk ke model
            }else {
                $dataSend['data_realized_gained_update'][] = array(
                    'AccountCode'           => $code_realized_akun,
                    'AccountNumber'         => ($cmb_akun_realized == "")? NULL:nl2br($cmb_akun_realized),
                    'LastUser'              => $this->session->userdata('username'),
                    
                    'code_realized_akun'    => $code_realized_akun // untuk parameter update di model
                );
//                $this->m_preference->update_sysaccount_sales($data_sysaccount_sales, $syssalescategoryid, $code_realized_akun);
            }
            //=========== realized gained ====================================//

            //=========== unrealized gained ==================================//
            $cmb_akun_unrealized = $this->input->post('select_account_unrealized_gained');
            $cek_akun = $this->m_preference->cek_kode_on_sales_account($code_unrealized_akun, $syssalescategoryid);
            if(empty($cek_akun)){
                $dataSend['data_sysaccount_sales_insert'][] = array(
                    'SysSalesCategoryID'    => $syssalescategoryid, 
                    'AccountCode'           => $code_unrealized_akun,
                    'AccountNumber'         => ($cmb_akun_unrealized == "")? NULL:nl2br($cmb_akun_unrealized),
                    'LastUser'              => $this->session->userdata('username')
                );
//                $this->m_preference->insert_sysaccount_sales($data_sysaccount_sales);
            } 
            else {
                $dataSend['data_unrealized_gained_update'][] = array(
                    'AccountCode'           => $code_unrealized_akun,
                    'AccountNumber'         => ($cmb_akun_unrealized == "")? NULL:nl2br($cmb_akun_unrealized),
                    'LastUser'              => $this->session->userdata('username'),
                    
                    'code_unrealized_akun'  => $code_unrealized_akun
                );
//                $this->m_preference->update_sysaccount_sales($data_sysaccount_sales, $syssalescategoryid, $code_unrealized_akun);//masuk ke model
            }
            //=========== END sunrealized gained =============================//
            
            $rstDataSend = $this->m_preference->data_send_preference_sales($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);
        }
    }
    public function proses_post_data_prefernce_sales_account_LAMA(){
        //set sys_itemcategory
        $CompanyCode = $this->session->userdata('company');	
        $ItemCategoryID = $this->input->post('select_item_category');
		
        $sys_salescategoryid = $this->m_preference->get_sales_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_salescategoryid)){
            $data_salescategoryid = array(
                                        'CompanyCode'       => $CompanyCode, 
                                        'ItemCategoryID'    => $ItemCategoryID, 
                                        'LastUser'          => 'admin'
                                    );

            $this->m_preference->insert_sys_salescategory($data_salescategoryid);
        }

        $syssalescategoryid = $this->m_preference->get_sales_categori_id($ItemCategoryID);
        
        //set sys sales account
        $code_sr_receiveble_akun        = 'SRAccountReceivable';
        $code_advance_sales_akun        = 'AdvanceSales';
        $code_invoice_discount_akun     = 'InvoiceDiscount';
        $code_receive_discount_akun     = 'ReceiveDiscount';
        $code_sales_taxable_akun        = 'SalesTaxable';
        $code_sales_freight_akun        = 'SalesFreight';
        $code_realized_akun             = 'RealizedGainedLoss';
        $code_unrealized_akun           = 'UnRealizedGainedLoss';
		
        //Sales Account Receivable
        $cmb_akun_sr_receiveble = $this->input->post('select_account_receivable');
        $cek_akun = $this->m_preference->cek_kode_on_sales_account($code_sr_receiveble_akun, $syssalescategoryid);
		
        if(empty($cek_akun)){
            $data_sysaccount_sales = array(
                                        'SysSalesCategoryID'    => $syssalescategoryid, 
                                        'AccountCode'           => $code_sr_receiveble_akun,
                                        'AccountNumber'         => ($cmb_akun_sr_receiveble == "")? NULL:nl2br($cmb_akun_sr_receiveble),
                                        'LastUser'              => $this->session->userdata('username')
                                    );

            $this->m_preference->insert_sysaccount_sales($data_sysaccount_sales);
        } 
        else {
            $data_sysaccount_sales = array(
                                        'AccountCode'           => $code_sr_receiveble_akun,
                                        'AccountNumber'         => ($cmb_akun_sr_receiveble == "")? NULL:nl2br($cmb_akun_sr_receiveble),
                                        'LastUser'              => $this->session->userdata('username')
                                    );

            $this->m_preference->update_sysaccount_sales($data_sysaccount_sales, $syssalescategoryid, $code_sr_receiveble_akun);
        }
		
        //Advance Sales
        $cmb_akun_advance_sales = $this->input->post('select_account_advance_sales');
        $cek_akun = $this->m_preference->cek_kode_on_sales_account($code_advance_sales_akun, $syssalescategoryid);

        if(empty($cek_akun)){
            $data_sysaccount_sales = array(
                                        'SysSalesCategoryID'    => $syssalescategoryid, 
                                        'AccountCode'           => $code_advance_sales_akun,
                                        'AccountNumber'         => ($cmb_akun_advance_sales == "")? NULL:nl2br($cmb_akun_advance_sales),
                                        'LastUser'              => $this->session->userdata('username')
                                    );

            $this->m_preference->insert_sysaccount_sales($data_sysaccount_sales);
        } 
        else {
            $data_sysaccount_sales = array(
                                        'AccountCode'           => $code_advance_sales_akun,
                                        'AccountNumber'         => ($cmb_akun_advance_sales == "")? NULL:nl2br($cmb_akun_advance_sales),
                                        'LastUser'              => $this->session->userdata('username')
            );

            $this->m_preference->update_sysaccount_sales($data_sysaccount_sales, $syssalescategoryid, $code_advance_sales_akun);
        }
		
        //Sales diskon
        $cmb_akun_invoice_diskon = $this->input->post('select_account_invoice_discount');
        $cek_akun = $this->m_preference->cek_kode_on_sales_account($code_invoice_discount_akun, $syssalescategoryid);

        if(empty($cek_akun)){
                $data_sysaccount_sales = array(
                        'SysSalesCategoryID' => $syssalescategoryid, 
                        'AccountCode' => $code_invoice_discount_akun,
                        'AccountNumber' => ($cmb_akun_invoice_diskon == "")? NULL:nl2br($cmb_akun_invoice_diskon),
                        'LastUser' => $this->session->userdata('username')
                );

                $this->m_preference->insert_sysaccount_sales($data_sysaccount_sales);
        } 
        else {
                $data_sysaccount_sales = array(
                        'AccountCode' => $code_invoice_discount_akun,
                        'AccountNumber' => ($cmb_akun_invoice_diskon == "")? NULL:nl2br($cmb_akun_invoice_diskon),
                        'LastUser' => $this->session->userdata('username')
                );

                $this->m_preference->update_sysaccount_sales($data_sysaccount_sales, $syssalescategoryid, $code_invoice_discount_akun);
        }
		
        //Receive diskon
        $cmb_akun_receive_diskon = $this->input->post('select_account_receive_discount');
        $cek_akun = $this->m_preference->cek_kode_on_sales_account($code_receive_discount_akun, $syssalescategoryid);

        if(empty($cek_akun)){
                $data_sysaccount_sales = array(
                        'SysSalesCategoryID' => $syssalescategoryid, 
                        'AccountCode' => $code_receive_discount_akun,
                        'AccountNumber' => ($cmb_akun_receive_diskon == "")? NULL:nl2br($cmb_akun_receive_diskon),
                        'LastUser' => $this->session->userdata('username')
                );

                $this->m_preference->insert_sysaccount_sales($data_sysaccount_sales);
        } 
        else {
                $data_sysaccount_sales = array(
                        'AccountCode' => $code_receive_discount_akun,
                        'AccountNumber' => ($cmb_akun_receive_diskon == "")? NULL:nl2br($cmb_akun_receive_diskon),
                        'LastUser' => $this->session->userdata('username')
                );

                $this->m_preference->update_sysaccount_sales($data_sysaccount_sales, $syssalescategoryid, $code_receive_discount_akun);
        }
	
        //Sales taxable
        $cmb_akun_sales_taxable = $this->input->post('select_account_sales_taxable');
        $cek_akun = $this->m_preference->cek_kode_on_sales_account($code_sales_taxable_akun, $syssalescategoryid);

        if(empty($cek_akun)){
            $data_sysaccount_sales = array(
                                        'SysSalesCategoryID'    => $syssalescategoryid, 
                                        'AccountCode'           => $code_sales_taxable_akun,
                                        'AccountNumber'         => ($cmb_akun_sales_taxable == "")? NULL:nl2br($cmb_akun_sales_taxable),
                                        'LastUser'              => $this->session->userdata('username')
                                    );

            $this->m_preference->insert_sysaccount_sales($data_sysaccount_sales);
        } 
        else {
            $data_sysaccount_sales = array(
                                        'AccountCode'           => $code_sales_taxable_akun,
                                        'AccountNumber'         => ($cmb_akun_sales_taxable == "")? NULL:nl2br($cmb_akun_sales_taxable),
                                        'LastUser'              => $this->session->userdata('username')
                                    );

            $this->m_preference->update_sysaccount_sales($data_sysaccount_sales, $syssalescategoryid, $code_sales_taxable_akun);
        }

        //Sales Freight
        $cmb_akun_sales_freight = $this->input->post('select_account_sales_freight');
        $cek_akun = $this->m_preference->cek_kode_on_sales_account($code_sales_freight_akun, $syssalescategoryid);

        if(empty($cek_akun)){
            $data_sysaccount_sales = array(
                                        'SysSalesCategoryID'    => $syssalescategoryid, 
                                        'AccountCode'           => $code_sales_freight_akun,
                                        'AccountNumber'         => ($cmb_akun_sales_freight == "")? NULL:nl2br($cmb_akun_sales_freight),
                                        'LastUser'              => $this->session->userdata('username')
                                    );

            $this->m_preference->insert_sysaccount_sales($data_sysaccount_sales);
        } 
        else {
            $data_sysaccount_sales = array(
                                        'AccountCode'           => $code_sales_freight_akun,
                                        'AccountNumber'         => ($cmb_akun_sales_freight == "")? NULL:nl2br($cmb_akun_sales_freight),
                                        'LastUser'              => $this->session->userdata('username')
                                    );

            $this->m_preference->update_sysaccount_sales($data_sysaccount_sales, $syssalescategoryid, $code_sales_freight_akun);
        }
    
        //realized gained
        $cmb_akun_realized = $this->input->post('select_account_realized_gained');
        $cek_akun = $this->m_preference->cek_kode_on_sales_account($code_realized_akun, $syssalescategoryid);

        if(empty($cek_akun)){
            $data_sysaccount_sales = array(
                                        'SysSalesCategoryID'    => $syssalescategoryid, 
                                        'AccountCode'           => $code_realized_akun,
                                        'AccountNumber'         => ($cmb_akun_realized == "")? NULL:nl2br($cmb_akun_realized),
                                        'LastUser'              => $this->session->userdata('username')
                                    );

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

            $this->m_preference->update_sysaccount_sales($data_sysaccount_sales, $syssalescategoryid, $code_realized_akun);
        }
		
        //unrealized gained
        $cmb_akun_unrealized = $this->input->post('select_account_unrealized_gained');
        $cek_akun = $this->m_preference->cek_kode_on_sales_account($code_unrealized_akun, $syssalescategoryid);

        if(empty($cek_akun)){
            $data_sysaccount_sales = array(
                                        'SysSalesCategoryID'    => $syssalescategoryid, 
                                        'AccountCode'           => $code_unrealized_akun,
                                        'AccountNumber'         => ($cmb_akun_unrealized == "")? NULL:nl2br($cmb_akun_unrealized),
                                        'LastUser'              => $this->session->userdata('username')
                                    );

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

            $this->m_preference->update_sysaccount_sales($data_sysaccount_sales, $syssalescategoryid, $code_unrealized_akun);
        }
		
        //approval
//        $jobtitlecode = $this->input->post('jobtitle');
//        $cek_approval = $this->m_preference->cek_approval_on_sales_account($jobtitlecode, $syssalescategoryid);
//        $date_approval = date('Y-m-d'); 
//
//        if($jobtitlecode == 'MGR'){
//            $data_sysaccount_sales = array(
//                                        'AppLevel1Status'   => 1,
//                                        'AppLevel1Code'     => $jobtitlecode,
//                                        'AppLevel1Date'     => $date_approval
//                                    );
//
//            $this->m_preference->update_approval_sales($data_sysaccount_sales, $syssalescategoryid);
//        }        
        
		$result['status'] 	= true;
        $result['message']	= "Preferences Berhasil Disimpan";
        echo json_encode($result);
    }
	
    //get value akun sales
    function combo_list_sr_receiveble(){
        $item_id = $this->input->post('item_id');
        $akun_value_sr_receiveble = $this->m_preference->get_akun_sr_receiveble($item_id);
        
        echo "$akun_value_sr_receiveble";
    }

    //get value akun sales advance
    function combo_list_advance_sales(){
        $item_id = $this->input->post('item_id');
        $akun_value_sales_advance = $this->m_preference->get_akun_sales_advance($item_id);

        echo "$akun_value_sales_advance";
    }

    //get value invoice diskon
    function combo_list_invoice_diskon(){
        $item_id = $this->input->post('item_id');
        $akun_value_invoice_diskon = $this->m_preference->get_akun_invoice_diskon($item_id);

//        echo $this->db->last_query(); die();
        echo "$akun_value_invoice_diskon";
    }
	
    //get value receive diskon
    function combo_list_receive_diskon(){
        $item_id = $this->input->post('item_id');
        $akun_value_receive_diskon = $this->m_preference->get_akun_receive_diskon($item_id);
        
        echo "$akun_value_receive_diskon";
    }
	
    //get value sales taxable
    function combo_list_sales_taxable(){
        $item_id = $this->input->post('item_id');
        $akun_value_sales_taxable = $this->m_preference->get_akun_sales_taxable($item_id);
        
        echo "$akun_value_sales_taxable";
    }

    //get value sales freight
    function combo_list_sales_freight(){
        $item_id = $this->input->post('item_id');
        $akun_value_sales_freight = $this->m_preference->get_akun_sales_freight($item_id);
        
        echo "$akun_value_sales_freight";
    }
    
    //get value realized
    function combo_list_realized(){
        $item_id = $this->input->post('item_id');
        $akun_value_realized = $this->m_preference->get_akun_realized($item_id);
        
        echo "$akun_value_realized";
    }

    //get value unrealized
    function combo_list_unrealized(){
        $item_id = $this->input->post('item_id');
        $akun_value_unrealized = $this->m_preference->get_akun_unrealized($item_id);
        
        echo "$akun_value_unrealized";
    }

    //cek approval sales akun
//    function cek_app_sales(){
//        $item_id = $this->input->post('item_id');
//        $approval_list = $this->m_preference->get_cek_app_sales($item_id);
//        
//        echo "$approval_list";
//    }
}

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