<?php
class classDiagnosticLiquids
{
	
	private $organizationID=NULL;
	private $userID=NULL;
	private $locationID=NULL;
	
	private $labTestID=NULL;
	
	private $liquidID=NULL;
	private $liquidName=NULL;
	private $amountPerBottle=NULL;
	private $pricePerBottle=NULL;
	private $qty=NULL;
	private $qtyUsedPerTest=NULL;
	
	private $liquidReceivedID=NULL;
	private $liquidReceivedDate=NULL;
	
	private $liquidStartDate=NULL;
	private $liquidResetDate=NULL;
	private $testPerformed=NULL;
	private $expectedQty=NULL;
	private $actualQty=NULL;
	
	
	################################# SET FUNCTIONS ############################################
	
	function setOrganizationID($organizationID)
	{
		if($organizationID!="")
		{
			$this->organizationID = $organizationID;
		}
		else
		{
			throw new MyException(117100);
		}
		
	}
	function setUserID($userID)
	{
		if($userID!='')
		{
			$this->userID = $userID;
		}
		else
		{
			throw new MyException(117101);
		}
	}
	function setLocationID($locationID)
	{
		if($locationID!='')
		{
			$this->locationID = $locationID;
		}
		else
		{
			throw new MyException(117102);
		}
	}
	
	function setLabTestID($labTestID) 
	{
		if($labTestID!="")
		{
			$this->labTestID = $labTestID;
		}
		else
		{
			throw new MyException(117103);
		}
	}
	
	function setLiquidID($liquidID) 
	{
		if($liquidID!="")
		{
			$this->liquidID = $liquidID;
		}
		else
		{
			throw new MyException(117104);
		}
	}
	function setLiquidName($liquidName) 
	{
		if($liquidName!="")
		{
			$this->liquidName = $liquidName;
		}
		else
		{
			throw new MyException(117105);
		}
	}
	function setAmountPerBottle($amountPerBottle) 
	{
		if($amountPerBottle!="")
		{
			if(is_numeric($amountPerBottle))
			{
				$this->amountPerBottle = $amountPerBottle;
			}
			else
			{
				throw new MyException(117106);
			}
		}
		else
		{
			throw new MyException(117107);
		}
	}
	function setPricePerBottle($pricePerBottle) 
	{
		if($pricePerBottle!="")
		{
			if(is_numeric($pricePerBottle))
			{
				$this->pricePerBottle = $pricePerBottle;
			}
			else
			{
				throw new MyException(117138);
			}
		}
		else
		{
			throw new MyException(117139);
		}
	}
	function setQty($qty) 
	{
		if($qty!="")
		{
			if(is_numeric($qty))
			{
				$this->qty = $qty;
			}
			else
			{
				throw new MyException(117108);
			}
		}
		else
		{
			throw new MyException(117109);
		}
	}
	function setQtyUsedPerTest($qtyUsedPerTest) 
	{
		if($qtyUsedPerTest!="")
		{
			if(is_numeric($qtyUsedPerTest))
			{
				$this->qtyUsedPerTest = $qtyUsedPerTest;
			}
			else
			{
				throw new MyException(117110);
			}
		}
		else
		{
			throw new MyException(117111);
		}
	}
	
	function setLiquidReceivedID($liquidReceivedID) 
	{
		if($liquidReceivedID!="")
		{
			$this->liquidReceivedID = $liquidReceivedID;
		}
		else
		{
			throw new MyException(117112);
		}
	}
	function setLiquidReceivedDate($liquidReceivedDate) 
	{
		if($liquidReceivedDate!="")
		{
			$this->liquidReceivedDate = $liquidReceivedDate;
		}
		else
		{
			throw new MyException(117113);
		}
	}
	
