<?php
class classIPDPresciption
{
	  	private $prescriptionID=NULL;
		
		private $locationID=NULL;
		private $organizationID=NULL;
		private $visitDate=NULL;
		private $ipdBedAllotmentID=NULL;
		private $patientID=NULL;
		private $requestedBy=NULL;
		private $requestedTime=NULL;
		private $prescriptionType=NULL;
		private $prescriptionStatus=NULL;
		private $prescriptionItemID=NULL;
		
		private $dosageID=NULL;
		private $dosage=NULL;
		private $quantity=NULL;
		private $duration=NULL;
		private $medicineID=NULL;
		private $labTestID=NULL;
		private $procedureID=NULL;
		private $medicineName=NULL;
		private $labTestName=NULL;
		private $procedureName=NULL;
		
		private $dressingID=NULL;
		private $dressing=NULL;
		
		private $medicineIDs=array();
		private $labTestIDs=array();
		private $procedureIDs=array();
		
		########################################## set functions ##############################
		function setPrescriptionID($prescriptionID) 
		{
			if($prescriptionID!="")
			{
				$this->prescriptionID = $prescriptionID;
			}
			else
			{
				throw new MyException(118110);
			}
		}
		
	   
		function setOrganizationID($organizationID)
		{
			if($organizationID!="")
			{
				$this->organizationID = $organizationID;
			}
			else
			{
				throw new MyException(118111);
			}
			
		}
		function setLocationID($locationID)
		{
			if($locationID!="")
			{
				$this->locationID = $locationID;
			}
			else
			{
				throw new MyException(118112);
			}
			
		}
		function setIpdBedAllotmentID($ipdBedAllotmentID)
		{
			if($ipdBedAllotmentID!="")
			{
				$this->ipdBedAllotmentID = $ipdBedAllotmentID;
			}
			else
			{
				throw new MyException(118113);
			}
		}
	  	function setDoctorID($doctorID)
		{
			if($doctorID!="")
			{
				if(classFilter::validateInteger($doctorID))
				{
					$this->doctorID = classFilter::addMySlashes($doctorID);
				}
				else
				{
					throw new MyException(118114);
				}
			}
			else
			{
				throw new MyException(118115);
			}
		}
		function setPatientID($patientID)
		{
			if($patientID!="")
			{
				if(classFilter::validateInteger($patientID))
				{
					$this->patientID = classFilter::addMySlashes($patientID);
				}
				else
				{
					throw new MyException(118116);
				}
			}
			else
			{
				throw new MyException(118117);
			}
		}
		function setRequestedBy($requestedBy)
		{
			if($requestedBy!="")
			{
				if(classFilter::validateInteger($requestedBy))
				{
					$this->requestedBy = classFilter::addMySlashes($requestedBy);
				}
				else
				{
					throw new MyException(118118);
				}
			}
			else
			{
				throw new MyException(118119);
			}
		}
		function setRequestedTime($requestedTime)
		{
			if($requestedTime!="")
			{
				$this->requestedTime = $requestedTime;
			}
		}
		function setPrescriptionType($prescriptionType) 
		{
			if($prescriptionType!="")
			{
				$this->prescriptionType = $prescriptionType;
			}
		}
		function setPrescriptionStatus($prescriptionStatus) 
		{
			if($prescriptionStatus!="")
			{
				$this->prescriptionStatus = $prescriptionStatus;
			}
		}
		
		function setPrescriptionItemID($prescriptionItemID)
		{
			if($prescriptionItemID!="")
			{
				$this->prescriptionItemID = $prescriptionItemID;
			}
			else
			{
				throw new MyException(118120);
			}
		}
		
