<?php
class classStoreItem
{
	private $storeItemID=NULL;
	private $storeItemName=NULL;
	private $storeItemCategory=NULL;
	private $storeItemReorderQty=NULL;
	private $storeItemQtyInStock=NULL;
	private $active=NULL;
	
	private $storeItemAdjustedQty=NULL;
	private $storeItemAdjustmentQty=NULL;
	
	private $manufacturerID=NULL;
	private $organizationID=NULL;
	private $locationID=NULL;
	private $userID=NULL;
	
	################################# SET FUNCTIONS ############################################
	
	function setStoreItemID($storeItemID)
	{
		if($storeItemID!='')
		{
			$this->storeItemID = $storeItemID;
		}
		else
		{
			throw new MyException(119100);
		}
	}
	function setStoreItemName($storeItemName)
	{
		if($storeItemName!='')
		{
			$this->storeItemName = $storeItemName;
		}
		else
		{
			throw new MyException(119101);
		}
	}
	function setStoreItemCategory($storeItemCategory)
	{
		if($storeItemCategory!='')
		{
			$this->storeItemCategory = $storeItemCategory;
		}
		else
		{
			throw new MyException(119102);
		}
	}
	function setStoreItemReorderQty($storeItemReorderQty)
	{
		if($storeItemReorderQty!='')
		{
			$this->storeItemReorderQty = $storeItemReorderQty;
		}
		else
		{
			throw new MyException(119103);
		}
	}
	function setStoreItemQtyInStock($storeItemQtyInStock)
	{
		if($storeItemQtyInStock!='')
		{
			$this->storeItemQtyInStock = $storeItemQtyInStock;
		}
	}
	function setActive($active)
	{
		$this->active = $active;
	}
	
	function setStoreItemAdjustedQty($storeItemAdjustedQty)
	{
		if($storeItemAdjustedQty!='')
		{
			$this->storeItemAdjustedQty = $storeItemAdjustedQty;
		}
		else
		{
			throw new MyException(119109);
		}
	}
	function setStoreItemAdjustmentQty($storeItemAdjustmentQty)
	{
		if($storeItemAdjustmentQty!='')
		{
			$this->storeItemAdjustmentQty = $storeItemAdjustmentQty;
		}
	}
	
	function setManufacturerID($manufacturerID)
	{
		if($manufacturerID!='')
		{
			$this->manufacturerID = $manufacturerID;
		}
	}
	function setLocationID($locationID)
	{
		if($locationID!='')
		{
			$this->locationID = $locationID;
		}
	}
	function setOrganizationID($organizationID)
	{
		if($organizationID!="")
		{
			$this->organizationID = $organizationID;
		}
		
	}
	function setuserID($userID)
	{
		if($userID!="")
		{
			$this->userID = $userID;
		}
		
	}
	
	
	################################# GET FUNCTIONS ############################################
	
	function getStoreItemID()
	{
		if($this->storeItemID!='')
		{
			return $this->storeItemID;
		}
		else
		{
			throw new MyException(119104);
		}
	}
	function getStoreItemName()
	{
		if($this->storeItemName!='')
		{
			return $this->storeItemName;
		}
		else
		{
			throw new MyException(119105);
		}
	}
	function getStoreItemCategory()
	{
		if($this->storeItemCategory!='')
		{
			return $this->storeItemCategory;
		}
		else
		{
			throw new MyException(119106);
		}
	}
	function getStoreItemReorderQty()
	{
		if($this->storeItemReorderQty!='')
		{
			return $this->storeItemReorderQty;
		}
		else
		{
			throw new MyException(119107);
		}
	}
	function getStoreItemQtyInStock()
	{
		if($this->storeItemQtyInStock!='')
		{
			return $this->storeItemQtyInStock;
		}
	}
	function getActive()
	{
		return $this->active;
	}
	
	function getStoreItemAdjustedQty()
	{
		if($this->storeItemAdjustedQty!='')
		{
			return $this->storeItemAdjustedQty;
		}
		else
		{
			throw new MyException(119110);
		}
	}
	function getStoreItemAdjustmentQty()
	{
		if($this->storeItemAdjustmentQty!='')
		{
			return $this->storeItemAdjustmentQty;
		}
	}
	

	function getManufacturerID()
	{
		if($this->manufacturerID!='')
		{
			return $this->manufacturerID;
		}
	}
	function getLocationID()
	{
		if($this->locationID!='')
		{
			return $this->locationID;
		}
	}
	function getOrganizationID()
	{
		if($this->organizationID!="")
		{
			return $this->organizationID;
		}
	}
	function getuserID()
	{
		if($this->userID!="")
		{
			return $this->userID;
		}
		
	}
	
