<?php
class classStoreItemReceiveInvoice
{
	
	private $receiveInvoiceID=NULL;
	private $userID=NULL;
	private $locationID=NULL;
	
	private $receiveItemID=NULL;
	private $receiveItemDate=NULL;
	private $storeItemID=NULL;
	private $expiryDate=NULL;
	private $receivedQty=NULL;
	
	private $verifiedByName=NULL;
	private $verifiedByID=NULL;
	private $verifiedDate=NULL;
	private $verifiedStatus=NULL;
	
	
	################################# SET FUNCTIONS ############################################
	
	function setReceiveInvoiceID($receiveInvoiceID)
	{
		if($receiveInvoiceID!='')
		{
			$this->receiveInvoiceID = $receiveInvoiceID;
		}
		else
		{
			throw new MyException(120100);
		}
	}
	function setUserID($userID)
	{
		if($userID!='')
		{
			$this->userID = $userID;
		}
		else
		{
			throw new MyException(120101);
		}
	}
	function setLocationID($locationID)
	{
		if($locationID!='')
		{
			$this->locationID = $locationID;
		}
	}
	
	function setReceiveItemID($receiveItemID)
	{
		if($receiveItemID!='')
		{
			$this->receiveItemID = $receiveItemID;
		}
		else
		{
			throw new MyException(120102);
		}
	}
	function setReceiveItemDate($receiveItemDate)
	{
		if($receiveItemDate!='')
		{
			$this->receiveItemDate = $receiveItemDate;
		}
	}
	function setStoreItemID($storeItemID)
	{
		if($storeItemID!='')
		{
			$this->storeItemID = $storeItemID;
		}
		else
		{
			throw new MyException(120103);
		}
	}
	function setExpiryDate($expiryDate)
	{
		if($expiryDate!='')
		{
			$this->expiryDate = $expiryDate;
		}
	}
	function setReceivedQty($receivedQty)
	{
		if($receivedQty!='')
		{
			$this->receivedQty = $receivedQty;
		}
		else
		{
			throw new MyException(120104);
		}
	}
	
	function setVerifiedByName($verifiedByName)
	{
		if($verifiedByName!='')
		{
			$this->verifiedByName = $verifiedByName;
		}
		else
		{
			throw new MyException(120111);
		}
	}
	function setVerifiedByID($verifiedByID)
	{
		if($verifiedByID!='')
		{
			$this->verifiedByID = $verifiedByID;
		}
		else
		{
			throw new MyException(120112);
		}
	}
	function setVerifiedDate($verifiedDate)
	{
		if($verifiedDate!='')
		{
			$this->verifiedDate = $verifiedDate;
		}
	}
	function setVerifiedStatus($verifiedStatus)
	{
		if($verifiedStatus!='')
		{
			$this->verifiedStatus = $verifiedStatus;
		}
	}
	
	
	################################# GET FUNCTIONS ############################################
	
	function getReceiveInvoiceID()
	{
		if($this->receiveInvoiceID!='')
		{
			return $this->receiveInvoiceID;
		}
		else
		{
			throw new MyException(120105);
		}
	}
	function getUserID()
	{
		if($this->userID!='')
		{
			return $this->userID ;
		}
		else
		{
			throw new MyException(120106);
		}
	}
	function getLocationID()
	{
		if($this->locationID!='')
		{
			return $this->locationID ;
		}
	}
	
	function getReceiveItemID()
	{
		if($this->receiveItemID!='')
		{
			return $this->receiveItemID ;
		}
		else
		{
			throw new MyException(120107);
		}
	}
	function getReceiveItemDate()
	{
		if($this->receiveItemDate!='')
		{
			return $this->receiveItemDate;
		}
	}
	function getStoreItemID()
	{
		if($this->storeItemID!='')
		{
			return $this->storeItemID;
		}
		else
		{
			throw new MyException(120108);
		}
	}
	function getExpiryDate()
	{
		if($this->expiryDate!='')
		{
			return $this->expiryDate;
		}
	}
	function getReceivedQty()
	{
		if($this->receivedQty!='')
		{
			return $this->receivedQty ;
		}
		else
		{
			throw new MyException(120109);
		}
	}
	
	function getVerifiedByName()
	{
		if($this->verifiedByName!='')
		{
			return $this->verifiedByName ;
		}
		else
		{
			throw new MyException(120113);
		}
	}
	function getVerifiedByID()
	{
		if($this->verifiedByID!='')
		{
			return $this->verifiedByID ;
		}
		else
		{
			throw new MyException(120114);
		}
	}
	function getVerifiedDate()
	{
		if($this->verifiedDate!='')
		{
			return $this->verifiedDate ;
		}
	}
	function getVerifiedStatus()
	{
		if($this->verifiedStatus!='')
		{
			return $this->verifiedStatus ;
		}
	}
	
	################################# MAIN CHECK FUNCTIONS ############################################
	
