<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*	
 *	Author 	: Wahyu / 22-07-2014	
 *	Revisi 	: Ferry / 24-03-2015	
 *	Revisi 	: DHIDHIK / 18 maret 2016
 */
 
class Preference_expense 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');
		$this->load->model('list/m_account_journal');
    }

    public function index(){
        $company = $this->session->userdata('company');		

//        $data['ExpenseGroup']      = $this->m_preference->get_all_category_expense_account();
//        $data['ItemExpense']        = $this->m_preference->get_all_categoies_item_expense();
		
		$PayableAkun 				= array('69', '70', '71', '72', '73', '76');
        $data['ExpenseAkun']        = $this->m_preference->get_expense_akun($company);
        
        $data['PayableAkun']        = $this->m_preference->get_coa_account($PayableAkun, 1);
		
        $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');
        
        $this->load->view('v_header');
        $this->load->view('preference/view_preference_expense', $data);
        $this->load->view('v_footer');
    }
	
    function list_expense_detail($id){
        echo $this->m_preference->get_all_list_expense_detail($id);
    }
	
    function ajax_categori_expense(){
        $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_all_categori_expense($value,$field);
					echo json_encode($result);
				}
			}
		}
		
		if($field == " "){
			$result = $this->m_preference->get_all_categori_expense_empty();
			echo json_encode($result);
		}
    }
	
    function ajax_categori_expense_payment(){
        $result = $this->m_preference->get_all_categori_expense_payment();
        echo json_encode($result);
//        echo $this->db->last_query();die();
    }
	
	function combo_list_payable_account(){
        $exp_category_id            = $this->input->post('category_id');
        $exp_expense_account        = $this->m_preference->get_expense_account($exp_category_id, 'Payable');
		
        if(!empty($exp_expense_account)){
            $result['AccountNumber']    = $exp_expense_account->AccountNumber;
            echo json_encode($result);
        }
        else{
            echo '[]';
        }
    }
	
	function combo_list_realized_account(){
        $exp_category_id            = $this->input->post('category_id');
        $exp_expense_account        = $this->m_preference->get_expense_account($exp_category_id, 'Realized');
        if(!empty($exp_expense_account)){
            $result['AccountNumber']    = $exp_expense_account->AccountNumber;
            echo json_encode($result);
        }
        else{
            echo '[]';
        }
    }
	
	function combo_list_unrealized_account(){
        $exp_category_id            = $this->input->post('category_id');
        $exp_expense_account        = $this->m_preference->get_expense_account($exp_category_id, 'UnRealized');
        if(!empty($exp_expense_account)){
            $result['AccountNumber']    = $exp_expense_account->AccountNumber;
            echo json_encode($result);
        }
        else{
            echo '[]';
        }
    }
	
	function combo_list_expense_account(){
        $exp_category_id            = $this->input->post('category_id');
        $exp_expense_account        = $this->m_preference->get_expense_account($exp_category_id, 'Expense');
        if(!empty($exp_expense_account)){
            $result['AccountNumber']    = $exp_expense_account->AccountNumber;
            echo json_encode($result);
        }
        else{
            echo '[]';
        }
    }
	
    function modify_category(){
        try{
            $advance_category_post  = $this->input->post('detail');
            $advance_category       = json_decode($advance_category_post);
            $result_insert          = 0;
            if(count($advance_category) > 0) {
                $dataSend   = [];
                $dataSend['data_category_expense_insert']  = [];
                $dataSend['data_category_expense_update']  = [];                
                $company_code       = $this->session->userdata('company');	

                foreach($advance_category as $category) {
                    $category_name  = $category->ExpCategoryName;
                    $category_desc  = $category->ExpCategoryDesc;

                    if($category->ExpCategoryType == 1){
                        $dataSend['data_category_expense_insert'][] = array(
                            'CompanyCode'       => $company_code, 
                            'ExpCategoryName'   => $category_name,
                            'ExpCategoryDesc'   => $category_desc, 
                            'LastUser'          => $this->session->userdata('username')
                        );
//                        $this->m_preference->insert_category_expense_account($data_category_expense);// masuk ke model
                    }else if($category->ExpCategoryType == 2) {
                        $category_id    = $category->ExpCategoryID;
                            $dataSend['data_category_expense_update'][] = array(
                                'CompanyCode'       => $company_code, 
                                'ExpCategoryName'   => $category_name,
                                'ExpCategoryDesc'   => $category_desc, 
                                'LastUser'          => $this->session->userdata('username'),
                                'idcode'    => $category_id //untuuk parameter update di model
                            );
//                            $this->m_preference->update_expense_category($data_category_expense, $category_id);
                        }
                }            

                $rstDataSend = $this->m_preference->data_send_preference_expense_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 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);
		$result_insert 			= 0;
        if(count($advance_category) > 0) {
            $this->db->trans_begin();
            $company_code       = $this->session->userdata('company');	

            foreach($advance_category as $category) {
                $category_name  = $category->ExpCategoryName;
                $category_desc  = $category->ExpCategoryDesc;

                if($category->ExpCategoryType == 1){
                    $data_category_expense = array(
						'CompanyCode'       => $company_code, 
						'ExpCategoryName'   => $category_name,
						'ExpCategoryDesc'   => $category_desc, 
						'LastUser'          => $this->session->userdata('username')
					);
					
                    $this->m_preference->insert_category_expense_account($data_category_expense);
                }
                else if($category->ExpCategoryType == 2) {
                    $category_id    = $category->ExpCategoryID;
                    
					$data_category_expense = array(
						'CompanyCode'       => $company_code, 
						'ExpCategoryName'   => $category_name,
						'ExpCategoryDesc'   => $category_desc, 
						'LastUser'          => $this->session->userdata('username')
					);
					
					$this->m_preference->update_expense_category($data_category_expense, $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 Expense Category tidak ditemukan';            
        }
        
        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');
            $exp_category_id    = $this->input->post('category_id');
            $exp_category_name  = strtoupper(trim($this->input->post('category_name')));
            $lenght_str         = strlen($exp_category_name);
            
            foreach($advance_category as $row_category){
                $grd_category_id    = $row_category->ExpCategoryID;
                $grd_category_name  = $row_category->ExpCategoryName;
                
                if(strtoupper(substr($grd_category_name, 0, $lenght_str)) == $exp_category_name){
                    if((($modus_type == 2) && ($grd_category_id != $exp_category_id)) || (($modus_type == 1) && ($grd_category_name != $exp_category_name))){
                        $result['status']   = true;

                        if(strlen($grd_category_name) == strlen($exp_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_category_name_detail(){
		
        $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');
            $exp_category_id    = $this->input->post('category_id');
            $exp_category_name  = strtoupper(trim($this->input->post('category_name')));
            $lenght_str         = strlen($exp_category_name);
            
            foreach($advance_category as $row_category){
                $grd_category_id    = $row_category->ExpDetailID;
                $grd_category_name  = $row_category->ExpDetailName;
                
                if(strtoupper(substr($grd_category_name, 0, $lenght_str)) == $exp_category_name){
                    if((($modus_type == 2) && ($grd_category_id != $exp_category_id)) || (($modus_type == 1) && ($grd_category_name != $exp_category_name))){
                        $result['status']   = true;

                        if(strlen($grd_category_name) == strlen($exp_category_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 modify_detail_pref(){
		try{
            $dataSend   = [];
            $dataSend['data_detail_insert']     = [];
            $dataSend['data_detail_update']     = [];
			
			$advance_detail_post    = $this->input->post('detail');
            $advance_detail         = json_decode($advance_detail_post);
			
			if(count($advance_detail) > 0) {
                foreach($advance_detail as $details) {
                    $detail_name        = $details->ExpDetailName;
                    $detail_desc        = $details->ExpDetailDesc;
					$ExpCategoryID        = $details->ExpCategoryID;
					
                    if($details->ExpDetailType == 1){
                        $dataSend['data_detail_insert'][] = array(
                                'ExpDetailName'     => $detail_name, 
                                'ExpDetailDesc'     => $detail_desc,
                                'ExpCategoryID' 	=> $ExpCategoryID,
                                'LastUser'          => $this->session->userdata('username')
                            );
//                        $this->m_preference->insert_detail_sysexpense($data_detail);
                    }else if($details->ExpDetailType == 2) {
                        $detail_id      = $details->ExpDetailID;
                        $dataSend['data_detail_update'][] = array(
                            'ExpDetailName'     => $detail_name, 
                            'ExpDetailDesc'     => $detail_desc,
                            'ExpCategoryID' 	=> $ExpCategoryID,
                            'LastUser'          => $this->session->userdata('username'),
                            'idcode'    => $detail_id
                        );
//                       $this->m_preference->update_expense_detail_category($data_detail, $detail_id);
                   }
                }

                $rstDataSend = $this->m_preference->data_preference_expense_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(){
        try{
            $dataSend   = [];
            $dataSend['data_account_insert']    = [];
            $dataSend['data_account_update']    = [];
            $dataSend['data_detail_insert']     = [];
            $dataSend['data_detail_update']     = [];
            
            $advance_detail_post    = $this->input->post('detail');
            $advance_detail         = json_decode($advance_detail_post);
            $exp_category_id    	= $this->input->post('categori_expense');
            //=============== Modify Expense Account =========================//
            $expense_code       = "Payable";		
            $expense_account    = $this->input->post('payable_expense');
            $expense_check      = $this->m_preference->cek_expense_account($expense_code, $exp_category_id);
            if(empty($expense_check)){
                    $dataSend['data_account_insert'][]   = array(
                        'ExpCategoryID'     => $exp_category_id, 
                        'AccountCode'       => $expense_code,
                        'AccountNumber'     => $expense_account,
                        'LastUser'          => $this->session->userdata('username')
                    );
//                        $this->m_preference->insert_sysexpense_acc_payable($data_account);// masuk ke model
                }else {
                    $dataSend['data_account_update'][]   = array(
                        'ExpCategoryID'     => $exp_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_sysexpense_acc_payable($data_account, $expense_check);//masuk ke model
                }
            //=============== End Modify Expense Account =====================//

            //=============== Modify Relized Account =========================//
            $realized_code       = "Realized";		
            $realized_account    = $this->input->post('realized_account');
            $realized_check      = $this->m_preference->cek_expense_account($realized_code, $exp_category_id);
            if(empty($realized_check)){
                $dataSend['data_account_insert'][]   = array(
                    'ExpCategoryID'     => $exp_category_id, 
                    'AccountCode'       => $realized_code,
                    'AccountNumber'     => $realized_account,
                    'LastUser'          => $this->session->userdata('username')
                );
//                    $this->m_preference->insert_sysexpense_acc_payable($data_account); // masuk ke model
            }else {
                $dataSend['data_account_update'][]   = array(
                    'ExpCategoryID'     => $exp_category_id, 
                    'AccountCode'       => $realized_code,
                    'AccountNumber'     => $realized_account,
                    'LastUser'          => $this->session->userdata('username'),
                    'idcode'    => $realized_check
                );
//                    $this->m_preference->update_sysexpense_acc_payable($data_account, $realized_check);
            }
            //=============== End Modify Relized Account =====================//

            //=============== Modify UnRelized Account =======================//
            $unrealized_code       = "UnRealized";		
            $unrealized_account    = $this->input->post('unrealized_account');
            $unrealized_check      = $this->m_preference->cek_expense_account($unrealized_code, $exp_category_id);
            if(empty($unrealized_check)){
                $dataSend['data_account_insert'][]   = array(
                    'ExpCategoryID'     => $exp_category_id, 
                    'AccountCode'       => $unrealized_code,
                    'AccountNumber'     => $unrealized_account,
                    'LastUser'          => $this->session->userdata('username')
                );
//                $this->m_preference->insert_sysexpense_acc_payable($data_account);//masuk ke model
            }else {
                $dataSend['data_account_update'][]   = array(
                    'ExpCategoryID'     => $exp_category_id, 
                    'AccountCode'       => $unrealized_code,
                    'AccountNumber'     => $unrealized_account,
                    'LastUser'          => $this->session->userdata('username'),
                    'idcode'    => $unrealized_check //utk parameter update di model
                );
//                $this->m_preference->update_sysexpense_acc_payable($data_account, $unrealized_check);//masuk ke model
            }
            //=============== End Modify UnRelized Account ===================//

            //=============== Modify Expense Account =========================//
            $expense_code       = "Expense";		
            $expense_account    = $this->input->post('expense_account');
            $expense_check      = $this->m_preference->cek_expense_account($expense_code, $exp_category_id);
            if(empty($expense_check)){
                $dataSend['data_account_insert'][]   = array(
                    'ExpCategoryID'     => $exp_category_id, 
                    'AccountCode'       => $expense_code,
                    'AccountNumber'     => $expense_account,
                    'LastUser'          => $this->session->userdata('username')
                );
//                $this->m_preference->insert_sysexpense_acc_payable($data_account);//masuk ke model
            }else {
                $dataSend['data_account_update'][]   = array(
                    'ExpCategoryID'     => $exp_category_id, 
                    'AccountCode'       => $expense_code,
                    'AccountNumber'     => $expense_account,
                    'LastUser'          => $this->session->userdata('username'),
                    'idcode'    => $expense_check // utk parameter update di model
                );
//                $this->m_preference->update_sysexpense_acc_payable($data_account, $expense_check);//masuk ke model
            }
            //=============== End Modify Expense Account =====================//
                    
            if(count($advance_detail) > 0) {
                foreach($advance_detail as $details) {
                    $detail_name        = $details->ExpDetailName;
                    $detail_desc        = $details->ExpDetailDesc;
                    if($details->ExpDetailType == 1){
                        $dataSend['data_detail_insert'][] = array(
                                'ExpDetailName'     => $detail_name, 
                                'ExpDetailDesc'     => $detail_desc,
                                'ExpCategoryID' 	=> $exp_category_id,
                                'LastUser'          => $this->session->userdata('username')
                            );
//                        $this->m_preference->insert_detail_sysexpense($data_detail);
                    }else if($details->ExpDetailType == 2) {
                        $detail_id      = $details->ExpDetailID;
                        $dataSend['data_detail_update'][] = array(
                            'ExpDetailName'     => $detail_name, 
                            'ExpDetailDesc'     => $detail_desc,
                            'ExpCategoryID' 	=> $exp_category_id,
                            'LastUser'          => $this->session->userdata('username'),
                            'idcode'    => $detail_id
                        );
//                       $this->m_preference->update_expense_detail_category($data_detail, $detail_id);
                   }
                }

                $rstDataSend = $this->m_preference->data_send_preference_expense_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);
        $exp_category_id    	= $this->input->post('categori_expense');
		$this->db->trans_begin();
		/*
			Modify Expense Account 
		*/
		$expense_code       = "Payable";		
		$expense_account    = $this->input->post('payable_expense');
		
		$expense_check      = $this->m_preference->cek_expense_account($expense_code, $exp_category_id);

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

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

			$this->m_preference->update_sysexpense_acc_payable($data_account, $expense_check);
			//echo $this->db->last_query();
		}
        //---------------------------------------------------------------------------------------------------------//
		
		/*
			Modify Relized Account 
		*/
		$realized_code       = "Realized";		
		$realized_account    = $this->input->post('realized_account');
		
		$realized_check      = $this->m_preference->cek_expense_account($realized_code, $exp_category_id);

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

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

			$this->m_preference->update_sysexpense_acc_payable($data_account, $realized_check);
			//echo $this->db->last_query();
		}
        //---------------------------------------------------------------------------------------------------------//
			
		/*
			Modify UnRelized Account 
		*/
		$unrealized_code       = "UnRealized";		
		$unrealized_account    = $this->input->post('unrealized_account');
		
		$unrealized_check      = $this->m_preference->cek_expense_account($unrealized_code, $exp_category_id);

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

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

			$this->m_preference->update_sysexpense_acc_payable($data_account, $unrealized_check);
			//echo $this->db->last_query();
		}
        //---------------------------------------------------------------------------------------------------------//
		
		/*
			Modify Expense Account 
		*/
		$expense_code       = "Expense";		
		$expense_account    = $this->input->post('expense_account');
		
		$expense_check      = $this->m_preference->cek_expense_account($expense_code, $exp_category_id);

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

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

			$this->m_preference->update_sysexpense_acc_payable($data_account, $expense_check);
			//echo $this->db->last_query();
		}
        //---------------------------------------------------------------------------------------------------------//
        if(count($advance_detail) > 0) {

            foreach($advance_detail as $details) {
                $detail_name        = $details->ExpDetailName;
                $detail_desc        = $details->ExpDetailDesc;
                
                if($details->ExpDetailType == 1){
                    $data_detail = array(
                                        'ExpDetailName'     => $detail_name, 
                                        'ExpDetailDesc'     => $detail_desc,
										'ExpCategoryID' 	=> $exp_category_id,
                                        'LastUser'          => $this->session->userdata('username')
                                    );

                    $this->m_preference->insert_detail_sysexpense($data_detail);
                }
                else if($details->ExpDetailType == 2) {
                    $detail_id      = $details->ExpDetailID;

                   $data_detail = array(
                                        'ExpDetailName'     => $detail_name, 
                                        'ExpDetailDesc'     => $detail_desc,
										'ExpCategoryID' 	=> $exp_category_id,
                                        'LastUser'          => $this->session->userdata('username')
                                    );

                   $this->m_preference->update_expense_detail_category($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 ajax_expense_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_expense_detail($value,$field);
					echo json_encode($result);
				}
			}
		}
		
		if($field == " "){
			$result = $this->m_preference->get_expense_detail_empty();
			echo json_encode($result);
		}
    }
	
	public function trans_rollback($modus) {
        $this->db->trans_rollback();
        $result['status']   = false;
        $result['message']  = get_notification($modus, 0);
                
        echo json_encode($result);
        die();
    } 
	
    function delete_detail(){
        try{
            $exp_detail_id  = $this->input->post('exp_detail_id');
            if (!empty($exp_detail_id)) {
                $dataSend   = [];
                $dataSend['exp_detail_id'] = $exp_detail_id;
//                $result_delete = $this->m_preference->delete_expense_detail($exp_detail_id); 
                
                $rstDataSend = $this->m_preference->data_send_preference_expense_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_category(){
        try{
            $exp_category_id  = $this->input->post('exp_category_id');
            if (!empty($exp_category_id)) {
                $dataSend   = [];
                $dataSend['exp_category_id'] = $exp_category_id;
                //$result_delete = $this->m_preference->delete_expen_category($exp_category_id);
                 
                $rstDataSend = $this->m_preference->data_send_preference_expense_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(){
        $exp_category_id  = $this->input->post('exp_category_id');
       	$this->db->trans_begin();
        if (!empty($exp_category_id)) {
            $result_delete = $this->m_preference->delete_expen_category($exp_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('delete', 0);       
			}
			else {
				$this->db->trans_commit();
				$result['status'] 		= TRUE;
				$result['message']  	= get_notification('delete', 1);       
			}
			/*==================================end======================================================*/

            echo json_encode($result);
        }
    }
}

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