<?php
class classReceiveInvoice
{
	
	private $receiveInvoiceID=NULL;
	private $userID=NULL;
	private $locationID=NULL;
	private $supplierID=NULL;
	private $receiveInvoiceAmount=NULL;
	private $discount=NULL;
	private $receiveInvoiceAmountPayable=NULL;
	private $payment=NULL;
	private $balance=NULL;
	private $paymentStatus=NULL;
	
	private $receiveItemID=NULL;
	private $receiveItemDate=NULL;
	private $productID=NULL;
	private $productExpiryDate=NULL;
	private $productTradePrice=NULL;
	private $productRetailPrice=NULL;
	private $packingType=NULL;
	private $receivedPackingQty=NULL;
	private $receivedUnitQty=NULL;
	private $receivedUnitOldQty=NULL;
	private $netTotal=NULL;
	
	private $refundStatus=NULL;
	private $refundQty=NULL;
	private $refundAmount=NULL;
	
	################################# SET FUNCTIONS ############################################
	
	function setReceiveInvoiceID($receiveInvoiceID)
	{
		if($receiveInvoiceID!='')
		{
			$this->receiveInvoiceID = $receiveInvoiceID;
		}
		else
		{
			throw new MyException(105100);
		}
	}
	function setUserID($userID)
	{
		if($userID!='')
		{
			$this->userID = $userID;
		}
		else
		{
			throw new MyException(105101);
		}
	}
	function setLocationID($locationID)
	{
		if($locationID!='')
		{
			$this->locationID = $locationID;
		}
		else
		{
			throw new MyException(105102);
		}
	}
	function setSupplierID($supplierID)
	{
		if($supplierID!='')
		{
			$this->supplierID = $supplierID;
		}
		else
		{
			throw new MyException(105103);
		}
	}
	function setReceiveInvoiceAmount($receiveInvoiceAmount)
	{
		if($receiveInvoiceAmount!='')
		{
			$this->receiveInvoiceAmount = $receiveInvoiceAmount;
		}
		else
		{
			throw new MyException(105104);
		}
	}
	function setDiscount($discount)
	{
		if($discount!='')
		{
			$this->discount = $discount;
		}
		else
		{
			throw new MyException(105105);
		}
	}
	function setReceiveInvoiceAmountPayable($receiveInvoiceAmountPayable)
	{
		if($receiveInvoiceAmountPayable!='')
		{
			$this->receiveInvoiceAmountPayable = $receiveInvoiceAmountPayable;
		}
		else
		{
			throw new MyException(105106);
		}
	}
	function setPayment($payment)
	{
		if($payment!='')
		{
			$this->payment = $payment;
		}
		else
		{
			throw new MyException(105107);
		}
	}
	function setBalance($balance)
	{
		if($balance!='')
		{
			$this->balance = $balance;
		}
		else
		{
			//throw new MyException(105108);
		}
	}
	function setPaymentStatus($paymentStatus)
	{
		if($paymentStatus!='')
		{
			$this->paymentStatus = $paymentStatus;
		}
		else
		{
			throw new MyException(105109);
		}
	}
	
	
	function setReceiveItemID($receiveItemID)
	{
		if($receiveItemID!='')
		{
			$this->receiveItemID = $receiveItemID;
		}
		else
		{
			throw new MyException(105110);
		}
	}
	function setReceiveItemDate($receiveItemDate)
	{
		if($receiveItemDate!='')
		{
			$this->receiveItemDate = $receiveItemDate;
		}
		else
		{
			throw new MyException(105111);
		}
	}
	function setProductID($productID)
	{
		if($productID!='')
		{
			$this->productID = $productID;
		}
		else
		{
			throw new MyException(105112);
		}
	}
	function setProductExpiryDate($productExpiryDate)
	{
		if($productExpiryDate!='')
		{
			$this->productExpiryDate = $productExpiryDate;
		}
		else
		{
			//throw new MyException(105113);
		}
	}
	function setProductTradePrice($productTradePrice)
	{
		if($productTradePrice!='')
		{
			$this->productTradePrice = $productTradePrice;
		}
		else
		{
			throw new MyException(105114);
		}
	}
	function setProductRetailPrice($productRetailPrice)
	{
		if($productRetailPrice!='')
		{
			$this->productRetailPrice = $productRetailPrice;
		}
		else
		{
			throw new MyException(105115);
		}
	}
	function setPackingType($packingType)
	{
		if($packingType!='')
		{
			$this->packingType = $packingType;
		}
		else
		{
			throw new MyException(105116);
		}
	}
	function setReceivedPackingQty($receivedPackingQty)
	{
		if($receivedPackingQty!='')
		{
			if($receivedPackingQty==0)
			{
				$this->receivedPackingQty = $receivedPackingQty;
			}
			else
			{
				if(classFilter::validateInteger($receivedPackingQty))
				{
					$this->receivedPackingQty = $receivedPackingQty;
				}
				else
				{
					throw new MyException(105147);	
				}
			}
		}
		else
		{
			throw new MyException(105117);
		}
	}
	
	function setReceivedUnitQty($receivedUnitQty)
	{
		if($receivedUnitQty!='')
		{
			$this->receivedUnitQty = $receivedUnitQty;
		}
		else
		{
			throw new MyException(105118);
		}
	}
	
	function setReceivedUnitOldQty($receivedUnitOldQty)
	{
		if($receivedUnitOldQty!='')
		{
			$this->receivedUnitOldQty = $receivedUnitOldQty;
		}
	}
	