	function checkItemExistInReceiveInvoice($storeitem_id,$invoice_id)
	{
		$query = "SELECT recitems_id FROM store_items_rec WHERE storeitem_id = '$storeitem_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();
		
		$query = "INSERT INTO store_items_recinvoice (recinvoice_date,user_id,location_id) VALUES ('$recinvoice_date','$user_id','$location_id')";
		$queryOb = new classQueryGlobal;
		$this->receiveInvoiceID = $queryOb->check_fetch_query($query,'insert',$server="",$debug_mode="off");
	}
	
	function addReceiveItemsInInvoice()
	{
		if($this->checkItemExistInReceiveInvoice($this->getStoreItemID(),$this->getReceiveInvoiceID()))
		{
			$received_date  = time();
			$recinvoice_id = $this->getReceiveInvoiceID();
			$storeitem_id = $this->getStoreItemID();
			
			$query = "INSERT INTO store_items_rec (received_date,recinvoice_id,storeitem_id) VALUES ('$received_date','$recinvoice_id','$storeitem_id')";
			$queryOb = new classQueryGlobal;
			$this->receiveItemID = $queryOb->check_fetch_query($query,'insert',$server="",$debug_mode="off");
		}
		else
		{
			throw new MyException(120110);
		}
	}
	
	################################# MAIN UPDATE FUNCTIONS #############################################
	
	function updateReceiveItemsInInvoice()
	{
		$recitems_id = $this->getReceiveItemID();
		$expiry_date = strtotime($this->getExpiryDate());
		$storeitem_id = $this->getStoreItemID();
		$location_id = $this->getLocationID();
		$qty_received = $this->getReceivedQty();
		
		$singleItemOb = new classStoreItem;
		$qty_in_stock = $singleItemOb->retrieveQtyInStock($storeitem_id,$location_id);
		
		$old_qty = $this->retrieveQtyReceivedByItemID();
		
		$query = "UPDATE store_items_rec set 
		 qty_received='$qty_received',
		 expiry_date='$expiry_date'
		 WHERE recitems_id = '$recitems_id'";
		$queryOb = new classQueryGlobal;
		$queryOb->check_fetch_query($query,'update',$server="",$debug_mode="off");
		
		
		######## update unit quantity in stock ########
		
		$qty_in_stock = $qty_in_stock + $qty_received - $old_qty;
		
		$singleItemLocaOb = new classStoreItem;
		$singleItemLocaOb->setStoreItemID($storeitem_id);
		$singleItemLocaOb->setLocationID($location_id);
		$singleItemLocaOb->setStoreItemQtyInStock($qty_in_stock);
		$singleItemLocaOb->updateQtyInStock();
		
	}
	
	
	function verfiyReceiveInvoice()
	{
		$recinvoice_id = $this->getReceiveInvoiceID();
		$verified_by_name = $this->getVerifiedByName();
		$verified_by_id = $this->getVerifiedByID();
		$verified_date = time();
		$verified_status = 'verified';
		
		$query = "UPDATE store_items_recinvoice set 
		 verified_by_name='$verified_by_name',
		 verified_by_id='$verified_by_id',
		 verified_date='$verified_date',
		 verified_status='$verified_status'
		 WHERE recinvoice_id = '$recinvoice_id'";
		$queryOb = new classQueryGlobal;
		$queryOb->check_fetch_query($query,'update',$server="",$debug_mode="off");
		
	}
	
	################################# MAIN DELETE FUNCTIONS #############################################
	
