<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*	
 *	Author 	: Wahyu
 *	Tanggal : 02-09-2014	
 *	Revisi 	: 
 */
 
class Terms extends CI_Controller {
    // inisialisasi manual
	public $menu_id = 184;
	public $menu_model = 'm_terms';
	public $name = "Term Description";	
  
    public function __construct()
    {
        parent::__construct();
		$this->load->model($this->menu_model);
		$this->load->helper('regex');
		$this->load->helper('message');
		$this->load->helper('status');
		$this->load->model('login/m_login');
	
		$is_login = $this->m_login->get_session_id($this->session->userdata('session_id'),$this->session->userdata('username'));
		if($is_login){

		}else{
			$username       = $this->session->userdata('username');
			$session_id     = $this->session->userdata('session_id');
			$this->m_login->delete_session($username,$session_id);
			$this->session->sess_destroy();

			$uri_func = $this->uri->segment(3);
			if(($uri_func === FALSE) || ($uri_func =='add') || ($uri_func =='edit')){
				redirect(base_url() . 'login', 'refresh');
			}else if(in_array($uri_func,array('approve_data','approved_mgr','approved_dir','approve','approve_transsaction','reverse_data','reversed_data','reverse_transsaction','save','save_data','insert','update','update_data','suspend','suspend_data','suspend_transsaction','suspend_mgr','suspend_dir'))){
				$result['status']   = 'session';
				$result['message']  = get_notification('session_destroy',0);
				echo json_encode($result);
				die();
			}
		}
	}
	
	//Function Index
    public function index(){
		if ($this->session->userdata("login") == TRUE) { 
            $group_menu             = $this->session->userdata('group_menu');
            $arr_group_id           = $this->m_menu->get_menu_by_group($group_menu, $this->menu_id);
            if(!empty($arr_group_id)){
				$data['available_menu'] = array_unique($arr_group_id);
				if(!in_array($this->menu_id, $data['available_menu'])) { 
					redirect(base_url() . 'error', 'refresh');	
				}
				else{
					//untuk ambil data dari session dan hak akses
					$group_menu 		= $this->session->userdata('group_menu');
					
					// set data
					$data['menu_id'] =  $this->menu_id;
					
					// get link from tbl_menu 
					$data['menu_label'] =  $this->m_menu->get_menulabel($this->menu_id);
					$data['menu_link']  = $this->m_menu->get_menulink($this->menu_id);
					
					//panggil model hak akses untuk autoriti
					$data['group_user'] = $this->m_menu->group_user_autority($this->menu_id, $group_menu);
					
					$this->load->view('v_header');
					$this->load->view('others/terms/v_terms', $data);	
					$this->load->view('v_footer');
				}
			}
			else{
				redirect(base_url() . 'error', 'refresh');
			}
        }
        else {
            redirect(base_url() . 'login', 'refresh');
        }	
	}
	
	//Function Menampilkan Data Pada Data Grid View
	public function ajax(){
		echo json_encode($this->m_terms->get_all());
	}
	
	//Functiom Add Form
	public function add(){
		if ($this->session->userdata("login") == TRUE) { 
            $group_menu             = $this->session->userdata('group_menu');
			/*
				untuk di index/list = get_menu_by_group, 
				untuk di add = get_menu_by_group_add
				untuk di edit = get_menu_by_group_edit
			*/
            $arr_group_id           = $this->m_menu->get_menu_by_group_add($group_menu, $this->menu_id);
			//--------------------------end-----------------------------------------------------------
            if(!empty($arr_group_id)){
				$data['available_menu'] = array_unique($arr_group_id);
				if(!in_array($this->menu_id, $data['available_menu'])) { 
					redirect(base_url() . 'error', 'refresh');	
				}
				else{
					/*
						modul untuk akses add masing masing modul berebda 
						jangan lupa copy $data nya (jika dia bener langsung ke add)
					*/
					//untuk ambil data dari session dan hak akses
					$group_menu 		= $this->session->userdata('group_menu');
					
					// get link from tbl_menu 
					$data['menu_label'] =  $this->m_menu->get_menulabel($this->menu_id);
					$data['menu_link']  = $this->m_menu->get_menulink($this->menu_id);
					
					//panggil model hak akses untuk autoriti
					$data['group_user'] = $this->m_menu->group_user_autority($this->menu_id, $group_menu);
					
					$this->load->view('v_header');
					$this->load->view('others/terms/v_add_terms',$data);	
					$this->load->view('v_footer');	
				}
			}
			else{
				redirect(base_url() . 'error', 'refresh');
			}
        }
        else {
            redirect(base_url() . 'login', 'refresh');
        }
	}
	