	function setNetTotal($netTotal)
	{
		if($netTotal!='')
		{
			$this->netTotal = $netTotal;
		}
		else
		{
			throw new MyException(105119);
		}
	}
	
	function setRefundStatus($refundStatus)
	{
		if($refundStatus!='')
		{
			$this->refundStatus = $refundStatus;
		}
		else
		{
			throw new MyException(105120);
		}
	}
	function setRefundQty($refundQty)
	{
		if($refundQty!='')
		{
			$this->refundQty = $refundQty;
		}
		else
		{
			throw new MyException(105121);
		}
	}
	function setRefundAmount($refundAmount)
	{
		if($refundAmount!='')
		{
			$this->refundAmount = $refundAmount;
		}
		else
		{
			throw new MyException(105122);
		}
	}
	
	
	################################# GET FUNCTIONS ############################################
	
	function getReceiveInvoiceID()
	{
		if($this->receiveInvoiceID!='')
		{
			return $this->receiveInvoiceID;
		}
		else
		{
			throw new MyException(105123);
		}
	}
	function getUserID()
	{
		if($this->userID!='')
		{
			return $this->userID;
		}
		else
		{
			throw new MyException(105124);
		}
	}
	function getLocationID()
	{
		if($this->locationID!='')
		{
			return $this->locationID;
		}
		else
		{
			throw new MyException(105125);
		}
	}
	function getSupplierID()
	{
		if($this->supplierID!='')
		{
			return $this->supplierID;
		}
		else
		{
			throw new MyException(105126);
		}
	}
	function getReceiveInvoiceAmount()
	{
		if($this->receiveInvoiceAmount!='')
		{
			return $this->receiveInvoiceAmount;
		}
		else
		{
			throw new MyException(105127);
		}
	}
	function getDiscount()
	{
		if($this->discount!='')
		{
			return $this->discount;
		}
		else
		{
			throw new MyException(105128);
		}
	}
	function getReceiveInvoiceAmountPayable()
	{
		if($this->receiveInvoiceAmountPayable!='')
		{
			return $this->receiveInvoiceAmountPayable;
		}
		else
		{
			throw new MyException(105129);
		}
	}
	function getPayment()
	{
		if($this->payment!='')
		{
			return $this->payment;
		}
		else
		{
			throw new MyException(105130);
		}
	}
	function getBalance()
	{
		if($this->balance!='')
		{
			return $this->balance;
		}
		else
		{
			//throw new MyException(105131);
		}
	}
	function getPaymentStatus()
	{
		if($this->paymentStatus!='')
		{
			return $this->paymentStatus;
		}
		else
		{
			throw new MyException(105132);
		}
	}
	
	
	function getReceiveItemID()
	{
		if($this->receiveItemID!='')
		{
			return $this->receiveItemID;
		}
		else
		{
			throw new MyException(105133);
		}
	}
	function getReceiveItemDate()
	{
		if($this->receiveItemDate!='')
		{
			return $this->receiveItemDate;
		}
		else
		{
			throw new MyException(105134);
		}
	}
	function getProductID()
	{
		if($this->productID!='')
		{
			return $this->productID;
		}
		else
		{
			throw new MyException(105135);
		}
	}
	function getProductExpiryDate()
	{
		if($this->productExpiryDate!='')
		{
			return $this->productExpiryDate;
		}
		else
		{
			//throw new MyException(105136);
		}
	}
	function getProductTradePrice()
	{
		if($this->productTradePrice!='')
		{
			return $this->productTradePrice;
		}
		else
		{
			throw new MyException(105137);
		}
	}
	function getProductRetailPrice()
	{
		if($this->productRetailPrice!='')
		{
			return $this->productRetailPrice;
		}
		else
		{
			throw new MyException(105138);
		}
	}
	function getPackingType()
	{
		if($this->packingType!='')
		{
			return $this->packingType;
		}
		else
		{
			throw new MyException(105139);
		}
	}
	function getReceivedPackingQty()
	{
		if($this->receivedPackingQty!='')
		{
			return $this->receivedPackingQty;
		}
		else
		{
			throw new MyException(105140);
		}
	}
	function getReceivedUnitQty()
	{
		if($this->receivedUnitQty!='')
		{
			return $this->receivedUnitQty;
		}
		else
		{
			throw new MyException(105141);
		}
	}
	
	function getReceivedUnitOldQty()
	{
		if($this->receivedUnitOldQty!='')
		{
			return $this->receivedUnitOldQty;
		}
	}
	
	function getNetTotal()
	{
		if($this->netTotal!='')
		{
			return $this->netTotal;
		}
		else
		{
			throw new MyException(105142);
		}
	}

	function getRefundStatus()
	{
		if($this->refundStatus!='')
		{
			return $this->refundStatus;
		}
		else

		{
			throw new MyException(105143);
		}
	}
	function getRefundQty()
	{
		if($this->refundQty!='')
		{
			return $this->refundQty;
		}
		else
		{
			throw new MyException(105144);
		}
	}
	function getRefundAmount()
	{
		if($this->refundAmount!='')
		{
			return $this->refundAmount;
		}
		else
		{
			throw new MyException(105145);
		}
	}
	
	################################# MAIN CHECK FUNCTIONS ############################################
	
