<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/* Author 	: Ferry / 31-03-2015 
 * REVISI       : DHIDHIK, 16 MARET 2016, modify ke bentuk process in array for model
 *  */
 
class Preference_services extends CI_Controller {
    public function __construct() {
        parent::__construct();
        
        $this->load->model('m_preference');
        $this->load->helper('regex');
        $this->load->helper('message');
        $this->load->helper('status');
    }
    
	function SalesExpense(){
		$get_akun_SalesExpense = $this->m_preference->get_coa_account('Revenue', 3);
		echo json_encode($get_akun_SalesExpense);
	}
	
	function SalesAdvance(){
		$get_akun_SalesAdvance = $this->m_preference->get_coa_account('Uang Muka Penjualan', 2);
		echo json_encode($get_akun_SalesAdvance);
	}
	
	function SalesInvDiscount(){
		$get_akun_SalesInvDiscount = $this->m_preference->get_coa_account('Potongan Penjualan', 2);
		echo json_encode($get_akun_SalesInvDiscount);
	}
	
	function SalesRecDiscount(){
		$get_akun_SalesRecDiscount = $this->m_preference->get_coa_account('Diskon Penjualan', 2);
		echo json_encode($get_akun_SalesRecDiscount);
	}
	
	function SalesUnbilled(){
		$get_akun_SalesUnbilled = $this->m_preference->get_coa_account('Piutang Expense', 2);
		echo json_encode($get_akun_SalesUnbilled);
	}
	
	function TaxablePPnSales(){
		$get_akun_TaxablePPnSales = $this->m_preference->get_coa_account('Hutang Pajak', 3);
		echo json_encode($get_akun_TaxablePPnSales);
	}
	
	function FreightSales(){
		$get_akun_FreightSales = $this->m_preference->get_coa_account('Biaya Freight Out', 3);
		echo json_encode($get_akun_FreightSales);
	}
	
	/*
		Akun Purchase
	*/
	
	function ExpenseAccount(){
		$get_akun_ExpenseAccount = $this->m_preference->get_coa_account(array('69', '70', '71', '72', '73', '76'), 1);
		echo json_encode($get_akun_ExpenseAccount);
	}
	
	function AdvanceAccount(){
		$get_akun_AdvanceAccount = $this->m_preference->get_coa_account('Uang Muka Pembelian IDR', 4);
		echo json_encode($get_akun_AdvanceAccount);
	}
	
	function InvoiceDiscount(){
		$get_akun_InvoiceDiscount = $this->m_preference->get_coa_account('68', 0);
		echo json_encode($get_akun_InvoiceDiscount);
	}
	
	function PaymentDiscount(){
		$get_akun_PaymentDiscount = $this->m_preference->get_coa_account('Retur Pembelian', 2);
		echo json_encode($get_akun_PaymentDiscount);
	}
	
	function UnbilledAccount(){
		$get_akun_UnbilledAccount = $this->m_preference->get_coa_account('Hutang Expense', 2);
		echo json_encode($get_akun_UnbilledAccount);
	}
	
	function TaxablePPnPurchase(){
		$get_akun_TaxablePPnPurchase = $this->m_preference->get_coa_account('PPN Masukan', 4);//Tambahan baru fahri, tgl 15/01/2015
		echo json_encode($get_akun_TaxablePPnPurchase);
	}
	
	function FreightPurchase(){
		$get_akun_FreightPurchase = $this->m_preference->get_coa_account('Biaya Freight In', 3);//Tambahan baru fahri, tgl 15/01/2015
		echo json_encode($get_akun_FreightPurchase);
	}
	
	function TaxPPh23Account(){
		$get_akun_TaxPPh23Account = $this->m_preference->get_coa_account('PPh Pasal 23', 2);
		echo json_encode($get_akun_TaxPPh23Account);
	}
	
	function TaxPPh21Account(){
		$get_akun_TaxPPh21Account = $this->m_preference->get_coa_account('PPh Pasal 21', 2);
		echo json_encode($get_akun_TaxPPh21Account);
	}
	
	function RealizedAccount(){
		$get_akun_RealizedAccount = $this->m_preference->get_coa_account('Selisih Kurs Transaksi', 2);
		echo json_encode($get_akun_RealizedAccount);
	}
	
	function UnRealizedAccount(){
		$get_akun_UnRealizedAccount = $this->m_preference->get_coa_account('Selisih Kurs Akhir Periode', 2);
		echo json_encode($get_akun_UnRealizedAccount);
	}
	
    public function index(){
		
		$SalesExpense 				= '66';
		$ExpenseAccount 			= array('');
		
        /*$data['SalesExpense']       = $this->m_preference->get_coa_account('Revenue', 3);
        $data['SalesAdvance']       = $this->m_preference->get_coa_account('Uang Muka Penjualan', 2);
        $data['SalesInvDiscount']   = $this->m_preference->get_coa_account('Potongan Penjualan', 2);
        $data['SalesRecDiscount']   = $this->m_preference->get_coa_account('Diskon Penjualan', 2);
        $data['SalesUnbilled']      = $this->m_preference->get_coa_account('Piutang Expense', 2);
		$data['TaxablePPnSales'] 	= $this->m_preference->get_coa_account('Hutang Pajak', 3);//Tambahan baru fahri, tgl 15/01/2015
		$data['FreightSales'] 		= $this->m_preference->get_coa_account('Biaya Freight Out', 3);//Tambahan baru fahri, tgl 15/01/2015*/
        
        /*$data['ExpenseAccount']     = $this->m_preference->get_coa_account(array('69', '70', '71', '72', '73', '76'), 1);
        $data['AdvanceAccount']     = $this->m_preference->get_coa_account('Uang Muka Pembelian IDR', 4);
        $data['InvoiceDiscount']    = $this->m_preference->get_coa_account('68', 0);
        $data['PaymentDiscount']    = $this->m_preference->get_coa_account('Retur Pembelian', 2);
        $data['UnbilledAccount']    = $this->m_preference->get_coa_account('Hutang Expense', 2);
		$data['TaxablePPnPurchase'] = $this->m_preference->get_coa_account('PPN Masukan', 4);//Tambahan baru fahri, tgl 15/01/2015
		$data['FreightPurchase'] 	= $this->m_preference->get_coa_account('Biaya Freight In', 3);//Tambahan baru fahri, tgl 15/01/2015*/
        
        /*$data['TaxPPh23Account']    = $this->m_preference->get_coa_account('Pajak PPh Pasal 23', 4);
        $data['TaxPPh21Account']    = $this->m_preference->get_coa_account('Pajak PPh Pasal 21', 2);
        $data['RealizedAccount']    = $this->m_preference->get_coa_account('Selisih Kurs Transaksi', 2);
        $data['UnRealizedAccount']  = $this->m_preference->get_coa_account('Selisih Kurs Akhir Periode', 2);*/
        $data['jobtitle'] = $this->session->userdata('jobtitle');	
        $this->load->view('v_header');
        $this->load->view('preference/view_preference_services',$data);
        $this->load->view('v_footer');
    }
    
