<?php
/*	
 *	Author 	: Ladur
 *	Tanggal : 01-10-2014	
 *	Revisi 	: 
 */
 
class M_warehouse_type extends CI_Model {
    
	// deklarasi nama table
	public $table = "am_warehousetype";
	
	// Function View	
	
	// Function get
	function get_all(){
		$WHTypeName	    = $this->input->post('id') ? $this->input->post('id') : '';
		$WHTypeDesc		= $this->input->post('desc') ? $this->input->post('desc') : '';
		$TransStatus 	= $this->input->post('status') ? $this->input->post('status') : '';
		
		$this->db->select("(case when am_warehousetype.TransStatus=1 then 'On Progress'
							when am_warehousetype.TransStatus=2 then 'Finished'
							when am_warehousetype.TransStatus=3 then 'Susppend'
							when am_warehousetype.TransStatus=4 then 'Managerial App'
							when am_warehousetype.TransStatus=5 then 'Direksi App'
							when am_warehousetype.TransStatus=6 then 'Mgr & Dir App'
							when am_warehousetype.TransStatus=7 then 'Approval Limit'
							else 'Active'
							end) as Status,	
			am_warehousetype.WHTypeID, am_warehousetype.WHTypeName, am_warehousetype.WHTypeDesc");
			
		$this->db->from($this->table);
		
		if($WHTypeName != "") {
			$this->db->like('WHTypeName',$WHTypeName);
		}
		
		if($WHTypeDesc != "") {
			$this->db->like('WHTypeDesc',$WHTypeDesc);
		}
		
		if($TransStatus!=' '){
			$this->db->where_in('TransStatus',$TransStatus);
		}
		
		$this->db->order_by('WHTypeName','ASC');
		return $this->db->get()->result();
    }
	
	/* function get_all(){
        $query = $this->db->get($this->table);
        return $query->result();
    } */
	
	function get_detail($id){
        $query = $this->db->get_where($this->table, array('WHTypeID' => $id));
        return $query->result();
    }
	
	// Function Join
	
	// Function Count	
	function check_code_exist($id) {
		$query = $this->db->get_where($this->table, array('WHTypeName' => $id), 1, 0);
		
		if ($query->num_rows() > 0) {
			return TRUE;
		}
		else {
			return FALSE;
		}	
	}
	
	// Function Insert
	function insert($data) {
		$this->db->trans_begin();
		$this->db->insert($this->table, $data);
		
		if ($this->db->trans_status() === FALSE) {
			$this->db->trans_rollback();
			return 0;
		}
		else {
			$this->db->trans_commit();
			return 1;
		}
	}

	// Function Update
	function update($data, $id) {
		$this->db->trans_begin();
        $this->db->update($this->table, $data, array('WHTypeID' => $id));
		
		if ($this->db->trans_status() === FALSE) {
			$this->db->trans_rollback();
			return 0;
		}
		else {
			$this->db->trans_commit();
			return 1;
		}
	}
	
	// Function Delete	
	function delete($id) {
		$this->db->trans_begin();
        $this->db->delete($this->table, array('WHTypeID' => $id));
		
		if ($this->db->trans_status() === FALSE) {
			$this->db->trans_rollback();
			return 0;
		}
		else {
			$this->db->trans_commit();
			return 1;
		}
	}
	
	function suspended($id) {
		$this->db->trans_begin();
        $this->db->set('TransStatus', 3);
		$this->db->where('WHTypeID', $id);
		$this->db->update($this->table);
		
		if ($this->db->trans_status() === FALSE) {
			$this->db->trans_rollback();
			return 0;
		}
		else {
			$this->db->trans_commit();
			return 1;
		}
	}
}