<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/*  Author  : Ladur /Tanggal : 21-07-2014	
 *  Revisi  : 
 */

class Custom extends CI_Controller {
    public function __construct() {
        parent::__construct();	
        $this->load->model('m_menu');	
        $this->load->model('m_custom');
        $this->load->helper('message_helper');
    }	
	
    public function cek() {
        $this->set_session();
		
        $menu = $this->uri->segment(3);
        $operation = $this->uri->segment(4);
        $id = $this->uri->segment(5);
		
        if(!in_array($menu, $this->session->userdata['menu'])){			
            redirect(base_url() . 'error', 'refresh');
        }
        else {
            // set session 
            $this->session->set_userdata('checking', TRUE);
			
            $link = $this->m_menu->get_menulink($menu);
			
            switch($operation) {
                case "add" : 
                    redirect(base_url() . $link."/".$operation, 'refresh');
                break;
            
                case "edit" : 
                    redirect(base_url() . $link."/".$operation."/".$id, 'refresh');
                break;
            
                case "delete" : 
                    redirect(base_url() . $link."/".$operation."/".$id, 'refresh');
                break;
            
                case "detail" : 
                    redirect(base_url() . $link."/".$operation."/".$id, 'refresh');
                break;
            
                case "to_preview" : 
                    redirect(base_url() . $link."/".$operation, 'refresh');
                break;
            
                case "to_pdf" : 
                    redirect(base_url() . $link."/".$operation, 'refresh');
                break;
            
                case "to_excel" : 
                    redirect(base_url() . $link."/".$operation, 'refresh');
                break;
            
                default : 
                    redirect(base_url() . $link, 'refresh');
                break;
            }
        } 
    }
	
    public function ajax() {
        // $model = $this->uri->segment(3);
        // $data = $this->load->model($model)->get_all();

        header('Content-Type: text/json');
        ob_clean();
        flush();        
        echo json_encode($data);
    }
	
    public function preview() {
        $data['data'] = $this->m_currency->get_all();
        $this->load->view('v_print_currency',$data);		
    }
	
    function add_userlog(){
        $data = array(
                    'UserName'      => $this->session->userdata('username'), 
                    'ModuleName'    => $ModuleName, 
                    'ActivityName'  => $ActivityName, 
                    'ActivityItem'  => $ActivityItem, 
                    'LoginDate'     => $this->session->userdata('login_date'), 
                    'LoginTime'     => $this->session->userdata('login_date')
                );
    }
	
    function set_session() {
        $data = array(
                    'menu' => array(93,100,157,158,1,2,3,4,6,7,8,9,10, 189, 190, 191, 192, 193, 194, 195, 196, 197, 222, 223, 135),
                    'login' => TRUE
                );

        $this->session->set_userdata($data);
    }
	
    function un_set_session() {
        $data = array(
                    'menu'  => '',
                    'login' => ''
                );
		
		$this->session->unset_userdata($data);
    }
	
    function change_jobtitlestatus(){
        $JobTitleStatus = $this->input->post('jobtitle_status');
        $this->session->set_userdata('jobtitle_status', $JobTitleStatus);
    }
	
    function change_department(){
        $DepartmentCode = $this->input->post('department_code');
        $this->session->set_userdata('department', $DepartmentCode);
    }

    function checking_approval(){
        $no_transaksi       = $this->input->post('no_transaksi');

        $return             = $this->m_custom->m_checking_approval($no_transaksi);

        if(count($return)>0){
            $arr_return     = array('status'=>true,'message'=>get_notification('checking_approval',0));
        }else{
            $arr_return     = array('status'=>false,'message'=>'');
        }

        echo json_encode($arr_return);
    }

    function checking_suspend(){
        $no_transaksi     = $this->input->post('no_transaksi');

        $return           = $this->m_custom->m_checking_suspend($no_transaksi);

        if(count($return)>0){
            $arr_return     = array('status'=>true,'message'=>'');
        }else{
            $arr_return     = array('status'=>false,'message'=>get_notification('checking_suspend',0)); 
        }

        echo json_encode($arr_return);
    }

    function checking_update(){
        $no_transaksi     = $this->input->post('no_transaksi');
        $status           = $this->input->post('status');

        $return           = $this->m_custom->m_checking_update($no_transaksi,$status);
        //echo $this->db->last_query();

        if(count($return)>0){
            $arr_return = array('status'=>true,'message'=>'');
        }else{
            $arr_return = array('status'=>false,'message'=>get_notification('checking_update',0));
        }

        echo json_encode($arr_return);
    }

    function checking_payment(){
        $no_transaksi       = $this->input->post('no_transaksi');
        $type_trans         = $this->input->post('type_trans');

        if($type_trans == 'AIns'){
            $tbl            = 'at_afiliasiinstallment';
            $where          = "at_afiliasiinstallment.ADebtNumber = '$no_transaksi' and at_afiliasiinstallment.TransStatus=5";
        }elseif($type_trans == 'Eins'){
            $tbl            = 'at_employeeinstallment';
            $where          = "at_employeeinstallment.EDebtNumber = '$no_transaksi' at_employeeinstallment.TransStatus=4";
        }

        $return             = $this->m_custom->m_checking_payment($tbl,$where);

        if(count($return)>0){
            $arr_return = array('status'=>true);
        }else{
            $arr_return = array('status'=>false);
        }

        echo json_encode($arr_return);
    }

