<?php
class M_Warehouses extends CI_Model {
    
	// deklarasi nama table
	public $table="am_warehouse";
	
	//custom data
	function get_country(){
		$this->db->select('*');
		$this->db->from('am_country');
		$this->db->order_by('CountryCode', 'ASC');
		$query = $this->db->get();
		
		$result=$query->result();
		return json_encode($result);
	}
	
	function get_state(){
		$this->db->select('*');
		$this->db->from('am_state');
		$this->db->order_by('StateCode', 'ASC');
		$query = $this->db->get();
		
		$result=$query->result();
		return json_encode($result);
	}
	
	function get_city(){
		$this->db->select('*');
		$this->db->from('am_city');
		$this->db->order_by('CityCode', 'ASC');
		$query = $this->db->get();
		
		$result=$query->result();
		return json_encode($result);
	}
	
	function get_employee(){
		$this->db->select('*');
		$this->db->from('am_employee');
		$this->db->order_by('EmployeeCode', 'ASC');
		$query = $this->db->get();
		
		$result=$query->result();
		return json_encode($result);
	}
	
	// Function get
	function get_all(){
	    $warehouse_name	= ($this->input->post('warehouse_name')) ? $this->input->post('warehouse_name') : '';
		$warehouse_desc	= ($this->input->post('warehouse_desc')) ? $this->input->post('warehouse_desc') : '';
		$TransStatus	= ($this->input->post('TransStatus')) ? $this->input->post('TransStatus') : '';
		
		$this->db->select("am_warehouse.SuspendStatus, am_warehouse.WarehouseID, am_warehouse.WarehouseName, am_warehouse.WarehouseDesc, am_warehouse.ContactPerson, am_employee.EmployeeName");
		$this->db->from($this->table);
		$this->db->join('am_employee','am_warehouse.ContactPerson=am_employee.EmployeeCode','INNER');
		$this->db->like('WarehouseName', $warehouse_name);
		$this->db->like('WarehouseDesc', $warehouse_desc);	
		
		if($TransStatus!=''){
			$this->db->where_in('am_warehouse.SuspendStatus',$TransStatus);
		}
		
		$query = $this->db->get();
		return $query->result();
    }
	
	function get_by_id($id) {
		$query = $this->db->get_where($this->table, array('WarehouseID' => $id));
		if($query->num_rows()>0){
			return $query->row();
		}
		else{
			return false;
		}    
	}
	
	public 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($id, $data){
		$this->db->where('WarehouseID', $id);
		return $this->db->update($this->table, $data);
	}
	
	function get_preview($id){
		$this->db->select("am_warehouse.SuspendStatus, am_warehouse.WarehouseID, am_warehouse.WarehouseName, am_warehouse.WarehouseDesc, am_warehouse.ContactPerson, am_employee.EmployeeName");
		$this->db->from($this->table);
		$this->db->join('am_employee','am_warehouse.ContactPerson=am_employee.EmployeeCode','INNER');
		
		if(!empty($id)){
			$this->db->where('WarehouseID', $id);
		}
		
		$query = $this->db->get();
		return $query->result();
	}

}