	function setLiquidStartDate($liquidStartDate) 
	{
		if($liquidStartDate!="")
		{
			$this->liquidStartDate = $liquidStartDate;
		}
		else
		{
			throw new MyException(117114);
		}
	}
	function setLiquidResetDate($liquidResetDate) 
	{
		if($liquidResetDate!="")
		{
			$this->liquidResetDate = $liquidResetDate;
		}
		else
		{
			throw new MyException(117115);
		}
	}
	function setTestPerformed($testPerformed) 
	{
		if($testPerformed!="")
		{
			$this->testPerformed = $testPerformed;
		}
		else
		{
			throw new MyException(117116);
		}
	}
	function setExpectedQty($expectedQty) 
	{
		if($expectedQty!="")
		{
			if(is_numeric($expectedQty))
			{
				$this->expectedQty = $expectedQty;
			}
			else
			{
				throw new MyException(117117);
			}
		}
		else
		{
			throw new MyException(117118);
		}
	}
	function setActualQty($actualQty) 
	{
		if($actualQty!="")
		{
			if(is_numeric($actualQty))
			{
				$this->actualQty = $actualQty;
			}
			else
			{
				throw new MyException(117119);
			}
		}
		else
		{
			throw new MyException(117120);
		}
	}
	
	################################# GET FUNCTIONS ############################################
	
	function getOrganizationID()
	{
		if($this->organizationID!="")
		{
			return $this->organizationID;
		}
		else
		{
			throw new MyException(117121);
		}
		
	}
	function getUserID()
	{
		if($this->userID!='')
		{
			return $this->userID;
		}
		else
		{
			throw new MyException(117122);
		}
	}
	function getLocationID() 
	{
		if($this->locationID!='')
		{
			return $this->locationID;
		}
		else
		{
			throw new MyException(117123);
		}
	}
	
	function getLabTestID() 
	{
		if($this->labTestID!="")
		{
			return $this->labTestID;
		}
		else
		{
			throw new MyException(117124);
		}
	}
	
	function getLiquidID() 
	{
		if($this->liquidID!="")
		{
			return $this->liquidID;
		}
		else
		{
			throw new MyException(117125);
		}
	}
	function getLiquidName() 
	{
		if($this->liquidName!="")
		{
			return $this->liquidName;
		}
		else
		{
			throw new MyException(117126);
		}
	}
	function getAmountPerBottle() 
	{
		if($this->amountPerBottle!="")
		{
			return $this->amountPerBottle;
		}
		else
		{
			throw new MyException(117127);
		}
	}
	function getPricePerBottle() 
	{
		if($this->pricePerBottle!="")
		{
			return $this->pricePerBottle;
		}
		else
		{
			throw new MyException(117140);
		}
	}
	function getQty() 
	{
		if($this->qty!="")
		{
			return $this->qty;
		}
		else
		{
			throw new MyException(117128);
		}
	}
	function getQtyUsedPerTest() 
	{
		if($this->qtyUsedPerTest!="")
		{
			return $this->qtyUsedPerTest;
		}
		else
		{
			throw new MyException(117129);
		}
	}
	
	function getLiquidReceivedID() 
	{
		if($this->liquidReceivedID!="")
		{
			return $this->liquidReceivedID;
		}
		else
		{
			throw new MyException(117130);
		}
	} 
	function getLiquidReceivedDate() 
	{
		if($this->liquidReceivedDate!="")
		{
			return $this->liquidReceivedDate;
		}
		else
		{
			throw new MyException(117131);
		}
	}
	
	function getLiquidStartDate() 
	{
		if($this->liquidStartDate!="")
		{
			return $this->liquidStartDate;
		}
		else
		{
			throw new MyException(117132);
		}
	}
	function getLiquidResetDate() 
	{
		if($this->liquidResetDate!="")
		{
			return $this->liquidResetDate;
		}
		else
		{
			throw new MyException(117133);
		}
	}
	function getTestPerformed() 
	{
		if($this->testPerformed!="")
		{
			return $this->testPerformed;
		}
		else
		{
			throw new MyException(117134);
		}
	}
	function getExpectedQty() 
	{
		if($this->expectedQty!="")
		{
			return $this->expectedQty;
		}
		else
		{
			throw new MyException(117135);
		}
	}
	function getActualQty() 
	{
		if($this->actualQty!="")
		{
			return $this->actualQty;
		}
		else
		{
			throw new MyException(117136);
		}
	}
	
	################################# MAIN CHECK FUNCTIONS ############################################
	
