<?php
class classProduct
{
	private $productID=NULL;
	private $productName=NULL;
	private $productCategory=NULL;
	private $productType=NULL;
	private $productReorderQty=NULL;
	
	private $productBoxQty=NULL;
	private $productLeafPerBoxQty=NULL;
	private $productUnitPerBoxQty=NULL;
	
	private $productBoxTradePrice=NULL;
	private $productLeafTradePrice=NULL;
	private $productUnitTradePrice=NULL;
	
	private $productBoxRetailPrice=NULL;
	private $productLeafRetailPrice=NULL;
	private $productUnitRetailPrice=NULL;
	
	private $productUnitQtyInStock=NULL;
	
	private $active=NULL;
	
	private $supplierID=NULL;
	private $supplierName=NULL;
	private $supplierPhone=NULL;
	private $supplierCompanyName=NULL;
	private $supplierCompanyPhone=NULL;
	private $supplierCompanyAddress=NULL;
	
	private $manufacturerID=NULL;
	private $manufacturerName=NULL;
	private $manufacturerPhone=NULL;
	private $manufacturerAddress=NULL;
	
	private $organizationID=NULL;
	private $locationID=NULL;
	
	################################# SET FUNCTIONS ############################################
	
	function setProductID($productID)
	{
		if($productID!='')
		{
			$this->productID = $productID;
		}
		else
		{
			throw new MyException(104100);
		}
	}
	function setProductName($productName)
	{
		if($productName!='')
		{
			$this->productName = $productName;
		}
		else
		{
			throw new MyException(104101);
		}
	}
	function setProductCategory($productCategory)
	{
		if($productCategory!='')
		{
			$this->productCategory = $productCategory;
		}
		else
		{
			throw new MyException(104102);
		}
	}
	function setProductType($productType)
	{
		if($productType!='')
		{
			$this->productType = $productType;
		}
		else
		{
			throw new MyException(104103);
		}
	}
	function setProductReorderQty($productReorderQty)
	{
		if($productReorderQty!='')
		{
			$this->productReorderQty = $productReorderQty;
		}
		else
		{
			throw new MyException(104104);
		}
	}
	
	function setProductBoxQty($productBoxQty)
	{
		if($productBoxQty!='')
		{
			$this->productBoxQty = $productBoxQty;
		}
		else
		{
			throw new MyException(104105);
		}
	}
	function setProductLeafPerBoxQty($productLeafPerBoxQty)
	{
		if($productLeafPerBoxQty!='')
		{
			$this->productLeafPerBoxQty = $productLeafPerBoxQty;
		}
		else
		{
			throw new MyException(104106);
		}
	}
	function setProductUnitPerBoxQty($productUnitPerBoxQty)
	{
		if($productUnitPerBoxQty!='')
		{
			$this->productUnitPerBoxQty = $productUnitPerBoxQty;
		}
		else
		{
			throw new MyException(104107);
		}
	}
	
	function setProductBoxTradePrice($productBoxTradePrice)
	{
		if($productBoxTradePrice!='')
		{
			$this->productBoxTradePrice = $productBoxTradePrice;
		}
		else
		{
			throw new MyException(104108);
		}
	}
	function setProductLeafTradePrice($productLeafTradePrice)
	{
		if($productLeafTradePrice!='')
		{
			$this->productLeafTradePrice = $productLeafTradePrice;
		}
		else
		{
			throw new MyException(104109);
		}
	}
	function setProductUnitTradePrice($productUnitTradePrice)
	{
		if($productUnitTradePrice!='')
		{
			$this->productUnitTradePrice = $productUnitTradePrice;
		}
		else
		{
			throw new MyException(104110);
		}
	}
	
	function setProductBoxRetailPrice($productBoxRetailPrice)
	{
		if($productBoxRetailPrice!='')
		{
			$this->productBoxRetailPrice = $productBoxRetailPrice;
		}
		else
		{
			throw new MyException(104111);
		}
	}
	function setProductLeafRetailPrice($productLeafRetailPrice)
	{
		
		if($productLeafRetailPrice!='')
		{
			$this->productLeafRetailPrice = $productLeafRetailPrice;
		}
		else
		{
			throw new MyException(104112);
		}
	}
	function setProductUnitRetailPrice($productUnitRetailPrice)
	{
		if($productUnitRetailPrice!='')
		{
			$this->productUnitRetailPrice = $productUnitRetailPrice;
		}
		else
		{
			throw new MyException(104113);
		}
	}
	
