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

class Purchase_pricing extends CI_Model{
	
	//Gather form data
	public function getFormData(){
		//Gethering item list belongs to the company
		$this->db
			->select('i.ID,i.itemCode,i.name as itemName,i.unitOfMeasureID,mc.name as Unit')
			->from('item as i')
			->join('master_code as mc','mc.ID = i.unitOfMeasureID','left')
			->where(array('companyID'=>element('compID', $this->session->userdata('logged_in'))));
		$query = $this->db->get();
		$data['item'] = $query->result();
				
		//Gethering supplier list
		$this->db
			->select('s.ID,s.supplierName,s.supplierCode,c.currencyCode')
			->from('supplier as s')
			->join('currency as c','c.ID=s.currencyID','left');
		$query = $this->db->get();
		$data['supplier'] = $query->result();
		
		//Gethering item list
		$this->db
			->select('i.ID,i.name,i.itemCode,s.supplierName,mc.name as UOM,i.supplierPrice')
			->from('item as i')
			->join('supplier as s','s.ID=i.supplierID','left')
			->join('master_code as mc','mc.ID = i.unitOfMeasureID','left')
			->where(array('i.companyID'=>element('compID', $this->session->userdata('logged_in')),'supplierPrice !='=>0));
			
		$query = $this->db->get();
		$data['datatbls'] = $query->result();
		return $data;
	}
	
	//Insert item purchase price
	public function insert($id,$data){
		
		$query = $this->db->where('ID',$id)
					->update('item', $data);
		
		if($query){
			return TRUE;
		}else{
			return FALSE;
		}	
	}
	
	//Update item price
	public function update($id,$data){
		$this->db->where('ID', $id);
		$sql = $this->db->update('comp_sales_price', $data);
		
		if($sql == 1){
			return $this->db->insert_id();
		} else {
			$last_query = $this->db->last_query();
			return false;
		}
	}
	
	//Get all sales prices
	public function allsalesprice(){
		$this->db->select('tbl_location.*');
		$this->db->select('_ref_state.fldname');
		$this->db->select('_ref_country.fldcountry');
		$this->db->from('tbl_location');
		$this->db->join('_ref_state', '_ref_state.fldid = tbl_location.state');
		$this->db->join('_ref_country', '_ref_country.fldid = tbl_location.country');
		$query = $this->db->get();
		if($query->num_rows() >= 1){
			return $query->result();
		}else{
			return false;
		}
	}
	
	//Get individual price
	public function getPrice($id){
		$this->db
			->select('ID,supplierID,supplierPrice')
			->from('item')
			->where(array('ID'=>$id));
			
		$query = $this->db->get();
		if($query->num_rows() >= 1){
			return $query->result();
		}else{
			return false;
		}
	}
	
	//Delete Sales prices
	public function delete($id){
		$this->db->where('ID', $id);
		$query = $this->db->delete('comp_sales_price'); 
		if($query === TRUE){
			return TRUE;
		} else {
			return FALSE;
		}	
	}
}