    function modify_category(){
        try{
            //throw new Exception('test sik john');
            $service_category_post  = $this->input->post('detail');
            $service_category       = json_decode($service_category_post);
            
            if(count($service_category) > 0){
                $dataSend   = [];
                $dataSend['data_category_insert']  = [];
                $dataSend['data_category_update']  = [];
                $company_code  = $this->session->userdata('company');	

                foreach($service_category as $category){
                    $category_name  = $category->SvcCategoryName;
                    $category_desc  = $category->SvcCategoryDesc;
					
					$get_item_categories_sum = $this->m_preference->get_item_categories_sum(1);
					$loop_value = $get_item_categories_sum->ItemCategoryID + 2;
					
                    if($category->SvcCategoryType == 1){
                        $dataSend['data_category_insert'][]  = array(
                            //'SvcCategoryID' 	=> $loop_value,
							'CompanyCode'       => $company_code, 
                            'SvcCategoryName'   => $category_name, 
                            'SvcCategoryDesc'   => $category_desc, 
                            'LastUser'          => $this->session->userdata('username')
                        ); 
//                        $this->m_preference->insert_service_category($data_category);//masuk ke model
                    }
                    else if($category->SvcCategoryType == 2) {
                        $category_id    = $category->SvcCategoryID;
                        $dataSend['data_category_update'][]  = array(
                            'SvcCategoryName'   => $category_name, 
                            'SvcCategoryDesc'   => $category_desc, 
                            'LastUser'          => $this->session->userdata('username'),
                            
                            'category_id'       => $category_id //untuk parameter update di model
                        ); 

//                       $this->m_preference->update_service_category($data_category, $category_id);//masuk ke model
                    }
                }            
                            
                $rstDataSend = $this->m_preference->data_send_preference_service_category($dataSend);
                if($rstDataSend){
                    $result['status'] 	= true;
                    $result['message']	= get_notification('insert', 1);                
                }else{
                    $result['status'] 	= false;
                    $result['message']	= get_notification('insert', 0);
                }
                echo json_encode($result);
            }        
            else{
                throw new Exception('Data Expense Category gagal disimpan, karena data Item Expense Category tidak ditemukan');
            }
            
        }catch (Exception $e){            
            $result['status']   = false;
            $result['message']  = $e->getMessage();
            echo json_encode($result);
        }
    }
    function modify_category_LAMA(){
        $service_category_post  = $this->input->post('detail');
        $service_category       = json_decode($service_category_post);

        if(count($service_category) > 0){
            $this->db->trans_begin();
            $company_code       = $this->session->userdata('company');	

            foreach($service_category as $category){
                $category_name  = $category->SvcCategoryName;
                $category_desc  = $category->SvcCategoryDesc;

                if($category->SvcCategoryType == 1){
                    $data_category  = array(
                                        'CompanyCode'       => $company_code, 
                                        'SvcCategoryName'   => $category_name, 
                                        'SvcCategoryDesc'   => $category_desc, 
                                        'LastUser'          => $this->session->userdata('username')
                                      ); 
                    $this->m_preference->insert_service_category($data_category);
                }
                else if($category->SvcCategoryType == 2) {
                    $category_id    = $category->SvcCategoryID;
                    
                    $data_category  = array(
                                        'SvcCategoryName'   => $category_name, 
                                        'SvcCategoryDesc'   => $category_desc, 
                                        'LastUser'          => $this->session->userdata('username')
                                      ); 

                   $this->m_preference->update_service_category($data_category, $category_id);
                }
            }            
            
			/*=============================trans roll back dan trans commit============================/*/
			if ($this->db->trans_status() === FALSE) {
				$this->db->trans_rollback();
				$result['status'] 		= FALSE;
				$result['message']  	= get_notification('insert', 0);       
			}
			else {
				$this->db->trans_commit();
				$result['status'] 		= TRUE;
				$result['message']  	= get_notification('insert', 1);       
			}
			/*==================================end======================================================*/
        }        
        else{
            $result['status']   = false;
            $result['message']  = 'Data Expense Category gagal disimpan, karena data Item Expense Category tidak ditemukan';            
        }
        
        echo json_encode($result);
    }
    
    function modify_detail(){
        try{
            $service_detail_post    = $this->input->post('detail');
            $service_detail         = json_decode($service_detail_post);

            if(count($service_detail) > 0) {
                $dataSend   = [];
                $dataSend['data_detail_insert'] = [];
                $dataSend['data_detail_update'] = [];
                
                $svc_category_id    = $this->input->post('category_id');
                foreach($service_detail as $details) {
                    $detail_name     = $details->SvcDetailName;
					$SvcCategoryID   = $details->SvcCategoryID;
                    $detail_desc     = $details->SvcDetailDesc;
                    $detail_unit     = $details->SvcDetailUnit;
                    $detail_pph23    = $details->SvcDetailTax == 'Yes' ? 1 : 0;

                    if($details->SvcDetailType == 1){
                        $dataSend['data_detail_insert'][] = array(
                            'SvcCategoryID'     => $SvcCategoryID, 
                            'SvcDetailName'     => $detail_name, 
                            'SvcDetailDesc'     => $detail_desc,
                            'SvcDetailUnit'     => $detail_unit,
                            'SvcPPh23Status'    => $detail_pph23,
                            'LastUser'          => $this->session->userdata('username')
                        );
//                        $this->m_preference->insert_service_detail($data_detail); // masuk ke model
                    }
                    else if($details->SvcDetailType == 2) {
                        $detail_id      = $details->SvcDetailID;
                        $dataSend['data_detail_update'][] = array(
                            'SvcCategoryID'     => $SvcCategoryID,
							'SvcDetailName'     => $detail_name, 
                            'SvcDetailDesc'     => $detail_desc,
                            'SvcDetailUnit'     => $detail_unit,
                            'SvcPPh23Status'    => $detail_pph23,
                            'LastUser'          => $this->session->userdata('username'),
                            
                            'detail_id'         => $detail_id // untuk parameter update di model
                        );
//                        $this->m_preference->update_service_detail($data_detail, $detail_id);//masuk ke models
                    }
                }        
                
                $rstDataSend = $this->m_preference->data_send_preference_service_detail($dataSend);
                if($rstDataSend){
                    $result['status'] 	= true;
                    $result['message']	= get_notification('insert', 1);                
                }else{
                    $result['status'] 	= false;
                    $result['message']	= get_notification('insert', 0);
                }
                echo json_encode($result);
            }
            else{                
                throw new Exception('Data Expense Detail gagal disimpan, karena data Item Expense Detail tidak ditemukan');
            } 
        }catch (Exception $e){            
            $result['status']   = false;
            $result['message']  = $e->getMessage();
            echo json_encode($result);
        }
    }
    function modify_detail_LAMA(){
        $service_detail_post    = $this->input->post('detail');
        $service_detail         = json_decode($service_detail_post);

        if(count($service_detail) > 0) {
            $this->db->trans_begin();
            $svc_category_id    = $this->input->post('category_id');

            foreach($service_detail as $details) {
                $detail_name     = $details->SvcDetailName;
                $detail_desc     = $details->SvcDetailDesc;
				$detail_unit     = $details->SvcDetailUnit;
                $detail_pph23    = $details->SvcDetailTax == 'Yes' ? 1 : 0;

                if($details->SvcDetailType == 1){
                    $data_detail = array(
                                        'SvcCategoryID'     => $svc_category_id, 
                                        'SvcDetailName'     => $detail_name, 
                                        'SvcDetailDesc'     => $detail_desc,
										'SvcDetailUnit'     => $detail_unit,
                                        'SvcPPh23Status'    => $detail_pph23,
                                        'LastUser'          => $this->session->userdata('username')
                                    );

                    $this->m_preference->insert_service_detail($data_detail);
                }
                else if($details->SvcDetailType == 2) {
                    $detail_id      = $details->SvcDetailID;

                    $data_detail = array(
                                        'SvcDetailName'     => $detail_name, 
                                        'SvcDetailDesc'     => $detail_desc,
										'SvcDetailUnit'     => $detail_unit,
                                        'SvcPPh23Status'    => $detail_pph23,
                                        'LastUser'          => $this->session->userdata('username')
                                    );

                    $this->m_preference->update_service_detail($data_detail, $detail_id);
                }
            }       

             /*=============================trans roll back dan trans commit============================/*/
			if ($this->db->trans_status() === FALSE) {
				$this->db->trans_rollback();
				$result['status'] 		= FALSE;
				$result['message']  	= get_notification('insert', 0);       
			}
			else {
				$this->db->trans_commit();
				$result['status'] 		= TRUE;
				$result['message']  	= get_notification('insert', 1);       
			}
			/*==================================end======================================================*/            
        }
        else{
            $result['status']   = false;
            $result['message']  = 'Data Expense Detail gagal disimpan, karena data Item Expense Detail tidak ditemukan';            
        }

        echo json_encode($result);            
    }
    