	function checkLiquidNameAlreadyExist($liquid_name,$location_id)
	{
		$liquid_name = strtolower($liquid_name);
		$query = "SELECT liquid_id FROM labtest_liquids WHERE location_id = '$location_id' AND liquid_name = '$liquid_name'";
		$queryOb = new classQueryGlobal;
		$labtest_liq_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($labtest_liq_array[0]['liquid_id'])&&$labtest_liq_array[0]['liquid_id']!='')
		{
			throw new MyException(117137);
		}
		else
		{
			return 1;
		}
	}
	
	function checkLabtestAlreadyExistInLiquid($liquid_id,$labtest_id)
	{
		$query = "SELECT relationship_id FROM labtest_liquid_relationship WHERE liquid_id = '$liquid_id' AND labtest_id = '$labtest_id'";
		$queryOb = new classQueryGlobal;
		$labtest_group_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($labtest_liq_array[0]['relationship_id'])&&$labtest_liq_array[0]['relationship_id']!='')
		{
			return 0;
		}
		else
		{
			return 1;
		}
	}
	
	function checkTestCanPerformWithLiquid($labtest_id,$location_id)
	{
		$query = "SELECT labtest_liquids.liquid_id 
		FROM 
		labtest_liquids JOIN labtest_liquid_relationship 
		ON 
		labtest_liquids.liquid_id = labtest_liquid_relationship.liquid_id 
		WHERE 
		labtest_liquids.location_id = '$location_id' 
		AND 
		labtest_liquid_relationship.labtest_id = '$labtest_id'";
		$queryOb = new classQueryGlobal;
		$liquid_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		
		if(isset($liquid_array[0]['liquid_id']))
		{
			$this->liquidID = $liquid_array[0]['liquid_id'];
			$detail_array = $this->retrieveSingleLiquidAllDetail();
			
			if(isset($detail_array[0]['remaining_stock_liquid'])&&isset($detail_array[0]['liq_req_per_test']))
			{
				if($detail_array[0]['remaining_stock_liquid']<$detail_array[0]['liq_req_per_test'])
				{
					return 0;
				}
				else
				{
					return 1;
				}
			}
			else
			{
				return 0;
			}
		}
		else
		{
			return 1;
		}
		
	}
	
	################################# MAIN INSERT FUNCTIONS ############################################
	
	function insertLiquid()
	{
		$liquid_name  = strtolower($this->getLiquidName());
		$amount_per_bottle = $this->getAmountPerBottle();
		$location_id = $this->getLocationID();
		$qty_used_per_test = $this->getQtyUsedPerTest();
		$price_per_bottle = $this->getPricePerBottle();
		$this->checkLiquidNameAlreadyExist($liquid_name,$location_id);
		
		$query = "INSERT INTO labtest_liquids (liquid_name,amount_per_bottle,location_id,qty_used_per_test,price_per_bottle) VALUES ('$liquid_name','$amount_per_bottle','$location_id','$qty_used_per_test','$price_per_bottle')";
		$queryOb = new classQueryGlobal;
		$this->liquidID = $queryOb->check_fetch_query($query,'insert',$server="",$debug_mode="off");
		
		$this->insertLiquidLabtestRelationship();
	}
	
	function insertLiquidLabtestRelationship()
	{
		$labtest_ids  = $this->getLabTestID();
		$liquid_id = $this->getLiquidID();
		
		$this->deleteLiquidLabtestRelationship();
		
		foreach($labtest_ids as $key => $val)
		{
			$labtest_id = $val;
			
			if($this->checkLabtestAlreadyExistInLiquid($liquid_id,$labtest_id))
			{
				$query = "INSERT INTO labtest_liquid_relationship (labtest_id,liquid_id) VALUES ('$labtest_id','$liquid_id')";
				$queryOb = new classQueryGlobal;
				$queryOb->check_fetch_query($query,'insert',$server="",$debug_mode="off");
			}
		}
		
		
	}
	
	function insertLiquidReceiving()
	{
		$liquid_id = $this->getLiquidID();
		$no_of_bottle = $this->getQty();
		$liqrec_date = time();
		$liqrec_by = $this->getUserID();
		
		$query = "INSERT INTO  labtest_liquids_received (liquid_id,no_of_bottle,liqrec_date,liqrec_by) VALUES ('$liquid_id','$no_of_bottle','$liqrec_date','$liqrec_by')";
		$queryOb = new classQueryGlobal;
		$this->liquidReceivedID = $queryOb->check_fetch_query($query,'insert',$server="",$debug_mode="off");
			
	}
	
	function resetLiquid()
	{
		$liquid_reset_details = $this->retrieveLiquidResetDetails();
		if(isset($liquid_reset_details[0]['reset_date'])&&$liquid_reset_details[0]['reset_date']!='')
		{
			$start_date = $liquid_reset_details[0]['reset_date'];
		}
		else
		{
			$start_date = time();
		}
		
		$reset_date = time();
		$liquid_id = $this->getLiquidID();
		$bottle_received = $this->getQty();
		$test_performed = $this->getTestPerformed();
		
		$actual_qty = $this->getActualQty();
		$expected_qty = $this->getExpectedQty();
		
		
		
		$query = "INSERT INTO  labtest_liquids_reset (liquid_id,start_date,reset_date,bottle_received,test_performed,actual_qty,expected_qty) VALUES ('$liquid_id','$start_date','$reset_date','$bottle_received','$test_performed','$actual_qty','$expected_qty')";
		$queryOb = new classQueryGlobal;
		$queryOb->check_fetch_query($query,'insert',$server="",$debug_mode="off");
			
	}
	
	################################# MAIN UPDATE FUNCTIONS #############################################
	
	function updateLiquid()
	{
		$liquid_name  = strtolower($this->getLiquidName());
		$amount_per_bottle = $this->getAmountPerBottle();
		$location_id = $this->getLocationID();
		$qty_used_per_test = $this->getQtyUsedPerTest();
		$liquid_id = $this->getLiquidID();
		$price_per_bottle = $this->getPricePerBottle();
		
		//$this->checkLiquidNameAlreadyExist($liquid_name,$location_id);
		
		$query = "UPDATE labtest_liquids SET liquid_name = '$liquid_name' , amount_per_bottle = '$amount_per_bottle' , location_id = '$location_id' , qty_used_per_test = '$qty_used_per_test' , price_per_bottle = '$price_per_bottle' WHERE liquid_id = '$liquid_id'";
		$queryOb = new classQueryGlobal;
		$queryOb->check_fetch_query($query,'update',$server="",$debug_mode="off");
		
		$this->insertLiquidLabtestRelationship();
	}
	
	
	################################# MAIN DELETE FUNCTIONS #############################################
	
	function deleteLiquidLabtestRelationship()
	{
		$liquid_id = $this->getLiquidID();
		
		$query = "DELETE FROM labtest_liquid_relationship WHERE liquid_id = '$liquid_id'";
		$queryOb = new classQueryGlobal;
		$queryOb->check_fetch_query($query,'delete',$server="",$debug_mode="off");
	}
	
	################################# MAIN RETRIEVE FUNCTIONS ############################################
	
	function retrieveLiquids()
	{
		$location_id = $this->getLocationID();
		$query = "SELECT * FROM  labtest_liquids WHERE labtest_liquids.location_id = '$location_id'";
		$queryOb = new classQueryGlobal;
		$liquid_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($liquid_array[0])&&$liquid_array[0]!='')
		{
			return $liquid_array;
		}
	}
	
	function retrieveLiquidByLiquidID()
	{
		$liquid_id = $this->getLiquidID();
		$query = "SELECT * FROM  labtest_liquids WHERE liquid_id = '$liquid_id'";
		$queryOb = new classQueryGlobal;
		$liquid_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($liquid_array[0])&&$liquid_array[0]!='')
		{
			return $liquid_array;
		}
	}
	
	function retrieveLiquidAndLabtestRelationship()
	{
		$liquid_id = $this->getLiquidID();
		$query = "SELECT * FROM labtest_liquid_relationship WHERE liquid_id = '$liquid_id'";
		$queryOb = new classQueryGlobal;
		$labtest_liquid_relationship = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($labtest_liquid_relationship[0])&&$labtest_liquid_relationship[0]!='')
		{
			return $labtest_liquid_relationship;
		}
	}
	
	function retrieveLiquidResetDetails()
	{
		$liquid_id = $this->getLiquidID();
		$query = "SELECT * FROM  labtest_liquids_reset WHERE liquid_id = '$liquid_id' ORDER BY reset_date DESC";
		$queryOb = new classQueryGlobal;
		$liquid_reset_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($liquid_reset_array[0])&&$liquid_reset_array[0]!='')
		{
			return $liquid_reset_array;
		}
	}
	
	function retrieveLiquidReceivedDetails($liquid_id,$start_date=NULL,$end_date=NULL)
	{
		$query = "SELECT liqrec_id,liqrec_date,sum(no_of_bottle) FROM labtest_liquids_received WHERE liquid_id = '$liquid_id'";
		
		if($start_date!=""&&$end_date!="")
		{
			$startDateArray = explode('-',$start_date);
			$startTime = mktime($startDateArray[3],$startDateArray[4],$startDateArray[5],$startDateArray[1],$startDateArray[2],$startDateArray[0]);
			
			$endDateArray = explode('-',$end_date);
			$endTime = mktime(23,59,59,$endDateArray[1],$endDateArray[2],$endDateArray[0]);
			
			if($startTime!="")
			{
				if(($endTime!="" && $endTime > $startTime) || $endTime="")
				{
					$query .= " AND liqrec_date  > $startTime";
				}
			}
			
			if($endTime!="")
			{
				if(($startTime!="" && $startTime < $endTime) || $startTime="")
				{
					$query .= " AND liqrec_date  <= $endTime";
				}
			}
		}
		
		$queryOb = new classQueryGlobal;
		$liquid_rec_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($liquid_rec_array[0]['liqrec_id']))
		{
			return $liquid_rec_array;
		}
	}
	
	function retrieveSumOfTestPerformedByDiagnosisID($from=NULL,$to=NULL,$location_id,$diagnosis_type,$diagnosis_id)
	{
		$query = "SELECT count(diagitems_id) 
		FROM diagnostic_invoiceitems JOIN diagnostic_invoice 
		ON diagnostic_invoiceitems.diaginvoice_id = diagnostic_invoice.diaginvoice_id 
		WHERE diagnostic_invoice.location_id = '$location_id' 
		AND diagnostic_invoiceitems.diagnosis_type = '$diagnosis_type'  
		AND diagnostic_invoiceitems.diagnosis_id = '$diagnosis_id'  
		AND diagnostic_invoiceitems.diagnosis_result != ''";
		
		if($from!=""&&$to!="")
		{
			$startDateArray = explode('-',$from);
			$startTime = mktime($startDateArray[3],$startDateArray[4],$startDateArray[5],$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 diagnosis_result_time  > $startTime";
				}
			}
			
			if($endTime!="")
			{
				if(($startTime!="" && $startTime < $endTime) || $startTime="")
				{
					$query .= " AND diagnosis_result_time  <= $endTime";
				}
			}
		}
		
		$queryOb = new classQueryGlobal;
		$invoice_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		if(isset($invoice_array[0]['count(diagitems_id)'])&&$invoice_array[0]['count(diagitems_id)']!='')
		{
			return $invoice_array[0]['count(diagitems_id)'];
		}
		
	}
	
	function retrieveSumOfTestPerformedByLiquid($from=NULL,$to=NULL,$location_id,$diagnosis_type,$liquid_id)
	{
		$this->liquidID = $liquid_id;
		$relation_array = $this->retrieveLiquidAndLabtestRelationship();
		
		$labtest_array['no_of_test'] = 0;
		$labtest_array['labtest_names'] = '';
		$labtest_array['labtest_id'] = '';
		
		if(isset($relation_array[0]['relationship_id']))
		{
			for($r=0;$r<count($relation_array);$r++)
			{
				$labtest_array['no_of_test'] += $this->retrieveSumOfTestPerformedByDiagnosisID($from,$to,$location_id,$diagnosis_type,$relation_array[$r]['labtest_id']);
				$labtest_array['labtest_names'] .= '<img src="../images/bullet1.png" width="13" height="12" /><span style="font-size:10px; color:grey;">'.ucfirst($this->retrieveLabTestNameByLabTestID($relation_array[$r]['labtest_id'])).'</span><br>';
				$labtest_array['labtest_id'] .= $relation_array[$r]['labtest_id'].',';
			}
			
		}
		return $labtest_array;
	}
	
	function retrieveLabTestNameByLabTestID($labtest_id)
	{
		$query = "SELECT labtest_name FROM labtests WHERE labtest_id = '$labtest_id'";
		$queryOb = new classQueryGlobal;
		$labtest_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($labtest_array[0]['labtest_name']))
		{
			return $labtest_array[0]['labtest_name'];
		}
	}
	
	function retrieveLiquidAllDetail($from=NULL,$to=NULL)
	{
		$location_id = $this->getLocationID();
		$liquid_array = $this->retrieveLiquids();
		
		if(isset($liquid_array[0]['liquid_id']))
		{
			for($i=0;$i<count($liquid_array);$i++)
			{
				$this->liquidID = $liquid_array[$i]['liquid_id'];
				$liquid_reset_details = $this->retrieveLiquidResetDetails();
				
				if(isset($liquid_reset_details[0]['reset_id']))
				{
					$liquid_id = $liquid_reset_details[0]['liquid_id'];
					if(!is_null($from)&&!is_null($to))
					{
						$start_date = date('Y-m-d-G-i-s',strtotime($from));
						$end_date = $to;
					}
					else
					{
						$start_date = date('Y-m-d-G-i-s',$liquid_reset_details[0]['reset_date']);
						$end_date = date('Y-m-d',time());
					}
					
					
					$liquid_rec_array = $this->retrieveLiquidReceivedDetails($liquid_id,$start_date,$end_date);
					$labtest_array = $this->retrieveSumOfTestPerformedByLiquid($start_date,$end_date,$location_id,'labtest',$liquid_id);
							
					$liquid_array[$i]['liq_req_per_test'] = $liquid_array[$i]['amount_per_bottle']/$liquid_array[$i]['qty_used_per_test'];
					$liquid_array[$i]['no_of_test_performed'] = $labtest_array['no_of_test'];
					$liquid_array[$i]['labtest_names'] = $labtest_array['labtest_names'];
					$liquid_array[$i]['labtest_id'] = $labtest_array['labtest_id'];
					$liquid_array[$i]['reset_date'] = $liquid_reset_details[0]['reset_date'];
					
					if(isset($liquid_rec_array[0]['liqrec_id']))
					{
						$liquid_array[$i]['remaining_stock_liquid'] = ($liquid_rec_array[0]['sum(no_of_bottle)']*$liquid_array[$i]['amount_per_bottle'])-($liquid_array[$i]['no_of_test_performed']*$liquid_array[$i]['liq_req_per_test']);
						
						$liquid_array[$i]['remaining_stock_bottle'] = $liquid_array[$i]['remaining_stock_liquid']/$liquid_array[$i]['amount_per_bottle'];
						$liquid_array[$i]['no_of_bottle_rec'] = $liquid_rec_array[0]['sum(no_of_bottle)'];
						
						$liquid_array[$i]['total_price'] = $liquid_rec_array[0]['sum(no_of_bottle)']*$liquid_array[$i]['price_per_bottle'];
					}
				}
				else
				{
					if(!is_null($from)&&!is_null($to))
					{
						$start_date = date('Y-m-d-G-i-s',strtotime($from));
						$end_date = $to;
					}
					else
					{
						$start_date = '';
						$end_date = '';
					}
					$liquid_id = $liquid_array[$i]['liquid_id'];
					
					$liquid_rec_array = $this->retrieveLiquidReceivedDetails($liquid_id,$start_date,$end_date);
					$labtest_array = $this->retrieveSumOfTestPerformedByLiquid($start_date,$end_date,$location_id,'labtest',$liquid_id);
					
					$liquid_array[$i]['liq_req_per_test'] = $liquid_array[$i]['amount_per_bottle']/$liquid_array[$i]['qty_used_per_test'];
					$liquid_array[$i]['no_of_test_performed'] = $labtest_array['no_of_test'];
					$liquid_array[$i]['labtest_names'] = $labtest_array['labtest_names'];
					$liquid_array[$i]['labtest_id'] = $labtest_array['labtest_id'];
					
					
					if(isset($liquid_rec_array[0]['liqrec_id']))
					{
							
						$liquid_array[$i]['remaining_stock_liquid'] = ($liquid_rec_array[0]['sum(no_of_bottle)']*$liquid_array[$i]['amount_per_bottle'])-($liquid_array[$i]['no_of_test_performed']*$liquid_array[$i]['liq_req_per_test']);
						
						$liquid_array[$i]['remaining_stock_bottle'] = $liquid_array[$i]['remaining_stock_liquid']/$liquid_array[$i]['amount_per_bottle'];
						$liquid_array[$i]['no_of_bottle_rec'] = $liquid_rec_array[0]['sum(no_of_bottle)'];
						
						$liquid_array[$i]['total_price'] = $liquid_rec_array[0]['sum(no_of_bottle)']*$liquid_array[$i]['price_per_bottle'];
					}
				}
				
			}
			return $liquid_array;
			
		}
	}
	
	function retrieveSingleLiquidAllDetail()
	{
		$liquid_id = $this->getLiquidID();
		$liquid_array = $this->retrieveLiquidByLiquidID();
		$location_id = $liquid_array[0]['location_id'];
		if(isset($liquid_array[0]['liquid_id']))
		{
			for($i=0;$i<count($liquid_array);$i++)
			{
				$this->liquidID = $liquid_array[$i]['liquid_id'];
				$liquid_reset_details = $this->retrieveLiquidResetDetails();
				
				if(isset($liquid_reset_details[0]['reset_id']))
				{
					$liquid_id = $liquid_reset_details[0]['liquid_id'];
					$start_date = date('Y-m-d-G-i-s',$liquid_reset_details[0]['reset_date']);
					$end_date = date('Y-m-d',time());
					
					$liquid_rec_array = $this->retrieveLiquidReceivedDetails($liquid_id,$start_date,$end_date);
					$labtest_array = $this->retrieveSumOfTestPerformedByLiquid($start_date,$end_date,$location_id,'labtest',$liquid_id);
							
					$liquid_array[$i]['liq_req_per_test'] = $liquid_array[$i]['amount_per_bottle']/$liquid_array[$i]['qty_used_per_test'];
					$liquid_array[$i]['no_of_test_performed'] = $labtest_array['no_of_test'];
					$liquid_array[$i]['labtest_names'] = $labtest_array['labtest_names'];
					$liquid_array[$i]['labtest_id'] = $labtest_array['labtest_id'];
					$liquid_array[$i]['reset_date'] = $liquid_reset_details[0]['reset_date'];
					if(isset($liquid_rec_array[0]['liqrec_id']))
					{
						$liquid_array[$i]['remaining_stock_liquid'] = ($liquid_rec_array[0]['sum(no_of_bottle)']*$liquid_array[$i]['amount_per_bottle'])-($liquid_array[$i]['no_of_test_performed']*$liquid_array[$i]['liq_req_per_test']);
						
						$liquid_array[$i]['remaining_stock_bottle'] = $liquid_array[$i]['remaining_stock_liquid']/$liquid_array[$i]['amount_per_bottle'];
						$liquid_array[$i]['no_of_bottle_rec'] = $liquid_rec_array[0]['sum(no_of_bottle)'];
						$liquid_array[$i]['total_price'] = $liquid_rec_array[0]['sum(no_of_bottle)']*$liquid_array[$i]['price_per_bottle'];
					}
		
				}
				else
				{
					$liquid_id = $liquid_array[$i]['liquid_id'];
					
					$liquid_rec_array = $this->retrieveLiquidReceivedDetails($liquid_id);
					$labtest_array = $this->retrieveSumOfTestPerformedByLiquid('','',$location_id,'labtest',$liquid_id);
					
					$liquid_array[$i]['liq_req_per_test'] = $liquid_array[$i]['amount_per_bottle']/$liquid_array[$i]['qty_used_per_test'];
					$liquid_array[$i]['no_of_test_performed'] = $labtest_array['no_of_test'];
					$liquid_array[$i]['labtest_names'] = $labtest_array['labtest_names'];
					$liquid_array[$i]['labtest_id'] = $labtest_array['labtest_id'];
					
					
					if(isset($liquid_rec_array[0]['liqrec_id']))
					{
							
						$liquid_array[$i]['remaining_stock_liquid'] = ($liquid_rec_array[0]['sum(no_of_bottle)']*$liquid_array[$i]['amount_per_bottle'])-($liquid_array[$i]['no_of_test_performed']*$liquid_array[$i]['liq_req_per_test']);
						
						$liquid_array[$i]['remaining_stock_bottle'] = $liquid_array[$i]['remaining_stock_liquid']/$liquid_array[$i]['amount_per_bottle'];
						$liquid_array[$i]['no_of_bottle_rec'] = $liquid_rec_array[0]['sum(no_of_bottle)'];
						$liquid_array[$i]['total_price'] = $liquid_rec_array[0]['sum(no_of_bottle)']*$liquid_array[$i]['price_per_bottle'];
					}
				}
				
			}
			
			return $liquid_array;
			
		}
	}
	
	
	
	
	
	
}
?>