<?php
class classDiagnostic
{
	
	private $organizationID=NULL;
	private $diagInvoiceID=NULL;
	private $userID=NULL;
	private $locationID=NULL;
	private $patientID=NULL;
	private $InvoiceAmount=NULL;
	private $discount=NULL;
	private $invoiceAmountPayable=NULL;
	private $payment=NULL;
	private $balance=NULL;
	private $paymentStatus=NULL;
	private $referringBy=NULL;
	private $referringTo=NULL;
	
	private $diagItemID=NULL;
	private $diagItemDate=NULL;
	private $diagnosisID=NULL;
	private $diagnosisType=NULL;
	private $diagnosisResult=NULL;
	private $diagnosisResultUnit=NULL;
	
	private $diagnosisSpecimenNo=NULL;
	
	private $price=NULL;
	private $netTotal=NULL;
	
	private $refundStatus=NULL;
	private $refundAmount=NULL;
	
	private $labTestID=NULL;
	private $labtestAbbrivation=NULL;
	private $labTestName=NULL;
	private $labtestType=NULL;
	private $labTestNormalValue =NULL;
	private $labTestUnit =NULL;
	private $labTestCost =NULL;
	private $labTestNotes =NULL;
	
	private $labTestGroupID=NULL;
	private $labTestGroupName=NULL;
	private $labTestGroupCost=NULL;
	
	
	private $procedureID=NULL;
	private $procedureName=NULL;
	private $procedureType=NULL;
	private $procedureCost=NULL;
	
	private $shiftID=NULL;
	private $visitID=NULL;
	
	private $bloodCrossMatchID=NULL;
	
	private $donorName=NULL;
	private $bloodGroup=NULL;
	
	private $hepatitisBAntigen=NULL;
	private $hepatitisCAntigen=NULL;
	private $hiv=NULL;
	private $mp=NULL;
	private $vdrl=NULL;
	
	private $recipientName=NULL;
	private $recipientBloodGroup=NULL;
	
	private $remarks=NULL;
	
	################################# SET FUNCTIONS ############################################
	function setOrganizationID($organizationID)
	{
		if($organizationID!="")
		{
			$this->organizationID = $organizationID;
		}
		else
		{
			throw new MyException(114100);
		}
		
	}
	
	function setDiagInvoiceID($diagInvoiceID)
	{
		if($diagInvoiceID!='')
		{
			$this->diagInvoiceID = $diagInvoiceID;
		}
		else
		{
			throw new MyException(114101);
		}
	}
	function setUserID($userID)
	{
		if($userID!='')
		{
			$this->userID = $userID;
		}
		else
		{
			throw new MyException(114102);
		}
	}
	function setLocationID($locationID)
	{
		if($locationID!='')
		{
			$this->locationID = $locationID;
		}
		else
		{
			throw new MyException(114103);
		}
	}
	function setPatientID($patientID)
	{
		if($patientID!='')
		{
			$this->patientID = $patientID;
		}
		else
		{
			throw new MyException(114104);
		}
	}
	
	function setInvoiceAmount($InvoiceAmount)
	{
		if($InvoiceAmount!='')
		{
			$this->InvoiceAmount = $InvoiceAmount;
		}
		else
		{
			throw new MyException(114105);
		}
	}
	function setDiscount($discount)
	{
		$this->discount = $discount;
	}
	function setInvoiceAmountPayable($invoiceAmountPayable)
	{
		if($invoiceAmountPayable!='')
		{
			$this->invoiceAmountPayable = $invoiceAmountPayable;
		}
		else
		{
			throw new MyException(114106);
		}
	}
	function setPayment($payment)
	{
		if($payment!='')
		{
			$this->payment = $payment;
		}
		else
		{
			throw new MyException(114107);
		}
	}
	function setBalance($balance)
	{
		$this->balance = $balance;
	}
	function setPaymentStatus($paymentStatus)
	{
		if($paymentStatus!='')
		{
			$this->paymentStatus = $paymentStatus;
		}
		else
		{
			throw new MyException(114108);
		}
	}
	
	
	function setDiagItemID($diagItemID)
	{
		if($diagItemID!='')
		{
			$this->diagItemID = $diagItemID;
		}
		else
		{
			throw new MyException(114109);
		}
	}
	function setDiagItemDate($diagItemDate)
	{
		if($diagItemDate!='')
		{
			$this->diagItemDate = $diagItemDate;
		}
		else
		{
			throw new MyException(114110);
		}
	}
	function setDiagnosisID($diagnosisID)
	{
		if($diagnosisID!='')
		{
			$this->diagnosisID = $diagnosisID;
		}
		else
		{
			throw new MyException(114111);
		}
	}
	function setDiagnosisType($diagnosisType)
	{
		if($diagnosisType!='')
		{
			$this->diagnosisType = $diagnosisType;
		}
		else
		{
			throw new MyException(114112);
		}
		
	}
	function setDiagnosisResult($diagnosisResult)
	{
		if($diagnosisResult!='')
		{
			$this->diagnosisResult = $diagnosisResult;
		}
		else
		{
			throw new MyException(114147);
		}
		
	}
	function setDiagnosisResultUnit($diagnosisResultUnit)
	{
		if($diagnosisResultUnit!='')
		{
			$this->diagnosisResultUnit = $diagnosisResultUnit;
		}
		else
		{
			throw new MyException(114148);
		}
		
	}
	
	function setDiagnosisSpecimenNo($diagnosisSpecimenNo)
	{
		$this->diagnosisSpecimenNo = $diagnosisSpecimenNo;
	}
	
	function setPrice($price)
	{
		if($price!='')
		{
			$this->price = $price;
		}
		else
		{
			throw new MyException(114113);
		}
	}
	function setNetTotal($netTotal)
	{
		if($netTotal!='')
		{
			$this->netTotal = $netTotal;
		}
		else
		{
			throw new MyException(114114);
		}
	}
	
	function setRefundStatus($refundStatus)
	{
		if($refundStatus!='')
		{
			$this->refundStatus = $refundStatus;
		}
		else
		{
			throw new MyException(114115);
		}
	}
	function setRefundAmount($refundAmount)
	{
		if($refundAmount!='')
		{
			$this->refundAmount = $refundAmount;
		}
		else
		{
			throw new MyException(114116);
		}
	}
	
	function setLabTestID($labTestID) 
	{
		if($labTestID!="")
		{
			$this->labTestID = $labTestID;
		}
		else
		{
			throw new MyException(114117);
		}
	}
	function setLabTestName($labTestName) 
	{
		if($labTestName!="")
		{
			$this->labTestName = $labTestName;
		}
		else
		{
			throw new MyException(114118);
		}
	}
	function setLabtestAbbrivation($labtestAbbrivation) 
	{
		if($labtestAbbrivation!="")
		{
			$this->labtestAbbrivation = $labtestAbbrivation;
		}
	}
	function setLabtestType($labtestType) 
	{
		if($labtestType!="")
		{
			$this->labtestType = $labtestType;
		}
	}
	function setLabTestNormalValue($labTestNormalValue) 
	{
		if($labTestNormalValue!="")
		{
			$this->labTestNormalValue = $labTestNormalValue;
		}
		else
		{
			//throw new MyException(114151);
		}
	}
	function setLabTestUnit($labTestUnit) 
	{
		if($labTestUnit!="")
		{
			$this->labTestUnit = $labTestUnit;
		}
	}
	function setLabTestCost($labTestCost) 
	{
		if($labTestCost!="")
		{
			$this->labTestCost = $labTestCost;
		}
		else
		{
			throw new MyException(114152);
		}
	}
	function setLabTestNotes($labTestNotes) 
	{
		if($labTestNotes!="")
		{
			$this->labTestNotes = $labTestNotes;
		}
	}
	