    function modify_account(){
        try{
            $dataSend   = [];
            $dataSend['data_account_insert']                            = [];
            $dataSend['data_account_update']                            = [];
                        
            $svc_category_id    = $this->input->post('category_id');
            //=============== Modify Sales Expense Account ===================//
            $sls_expense_code       = "SalesExpense";		
            $sls_expense_account    = $this->input->post('sales_expense');
            $sls_expense_check      = $this->m_preference->cek_service_account($sls_expense_code, $svc_category_id);
//            throw new Exception('svc_category_id='.$svc_category_id."; sls_expense_check=".$sls_expense_check);
            if(empty($sls_expense_check)){
                $dataSend['data_account_insert'][]   = array(
                    'SvcCategoryID'     => $svc_category_id, 
                    'AccountCode'       => $sls_expense_code,
                    'AccountNumber'     => $sls_expense_account,
                    'LastUser'          => $this->session->userdata('username')
                );
//                $this->m_preference->insert_service_account($data_account);masuk ke model
            }else {
                $dataSend['data_account_update'][]   = array(
                    'SvcCategoryID'     => $svc_category_id, 
                    'AccountCode'       => $sls_expense_code,
                    'AccountNumber'     => $sls_expense_account,
                    'LastUser'          => $this->session->userdata('username'),
                    
                    'idcode' => $sls_expense_check //untuk parameter update di model
                );
//                $this->m_preference->update_service_account($data_account, $sls_expense_check);
            }
            //=============== End Modify Sales Expense Account ===============//

            //=============== Modify Sales Advance Account ===================//
            $sls_advance_code       = "SalesAdvance";		
            $sls_advance_account    = $this->input->post('sales_advance');
            $sls_advance_check      = $this->m_preference->cek_service_account($sls_advance_code, $svc_category_id);
            if(empty($sls_advance_check)){
                $dataSend['data_account_insert'][] = array(
                    'SvcCategoryID'     => $svc_category_id, 
                    'AccountCode'       => $sls_advance_code,
                    'AccountNumber'     => $sls_advance_account,
                    'LastUser'          => $this->session->userdata('username')
                );
//                $this->m_preference->insert_service_account($data_account);
            }else {
                $dataSend['data_account_update'][]   = array(
                    'SvcCategoryID'     => $svc_category_id, 
                    'AccountCode'       => $sls_advance_code,
                    'AccountNumber'     => $sls_advance_account,
                    'LastUser'          => $this->session->userdata('username'),
                    
                    'idcode' => $sls_advance_check //untuk parameter update di model
                );
//                $this->m_preference->update_service_account($data_account, $sls_advance_check);// masuk ke model
            }
            //=============== End Modify Sales Advance Account ===============//

            //=============== Modify Sales Invoice Discount Account ==========//
            $sls_inv_disc_code      = "SalesInvDiscount";		
            $sls_inv_disc_account   = $this->input->post('sales_inv_disc');
            $sls_inv_disc_check     = $this->m_preference->cek_service_account($sls_inv_disc_code, $svc_category_id);
            if(empty($sls_inv_disc_check)){
                $dataSend['data_account_insert'][]   = array(
                    'SvcCategoryID'     => $svc_category_id, 
                    'AccountCode'       => $sls_inv_disc_code,
                    'AccountNumber'     => $sls_inv_disc_account,
                    'LastUser'          => $this->session->userdata('username')
                );
//                $this->m_preference->insert_service_account($data_account);// masuk ke model
            }else {
                $dataSend['data_account_update'][]   = array(
                    'SvcCategoryID'     => $svc_category_id, 
                    'AccountCode'       => $sls_inv_disc_code,
                    'AccountNumber'     => $sls_inv_disc_account,
                    'LastUser'          => $this->session->userdata('username'),
                    
                    'idcode'=> $sls_inv_disc_check //untuuk parameter update di models
                );
//               $this->m_preference->update_service_account($data_account, $sls_inv_disc_check); //masuk ke model
            }
            //=============== End Modify Sales Invoice Discount Account ======//

            //=============== Modify Sales Receipt Discount Account ==========//
            $sls_rec_disc_code      = "SalesRecDiscount";		
            $sls_rec_disc_account   = $this->input->post('sales_rec_disc');
            $sls_rec_disc_check     = $this->m_preference->cek_service_account($sls_rec_disc_code, $svc_category_id);
            if(empty($sls_rec_disc_check)){
                $dataSend['data_account_insert'][]   = array(
                    'SvcCategoryID'     => $svc_category_id, 
                    'AccountCode'       => $sls_rec_disc_code,
                    'AccountNumber'     => $sls_rec_disc_account,
                    'LastUser'          => $this->session->userdata('username')
                );
//                $this->m_preference->insert_service_account($data_account);//masuk ke model
            }else {
                $dataSend['data_account_update'][]   = array(
                    'SvcCategoryID'     => $svc_category_id, 
                    'AccountCode'       => $sls_rec_disc_code,
                    'AccountNumber'     => $sls_rec_disc_account,
                    'LastUser'          => $this->session->userdata('username'),
                    
                    'idcode'=> $sls_rec_disc_check //untuk parameter update di model
                );
//               $this->m_preference->update_service_account($data_account, $sls_rec_disc_check);//masuk ke model
            }
            //=============== End Modify Sales Receipt Discount Account ======//

            //=============== Modify Sales UnBilled Account ==================//
            $sls_unbilled_code      = "SalesUnbilled";		
            $sls_unbilled_account   = $this->input->post('sales_unbilled');
            $sls_unbilled_check     = $this->m_preference->cek_service_account($sls_unbilled_code, $svc_category_id);
            if(empty($sls_unbilled_check)){
                $dataSend['data_account_insert'][]   = array(
                    'SvcCategoryID'     => $svc_category_id, 
                    'AccountCode'       => $sls_unbilled_code,
                    'AccountNumber'     => $sls_unbilled_account,
                    'LastUser'          => $this->session->userdata('username')
                );
//               $this->m_preference->insert_service_account($data_account);//masuk ke model
            }else {
                $dataSend['data_account_update'][]   = array(
                    'SvcCategoryID'     => $svc_category_id, 
                    'AccountCode'       => $sls_unbilled_code,
                    'AccountNumber'     => $sls_unbilled_account,
                    'LastUser'          => $this->session->userdata('username'),
                    
                    'idcode'=> $sls_unbilled_check // untuk parameter update di model
                );
//               $this->m_preference->update_service_account($data_account, $sls_unbilled_check);//masuk ke model
            }
            //=============== End Modify Sales UnBilled Account ==============//

            //=============== Modify Sales Taxable Account ===================//
            $sls_taxable_code      = "SalesTaxable";		
            $sls_taxable_account   = $this->input->post('sales_taxable');
            $sls_taxable_check     = $this->m_preference->cek_service_account($sls_taxable_code, $svc_category_id);
            if(empty($sls_taxable_check)){
                $dataSend['data_account_insert'][]   = array(
                    'SvcCategoryID'     => $svc_category_id, 
                    'AccountCode'       => $sls_taxable_code,
                    'AccountNumber'     => $sls_taxable_account,
                    'LastUser'          => $this->session->userdata('username')
                );
//               $this->m_preference->insert_service_account($data_account);//masuk ke model
            }else {
                $dataSend['data_account_update'][]   = array(
                    'SvcCategoryID'     => $svc_category_id, 
                    'AccountCode'       => $sls_taxable_code,
                    'AccountNumber'     => $sls_taxable_account,
                    'LastUser'          => $this->session->userdata('username'),
                    
                    'idcode' => $sls_taxable_check //untuk parameter update di model
                );
//               $this->m_preference->update_service_account($data_account, $sls_taxable_check);//masuk ke model
            }
            //=============== End Modify Sales Taxable Account ===============//

            //=============== Modify Sales Freight Account ===================//
            $sls_freight_code      = "SalesFreight";		
            $sls_freight_account   = $this->input->post('sales_freight');
            $sls_freight_check     = $this->m_preference->cek_service_account($sls_freight_code, $svc_category_id);
            if(empty($sls_freight_check)){
                $dataSend['data_account_insert'][]   = array(
                    'SvcCategoryID'     => $svc_category_id, 
                    'AccountCode'       => $sls_freight_code,
                    'AccountNumber'     => $sls_freight_account,
                    'LastUser'          => $this->session->userdata('username')
                );
//               $this->m_preference->insert_service_account($data_account);// masuk ke model
            }else {
                $dataSend['data_account_update'][]   = array(
                    'SvcCategoryID'     => $svc_category_id, 
                    'AccountCode'       => $sls_freight_code,
                    'AccountNumber'     => $sls_freight_account,
                    'LastUser'          => $this->session->userdata('username'),
                    
                    'idcode' => $sls_freight_check //untuk parameter update di model
                );
//               $this->m_preference->update_service_account($data_account, $sls_freight_check); //masuk ke model
            }
            //=============== End Modify Sales Freight Account ===============//        

            //=============== Modify Expense Account =========================//
            $expense_code       = "Expense";		
            $expense_account    = $this->input->post('expense_account');
            $expense_check      = $this->m_preference->cek_service_account($expense_code, $svc_category_id);
            if(empty($expense_check)){
                $dataSend['data_account_insert'][]   = array(
                    'SvcCategoryID'     => $svc_category_id, 
                    'AccountCode'       => $expense_code,
                    'AccountNumber'     => $expense_account,
                    'LastUser'          => $this->session->userdata('username')
                );
//                $this->m_preference->insert_service_account($data_account);//masuk ke model
            }else {
                $dataSend['data_account_update'][]   = array(
                    'SvcCategoryID'     => $svc_category_id, 
                    'AccountCode'       => $expense_code,
                    'AccountNumber'     => $expense_account,
                    'LastUser'          => $this->session->userdata('username'),
                    
                    'idcode'     => $expense_check // untuk parameter update di model
                );
//                $this->m_preference->update_service_account($data_account, $expense_check);//masuk ke model
            }
            //=============== End Modify Expense Account =====================//

            //=============== Modify Advance Account =========================// 
            $advance_code       = "Advance";		
            $advance_account    = $this->input->post('advance_account');
            $advance_check      = $this->m_preference->cek_service_account($advance_code, $svc_category_id);
            if(empty($advance_check)){
                $dataSend['data_account_insert'][]   = array(
                    'SvcCategoryID'     => $svc_category_id, 
                    'AccountCode'       => $advance_code,
                    'AccountNumber'     => $advance_account,
                    'LastUser'          => $this->session->userdata('username')
                );
//                $this->m_preference->insert_service_account($data_account);// masuk ke model
            }else {
                $dataSend['data_account_update'][]   = array(
                    'SvcCategoryID'     => $svc_category_id, 
                    'AccountCode'       => $advance_code,
                    'AccountNumber'     => $advance_account,
                    'LastUser'          => $this->session->userdata('username'),
                    
                    'idcode'     => $advance_check // untuk parameter update di model
                );
//                $this->m_preference->update_service_account($data_account, $advance_check);
            }
            //=============== End Modify Advance Account =====================//

            //=============== Modify Purchase Invoice Discount Account =======//
            $invoice_disc_code      = "InvoiceDiscount";		
            $invoice_disc_account   = $this->input->post('pinv_disc_account');
            $invoice_disc_check     = $this->m_preference->cek_service_account($invoice_disc_code, $svc_category_id);
            if(empty($invoice_disc_check)){
                $dataSend['data_account_insert'][] = array(
                    'SvcCategoryID'     => $svc_category_id, 
                    'AccountCode'       => $invoice_disc_code,
                    'AccountNumber'     => $invoice_disc_account,
                    'LastUser'          => $this->session->userdata('username')
                );
//                $this->m_preference->insert_service_account($data_account); //masuk ke models
            }else {
                $dataSend['data_account_update'][]   = array(
                    'SvcCategoryID'     => $svc_category_id, 
                    'AccountCode'       => $invoice_disc_code,
                    'AccountNumber'     => $invoice_disc_account,
                    'LastUser'          => $this->session->userdata('username'),
                    
                    'idcode'=> $invoice_disc_check //untuk parameter update di model
                );
//                $this->m_preference->update_service_account($data_account, $invoice_disc_check);//masuk ke model
            }
            //=============== End Modify Purchase Invoice Discount Account ===//

            //=============== Modify Purchase Payment Discount Account =======//
            $payment_disc_code      = "PaymentDiscount";		
            $payment_disc_account   = $this->input->post('ppay_disc_account');
            $payment_disc_check     = $this->m_preference->cek_service_account($payment_disc_code, $svc_category_id);
            if(empty($payment_disc_check)){
                $dataSend['data_account_insert'][] = array(
                    'SvcCategoryID'     => $svc_category_id, 
                    'AccountCode'       => $payment_disc_code,
                    'AccountNumber'     => $payment_disc_account,
                    'LastUser'          => $this->session->userdata('username')
                );
//               $this->m_preference->insert_service_account($data_account); // masuk ke model
            }else {
                $dataSend['data_account_update'][] = array(
                    'SvcCategoryID'     => $svc_category_id, 
                    'AccountCode'       => $payment_disc_code,
                    'AccountNumber'     => $payment_disc_account,
                    'LastUser'          => $this->session->userdata('username'),
                    
                    'idcode'=> $payment_disc_check //untuk parameter update di model
                );
//                $this->m_preference->update_service_account($data_account, $payment_disc_check); //masuk ke model
            }
            //=============== End Modify Purchase Payment Discount Account ===//

            //=============== Modify UnBilled Account ========================//
            $unbilled_code      = "Unbilled";		
            $unbilled_account   = $this->input->post('unbilled_account');
            $unbilled_check     = $this->m_preference->cek_service_account($unbilled_code, $svc_category_id);
            if(empty($unbilled_check)){
                $dataSend['data_account_insert'][]   = array(
                    'SvcCategoryID'     => $svc_category_id, 
                    'AccountCode'       => $unbilled_code,
                    'AccountNumber'     => $unbilled_account,
                    'LastUser'          => $this->session->userdata('username')
                );
//                $this->m_preference->insert_service_account($data_account);// MASUK KE MODEL
            }else {
                $dataSend['data_account_update'][]   = array(
                    'SvcCategoryID'     => $svc_category_id, 
                    'AccountCode'       => $unbilled_code,
                    'AccountNumber'     => $unbilled_account,
                    'LastUser'          => $this->session->userdata('username'),
                    
                    'idcode'    => $unbilled_check // untuk parameter update di model
                );
//               $this->m_preference->update_service_account($data_account, $unbilled_check);// masuk ke model
            }
            //=============== End Modify UnBilled Account ====================//

            //=============== Modify Purchase Taxable Account ================// 
            $prc_taxable_code      = "PurchaseTaxable";		
            $prc_taxable_account   = $this->input->post('purchase_taxable');
            $prc_taxable_check     = $this->m_preference->cek_service_account($prc_taxable_code, $svc_category_id);
            if(empty($prc_taxable_check)){
                $dataSend['data_account_insert'][]   = array(
                    'SvcCategoryID'     => $svc_category_id, 
                    'AccountCode'       => $prc_taxable_code,
                    'AccountNumber'     => $prc_taxable_account,
                    'LastUser'          => $this->session->userdata('username')
                );
//               $this->m_preference->insert_service_account($data_account);// masuk ke model
            }else {
                $dataSend['data_account_update'][]   = array(
                    'SvcCategoryID'     => $svc_category_id, 
                    'AccountCode'       => $prc_taxable_code,
                    'AccountNumber'     => $prc_taxable_account,
                    'LastUser'          => $this->session->userdata('username'),
                    
                    'idcode' => $prc_taxable_check // untuk parameter update di model
                );
//               $this->m_preference->update_service_account($data_account, $prc_taxable_check);
            }
            //=============== End Modify Purchase Taxable Account ============//

            //=============== Modify Purchase Freight Account ================//
            $prc_freight_code      = "PurchaseFreight";		
            $prc_freight_account   = $this->input->post('purchase_freight');
            $prc_freight_check     = $this->m_preference->cek_service_account($prc_freight_code, $svc_category_id);
            if(empty($prc_freight_check)){
                $dataSend['data_account_insert'][]   = array(
                    'SvcCategoryID'     => $svc_category_id, 
                    'AccountCode'       => $prc_freight_code,
                    'AccountNumber'     => $prc_freight_account,
                    'LastUser'          => $this->session->userdata('username')
                );
//               $this->m_preference->insert_service_account($data_account);// masuk ke model
            }else {
                $dataSend['data_account_update'][]   = array(
                    'SvcCategoryID'     => $svc_category_id, 
                    'AccountCode'       => $prc_freight_code,
                    'AccountNumber'     => $prc_freight_account,
                    'LastUser'          => $this->session->userdata('username'),
                    
                    'idcode' => $prc_freight_check // untuk parameter update di model
                );
//               $this->m_preference->update_service_account($data_account, $prc_freight_check); // masuk ke model
            }
            //=============== End Modify Purchase Freight Account ============//

            //=============== Modify TaxablePPh23 Account ====================//
            $taxpph23_code       = "TaxPPh23";		
            $taxpph23_account    = $this->input->post('taxpph23_account');
            $taxpph23_check      = $this->m_preference->cek_service_account($taxpph23_code, $svc_category_id);
            if(empty($taxpph23_check)){
                $dataSend['data_account_insert'][]   = array(
                    'SvcCategoryID'     => $svc_category_id, 
                    'AccountCode'       => $taxpph23_code,
                    'AccountNumber'     => $taxpph23_account,
                    'LastUser'          => $this->session->userdata('username')
                );
//                $this->m_preference->insert_service_account($data_account);// masuk ke model
            }else {
                $dataSend['data_account_update'][]   = array(
                    'SvcCategoryID'     => $svc_category_id, 
                    'AccountCode'       => $taxpph23_code,
                    'AccountNumber'     => $taxpph23_account,
                    'LastUser'          => $this->session->userdata('username'),
                    
                    'idcode'    => $taxpph23_check // untuk parameter update di model
                );
//                $this->m_preference->update_service_account($data_account, $taxpph23_check); // masuk ke model
            }
            //=============== End Modify TaxablePPh23 Account ================//

            //=============== Modify TaxablePPh21 Account ====================//
            $taxpph21_code       = "TaxPPh21";		
            $taxpph21_account    = $this->input->post('taxpph21_account');
            $taxpph21_check      = $this->m_preference->cek_service_account($taxpph21_code, $svc_category_id);
            if(empty($taxpph21_check)){
                $dataSend['data_account_insert'][] = array(
                    'SvcCategoryID'     => $svc_category_id, 
                    'AccountCode'       => $taxpph21_code,
                    'AccountNumber'     => $taxpph21_account,
                    'LastUser'          => $this->session->userdata('username')
                );
//                $this->m_preference->insert_service_account($data_account); //masuk ke model
            }else {
                $dataSend['data_account_update'][] = array(
                    'SvcCategoryID'     => $svc_category_id, 
                    'AccountCode'       => $taxpph21_code,
                    'AccountNumber'     => $taxpph21_account,
                    'LastUser'          => $this->session->userdata('username'),
                    
                    'idcode'    => $taxpph21_check // untuk parameter update di model
                );
//                $this->m_preference->update_service_account($data_account, $taxpph21_check); //masuk ke model
            }
            //=============== End Modify TaxablePPh21 Account ================//

            //=============== Modify Realized Account ========================// 
            $realized_code      = "Realized";		
            $realized_account   = $this->input->post('realized_account');
            $realized_check     = $this->m_preference->cek_service_account($realized_code, $svc_category_id);
            if(empty($realized_check)){
                $dataSend['data_account_insert'][]   = array(
                    'SvcCategoryID'     => $svc_category_id, 
                    'AccountCode'       => $realized_code,
                    'AccountNumber'     => $realized_account,
                    'LastUser'          => $this->session->userdata('username')
                );
//               $this->m_preference->insert_service_account($data_account); // masuk ke model
            }else {
                $dataSend['data_account_update'][]   = array(
                    'SvcCategoryID'     => $svc_category_id, 
                    'AccountCode'       => $realized_code,
                    'AccountNumber'     => $realized_account,
                    'LastUser'          => $this->session->userdata('username'),
                    
                    'idcode'    => $realized_check // untuk parameter update di model
                );
//                $this->m_preference->update_service_account($data_account, $realized_check);
            }
            //=============== End Modify Realized Account ====================//

            //=============== Modify UnRealized Account ======================//
            $unrealized_code    = "UnRealized";		
            $unrealized_account = $this->input->post('unrealized_account');
            $unrealized_check   = $this->m_preference->cek_service_account($unrealized_code, $svc_category_id);
            if(empty($unrealized_check)){
                $dataSend['data_account_insert'][] = array(
                    'SvcCategoryID'     => $svc_category_id, 
                    'AccountCode'       => $unrealized_code,
                    'AccountNumber'     => $unrealized_account,
                    'LastUser'          => $this->session->userdata('username')
                );
//              $this->m_preference->insert_service_account($data_account); //masuk ke model
            }else {
                $dataSend['data_account_update'][]   = array(
                    'SvcCategoryID'     => $svc_category_id, 
                    'AccountCode'       => $unrealized_code,
                    'AccountNumber'     => $unrealized_account,
                    'LastUser'          => $this->session->userdata('username'),
                    
                    'idcode'  => $unrealized_check//untuk parameter update di model
                );
//               $this->m_preference->update_service_account($data_account, $unrealized_check);
            }
            //=============== End Modify UnRealized Account ======================//
            
            $rstDataSend = $this->m_preference->data_send_preference_service_account($dataSend);
            if($rstDataSend){
                $result['status'] 	= true;
                $result['message']	= get_notification('insert', 1);                
            }else{
                $result['status'] 	= false;
                $result['message']	= get_notification('insert', 0);
            }
            echo json_encode($result);
        
        }catch (Exception $e){            
            $result['status']   = false;
            $result['message']  = $e->getMessage();
            echo json_encode($result);
        }
    }
    function modify_account_LAMA(){
        $this->db->trans_begin();
        $svc_category_id    = $this->input->post('category_id');

        //Modify Sales Expense Account 
        $sls_expense_code       = "SalesExpense";		
        $sls_expense_account    = $this->input->post('sales_expense');
        $sls_expense_check      = $this->m_preference->cek_service_account($sls_expense_code, $svc_category_id);

        if(empty($sls_expense_check)){
            $data_account   = array(
                                'SvcCategoryID'     => $svc_category_id, 
                                'AccountCode'       => $sls_expense_code,
                                'AccountNumber'     => $sls_expense_account,
                                'LastUser'          => $this->session->userdata('username')
                            );

            $this->m_preference->insert_service_account($data_account);
        } 
        else {
            $data_account   = array(
                                'SvcCategoryID'     => $svc_category_id, 
                                'AccountCode'       => $sls_expense_code,
                                'AccountNumber'     => $sls_expense_account,
                                'LastUser'          => $this->session->userdata('username')
                            );

            $this->m_preference->update_service_account($data_account, $sls_expense_check);
        }
//        echo $this->db->last_query();
        //----------------------------------------------------------------------------------------------------------//

        //Modify Sales Advance Account 
        $sls_advance_code       = "SalesAdvance";		
        $sls_advance_account    = $this->input->post('sales_advance');
        $sls_advance_check      = $this->m_preference->cek_service_account($sls_advance_code, $svc_category_id);

        if(empty($sls_advance_check)){
            $data_account   = array(
                                'SvcCategoryID'     => $svc_category_id, 
                                'AccountCode'       => $sls_advance_code,
                                'AccountNumber'     => $sls_advance_account,
                                'LastUser'          => $this->session->userdata('username')
                            );

            $this->m_preference->insert_service_account($data_account);
        } 
        else {
            $data_account   = array(
                                'SvcCategoryID'     => $svc_category_id, 
                                'AccountCode'       => $sls_advance_code,
                                'AccountNumber'     => $sls_advance_account,
                                'LastUser'          => $this->session->userdata('username')
                            );

            $this->m_preference->update_service_account($data_account, $sls_advance_check);
        }

        //Modify Sales Invoice Discount Account 
        $sls_inv_disc_code      = "SalesInvDiscount";		
        $sls_inv_disc_account   = $this->input->post('sales_inv_disc');
        $sls_inv_disc_check     = $this->m_preference->cek_service_account($sls_inv_disc_code, $svc_category_id);

        if(empty($sls_inv_disc_check)){
            $data_account   = array(
                                'SvcCategoryID'     => $svc_category_id, 
                                'AccountCode'       => $sls_inv_disc_code,
                                'AccountNumber'     => $sls_inv_disc_account,
                                'LastUser'          => $this->session->userdata('username')
                            );

            $this->m_preference->insert_service_account($data_account);
        } 
        else {
            $data_account   = array(
                                'SvcCategoryID'     => $svc_category_id, 
                                'AccountCode'       => $sls_inv_disc_code,
                                'AccountNumber'     => $sls_inv_disc_account,
                                'LastUser'          => $this->session->userdata('username')
                            );

           $this->m_preference->update_service_account($data_account, $sls_inv_disc_check);
        }

        //Modify Sales Receipt Discount Account 
        $sls_rec_disc_code      = "SalesRecDiscount";		
        $sls_rec_disc_account   = $this->input->post('sales_rec_disc');
        $sls_rec_disc_check     = $this->m_preference->cek_service_account($sls_rec_disc_code, $svc_category_id);

        if(empty($sls_rec_disc_check)){
            $data_account   = array(
                                'SvcCategoryID'     => $svc_category_id, 
                                'AccountCode'       => $sls_rec_disc_code,
                                'AccountNumber'     => $sls_rec_disc_account,
                                'LastUser'          => $this->session->userdata('username')
                            );

            $this->m_preference->insert_service_account($data_account);
        } 
        else {
            $data_account   = array(
                                'SvcCategoryID'     => $svc_category_id, 
                                'AccountCode'       => $sls_rec_disc_code,
                                'AccountNumber'     => $sls_rec_disc_account,
                                'LastUser'          => $this->session->userdata('username')
                            );

           $this->m_preference->update_service_account($data_account, $sls_rec_disc_check);
        }
        
        //Modify Sales UnBilled Account 
        $sls_unbilled_code      = "SalesUnbilled";		
        $sls_unbilled_account   = $this->input->post('sales_unbilled');
		
        $sls_unbilled_check     = $this->m_preference->cek_service_account($sls_unbilled_code, $svc_category_id);

        if(empty($sls_unbilled_check)){
            $data_account   = array(
                                'SvcCategoryID'     => $svc_category_id, 
                                'AccountCode'       => $sls_unbilled_code,
                                'AccountNumber'     => $sls_unbilled_account,
                                'LastUser'          => $this->session->userdata('username')
                            );

           $this->m_preference->insert_service_account($data_account);
        } 
        else {
            $data_account   = array(
                                'SvcCategoryID'     => $svc_category_id, 
                                'AccountCode'       => $sls_unbilled_code,
                                'AccountNumber'     => $sls_unbilled_account,
                                'LastUser'          => $this->session->userdata('username')
                            );

           $this->m_preference->update_service_account($data_account, $sls_unbilled_check);
        }
        //----------------------------------------------------------------------------------------------------------//
		
		//Modify Sales Taxable Account 
        $sls_taxable_code      = "SalesTaxable";		
        $sls_taxable_account   = $this->input->post('sales_taxable');
        $sls_taxable_check     = $this->m_preference->cek_service_account($sls_taxable_code, $svc_category_id);

        if(empty($sls_taxable_check)){
            $data_account   = array(
                                'SvcCategoryID'     => $svc_category_id, 
                                'AccountCode'       => $sls_taxable_code,
                                'AccountNumber'     => $sls_taxable_account,
                                'LastUser'          => $this->session->userdata('username')
                            );

           $this->m_preference->insert_service_account($data_account);
        } 
        else {
            $data_account   = array(
                                'SvcCategoryID'     => $svc_category_id, 
                                'AccountCode'       => $sls_taxable_code,
                                'AccountNumber'     => $sls_taxable_account,
                                'LastUser'          => $this->session->userdata('username')
                            );

           $this->m_preference->update_service_account($data_account, $sls_taxable_check);
        }
        //----------------------------------------------------------------------------------------------------------// 
		
		//Modify Sales Freight Account 
        $sls_freight_code      = "SalesFreight";		
        $sls_freight_account   = $this->input->post('sales_freight');
        $sls_freight_check     = $this->m_preference->cek_service_account($sls_freight_code, $svc_category_id);

        if(empty($sls_freight_check)){
            $data_account   = array(
                                'SvcCategoryID'     => $svc_category_id, 
                                'AccountCode'       => $sls_freight_code,
                                'AccountNumber'     => $sls_freight_account,
                                'LastUser'          => $this->session->userdata('username')
                            );

           $this->m_preference->insert_service_account($data_account);
        } 
        else {
            $data_account   = array(
                                'SvcCategoryID'     => $svc_category_id, 
                                'AccountCode'       => $sls_freight_code,
                                'AccountNumber'     => $sls_freight_account,
                                'LastUser'          => $this->session->userdata('username')
                            );

           $this->m_preference->update_service_account($data_account, $sls_freight_check);
        }
        //----------------------------------------------------------------------------------------------------------//            

        //Modify Expense Account 
        $expense_code       = "Expense";		
        $expense_account    = $this->input->post('expense_account');
        $expense_check      = $this->m_preference->cek_service_account($expense_code, $svc_category_id);

        if(empty($expense_check)){
            $data_account   = array(
                                'SvcCategoryID'     => $svc_category_id, 
                                'AccountCode'       => $expense_code,
                                'AccountNumber'     => $expense_account,
                                'LastUser'          => $this->session->userdata('username')
                            );

            $this->m_preference->insert_service_account($data_account);
        } 
        else {
            $data_account   = array(
                                'SvcCategoryID'     => $svc_category_id, 
                                'AccountCode'       => $expense_code,
                                'AccountNumber'     => $expense_account,
                                'LastUser'          => $this->session->userdata('username')
                            );

            $this->m_preference->update_service_account($data_account, $expense_check);
        }
        //----------------------------------------------------------------------------------------------------------//
        
        //Modify Advance Account 
        $advance_code       = "Advance";		
        $advance_account    = $this->input->post('advance_account');
        $advance_check      = $this->m_preference->cek_service_account($advance_code, $svc_category_id);

        if(empty($advance_check)){
            $data_account   = array(
                                'SvcCategoryID'     => $svc_category_id, 
                                'AccountCode'       => $advance_code,
                                'AccountNumber'     => $advance_account,
                                'LastUser'          => $this->session->userdata('username')
                            );

            $this->m_preference->insert_service_account($data_account);
        } 
        else {
            $data_account   = array(
                                'SvcCategoryID'     => $svc_category_id, 
                                'AccountCode'       => $advance_code,
                                'AccountNumber'     => $advance_account,
                                'LastUser'          => $this->session->userdata('username')
                            );

            $this->m_preference->update_service_account($data_account, $advance_check);
        }
        //----------------------------------------------------------------------------------------------------------//

        //Modify Purchase Invoice Discount Account 
        $invoice_disc_code      = "InvoiceDiscount";		
        $invoice_disc_account   = $this->input->post('pinv_disc_account');
        $invoice_disc_check     = $this->m_preference->cek_service_account($invoice_disc_code, $svc_category_id);

        if(empty($invoice_disc_check)){
            $data_account   = array(
                                'SvcCategoryID'     => $svc_category_id, 
                                'AccountCode'       => $invoice_disc_code,
                                'AccountNumber'     => $invoice_disc_account,
                                'LastUser'          => $this->session->userdata('username')
                            );

            $this->m_preference->insert_service_account($data_account);
        } 
        else {
            $data_account   = array(
                                'SvcCategoryID'     => $svc_category_id, 
                                'AccountCode'       => $invoice_disc_code,
                                'AccountNumber'     => $invoice_disc_account,
                                'LastUser'          => $this->session->userdata('username')
                            );

            $this->m_preference->update_service_account($data_account, $invoice_disc_check);
        }

        //Modify Purchase Payment Discount Account 
        $payment_disc_code      = "PaymentDiscount";		
        $payment_disc_account   = $this->input->post('ppay_disc_account');
        $payment_disc_check     = $this->m_preference->cek_service_account($payment_disc_code, $svc_category_id);

        if(empty($payment_disc_check)){
            $data_account   = array(
                                'SvcCategoryID'     => $svc_category_id, 
                                'AccountCode'       => $payment_disc_code,
                                'AccountNumber'     => $payment_disc_account,
                                'LastUser'          => $this->session->userdata('username')
                            );

           $this->m_preference->insert_service_account($data_account);
        } 
        else {
            $data_account   = array(
                                'SvcCategoryID'     => $svc_category_id, 
                                'AccountCode'       => $payment_disc_code,
                                'AccountNumber'     => $payment_disc_account,
                                'LastUser'          => $this->session->userdata('username')
                            );

            $this->m_preference->update_service_account($data_account, $payment_disc_check);
        }
        
        //Modify UnBilled Account 
        $unbilled_code      = "Unbilled";		
        $unbilled_account   = $this->input->post('unbilled_account');
        $unbilled_check     = $this->m_preference->cek_service_account($unbilled_code, $svc_category_id);

        if(empty($unbilled_check)){
            $data_account   = array(
                                'SvcCategoryID'     => $svc_category_id, 
                                'AccountCode'       => $unbilled_code,
                                'AccountNumber'     => $unbilled_account,
                                'LastUser'          => $this->session->userdata('username')
                            );

            $this->m_preference->insert_service_account($data_account);
        } 
        else {
            $data_account   = array(
                                'SvcCategoryID'     => $svc_category_id, 
                                'AccountCode'       => $unbilled_code,
                                'AccountNumber'     => $unbilled_account,
                                'LastUser'          => $this->session->userdata('username')
                            );

           $this->m_preference->update_service_account($data_account, $unbilled_check);
        }
        //----------------------------------------------------------------------------------------------------------//
		
		//Modify Purchase Taxable Account 
        $prc_taxable_code      = "PurchaseTaxable";		
        $prc_taxable_account   = $this->input->post('purchase_taxable');
        $prc_taxable_check     = $this->m_preference->cek_service_account($prc_taxable_code, $svc_category_id);

        if(empty($prc_taxable_check)){
            $data_account   = array(
                                'SvcCategoryID'     => $svc_category_id, 
                                'AccountCode'       => $prc_taxable_code,
                                'AccountNumber'     => $prc_taxable_account,
                                'LastUser'          => $this->session->userdata('username')
                            );

           $this->m_preference->insert_service_account($data_account);
        } 
        else {
            $data_account   = array(
                                'SvcCategoryID'     => $svc_category_id, 
                                'AccountCode'       => $prc_taxable_code,
                                'AccountNumber'     => $prc_taxable_account,
                                'LastUser'          => $this->session->userdata('username')
                            );

           $this->m_preference->update_service_account($data_account, $prc_taxable_check);
        }
        //----------------------------------------------------------------------------------------------------------// 
        
		//Modify Purchase Freight Account 
        $prc_freight_code      = "PurchaseFreight";		
        $prc_freight_account   = $this->input->post('purchase_freight');
        $prc_freight_check     = $this->m_preference->cek_service_account($prc_freight_code, $svc_category_id);

        if(empty($prc_freight_check)){
            $data_account   = array(
                                'SvcCategoryID'     => $svc_category_id, 
                                'AccountCode'       => $prc_freight_code,
                                'AccountNumber'     => $prc_freight_account,
                                'LastUser'          => $this->session->userdata('username')
                            );

           $this->m_preference->insert_service_account($data_account);
        } 
        else {
            $data_account   = array(
                                'SvcCategoryID'     => $svc_category_id, 
                                'AccountCode'       => $prc_freight_code,
                                'AccountNumber'     => $prc_freight_account,
                                'LastUser'          => $this->session->userdata('username')
                            );

           $this->m_preference->update_service_account($data_account, $prc_freight_check);
        }
        //----------------------------------------------------------------------------------------------------------// 
		
        //Modify TaxablePPh23 Account 
        $taxpph23_code       = "TaxPPh23";		
        $taxpph23_account    = $this->input->post('taxpph23_account');
        $taxpph23_check      = $this->m_preference->cek_service_account($taxpph23_code, $svc_category_id);

        if(empty($taxpph23_check)){
            $data_account   = array(
                                'SvcCategoryID'     => $svc_category_id, 
                                'AccountCode'       => $taxpph23_code,
                                'AccountNumber'     => $taxpph23_account,
                                'LastUser'          => $this->session->userdata('username')
                            );

            $this->m_preference->insert_service_account($data_account);
        } 
        else {
            $data_account   = array(
                                'SvcCategoryID'     => $svc_category_id, 
                                'AccountCode'       => $taxpph23_code,
                                'AccountNumber'     => $taxpph23_account,
                                'LastUser'          => $this->session->userdata('username')
                            );

            $this->m_preference->update_service_account($data_account, $taxpph23_check);
        }
        //----------------------------------------------------------------------------------------------------------//

        //Modify TaxablePPh21 Account 
        $taxpph21_code       = "TaxPPh21";		
        $taxpph21_account    = $this->input->post('taxpph21_account');
        $taxpph21_check      = $this->m_preference->cek_service_account($taxpph21_code, $svc_category_id);

        if(empty($taxpph21_check)){
            $data_account   = array(
                                'SvcCategoryID'     => $svc_category_id, 
                                'AccountCode'       => $taxpph21_code,
                                'AccountNumber'     => $taxpph21_account,
                                'LastUser'          => $this->session->userdata('username')
                            );

            $this->m_preference->insert_service_account($data_account);
        } 
        else {
            $data_account   = array(
                                'SvcCategoryID'     => $svc_category_id, 
                                'AccountCode'       => $taxpph21_code,
                                'AccountNumber'     => $taxpph21_account,
                                'LastUser'          => $this->session->userdata('username')
                            );

            $this->m_preference->update_service_account($data_account, $taxpph21_check);
        }
        //----------------------------------------------------------------------------------------------------------//
        
        //Modify Realized Account 
        $realized_code      = "Realized";		
        $realized_account   = $this->input->post('realized_account');
        $realized_check     = $this->m_preference->cek_service_account($realized_code, $svc_category_id);

        if(empty($realized_check)){
            $data_account   = array(
                                'SvcCategoryID'     => $svc_category_id, 
                                'AccountCode'       => $realized_code,
                                'AccountNumber'     => $realized_account,
                                'LastUser'          => $this->session->userdata('username')
                            );

           $this->m_preference->insert_service_account($data_account);
        } 
        else {
            $data_account   = array(
                                'SvcCategoryID'     => $svc_category_id, 
                                'AccountCode'       => $realized_code,
                                'AccountNumber'     => $realized_account,
                                'LastUser'          => $this->session->userdata('username')
                            );

            $this->m_preference->update_service_account($data_account, $realized_check);
        }
        //----------------------------------------------------------------------------------------------------------//

        //Modify UnRealized Account 
        $unrealized_code    = "UnRealized";		
        $unrealized_account = $this->input->post('unrealized_account');
        $unrealized_check   = $this->m_preference->cek_service_account($unrealized_code, $svc_category_id);

        if(empty($unrealized_check)){
            $data_account   = array(
                                'SvcCategoryID'     => $svc_category_id, 
                                'AccountCode'       => $unrealized_code,
                                'AccountNumber'     => $unrealized_account,
                                'LastUser'          => $this->session->userdata('username')
                            );

          $this->m_preference->insert_service_account($data_account);
        } 
        else {
            $data_account   = array(
                                'SvcCategoryID'     => $svc_category_id, 
                                'AccountCode'       => $unrealized_code,
                                'AccountNumber'     => $unrealized_account,
                                'LastUser'          => $this->session->userdata('username')
                            );

           $this->m_preference->update_service_account($data_account, $unrealized_check);
        }
        //-----------------------------------------------------------------------------------------------------------//

        /*=============================trans roll back dan trans commit============================/*/
		if ($this->db->trans_status() === FALSE) {
			$this->db->trans_rollback();
			$result['status'] 		= FALSE;
			$result['message']  	= get_notification('insert', 0);       
		}
		else {
			$this->db->trans_commit();
			$result['status'] 		= TRUE;
			$result['message']  	= get_notification('insert', 1);       
		}
		/*==================================end======================================================*/
        
        echo json_encode($result);
    }