		function setDosageID($dosageID) 
		{
			if($dosageID!="")
			{
				$this->dosageID = $dosageID;
			}
		}
		function setDosage($dosage) 
		{
			if($dosage!="")
			{
				$this->dosage = $dosage;
			}
		}
		function setQuantity($quantity) 
		{
			if($quantity!="")
			{
				$this->quantity = $quantity;
			}
		}
		function setDuration($duration) 
		{
			if($duration!="")
			{
				$this->duration = $duration;
			}
		}
		function setMedicineID($medicineID) 
		{
			if($medicineID!="")
			{
				$this->medicineID = $medicineID;
			}
			else
			{
				throw new MyException(118121);
			}
		}
		function setLabTestID($labTestID) 
		{
			if($labTestID!="")
			{
				$this->labTestID = $labTestID;
			}
			else
			{
				throw new MyException(118122);
			}
		}
		function setProcedureID($procedureID) 
		{
			if($procedureID!="")
			{
				$this->procedureID = $procedureID;
			}
			else
			{
				throw new MyException(118123);
			}
		}
		function setMedicineName($medicineName) 
		{
			if($medicineName!="")
			{
				$this->medicineName = $medicineName;
			}
			else
			{
				throw new MyException(118124);
			}
		}
		function setLabTestName($labTestName) 
		{
			if($labTestName!="")
			{
				$this->labTestName = $labTestName;
			}
			else
			{
				throw new MyException(118125);
			}
		}
		function setProcedureName($procedureName) 
		{
			if($procedureName!="")
			{
				$this->procedureName = $procedureName;
			}
			else
			{
				throw new MyException(118126);
			}
		}
		function setDressingID($dressingID) 
		{
			if($dressingID!="")
			{
				$this->dressingID = $dressingID;
			}
		}
		function setDressing($dressing) 
		{
			if($dressing!="")
			{
				$this->dressing = $dressing;
			}
		}
		
		function setMedicineIDs($medicineIDs) 
		{
			if($medicineIDs!="")
			{
				$this->medicineIDs = $medicineIDs;
			}
		}
		function setLabTestIDs($labTestIDs) 
		{
			if($labTestIDs!="")
			{
				$this->labTestIDs = $labTestIDs;
			}
		}
		function setProcedureIDs($procedureIDs) 
		{
			if($procedureIDs!="")
			{
				$this->procedureIDs = $procedureIDs;
			}
		}
	
		
		
		##################################################################### GET FUNCTIONS  ########################
		
		function getPrescriptionID() 
		{
			if($this->prescriptionID!="")
			{
				return $this->prescriptionID ;
			}
			else
			{
				throw new MyException(118127);
			}
		}
		
		
		function getOrganizationID()
		{
			if($this->organizationID!="")
			{
				return $this->organizationID;
			}
			else
			{
				throw new MyException(118128);
			}
			
		}
		
		function getLocationID()
		{
			if($this->locationID!="")
			{
				return $this->locationID;
			}
			else
			{
				throw new MyException(118129);
			}
			
		}
		
		function getIpdBedAllotmentID()
		{
			if($this->ipdBedAllotmentID!="")
			{
				return $this->ipdBedAllotmentID;
			}
			else
			{
				throw new MyException(118130);
			}
		}
	  	function getDoctorID()
		{
			if($this->doctorID!="")
			{
				return $this->doctorID;
			}
			else
			{
				throw new MyException(118131);
			}
		}
		function getPatientID()
		{
			if($this->patientID!="")
			{
				return $this->patientID;
			
			}
			else
			{
				throw new MyException(118132);
			}
		}
		function getRequestedBy()
		{
			if($this->requestedBy!="")
			{
				return $this->requestedBy;
			}
			else
			{
				throw new MyException(118133);
			}
		}
		function getRequestedTime() 
		{
			if($this->requestedTime!="")
			{
				return $this->requestedTime;
			}
		}
		function getPrescriptionType() 
		{
			if($this->prescriptionType!="")
			{
				return $this->prescriptionType;
			}
		}
		function getPrescriptionStatus() 
		{
			if($this->prescriptionStatus!="")
			{
				return $this->prescriptionStatus;
			}
		}
		
		function getPrescriptionItemID() 
		{
			if($this->prescriptionItemID!="")
			{
				return $this->prescriptionItemID ;
			}
			else
			{
				throw new MyException(118134);
			}
		}
		function getDosageID() 
		{
			if($this->dosageID!="")
			{
				return $this->dosageID;
			}
		}
		function getDosage() 
		{
			if($this->dosage!="")
			{
				return $this->dosage;
			}
		}
		function getQuantity() 
		{
			if($this->quantity!="")
			{
				return $this->quantity;
			}
		}
		function getDuration() 
		{
			if($this->duration!="")
			{
				return $this->duration;
			}
		}
		function getMedicineID() 
		{
			if($this->medicineID!="")
			{
				return $this->medicineID;
			}
			else
			{
				throw new MyException(118135);
			}
		}
		function getLabTestID() 
		{
			if($this->labTestID!="")
			{
				return $this->labTestID;
			}
			else
			{
				throw new MyException(118136);
			}
		}
		function getProcedureID() 
		{
			if($this->procedureID!="")
			{
				return $this->procedureID;
			}
			else
			{
				throw new MyException(118137);
			}
		}
		function getMedicineName() 
		{
			if($this->medicineName!="")
			{
				return $this->medicineName;
			}
			else
			{
				throw new MyException(118138);
			}
		}
		function getLabTestName() 
		{
			if($this->labTestName!="")
			{
				return $this->labTestName;
			}
			else
			{
				throw new MyException(118139);
			}
		}
		function getProcedureName() 
		{
			if($this->procedureName!="")
			{
				return $this->procedureName;
			}
			else
			{
				throw new MyException(118140);
			}
		}
		function getDressingID() 
		{
			if($this->dressingID!="")
			{
				return $this->dressingID;
			}
		}
		function getDressing() 
		{
			if($this->dressing!="")
			{
				return $this->dressing;
			}
		}
		
		
		function getMedicineIDs() 
		{
			if($this->medicineIDs!="")
			{
				return $this->medicineIDs;
			}
		}
		function getLabTestIDs() 
		{
			if($this->labTestIDs!="")
			{
				return $this->labTestIDs;
			}
		}
		function getProcedureIDs() 
		{
			if($this->procedureIDs!="")
			{
				return $this->procedureIDs;
			}
		}
	