	function setLabTestPriorityID($labTestPriorityID) 
	{
		if($labTestPriorityID!="")
		{
			if(is_numeric($labTestPriorityID))
			{
				$this->labTestPriorityID = $labTestPriorityID;
			}
			else
			{
				throw new MyException(114168);
			}
		}
		else
		{
			throw new MyException(114165);
		}
	}
	
	function setLabTestGroupID($labTestGroupID) 
	{
		if($labTestGroupID!="")
		{
			$this->labTestGroupID = $labTestGroupID;
		}
		else
		{
			throw new MyException(114155);
		}
	}
	function setLabTestGroupName($labTestGroupName) 
	{
		if($labTestGroupName!="")
		{
			$this->labTestGroupName = $labTestGroupName;
		}
		else
		{
			throw new MyException(114156);
		}
	}
	function setLabTestGroupCost($labTestGroupCost) 
	{
		if($labTestGroupCost!="")
		{
			$this->labTestGroupCost = $labTestGroupCost;
		}
		else
		{
			throw new MyException(114163);
		}
	}
	
	function setProcedureID($procedureID) 
	{
		if($procedureID!="")
		{
			$this->procedureID = $procedureID;
		}
		else
		{
			throw new MyException(114119);
		}
	}
	function setProcedureName($procedureName) 
	{
		if($procedureName!="")
		{
			$this->procedureName = $procedureName;
		}
		else
		{
			throw new MyException(114120);
		}
	}
	function setProcedureType($procedureType) 
	{
		$this->procedureType = $procedureType;
	}
	function setProcedureCost($procedureCost) 
	{
		if($procedureCost!="")
		{
			$this->procedureCost = $procedureCost;
		}
		else
		{
			throw new MyException(114161);
		}
	}
	
	function setShiftID($shiftID) 
	{
		if($shiftID!="")
		{
			$this->shiftID = $shiftID;
		}
		else
		{
			throw new MyException(114143);
		}
	}
	function setVisitID($visitID) 
	{
		if($visitID!="")
		{
			$this->visitID = $visitID;
		}
		else
		{
			throw new MyException(114144);
		}
	}
	
	function setReferringBy($referringBy) 
	{
		if($referringBy!="")
		{
			$this->referringBy = $referringBy;
		}
	}
	
	function setReferringTo($referringTo) 
	{
		if($referringTo!="")
		{
			$this->referringTo = $referringTo;
		}
	}
	
	function setRemarks($remarks) 
	{
		if($remarks!="")
		{
			$this->remarks = $remarks;
		}
	}
	
	
	///////////////////////////////////// Cross Match /////////////////////
	
	function setBloodCrossMatchID($bloodCrossMatchID) 
	{
		if($bloodCrossMatchID!="")
		{
			$this->bloodCrossMatchID = $bloodCrossMatchID;
		}
		else
		{
			throw new MyException(114179);
		}
	}
	
	function setDonorName($donorName) 
	{
		if($donorName!="")
		{
			$this->donorName = $donorName;
		}
		else
		{
			throw new MyException(114180);
		}
	}
	function setBloodGroup($bloodGroup) 
	{
		if($bloodGroup!="")
		{
			$this->bloodGroup = $bloodGroup;
		}
		else
		{
			throw new MyException(114181);
		}
	}
	
	function setHepatitisBAntigen($hepatitisBAntigen) 
	{
		if($hepatitisBAntigen!="")
		{
			$this->hepatitisBAntigen = $hepatitisBAntigen;
		}
		else
		{
			throw new MyException(114182);
		}
	}
	function setHepatitisCAntigen($hepatitisCAntigen) 
	{
		if($hepatitisCAntigen!="")
		{
			$this->hepatitisCAntigen = $hepatitisCAntigen;
		}
		else
		{
			throw new MyException(114183);
		}
	}
	function setHIV($hiv) 
	{
		if($hiv!="")
		{
			$this->hiv = $hiv;
		}
		else
		{
			throw new MyException(114184);
		}
	}
	function setMP($mp) 
	{
		if($mp!="")
		{
			$this->mp = $mp;
		}
		else
		{
			throw new MyException(114185);
		}
	}
	function setVDRL($vdrl) 
	{
		if($vdrl!="")
		{
			$this->vdrl = $vdrl;
		}
		else
		{
			throw new MyException(114186);
		}
	}
	
	function setRecipientName($recipientName) 
	{
		if($recipientName!="")
		{
			$this->recipientName = $recipientName;
		}
		else
		{
			throw new MyException(114187);
		}
	}
	function setRecipientBloodGroup($recipientBloodGroup) 
	{
		if($recipientBloodGroup!="")
		{
			$this->recipientBloodGroup = $recipientBloodGroup;
		}
		else
		{
			throw new MyException(114188);
		}
	}
	
	
	
	
	################################# GET FUNCTIONS ############################################
	function getOrganizationID()
	{
		if($this->organizationID!="")
		{
			return $this->organizationID;
		}
		else
		{
			throw new MyException(114121);
		}
		
	}
	
	function getDiagInvoiceID()
	{
		if($this->diagInvoiceID!='')
		{
			return $this->diagInvoiceID;
		}
		else
		{
			throw new MyException(114122);
		}
	}
	function getUserID()
	{
		if($this->userID!='')
		{
			return $this->userID;
		}
		else
		{
			throw new MyException(114123);
		}
	}
	function getLocationID()
	{
		if($this->locationID!='')
		{
			return $this->locationID;
		}
		else
		{
			throw new MyException(114124);
		}
	}
	function getPatientID()
	{
		if($this->patientID!='')
		{
			return $this->patientID;
		}
		else
		{
			throw new MyException(114125);
		}
	}
	
	function getInvoiceAmount()
	{
		if($this->InvoiceAmount!='')
		{
			return $this->InvoiceAmount;
		}
		else
		{
			throw new MyException(114126);
		}
	}
	function getDiscount()
	{
		return $this->discount;
	}
	function getInvoiceAmountPayable()
	{
		if($this->invoiceAmountPayable!='')
		{
			return $this->invoiceAmountPayable;
		}
		else
		{
			throw new MyException(114127);
		}
	}
	function getPayment()
	{
		if($this->payment!='')
		{
			return $this->payment;
		}
		else
		{
			throw new MyException(114128);
		}
	}
	function getBalance()
	{
		return $this->balance;
	}
	function getPaymentStatus()
	{
		if($this->paymentStatus!='')
		{
			return $this->paymentStatus;
		}
		else
		{
			throw new MyException(114129);
		}
	}
	
	
	function getDiagItemID()
	{
		if($this->diagItemID!='')
		{
			return $this->diagItemID;
		}
		else
		{
			throw new MyException(114130);
		}
	}
	function getDiagItemDate()
	{
		if($this->diagItemDate!='')
		{
			return $this->diagItemDate;
		}
		else
		{
			throw new MyException(114131);
		}
	}
	function getDiagnosisID()
	{
		if($this->diagnosisID!='')
		{
			return $this->diagnosisID;
		}
		else
		{
			throw new MyException(114132);
		}
	}
	function getDiagnosisType()
	{
		if($this->diagnosisType!='')
		{
			return $this->diagnosisType;
		}
		else
		{
			throw new MyException(114133);
		}
		
	}
	function getDiagnosisResult()
	{
		if($this->diagnosisResult!='')
		{
			return $this->diagnosisResult;
		}
		else
		{
			throw new MyException(114149);
		}
		
	}
	function getDiagnosisResultUnit()
	{
		if($this->diagnosisResultUnit!='')
		{
			return $this->diagnosisResultUnit;
		}
		else
		{
			throw new MyException(114150);
		}
		
	}
	