    function delete_category(){
        try{
            $svc_category_id  = $this->input->post('svc_category_id');
            if (!empty($svc_category_id)) {
                $dataSend   = [];
                $dataSend['svc_category_id'] = $svc_category_id;
//                $result_delete = $this->m_preference->delete_service_category($svc_category_id);//masuk ke model
                
                $rstDataSend = $this->m_preference->data_send_preference_service_category_delete($dataSend);
                if($rstDataSend){
                    $result['status'] 	= true;
                    $result['message']	= get_notification('delete', 1);                
                }else{
                    $result['status'] 	= false;
                    $result['message']	= get_notification('delete', 0);
                }
                echo json_encode($result);
            }        
        }catch (Exception $e){            
            $result['status']   = false;
            $result['message']  = $e->getMessage();
            echo json_encode($result);
        }
    }
    
    function delete_detail(){
        try{
            $svc_detail_id  = $this->input->post('svc_detail_id');
            if (!empty($svc_detail_id)) {
                $dataSend   = [];
                $dataSend['svc_detail_id'] = $svc_detail_id;
//                $result_delete = $this->m_preference->delete_service_detail($svc_detail_id);

                $rstDataSend = $this->m_preference->data_send_preference_service_detail_delete($dataSend);
                if($rstDataSend){
                    $result['status'] 	= true;
                    $result['message']	= get_notification('delete', 1);                
                }else{
                    $result['status'] 	= false;
                    $result['message']	= get_notification('delete', 0);
                }
                echo json_encode($result);
            }        
        }catch (Exception $e){            
            $result['status']   = false;
            $result['message']  = $e->getMessage();
            echo json_encode($result);
        }
    }
    