		##################################################################### Check FUNCTIONS  #####################
		
	  	function checkMedicineLabTestProcedureAlreadyInIPDPrescription($ipd_bed_allotment_id,$type,$id) 
		{
			if($type=='medicine')
			{
				$column_name = 'product_id';
			}
			if($type=='labtest')
			{
				$column_name = 'labtest_id';
			}
			if($type=='procedure')
			{
				$column_name = 'procedure_id';
			}
			if($type=='dressing')
			{
				$column_name = 'dressing_id';
			}
			
			$query = "SELECT presc_item_id FROM presc_items WHERE ipd_bed_allotment_id = '$ipd_bed_allotment_id' AND $column_name = '$id'";
			$queryOb = new classQueryGlobal;
			$array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
			
			if(isset($array[0]['presc_item_id'])&&$array[0]['presc_item_id']!= "")
			{
				return 0;
			}
			else
			{
				return 1;
			}
			
			
		}
		
		##################################################################### Main INSERT FUNCTIONS  ###################
		
		function execute() 
		{
			$location_id = $this->getLocationID();
			$patient_id = $this->getPatientID();
			$requested_by = $this->getRequestedBy();
			$requested_time=time();
			$ipd_bed_allotment_id = $this->getIpdBedAllotmentID();
			$ipd_presc_type = $this->getPrescriptionType();
			
			$this->retrieveSpecificRecentIPDPresciption($location_id,$ipd_presc_type,$ipd_bed_allotment_id);
			
			if(is_null($this->prescriptionID))
			{
				$query="INSERT INTO ipd_presciption 
				(
					location_id,
					patient_id,
					requested_by,
					requested_time,
					ipd_bed_allotment_id,
					ipd_presc_type
				) 
				VALUES
				(
					'$location_id',
					'$patient_id',
					'$requested_by',
					'$requested_time',
					'$ipd_bed_allotment_id',
					'$ipd_presc_type'
					
				)";
				$queryOb = new classQueryGlobal;
				$this->prescriptionID = $queryOb->check_fetch_query($query,'insert',$server="",$debug_mode="off");
			}
			
			if($ipd_presc_type=='medicine')
			{
				$this->insertPrescriptionItemsForIPD($this->getPrescriptionID(),$ipd_presc_type,$this->getMedicineIDs());	
			}
			if($ipd_presc_type=='labtest')
			{
				$this->insertPrescriptionItemsForIPD($this->getPrescriptionID(),$ipd_presc_type,$this->getLabTestIDs());
			}
			if($ipd_presc_type=='procedure')
			{
				$this->insertPrescriptionItemsForIPD($this->getPrescriptionID(),$ipd_presc_type,$this->getProcedureIDs());
			}
			
		}
		
		function insertPrescriptionItemsForIPD($ipd_presc_id,$type,$ids)
		{
			if($type=='medicine')
			{
				$column_name = 'product_id';
			}
			if($type=='labtest')
			{
				$column_name = 'labtest_id';
			}
			if($type=='procedure')
			{
				$column_name = 'procedure_id';
			}
			if($type=='dressing')
			{
				$column_name = 'dressing_id';
			}
			$requested_time = time();
			for($p=0;$p<count($ids);$p++)
			{	
				$id = $ids[$p];
				if($this->checkMedicineLabTestProcedureAlreadyInIPDPrescription($ipd_presc_id,$type,$id))
				{
					$query = "INSERT INTO presc_items (ipd_presc_id,$column_name,presc_type) VALUES ('$ipd_presc_id','$id','ipd')";
					$queryOb = new classQueryGlobal;
					$queryOb->check_fetch_query($query,'insert',$server="",$debug_mode="off");
				}
				else
				{
					//$token = array('var1'=>ucwords($type));
					//throw new MyException(113128,$token);
				}
			}
		}
		