	// Function Insert
    public function insert() {
        try{
            $txt_paid_within		= $this->input->post('txt_paid_within');
            $txt_discount_percent 	= $this->input->post('txt_discount_percent');
            $txt_net_due_in 		= $this->input->post('txt_net_due_in');
            $TermDesc   	 	= $this->input->post('txt_description');
            $dataSend   = [];
            $dataSend['data_term']   = [];	
            if(!regex_name($TermDesc)) {
                // set nilai untuk ke form
                throw new Exception(sg_invalid_name($this->name));
            }
            else {

                // cek apakah kode sudah digunakan
                if ($this->m_terms->check_code_exist($TermDesc) == TRUE) {
                        // set nilai untuk ke form
                    throw new Exception(msg_already_used($this->name));
                }else {	
                    // inisialisasi array
                    $dataSend['data_term'] = array (
                        'PaidWithin'				=> $txt_paid_within,
                        'DiscountPercent'			=> $txt_discount_percent,
                        'NetDueIn' 					=> $txt_net_due_in,
                        'TermDesc' 					=> $TermDesc,
                        'LastUser' 					=> $this->session->userdata("username")
                    );
                    // insert database
//                    $action = $this->m_terms->insert($data);                        
                    $rstDataSend = $this->m_terms->datasend_term_insert($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);
        }
    }
    public function insert_LAMA() {
            $txt_paid_within		= $this->input->post('txt_paid_within');
            $txt_discount_percent 	= $this->input->post('txt_discount_percent');
            $txt_net_due_in 		= $this->input->post('txt_net_due_in');
            $TermDesc   	 	    = $this->input->post('txt_description');	
            if(!regex_name($TermDesc)) {
                    // set nilai untuk ke form
                    $result['status'] = false;
                    $result['message'] = msg_invalid_name($this->name);
            }
            else {

                    // cek apakah kode sudah digunakan
                    if ($this->m_terms->check_code_exist($TermDesc) == TRUE) {
                            // set nilai untuk ke form
                            $result['status'] = false;
                            $result['message'] = msg_already_used($this->name);
                    }
                    else {	
                            // inisialisasi array
                            $data = array (
                                    'PaidWithin'				=> $txt_paid_within,
                                    'DiscountPercent'			=> $txt_discount_percent,
                                    'NetDueIn' 					=> $txt_net_due_in,
                                    'TermDesc' 					=> $TermDesc,
                                    'LastUser' 					=> $this->session->userdata("username")
                            );

                            // insert database
                            $action = $this->m_terms->insert($data);
                            if($action == 1) {
                                    $result['status'] = true;
                                    $result['message'] = 'Data  berhasil disimpan';
                            }
                            else {
                                    $result['status'] = false;
                                    $result['message'] = 'Data gagal disimpan,silahkan cek kembali data Anda';
                            }
                    }
            }
            echo json_encode($result);
    }
	
	//Functiom Edit Form
	public function edit(){
		if ($this->session->userdata("login") == TRUE) { 
            $group_menu             = $this->session->userdata('group_menu');
			/*
				untuk di index/list = get_menu_by_group, 
				untuk di add = get_menu_by_group_add
				untuk di edit = get_menu_by_group_edit
			*/
            $arr_group_id           = $this->m_menu->get_menu_by_group_edit($group_menu, $this->menu_id);
			//--------------------------end-----------------------------------------------------------
            if(!empty($arr_group_id)){
				$data['available_menu'] = array_unique($arr_group_id);
				if(!in_array($this->menu_id, $data['available_menu'])) { 
					redirect(base_url() . 'error', 'refresh');	
				}
				else{
					/*
						modul untuk akses add masing masing modul berebda 
						jangan lupa copy $data nya (jika dia bener langsung ke add)
					*/
					//untuk ambil data dari session dan hak akses
					$group_menu 		= $this->session->userdata('group_menu');
					
					// inisialisasi additional parameter
					$id = $this->uri->segment(4);
					$data['detail'] = $this->m_terms->get_detail($id);
					
					 // set data
					$data['menu_id'] =  $this->menu_id;
					//$data['menu_model'] = $this->menu_model;
					
					// get link from tbl_menu 
					$data['menu_link'] = $this->m_menu->get_menulink($this->menu_id);
					
					// get label from tbl_menu 
					$data['menu_label'] = $this->m_menu->get_menulabel($this->menu_id);
					
					//panggil model hak akses untuk autoriti
					$data['group_user'] = $this->m_menu->group_user_autority($this->menu_id, $group_menu);
					
					// set for view
					$this->load->view('v_header');
					$this->load->view('others/terms/v_edit_terms',$data);	
					$this->load->view('v_footer');		
				}
			}
			else{
				redirect(base_url() . 'error', 'refresh');
			}
        }
        else {
            redirect(base_url() . 'login', 'refresh');
        }
	}
	
	// Function Update
    public function update() {
        try{
            $id                     = $this->input->post('id');
            $txt_paid_within        = $this->input->post('txt_paid_within');
            $txt_discount_percent   = $this->input->post('txt_discount_percent');
            $txt_net_due_in         = $this->input->post('txt_net_due_in');
            $txt_description 	    = $this->input->post('txt_description');
			
            $dataSend   = [];
            $dataSend['data_term']   = [];
            $dataSend['idcode']      = $id;
			
			if(!regex_name($txt_description)) {
                // set nilai untuk ke form
                throw new Exception(sg_invalid_name($this->name));
            }
            else {
				// cek apakah kode sudah digunakan
				if ($this->m_terms->check_code_exist($txt_description) == TRUE) {
						throw new Exception(msg_already_used($this->name));
				}
				else {
					// inisialisasi array
					$dataSend['data_term'] = array (
						'PaidWithin'        => $txt_paid_within,
						'DiscountPercent'   => $txt_discount_percent,
						'NetDueIn'          => $txt_net_due_in,
						'TermDesc'          => $txt_description,
						'LastUser'          => $this->session->userdata("username")
					);
					// update database
					//            $action = $this->m_terms->update($data,$id);            
					$rstDataSend = $this->m_terms->datasend_term_update($dataSend);
					if($rstDataSend){
						$result['status'] 	= true;
						$result['message']	= get_notification('update', 1);                
					}else{
						$result['status'] 	= false;
						$result['message']	= get_notification('update', 0);
					}
					echo json_encode($result);
				}
			}
            
        }catch (Exception $e){            
            $result['status']   = false;
            $result['message']  = $e->getMessage();
            echo json_encode($result);
        }
    }
	public function update_LAMA() {
	    $id		= $this->input->post('id');
	    $txt_paid_within		= $this->input->post('txt_paid_within');
		$txt_discount_percent 	= $this->input->post('txt_discount_percent');
		$txt_net_due_in 		= $this->input->post('txt_net_due_in');
		$txt_description 	    = $this->input->post('txt_description');
				
		// inisialisasi array
		$data = array (
			'PaidWithin'				=> $txt_paid_within,
			'DiscountPercent'			=> $txt_discount_percent,
			'NetDueIn' 					=> $txt_net_due_in,
			'TermDesc' 					=> $txt_description,
			'LastUser' 					=> $this->session->userdata("username")
		);
		
		// update database
		$action = $this->m_terms->update($data,$id);
		if($action == 1) {
			$result['status'] = true;
			$result['message'] = 'Data berhasil disimpan';
		}
		else {
			$result['status'] = false;
			$result['message'] = 'Data gagal disimpan,silahkan cek kembali data Anda';
		}	
		
		echo json_encode($result);
	}
	// Function Delete
    public function delete() {
        try{
            $id = $this->input->post('id');	
            $dataSend   = [];
            $dataSend['data_terms']   = [];	
            $dataSend['data_terms'] = array (
                'TransStatus'   => 3,
                'LastUser'      => $this->session->userdata("username")
            );
//            $action = $this->m_terms->update($data, $id);
            $rstDataSend = $this->m_terms->datasend_term_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);
        }	
    }        
	public function delete_LAMA() {
	    $id = $this->input->post('id');		
		$data = array (
                        'TransStatus'			=> 3,
                        'LastUser' 				=> $this->session->userdata("username")
                );
		
		$action = $this->m_terms->update($data, $id);
		if($action == 1) {
			$result['status']  = true;
			$result['message'] = 'Data berhasil suspend';
		}
		else {
			$result['status']  = false;
			$result['message'] = 'Data gagal dihapus';
		}	
		echo json_encode($result);	
		/* // inisialisasi additional parameter
        $id = $this->input->post('id');
		
		$action = $this->m_terms->delete($id);
		if($action == 1) {
			$result['status'] = true;
			$result['message'] = 'Data berhasil dihapus';
		}
		else {
			$result['status'] = false;
			$result['message'] = 'Data gagal dihapus';
		}
		echo json_encode($result);	 */	
	}
	
    public function suspend_data() {
        try{
            $id = $this->input->post('id');	
            $dataSend   = [];
            $dataSend['data_terms']   = [];
            $dataSend['idcode']   = $id;	
            $dataSend['data_terms'] = array (
                'TransStatus'   => 3,
                'LastUser'      => $this->session->userdata("username")
            );
//            $action = $this->m_terms->update($data, $id);
            $result['dataSend'] 	= $dataSend;
            $rstDataSend = $this->m_terms->datasend_term_suspend($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);
        }	
    }  
	
}
	