<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*	
 *	Author 	: Wahyu
 *	Tanggal : 02-09-2014	
 *	Revisi 	: 
 */
 
class Warehouse_type extends CI_Controller {
    // inisialisasi manual
	public $menu_id = 263;
	public $code = "Warehouse Type Code";
	public $name = "Warehouse Type Name";
	
    public function __construct() {
        parent::__construct();
		$this->load->helper('regex');
		$this->load->helper('message');
		$this->load->helper('status');
		
		$this->load->model('m_menu');
		$this->load->model('m_warehouse_type');	
	}
	
	//Function Index
    public function index() 	{	
		// pengecekan otoritas
		$group_menu = $this->session->userdata('group_menu');
		$arr_group_id = $this->m_menu->get_menu_by_group($group_menu);
		$data['available_menu'] = array_unique($arr_group_id);
		
		if(!in_array($this->menu_id, $data['available_menu'])) { 
			redirect(base_url() . 'error', 'refresh');	
		}
		
		// 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);
		
		$this->load->view('v_header');
		$this->load->view('warehouse_type/v_warehouse_type', $data);	
		$this->load->view('v_footer');	
	}
	
	//Functiom Add Form
	public function add(){
		// pengecekan otoritas
		$group_menu = $this->session->userdata('group_menu');
		$arr_group_id = $this->m_menu->get_menu_by_group($group_menu);
		$data['available_menu'] = array_unique($arr_group_id);
		
		if(!in_array($this->menu_id, $data['available_menu'])) { 
			redirect(base_url() . 'error', 'refresh');	
		}
		
		// 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);
		
		$this->load->view('v_header');
		$this->load->view('warehouse_type/v_new_warehouse_type', $data);	
		$this->load->view('v_footer');	
	}
	
	// Function Insert
	public function insert() {
		$txt_name = trim($this->input->post('txt_name'));
		$txt_desc = $this->input->post('txt_desc');
		
		// cek suspended
		$chk_suspended = $this->input->post('chk_suspended');
		if($chk_suspended == 1) { $chk_suspended = 3; } else { $chk_suspended = 0; }
		
		if(!regex_name($txt_name)) {
			// set nilai untuk ke form
			$result['status'] = false;
			$result['message'] = msg_invalid_name($this->name);
		}
		else {
			// cek apakah kode sudah digunakan
			if ($this->m_warehouse_type->check_code_exist($txt_name) == TRUE) {
				// set nilai untuk ke form
				$result['status'] = false;
				$result['message'] = msg_already_used($this->name);
			}
			else {
				// inisialisasi array
				$data = array (
					'WHTypeName' 	=> strtoupper($txt_name),
					'WHTypeDesc' 	=> $txt_desc,
					'TransStatus'	=> $chk_suspended,
					'LastUser' 		=> $this->session->userdata("username")
				);
				
				// insert database
				$action = $this->m_warehouse_type->insert($data);
				if($action == 1) {
					$result['status'] = true;
					$result['message'] = get_notification('insert', 0);
				}
				else {
					$result['status'] = false;
					$result['message'] = get_notification('insert', 1);
				}						
			}
		}
		
		echo json_encode($result);
	}
	
	// Function Edit
	public function edit() {
		// pengecekan otoritas
		$group_menu = $this->session->userdata('group_menu');
		$arr_group_id = $this->m_menu->get_menu_by_group($group_menu);
		$data['available_menu'] = array_unique($arr_group_id);
		
		if(!in_array($this->menu_id, $data['available_menu'])) { 
			redirect(base_url() . 'error', 'refresh');	
		}
		
		// inisialisasi additional parameter
        $id = $this->uri->segment(4);
		$data['detail'] = $this->m_warehouse_type->get_detail($id);
				
		 // set data
		$data['menu_id'] =  $this->menu_id;
		
		// get link from tbl_menu 
		$data['menu_link'] = $this->m_menu->get_menulink($this->menu_id);
		$data['menu_label'] = $this->m_menu->get_menulabel($this->menu_id);
		
		$this->load->view('v_header');
		$this->load->view('warehouse_type/v_edit_warehouse_type', $data);
		$this->load->view('v_footer');
	}
	