	function getDiagnosisSpecimenNo()
	{
		return $this->diagnosisSpecimenNo;
	}
	
	function getPrice()
	{
		if($this->price!='')
		{
			return $this->price;
		}
		else
		{
			throw new MyException(114134);
		}
	}
	function getNetTotal()
	{
		if($this->netTotal!='')
		{
			return $this->netTotal;
		}
		else
		{
			throw new MyException(114135);
		}
	}
	
	function getRefundStatus()
	{
		if($this->refundStatus!='')
		{
			return $this->refundStatus;
		}
		else
		{
			throw new MyException(114136);
		}
	}
	function getRefundAmount()
	{
		if($this->refundAmount!='')
		{
			return $this->refundAmount;
		}
		else
		{
			throw new MyException(114137);
		}
	}
	
	function getLabTestID() 
	{
		if($this->labTestID!="")
		{
			return $this->labTestID;
		}
		else
		{
			throw new MyException(114138);
		}
	}
	function getLabTestName() 
	{
		if($this->labTestName!="")
		{
			return $this->labTestName;
		}
		else
		{
			throw new MyException(114139);
		}
	}
	function getLabtestAbbrivation() 
	{
		return $this->labtestAbbrivation;
	}
	function getLabtestType() 
	{
		return $this->labtestType;
	}
	function getLabTestNormalValue() 
	{
		if($this->labTestNormalValue!="")
		{
			return $this->labTestNormalValue;
		}
		else
		{
			//throw new MyException(114153);
		}
	}
	function getLabTestUnit() 
	{
		if($this->labTestUnit!="")
		{
			return $this->labTestUnit;
		}
	}
	function getLabTestCost() 
	{
		if($this->labTestCost!="")
		{
			return $this->labTestCost;
		}
		else
		{
			throw new MyException(114154);
		}
	}
	function getLabTestNotes() 
	{
		if($this->labTestNotes!="")
		{
			return $this->labTestNotes;
		}
	}
	
	function getLabTestPriorityID() 
	{
		if($this->labTestPriorityID!="")
		{
			return $this->labTestPriorityID;
		}
		else
		{
			throw new MyException(114166);
		}
	}
	
	function getLabTestGroupID() 
	{
		if($this->labTestGroupID!="")
		{
			return $this->labTestGroupID;
		}
		else
		{
			throw new MyException(114157);
		}
	}
	function getLabTestGroupName() 
	{
		if($this->labTestGroupName!="")
		{
			return $this->labTestGroupName;
		}
		else
		{
			throw new MyException(114158);
		}
	}
	
	function getLabTestGroupCost() 
	{
		if($this->labTestGroupCost!="")
		{
			return $this->labTestGroupCost;
		}
		else
		{
			throw new MyException(114164);
		}
	}
	
	function getProcedureID() 
	{
		if($this->procedureID!="")
		{
			return $this->procedureID;
		}
		else
		{
			throw new MyException(114140);
		}
	}
	function getProcedureName() 
	{
		if($this->procedureName!="")
		{
			return $this->procedureName;
		}
		else
		{
			throw new MyException(114141);
		}
	}
	function getProcedureType() 
	{
		return $this->procedureType;
	}
	function getProcedureCost() 
	{
		if($this->procedureCost!="")
		{
			return $this->procedureCost;
		}
		else
		{
			throw new MyException(114162);
		}
	}
	
	function getShiftID() 
	{
		if($this->shiftID!="")
		{
			return $this->shiftID;
		}
		else
		{
			throw new MyException(114145);
		}
	}
	function getVisitID() 
	{
		if($this->visitID!="")
		{
			return $this->visitID;
		}
		else
		{
			throw new MyException(114146);
		}
	}
	
	function getReferringBy() 
	{
		if($this->referringBy!="")
		{
			return $this->referringBy;
		}
	}
	
	function getReferringTo() 
	{
		if($this->referringTo!="")
		{
			return $this->referringTo;
		}
	}
	
	function getRemarks() 
	{
		if($this->remarks!="")
		{
			return $this->remarks;
		}
	}
	
	///////////////////////////////////// Cross Match /////////////////////
	
	function getBloodCrossMatchID() 
	{
		if($this->bloodCrossMatchID!="")
		{
			return $this->bloodCrossMatchID;
		}
		else
		{
			throw new MyException(114169);
		}
	}
	
	function getDonorName() 
	{
		if($this->donorName!="")
		{
			return $this->donorName;
		}
		else
		{
			throw new MyException(114170);
		}
	}
	function getBloodGroup() 
	{
		if($this->bloodGroup!="")
		{
			return $this->bloodGroup;
		}
		else
		{
			throw new MyException(114171);
		}
	}
	
	function getHepatitisBAntigen() 
	{
		if($this->hepatitisBAntigen!="")
		{
			return $this->hepatitisBAntigen;
		}
		else
		{
			throw new MyException(114172);
		}
	}
	function getHepatitisCAntigen() 
	{
		if($this->hepatitisCAntigen!="")
		{
			return $this->hepatitisCAntigen;
		}
		else
		{
			throw new MyException(114173);
		}
	}
	function getHIV() 
	{
		if($this->hiv!="")
		{
			return $this->hiv;
		}
		else
		{
			throw new MyException(114174);
		}
	}
	function getMP() 
	{
		if($this->mp!="")
		{
			return $this->mp;
		}
		else
		{
			throw new MyException(114175);
		}
	}
	function getVDRL() 
	{
		if($this->vdrl!="")
		{
			return $this->vdrl;
		}
		else
		{
			throw new MyException(114176);
		}
	}
	
	
	function getRecipientName() 
	{
		if($this->recipientName!="")
		{
			return $this->recipientName;
		}
		else
		{
			throw new MyException(114177);
		}
	}
	function getRecipientBloodGroup() 
	{
		if($this->recipientBloodGroup!="")
		{
			return $this->recipientBloodGroup;
		}
		else
		{
			throw new MyException(114178);
		}
	}
	
	
	
	################################# MAIN CHECK FUNCTIONS ############################################
	