	function checkProductExistInReceiveInvoice($product_id,$invoice_id)
	{
		$query = "SELECT recitems_id FROM  product_recitems WHERE product_id = '$product_id' AND recinvoice_id = '$invoice_id 	' ";
		$queryOb = new classQueryGlobal;
		$product_invoice_relationship = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($product_invoice_relationship[0]['recitems_id'])&&$product_invoice_relationship[0]['recitems_id']!='')
		{
			return 0;
		}
		else
		{
			return 1;
		}
		
	}
	
	################################# MAIN INSERT FUNCTIONS ############################################
	
	function createReceiveInvoice()
	{
		$recinvoice_date  = time();
		$user_id = $this->getUserID();
		$location_id = $this->getLocationID();
		$supplier_id = $this->getSupplierID();
		
		$query = "INSERT INTO product_recinvoice (recinvoice_date,user_id,location_id,supplier_id) VALUES ('$recinvoice_date','$user_id','$location_id','$supplier_id')";
		$queryOb = new classQueryGlobal;
		$this->receiveInvoiceID = $queryOb->check_fetch_query($query,'insert',$server="",$debug_mode="off");
	}
	
	function addReceiveItemsInInvoice()
	{
		if($this->checkProductExistInReceiveInvoice($this->getProductID(),$this->getReceiveInvoiceID()))
		{
			$received_date  = time();
			$recinvoice_id = $this->getReceiveInvoiceID();
			$product_id = $this->getProductID();
			
			$query = "INSERT INTO product_recitems (received_date,recinvoice_id,product_id) VALUES ('$received_date','$recinvoice_id','$product_id')";
			$queryOb = new classQueryGlobal;
			$this->receiveItemID = $queryOb->check_fetch_query($query,'insert',$server="",$debug_mode="off");
		}
		else
		{
			throw new MyException(105146);
		}
	}
	
	################################# MAIN UPDATE FUNCTIONS #############################################
	
	function updateReceiveInvoice()
	{
		$recinvoice_id = $this->getReceiveInvoiceID();
		$discount = $this->getDiscount();
		$invoice_amount = $this->getPayment();
		if($discount!='' && $discount!='0')
		{		
			$amount_payable = $invoice_amount-($invoice_amount*$discount/100);
		}
		else
		{
			$amount_payable = $invoice_amount;
		}
		
		$payment = 0;
		$balance = $amount_payable-$payment;
		
		
		$query = "UPDATE product_recinvoice SET 
		 invoice_amount='$invoice_amount',
		 discount='$discount',
		 amount_payable='$amount_payable',
		 payment = '$payment',
		 balance = '$balance'
		 where recinvoice_id = '$recinvoice_id'";
		$queryOb = new classQueryGlobal;
		$queryOb->check_fetch_query($query,'update',$server="",$debug_mode="off");
	}

	function updateReceiveInvoiceAfterTransaction()
	{
		$recinvoice_id = $this->getReceiveInvoiceID();
		$payment_status = $this->getPaymentStatus();
		$payment = $this->getPayment();
		$balance = $this->getBalance();
		
		
		$query = "UPDATE product_recinvoice SET 
		 payment_status='$payment_status',
		 payment = '$payment',
		 balance = '$balance'
		 where recinvoice_id = '$recinvoice_id'";
		$queryOb = new classQueryGlobal;
		$queryOb->check_fetch_query($query,'update',$server="",$debug_mode="off");
	}
	
	function updateReceiveItemsInInvoice()
	{
		$packing_type = $this->getPackingType();
		$packing_qty = $this->getReceivedPackingQty();
		$discount = $this->getDiscount();
		$recitems_id = $this->getReceiveItemID();
		$expiry_date = strtotime($this->getProductExpiryDate());
		$product_id = $this->getProductID();
		$supplier_id = $this->getSupplierID();
		$location_id = $this->getLocationID();
		
		######## retrieve product trade price and unit quantity according to location detail ########
		
		$singleProdOb = new classProduct;
		$single_prod_detail = $singleProdOb->retrieveProductSupplierRelationship($product_id,$supplier_id);
		$prod_loc_relationship = $singleProdOb->retrieveProductLocationRelationship($product_id,$location_id);
		$unit_qtyinstock = $prod_loc_relationship[0]['unit_qtyinstock'];
		
		######## retrieve product detail ########
		
		$singleProdOb->setProductID($product_id);
		$prod_detail = $singleProdOb->retrieveProductByProductID();
		
		######## retrieve old unit quantity ########
		
		$single_item_detail = $this->retrieveInvoiceItemsDetailByItemID();
		$unitold_qty = $single_item_detail[0]['unitqty_recieved'];
		
		######## calculate unit quantity ########
		
		if($packing_type=='Unit/Tablets') 
		{
			$trade_price = $single_prod_detail[0]['unit_tradeprice'];
			
			$unittrade_price = $single_prod_detail[0]['unit_tradeprice'];
			
			$total = $trade_price*$packing_qty;
			$net_total = $total-($total*$discount/100);
			
			$unitqty_recieved = $packing_qty;
			$retail_price = $prod_loc_relationship[0]['unit_retailprice'];
		} 
		elseif($packing_type=='Leaf') 
		{
			$trade_price = $single_prod_detail[0]['leaf_tradeprice'];
			
			$unittrade_price = $single_prod_detail[0]['unit_tradeprice'];
			
			$total = $trade_price*$packing_qty;
			$net_total = $total-($total*$discount/100);
			
			$unitqty_recieved= $packing_qty*($prod_detail[0]['unitperbox']/$prod_detail[0]['leafperbox']);
			$retail_price = $prod_loc_relationship[0]['leaf_retailprice'];
		} 
		elseif($packing_type=='Box/Pack') 
		{
			$trade_price = $single_prod_detail[0]['box_tradeprice'];
			
			$unittrade_price = $single_prod_detail[0]['unit_tradeprice'];
			
			$total = $trade_price*$packing_qty;
			$net_total = $total-($total*$discount/100);
			
			$unitqty_recieved= $packing_qty*($prod_detail[0]['unitperbox']);
			$retail_price = $prod_loc_relationship[0]['box_retailprice'];
		}
		else
		{
			$trade_price="0";
			$total="0";
			$net_total ="0";
			$unitqty_recieved = "0";
		}
		
		
	
		
		$query = "UPDATE product_recitems set 
		 packing_type='$packing_type',
		 qty_received='$packing_qty',
		 unitqty_recieved = '$unitqty_recieved',
		 discount='$discount',
		 trade_price='$trade_price',
		 unittrade_price = '$unittrade_price',
		 retail_price = '$retail_price',
		 net_total='$net_total',
		 expiry_date='$expiry_date'
		 WHERE recitems_id = '$recitems_id'";
		$queryOb = new classQueryGlobal;
		$queryOb->check_fetch_query($query,'update',$server="",$debug_mode="off");
		
		######## update payemts in receive invoice ########
		$this->payment = $this->retrieveInvoiceItemsTotal();
		$invoice_info = $this->retrieveInvoiceDetail();
		$discount = $invoice_info[0]['discount'];
		if($discount==0)
		{
			$this->discount = '0';
		}
		else
		{
			$this->discount = $discount;
		}
		$this->updateReceiveInvoice();
		
		######## update unit quantity in stock ########
		$unit_qtyinstock = $unit_qtyinstock + $unitqty_recieved - $unitold_qty;
		
		$singleProdLocaOb = new classProduct;
		$singleProdLocaOb->setProductID($product_id);
		$singleProdLocaOb->setLocationID($location_id);
		$singleProdLocaOb->setProductUnitQtyInStock($unit_qtyinstock);
		$singleProdLocaOb->updateProductUnitQtyInStock();
		
	}
	
	function refundReceiveItemsInInvoice()
	{
		$recitems_id = $this->getReceiveItemID();
		$product_id = $this->getProductID();
		$refund_qty = $this->getRefundQty();
		$supplier_id = $this->getSupplierID();
		$location_id = $this->getLocationID();
		
		######## retrieve item detail ########
		$single_item_detail = $this->retrieveInvoiceItemsDetailByItemID();
		$recinvoice_id = $single_item_detail[0]['recinvoice_id'];
		$discount = $single_item_detail[0]['discount'];
		$packing_type = $single_item_detail[0]['packing_type'];
		$available_qty = $single_item_detail[0]['qty_received']-$single_item_detail[0]['refund_qty'];
		$total = $single_item_detail[0]['trade_price']*$refund_qty;
		
		$refund_amount = $total-($total*$discount/100);
		
		if($refund_qty>0&&$refund_qty<=$available_qty)
		{
			$query = "UPDATE product_recitems SET refund_status = 'refund' , refund_qty = refund_qty + '$refund_qty' , refund_amount = refund_amount + '$refund_amount' where recitems_id='$recitems_id'";
			$queryOb = new classQueryGlobal;
			$queryOb->check_fetch_query($query,'update',$server="",$debug_mode="off");
			
			
			$singleProdOb = new classProduct;
			
			######## retrieve product unit quantity according to location detail ########
			
			$single_prod_detail = $singleProdOb->retrieveProductSupplierRelationship($product_id,$supplier_id);
			$prod_loc_relationship = $singleProdOb->retrieveProductLocationRelationship($product_id,$location_id);
			$unit_qtyinstock = $prod_loc_relationship[0]['unit_qtyinstock'];
			
			######## retrieve product detail ########
			
			$singleProdOb->setProductID($product_id);
			$prod_detail = $singleProdOb->retrieveProductByProductID();
			
			######## calculate unit quantity ########
			
			if($packing_type=='Unit/Tablets') 
			{
				$unitqty_recieved = $refund_qty;
			} 
			elseif($packing_type=='Leaf') 
			{
				$unitqty_recieved= $refund_qty*($prod_detail[0]['unitperbox']/$prod_detail[0]['leafperbox']);
			} 
			elseif($packing_type=='Box/Pack') 
			{
				$unitqty_recieved= $refund_qty*($prod_detail[0]['unitperbox']);
			}
			
			######## update unit quantity in stock ########
			
			$unit_qtyinstock = $unit_qtyinstock - $unitqty_recieved;
			
			$singleProdLocaOb = new classProduct;
			$singleProdLocaOb->setProductID($product_id);
			$singleProdLocaOb->setLocationID($location_id);
			$singleProdLocaOb->setProductUnitQtyInStock($unit_qtyinstock);
			$singleProdLocaOb->updateProductUnitQtyInStock();
			
			######## insert in refund items ########
			
			$date = time();
			$query = "INSERT INTO product_refunditems (refund_time,recinvoice_id,product_id,packing_type,qty,refund_amount) VALUES ('$date', '$recinvoice_id','$product_id','$packing_type','$refund_qty','$refund_amount')";
			$queryOb = new classQueryGlobal;
			$queryOb->check_fetch_query($query,'insert',$server="",$debug_mode="off");
			
		}
		else
		{
			throw new MyException(105148);
		}
		
		
		
		
	}
	
	################################# MAIN DELETE FUNCTIONS #############################################
	
	function deleteReceiveItemsInInvoice()
	{
		$recitems_id = $this->getReceiveItemID();
		$recitem_array = $this->retrieveInvoiceItemsDetailByItemID();
		
		
		if(isset($recitem_array[0]['recitems_id'])&&$recitem_array[0]['recitems_id']!='') 
		{
			$product_id = $recitem_array[0]['product_id'];
			
			$this->receiveInvoiceID = $recitem_array[0]['recinvoice_id'];
			$invoice_info = $this->retrieveInvoiceDetail();
			
			$location_id = $invoice_info[0]['location_id'];
			
			############## update product unit qty #########################
			

			$retrieveSingleProdOb = new classProduct;
			$prod_detail = $retrieveSingleProdOb->retrieveProductLocationRelationship($product_id,$location_id);
				
			$unitqty_recieved = $prod_detail[0]['unit_qtyinstock']-$recitem_array[0]['unitqty_recieved'];
			
			$updateSingleProdOb = new classProduct;
			$updateSingleProdOb->setProductID($product_id);
			$updateSingleProdOb->setLocationID($location_id);
			$updateSingleProdOb->setProductUnitQtyInStock($unitqty_recieved);
			$updateSingleProdOb->updateProductUnitQtyInStock();
			
			############## update invoice amount #########################
			
			
			$payment = $invoice_info[0]['invoice_amount']-$recitem_array[0]['net_total'];
			if($payment==0)
			{
				$this->payment = "0";
			}
			else
			{
				$this->payment = $payment;
			}
			
			if($invoice_info[0]['discount']!=0)
			{
				$discount = $invoice_info[0]['discount'];
			}
			else
			{
				$discount = "0";
			}
			
			$this->discount = $discount;
			$this->updateReceiveInvoice();
			
			
			############## Delete invoice items #########################
			$query = "DELETE FROM product_recitems WHERE recitems_id='$recitems_id'";
			$queryOb = new classQueryGlobal;
			$queryOb->check_fetch_query($query,'delete',$server="",$debug_mode="off");
		}
		
	}
	
	function deleteReceiveInvoice()
	{
		$recinvoice_id  = $this->getReceiveInvoiceID();
		$invoice_info_array = $this->retrieveInvoiceItemsDetail();
		for($di=0;$di<count($invoice_info_array);$di++)
		{
			$this->receiveItemID = $invoice_info_array[$di]['recitems_id'];
			$this->deleteReceiveItemsInInvoice();
		}
		############## Delete invoice #########################
		$query = "DELETE FROM product_recinvoice WHERE recinvoice_id='$recinvoice_id'";
		$queryOb = new classQueryGlobal;
		$queryOb->check_fetch_query($query,'delete',$server="",$debug_mode="off");
		
	}
	
	################################# MAIN RETRIEVE FUNCTIONS ############################################
	
	function retrieveInvoiceDetail()
	{
		$recinvoice_id = $this->getReceiveInvoiceID();
		
		$query = "SELECT * FROM product_recinvoice WHERE recinvoice_id = '$recinvoice_id'";
		$queryOb = new classQueryGlobal;
		$invoice_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($invoice_array[0])&&$invoice_array[0]!='')
		{
			return $invoice_array;
		}
		
	}
	
	function retrieveInvoiceDetailByInvoiceIDRanges($range,$location_id)
	{
		$range_array = explode('-',$range);
		if(isset($range_array[0])&&$range_array[0]!=''&&isset($range_array[1])&&$range_array[1]!='')
		{
			$min = $range_array[0];
			$max = $range_array[1];
		}
		else
		{
			$min = '';
			$max = '';
			$id = $range;
		}
		
		$query = "SELECT * FROM  product_recinvoice WHERE location_id = '$location_id'";
		
		if($min!=''&&$max!='')
		{
			$query .= " AND recinvoice_id BETWEEN $min and $max";
		}
		else
		{
			$query .= " AND recinvoice_id = '$id'";
		}
		
		$query .= " ORDER BY recinvoice_date DESC";
		
		$queryOb = new classQueryGlobal;
		$invoice_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		

		if(isset($invoice_array[0])&&$invoice_array[0]!='')
		{
			return $invoice_array;
		}
		
	}
	
	function retrieveInvoiceDetailByDate($by_date,$location_id)
	{
		$query = "SELECT * FROM product_recinvoice WHERE location_id = '$location_id'";
	
		$startDateArray = explode('-',$by_date);
		$startTime = mktime(0,0,0,$startDateArray[1],$startDateArray[2],$startDateArray[0]);
		
		$endDateArray = explode('-',$by_date);
		$endTime = mktime(23,59,59,$endDateArray[1],$endDateArray[2],$endDateArray[0]);
		
		if($startTime!="")
		{
			if(($endTime!="" && $endTime > $startTime) || $endTime="")
			{
				$query .= " AND recinvoice_date > $startTime";
			}
		}
		
		if($endTime!="")
		{
			if(($startTime!="" && $startTime < $endTime) || $startTime="")
			{
				$query .= " AND recinvoice_date <= $endTime";
			}
		}
		
		$queryOb = new classQueryGlobal;
		$invoice_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($invoice_array[0])&&$invoice_array[0]!='')
		{
			return $invoice_array;
		}
		
	}
	
	function retrieveInvoiceDetailByLocationID()
	{
		$location_id = $this->getLocationID();
		
		$query = "SELECT * FROM product_recinvoice WHERE location_id = '$location_id' ORDER BY recinvoice_date DESC";
		$queryOb = new classQueryGlobal;
		$invoice_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($invoice_array[0])&&$invoice_array[0]!='')
		{
			return $invoice_array;
		}
		
	}
	
	function retrieveInvoiceDetailBySupplierID()
	{
		$supplier_id = $this->getSupplierID();
		
		$query = "SELECT * FROM product_recinvoice WHERE supplier_id = '$supplier_id' ORDER BY recinvoice_date DESC";
		$queryOb = new classQueryGlobal;
		$invoice_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($invoice_array[0])&&$invoice_array[0]!='')
		{
			return $invoice_array;
		}
		
	}
	
	function retrieveInvoiceDetailBySupplierIDRanges($range,$location_id)
	{
		$range_array = explode('-',$range);
		if(isset($range_array[0])&&$range_array[0]!=''&&isset($range_array[1])&&$range_array[1]!='')
		{
			$min = $range_array[0];
			$max = $range_array[1];
		}
		else
		{
			$min = '';
			$max = '';
			$id = $range;
		}
		
		$query = "SELECT * FROM  product_recinvoice WHERE location_id = '$location_id'";
		
		if($min!=''&&$max!='')
		{
			$query .= " AND supplier_id BETWEEN $min and $max";
		}
		else
		{
			$query .= " AND supplier_id = '$id'";
		}
		
		$query .= " ORDER BY recinvoice_date DESC";
		
		$queryOb = new classQueryGlobal;
		$invoice_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		

		if(isset($invoice_array[0])&&$invoice_array[0]!='')
		{
			return $invoice_array;
		}
		
	}
	
	function retrieveInvoiceDetailHavingBalanceByDate($by_date,$location_id)
	{
		$query = "SELECT * FROM product_recinvoice WHERE location_id = '$location_id' AND balance != 0";
	
		$startDateArray = explode('-',$by_date);
		$startTime = mktime(0,0,0,$startDateArray[1],$startDateArray[2],$startDateArray[0]);
		
		$endDateArray = explode('-',$by_date);
		$endTime = mktime(23,59,59,$endDateArray[1],$endDateArray[2],$endDateArray[0]);
		
		if($startTime!="")
		{
			if(($endTime!="" && $endTime > $startTime) || $endTime="")
			{
				$query .= " AND recinvoice_date > $startTime";
			}
		}
		
		if($endTime!="")
		{
			if(($startTime!="" && $startTime < $endTime) || $startTime="")
			{
				$query .= " AND recinvoice_date <= $endTime";
			}
		}
		
		$queryOb = new classQueryGlobal;
		$invoice_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($invoice_array[0])&&$invoice_array[0]!='')
		{
			return $invoice_array;
		}
		
	}
	
	function retrieveInvoiceDetailHavingBalanceByLocationID()
	{
		$location_id = $this->getLocationID();
		
		$query = "SELECT * FROM product_recinvoice WHERE location_id = '$location_id' AND balance != 0 ORDER BY recinvoice_date DESC";
		$queryOb = new classQueryGlobal;
		$invoice_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($invoice_array[0])&&$invoice_array[0]!='')
		{
			return $invoice_array;
		}
		
	}
	
	function countInvoiceDetailPaidByLocationID()
	{
		$location_id = $this->getLocationID();
		
		$query = "SELECT count(*) FROM product_recinvoice WHERE location_id = '$location_id' AND payment_status ='paid'";
		$queryOb = new classQueryGlobal;
		$invoice_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($invoice_array[0]['count(*)'])&&$invoice_array[0]['count(*)']!='')
		{
			return $invoice_array[0]['count(*)'];
		}
		
	}
	
	
	function retrieveInvoiceDetailPaidByLocationID($limit=NULL)
	{
		$location_id = $this->getLocationID();
		
		$query = "SELECT * FROM product_recinvoice WHERE location_id = '$location_id' AND payment_status ='paid' ORDER BY recinvoice_date DESC";
		if(!is_null($limit))
		{
			$query .= " LIMIT $limit";
		}
		$queryOb = new classQueryGlobal;
		$invoice_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($invoice_array[0])&&$invoice_array[0]!='')
		{
			return $invoice_array;
		}
		
	}
	
	
	function retrieveInvoiceItemsDetail()
	{
		$recinvoice_id = $this->getReceiveInvoiceID();
		
		$query = "SELECT * FROM product_recitems WHERE recinvoice_id = '$recinvoice_id'";
		$queryOb = new classQueryGlobal;
		$invoice_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($invoice_array[0])&&$invoice_array[0]!='')
		{
			return $invoice_array;
		}
		
	}
	
	function retrieveInvoiceItemsTotal()
	{
		$recinvoice_id = $this->getReceiveInvoiceID();
		
		$query = "SELECT sum(net_total) FROM product_recitems WHERE recinvoice_id = '$recinvoice_id'";
		$queryOb = new classQueryGlobal;
		$invoice_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($invoice_array[0]['sum(net_total)'])&&$invoice_array[0]['sum(net_total)']!='')
		{
			return $invoice_array[0]['sum(net_total)'];
		}
		
	}
	
	function retrieveInvoiceItemsDetailByItemID()
	{
		$recitems_id = $this->getReceiveItemID();
		
		$query = "SELECT * FROM product_recitems WHERE recitems_id = '$recitems_id'";
		$queryOb = new classQueryGlobal;
		$invoice_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($invoice_array[0])&&$invoice_array[0]!='')
		{
			return $invoice_array;
		}
		
	}
	
	function retrieveSumOfInvoiceItemsByProductID($product_id,$location_id,$startTime,$endTime)
	{
		/*$month = date('m',$date);
		$year = date('Y',$date);
		$day = cal_days_in_month(CAL_GREGORIAN, $month, $year);
		
		$startTime = mktime(0,0,0,$month,1,$year);
		$endTime = mktime(23,59,59,$month,$day,$year);*/
		
		$query = "SELECT SUM(unitqty_recieved) , SUM(net_total) FROM product_recitems JOIN product_recinvoice ON  product_recitems.recinvoice_id = product_recinvoice.recinvoice_id WHERE product_recitems.product_id = '$product_id' AND product_recinvoice.location_id = '$location_id'"; 
		
		if($startTime!="")
		{
			if(($endTime!="" && $endTime > $startTime) || $endTime="")
			{
				$query .= " AND product_recitems.received_date  >= $startTime";
			}
		}
		
		if($endTime!="")
		{
			if(($startTime!="" && $startTime < $endTime) || $startTime="")
			{
				$query .= " AND product_recitems.received_date  <= $endTime";
			}
		}
		
		$queryOb = new classQueryGlobal;
		$invoice_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($invoice_array[0])&&$invoice_array[0]!='')
		{
			return $invoice_array;
		}
	}
	
	function retrieveSumOfInvoiceRefundItemsByProductID($product_id,$startTime,$endTime)
	{
		
		$query = "SELECT SUM(qty) , SUM(refund_amount) FROM product_refunditems  WHERE product_id = '$product_id' AND recinvoice_id!=0"; 
		
		if($startTime!="")
		{
			if(($endTime!="" && $endTime > $startTime) || $endTime="")
			{
				$query .= " AND refund_time  >= $startTime";
			}
		}
		
		if($endTime!="")
		{
			if(($startTime!="" && $startTime < $endTime) || $startTime="")
			{
				$query .= " AND refund_time  <= $endTime";
			}
		}
		
		$queryOb = new classQueryGlobal;
		$invoice_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($invoice_array[0])&&$invoice_array[0]!='')
		{
			return $invoice_array;
		}
	}
	
	function retrieveSumOfRefundItems()
	{
		$recinvoice_id = $this->getReceiveInvoiceID();
		$query = "SELECT sum(refund_amount) FROM product_recitems where recinvoice_id='$recinvoice_id'"; 
		$queryOb = new classQueryGlobal;
		$refund_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($refund_array[0]['sum(refund_amount)'])&&$refund_array[0]['sum(refund_amount)']!='')
		{
			return $refund_array[0]['sum(refund_amount)'];
		}
	}
	
	function retrieveSupplierIDOfInvoice($recinvoice_id)
	{
		$query = "SELECT supplier_id FROM product_recinvoice WHERE recinvoice_id = '$recinvoice_id'";
		$queryOb = new classQueryGlobal;
		$invoice_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($invoice_array[0]['supplier_id'])&&$invoice_array[0]['supplier_id']!='')
		{
			return $invoice_array[0]['supplier_id'];
		}
		
	}
	
	
	################################# MAIN RETRIEVE REPORTS FUNCTIONS ############################################
	
	function productsReceivesReport($from,$to,$location_id)
	{
		$query = "select product_id, sum(unitqty_recieved) , sum(net_total) , unittrade_price from  product_recitems JOIN product_recinvoice ON  product_recitems.recinvoice_id = product_recinvoice.recinvoice_id WHERE product_recinvoice.location_id = '$location_id'";
		
		$startDateArray = explode('-',$from);
		$startTime = mktime(0,0,0,$startDateArray[1],$startDateArray[2],$startDateArray[0]);
		
		$endDateArray = explode('-',$to);
		$endTime = mktime(23,59,59,$endDateArray[1],$endDateArray[2],$endDateArray[0]);
		
		if($startTime!="")
		{
			if(($endTime!="" && $endTime > $startTime) || $endTime="")
			{
				$query .= " AND received_date  > $startTime";
			}
		}
		
		if($endTime!="")
		{
			if(($startTime!="" && $startTime < $endTime) || $startTime="")
			{
				$query .= " AND received_date  <= $endTime";
			}
		}
		
		$query .= "  group by product_id order by product_id ASC ";
		
		$queryOb = new classQueryGlobal;
		$sale_report_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($sale_report_array[0])&&$sale_report_array[0]!='')
		{
			return $sale_report_array;
		}
	}
	
	function productsInvoiceTotalReceivesReport($from,$to,$location_id)
	{
		$query = "select sum(invoice_amount), sum(amount_payable),sum(payment),sum(balance) from product_recinvoice  WHERE location_id = '$location_id'";
		
		$startDateArray = explode('-',$from);
		$startTime = mktime(0,0,0,$startDateArray[1],$startDateArray[2],$startDateArray[0]);
		
		$endDateArray = explode('-',$to);
		$endTime = mktime(23,59,59,$endDateArray[1],$endDateArray[2],$endDateArray[0]);
		
		if($startTime!="")
		{
			if(($endTime!="" && $endTime > $startTime) || $endTime="")
			{
				$query .= " AND recinvoice_date  > $startTime";
			}
		}
		
		if($endTime!="")
		{
			if(($startTime!="" && $startTime < $endTime) || $startTime="")
			{
				$query .= " AND recinvoice_date  <= $endTime";
			}
		}
		
		//$query .= "  group by product_id order by product_id ASC ";
		
		$queryOb = new classQueryGlobal;
		$sale_report_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($sale_report_array[0])&&$sale_report_array[0]!='')
		{
			return $sale_report_array;
		}
	}
	
	function productsReceivesDetailReport($from,$to,$location_id)
	{
		$query = "select product_id, product_recitems.recinvoice_id, unittrade_price, unitqty_recieved, received_date , net_total from product_recitems JOIN product_recinvoice ON product_recitems.recinvoice_id = product_recinvoice.recinvoice_id WHERE product_recinvoice.location_id = '$location_id'";
		
		$startDateArray = explode('-',$from);
		$startTime = mktime(0,0,0,$startDateArray[1],$startDateArray[2],$startDateArray[0]);
		
		$endDateArray = explode('-',$to);
		$endTime = mktime(23,59,59,$endDateArray[1],$endDateArray[2],$endDateArray[0]);
		
		if($startTime!="")
		{
			if(($endTime!="" && $endTime > $startTime) || $endTime="")
			{
				$query .= " AND received_date  > $startTime";
			}
		}
		
		if($endTime!="")
		{
			if(($startTime!="" && $startTime < $endTime) || $startTime="")
			{
				$query .= " AND received_date  <= $endTime";
			}
		}
		
		$query .= "  order by product_id ASC ";
		
		$queryOb = new classQueryGlobal;
		$sale_report_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($sale_report_array[0])&&$sale_report_array[0]!='')
		{
			return $sale_report_array;
		}
	}
	
	function invoiceWiseReceivesReport($from,$to,$location_id)
	{
		$query = "select * from  product_recinvoice WHERE location_id = '$location_id'";
		
		$startDateArray = explode('-',$from);
		$startTime = mktime(0,0,0,$startDateArray[1],$startDateArray[2],$startDateArray[0]);
		
		$endDateArray = explode('-',$to);
		$endTime = mktime(23,59,59,$endDateArray[1],$endDateArray[2],$endDateArray[0]);
		
		if($startTime!="")
		{
			if(($endTime!="" && $endTime > $startTime) || $endTime="")
			{
				$query .= " AND  recinvoice_date > $startTime";
			}
		}
		
		if($endTime!="")
		{
			if(($startTime!="" && $startTime < $endTime) || $startTime="")
			{
				$query .= " AND  recinvoice_date  <= $endTime";
			}
		}
		
		$query .= "  order by recinvoice_date DESC ";
		
		$queryOb = new classQueryGlobal;
		$sale_report_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($sale_report_array[0])&&$sale_report_array[0]!='')
		{
			return $sale_report_array;
		}
	}
	
	function productsReceivesReportOfMonth($date,$location_id)
	{
		$query = "select product_id , sum(net_total) as receives_total , sum(unitqty_recieved) as receives_qty 
		
		from product_recitems r JOIN product_recinvoice inv ON r.recinvoice_id = inv.recinvoice_id 
		
		WHERE inv.location_id = '$location_id'";
		
		$startDateArray = explode('-',$date);
		
		$month = date('m', strtotime($startDateArray[0]));
		$year = $startDateArray[1];
		$day = cal_days_in_month(CAL_GREGORIAN, $month, $year);
		
		$startTime = mktime(0,0,0,$month,1,$year);
		$endTime = mktime(23,59,59,$month,$day,$year);
		
		if($startTime!="")
		{
			if(($endTime!="" && $endTime > $startTime) || $endTime="")
			{
				$query .= " AND received_date  >= $startTime";
			}
		}
		
		if($endTime!="")
		{
			if(($startTime!="" && $startTime < $endTime) || $startTime="")
			{
				$query .= " AND received_date  <= $endTime";
			}
		}
		
		
		$query .= " GROUP BY product_id ORDER BY product_id ASC ";
		
		$queryOb = new classQueryGlobal;
		$sale_report_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		//if(isset($sale_report_array[0])&&$sale_report_array[0]!='')
		//{
			return $sale_report_array;
		//}
	}
	
	function singleProductsReceivesDetailReport($from,$to,$location_id,$product_id)
	{
		$query = "select product_id, recitems_id, product_recitems.recinvoice_id, product_recinvoice.supplier_id ,  trade_price, unitqty_recieved, received_date from product_recitems JOIN product_recinvoice ON product_recitems.recinvoice_id = product_recinvoice.recinvoice_id WHERE product_id = '$product_id' AND product_recinvoice.location_id = '$location_id'";
		
		$startDateArray = explode('-',$from);
		$startTime = mktime(0,0,0,$startDateArray[1],$startDateArray[2],$startDateArray[0]);
		
		$endDateArray = explode('-',$to);
		$endTime = mktime(23,59,59,$endDateArray[1],$endDateArray[2],$endDateArray[0]);
		
		if($startTime!="")
		{
			if(($endTime!="" && $endTime > $startTime) || $endTime="")
			{
				$query .= " AND received_date  > $startTime";
			}
		}
		
		if($endTime!="")
		{
			if(($startTime!="" && $startTime < $endTime) || $startTime="")
			{
				$query .= " AND received_date  <= $endTime";
			}
		}
		
		$query .= "  order by received_date DESC ";
		
		$queryOb = new classQueryGlobal;
		$sale_report_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		//if(isset($sale_report_array[0])&&$sale_report_array[0]!='')
		//{
			return $sale_report_array;
		//}
	}
	
	
	
	
	
	
	
}
?>