		##################################################################### Main UPDATE FUNCTIONS  ##########
	  
	  	function updateDosage()
		{
			$visit_id = $this->getVisitID();
			$dosage = $this->getDosage();
			
			foreach($dosage as $key => $value)
			{
				if($value!='')
				{
					$val = $value;
				}
				else
				{
					$val = '';
				}
				$product_id = $key;
				$dosage = $val;
				
				$query = "UPDATE presc_items SET dosage_id = '$dosage' WHERE visit_id='$visit_id' AND product_id='$product_id'";
				$queryOb = new classQueryGlobal;
				$queryOb->check_fetch_query($query,'update',$server="",$debug_mode="off");
				
			}
		}
		function updateDosageQuantity()
		{
			$visit_id = $this->getVisitID();
			$quantity = $this->getQuantity();
			
			foreach($quantity as $key => $value)
			{
				if($value!='')
				{
					$val = $value;
				}
				else
				{
					$val = '';
				}
				$product_id = $key;
				$quantity = $val;
				
				$query = "UPDATE presc_items SET quantity = '$quantity' WHERE visit_id='$visit_id' AND product_id='$product_id'";
				$queryOb = new classQueryGlobal;
				$queryOb->check_fetch_query($query,'update',$server="",$debug_mode="off");
				
			}
		}
		function updateDosageDuration()
		{
			$visit_id = $this->getVisitID();
			$duration = $this->getDuration();
			
			foreach($duration as $key => $value)
			{
				if($value!='')
				{
					$val = $value;
				}
				else
				{
					$val = '';
				}
				$product_id = $key;
				$duration = $val;
				
				$query = "UPDATE presc_items SET duration = '$duration' WHERE visit_id='$visit_id' AND product_id='$product_id'";
				$queryOb = new classQueryGlobal;
				$queryOb->check_fetch_query($query,'update',$server="",$debug_mode="off");
				
			}
		}
		function updateDressingDuration()
		{
			$visit_id = $this->getVisitID();
			$duration = $this->getDuration();
			
			foreach($duration as $key => $value)
			{
				if($value!='')
				{
					$val = $value;
				}
				else
				{
					$val = '';
				}
				$dressing_id = $key;
				$duration = $val;
				
				$query = "UPDATE presc_items SET duration = '$duration' WHERE visit_id='$visit_id' AND dressing_id='$dressing_id'";
				$queryOb = new classQueryGlobal;
				$queryOb->check_fetch_query($query,'update',$server="",$debug_mode="off");
				
			}
		}
		
		##################################################################### Main DELETE  FUNCTIONS  ##########
		
		function deletePrescription()
		{
			$presc_item_id = $this->getPrescriptionItemID();
			
			$query = "DELETE FROM presc_items WHERE  presc_item_id = '$presc_item_id'";
			$queryOb = new classQueryGlobal;
			$queryOb->check_fetch_query($query,'delete',$server="",$debug_mode="off");
		}
		
		##################################################################### Main RETRIEVE FUNCTIONS  ##########
		
		function retrieveAllIPDPresciption()
		{
			$location_id = $this->getLocationID();
			
			$query = "SELECT * FROM ipd_presciption WHERE location_id = '$location_id' order by requested_time DESC";
			$queryOb = new classQueryGlobal;
			$visits_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
			
			if(isset($presc_array[0]))
			{
				return $presc_array;
			}
			
		}
		
		function countAllIPDPresciptionByType($location_id,$ipd_presc_type,$from=NULL,$to=NULL)
		{
			
			$query = "SELECT count(*) FROM ipd_presciption WHERE location_id = '$location_id' AND ipd_presc_type = '$ipd_presc_type'";
			
			if(!is_null($from)&&!is_null($to))
			{
				$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 requested_time  > $startTime";
					}
				}
				