	################################# MAIN CHECK FUNCTIONS ############################################
	
	function checkStoreNameAlreadyExist($storeitem_name,$org_id)
	{
		$query = "SELECT storeitem_id FROM store_items WHERE storeitem_name = '$storeitem_name' AND org_id = '$org_id'";
		$queryOb = new classQueryGlobal;
		$store_item = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($store_item[0]['storeitem_id'])&&$store_item[0]['storeitem_id']!='')
		{
			throw new MyException(119108);
		}
		else
		{
			return 1;
		}
		
	}
	
	################################# MAIN INSERT FUNCTIONS ############################################
	
	function insertStoreItem()
	{
		$manf_id = $this->getManufacturerID();
		$storeitem_name = $this->getStoreItemName();
		$category = $this->getStoreItemCategory();
		$reorder_qty = $this->getStoreItemReorderQty();
		$active = 1;
		$org_id = $this->getOrganizationID();
		
		$this->checkStoreNameAlreadyExist($storeitem_name,$org_id);
		
		$query = "INSERT INTO store_items
		(
			manf_id,
			storeitem_name,
			category,
			reorder_qty,
			active,
			org_id
		)
		VALUES
		(
			'$manf_id',
			'$storeitem_name',
			'$category',
			'$reorder_qty',
			'$active',
			'$org_id'
		)";
		
		$queryOb = new classQueryGlobal;
		$this->storeItemID = $queryOb->check_fetch_query($query,'insert',$server="",$debug_mode="off");
		
		
		$this->insertStoreItemLocationRelationshipData();
		
		
	}
	
	function insertStoreItemLocationRelationshipData()
	{
		$storeitem_id = $this->getStoreItemID();
		$location_id = $this->getLocationID();
		
		$query = "INSERT INTO store_items_location_relationship
		(
			storeitem_id,
			location_id
		)
		VALUES
		(
			'$storeitem_id',
			'$location_id'
		)";
		
		$queryOb = new classQueryGlobal;
		$queryOb->check_fetch_query($query,'insert',$server="",$debug_mode="off");
	}
	
	function insertStoreItemAdjustments()
	{
		$storeitem_id = $this->getStoreItemID();
		$adjusted_qty = $this->getStoreItemAdjustedQty();
		$location_id = $this->getLocationID();
		$adjustment_by = $this->getuserID();
		$adjustment_date = time();
		
		$old_qty  = $this->retrieveQtyInStock($storeitem_id,$location_id);
		
		$adjustment_qty = $adjusted_qty-$old_qty;
		
		$query = "INSERT INTO store_items_adjustment 
		(
			storeitem_id,
			adjusted_qty,
			adjustment_by,
			adjustment_date,
			old_qty,
			adjustment_qty
		) 
		VALUES 
		(
			'$storeitem_id',
			'$adjusted_qty',
			'$adjustment_by',
			'$adjustment_date',
			'$old_qty',
			'$adjustment_qty'
		)";
		$queryOb = new classQueryGlobal;
		$queryOb->check_fetch_query($query,'insert',$server="",$debug_mode="off");
		
		$this->storeItemQtyInStock = $adjusted_qty;
		$this->updateQtyInStock();
		
	}
	
	################################# MAIN UPDATE FUNCTIONS #############################################
	
	function updateStoreItem()
	{
		
		$storeitem_id = $this->getStoreItemID();
		$manf_id = $this->getManufacturerID();
		$storeitem_name = $this->getStoreItemName();
		$category = $this->getStoreItemCategory();
		$reorder_qty = $this->getStoreItemReorderQty();
		$active = 1;
		
		$query = "UPDATE store_items SET
			storeitem_name = '$storeitem_name',
			manf_id = '$manf_id',
			category = '$category',
			reorder_qty = '$reorder_qty',
			active = '$active'
			WHERE storeitem_id = '$storeitem_id'";

			
		$queryOb = new classQueryGlobal;
		$queryOb->check_fetch_query($query,'update',$server="",$debug_mode="off");
	}
	
	function updateStoreItemStatus()
	{
		$storeitem_id = $this->getStoreItemID();
		$active = $this->getActive();
		
		$query = "UPDATE store_items SET active = '$active' WHERE storeitem_id = '$storeitem_id'";
		$queryOb = new classQueryGlobal;
		$queryOb->check_fetch_query($query,'update',$server="",$debug_mode="off");
		
	}
	
	function updateQtyInStock()
	{
		$storeitem_id = $this->getStoreItemID();
		$location_id = $this->getLocationID();
		$qty_in_stock = $this->getStoreItemQtyInStock();
		
		$query = "UPDATE store_items_location_relationship SET
			qty_in_stock = '$qty_in_stock'
			WHERE storeitem_id = '$storeitem_id' AND location_id = '$location_id'";

		$queryOb = new classQueryGlobal;
		$queryOb->check_fetch_query($query,'update',$server="",$debug_mode="off");
	}
	
	
	
	################################# MAIN DELETE FUNCTIONS ############################################
	
	
	
	################################# MAIN RETRIEVE FUNCTIONS ############################################
	
	function retrieveStoreItems()
	{
		$org_id = $this->getOrganizationID();
		$active = $this->getActive();
		
		$query = "SELECT * FROM store_items WHERE active = '$active' AND org_id = '$org_id' order by storeitem_name asc";
		$queryOb = new classQueryGlobal;
		$products_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($products_array[0])&&$products_array[0]!='')
		{
			return $products_array;
		}
		
	}
	
	function countStoreItemsThroughFilter($search=NULL,$sortby=NULL,$org_id=NULL,$active=NULL)
	{
		$query = "SELECT  count(*) FROM store_items WHERE org_id = '$org_id' AND active = '$active'";
		
		if(isset($search)&&$search!="")
		{
			$query .= " AND (storeitem_name LIKE '%".$search."%') OR (category LIKE '%".$search."%')";
		}
		elseif(isset($sortby)&&$sortby!=""&&$sortby!="all")
		{
			$query .= " AND storeitem_name LIKE '$sortby%'";
		}
		
		$queryOb = new classQueryGlobal;
		$products_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($products_array[0]['count(*)'])&&$products_array[0]['count(*)']!='')
		{
			return $products_array[0]['count(*)'];
		}
	}
	
	function retrieveStoreItemsThroughFilter($search=NULL,$sortby=NULL,$org_id=NULL,$active=NULL,$limit=NULL)
	{
		$query = "SELECT * FROM store_items WHERE org_id = '$org_id' AND active = '$active'";
		
		if(isset($search)&&$search!="")
		{
			$query .= " AND (storeitem_name LIKE '%".$search."%') OR (category LIKE '%".$search."%')";
		}
		elseif(isset($sortby)&&$sortby!=""&&$sortby!="all")
		{
			$query .= " AND storeitem_name LIKE '$sortby%'";
		}
		
		$query .= " ORDER BY storeitem_name ASC";
		
		if(!is_null($limit))
		{
			$query .= " LIMIT $limit";
		}
		
		
		$queryOb = new classQueryGlobal;
		$products_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($products_array[0])&&$products_array[0]!='')
		{
			return $products_array;
		}
	}
	
	function retrieveStoreItemByID()
	{
		$storeitem_id = $this->getStoreItemID();
		$query = "SELECT * FROM store_items WHERE storeitem_id = '$storeitem_id'";
		$queryOb = new classQueryGlobal;
		$products_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($products_array[0])&&$products_array[0]!='')
		{
			return $products_array;
		}
		
	}
	
	function retrieveStoreItemByLocationID()
	{
		$location_id = $this->getLocationID();
		
		$query = "SELECT * FROM store_items_location_relationship WHERE location_id = '$location_id' ";
		$queryOb = new classQueryGlobal;
		$store_items_location_relationship = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($store_items_location_relationship[0]['storeitem_loc_id'])&&$store_items_location_relationship[0]['storeitem_loc_id']!='')
		{
			return $store_items_location_relationship;
		}
		
	}
	
	function retrieveStoreItemLocationRelationship($storeitem_id,$location_id)
	{
		$query = "SELECT * FROM store_items_location_relationship WHERE storeitem_id = '$storeitem_id' AND location_id = '$location_id' ";
		$queryOb = new classQueryGlobal;
		$store_items_location_relationship = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($store_items_location_relationship[0]['storeitem_id'])&&$store_items_location_relationship[0]['storeitem_id']!='')
		{
			return $store_items_location_relationship;
		}
		
	}
	
	function retrieveQtyInStock($storeitem_id,$location_id)
	{
		$query = "SELECT qty_in_stock FROM store_items_location_relationship WHERE storeitem_id = '$storeitem_id' AND location_id = '$location_id' ";
		$queryOb = new classQueryGlobal;
		$store_items_location_relationship = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($store_items_location_relationship[0]['qty_in_stock'])&&$store_items_location_relationship[0]['qty_in_stock']!='')
		{
			return $store_items_location_relationship[0]['qty_in_stock'];
		}
		
	}
	
	
}
?>