<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/* Author 	: Ferry / 31-03-2015 */
 
class Preference_advances 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 ExpenseAccount(){
		$get_ExpenseAccount = $this->m_preference->get_coa_account(array('69', '70', '71', '72', '73', '76'), 1);
		echo json_encode($get_ExpenseAccount);
	}
	
	function AdvanceAccount(){
		$get_AdvanceAccount =  $this->m_preference->get_coa_account('31', 0);
		echo json_encode($get_AdvanceAccount);
	}
	
	function UangMukaAccount(){
		$get_UangMukaAccount =  $this->m_preference->get_coa_account('Uang Muka Pembelian IDR', 4);
		echo json_encode($get_UangMukaAccount);
	}
	
	function InvoiceDiscount(){
		$get_InvoiceDiscount =  $this->m_preference->get_coa_account('68', 0);
		echo json_encode($get_InvoiceDiscount);
	}
	
	function PaymentDiscount(){
		$get_PaymentDiscount =  $this->m_preference->get_coa_account('Retur Pembelian', 2);
		echo json_encode($get_PaymentDiscount);
	}
	
	function UnbilledAccount(){
		$get_UnbilledAccount =  $this->m_preference->get_coa_account('Hutang Advance Payment', 2);
		echo json_encode($get_UnbilledAccount);
	}
	
	function RealizedAccount(){
		$get_RealizedAccount =  $this->m_preference->get_coa_account('Selisih Kurs Transaksi', '2');
		echo json_encode($get_RealizedAccount);
	}
	
	function UnRealizedAccount(){
		$get_UnRealizedAccount =  $this->m_preference->get_coa_account('Selisih Kurs Akhir Periode', '2');
		echo json_encode($get_UnRealizedAccount);
	}
	
	function TaxablePPnPurchase(){
		$get_TaxablePPnPurchase =  $this->m_preference->get_coa_account('PPN Masukan', 4);//Tambahan baru fahri, tgl 15/01/2015
		echo json_encode($get_TaxablePPnPurchase);
	}
	
	function FreightAccount(){
		$get_FreightAccount =  $this->m_preference->get_coa_account('Biaya Freight In', 2);//Tambahan baru fahri, tgl 15/01/2015
		echo json_encode($get_FreightAccount);
	}
		
    public function index(){
        /*$data['ExpenseAccount']     = $this->m_preference->get_coa_account(array('69', '70', '71', '72', '73', '76'), 1);
        $data['AdvanceAccount']     = $this->m_preference->get_coa_account('31', 0);
		$data['UangMukaAccount']    = $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 Advance Payment', 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['TaxablePPnPurchase'] = $this->m_preference->get_coa_account('PPN Masukan', 4);//Tambahan baru fahri, tgl 15/01/2015
		$data['FreightAccount'] 	= $this->m_preference->get_coa_account('Biaya Freight In', 2);//Tambahan baru fahri, tgl 15/01/2015*/
		$data['jobtitle'] = $this->session->userdata('jobtitle');	
        $this->load->view('v_header');
        $this->load->view('preference/view_preference_advances', $data);
        $this->load->view('v_footer');
    }
	
    function modify_category(){
        try{            
            $advance_category_post  = $this->input->post('detail');
            $advance_category       = json_decode($advance_category_post);

            if(count($advance_category) > 0) {                
                $dataSend = [];
                $dataSend['data_category_insert']  = [];
                $dataSend['data_category_update']  = [];
                
                $company_code       = $this->session->userdata('company');	

                foreach($advance_category as $category) {
                    $category_name  = $category->AdvCategoryName;
                    $category_desc  = $category->AdvCategoryDesc;

                    if($category->AdvCategoryType == 1){
                        $dataSend['data_category_insert'][]  = array(
                            'CompanyCode'       => $company_code, 
                            'AdvCategoryName'   => $category_name, 
                            'AdvCategoryDesc'   => $category_desc, 
                            'LastUser'          => $this->session->userdata('username')
                        ); 
//                        $result_insert = $this->m_preference->insert_advance_category($data_category);
                    }
                    else if($category->AdvCategoryType == 2) {
                        $category_id    = $category->AdvCategoryID;

                        $dataSend['data_category_update'][]  = array(
                            'AdvCategoryName'   => $category_name, 
                            'AdvCategoryDesc'   => $category_desc, 
                            'LastUser'          => $this->session->userdata('username'),
                            
                            'category_id'       => $category_id
                        ); 
//                        $result_insert = $this->m_preference->update_advance_category($data_category, $category_id);
                    }
                }            
            
                $rstDataSend = $this->m_preference->data_send_preference_advances_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(){
        $advance_category_post  = $this->input->post('detail');
        $advance_category       = json_decode($advance_category_post);

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

            foreach($advance_category as $category) {
                $category_name  = $category->AdvCategoryName;
                $category_desc  = $category->AdvCategoryDesc;

                if($category->AdvCategoryType == 1){
                    $data_category  = array(
                                        'CompanyCode'       => $company_code, 
                                        'AdvCategoryName'   => $category_name, 
                                        'AdvCategoryDesc'   => $category_desc, 
                                        'LastUser'          => $this->session->userdata('username')
                                      ); 
                    $result_insert = $this->m_preference->insert_advance_category($data_category);
                }
                else if($category->AdvCategoryType == 2) {
                    $category_id    = $category->AdvCategoryID;
                    
                    $data_category  = array(
                                        'AdvCategoryName'   => $category_name, 
                                        'AdvCategoryDesc'   => $category_desc, 
                                        'LastUser'          => $this->session->userdata('username')
                                      ); 

                    $result_insert = $this->m_preference->update_advance_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_data(){
		try{
			$advance_detail_post    = $this->input->post('detail');
        	$advance_detail         = json_decode($advance_detail_post);
			
			$dataSend['data_detail_insert']     = [];
            $dataSend['data_detail_update']     = [];
				
			foreach($advance_detail as $details) {
				$detail_name        = $details->AdvDetailName;
				$detail_desc        = $details->AdvDetailDesc;
				$adv_netduein       = $details->AdvNetDueIn;
				$AdvCategoryID   	= $details->AdvCategoryID;

				if($details->AdvDetailType == 1){
					$dataSend['data_detail_insert'][] = array(
						'AdvCategoryID'     => $AdvCategoryID, 
						'AdvDetailName'     => $detail_name, 
						'AdvDetailDesc'     => $detail_desc,
						'AdvNetDueIn'       => $adv_netduein,
						'LastUser'          => $this->session->userdata('username')
					);
//                        $result_insert = $this->m_preference->insert_advance_detail($data_detail);
				}else if($details->AdvDetailType == 2) {
					$detail_id      = $details->AdvDetailID;
					$dataSend['data_detail_update'][]  = array(
						'AdvDetailName'     => $detail_name, 
						'AdvCategoryID'     => $AdvCategoryID, 
						'AdvDetailDesc'     => $detail_desc,
						'AdvNetDueIn'       => $adv_netduein,
						'LastUser'          => $this->session->userdata('username'),
						
						'idcode'            => $detail_id
					);
//                        $result_insert = $this->m_preference->update_advance_detail($data_detail, $detail_id);
			   }
			}
			
			$rstDataSend = $this->m_preference->data_advances_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);
		}
		catch (Exception $e){            
            $result['status']   = false;
            $result['message']  = $e->getMessage();
            echo json_encode($result);
        }
	}
	
    function modify_detail(){
        try{
            $advance_detail_post    = $this->input->post('detail');
            $advance_detail         = json_decode($advance_detail_post);

            if(count($advance_detail) > 0) {
                $dataSend   = [];
                $dataSend['data_account_insert']    = [];
                $dataSend['data_account_update']    = [];
                $dataSend['data_detail_insert']     = [];
                $dataSend['data_detail_update']     = [];
                
                $adv_category_id    = $this->input->post('category_id');

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

                //========== Modify Sewa DIbayar Dimuka Account ==============//
                $advance_code       = "Advance";		
                $advance_account    = $this->input->post('advance_account');
                $advance_check      = $this->m_preference->cek_advance_account($advance_code, $adv_category_id);

                if(empty($advance_check)){
                    $dataSend['data_account_insert'][]   = array(
                        'AdvCategoryID'     => $adv_category_id, 
                        'AccountCode'       => $advance_code,
                        'AccountNumber'     => $advance_account,
                        'LastUser'          => $this->session->userdata('username')
                    );
//                    $result_insert = $this->m_preference->insert_advance_account($data_account);
                }else {
                    $dataSend['data_account_update'][]   = array(
                        'AdvCategoryID'     => $adv_category_id, 
                        'AccountCode'       => $advance_code,
                        'AccountNumber'     => $advance_account,
                        'LastUser'          => $this->session->userdata('username'),
                        
                        'idcode'            => $advance_check
                    );
//                    $result_insert = $this->m_preference->update_advance_account($data_account, $advance_check);
                }
                //========== End Modify Sewa DIbayar Dimuka Account ==========//

                //========== Modify Advance Account ==========================//
                $advance_code       = "DownPayment";		
                $advance_account    = $this->input->post('uangmuka_account');
                $advance_check      = $this->m_preference->cek_advance_account($advance_code, $adv_category_id);
                if(empty($advance_check)){
                    $dataSend['data_account_insert'][]   = array(
                        'AdvCategoryID'     => $adv_category_id, 
                        'AccountCode'       => $advance_code,
                        'AccountNumber'     => $advance_account,
                        'LastUser'          => $this->session->userdata('username')
                    );
//                    $result_insert = $this->m_preference->insert_advance_account($data_account);
                }else {
                    $dataSend['data_account_update'][]   = array(
                        'AdvCategoryID'     => $adv_category_id, 
                        'AccountCode'       => $advance_code,
                        'AccountNumber'     => $advance_account,
                        'LastUser'          => $this->session->userdata('username'),
                        
                        'idcode'            => $advance_check
                    );
//                    $result_insert = $this->m_preference->update_advance_account($data_account, $advance_check);
                }
                //========== End Modify Advance Account ======================//

                //========== Modify Invoice Discount Account =================//
                $account_code       = "InvoiceDiscount";		
                $pinv_disc_account  = $this->input->post('pinv_disc_account');
                $account_check      = $this->m_preference->cek_advance_account($account_code, $adv_category_id);
                if(empty($account_check)){
                    $dataSend['data_account_insert'][]  = array(
                        'AdvCategoryID'     => $adv_category_id, 
                        'AccountCode'       => $account_code,
                        'AccountNumber'     => $pinv_disc_account,
                        'LastUser'          => $this->session->userdata('username')
                    );
//                    $result_insert = $this->m_preference->insert_advance_account($data_account);
                }else {
                    $dataSend['data_account_update'][]   = array(
                        'AdvCategoryID'     => $adv_category_id, 
                        'AccountCode'       => $account_code,
                        'AccountNumber'     => $pinv_disc_account,
                        'LastUser'          => $this->session->userdata('username'),
                        
                        'idcode'            => $account_check //untuk parameter update di model
                    );
//                    $result_insert = $this->m_preference->update_advance_account($data_account, $account_check);//masuk ke model
                }
                //========== End Modify Invoice Discount Account =============//

                //========== Modify Payment Discount Account =================//
                $account_code       = "PaymentDiscount";		
                $ppay_disc_account  = $this->input->post('ppay_disc_account');
                $account_check      = $this->m_preference->cek_advance_account($account_code, $adv_category_id);
                if(empty($account_check)){
                    $dataSend['data_account_insert'][]   = array(
                        'AdvCategoryID'     => $adv_category_id, 
                        'AccountCode'       => $account_code,
                        'AccountNumber'     => $ppay_disc_account,
                        'LastUser'          => $this->session->userdata('username')
                    );
//                    $result_insert = $this->m_preference->insert_advance_account($data_account);//masuk ke model
                }else {
                    $dataSend['data_account_update'][]   = array(
                        'AdvCategoryID'     => $adv_category_id, 
                        'AccountCode'       => $account_code,
                        'AccountNumber'     => $ppay_disc_account,
                        'LastUser'          => $this->session->userdata('username'),
                        
                        'idcode'            => $account_check //untuk parameter update di model
                    );
//                    $result_insert = $this->m_preference->update_advance_account($data_account, $account_check);
                }
                //========== End Modify Payment Discount Account =============//

                //========== Modify UnBilled Account =========================//
                $account_code       = "Unbilled";		
                $unbilled_account   = $this->input->post('unbilled_account');
                $account_check      = $this->m_preference->cek_advance_account($account_code, $adv_category_id);
                if(empty($account_check)){
                    $dataSend['data_account_insert'][]   = array(
                        'AdvCategoryID'     => $adv_category_id, 
                        'AccountCode'       => $account_code,
                        'AccountNumber'     => $unbilled_account,
                        'LastUser'          => $this->session->userdata('username')
                    );
//                    $result_insert = $this->m_preference->insert_advance_account($data_account);//masuk ke model
                }else {
                    $dataSend['data_account_update'][]   = array(
                        'AdvCategoryID'     => $adv_category_id, 
                        'AccountCode'       => $account_code,
                        'AccountNumber'     => $unbilled_account,
                        'LastUser'          => $this->session->userdata('username'),
                        
                        'idcode'            => $account_check //untuk parameter update di model
                    );
//                    $result_insert = $this->m_preference->update_advance_account($data_account, $account_check);//masuk ke model
                }
                //========== End Modify UnBilled Account =====================//

                //========== Modify Realized Account =========================//
                $account_code       = "Realized";		
                $realized_account   = $this->input->post('realized_account');
                $account_check      = $this->m_preference->cek_advance_account($account_code, $adv_category_id);
                if(empty($account_check)){
                    $dataSend['data_account_insert'][]   = array(
                        'AdvCategoryID'     => $adv_category_id, 
                        'AccountCode'       => $account_code,
                        'AccountNumber'     => $realized_account,
                        'LastUser'          => $this->session->userdata('username')
                    );
//                    $result_insert = $this->m_preference->insert_advance_account($data_account);//masuk ke model
                }else{
                    $dataSend['data_account_update'][]   = array(
                        'AdvCategoryID'     => $adv_category_id, 
                        'AccountCode'       => $account_code,
                        'AccountNumber'     => $realized_account,
                        'LastUser'          => $this->session->userdata('username'),
                        
                        'idcode'            => $account_check //untuk parameter update di model
                    );
//                    $result_insert = $this->m_preference->update_advance_account($data_account, $account_check);//masuk ke model
                }
                //========== End Modify Realized Account =====================//

                //========== Modify UnRealized Account =======================//
                $account_code       = "UnRealized";		
                $unrealized_account = $this->input->post('unrealized_account');
                $account_check      = $this->m_preference->cek_advance_account($account_code, $adv_category_id);
                if(empty($account_check)){
                    $dataSend['data_account_insert'][]   = array(
                        'AdvCategoryID'     => $adv_category_id, 
                        'AccountCode'       => $account_code,
                        'AccountNumber'     => $unrealized_account,
                        'LastUser'          => $this->session->userdata('username')
                    );
//                    $result_insert = $this->m_preference->insert_advance_account($data_account);
                }else{
                    $dataSend['data_account_update'][]   = array(
                        'AdvCategoryID'     => $adv_category_id, 
                        'AccountCode'       => $account_code,
                        'AccountNumber'     => $unrealized_account,
                        'LastUser'          => $this->session->userdata('username'),
                        
                        'idcode'            => $account_check //untuk parameter update di model
                    );
//                    $result_insert = $this->m_preference->update_advance_account($data_account, $account_check);// masuk ke model
                }
                //========== End Modify UnRealized Account ===================//

                //========== Modify Tax ppn 10 ===============================//
                $account_code       = "PurchaseTaxable";		
                $taxableppn_account = $this->input->post('taxableppn_account');
                $account_check      = $this->m_preference->cek_advance_account($account_code, $adv_category_id);
                if(empty($account_check)){
                    $dataSend['data_account_insert'][]   = array(
                        'AdvCategoryID'     => $adv_category_id, 
                        'AccountCode'       => $account_code,
                        'AccountNumber'     => $taxableppn_account,
                        'LastUser'          => $this->session->userdata('username')
                    );
//                    $result_insert = $this->m_preference->insert_advance_account($data_account);//masuk ke model
                }else {
                    $dataSend['data_account_update'][]   = array(
                        'AdvCategoryID'     => $adv_category_id, 
                        'AccountCode'       => $account_code,
                        'AccountNumber'     => $taxableppn_account,
                        'LastUser'          => $this->session->userdata('username'),
                        
                        'idcode'            => $account_check //untuk parameter update di model
                    );
//                    $result_insert = $this->m_preference->update_advance_account($data_account, $account_check);//masuk ke model
                }
                //========== End Modify Tax ppn 10 ===========================//

                //========== Modify Freight ==================================//
                $account_code       = "FreightAccount";		
                $freight_account    = $this->input->post('freight_account');
                $account_check      = $this->m_preference->cek_advance_account($account_code, $adv_category_id);
                if(empty($account_check)){
                    $dataSend['data_account_insert'][]   = array(
                        'AdvCategoryID'     => $adv_category_id, 
                        'AccountCode'       => $account_code,
                        'AccountNumber'     => $freight_account,
                        'LastUser'          => $this->session->userdata('username')
                    );
//                    $result_insert = $this->m_preference->insert_advance_account($data_account);
                }else {
                    $dataSend['data_account_update'][]   = array(
                        'AdvCategoryID'     => $adv_category_id, 
                        'AccountCode'       => $account_code,
                        'AccountNumber'     => $freight_account,
                        'LastUser'          => $this->session->userdata('username'),
                        
                        'idcode'            => $account_check //untuk parameter update di model
                    );
//                    $result_insert = $this->m_preference->update_advance_account($data_account, $account_check);
                }
                //========== End Modify Freight ==================================//

                foreach($advance_detail as $details) {
                    $detail_name        = $details->AdvDetailName;
                    $detail_desc        = $details->AdvDetailDesc;
                    $adv_netduein       = $details->AdvNetDueIn;

                    if($details->AdvDetailType == 1){
                        $dataSend['data_detail_insert'][] = array(
                            'AdvCategoryID'     => $adv_category_id, 
                            'AdvDetailName'     => $detail_name, 
                            'AdvDetailDesc'     => $detail_desc,
                            'AdvNetDueIn'       => $adv_netduein,
                            'LastUser'          => $this->session->userdata('username')
                        );
//                        $result_insert = $this->m_preference->insert_advance_detail($data_detail);
                    }else if($details->AdvDetailType == 2) {
                        $detail_id      = $details->AdvDetailID;
                        $dataSend['data_detail_update'][]  = array(
                            'AdvDetailName'     => $detail_name, 
                            'AdvDetailDesc'     => $detail_desc,
                            'AdvNetDueIn'       => $adv_netduein,
                            'LastUser'          => $this->session->userdata('username'),
                            
                            'idcode'            => $detail_id
                        );
//                        $result_insert = $this->m_preference->update_advance_detail($data_detail, $detail_id);
                   }
                }

                $rstDataSend = $this->m_preference->data_send_preference_advances_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(){
        $advance_detail_post    = $this->input->post('detail');
        $advance_detail         = json_decode($advance_detail_post);
        
        if(count($advance_detail) > 0) {
            $this->db->trans_begin();
            $adv_category_id    = $this->input->post('category_id');

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

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

                $result_insert = $this->m_preference->insert_advance_account($data_account);
            } 
            else {
                $data_account   = array(
                                    'AdvCategoryID'     => $adv_category_id, 
                                    'AccountCode'       => $expense_code,
                                    'AccountNumber'     => $expense_account,
                                    'LastUser'          => $this->session->userdata('username')
                                );

                $result_insert = $this->m_preference->update_advance_account($data_account, $expense_check);
            }
            //---------------------------------------------------------------------------------------------------------//

            //Modify Sewa DIbayar Dimuka Account 
            $advance_code       = "Advance";		
            $advance_account    = $this->input->post('advance_account');
            $advance_check      = $this->m_preference->cek_advance_account($advance_code, $adv_category_id);

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

                $result_insert = $this->m_preference->insert_advance_account($data_account);
            } 
            else {
                $data_account   = array(
                                    'AdvCategoryID'     => $adv_category_id, 
                                    'AccountCode'       => $advance_code,
                                    'AccountNumber'     => $advance_account,
                                    'LastUser'          => $this->session->userdata('username')
                                );

                $result_insert = $this->m_preference->update_advance_account($data_account, $advance_check);
            }
            //---------------------------------------------------------------------------------------------------------//
            
			//Modify Advance Account 
            $advance_code       = "DownPayment";		
            $advance_account    = $this->input->post('uangmuka_account');
            $advance_check      = $this->m_preference->cek_advance_account($advance_code, $adv_category_id);

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

                $result_insert = $this->m_preference->insert_advance_account($data_account);
            } 
            else {
                $data_account   = array(
                                    'AdvCategoryID'     => $adv_category_id, 
                                    'AccountCode'       => $advance_code,
                                    'AccountNumber'     => $advance_account,
                                    'LastUser'          => $this->session->userdata('username')
                                );

                $result_insert = $this->m_preference->update_advance_account($data_account, $advance_check);
            }
            //---------------------------------------------------------------------------------------------------------//
			            
            //Modify Invoice Discount Account 
            $account_code       = "InvoiceDiscount";		
            $pinv_disc_account  = $this->input->post('pinv_disc_account');
            $account_check      = $this->m_preference->cek_advance_account($account_code, $adv_category_id);

            if(empty($account_check)){
                $data_account   = array(
                                    'AdvCategoryID'     => $adv_category_id, 
                                    'AccountCode'       => $account_code,
                                    'AccountNumber'     => $pinv_disc_account,
                                    'LastUser'          => $this->session->userdata('username')
                                );

                $result_insert = $this->m_preference->insert_advance_account($data_account);
            } 
            else {
                $data_account   = array(
                                    'AdvCategoryID'     => $adv_category_id, 
                                    'AccountCode'       => $account_code,
                                    'AccountNumber'     => $pinv_disc_account,
                                    'LastUser'          => $this->session->userdata('username')
                                );

                $result_insert = $this->m_preference->update_advance_account($data_account, $account_check);
            }
            
            //Modify Payment Discount Account 
            $account_code       = "PaymentDiscount";		
            $ppay_disc_account  = $this->input->post('ppay_disc_account');
            $account_check      = $this->m_preference->cek_advance_account($account_code, $adv_category_id);

            if(empty($account_check)){
                $data_account   = array(
                                    'AdvCategoryID'     => $adv_category_id, 
                                    'AccountCode'       => $account_code,
                                    'AccountNumber'     => $ppay_disc_account,
                                    'LastUser'          => $this->session->userdata('username')
                                );

                $result_insert = $this->m_preference->insert_advance_account($data_account);
            } 
            else {
                $data_account   = array(
                                    'AdvCategoryID'     => $adv_category_id, 
                                    'AccountCode'       => $account_code,
                                    'AccountNumber'     => $ppay_disc_account,
                                    'LastUser'          => $this->session->userdata('username')
                                );

                $result_insert = $this->m_preference->update_advance_account($data_account, $account_check);
            }
            
            //Modify UnBilled Account 
            $account_code       = "Unbilled";		
            $unbilled_account   = $this->input->post('unbilled_account');
            $account_check      = $this->m_preference->cek_advance_account($account_code, $adv_category_id);

            if(empty($account_check)){
                $data_account   = array(
                                    'AdvCategoryID'     => $adv_category_id, 
                                    'AccountCode'       => $account_code,
                                    'AccountNumber'     => $unbilled_account,
                                    'LastUser'          => $this->session->userdata('username')
                                );

                $result_insert = $this->m_preference->insert_advance_account($data_account);
            } 
            else {
                $data_account   = array(
                                    'AdvCategoryID'     => $adv_category_id, 
                                    'AccountCode'       => $account_code,
                                    'AccountNumber'     => $unbilled_account,
                                    'LastUser'          => $this->session->userdata('username')
                                );

                $result_insert = $this->m_preference->update_advance_account($data_account, $account_check);
            }
            
            //Modify Realized Account 
            $account_code       = "Realized";		
            $realized_account   = $this->input->post('realized_account');
            $account_check      = $this->m_preference->cek_advance_account($account_code, $adv_category_id);

            if(empty($account_check)){
                $data_account   = array(
                                    'AdvCategoryID'     => $adv_category_id, 
                                    'AccountCode'       => $account_code,
                                    'AccountNumber'     => $realized_account,
                                    'LastUser'          => $this->session->userdata('username')
                                );

                $result_insert = $this->m_preference->insert_advance_account($data_account);
            } 
            else {
                $data_account   = array(
                                    'AdvCategoryID'     => $adv_category_id, 
                                    'AccountCode'       => $account_code,
                                    'AccountNumber'     => $realized_account,
                                    'LastUser'          => $this->session->userdata('username')
                                );

                $result_insert = $this->m_preference->update_advance_account($data_account, $account_check);
            }
            
            //Modify UnRealized Account 
            $account_code       = "UnRealized";		
            $unrealized_account = $this->input->post('unrealized_account');
            $account_check      = $this->m_preference->cek_advance_account($account_code, $adv_category_id);

            if(empty($account_check)){
                $data_account   = array(
                                    'AdvCategoryID'     => $adv_category_id, 
                                    'AccountCode'       => $account_code,
                                    'AccountNumber'     => $unrealized_account,
                                    'LastUser'          => $this->session->userdata('username')
                                );

                $result_insert = $this->m_preference->insert_advance_account($data_account);
            } 
            else {
                $data_account   = array(
                                    'AdvCategoryID'     => $adv_category_id, 
                                    'AccountCode'       => $account_code,
                                    'AccountNumber'     => $unrealized_account,
                                    'LastUser'          => $this->session->userdata('username')
                                );

                $result_insert = $this->m_preference->update_advance_account($data_account, $account_check);
            }
            //-----------------------------------------------------------------------------------------------------------//
			
			//Modify Tax ppn 10, tgl 15/01/2016 
            $account_code       = "PurchaseTaxable";		
            $taxableppn_account = $this->input->post('taxableppn_account');
            $account_check      = $this->m_preference->cek_advance_account($account_code, $adv_category_id);

            if(empty($account_check)){
                $data_account   = array(
                                    'AdvCategoryID'     => $adv_category_id, 
                                    'AccountCode'       => $account_code,
                                    'AccountNumber'     => $taxableppn_account,
                                    'LastUser'          => $this->session->userdata('username')
                                );

                $result_insert = $this->m_preference->insert_advance_account($data_account);
            } 
            else {
                $data_account   = array(
                                    'AdvCategoryID'     => $adv_category_id, 
                                    'AccountCode'       => $account_code,
                                    'AccountNumber'     => $taxableppn_account,
                                    'LastUser'          => $this->session->userdata('username')
                                );

                $result_insert = $this->m_preference->update_advance_account($data_account, $account_check);
            }
            //-----------------------------------------------------------------------------------------------------------//
			
			//Modify Freight, tgl 27/01/2016 
            $account_code       = "FreightAccount";		
            $freight_account 	= $this->input->post('freight_account');
            $account_check      = $this->m_preference->cek_advance_account($account_code, $adv_category_id);

            if(empty($account_check)){
                $data_account   = array(
                                    'AdvCategoryID'     => $adv_category_id, 
                                    'AccountCode'       => $account_code,
                                    'AccountNumber'     => $freight_account,
                                    'LastUser'          => $this->session->userdata('username')
                                );

                $result_insert = $this->m_preference->insert_advance_account($data_account);
            } 
            else {
                $data_account   = array(
                                    'AdvCategoryID'     => $adv_category_id, 
                                    'AccountCode'       => $account_code,
                                    'AccountNumber'     => $freight_account,
                                    'LastUser'          => $this->session->userdata('username')
                                );

                $result_insert = $this->m_preference->update_advance_account($data_account, $account_check);
            }
            //-----------------------------------------------------------------------------------------------------------//
            
            foreach($advance_detail as $details) {
                $detail_name        = $details->AdvDetailName;
                $detail_desc        = $details->AdvDetailDesc;
                $adv_netduein       = $details->AdvNetDueIn;
                
                if($details->AdvDetailType == 1){
                    $data_detail = array(
                                        'AdvCategoryID'     => $adv_category_id, 
                                        'AdvDetailName'     => $detail_name, 
                                        'AdvDetailDesc'     => $detail_desc,
                                        'AdvNetDueIn'       => $adv_netduein,
                                        'LastUser'          => $this->session->userdata('username')
                                    );

                    $result_insert = $this->m_preference->insert_advance_detail($data_detail);
                }
                else if($details->AdvDetailType == 2) {
                    $detail_id      = $details->AdvDetailID;

                    $data_detail = array(
                                        'AdvDetailName'     => $detail_name, 
                                        'AdvDetailDesc'     => $detail_desc,
                                        'AdvNetDueIn'       => $adv_netduein,
                                        'LastUser'          => $this->session->userdata('username')
                                    );

                    $result_insert = $this->m_preference->update_advance_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 delete_category(){
        try{
            $adv_category_id  = $this->input->post('adv_category_id');
            if (!empty($adv_category_id)) {
                $dataSend   = [];
                $dataSend['adv_category_id'] = $adv_category_id;
//                $result_delete = $this->m_preference->delete_advance_category($adv_category_id);
                
                $rstDataSend = $this->m_preference->data_send_preference_advances_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_category_LAMA(){
        $adv_category_id  = $this->input->post('adv_category_id');
       	$this->db->trans_begin();
        if (!empty($adv_category_id)) {
            $result_delete = $this->m_preference->delete_advance_category($adv_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======================================================*/

            echo json_encode($result);
        }
    }
    
    function delete_detail(){
        try{
            $adv_detail_id  = $this->input->post('adv_detail_id');

            if (!empty($adv_detail_id)) {
                $dataSend   = [];
                $dataSend['adv_detail_id'] = $adv_detail_id;
//                $result_delete = $this->m_preference->delete_advance_detail($adv_detail_id);
                
                $rstDataSend = $this->m_preference->data_send_preference_advances_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 delete_detail_LAMA(){
        $adv_detail_id  = $this->input->post('adv_detail_id');

        if (!empty($adv_detail_id)) {
            $this->db->trans_begin();
            $result_delete = $this->m_preference->delete_advance_detail($adv_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======================================================*/

            echo json_encode($result);
        }
    }
    
    function check_category_name(){
        $advance_category_post  = $this->input->post('detail');
        $advance_category       = json_decode($advance_category_post);
        
        $result['status']       = false;
        $result['flag']         = false;                    
        $result['message']      = '';

        if(count($advance_category) > 0){
            $modus_type         = $this->input->post('modus_type');
            $adv_category_id    = $this->input->post('category_id');
            $adv_category_name  = strtoupper(trim($this->input->post('category_name')));
            $lenght_str         = strlen($adv_category_name);
            
            foreach($advance_category as $row_category){
                $grd_category_id    = $row_category->AdvCategoryID;
                $grd_category_name  = $row_category->AdvCategoryName;
                
                if(strtoupper(substr($grd_category_name, 0, $lenght_str)) == $adv_category_name){
                    if((($modus_type == 2) && ($grd_category_id != $adv_category_id)) || (($modus_type == 1) && ($grd_category_name != $adv_category_name))){
                        $result['status']   = true;

                        if(strlen($grd_category_name) == strlen($adv_category_name)){
                            $result['flag']     = true;
                            $result['message']  = msg_data_used();                        
                        }
                        else{
                            $result['flag']     = false;                    
                            $result['message']  = 'Item Expense Category Name sudah ada, Apakah Anda ingin menyimpan dengan nama yang sama';
                        }

                        break;
                    }
                }
            }
        }
        
        echo json_encode($result);
    }
    
    function check_detail_name(){
        $advance_detail_post    = $this->input->post('detail');
        $advance_detail         = json_decode($advance_detail_post);
        
        $result['status']       = false;
        $result['flag']         = false;                    
        $result['message']      = '';

        if(count($advance_detail) > 0){
            $modus_type         = $this->input->post('modus_type');
            $adv_detail_id      = $this->input->post('detail_id');
            $adv_detail_name    = strtoupper(trim($this->input->post('detail_name')));
            $lenght_str         = strlen($adv_detail_name);            
            
            foreach($advance_detail as $row_detail){
                $grd_detail_id      = $row_detail->AdvDetailID;
                $grd_detail_name    = $row_detail->AdvDetailName;
                
                if(strtoupper(substr($grd_detail_name, 0, $lenght_str)) == $adv_detail_name) {
                    if((($modus_type == 2) && ($grd_detail_id != $adv_detail_id)) || (($modus_type == 1) && ($grd_detail_name != $adv_detail_name))){
                        $result['status']   = true;
                        
                        if(strlen($grd_detail_name) == strlen($adv_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 combo_list_expense_account(){
        $adv_category_id            = $this->input->post('category_id');
        $adv_expense_account        = $this->m_preference->get_advance_account($adv_category_id, 'Expense');
        if(!empty($adv_expense_account)){
            $result['AccountNumber']    = $adv_expense_account->AccountNumber;
            echo json_encode($result);
        }
        else{
            echo '[]';
        }
    }

    function combo_list_advance_account(){
        $adv_category_id            = $this->input->post('category_id');
        $adv_advance_account        = $this->m_preference->get_advance_account($adv_category_id, 'Advance');		

        if(!empty($adv_advance_account)){
            $result['AccountNumber']    = $adv_advance_account->AccountNumber;
            echo json_encode($result);
        }
        else{
            echo '[]';
        }
    }
    
	function combo_list_uangmuka_account(){
        $adv_category_id            = $this->input->post('category_id');
        $adv_advance_account        = $this->m_preference->get_advance_account($adv_category_id, 'DownPayment');		

        if(!empty($adv_advance_account)){
            $result['AccountNumber']    = $adv_advance_account->AccountNumber;
            echo json_encode($result);
        }
        else{
            echo '[]';
        }
    }
	
    function combo_list_realized_account(){
        $adv_category_id            = $this->input->post('category_id');
        $adv_realized_account       = $this->m_preference->get_advance_account($adv_category_id, 'Realized');		

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

    function combo_list_pinv_discount_account(){
        $adv_category_id            = $this->input->post('category_id');
        $adv_pinv_disc_account      = $this->m_preference->get_advance_account($adv_category_id, 'InvoiceDiscount');		
        
        if(!empty($adv_pinv_disc_account)){
            $result['AccountNumber']    = $adv_pinv_disc_account->AccountNumber;
            echo json_encode($result);
        }
        else{
            echo '[]';
        }
    }

    function combo_list_ppay_discount_account(){
        $adv_category_id            = $this->input->post('category_id');
        $adv_ppay_disc_account      = $this->m_preference->get_advance_account($adv_category_id, 'PaymentDiscount');		

        if(!empty($adv_ppay_disc_account)){
            $result['AccountNumber']    = $adv_ppay_disc_account->AccountNumber;
            echo json_encode($result);
        }
        else{
            echo '[]';
        }
    }
    
    function combo_list_unbilled_account(){
        $adv_category_id            = $this->input->post('category_id');
        $adv_unbilled_account       = $this->m_preference->get_advance_account($adv_category_id, 'Unbilled');		

        if(!empty($adv_unbilled_account)){
            $result['AccountNumber']    = $adv_unbilled_account->AccountNumber;
            echo json_encode($result);
        }
        else{
            echo '[]';
        }
    }
        
    function combo_list_unrealized_account(){
        $adv_category_id            = $this->input->post('category_id');
        $adv_unrealized_account     = $this->m_preference->get_advance_account($adv_category_id, 'UnRealized');		

        if(!empty($adv_unrealized_account)){
            $result['AccountNumber']    = $adv_unrealized_account->AccountNumber;
            echo json_encode($result);
        }
        else{
            echo '[]';
        }
    }
	
	/*
		Tambahan baru tgl 15/01/2015, fahri
	*/
	function combo_list_taxable(){
        $adv_category_id            = $this->input->post('category_id');
        $adv_taxable_account     = $this->m_preference->get_advance_account($adv_category_id, 'PurchaseTaxable');		

        if(!empty($adv_taxable_account)){
            $result['AccountNumber']    = $adv_taxable_account->AccountNumber;
            echo json_encode($result);
        }
        else{
            echo '[]';
        }
    }
	
	function combo_list_freight(){
        $adv_category_id         = $this->input->post('category_id');
        $adv_freight_account     = $this->m_preference->get_advance_account($adv_category_id, 'FreightAccount');		
		
        if(!empty($adv_freight_account)){
            $result['AccountNumber']    = $adv_freight_account->AccountNumber;
            echo json_encode($result);
        }
        else{
            echo '[]';
        }
    }
	/*=========================End=================================*/
	
    function ajax_advance_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 != " "){
					$result = $this->m_preference->get_advance_category($value,$field);
					echo json_encode($result);
				}
			}
		}
		
		if($field == " "){
			$result = $this->m_preference->get_advance_category_empty();
			echo json_encode($result);

		}  
    }

    function ajax_advance_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 != " "){
					$result = $this->m_preference->get_advance_detail($value,$field);
					echo json_encode($result);
				}
			}
		}
		
		if($field == " "){
			$result = $this->m_preference->get_advance_detail_empty();
			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_advances.php */
/* Location: ./application/controllers/preference_advances.php */