    function check_category_name(){
        $service_category_post  = $this->input->post('detail');
        $service_category       = json_decode($service_category_post);
        
        $result['status']       = false;
        $result['flag']         = false;                    
        $result['message']      = '';

        if(count($service_category) > 0){
            $modus_type         = $this->input->post('modus_type');
            $svc_category_id    = $this->input->post('category_id');
            $svc_category_name  = strtoupper(trim($this->input->post('category_name')));
            $lenght_str         = strlen($svc_category_name);
            
            foreach($service_category as $row_category){
                $grd_category_id    = $row_category->SvcCategoryID;
                $grd_category_name  = $row_category->SvcCategoryName;
                
                if(strtoupper(substr($grd_category_name, 0, $lenght_str)) == $svc_category_name){
                    if((($modus_type == 2) && ($grd_category_id != $svc_category_id)) || (($modus_type == 1) && ($grd_category_name != $svc_category_name))){
                        $result['status']   = true;

                        if(strlen($grd_category_name) == strlen($svc_category_name)){
                            $result['flag']     = true;
                            $result['message']  = msg_data_used();                        
                        }
                        else{
                            $result['flag']     = false;                    
                            $result['message']  = 'Data item category berhasil disimpan';
                        }

                        break;
                    }
					else{
						if(strlen($grd_category_name) == strlen($svc_category_name)){
                            $result['flag']     = true;
                            $result['message']  = msg_data_used();                        
                        }
                        else{
                            $result['flag']     = false;                    
                            $result['message']  = 'Data item category berhasil disimpan';
                        }
					}
                }
            }
        }
        
        echo json_encode($result);
    }
    