	function setProductUnitQtyInStock($productUnitQtyInStock)
	{
		if($productUnitQtyInStock!='')
		{
			$this->productUnitQtyInStock = $productUnitQtyInStock;
		}
	}
	
	function setActive($active)
	{
		$this->active = $active;
	}
	
	function setSupplierID($supplierID)
	{
		if($supplierID!='')
		{
			$this->supplierID = $supplierID;
		}
		else
		{
			throw new MyException(104114);
		}
	}
	function setSupplierName($supplierName)
	{
		if($supplierName!='')
		{
			$this->supplierName = $supplierName;
		}
		else
		{
			throw new MyException(104115);
		}
	}
	function setSupplierPhone($supplierPhone)
	{
		if($supplierPhone!='')
		{
			$this->supplierPhone = $supplierPhone;
		}
		else
		{
			//throw new MyException(104116);
		}
	}
	function setSupplierCompanyName($supplierCompanyName)
	{
		if($supplierCompanyName!='')
		{
			$this->supplierCompanyName = $supplierCompanyName;
		}
		else
		{
			//throw new MyException(104117);
		}
	}
	function setSupplierCompanyPhone($supplierCompanyPhone)
	{
		if($supplierCompanyPhone!='')
		{
			$this->supplierCompanyPhone = $supplierCompanyPhone;
		}
		else
		{
			//throw new MyException(104118);
		}
	}
	function setSupplierCompanyAddress($supplierCompanyAddress)
	{
		if($supplierCompanyAddress!='')
		{
			$this->supplierCompanyAddress = $supplierCompanyAddress;
		}
		else
		{
			//throw new MyException(104119);
		}
	}
	
	function setManufacturerID($manufacturerID)
	{
		if($manufacturerID!='')
		{
			$this->manufacturerID = $manufacturerID;
		}
		else
		{
			//throw new MyException(104120);
		}
	}
	function setManufacturerName($manufacturerName)
	{
		if($manufacturerName!='')
		{
			$this->manufacturerName = $manufacturerName;
		}
		else
		{
			throw new MyException(104121);
		}
	}
	function setManufacturerPhone($manufacturerPhone)
	{
		if($manufacturerPhone!='')
		{
			$this->manufacturerPhone = $manufacturerPhone;
		}
		else
		{
			//throw new MyException(104122);
		}
	}
	function setManufacturerAddress($manufacturerAddress)
	{
		if($manufacturerAddress!='')
		{
			$this->manufacturerAddress = $manufacturerAddress;
		}
		else
		{
			//throw new MyException(104123);
		}
	}
	
	function setLocationID($locationID)
	{
		if($locationID!='')
		{
			$this->locationID = $locationID;
		}
		else
		{
			throw new MyException(104149);
		}
	}
	
	function setOrganizationID($organizationID)
	{
		if($organizationID!="")
		{
			$this->organizationID = $organizationID;
		}
		
	}
	
	
	################################# GET FUNCTIONS ############################################
	
	function getProductID()
	{
		if($this->productID!='')
		{
			return $this->productID;
		}
		else
		{
			throw new MyException(104124);
		}
	}
	function getProductName()
	{
		if($this->productName!='')
		{
			return $this->productName;
		}
		else
		{
			throw new MyException(104125);
		}
	}
	function getProductCategory()
	{
		if($this->productCategory!='')
		{
			return $this->productCategory;
		}
		else
		{
			throw new MyException(104126);
		}
	}
	function getProductType()
	{
		if($this->productType!='')
		{
			return $this->productType;
		}
		else
		{
			throw new MyException(104127);
		}
	}
	function getProductReorderQty()
	{
		if($this->productReorderQty!='')
		{
			return $this->productReorderQty;
		}
		else
		{
			throw new MyException(104128);
		}
	}