	function checkTestExistInDiagInvoice($diagnosis_id,$diagnosis_type,$diaginvoice_id)
	{
		$query = "SELECT diagitems_id FROM diagnostic_invoiceitems WHERE diagnosis_id = '$diagnosis_id' AND diagnosis_type = '$diagnosis_type' AND diaginvoice_id = '$diaginvoice_id'";
		$queryOb = new classQueryGlobal;
		$product_invoice_relationship = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($product_invoice_relationship[0]['diagitems_id'])&&$product_invoice_relationship[0]['diagitems_id']!='')
		{
			return 0;
		}
		else
		{
			return 1;
		}
		
	}
	
	function checkGroupNameAlreadyExist($group_name,$org_id)
	{
		$group_name = strtolower($group_name);
		$query = "SELECT group_id FROM labtest_groups WHERE org_id = '$org_id' AND group_name = '$group_name'";
		$queryOb = new classQueryGlobal;
		$labtest_group_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($labtest_group_array[0]['group_id'])&&$labtest_group_array[0]['group_id']!='')
		{
			throw new MyException(114159);
		}
		else
		{
			return 1;
		}
	}
	
	function checkLabtestAlreadyExistInGroup($group_id,$labtest_id)
	{
		$query = "SELECT group_id FROM labtests WHERE group_id = '$group_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_group_array[0]['group_id'])&&$labtest_group_array[0]['group_id']!='')
		{
			throw new MyException(114160);
		}
		else
		{
			return 1;
		}
	}
	
	function checkLabtestPriorityIDAlreadyExistIn($priority_no,$org_id)
	{
		$query = "SELECT group_id FROM labtests WHERE org_id = '$org_id' AND priority_no = '$priority_no'";
		$queryOb = new classQueryGlobal;
		$labtest_group_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($labtest_group_array[0]['group_id'])&&$labtest_group_array[0]['group_id']!='')
		{
			throw new MyException(114167);
		}
		else
		{
			return 1;
		}
	}
	
	################################# MAIN INSERT FUNCTIONS ############################################
	
	function createDiagInvoice()
	{
		$diaginvoice_date  = time();
		$user_id = $this->getUserID();
		$location_id = $this->getLocationID();
		$patient_id = $this->getPatientID();
		$shift_id = $this->getShiftID();
		$visit_id = $this->getVisitID();
		
		
		$query = "INSERT INTO diagnostic_invoice (diaginvoice_date,user_id,location_id,patient_id,shift_id,visit_id) VALUES ('$diaginvoice_date','$user_id','$location_id','$patient_id','$shift_id','$visit_id')";
		$queryOb = new classQueryGlobal;
		$this->diagInvoiceID = $queryOb->check_fetch_query($query,'insert',$server="",$debug_mode="off");
	}
	
	function addItemsInInvoice()
	{
		if($this->checkTestExistInDiagInvoice($this->getDiagnosisID(),$this->getDiagnosisType(),$this->getDiagInvoiceID()))
		{
			$group_id = 0;
			$diagitem_date  = time();
			$diaginvoice_id = $this->getDiagInvoiceID();
			$diagnosis_id = $this->getDiagnosisID();
			$diagnosis_type = $this->getDiagnosisType();
			
			if($diagnosis_type=='group')
			{
				$price = $this->retrieveTestGroupCostByID($diagnosis_id);
				$group_id = $diagnosis_id;
			}
			
			if($diagnosis_type=='labtest')
			{
				$this->setLabTestID($diagnosis_id);
				$group_id = $this->getLabTestGroupID();
				if($group_id==0)
				{
					$labtest_array = $this->retrieveLabTestsByLabTestID();
					$price = $labtest_array[0]['labtest_cost'];
				}
				else
				{
					$price = 0;	
				}
				
			}
			
			if($diagnosis_type=='procedure')
			{
				$this->setProcedureID($diagnosis_id);
				$proc_array = $this->retrieveProcedureByProcedureID();
				$price = $proc_array[0]['procedure_cost'];
			}
			
			
			$query = "INSERT INTO  diagnostic_invoiceitems (diagitem_date,diaginvoice_id,diagnosis_id,diagnosis_type,price,net_total,group_id) VALUES ('$diagitem_date','$diaginvoice_id','$diagnosis_id','$diagnosis_type','$price','$price','$group_id')";
			
			$queryOb = new classQueryGlobal;
			$this->diagItemID = $queryOb->check_fetch_query($query,'insert',$server="",$debug_mode="off");
			
			############## update invoice total #################
			$payment = $this->retrieveInvoiceItemsTotal();
			$this->payment = $payment;
			$this->discount = "0";
			$this->updateDiagInvoice();
			
		}
		else
		{
			throw new MyException(114142);
		}
	}
	
	function insertLabTest()
	{
		$this->checkLabtestPriorityIDAlreadyExistIn($this->getLabTestPriorityID(),$this->getOrganizationID());
		
		$labtest_abb = $this->getLabtestAbbrivation();
		$labtest_name = $this->getLabTestName();
		$normal_value = $this->getLabTestNormalValue();
		$unit = $this->getLabTestUnit();
		$labtest_cost = $this->getLabTestCost();
		$priority_no = $this->getLabTestPriorityID();
		$labtest_notes = $this->getLabTestNotes();
		$org_id = $this->getOrganizationID();
		
		$query = "INSERT INTO labtests (labtest_abb,labtest_name,normal_value,unit,labtest_cost,labtest_notes,priority_no,org_id,active) VALUES ('$labtest_abb','$labtest_name','$normal_value','$unit','$labtest_cost','$labtest_notes','$priority_no','$org_id','1')";
		$queryOb = new classQueryGlobal;
		$this->labTestID = $queryOb->check_fetch_query($query,'insert',$server="",$debug_mode="off");
	}
	
	function insertLabTestGroups()
	{
		$this->checkGroupNameAlreadyExist($this->getOrganizationID(),$this->getLabTestGroupName());
		$group_name = strtolower($this->getLabTestGroupName());
		$group_cost = $this->getLabTestGroupCost();
		$org_id = $this->getOrganizationID();
		$query = "INSERT INTO labtest_groups (group_name,group_cost,org_id,active) VALUES ('$group_name','$group_cost','$org_id','1')";
		$queryOb = new classQueryGlobal;
		$this->labTestGroupID = $queryOb->check_fetch_query($query,'insert',$server="",$debug_mode="off");
		
	
	}
	
	function insertProcedure()
	{
		$procedure_name = $this->getProcedureName();
		$procedure_type = $this->getProcedureType();
		$procedure_cost = $this->getProcedureCost();
		$org_id = $this->getOrganizationID();
		
		$query = "INSERT INTO procedures (procedure_name,procedure_type,procedure_cost,org_id) VALUES ('$procedure_name','$procedure_type','$procedure_cost','$org_id')";
		$queryOb = new classQueryGlobal;
		$queryOb->check_fetch_query($query,'insert',$server="",$debug_mode="off");
	}
	
	function insertBloodCrossMatch()
	{
		$donor_name = $this->getDonorName();
		$blood_group = $this->getBloodGroup();
		
		$hb_antigen = $this->getHepatitisBAntigen();
		$hc_anitgen = $this->getHepatitisCAntigen();
		$hiv = $this->getHIV();
		$mp = $this->getMP();
		$vdrl = $this->getVDRL();
		
		$receipient_name = $this->getRecipientName();
		$recipient_blood_group = $this->getRecipientBloodGroup();
		
		$diaginvoice_id = $this->getDiagInvoiceID();
		$diagitem_id = $this->getDiagItemID();
		
		$remarks = $this->getRemarks();
		
		
		$query = "INSERT INTO labtest_bloodcrossmatch
		 (
			 donor_name,
			 blood_group,
			 hb_antigen,
			 hc_anitgen,
			 hiv,
			 mp,
			 vdrl,
			 receipient_name,
			 recipient_blood_group,
			 diaginvoice_id,
			 diagitem_id,
			 remarks
		 ) 
		 VALUES 
		 (
			 '$donor_name',
			 '$blood_group',
			 '$hb_antigen',
			 '$hc_anitgen',
			 '$hiv',
			 '$mp',
			 '$vdrl',
			 '$receipient_name',
			 '$recipient_blood_group',
			 '$diaginvoice_id',
			 '$diagitem_id',
			 '$remarks'
		 )";
		$queryOb = new classQueryGlobal;
		$this->bloodCrossMatchID = $queryOb->check_fetch_query($query,'insert',$server="",$debug_mode="off");
	}
	
	################################# MAIN UPDATE FUNCTIONS #############################################
	
	function updateRefferedByInDiagInvoice()
	{
		$diaginvoice_id = $this->getDiagInvoiceID();
		$referred_by = strtolower($this->getReferringBy());
		
		$query = "UPDATE diagnostic_invoice SET referred_by='$referred_by' where diaginvoice_id = '$diaginvoice_id'";
		$queryOb = new classQueryGlobal;
		$queryOb->check_fetch_query($query,'update',$server="",$debug_mode="off");
	}
	
	function updateRefferedToInDiagInvoice()
	{
		$diaginvoice_id = $this->getDiagInvoiceID();
		$referred_to = strtolower($this->getReferringTo());
		
		$query = "UPDATE diagnostic_invoice SET referred_to='$referred_to' where diaginvoice_id = '$diaginvoice_id'";
		$queryOb = new classQueryGlobal;
		$queryOb->check_fetch_query($query,'update',$server="",$debug_mode="off");
	}
	
	function updateDiagInvoice()
	{
		$diaginvoice_id = $this->getDiagInvoiceID();
		$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 diagnostic_invoice SET 
		 invoice_amount= '$invoice_amount',
		 discount='$discount',
		 amount_payable='$amount_payable',
		 payment = '$payment',
		 balance = '$balance'
		 where diaginvoice_id = '$diaginvoice_id'";
		$queryOb = new classQueryGlobal;
		$queryOb->check_fetch_query($query,'update',$server="",$debug_mode="off");
	}
	
	function updateDiagInvoiceAfterTransaction()
	{
		$diaginvoice_id = $this->getDiagInvoiceID();
		$payment_status = $this->getPaymentStatus();
		$payment = $this->getPayment();
		$balance = $this->getBalance();
		
		
		$query = "UPDATE diagnostic_invoice SET 
		 payment_status='$payment_status',
		 payment = '$payment',
		 balance = '$balance'
		 where diaginvoice_id = '$diaginvoice_id'";
		$queryOb = new classQueryGlobal;
		$queryOb->check_fetch_query($query,'update',$server="",$debug_mode="off");
	}
	
	function updateDiagItemsInInvoice()
	{
		$discount = $this->getDiscount();
		$specimen = $this->getDiagnosisSpecimenNo();
		$diagitems_id = $this->getDiagItemID();
		######## retrieve product detail ########
		
		$item_detail = $this->retrieveInvoiceItemsDetailByItemID();
		
		$this->diagInvoiceID = $item_detail[0]['diaginvoice_id'];
		$price = $item_detail[0]['price'];
		$net_total = $price-($price*$discount/100);
		
		$query = "UPDATE  diagnostic_invoiceitems set 
		 price='$price',
		 discount='$discount',
		 net_total='$net_total',
		 specimen = '$specimen'
		 WHERE diagitems_id = '$diagitems_id'";
		$queryOb = new classQueryGlobal;
		$queryOb->check_fetch_query($query,'update',$server="",$debug_mode="off");
		
		
		######## update payemts in sale invoice ########
		$invoice_info = $this->retrieveInvoiceDetail();
		if($invoice_info[0]['payment_status']!='paid')
		{
			$this->payment = $this->retrieveInvoiceItemsTotal();
			$discount = $invoice_info[0]['discount'];
			if($discount==0)
			{
				$this->discount = '0';
			}
			else
			{
				$this->discount = $discount;
			}
			$this->updateDiagInvoice();
		}
	}
	
	function updateDiagItemsResult()
	{
		$diagitems_id = $this->getDiagItemID();
		$diagnosis_result = $this->getDiagnosisResult();
		$diagnosis_result_time = time();
		$query = "UPDATE  diagnostic_invoiceitems set 
		 diagnosis_result='$diagnosis_result',
		 diagnosis_result_time = '$diagnosis_result_time'
		 WHERE diagitems_id = '$diagitems_id'";
		$queryOb = new classQueryGlobal;
		$queryOb->check_fetch_query($query,'update',$server="",$debug_mode="off");
	}
	
	function refunddiagItemsInInvoice()
	{
		$diagitems_id = $this->getDiagItemID();
		
		######## retrieve item detail ########
		$single_item_detail = $this->retrieveInvoiceItemsDetailByItemID();
		$diaginvoice_id = $single_item_detail[0]['diaginvoice_id'];
		$discount = $single_item_detail[0]['discount'];
		$price = $single_item_detail[0]['price'];
		
		$refund_amount = $price-($price*$discount/100);
		
		$query = "UPDATE diagnostic_invoiceitems SET refund_status = 'refund' , refund_amount = refund_amount + '$refund_amount' where diagitems_id='$diagitems_id'";
		$queryOb = new classQueryGlobal;
		$queryOb->check_fetch_query($query,'update',$server="",$debug_mode="off");
			
	}
	
	function updateGroupIDInLabtest()
	{
		if($this->getLabTestGroupID()!=0)
		{
			$this->checkLabtestAlreadyExistInGroup($this->getLabTestGroupID(),$this->getLabTestID());
		}
		
		$labtest_id = $this->getLabTestID();
		$group_id = $this->getLabTestGroupID();
		
		$query = "UPDATE labtests SET group_id = '$group_id' WHERE labtest_id = '$labtest_id'";
		$queryOb = new classQueryGlobal;
		$queryOb->check_fetch_query($query,'update',$server="",$debug_mode="off");
	}
	
	function updateLabTestGroups()
	{
		
		$this->checkGroupNameAlreadyExist($this->getOrganizationID(),$this->getLabTestGroupName());
		
		$group_name = strtolower($this->getLabTestGroupName());
		$group_id = $this->getLabTestGroupID();
		$group_cost = $this->getLabTestGroupCost();
		$query = "UPDATE labtest_groups SET group_name = '$group_name' , group_cost = '$group_cost' WHERE group_id = '$group_id'";
		$queryOb = new classQueryGlobal;
		$queryOb->check_fetch_query($query,'update',$server="",$debug_mode="off");
		
	
	}
	
	function updateLabTestGroupsActiveOrInactive($group_id,$active)
	{
		$query = "UPDATE labtest_groups SET active = '$active' WHERE group_id = '$group_id'";
		$queryOb = new classQueryGlobal;
		$queryOb->check_fetch_query($query,'update',$server="",$debug_mode="off");
	}
	
	function updateLabTest()
	{
		//$this->checkLabtestPriorityIDAlreadyExistIn($this->getLabTestPriorityID(),$this->getOrganizationID());
		
		$labtest_abb = $this->getLabtestAbbrivation();
		$labtest_name = $this->getLabTestName();
		$normal_value = $this->getLabTestNormalValue();
		$unit = $this->getLabTestUnit();
		$labtest_cost = $this->getLabTestCost();
		$labtest_notes = $this->getLabTestNotes();
		$priority_no = $this->getLabTestPriorityID();
		$org_id = $this->getOrganizationID();
		$labtest_id = $this->getLabTestID();
		
		$query = "UPDATE labtests SET labtest_abb = '$labtest_abb' , labtest_name = '$labtest_name' , normal_value = '$normal_value' , unit = '$unit' , labtest_cost = '$labtest_cost' , labtest_notes = '$labtest_notes'  , priority_no = '$priority_no' , org_id = '$org_id' WHERE labtest_id = '$labtest_id'";
		
		$queryOb = new classQueryGlobal;
		$queryOb->check_fetch_query($query,'update',$server="",$debug_mode="off");
	}
	
	function updateLabTestActiveOrInactive($labtest_id,$active)
	{
		$query = "UPDATE labtests SET active = '$active' WHERE labtest_id = '$labtest_id'";
		$queryOb = new classQueryGlobal;
		$queryOb->check_fetch_query($query,'update',$server="",$debug_mode="off");
	}
	
	function updateBloodCrossMatch()
	{
		$donor_name = $this->getDonorName();
		$blood_group = $this->getBloodGroup();
		
		$hb_antigen = $this->getHepatitisBAntigen();
		$hc_anitgen = $this->getHepatitisCAntigen();
		$hiv = $this->getHIV();
		$mp = $this->getMP();
		$vdrl = $this->getVDRL();
		
		$receipient_name = $this->getRecipientName();
		$recipient_blood_group = $this->getRecipientBloodGroup();
		
		$diaginvoice_id = $this->getDiagInvoiceID();
		$diagitem_id = $this->getDiagItemID();
		
		$remarks = $this->getRemarks();
		
		
		$query = "UPDATE labtest_bloodcrossmatch SET
		
		 donor_name = '$donor_name',
		 blood_group = '$blood_group',
		 hb_antigen = '$hb_antigen',
		 hc_anitgen = '$hc_anitgen',
		 hiv = '$hiv',
		 mp = '$mp',
		 vdrl = '$vdrl',
		 receipient_name = '$receipient_name',
		 recipient_blood_group = '$recipient_blood_group',
		 remarks = '$remarks'
		 WHERE 
		 diaginvoice_id = '$diaginvoice_id'
		 AND
		 diagitem_id = '$diagitem_id'";
			 
		$queryOb = new classQueryGlobal;
		$queryOb->check_fetch_query($query,'update',$server="",$debug_mode="off");
	}
	
	
	################################# MAIN DELETE FUNCTIONS #############################################
	
	function deleteDiagItemsInInvoice()
	{
		$diagitems_id = $this->getDiagItemID();
		$item_array = $this->retrieveInvoiceItemsDetailByItemID();
		
		$this->diagInvoiceID = $item_array[0]['diaginvoice_id'];
		$invoice_info = $this->retrieveInvoiceDetail();
		
		$payment = $invoice_info[0]['invoice_amount']-$item_array[0]['net_total'];
		if($payment==0)
		{
			$this->payment = "0";
		}
		else
		{
			$this->payment = $payment;
		}
		
		$this->discount = $invoice_info[0]['discount'];
		$this->updateDiagInvoice();
		
		$query = "DELETE FROM diagnostic_invoiceitems WHERE diagitems_id='$diagitems_id'";
		$queryOb = new classQueryGlobal;
		$queryOb->check_fetch_query($query,'delete',$server="",$debug_mode="off");
	}
	
	function deleteDiagInvoice()
	{
		$diaginvoice_id = $this->getDiagInvoiceID();
		
		############## Delete invoice items #########################
		$query = "DELETE FROM diagnostic_invoiceitems WHERE diaginvoice_id='$diaginvoice_id'";
		$queryOb = new classQueryGlobal;
		$queryOb->check_fetch_query($query,'delete',$server="",$debug_mode="off");
		
		############## Delete invoice #########################
		$query = "DELETE FROM diagnostic_invoice WHERE diaginvoice_id='$diaginvoice_id'";
		$queryOb = new classQueryGlobal;
		$queryOb->check_fetch_query($query,'delete',$server="",$debug_mode="off");
		
	}
	
	function deleteDiagGroupTestsInInvoice()
	{
		$group_id = $this->getLabTestGroupID();
		$diaginvoice_id = $this->getDiagInvoiceID();
		
		$this->diagInvoiceID = $diaginvoice_id;
		$invoice_info = $this->retrieveInvoiceDetail();
		$payment = $invoice_info[0]['invoice_amount']-$this->retrieveTestGroupCostByID($group_id);
		
		if($payment==0)
		{
			$this->payment = "0";
		}
		else
		{
			$this->payment = $payment;
		}
		
		$this->discount = $invoice_info[0]['discount'];
		$this->updateDiagInvoice();
		
		$query = "DELETE FROM diagnostic_invoiceitems WHERE diaginvoice_id='$diaginvoice_id' AND group_id = '$group_id'";
		$queryOb = new classQueryGlobal;
		$queryOb->check_fetch_query($query,'delete',$server="",$debug_mode="off");
	}
	
	################################# MAIN RETRIEVE FUNCTIONS ############################################
	
	function retrieveInvoiceDetail()
	{
		$diaginvoice_id = $this->getDiagInvoiceID();
		
		$query = "SELECT * FROM  diagnostic_invoice WHERE diaginvoice_id = '$diaginvoice_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  diagnostic_invoice WHERE location_id = '$location_id'";
		
		if($min!=''&&$max!='')
		{
			$query .= " AND diaginvoice_id BETWEEN $min and $max";
		}
		else
		{
			$query .= " AND diaginvoice_id = '$id'";
		}
		
		$query .= " ORDER BY diaginvoice_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;
		}
		
	}
	
	////// shift and location and date //////////////
	function retrieveInvoiceDetailByLocationIDAndShiftID()
	{
		$location_id = $this->getLocationID();
		$shift_id = $this->getShiftID();
		
		$query = "SELECT * FROM  diagnostic_invoice WHERE location_id = '$location_id' AND shift_id = '$shift_id' ORDER BY diaginvoice_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,$shift_id)
	{
		$query = "SELECT * FROM diagnostic_invoice WHERE location_id = '$location_id' AND shift_id = '$shift_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 diaginvoice_date  > $startTime";
			}
		}
		
		if($endTime!="")
		{
			if(($startTime!="" && $startTime < $endTime) || $startTime="")
			{
				$query .= " AND diaginvoice_date  <= $endTime";
			}
		}
		$query .= " ORDER BY diaginvoice_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;
		}
		
	}
	
	////// balance and location and date //////////////
	function retrieveInvoiceDetailHavingBalanceByLocationID()
	{
		$location_id = $this->getLocationID();
		
		$query = "SELECT * FROM diagnostic_invoice WHERE location_id = '$location_id' AND balance != 0  ORDER BY diaginvoice_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 diagnostic_invoice 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 diaginvoice_date  > $startTime";
			}
		}
		
		if($endTime!="")
		{
			if(($startTime!="" && $startTime < $endTime) || $startTime="")
			{
				$query .= " AND diaginvoice_date  <= $endTime";
			}
		}
		$query .= " ORDER BY diaginvoice_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;
		}
		
	}
	
	////// paid and location and date //////////////
	function retrieveInvoiceDetailPaidByLocationID($limit=NULL)
	{
		$location_id = $this->getLocationID();
		if($limit!='')
		{
			$query = "SELECT * FROM  diagnostic_invoice WHERE location_id = '$location_id' AND payment_status ='paid'  ORDER BY diaginvoice_date DESC limit $limit";
		}
		else
		{
			$query = "SELECT * FROM  diagnostic_invoice WHERE location_id = '$location_id' AND payment_status ='paid'  ORDER BY diaginvoice_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 retrieveInvoiceDetailPaidByDate($from,$to,$location_id)
	{
		$query = "SELECT * FROM diagnostic_invoice WHERE location_id = '$location_id' AND payment_status ='paid'";
	
		$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 diaginvoice_date  > $startTime";
			}
		}
		
		if($endTime!="")
		{
			if(($startTime!="" && $startTime < $endTime) || $startTime="")
			{
				$query .= " AND diaginvoice_date  <= $endTime";
			}
		}
		$query .= " ORDER BY diaginvoice_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;
		}
		
	}
	
	////// location and date //////////////
	function retrieveAllInvoiceDetailByLocationID($limit=NULL)
	{
		$location_id = $this->getLocationID();
		if($limit!='')
		{
			$query = "SELECT * FROM  diagnostic_invoice WHERE location_id = '$location_id' AND payment_status ='paid'  ORDER BY diaginvoice_date DESC limit $limit";
		}
		else
		{
			$query = "SELECT * FROM  diagnostic_invoice WHERE location_id = '$location_id'  ORDER BY diaginvoice_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 retrieveAllInvoiceDetailByDate($from,$to,$location_id)
	{
		$query = "SELECT * FROM diagnostic_invoice 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 diaginvoice_date  > $startTime";
			}
		}
		
		if($endTime!="")
		{
			if(($startTime!="" && $startTime < $endTime) || $startTime="")
			{
				$query .= " AND diaginvoice_date  <= $endTime";
			}
		}
		
		$query .= "  ORDER BY diaginvoice_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;
		}
		
	}
	
	////// patient id and visit id //////////////
	function retrieveInvoiceDetailByPatientID()
	{
		$patient_id = $this->getPatientID();
		$query = "SELECT * FROM  diagnostic_invoice WHERE patient_id = '$patient_id' ORDER BY diaginvoice_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 retrieveInvoiceDetailByPatientIDRanges($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  diagnostic_invoice WHERE location_id = '$location_id'";
		
		if($min!=''&&$max!='')
		{
			$query .= " AND patient_id BETWEEN $min and $max";
		}
		else
		{
			$query .= " AND patient_id = '$id'";
		}
		
		$query .= " ORDER BY diaginvoice_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 retrieveInvoiceDetailByPatientName($from,$to,$name,$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]);
		
		$query = "SELECT diagnostic_invoice.* FROM  diagnostic_invoice JOIN patients ON diagnostic_invoice.patient_id = patients.patient_auto_id WHERE diagnostic_invoice.location_id = '$location_id'";
		$query .= " AND (patient_fname LIKE '%".$name."%' OR patient_lname LIKE '%".$name."%')"; 
		
		if($startTime!="")
		{
			if(($endTime!="" && $endTime > $startTime) || $endTime="")
			{
				$query .= " AND diaginvoice_date  > $startTime";
			}
		}
		
		if($endTime!="")
		{
			if(($startTime!="" && $startTime < $endTime) || $startTime="")
			{
				$query .= " AND diaginvoice_date  <= $endTime";
			}
		}
		$query .= " ORDER BY diaginvoice_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 retrieveInvoiceDetailByReferrerName($from,$to,$name,$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]);
		
		$query = "SELECT * FROM  diagnostic_invoice WHERE location_id = '$location_id'";
		$query .= " AND (referred_by LIKE '%".$name."%')"; 
		if($startTime!="")
		{
			if(($endTime!="" && $endTime > $startTime) || $endTime="")
			{
				$query .= " AND diaginvoice_date  > $startTime";
			}
		}
		
		if($endTime!="")
		{
			if(($startTime!="" && $startTime < $endTime) || $startTime="")
			{
				$query .= " AND diaginvoice_date  <= $endTime";
			}
		}
		$query .= " ORDER BY diaginvoice_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 retrieveInvoiceDetailByVisitID($visit_id)
	{
		
		$query = "SELECT * FROM  diagnostic_invoice WHERE visit_id = '$visit_id' ORDER BY diaginvoice_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;
		}
		
	}
	
	////// Invoice Items detail //////////////
	
	function retrieveInvoiceItemsDetail()
	{
		$diaginvoice_id = $this->getDiagInvoiceID();
		
		$query = "SELECT * FROM  diagnostic_invoiceitems WHERE diaginvoice_id = '$diaginvoice_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 retrieveInvoiceItemsDetailGrouped()
	{
		$diaginvoice_id = $this->getDiagInvoiceID();
		$query = "SELECT DISTINCT group_id, GROUP_CONCAT(DISTINCT diagitems_id ORDER BY diagitems_id) AS id_list FROM diagnostic_invoiceitems WHERE diaginvoice_id = '$diaginvoice_id' group by group_id ORDER BY group_id";	
		
		$queryOb = new classQueryGlobal;
		$invoice_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");	
		
		if(isset($invoice_array[0])&&$invoice_array[0]!='')
		{
			for($inv=0;$inv<count($invoice_array);$inv++)
			{
				$item_ids = explode(',',$invoice_array[$inv]['id_list']);
				if($invoice_array[$inv]['group_id']!=0)
				{
					$raw_array[$inv]['group_name'] = $this->retrieveTestGroupNameByID($invoice_array[$inv]['group_id']);
				}
				else
				{
					$raw_array[$inv]['group_name'] = '';
				}
				
				for($it=0;$it<count($item_ids);$it++)
				{
					$this->diagItemID = $item_ids[$it];
					$items_singel_array = $this->retrieveInvoiceItemsDetailByItemID();
					if($items_singel_array[0]['diagnosis_type']!='group')
					{
						if($items_singel_array[0]['diagnosis_type']=='labtest')
						{
							$this->setLabTestID($items_singel_array[0]['diagnosis_id']);
							$labtest_array = $this->retrieveLabTestsByLabTestID();
							$items_singel_array[0]['diagnosis_name'] = $labtest_array[0]['labtest_name'];
							$items_singel_array[0]['normal_value'] = $labtest_array[0]['normal_value'];
							$items_singel_array[0]['unit'] = $labtest_array[0]['unit'];
							$items_singel_array[0]['labtest_notes'] = $labtest_array[0]['labtest_notes'];
						}
						if($items_singel_array[0]['diagnosis_type']=='procedure')
						{
							$this->setProcedureID($items_singel_array[0]['diagnosis_id']);
							$proc_array = $this->retrieveProcedureByProcedureID();
							$items_singel_array[0]['diagnosis_name'] = $proc_array[0]['procedure_name'];
							$items_singel_array[0]['normal_value'] = '';
							$items_singel_array[0]['unit'] = '';
							$items_singel_array[0]['labtest_notes'] = '';
							
						}
						
						$raw_array[$inv]['group_items'][] = $items_singel_array[0];
					}
				}
				
			}
			return $raw_array;
		}
		
	}
	
	function retrieveInvoiceItemsTotal()
	{
		$diaginvoice_id = $this->getDiagInvoiceID();
		
		$query = "SELECT sum(net_total) FROM diagnostic_invoiceitems WHERE diaginvoice_id = '$diaginvoice_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()
	{
		$diagitems_id = $this->getDiagItemID();
		
		$query = "SELECT * FROM  diagnostic_invoiceitems WHERE diagitems_id = '$diagitems_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 retrieveSumOfInvoiceItemsDetail($from,$to,$location_id,$diagnosis_type,$diagnosis_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]);
		
		$query = "SELECT count(diagitems_id),sum(net_total) 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'";
		
		if($startTime!="")
		{
			if(($endTime!="" && $endTime > $startTime) || $endTime="")
			{
				$query .= " AND diagitem_date  > $startTime";
			}
		}
		
		if($endTime!="")
		{
			if(($startTime!="" && $startTime < $endTime) || $startTime="")
			{
				$query .= " AND diagitem_date  <= $endTime";
			}
		}
		
		$query .= "  ORDER BY diagitem_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 retrieveInvoiceItemsDetailByLocationIDAndType()
	{
		$location_id = $this->getLocationID();
		$diagnosis_type = $this->getDiagnosisType();
		
		$query = "SELECT * 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' ORDER BY diagitem_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 retrieveInvoiceItemsDetailByLocationIDPatientIDAndType()
	{
		$location_id = $this->getLocationID();
		$diagnosis_type = $this->getDiagnosisType();
		$patient_id = $this->getPatientID();
		
		$query = "SELECT * FROM  diagnostic_invoiceitems JOIN diagnostic_invoice ON diagnostic_invoiceitems.diaginvoice_id = diagnostic_invoice.diaginvoice_id WHERE diagnostic_invoice.location_id = '$location_id' AND diagnostic_invoice.patient_id = '$patient_id' AND diagnostic_invoiceitems.diagnosis_type = '$diagnosis_type' ORDER BY diagitem_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 countPaidInvoiceItemsDetailByLocationIDAndType()
	{
		$location_id = $this->getLocationID();
		$diagnosis_type = $this->getDiagnosisType();
		
		$query = "SELECT count(*) FROM  diagnostic_invoiceitems JOIN diagnostic_invoice ON diagnostic_invoiceitems.diaginvoice_id = diagnostic_invoice.diaginvoice_id 
		WHERE diagnostic_invoice.location_id = '$location_id' 
		AND diagnostic_invoice.payment_status ='paid'
		AND diagnostic_invoiceitems.diagnosis_type = '$diagnosis_type'";
		
		
		
		$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 retrievePaidInvoiceItemsDetailByLocationIDAndType($limit=NULL)
	{
		$location_id = $this->getLocationID();
		$diagnosis_type = $this->getDiagnosisType();
		
		$query = "SELECT * FROM  diagnostic_invoiceitems JOIN diagnostic_invoice 
		ON diagnostic_invoiceitems.diaginvoice_id = diagnostic_invoice.diaginvoice_id 
		WHERE diagnostic_invoice.location_id = '$location_id' 
		AND diagnostic_invoice.payment_status ='paid' 
		AND diagnostic_invoiceitems.diagnosis_type = '$diagnosis_type' 
		ORDER BY diagitem_date DESC";
		if($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 retrievePaidInvoiceItemsDetailByLocationIDPatientIDAndType()
	{
		$location_id = $this->getLocationID();
		$diagnosis_type = $this->getDiagnosisType();
		$patient_id = $this->getPatientID();
		
		$query = "SELECT * FROM  diagnostic_invoice JOIN diagnostic_invoiceitems 
		ON diagnostic_invoice.diaginvoice_id = diagnostic_invoiceitems.diaginvoice_id
		WHERE diagnostic_invoice.location_id = '$location_id' 
		AND diagnostic_invoice.payment_status ='paid' 
		AND diagnostic_invoice.patient_id = '$patient_id' 
		AND diagnostic_invoiceitems.diagnosis_type = '$diagnosis_type' 
		ORDER BY diagitem_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;
		}
		
	}
	
	
	////// TESTS AND GROUPS DETAIL //////////////
	
	function retrieveBloodCrossMatchDetailByItemID()
	{
		$diagitems_id = $this->getDiagItemID();
		
		$query = "SELECT * FROM  labtest_bloodcrossmatch WHERE diagitem_id  = '$diagitems_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 retrieveBloodCrossMatchDetailByInvoiceID()
	{
		$diaginvoice_id = $this->getDiagInvoiceID();
		
		$query = "SELECT * FROM  labtest_bloodcrossmatch WHERE diaginvoice_id  = '$diaginvoice_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 retrieveSumOfRefundItems()
	{
		$diaginvoice_id = $this->getDiagInvoiceID();
		$query = "SELECT sum(refund_amount) FROM  diagnostic_invoiceitems where diaginvoice_id='$diaginvoice_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 retrieveLabTests()
	{
		$org_id = $this->getOrganizationID();
		
		$query = "SELECT * FROM labtests WHERE org_id = '$org_id' AND active = '1' order by labtest_name asc";
		$queryOb = new classQueryGlobal;
		$labtest_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($labtest_array[0]))
		{
			return $labtest_array;
		}
	}
	function retrieveProcedures()
	{
		$org_id = $this->getOrganizationID();
		
		$query = "SELECT * FROM procedures WHERE org_id = '$org_id' order by procedure_name asc";
		$queryOb = new classQueryGlobal;
		$proc_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($proc_array[0]))
		{
			return $proc_array;
		}
	}
	function retrieveLabTestsByLabTestID()
	{
		$labtest_id = $this->getLabTestID();
		$query = "SELECT * 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]))
		{
			return $labtest_array;
		}
	}
	function retrieveLabTestsByLabTestName()
	{
		$labtest_name = $this->getLabTestName();
		$query = "SELECT * FROM labtests WHERE labtest_name LIKE '%".$labtest_name."%'";
		$queryOb = new classQueryGlobal;
		$labtest_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($labtest_array[0]))
		{
			return $labtest_array;
		}
	}
	function retrieveProcedureByProcedureID()
	{
		$procedure_id = $this->getProcedureID();
		$query = "SELECT * FROM procedures WHERE procedure_id = '$procedure_id'";
		$queryOb = new classQueryGlobal;
		$proc_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($proc_array[0]))
		{
			return $proc_array;
		}
	}
	
	function retrieveTestGroups()
	{
		$org_id = $this->getOrganizationID();
		$query = "SELECT * FROM labtest_groups WHERE org_id = '$org_id' AND active = '1' order by group_name asc";
		$queryOb = new classQueryGlobal;
		$labtest_group_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($labtest_group_array[0]))
		{
			return $labtest_group_array;
		}
		
	}
	function retrieveTestGroupNameByID($group_id)
	{
		$query = "SELECT group_name FROM labtest_groups WHERE group_id = '$group_id'";
		$queryOb = new classQueryGlobal;
		$labtest_group_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($labtest_group_array[0]['group_name']))
		{
			return $labtest_group_array[0]['group_name'];
		}
		
	}
	function retrieveTestGroupCostByID($group_id)
	{
		$query = "SELECT group_cost FROM labtest_groups WHERE group_id = '$group_id'";
		$queryOb = new classQueryGlobal;
		$labtest_group_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($labtest_group_array[0]['group_cost']))
		{
			return $labtest_group_array[0]['group_cost'];
		}
		
	}
	function retrieveTestGroupsHavingTests()
	{
		$org_id = $this->getOrganizationID();
		$query = "SELECT  lg.* FROM labtest_groups lg WHERE lg.org_id = '$org_id' AND  lg.group_id IN ( SELECT group_id FROM labtests) ";
	
		$queryOb = new classQueryGlobal;
		$labtest_group_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($labtest_group_array[0]))
		{
			return $labtest_group_array;
		}
		
	}
	function retrieveLabTestByGroupID()
	{
		$group_id = $this->getLabTestGroupID();
		$query = "SELECT * FROM labtests WHERE group_id = '$group_id' order by priority_no asc";
		$queryOb = new classQueryGlobal;
		$labtest_group_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($labtest_group_array[0]))
		{
			return $labtest_group_array;
		}
		
	}
	
	
	
	
}
?>