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

class ItemSetup_model extends CI_Model{
	
	public function get_all(){
		$this->db->select('a.ID');
		$this->db->select('a.itemCode');
        $this->db->select('a.salesPrice');
		$this->db->select('a.name');
		$this->db->select('a.description');
		$this->db->select('a.quantity');
		$this->db->select('a.reorderLevel');
                $this->db->select('a.supplierPrice');
                
                $this->db->select('a.outputTaxID');
                $this->db->select('e.code As outputTaxCode');
                $this->db->select('e.taxPercentage As outputTaxPercentage');
                
                $this->db->select('a.inputTaxID');
                $this->db->select('f.code As inputTaxCode');
                $this->db->select('f.taxPercentage As inputTaxPercentage');
                 
		$this->db->select('a.itemStatusID');
		$this->db->select('b.name as status');
		$this->db->select('a.itemCategoryID');
		$this->db->select('c.name as category');
		$this->db->select('a.itemTypeID');
		$this->db->select('d.name as type');
		$this->db->where('a.itemStatusID = b.id');
                
                $this->db->where('a.ID = e.ID');
                $this->db->where('a.ID = f.ID');
                
		$this->db->where('a.itemCategoryID = c.id');
		$this->db->where('a.itemTypeID = d.id');
		$this->db->where ('a.companyID',element('compID', $this->session->userdata('logged_in')));
		$this->db->order_by('a.name');
		$query = $this->db->get('item a, master_code b, master_code c, master_code d, tax_master e, tax_master f');
		//echo $this->db->last_query();
		if($query->num_rows() >= 1){
			return $query->result();
		}else{
			return FALSE;
		}
	}
	
        public function getItemListByLocation($idLocation=0){
            $this->db->select('a.ID');
		//$this->db->select('a.itemCode');
		$this->db->select('concat("[",a.itemCode,"] ",a.name) as item', false);
		//$this->db->select(concat(left(a.itemCodede,4),"-',mid(b.acctCode,4,3),"-",right(b.acctCode,2),"",b.acctName);;
		$this->db->select('a.itemCode');
		$this->db->select('a.name');
		$this->db->select('a.reorderLevel');
		
                $this->db->select('sum(f.onHandQty) as quantity');
                 $this->db->select('a.salesPrice');
		$this->db->select('a.description');
		$this->db->select('a.itemStatusID');
		$this->db->select('b.name as status');
		$this->db->select('a.itemCategoryID');
		$this->db->select('c.name as category');
		$this->db->select('a.itemTypeID');
		$this->db->select('d.name as type');
		$this->db->where('a.itemStatusID = b.id');
		$this->db->where('a.itemCategoryID = c.id');
		$this->db->where('a.itemTypeID = d.id');
		$this->db->where('a.ID = f.itemid');
		
		$this->db->group_by('ID','itemCode','name','description','itemStatusID','status','itemCategoryID','category','itemTypeID','type','itemid');
//                $this->db->where('a.locationID',$idLocation);
                
                $this->db->select('a.outputTaxID');
                $this->db->select('e.code As outputTaxCode');
                $this->db->select('e.taxPercentage As outputTaxPercentage');
                
                 $this->db->where('a.ID = e.ID');
		$this->db->where ('a.companyID',element('compID', $this->session->userdata('logged_in')));
		$this->db->order_by('a.name');
		$query = $this->db->get('item a, master_code b, master_code c, master_code d, tax_master e, item_details f');
		//echo $this->db->last_query();
		if($query->num_rows() >= 1){
			return $query->result();
		}else{
			return FALSE;
		}
        }

        public function insert($newData){
		$sql = $this->db->insert_string('item', $newData);
		$query = $this->db->query($sql);
		if($query === TRUE){
			return TRUE;
		} else {
			$last_query = $this->db->last_query();
			return $last_query;
		}
	}
	
	public function itemdelete($id){
		$this->db->where('ID', $id);
		$query = $this->db->delete('item'); 
		if($query === TRUE){
			return TRUE;
		} else {
			return FALSE;
		}
	}
	public function get_byID($id){
		$query = $this->db->get_where('item', array('ID'=>$id), 1);		
		if($query->num_rows() == 1){
			return $query->result();
		}else{
			return false;
		}

	}
	
	public function get_byCode($code){
		$sql='Select * FROM item WHERE itemCode = "'.$code.'"';
		$query = $this->db->query($sql);

		if($query->num_rows() >= 1){
			return $query->result();
		}else{
			return FALSE;
		}

	}
	
	public function update_byID($id, $data){
		$this->db->where('ID', $id);
		$query = $this->db->update('item', $data); 
		if($query === TRUE){
			return TRUE;
		} else {
			return FALSE;
		}
	}
}