    function checking_reverse(){
        $no_transaksi     = $this->input->post('no_transaksi');

        $return           = $this->m_custom->m_checking_reverse($no_transaksi);
        //echo $this->db->last_query();

        if(count($return)>0){
            $arr_return = array('status'=>true,'message'=>'');
        }else{
            $arr_return = array('status'=>false,'message'=>get_notification('checking_reverse',0));
        }

        echo json_encode($arr_return);
    }

    function checking_payment_save(){
        $no_piutang     = $this->input->post('no_piutang');
        $period_to      = $this->input->post('period_to');
        $type_trans     = $this->input->post('type_trans');

        if(($type_trans == 'AIns') or ($type_trans == 'AInj')){
            $tbl            = 'at_afiliasiinstallment';
            $tbl_detail     = 'at_ainsdetail';
            $join           = $tbl_detail.'.AInsNumber='.$tbl.'.AInsNumber';
            $where          = "at_ainsdetail.ADebtNumber = '$no_piutang' and at_ainsdetail.PaymentPeriod = '$period_to' and at_afiliasiinstallment.TransStatus!=3";
        }elseif(($type_trans == 'EIns') or ($type_trans == 'EInj')){
            $tbl            = 'at_employeeinstallment';
            $tbl_detail     = 'at_einsdetail';
            $join           = $tbl_detail.'.EInsNumber='.$tbl.'.EInsNumber';
            $where          = "at_einsdetail.EDebtNumber = '$no_piutang' and at_einsdetail.PaymentPeriod = '$period_to' and at_employeeinstallment.TransStatus!=3";
        }elseif(($type_trans == 'LPay') or ($type_trans == 'LPaj')){
            $tbl            = 'at_loanpayment';
            $tbl_detail     = 'at_lpdetail';
            $join           = $tbl_detail.'.LPNumber='.$tbl.'.LPNumber';
            $where          = "at_lpdetail.LPNumber = '$no_piutang' and at_lpdetail.PaymentPeriod = '$period_to' and at_loanpayment.TransStatus!=3";
        }

        $return         = $this->m_custom->m_checking_payment_save($tbl,$tbl_detail,$where,$join);

        if(count($return)>0){
            $arr_return = array('status'=>true,'message'=>get_notification('checking_payment_save',0));
        }else{
            $arr_return = array('status'=>false,'message'=>'');
        }

        echo json_encode($arr_return);
    }

    function get_bank_balance(){
        $AccountNumber  = $this->input->post('AccountNumber');
        $branch         = $this->session->userdata('branch');

        $bank_balance   = $this->m_custom->m_get_bank_balance($branch,$AccountNumber);

        if($bank_balance == false){
            $return = array('message'=>false,'value'=>0);
        }else{
            $return = array('message'=>true,'value'=>$bank_balance);
        }

        echo json_encode($return);
    }

    function get_master_rate(){
        $CurrencyCode       = $this->input->post('CurrencyCode');
        $company            = $this->session->userdata('company');

        $master_rate        = $this->m_custom->m_get_master_rate($company,$CurrencyCode);

        if($master_rate == false){
            $return = array('message'=>false,'value'=>0);
        }else{
            $return = array('message'=>true,'value'=>$master_rate);
        }

        echo json_encode($return);
    }

    function get_data_session(){
        $status_branch  = array(
                    'status_branch' => $this->session->userdata('branch_status'),
                    'branch'        => $this->session->userdata('branch'));

        echo  json_encode($status_branch);
    }

    function get_paging(){
        $branch         = $this->session->userdata('branch');
        $nm_table       = $this->input->post('nm_table');
        $nm_field       = $this->input->post('nm_field');
        $TransNumber    = $this->input->post('TransNumber');
        $CreatedAt      = $this->input->post('CreatedAt');

        $prev           = $this->m_custom->get_nextprev_transnumber($nm_table,$nm_field,$branch,$CreatedAt,'DESC');
        $next           = $this->m_custom->get_nextprev_transnumber($nm_table,$nm_field,$branch,$CreatedAt,'ASC');

        $result         = array('prev' => $prev,'next' => $next);
        
        echo json_encode($result); 
    }

    function search_key($value,$search,$array) {
       foreach ($array as $key => $val) {
           if ($val[$search] === $value) {
               return $key;
           }
       }
        return FALSE; 
       
    }

    function checking_transstatus(){
        $no_transaksi      = $this->input->post('no_transaksi');
        $return            = $this->m_custom->checking_transstatus($no_transaksi);
        //echo $this->db->last_query();

        if(count($return)>0){
            $arr_return = array('status'=>false,'message'=>get_notification('checking_transstatus',0));
        }else{
            $arr_return = array('status'=>true,'message'=>'');
        }

        echo json_encode($arr_return);
    }

}