	function getProductBoxQty()
	{
		if($this->productBoxQty!='')
		{
			return $this->productBoxQty;
		}
		else
		{
			throw new MyException(104129);
		}
	}
	function getProductLeafPerBoxQty()
	{
		if($this->productLeafPerBoxQty!='')
		{
			return $this->productLeafPerBoxQty;
		}
		else
		{
			throw new MyException(104130);
		}
	}
	function getProductUnitPerBoxQty()
	{
		if($this->productUnitPerBoxQty!='')
		{
			return $this->productUnitPerBoxQty;
		}
		else
		{
			throw new MyException(104131);
		}
	}
	
	function getProductBoxTradePrice()
	{
		if($this->productBoxTradePrice!='')
		{
			return $this->productBoxTradePrice;
		}
		else
		{
			throw new MyException(104132);
		}
	}
	function getProductLeafTradePrice()
	{
		if($this->productLeafTradePrice!='')
		{
			return $this->productLeafTradePrice;
		}
		else
		{
			throw new MyException(104133);
		}
	}
	function getProductUnitTradePrice()
	{
		if($this->productUnitTradePrice!='')
		{
			return $this->productUnitTradePrice;
		}
		else
		{
			throw new MyException(104134);
		}
	}
	
	function getProductBoxRetailPrice()
	{
		if($this->productBoxRetailPrice!='')
		{
			return $this->productBoxRetailPrice;
		}
		else
		{
			throw new MyException(104135);
		}
	}
	function getProductLeafRetailPrice()
	{
		
		if($this->productLeafRetailPrice!='')
		{
			return $this->productLeafRetailPrice;
		}
		else
		{
			throw new MyException(104136);
		}
	}
	function getProductUnitRetailPrice()
	{
		if($this->productUnitRetailPrice!='')
		{
			return $this->productUnitRetailPrice;
		}
		else
		{
			throw new MyException(104137);
		}
	}
	
	function getProductUnitQtyInStock()
	{
		if($this->productUnitQtyInStock!='')
		{
			return $this->productUnitQtyInStock;
		}
	}
	
	function getActive()
	{
		return $this->active;
	}
	
	function getSupplierID()
	{
		if($this->supplierID!='')
		{
			return $this->supplierID;
		}
		else
		{
			throw new MyException(104138);
		}
	}
	function getSupplierName()
	{
		if($this->supplierName!='')
		{
			return $this->supplierName;
		}
		else
		{
			throw new MyException(104139);
		}
	}
	function getSupplierPhone()
	{
		if($this->supplierPhone!='')
		{
			return $this->supplierPhone;
		}
		else
		{
			//throw new MyException(104140);
		}
	}
	function getSupplierCompanyName()
	{
		if($this->supplierCompanyName!='')
		{
			return $this->supplierCompanyName;
		}
		else
		{
			//throw new MyException(104141);
		}
	}
	function getSupplierCompanyPhone()
	{
		if($this->supplierCompanyPhone!='')
		{
			return $this->supplierCompanyPhone;
		}
		else
		{
			//throw new MyException(104142);
		}
	}
	function getSupplierCompanyAddress()
	{
		if($this->supplierCompanyAddress!='')
		{
			return $this->supplierCompanyAddress;
		}
		else
		{
			//throw new MyException(104143);
		}
	}
	
	function getManufacturerID()
	{
		if($this->manufacturerID!='')
		{
			return $this->manufacturerID;
		}
		else
		{
			//throw new MyException(104144);
		}
	}
	function getManufacturerName()
	{
		if($this->manufacturerName!='')
		{
			return $this->manufacturerName;
		}
		else
		{
			throw new MyException(104145);
		}
	}
	function getManufacturerPhone()
	{
		if($this->manufacturerPhone!='')
		{
			return $this->manufacturerPhone;
		}
		else
		{
			//throw new MyException(104146);
		}
	}
	function getManufacturerAddress()
	{
		if($this->manufacturerAddress!='')
		{
			return $this->manufacturerAddress;
		}
		else
		{
			//throw new MyException(104147);
		}
	}
	
	function getLocationID()
	{
		if($this->locationID!='')
		{
			return $this->locationID;
		}
		else
		{
			throw new MyException(104150);
		}
	}
	