	// Function Update
	public function update() {
		$id = $this->input->post('id');
		$txt_name = trim($this->input->post('txt_name'));
		$txt_desc = $this->input->post('txt_desc');
		
		// cek suspended
		$chk_suspended = $this->input->post('chk_suspended');
		if($chk_suspended == 1) { $chk_suspended = 3; } else { $chk_suspended = 0; }
		
		if(!regex_name($txt_name)) {
			// set nilai untuk ke form
			$result['status'] = false;
			$result['message'] = msg_invalid_name();
		}
		else {
			// cek apakah kode sudah digunakan
			if ($this->m_warehouse_type->check_code_exist($txt_name) == TRUE) {
				// set nilai untuk ke form
				$result['status'] = false;
				$result['message'] = msg_already_used($this->name);
			}
			else {
				// inisialisasi array
				$data = array (
					'WHTypeName' 	=> strtoupper($txt_name),
					'WHTypeDesc' 	=> $txt_desc,
					'TransStatus'	=> $chk_suspended,
					'LastUser' 		=> $this->session->userdata("username")
				);
				
				// insert database
				$action = $this->m_warehouse_type->update($data, $id);
				if($action == 1) {
					$result['status'] = true;
					$result['message'] = get_notification('update', 0);
				}
				else {
					$result['status'] = false;
					$result['message'] = get_notification('update', 1);
				}						
			}
		}
		
		echo json_encode($result);
	}
	
	// Function Delete
	public function delete() {
		// pengecekan otoritas
		$group_menu = $this->session->userdata('group_menu');
		$arr_group_id = $this->m_menu->get_menu_by_group($group_menu);
		$data['available_menu'] = array_unique($arr_group_id);
		
		if(!in_array($this->menu_id, $data['available_menu'])) { 
			redirect(base_url() . 'error', 'refresh');	
		}
		
		// inisialisasi additional parameter
        $id = $this->input->post('id');
		
		$action = $this->m_warehouse_type->suspended($id);
		if($action == 1) {
			$result['status'] = true;
			$result['message'] = get_notification('delete', 0);
		}
		else {
			$result['status'] = false;
			$result['message'] = get_notification('delete', 1);
		}
		echo json_encode($result);		
	}
	
	//Function preview data
	public function to_preview(){
	     // set data
		$data['menu_id'] =  $this->menu_id;
		$data['menu_label'] =  $this->m_menu->get_menulabel($this->menu_id);
		$data['menu_link'] = $this->m_menu->get_menulink($this->menu_id);
		
		$data['list'] = $this->m_warehouse_type->get_all();
		
		$this->load->view('v_header');
		$this->load->view('warehouse_type/v_preview_warehouse_type', $data);	
		$this->load->view('v_footer');	
	}
	
	//Function To Print
	public function to_print(){	
		 // set data
		$data['menu_id'] =  $this->menu_id;
		$data['menu_label'] =  $this->m_menu->get_menulabel($this->menu_id);
		
		$data['list'] = $this->m_warehouse_type->get_all();
		
		$this->load->view('v_header');
		$this->load->view('warehouse_type/v_print_warehouse_type', $data);	
		$this->load->view('v_footer');	
	}
	
	public function ajax(){
		echo json_encode($this->m_warehouse_type->get_all());
	}
	
	/* public function ajax(){
		$result = $this->m_warehouse_type->get_all();
		
		foreach($result as $row) {
			$TransStatus = get_status_master($row->TransStatus);
			
			$hasil[] = array(
				'WHTypeID' => $row->WHTypeID,
				'WHTypeName' => $row->WHTypeName,
				'WHTypeDesc' => $row->WHTypeDesc,
				'TransStatus' => $TransStatus
			); 
		}
		
		echo json_encode($hasil);
	}	 */
}
	