				if($endTime!="")
				{
					if(($startTime!="" && $startTime < $endTime) || $startTime="")
					{
						$query .= " AND requested_time  <= $endTime";
					}
				}
			}
			$query .= ' order by requested_time DESC';
			$queryOb = new classQueryGlobal;
			$presc_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
			
			if(isset($presc_array[0]['count(*)']))
			{
				return $presc_array[0]['count(*)'];
			}
			
		}
		
		function retrieveAllIPDPresciptionByType($location_id,$ipd_presc_type,$from=NULL,$to=NULL,$limit=NULL)
		{
			$query = "SELECT * FROM ipd_presciption WHERE location_id = '$location_id' AND ipd_presc_type = '$ipd_presc_type'";
			
			if(!is_null($from)&&!is_null($to))
			{
				$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 requested_time  > $startTime";
					}
				}
				
				if($endTime!="")
				{
					if(($startTime!="" && $startTime < $endTime) || $startTime="")
					{
						$query .= " AND requested_time  <= $endTime";
					}
				}
			}
			$query .= ' order by requested_time DESC';
			
			if(!is_null($limit))
			{
				$query .= " LIMIT $limit";
			}
			
			$queryOb = new classQueryGlobal;
			$presc_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
			
			if(isset($presc_array[0]))
			{
				return $presc_array;
			}
			
		}
		
		function retrieveSpecificRecentIPDPresciption($location_id,$ipd_presc_type,$ipd_bed_allotment_id)
		{
			$query = "SELECT * FROM ipd_presciption WHERE location_id = '$location_id' AND ipd_presc_type = '$ipd_presc_type' AND ipd_bed_allotment_id = '$ipd_bed_allotment_id' AND ipd_presc_status != 'processed' ORDER BY requested_time DESC limit 1";
			$queryOb = new classQueryGlobal;
			$presc_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
			/*echo '<pre>';
			print_r($presc_array);
			echo '</pre>';*/
			
			if(isset($presc_array[0]))
			{
				$this->prescriptionID = $presc_array[0]['ipd_presc_id'];
				$this->locationID = $presc_array[0]['location_id'];
				$this->patientID = $presc_array[0]['patient_id'];
				$this->requestedBy = $presc_array[0]['requested_by'];
				$this->requestedTime = $presc_array[0]['requested_time'];
				$this->prescriptionType = $presc_array[0]['ipd_presc_type'];
				$this->prescriptionStatus = $presc_array[0]['ipd_presc_status'];
				$this->ipdBedAllotmentID = $presc_array[0]['ipd_bed_allotment_id'];
				
			}
		}
		
		function retrieveAllIPDPresciptionByAllotID()
		{
			$ipd_bed_allotment_id = $this->getIpdBedAllotmentID();
			
			$query = "SELECT * 
			FROM ipd_presciption JOIN presc_items 
			ON ipd_presciption.ipd_presc_id = presc_items.ipd_presc_id
			WHERE ipd_bed_allotment_id = '$ipd_bed_allotment_id'  
			ORDER BY requested_time DESC";
			$queryOb = new classQueryGlobal;
			$presc_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
			
			if(isset($presc_array[0]))
			{
				
				return $presc_array;	
				
			}
		}
		
		function retrieveSpecificIPDPresciptionByAllotID()
		{
			$ipd_bed_allotment_id = $this->getIpdBedAllotmentID();
			$ipd_presc_type = $this->getPrescriptionType();
			
			$query = "SELECT * 
			FROM ipd_presciption JOIN presc_items 
			ON ipd_presciption.ipd_presc_id = presc_items.ipd_presc_id
			WHERE ipd_bed_allotment_id = '$ipd_bed_allotment_id' AND ipd_presc_type = '$ipd_presc_type'  
			ORDER BY requested_time DESC";
			$queryOb = new classQueryGlobal;
			$presc_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
			
			if(isset($presc_array[0]))
			{
				return $presc_array;	
				
			}
		}
		
		function retrieveIPDPresciptionHaivingTests()
		{
			$location_id = $this->getLocationID();
			$query = "SELECT  p.* FROM ipd_presciption p WHERE  p.location_id = '$location_id' AND  p.ipd_presc_id IN ( SELECT ipd_presc_id FROM presc_items) ";
			$queryOb = new classQueryGlobal;
			$ipd_presc_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
			
			if(isset($ipd_presc_array[0]))
			{
				return $ipd_presc_array;
			}
		}
		
		function retrievePrescriptionItems()
		{
			$ipd_presc_id = $this->getPrescriptionID();
			
			$query = "SELECT * FROM presc_items LEFT JOIN dosage ON presc_items.dosage_id = dosage.dosage_id  WHERE ipd_presc_id = '$ipd_presc_id'";
			$queryOb = new classQueryGlobal;
			$prec_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
			
			if(isset($prec_array[0]))
			{
				return $prec_array;
			}
			
		}
}
?>