	function getOrganizationID()
	{
		if($this->organizationID!="")
		{
			return $this->organizationID;
		}
		
	}
	
	
	################################# MAIN CHECK FUNCTIONS ############################################
	
	function checkProductSupplierRelationship($product_id,$supplier_id)
	{
		$query = "SELECT * FROM  product_supplier_relationship WHERE product_id = '$product_id' AND supplier_id = '$supplier_id' ";
		$queryOb = new classQueryGlobal;
		$product_supplier_relationship = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($product_supplier_relationship[0]['relationship_id'])&&$product_supplier_relationship[0]['relationship_id']!='')
		{
			return 0;
		}
		else
		{
			return 1;
		}
		
	}
	
	function checkSupplierAgainstProduct($supplier_id)
	{
		$query = "SELECT supplier_id FROM product_supplier_relationship WHERE supplier_id = '$supplier_id' ";
		$queryOb = new classQueryGlobal;
		$product_supplier_relationship = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($product_supplier_relationship[0]['supplier_id'])&&$product_supplier_relationship[0]['supplier_id']!='')
		{
			return 0;
		}
		else
		{
			return 1;
		}
		
	}
	function checkManufacturerAgainstProduct($manf_id)
	{
		$query = "SELECT product_id FROM products WHERE manf_id = '$manf_id' ";
		$queryOb = new classQueryGlobal;
		$product_manuf_relationship = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($product_manuf_relationship[0]['product_id'])&&$product_manuf_relationship[0]['product_id']!='')
		{
			return 0;
		}
		else
		{
			return 1;
		}
		
	}
	
	################################# MAIN INSERT FUNCTIONS ############################################
	
	function insertProduct()
	{
		$manf_id = $this->getManufacturerID();
		$org_id = $this->getOrganizationID();
		$product_name = $this->getProductName();
		$category = $this->getProductCategory();
		$medicine_type = $this->getProductType();
		$reorder_qty = $this->getProductReorderQty();
		
		$box_qty = $this->getProductBoxQty();
		$leafperbox = $this->getProductLeafPerBoxQty();
		$unitperbox = $this->getProductUnitPerBoxQty();
		
		$active = $this->getActive();
		
		$query = "INSERT INTO products
		(
			product_name,
			manf_id,
			category,
			medicine_type,
			reorder_qty,
			box_qty,
			leafperbox,
			unitperbox,
			active,
			org_id
		)
		VALUES
		(
			'$product_name',
			'$manf_id',
			'$category',
			'$medicine_type',
			'$reorder_qty',
			'$box_qty',
			'$leafperbox',
			'$unitperbox',
			'$active',
			'$org_id'
		)";
		
		$queryOb = new classQueryGlobal;
		$this->productID = $queryOb->check_fetch_query($query,'insert',$server="",$debug_mode="off");
		
		$this->insertProductLocationRelationshipData();
		
		$this->insertProductSupplierRelationshipData();
	}
	
	function insertProductSupplierRelationshipData()
	{
		
		if($this->checkProductSupplierRelationship($this->getProductID(),$this->getSupplierID()))
		{
			$product_id = $this->getProductID();
			$supplier_id = $this->getSupplierID();
			$box_tradeprice = $this->getProductBoxTradePrice();
			$leaf_tradeprice = $this->getProductLeafTradePrice();
			$unit_tradeprice = $this->getProductUnitTradePrice();
			
			$query = "INSERT INTO product_supplier_relationship
			(
				product_id,
				supplier_id,
				box_tradeprice,
				leaf_tradeprice,
				unit_tradeprice
				
			)
			VALUES
			(
				'$product_id',
				'$supplier_id',
				'$box_tradeprice',
				'$leaf_tradeprice',
				'$unit_tradeprice'
				
			)";
			
			$queryOb = new classQueryGlobal;
			$queryOb->check_fetch_query($query,'insert',$server="",$debug_mode="off");
		}
		else
		{
			
			throw new MyException(104148);
		}
		
	}
	
	function insertProductLocationRelationshipData()
	{
		$product_id = $this->getProductID();
		$location_id = $this->getLocationID();
		$box_retailprice = $this->getProductBoxRetailPrice();
		$leaf_retailprice = $this->getProductLeafRetailPrice();
		$unit_retailprice = $this->getProductUnitRetailPrice();
		
		$query = "INSERT INTO product_location_relationship
		(
			product_id,
			location_id,
			box_retailprice,
			leaf_retailprice,
			unit_retailprice
			
		)
		VALUES
		(
			'$product_id',
			'$location_id',
			'$box_retailprice',
			'$leaf_retailprice',
			'$unit_retailprice'
			
		)";
		
		$queryOb = new classQueryGlobal;
		$queryOb->check_fetch_query($query,'insert',$server="",$debug_mode="off");
	}
	
	function insertManufacturer()
	{
		$manf_name = $this->getManufacturerName();
		$manf_phone = $this->getManufacturerPhone();
		$manf_address = $this->getManufacturerAddress();
		$org_id = $this->getOrganizationID();
		
		$query = "INSERT INTO  manufacturers
		(
			manf_name,
			manf_phone,
			manf_address,
			org_id
		)
		VALUES
		(
			'$manf_name',
			'$manf_phone',
			'$manf_address',
			'$org_id'
		)";
		
		$queryOb = new classQueryGlobal;
		$this->manufacturerID = $queryOb->check_fetch_query($query,'insert',$server="",$debug_mode="off");
	}
	
	function insertSupplier()
	{
		$supplier_name = $this->getSupplierName();
		$supplier_phone = $this->getSupplierPhone();
		$supplier_address = $this->getSupplierCompanyAddress();
		$company_name = $this->getSupplierCompanyName();
		$company_phone = $this->getSupplierCompanyPhone();
		$location_id = $this->getLocationID();
		
		$query = "INSERT INTO suppliers
		(
			supplier_name,
			supplier_phone,
			supplier_address,
			company_name,
			company_phone,
			location_id
		)
		VALUES
		(
			'$supplier_name',
			'$supplier_phone',
			'$supplier_address',
			'$company_name',
			'$company_phone',
			'$location_id'
		)";
		
		$queryOb = new classQueryGlobal;
		$this->supplierID = $queryOb->check_fetch_query($query,'insert',$server="",$debug_mode="off");
	}
	
	################################# MAIN UPDATE FUNCTIONS #############################################
	
	function updateProduct()
	{
		$product_id = $this->getProductID();
		$org_id = $this->getOrganizationID();
		$manf_id = $this->getManufacturerID();
		
		$product_name = $this->getProductName();
		$category = $this->getProductCategory();
		$medicine_type = $this->getProductType();
		$reorder_qty = $this->getProductReorderQty();
		
		$box_qty = $this->getProductBoxQty();
		$leafperbox = $this->getProductLeafPerBoxQty();
		$unitperbox = $this->getProductUnitPerBoxQty();
		
		$active = $this->getActive();
		
		$query = "UPDATE products SET
			product_name = '$product_name',
			manf_id = '$manf_id',
			category = '$category',
			medicine_type = '$medicine_type',
			reorder_qty = '$reorder_qty',
			box_qty = '$box_qty',
			leafperbox = '$leafperbox',
			unitperbox = '$unitperbox',
			active = '$active',
			org_id = '$org_id'
			WHERE product_id = '$product_id'";

			
		$queryOb = new classQueryGlobal;
		$queryOb->check_fetch_query($query,'update',$server="",$debug_mode="off");
		
		$this->updateProductLocationRelationshipData();
		
		$this->updateProductSupplierRelationshipData();
		
	}
	
	function updateProductStatus()
	{
		$product_id = $this->getProductID();
		$active = $this->getActive();
		
		$query = "UPDATE products SET active = '$active' WHERE product_id = '$product_id'";
		$queryOb = new classQueryGlobal;
		$queryOb->check_fetch_query($query,'update',$server="",$debug_mode="off");
		
	}
	
	function updateProductUnitQtyInStock()
	{
		$product_id = $this->getProductID();
		$location_id = $this->getLocationID();
		$unit_qtyinstock = $this->getProductUnitQtyInStock();
		
		$query = "UPDATE product_location_relationship SET
			unit_qtyinstock = '$unit_qtyinstock'
			WHERE product_id = '$product_id' AND location_id = '$location_id'";

		$queryOb = new classQueryGlobal;
		$queryOb->check_fetch_query($query,'update',$server="",$debug_mode="off");
	}
	
	function updateProductSupplierRelationshipData()
	{
		$product_id = $this->getProductID();
		
		$supplier_ids = $this->getSupplierID();
		$box_tradeprice_array = $this->getProductBoxTradePrice();
		$leaf_tradeprice_array = $this->getProductLeafTradePrice();
		$unit_tradeprice_array = $this->getProductUnitTradePrice();
		
		for($i=0;$i<count($supplier_ids);$i++)
		{
			$supplier_id = $supplier_ids[$i];
			$box_tradeprice = $box_tradeprice_array[$i];
			$leaf_tradeprice = $leaf_tradeprice_array[$i];
			$unit_tradeprice= $unit_tradeprice_array[$i];
			if($box_tradeprice<$this->getProductBoxRetailPrice())
			{
				$query = "UPDATE product_supplier_relationship SET
				box_tradeprice = '$box_tradeprice',
				leaf_tradeprice = '$leaf_tradeprice',
				unit_tradeprice = '$unit_tradeprice'
				WHERE product_id = '$product_id' AND supplier_id = '$supplier_id'";
				$queryOb = new classQueryGlobal;
				$queryOb->check_fetch_query($query,'update',$server="",$debug_mode="off");
			}
			else
			{
				throw new MyException(104151);
			}
		}
	}
	
	function updateProductLocationRelationshipData()
	{
		$product_id = $this->getProductID();
		$location_id = $this->getLocationID();
		$box_retailprice = $this->getProductBoxRetailPrice();
		$leaf_retailprice = $this->getProductLeafRetailPrice();
		$unit_retailprice = $this->getProductUnitRetailPrice();
		
		$query = "UPDATE product_location_relationship SET
		box_retailprice = '$box_retailprice',
		leaf_retailprice = '$leaf_retailprice',
		unit_retailprice = '$unit_retailprice'
		WHERE product_id = '$product_id' AND location_id = '$location_id'";
		$queryOb = new classQueryGlobal;
		$queryOb->check_fetch_query($query,'update',$server="",$debug_mode="off");
		
	}
	
	function updateManufacturer()
	{
		$manf_name = $this->getManufacturerName();
		$manf_phone = $this->getManufacturerPhone();
		$manf_address = $this->getManufacturerAddress();
		$manf_id = $this->getManufacturerID();
		
		$query = "UPDATE manufacturers SET
		manf_name = '$manf_name',
		manf_phone = '$manf_phone',
		manf_address = '$manf_address'
		WHERE manf_id = '$manf_id'";
		$queryOb = new classQueryGlobal;
		$queryOb->check_fetch_query($query,'update',$server="",$debug_mode="off");
	}
	
	function updateSupplier()
	{
		$supplier_name = $this->getSupplierName();
		$supplier_phone = $this->getSupplierPhone();
		$supplier_address = $this->getSupplierCompanyAddress();
		$company_name = $this->getSupplierCompanyName();
		$company_phone = $this->getSupplierCompanyPhone();
		$supplier_id = $this->getSupplierID();
		
		$query = "UPDATE suppliers SET
		supplier_name = '$supplier_name',
		supplier_phone = '$supplier_phone',
		supplier_address = '$supplier_address',
		company_name = '$company_name',
		company_phone = '$company_phone'
		WHERE supplier_id = '$supplier_id'";
		$queryOb = new classQueryGlobal;
		$queryOb->check_fetch_query($query,'update',$server="",$debug_mode="off");
	}
	
	################################# MAIN DELETE FUNCTIONS ############################################
	
	function deleteManufacturer()
	{
		
		$manf_id = $this->getManufacturerID();
		
		$query = "DELETE FROM manufacturers WHERE manf_id = '$manf_id'";
		$queryOb = new classQueryGlobal;
		$queryOb->check_fetch_query($query,'delete',$server="",$debug_mode="off");
	}
	
	function deleteSupplier()
	{
		$supplier_id = $this->getSupplierID();
		
		$query = "DELETE FROM suppliers WHERE supplier_id = '$supplier_id'";
		$queryOb = new classQueryGlobal;
		$queryOb->check_fetch_query($query,'delete',$server="",$debug_mode="off");
	}
	
	################################# MAIN RETRIEVE FUNCTIONS ############################################
	
	function retrieveActiveProducts()
	{
		$org_id = $this->getOrganizationID();
		$query = "SELECT * FROM products WHERE active = 1 AND org_id = '$org_id' order by product_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 retrieveDeActiveProducts()
	{
		$org_id = $this->getOrganizationID();
		
		$query = "SELECT * FROM products WHERE active != 1 AND org_id = '$org_id' order by product_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 countActiveProductsThroughFilter($search=NULL,$sortby=NULL,$org_id=NULL)
	{
		if(isset($search)&&$search!="")
		{
			$query = "SELECT  count(*) FROM products WHERE org_id = '$org_id' AND active = 1 AND (product_name LIKE '%".$search."%') OR (category LIKE '%".$search."%') OR (medicine_type LIKE '%".$search."%') order by product_name asc";
		}
		elseif(isset($sortby)&&$sortby!=""&&$sortby!="all")
		{
			$query = "SELECT  count(*) FROM products WHERE org_id = '$org_id' AND active = 1 AND product_name LIKE '$sortby%'  order by product_name asc";
		}
		else
		{
			$query = "SELECT  count(*) FROM products WHERE org_id = '$org_id' AND active = 1 order by product_name asc";
		}
		
		$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 retrieveActiveProductsThroughFilter($search=NULL,$sortby=NULL,$org_id=NULL,$limit=NULL)
	{
		if(isset($search)&&$search!="")
		{
			$query = "SELECT * FROM products WHERE org_id = '$org_id' AND active = 1 AND (product_name LIKE '%".$search."%') OR (category LIKE '%".$search."%') OR (medicine_type LIKE '%".$search."%') order by product_name asc LIMIT $limit";
		}
		elseif(isset($sortby)&&$sortby!=""&&$sortby!="all")
		{
			$query = "SELECT * FROM products WHERE org_id = '$org_id' AND active = 1 AND product_name LIKE '$sortby%'  order by product_name asc LIMIT $limit";
		}
		elseif(!is_null($limit))
		{
			$query = "SELECT * FROM products WHERE org_id = '$org_id' AND active = 1 order by product_name asc LIMIT $limit";
		}
		else
		{
			$query = "SELECT * FROM products WHERE org_id = '$org_id' AND active = 1 order by product_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 countDeActiveProductsThroughFilter($search=NULL,$sortby=NULL,$org_id=NULL)
	{
		if(isset($search)&&$search!="")
		{
			$query = "SELECT count(*) FROM products WHERE org_id = '$org_id' AND active = 0 AND (product_name LIKE '%".$search."%') OR (category LIKE '%".$search."%') OR (medicine_type LIKE '%".$search."%') order by product_name asc";
		}
		elseif(isset($sortby)&&$sortby!=""&&$sortby!="all")
		{
			$query = "SELECT count(*) FROM products WHERE org_id = '$org_id' AND active = 0 AND product_name LIKE '$sortby%'  order by product_name asc";
		}
		else
		{
			$query = "SELECT count(*) FROM products WHERE org_id = '$org_id' AND active = 0 order by product_name asc";
		}
		
		$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 retrieveDeActiveProductsThroughFilter($search=NULL,$sortby=NULL,$org_id=NULL,$limit=NULL)
	{
		if(isset($search)&&$search!="")
		{
			$query = "SELECT * FROM products WHERE org_id = '$org_id' AND active = 0 AND (product_name LIKE '%".$search."%') OR (category LIKE '%".$search."%') OR (medicine_type LIKE '%".$search."%') order by product_name asc LIMIT $limit";
		}
		elseif(isset($sortby)&&$sortby!=""&&$sortby!="all")
		{
			$query = "SELECT * FROM products WHERE org_id = '$org_id' AND active = 0 AND product_name LIKE '$sortby%'  order by product_name asc LIMIT $limit";
		}
		else
		{
			$query = "SELECT * FROM products WHERE org_id = '$org_id' AND active = 0 order by product_name asc 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 retrieveProductByProductID()
	{
		$product_id = $this->getProductID();
		$query = "SELECT * FROM products WHERE product_id = '$product_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 retrieveProductsByLocationID()
	{
		$location_id = $this->getLocationID();
		$query = "SELECT * FROM product_location_relationship WHERE location_id = '$location_id' ";
		$queryOb = new classQueryGlobal;
		$product_location_relationship = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($product_location_relationship[0]['prod_loc_id'])&&$product_location_relationship[0]['prod_loc_id']!='')
		{
			return $product_location_relationship;
		}
		
	}
	
	function retrieveProductLocationRelationship($product_id,$location_id)
	{
		$query = "SELECT * FROM product_location_relationship WHERE product_id = '$product_id' AND location_id = '$location_id' ";
		$queryOb = new classQueryGlobal;
		$product_location_relationship = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($product_location_relationship[0]['prod_loc_id'])&&$product_location_relationship[0]['prod_loc_id']!='')
		{
			return $product_location_relationship;
		}
		
	}
	
	function retrieveProductSupplierDataByProductID()
	{
		$product_id = $this->getProductID();
		$query = "SELECT * FROM  product_supplier_relationship WHERE product_id = '$product_id'";
		$queryOb = new classQueryGlobal;
		$product_supplier_relationship = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($product_supplier_relationship[0])&&$product_supplier_relationship[0]!='')
		{
			return $product_supplier_relationship;
		}
		
	}
	
	function retrieveProductSupplierDataBySupplierID()
	{
		$supplier_id = $this->getSupplierID();
		$query = "SELECT product_supplier_relationship.product_id,supplier_id,box_tradeprice,leaf_tradeprice,unit_tradeprice FROM product_supplier_relationship JOIN products ON product_supplier_relationship.product_id = products.product_id WHERE product_supplier_relationship.supplier_id = '$supplier_id' AND products.active=1";
		$queryOb = new classQueryGlobal;
		$product_supplier_relationship = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($product_supplier_relationship[0])&&$product_supplier_relationship[0]!='')
		{
			return $product_supplier_relationship;
		}
		
	}
	
	function retrieveProductSupplierRelationship($product_id,$supplier_id)
	{
		$query = "SELECT * FROM  product_supplier_relationship WHERE product_id = '$product_id' AND supplier_id = '$supplier_id' ";
		$queryOb = new classQueryGlobal;
		$product_supplier_relationship = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($product_supplier_relationship[0]['relationship_id'])&&$product_supplier_relationship[0]['relationship_id']!='')
		{
			return $product_supplier_relationship;
		}
		
	}
	
	function retrieveSuppliers()
	{
		$location_id = $this->getLocationID();
		
		$query = "SELECT * FROM suppliers WHERE location_id = '$location_id' ORDER BY supplier_name ASC";
		$queryOb = new classQueryGlobal;
		$suppliers_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($suppliers_array[0])&&$suppliers_array[0]!='')
		{
			return $suppliers_array;
		}
		
	}
	
	function retrieveSupplierBySupplierID($supplier_id)
	{
		
		$query = "SELECT * FROM suppliers WHERE supplier_id = '$supplier_id'";
		$queryOb = new classQueryGlobal;
		$suppliers_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($suppliers_array[0])&&$suppliers_array[0]!='')
		{
			return $suppliers_array;
		}
		
	}
	
	function retrieveManufacturers()
	{
		$org_id = $this->getOrganizationID();
		$query = "SELECT * FROM manufacturers WHERE org_id = '$org_id'";
		$queryOb = new classQueryGlobal;
		$manufacturers_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($manufacturers_array[0])&&$manufacturers_array[0]!='')
		{
			return $manufacturers_array;
		}
		
	}
	
	function retrieveManufacturerByID()
	{
		$manf_id = $this->getManufacturerID();
		$query = "SELECT * FROM manufacturers WHERE manf_id = '$manf_id'";
		$queryOb = new classQueryGlobal;
		$manufacturers_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($manufacturers_array[0])&&$manufacturers_array[0]!='')
		{
			return $manufacturers_array;
		}
		
	}
	
}
?>