	function deleteReceiveItemsInInvoice()
	{
		$recitems_id = $this->getReceiveItemID();
		$recitem_array = $this->retrieveInvoiceItemsByItemID();
		
		
		if(isset($recitem_array[0]['recitems_id'])&&$recitem_array[0]['recitems_id']!='') 
		{
			$storeitem_id = $recitem_array[0]['storeitem_id'];
			
			$this->receiveInvoiceID = $recitem_array[0]['recinvoice_id'];
			$invoice_info = $this->retrieveInvoiceByID();
			
			$location_id = $invoice_info[0]['location_id'];
			
			############## update product unit qty #########################
			

			$singleItemOb = new classStoreItem;
			$qty_in_stock = $singleItemOb->retrieveQtyInStock($storeitem_id,$location_id);
			
			$qty_in_stock = $qty_in_stock-$recitem_array[0]['qty_received'];
			
			$singleItemLocaOb = new classStoreItem;
			$singleItemLocaOb->setStoreItemID($storeitem_id);
			$singleItemLocaOb->setLocationID($location_id);
			$singleItemLocaOb->setStoreItemQtyInStock($qty_in_stock);
			$singleItemLocaOb->updateQtyInStock();
			
			############## Delete invoice items #########################
			
			$query = "DELETE FROM store_items_rec 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->retrieveInvoiceItems();
		for($di=0;$di<count($invoice_info_array);$di++)
		{
			$this->receiveItemID = $invoice_info_array[$di]['recitems_id'];
			$this->deleteReceiveItemsInInvoice();
		}
		############## Delete invoice #########################
		$query = "DELETE FROM store_items_recinvoice WHERE recinvoice_id='$recinvoice_id'";
		$queryOb = new classQueryGlobal;
		$queryOb->check_fetch_query($query,'delete',$server="",$debug_mode="off");
		
	}
	
	################################# MAIN RETRIEVE FUNCTIONS ############################################
	
	function retrieveCountAllInvoices()
	{
		$location_id = $this->getLocationID();
		
		$query = "SELECT count(*) FROM store_items_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]['count(*)'])&&$invoice_array[0]['count(*)']!='')
		{
			return $invoice_array[0]['count(*)'];
		}
		
	}
	function retrieveAllInvoices()
	{
		$location_id = $this->getLocationID();
		
		$query = "SELECT * FROM store_items_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 retrieveInvoiceByID()
	{
		$recinvoice_id = $this->getReceiveInvoiceID();
		
		$query = "SELECT * FROM store_items_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 retrieveInvoiceByInvoiceIDRanges($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  store_items_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 retrieveInvoiceByDate($by_date,$location_id)
	{
		$query = "SELECT * FROM store_items_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 countAllInvoicesItems()
	{
		$location_id = $this->getLocationID();
		
		$query = "SELECT count(*) as no_of_items FROM store_items_rec JOIN store_items_recinvoice ON store_items_rec.recinvoice_id = store_items_recinvoice.recinvoice_id WHERE location_id = '$location_id'";
		$queryOb = new classQueryGlobal;
		$invoice_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($invoice_array[0]['no_of_items'])&&$invoice_array[0]['no_of_items']!='')
		{
			return $invoice_array[0]['no_of_items'];
		}
		
	}
	function countAllInvoicesItemsByDate($by_date,$location_id)
	{
		$query = "SELECT count(*) as no_of_items FROM store_items_rec JOIN store_items_recinvoice ON store_items_rec.recinvoice_id = store_items_recinvoice.recinvoice_id 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]['no_of_items'])&&$invoice_array[0]['no_of_items']!='')
		{
			return $invoice_array[0]['no_of_items'];
		}
		
	}
	
	function retrieveInvoiceItems()
	{
		$recinvoice_id = $this->getReceiveInvoiceID();
		
		$query = "SELECT * FROM store_items_rec 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 retrieveInvoiceItemsAndQty()
	{
		$recinvoice_id = $this->getReceiveInvoiceID();
		
		$query = "SELECT storeitem_name , qty_received FROM store_items_rec JOIN store_items ON store_items_rec.storeitem_id = store_items.storeitem_id  WHERE recinvoice_id = '$recinvoice_id' ORDER BY storeitem_name DESC";
		$queryOb = new classQueryGlobal;
		$invoice_items_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($invoice_items_array[0])&&$invoice_items_array[0]!='')
		{
			$store_items_names='';
			$store_items_qty='';
			
			for($ii=0;$ii<count($invoice_items_array);$ii++)
			{
				if($invoice_items_array[$ii]['storeitem_name'])
				{
					$store_items_names .= ucwords($invoice_items_array[$ii]['storeitem_name']).'<br/>';
				}
				if($invoice_items_array[$ii]['qty_received'])
				{
					$store_items_qty .= ucwords($invoice_items_array[$ii]['qty_received']).'<br/>';
				}
				
			}
			$store_items[0]['store_items_names']=$store_items_names;
			$store_items[0]['store_items_qty']=$store_items_qty;
			
			return $store_items;
		}
		
	}
	function retrieveInvoiceItemsByItemID()
	{
		$recitems_id = $this->getReceiveItemID();
		
		$query = "SELECT * FROM store_items_rec 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 retrieveQtyReceivedByItemID()
	{
		$recitems_id = $this->getReceiveItemID();
		
		$query = "SELECT qty_received FROM store_items_rec 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]['qty_received'])&&$invoice_array[0]['qty_received']!='')
		{
			return $invoice_array[0]['qty_received'];
		}
		
	}
	
	function sumOfInvoiceItemsByStoreItemID($storeitem_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(qty_received) FROM store_items_rec JOIN store_items_recinvoice ON  store_items_rec.recinvoice_id = store_items_recinvoice.recinvoice_id WHERE store_items_rec.storeitem_id = '$storeitem_id' AND store_items_recinvoice.location_id = '$location_id'"; 
		
		if($startTime!="")
		{
			if(($endTime!="" && $endTime > $startTime) || $endTime="")
			{
				$query .= " AND store_items_rec.received_date  >= $startTime";
			}
		}
		
		if($endTime!="")
		{
			if(($startTime!="" && $startTime < $endTime) || $startTime="")
			{
				$query .= " AND store_items_rec.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;
		}
	}
	
	
	################################# MAIN RETRIEVE REPORTS FUNCTIONS ############################################
	
	function itemsReceivesReport($from,$to,$location_id)
	{
		$query = "select storeitem_id, sum(qty_recieved) from  store_items_rec JOIN store_items_recinvoice ON  store_items_rec.recinvoice_id = store_items_recinvoice.recinvoice_id WHERE store_items_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 storeitem_id order by storeitem_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;
		//}
	}
	
	
	
	
	
	
	
}
?>