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

/*	
 *	Author 	: Tagor
 *	Tanggal : 05-08-2014	
 *	Revisi 	: 
 */ 

class Warehouses extends CI_Controller {
	// inisialisasi manual
	public $menu_id = 174;
	public $menu_model = 'm_warehouses';
	
	// inisialisasi otomatis
	public $add = "add";
	public $edit = "edit";
	public $delete = "delete";
	public $to_preview = "to_preview";
	public $to_pdf = "to_pdf";
	public $to_excel = "to_excel";
	
	public function __construct() {
        parent::__construct();
		$this->load->model('m_warehouses');
        $this->load->model('m_menu');			
        $this->load->helper('regex');
	    $this->load->helper('message');
	    $this->load->helper('status');			
	}
	
	// Function Index	
	public function index() {
		// pengecekan otoritas
		//if($this->session->userdata("checking") == "") {
			//redirect(base_url() . 'custom/cek/'.$this->menu_id.'/'.$this->menu_model, 'refresh');	
		//}
		
		// reset session 
		$this->session->unset_userdata('checking');
		
		// 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);
		$data['menu_label'] = $this->m_menu->get_menulabel($this->menu_id);
		
		// set for view
		$this->load->view('v_header');
		$this->load->view('warehouses/v_warehouses', $data);
		$this->load->view('v_footer');
	}

    public function get_country(){
	    $list_country =  $this->m_warehouses->get_country();
	    echo"$list_country";
    }
    
    public function get_state(){
		$list_state =  $this->m_warehouses->get_state();
	    echo"$list_state";
    }	

    public function get_city(){
		$list_city =  $this->m_warehouses->get_city();
	    echo"$list_city";
    }
	
	public function get_employee(){
		$list_employee =  $this->m_warehouses->get_employee();
	    echo"$list_employee";
    }

    public function ajax(){
		$result = $this->m_warehouses->get_all();
		
		foreach($result as $row) {
			$TransStatus = get_status_master($row->SuspendStatus);
			
			$hasil[] = array(
			    'WarehouseID' => $row->WarehouseID,
				'WarehouseName' => $row->WarehouseName,
				'WarehouseDesc' => $row->WarehouseDesc,
				'ContactPerson' => $row->EmployeeName,
				'SuspendStatus' => $TransStatus
			); 
		}
		
		if(!empty($hasil)){
			echo json_encode($hasil);
		}
		else{
			$notFound = array();
			echo json_encode($notFound);
		}
	}	
	
	// Function Add
	public function add() {
		// pengecekan otoritas
		//if($this->session->userdata("checking") == "") {
			//redirect(base_url() . 'custom/cek/'.$this->menu_id.'/'.$this->menu_model.'/'.$this->add, 'refresh');	
		//}
		
		// reset session 
		$this->session->unset_userdata('checking');
		
		// 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);
		
		// set for view
		$this->load->view('v_header');
		$this->load->view('warehouses/v_new_warehouses', $data);
		$this->load->view('v_footer');
	}
	
	// Function Insert
	public function insert() {
		$txt_warehousename = $this->input->post('txt_warehousename');
		$chk_suspend = $this->input->post('chk_suspend');
		$txt_description = $this->input->post('txt_description');
		$txt_address = $this->input->post('txt_address');
		$country = $this->input->post('cmb_country');
		$state = $this->input->post('cmb_state');
		$city = $this->input->post('cmb_city');
		$txt_contact_person = $this->input->post('txt_contact_person');
		
		$data = array (
			'WarehouseName' => $txt_warehousename,
			'SuspendStatus' => $chk_suspend,
			'WarehouseDesc' => $txt_description,
			'WarehouseAddress' => $txt_address,
			'WarehouseCity' => $city,
			'WarehouseState' => $state,
			'WarehouseCountry' => $country,
			'ContactPerson' => $txt_contact_person
		);
		
		// insert database
		$action = $this->m_warehouses->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() {
		// inisialisasi additional parameter
        $id = $this->uri->segment(4);
		
		// pengecekan otoritas
		//if($this->session->userdata("checking") == "") {
			//redirect(base_url() . 'custom/cek/'.$this->menu_id.'/'.$this->menu_model.'/'.$this->edit.'/'.$id, 'refresh');	
		//}
		
		// reset session 
		$this->session->unset_userdata('checking');
		
		// set data
		$data['detail'] =  $this->m_warehouses->get_by_id($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);
		
		// set for view
		$this->load->view('v_header');
		$this->load->view('warehouses/v_edit_warehouses', $data);
		$this->load->view('v_footer');
	}
	
	// Function Update
	public function update() {
		$id = $this->input->post('WarehouseID');
		
		$txt_warehousename = $this->input->post('txt_warehousename');
		$chk_suspend = $this->input->post('chk_suspend');
		$txt_description = $this->input->post('txt_description');
		$txt_address = $this->input->post('txt_address');
		$country = $this->input->post('cmb_country');
		$state = $this->input->post('cmb_state');
		$city = $this->input->post('cmb_city');
		$txt_contact_person = $this->input->post('txt_contact_person');
		
		$data = array (
			'WarehouseName' => $txt_warehousename,
			'SuspendStatus' => $chk_suspend,
			'WarehouseDesc' => $txt_description,
			'WarehouseAddress' => $txt_address,
			'WarehouseCity' => $city,
			'WarehouseState' => $state,
			'WarehouseCountry' => $country,
			'ContactPerson' => $txt_contact_person
		);
		
		if($this->m_warehouses->update($id, $data))
			echo json_encode(array('status'=>true, 'message'=> get_notification('update', 0)));
		else
			echo json_encode(array('status'=>false, 'message'=> get_notification('update', 1)));
	}
	
	// Function Delete
	public function delete() {
		// inisialisasi additional parameter
      $id = $this->input->post('id');
		
		$data = array (
			'SuspendStatus' => 3,
			'LastUser' => $this->session->userdata("username")
		);		
		
		if($this->m_warehouses->update($id, $data))
			echo json_encode(array('status'=>true, 'message'=>'Warehouse berhasil disuspend'));
		else
			echo json_encode(array('status'=>false));
	}
	
	/*Added by Hasan on 12/02/2015*/
	public function suspend(){
		$id = $this->input->post('id');
		$type = $this->input->post('type');
		
		if($type=='default'){
			$TransStatus = 3;
		}
		else{
			$TransStatus = 10;
		}
		
		$data = array (
			'SuspendStatus' => $TransStatus,
			'LastUser' 		=> $this->session->userdata("username")
		);		
		
		if($this->m_warehouses->update($id, $data)){
			echo json_encode(array('status'=>true, 'message'=>'data have been suspended'));
		}
		else{
			echo json_encode(array('status'=>false, 'message'=>'data cannot be suspended'));
		}
	}
	/*End of added by Hasan on 12/02/2015*/
	
	//Function preview data
	public function to_preview(){
	    $id = $this->uri->segment(4);
	    // set data
		$data['list'] = $this->m_warehouses->get_preview($id);
		
		$this->load->view('v_header');
		$this->load->view('warehouses/v_preview_warehouse', $data);	
		$this->load->view('v_footer');	
	}
	
	//Function To Print
	public function to_print(){	
		 // set data
		$data['list'] = $this->m_warehouses->get_preview();
		
		$this->load->view('v_header');
		$this->load->view('warehouses/v_print_warehouse', $data);	
		$this->load->view('v_footer');	
	}
}