<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*	
 *	Author 	: Wahyu / Tanggal : 22-07-2014	
 *	Revisi 	: Ferry
 */
 
class Preference_miscellaneous extends CI_Controller {
    public function __construct() {
	    parent::__construct();
        $this->load->model('m_preference');
    }

    public function index(){	
        $data['aging_range']    = $this->m_preference->get_invoice_aging_range();  //ValueAgingRange (PreferenceValue)
        $data['tolerance']      = $this->m_preference->get_tolerance();              //select_days (PreferenceValue)
        //set data
        $this->load->view('v_header');		
        $this->load->view('preference/view_preference_miscellaneous', $data);		
        $this->load->view('v_footer');
    }
	
    public function proses_post_data_miscellaneous(){
        try{	
            $ValueAgingRange    = $this->input->post('ValueAgingRange');//invoice
            $ValueTolerance     = $this->input->post('ValueTolerance');
            $dataSend   = [];
            $dataSend['ValueAgingRange']    = $ValueAgingRange;
            $dataSend['ValueTolerance']     = $ValueTolerance;
            $dataSend['cek_agingDueDateExist'] = false;
            $dataSend['cek_ToleranceExist']    = false;
                        		
//            $company = $this->session->userdata("company");
            
            $cek_agingRangeExist = $this->m_preference->check_num_sys_preference("IARange");
            $dataSend['cek_agingDueDateExist'] = $cek_agingRangeExist;

            $cek_ToleranceExist = $this->m_preference->check_num_sys_preference("ToleranceID");
            $dataSend['cek_ToleranceExist'] = $cek_ToleranceExist;

            $rstDataSend = $this->m_preference->data_send_preference_approval_insert_update($dataSend);//tinggal di model
            if($rstDataSend){
                $result['status'] 	= true;
                $result['message']	= "update Berhasil"; 
                
//                $data['aging_range']    = $this->m_preference->get_invoice_aging_range();  //ValueAgingRange (PreferenceValue)
//                $data['tolerance']      = $this->m_preference->get_tolerance();              //select_days (PreferenceValue)
            }else{
                $result['status'] 	= false;
                $result['message']	= "update gagal";
            }
            echo json_encode($result);
        }catch (Exception $e){            
            $result['status']   = false;
            $result['message']  = $e->getMessage();
            echo json_encode($result);
        }
    }
	
    public function proses_post_data_miscellaneous_LAMA(){  
        $agingDueDateID = $this->input->post('agingDueDateID');	
        $agingRangeID = $this->input->post('agingRangeID');
        $CalculateAgingID = $this->input->post('CalculateAgingID');
        $DigitDesimalID = $this->input->post('DigitDesimalID');
        $CogsMethodID = $this->input->post('CogsMethodID');
        $ToleranceID = $this->input->post('ToleranceID');
		
		$cek_agingDueDateID = $this->m_preference->cek_data_agingDueDate($agingDueDateID);
		if(!empty($cek_agingDueDateID)){
			$this->m_preference->update_aging_due_date($agingDueDateID);
		} else {
			$this->m_preference->insert_aging_due_date($agingDueDateID);
		}
		
		$cek_agingRangeID = $this->m_preference->cek_data_agingRange($agingRangeID);
		if(!empty($cek_agingDueDateID)){
			$this->m_preference->update_aging_range($agingRangeID);
		} else {
			$this->m_preference->insert_aging_range($agingRangeID);
		}
		
		$cek_CalculateAgingID = $this->m_preference->cek_data_calculate_aging($CalculateAgingID);
		if(!empty($cek_CalculateAgingID)){
			$this->m_preference->update_calculate_aging($CalculateAgingID);
		} else {
			$this->m_preference->insert_calculate_aging($CalculateAgingID);
		}
		
		$cek_DigitDesimalID = $this->m_preference->cek_data_desimal_digit($DigitDesimalID);
		if(!empty($cek_DigitDesimalID)){
			$this->m_preference->update_digit_desimal($DigitDesimalID);
		} else {
		    $this->m_preference->insert_digit_desimal($DigitDesimalID);
		}
		
		$cek_ToleranceID = $this->m_preference->cek_data_tolerance($ToleranceID);
		if(!empty($cek_ToleranceID)){
			$this->m_preference->update_tolerance($ToleranceID);
		} else {
		    $this->m_preference->insert_tolerance($ToleranceID);
		}
		
		$cek_CogsMethodID = $this->m_preference->cek_data_cogs_method($CogsMethodID);
		if(!empty($cek_CogsMethodID)){
			$this->m_preference->update_cogs_method($CogsMethodID);
		} else {
		    $this->m_preference->insert_cogs_method($CogsMethodID);
		}
		
		$data['aging_due_date'] = $this->m_preference->get_invoice_aging_due_date();	
        $data['aging_range'] = $this->m_preference->get_invoice_aging_range();
		$data['calculate_aging'] = $this->m_preference->get_calculate_aging();
		$data['digit_desimal'] = $this->m_preference->get_digit_desimal();
		$data['rounding'] = $this->m_preference->get_rounding();
		$data['tolerance'] = $this->m_preference->get_tolerance();
		$data['cogs_method'] = $this->m_preference->get_cogs_method();
		
                //set data
		$this->load->view('v_header');		
		$this->load->view('preference/view_preference_miscellaneous', $data);		
		$this->load->view('v_footer');
	}
}

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