    function check_detail_name(){
        $service_detail_post    = $this->input->post('detail');
        $service_detail         = json_decode($service_detail_post);
        
        $result['status']       = false;
        $result['flag']         = false;                    
        $result['message']      = '';

        if(count($service_detail) > 0){
            $modus_type         = $this->input->post('modus_type');
            $svc_detail_id      = $this->input->post('detail_id');
            $svc_detail_name    = strtoupper(trim($this->input->post('detail_name')));
            $lenght_str         = strlen($svc_detail_name);            
            
            foreach($service_detail as $row_detail){
                $grd_detail_id      = $row_detail->SvcDetailID;
                $grd_detail_name    = $row_detail->SvcDetailName;
                
                if(strtoupper(substr($grd_detail_name, 0, $lenght_str)) == $svc_detail_name){
                    if((($modus_type == 2) && ($grd_detail_id != $svc_detail_id)) || (($modus_type == 1) && ($grd_detail_name != $svc_detail_name))){
                        $result['status']   = true;
                        
                        if(strlen($grd_detail_name) == strlen($svc_detail_name)){
                            $result['flag']     = true;
                            $result['message']  = msg_data_used();                        
                        }
                        else{
                            $result['flag']     = false;                    
                            $result['message']  = 'Item Expense Detail Name sudah ada, Apakah Anda ingin menyimpan dengan nama yang sama';
                        }

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

    function sales_list_expense_account(){
        $sls_category_id            = $this->input->post('category_id');
        $sls_expense_account        = $this->m_preference->get_service_account($sls_category_id, 'SalesExpense');

        if(!empty($sls_expense_account)){
            $result['AccountNumber']    = $sls_expense_account->AccountNumber;
            echo json_encode($result);
        }
        else{
            echo '[]';
        }
    }

    function sales_list_advance_account(){
        $sls_category_id            = $this->input->post('category_id');
        $sls_advance_account        = $this->m_preference->get_service_account($sls_category_id, 'SalesAdvance');		
        
        if(!empty($sls_advance_account)){
            $result['AccountNumber']    = $sls_advance_account->AccountNumber;
            echo json_encode($result);
        }
        else{
            echo '[]';
        }
    }
    
    function sales_list_sinv_discount_account(){
        $sls_category_id            = $this->input->post('category_id');
        $sls_sinv_disc_account      = $this->m_preference->get_service_account($sls_category_id, 'SalesInvDiscount');

        if(!empty($sls_sinv_disc_account)){
            $result['AccountNumber']    = $sls_sinv_disc_account->AccountNumber;
            echo json_encode($result);
        }
        else{
            echo '[]';
        }
    }

    function sales_list_srec_discount_account(){
        $sls_category_id            = $this->input->post('category_id');
        $sls_srec_disc_account      = $this->m_preference->get_service_account($sls_category_id, 'SalesRecDiscount');

        if(!empty($sls_srec_disc_account)){
            $result['AccountNumber']    = $sls_srec_disc_account->AccountNumber;
            echo json_encode($result);
        }
        else{
            echo '[]';
        }
    }
    
    function sales_list_unbilled_account(){
        $sls_category_id            = $this->input->post('category_id');
        $sls_unbilled_account       = $this->m_preference->get_service_account($sls_category_id, 'SalesUnbilled');	

        if(!empty($sls_unbilled_account)){
            $result['AccountNumber']    = $sls_unbilled_account->AccountNumber;
            echo json_encode($result);
        }
        else{
            echo '[]';
        }
    }
	
	function sales_list_taxableppn10_account(){
        $sls_category_id            = $this->input->post('category_id');
        $sls_taxable_account       = $this->m_preference->get_service_account($sls_category_id, 'SalesTaxable');	

        if(!empty($sls_taxable_account)){
            $result['AccountNumber']    = $sls_taxable_account->AccountNumber;
            echo json_encode($result);
        }
        else{
            echo '[]';
        }
    }
	
	function sales_list_freight_account(){
        $sls_category_id            = $this->input->post('category_id');
        $sls_freight_account       	= $this->m_preference->get_service_account($sls_category_id, 'SalesFreight');	

        if(!empty($sls_freight_account)){
            $result['AccountNumber']    = $sls_freight_account->AccountNumber;
            echo json_encode($result);
        }
        else{
            echo '[]';
        }
    }
    //-------------------------------------------------------------------------------------------------//
        
    function combo_list_expense_account(){
        $svc_category_id            = $this->input->post('category_id');
        $svc_expense_account        = $this->m_preference->get_service_account($svc_category_id, 'Expense');

        if(!empty($svc_expense_account)){
            $result['AccountNumber']    = $svc_expense_account->AccountNumber;
            echo json_encode($result);
        }
        else{
            echo '[]';
        }
    }

    function combo_list_advance_account(){
        $svc_category_id            = $this->input->post('category_id');
        $svc_advance_account        = $this->m_preference->get_service_account($svc_category_id, 'Advance');		

        if(!empty($svc_advance_account)){
            $result['AccountNumber']    = $svc_advance_account->AccountNumber;
            echo json_encode($result);
        }
        else{
            echo '[]';
        }
    }

    function combo_list_pinv_disc_account(){
        $svc_category_id            = $this->input->post('category_id');
        $svc_pinv_disc_account      = $this->m_preference->get_service_account($svc_category_id, 'InvoiceDiscount');

        if(!empty($svc_pinv_disc_account)){
            $result['AccountNumber']    = $svc_pinv_disc_account->AccountNumber;
            echo json_encode($result);
        }
        else{
            echo '[]';
        }
    }
    
    function combo_list_ppay_disc_account(){
        $svc_category_id            = $this->input->post('category_id');
        $svc_ppay_disc_account      = $this->m_preference->get_service_account($svc_category_id, 'PaymentDiscount');

        if(!empty($svc_ppay_disc_account)){
            $result['AccountNumber']    = $svc_ppay_disc_account->AccountNumber;
            echo json_encode($result);
        }
        else{
            echo '[]';
        }
    }
    
    function combo_list_unbilled_account(){
        $svc_category_id            = $this->input->post('category_id');
        $svc_unbilled_account       = $this->m_preference->get_service_account($svc_category_id, 'Unbilled');	

        if(!empty($svc_unbilled_account)){
            $result['AccountNumber']    = $svc_unbilled_account->AccountNumber;
            echo json_encode($result);
        }
        else{
            echo '[]';
        }
    }
	
	function purchase_list_taxableppn10_account(){
        $svc_category_id            = $this->input->post('category_id');
        $svc_taxable_account       = $this->m_preference->get_service_account($svc_category_id, 'PurchaseTaxable');	

        if(!empty($svc_taxable_account)){
            $result['AccountNumber']    = $svc_taxable_account->AccountNumber;
            echo json_encode($result);
        }
        else{
            echo '[]';
        }
    }
	
	function purchase_list_freight_account(){
        $svc_category_id            = $this->input->post('category_id');
        $svc_freight_account       = $this->m_preference->get_service_account($svc_category_id, 'PurchaseFreight');	

        if(!empty($svc_freight_account)){
            $result['AccountNumber']    = $svc_freight_account->AccountNumber;
            echo json_encode($result);
        }
        else{
            echo '[]';
        }
    }
    //-------------------------------------------------------------------------------------------------//

    function combo_list_taxpph23_account(){
        $svc_category_id            = $this->input->post('category_id');
        $svc_taxpph23_account       = $this->m_preference->get_service_account($svc_category_id, 'TaxPPh23');		

        if(!empty($svc_taxpph23_account)){
            $result['AccountNumber']    = $svc_taxpph23_account->AccountNumber;
            echo json_encode($result);
        }
        else{
            echo '[]';
        }
    }

    function combo_list_taxpph21_account(){
        $svc_category_id            = $this->input->post('category_id');
        $svc_taxpph21_account       = $this->m_preference->get_service_account($svc_category_id, 'TaxPPh21');		

        if(!empty($svc_taxpph21_account)){
            $result['AccountNumber']    = $svc_taxpph21_account->AccountNumber;
            echo json_encode($result);
        }
        else{
            echo '[]';
        }
    }
    
    function combo_list_realized_account(){
        $svc_category_id            = $this->input->post('category_id');
        $svc_realized_account       = $this->m_preference->get_service_account($svc_category_id, 'Realized');		

        if(!empty($svc_realized_account)){
            $result['AccountNumber']    = $svc_realized_account->AccountNumber;
            echo json_encode($result);
        }
        else{
            echo '[]';
        }
    }

    function combo_list_unrealized_account(){
        $svc_category_id            = $this->input->post('category_id');
        $svc_unrealized_account     = $this->m_preference->get_service_account($svc_category_id, 'UnRealized');		

        if(!empty($svc_unrealized_account)){
            $result['AccountNumber']    = $svc_unrealized_account->AccountNumber;
            echo json_encode($result);
        }
        else{
            echo '[]';
        }
    }

    function ajax_service_category(){
        $filterRules = isset($_POST['filterRules']) ? ($_POST['filterRules']) : '';
		$field 		 = " "; 
		$value 		 = " "; 
		if (!empty($filterRules)){
			$filterRules = json_decode($filterRules);
			//print_r ($filterRules);
			foreach($filterRules as $rule){
				$field 	= $rule->field;
				$op 	= $rule->op;
				$value 	= $rule->value;
				
				if($field == "SvcDetailTax"){
					$field = "SvcPPh23Status";
					if($value == "No"){
						$value = 0;
					}
					else if($value == "Yes"){
						$value = 1;
					}
				}
				
				if($field != " "){
					$data = $this->m_preference->get_service_category($value, $field);		
        			echo json_encode($data);
				}
			}
		}
		
		if($field == " "){
			$data = $this->m_preference->get_service_category_empty();		
        	echo json_encode($data);

		}      
    }

    function ajax_service_detail(){
		$filterRules = isset($_POST['filterRules']) ? ($_POST['filterRules']) : '';
		$field 		 = " "; 
		$value 		 = " "; 
		if (!empty($filterRules)){
			$filterRules = json_decode($filterRules);
			//print_r ($filterRules);
			foreach($filterRules as $rule){
				$field 	= $rule->field;
				$op 	= $rule->op;
				$value 	= $rule->value;
				
				if($field == "SvcDetailTax"){
					$field = "SvcPPh23Status";
					if($value == "No"){
						$value = 0;
					}
					else if($value == "Yes"){
						$value = 1;
					}
				}
				
				if($field != " "){
					$result = $this->m_preference->get_service_detail($value,$field);
					echo json_encode($result);
				}
			}
		}
		
		if($field == " "){
			$result = $this->m_preference->get_service_detail_empty();
			echo json_encode($result);

		}
    }
    
    public function ajax_item($account_search){
        $result = $this->m_preference->get_coa_account(explode(',', $account_search), 1);        
        echo json_encode($result);
    }
    
    public function trans_rollback($modus) {
        $this->db->trans_rollback();
        $result['status']   = false;
        $result['message']  = get_notification_transaksi($this->name, $modus, 0);
                
        echo json_encode($result);
        die();
    }                    
}

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