<?php
class classPatient
{
	  	private $patientAutoID=NULL;
		private $patientID=NULL;
		private $locationID=NULL;
		private $firstName=NULL;
		private $lastName=NULL;
		private $relationShipName=NULL;
		private $DOB=NULL;
		private $age=NULL;
		private $gender=NULL;
		private $address=NULL;
		private $area=NULL;
		private $city=NULL;
		private $phoneNumber=NULL;
		private $NIC=NULL;
		private $email=NULL;
		
		private $attendentName=NULL;
		private $attendentPhoneNumber=NULL;
		private $attendentNIC=NULL;
		
		private $broughtToByName=NULL;
		private $broughtToByPhoneNumber=NULL;
		private $broughtToByNIC=NULL;
		
		private $referringDoctor=NULL;
		private $bloodGroup=NULL;
		private $medicalHistory=NULL;
		private $allergies=NULL;
		private $familyHistory=NULL;
		private $socialHistory=NULL;
		
		private $feePerSession=NULL;
		
		private $regDate=NULL;
		
		
		
	  ########################################## set functions ##############################
	  
	  	function setPatientAutoID($patientAutoID)
		{
			if($patientAutoID!="")
			{
				if(classFilter::validateInteger($patientAutoID))
				{
					$this->patientAutoID = classFilter::addMySlashes($patientAutoID);
				}
				else
				{
					throw new MyException(106100);
				}
			}
			else
			{
				throw new MyException(106101);
			}
		}
	  	function setPatientID($patientID)
		{
			if($patientID!="")
			{
				if(classFilter::validateInteger($patientID))
				{
					
						$this->patientID = classFilter::addMySlashes($patientID);
				}
				else
				{
					throw new MyException(106136);
				}
			}
			else
			{
				throw new MyException(106102);
			}
		}
		
		public function setLocationID($locationID)
		{
			if($locationID!="")
			{
				$this->locationID=$locationID;	
			}
			else
			{
				throw new MyException(106104);
				
			}
		}
		
		function setFirstName($firstName) 
		{
			if($firstName!="")
			{
				if(classFilter::validateName($firstName))
				{
					$this->firstName = classFilter::addMySlashes($firstName);
				}
				else
				{
					throw new MyException(106105);
				}
			}
			else
			{
				throw new MyException(106106);
			}
		}
		
		function setLastName($lastName) 
		{
			if($lastName!="")
			{
				if(classFilter::validateName($lastName))
				{
					$this->lastName = classFilter::addMySlashes($lastName);
				}
				else
				{
					throw new MyException(106107);
				}
			}
			else
			{
				//throw new MyException(106108);
			}
		}
		
		function setRelationShipName($relationShipName) 
		{
			if($relationShipName!="")
			{
				if(classFilter::validateName($relationShipName))
				{
					$this->relationShipName = classFilter::addMySlashes($relationShipName);
				}
				else
				{
					throw new MyException(106141);
				}
			}
		}
		
		function setDOB($DOB)
		{
			if($DOB!="")
			{
				if(classFilter::validateDate($DOB))
				{
					
					$this->DOB = $DOB;
				}
				else
				{
					throw new MyException(106109);	
				}
			}
			else
			{
				throw new MyException(106110);
			}
		}
		
		function setAge($age)
		{
			if($age!="")
			{
				$this->age = $age;
			}
		}
		
		function setGender($gender)
		{
			if($gender!="")
			{
				$this->gender = $gender;
			}
		}
		
		function setAddress($address)
		{
			if($address!="")
			{
				$this->address = $address;
			}
		}
		
		function setArea($area)
		{
			if($area!="")
			{
				$this->area = $area;
			}
		}
		
		function setCity($userCity) 
		{
			if($userCity!="")
			{
				if(classFilter::validateStringWithoutSpecialCharacters($userCity))
				{
					$this->city = classFilter::addMySlashes($userCity);
				}
				else
				{
					throw new MyException(106111);
				}
			}
		}
		
		function setPhoneNumber($userPhoneNumber) 
		{
			if($userPhoneNumber!="")
			{
				if(classFilter::validateNumber($userPhoneNumber))
				{
					$this->phoneNumber = classFilter::addMySlashes($userPhoneNumber);
				}
				else
				{
					throw new MyException(106112);
				}
			}
			else
			{
				throw new MyException(106113);
			}
		}
		
		function setNIC($NIC) 
		{
			if($NIC!="")
			{
				if(classFilter::validateNumber($NIC))
				{
					$this->NIC = classFilter::addMySlashes($NIC);
				}
				else
				{
					throw new MyException(106114);
				}
			}
			else
			{
				//throw new MyException(106115);
			}
		}
		
		function setEmail($email) 
		{
			if($email!="")
			{
				if(classFilter::validateEmail($email))
				{
					$this->email = classFilter::addMySlashes($email);
				}
				else
				{
					throw new MyException(106137);
				}
			}
			else
			{
				//throw new MyException(106138);
			}
		}
		
		function setAttendentName($attendentName) 
		{
			if($attendentName!="")
			{
				if(classFilter::validateName($attendentName))
				{
					$this->attendentName = classFilter::addMySlashes($attendentName);
				}
				else
				{
					throw new MyException(106116);
				}
			}
			//else
//			{
//				if($this->age_from_dob($this->getDOB())<18)
//				{
//					throw new MyException(106117);
//				}
//				
//			}
		}
		
		function setAttendentPhoneNumber($attendentPhoneNumber) 
		{
			if($attendentPhoneNumber!="")
			{
				if(classFilter::validateNumber($attendentPhoneNumber))
				{
					$this->attendentPhoneNumber = classFilter::addMySlashes($attendentPhoneNumber);
				}
				else
				{
					throw new MyException(106118);
				}
			}
			//else
//			{
//				if($this->age_from_dob($this->getDOB())<18)
//				{
//					throw new MyException(106119);
//				}
//			}
		}
		
		function setAttendentNIC($attendentNIC) 
		{
			if($attendentNIC!="")
			{
				if(classFilter::validateNumber($attendentNIC))
				{
					$this->attendentNIC = classFilter::addMySlashes($attendentNIC);
				}
				else
				{
					throw new MyException(106120);
				}
			}
			//else
//			{
//				if($this->age_from_dob($this->getDOB())<18)
//				{
//					throw new MyException(106121);
//				}
//			}
		}
		
		function setBroughtToByName($broughtToByName) 
		{
			if($broughtToByName!="")
			{
				if(classFilter::validateName($broughtToByName))
				{
					$this->broughtToByName = classFilter::addMySlashes($broughtToByName);
				}
				else
				{
					throw new MyException(106122);
				}
			}
		}
		
		function setBroughtToByPhoneNumber($broughtToByPhoneNumber) 
		{
			if($broughtToByPhoneNumber!="")
			{
				if(classFilter::validateNumber($broughtToByPhoneNumber))
				{
					$this->broughtToByPhoneNumber = classFilter::addMySlashes($broughtToByPhoneNumber);
				}
				else
				{
					throw new MyException(106123);
				}
			}
			
		}
		
		function setBroughtToByNIC($broughtToByNIC) 
		{
			if($broughtToByNIC!="")
			{
				if(classFilter::validateNumber($broughtToByNIC))
				{
					$this->broughtToByNIC = classFilter::addMySlashes($broughtToByNIC);
				}
				else
				{
					throw new MyException(106124);
				}
			}
			
		}
		
		function setReferringDoctor($referringDoctor) 
		{
			if($referringDoctor!="")
			{
				$this->referringDoctor = $referringDoctor;
			}
		}
		
		function setBloodGroup($bloodGroup) 
		{
			if($bloodGroup!="")
			{
				$this->bloodGroup = $bloodGroup;
			}
		}
		
		function setMedicalHistory($medicalHistory) 
		{
			if($medicalHistory!="")
			{
				$this->medicalHistory = $medicalHistory;
			}
		}
		
		function setAllergies($allergies) 
		{
			if($allergies!="")
			{
				$this->allergies = $allergies;
			}
		}
		
		function setFamilyHistory($familyHistory) 
		{
			if($familyHistory!="")
			{
				$this->familyHistory = $familyHistory;
			}
		}
		
		function setSocialHistory($socialHistory) 
		{
			if($socialHistory!="")
			{
				$this->socialHistory = $socialHistory;
			}
		}
		
		function setFeePerSession($feePerSession) 
		{
			if($feePerSession!="")
			{
				if(is_numeric($feePerSession))
				{
					$this->feePerSession = $feePerSession;
				}
				else
				{
					throw new MyException(106143);
				}
			}
		}
		
		function setRegDate($regDate) 
		{
			if($regDate!="")
			{
				$this->regDate = $regDate;
			}
		}
		
		
		
		
		##################################################################### GET FUNCTIONS  ########################
		
		function getPatientAutoID()
		{
			if($this->patientAutoID!="")
			{
				return $this->patientAutoID;
			}
			else
			{
				throw new MyException(106125);
			}
		}
		
		function getPatientID()
		{
			if($this->patientID!="")
			{
				return $this->patientID;
			}
			else
			{
				throw new MyException(106126);
			}
		}
		
		public function getLocationID()
		{
			if($this->locationID!="")
			{
				return $this->locationID;	
			}
			else
			{
				throw new MyException(106127);
				
			}
		}
		
		
		function getFirstName() 
		{
			if($this->firstName!="")
			{
				return $this->firstName;
			}
			else
			{
				throw new Exception(106128);
			}
		}
		
		function getLastName() 
		{
			if($this->lastName!="")
			{
				return $this->lastName;
			}
			else
			{
				//throw new Exception(106129);
			}
		}
		
		function getRelationShipName() 
		{
			if($this->relationShipName!="")
			{
				return $this->relationShipName;
			}
		}
		
		function getDOB()
		{
			if($this->DOB!="")
			{
				return $this->DOB;
			}
			else
			{
				throw new Exception(106130);
			}
		}
		
		function getAge()
		{
			if($this->age!="")
			{
				return $this->age;
			}
			
		}
		
		function getGender()
		{
			if($this->gender!="")
			{
				return $this->gender;
			}
			
		}
		
		function getAddress()
		{
			if($this->address!="")
			{
				return $this->address;
			}
			
		}
		
		function getArea()
		{
			if($this->area!="")
			{
				return $this->area;
			}
		
		}
		
		function getCity() 
		{
			if($this->city!="")
			{
				return $this->city;
			}
			
		}
		
		function getPhoneNumber() 
		{
			if($this->phoneNumber!="")
			{
				return $this->phoneNumber;
			}
			else
			{
				//throw new Exception(106131);
			}
		}
		
		function getNIC() 
		{
			if($this->NIC!="")
			{
				return $this->NIC;
			}
			else
			{
				//throw new Exception(106132);
			}
		}
		
		function getEmail() 
		{
			if($this->email!="")
			{
				return $this->email;
			}
			else
			{
				//throw new MyException(106139);
			}
		}
		
		function getAttendentName() 
		{
			if($this->attendentName!="")
			{
				return $this->attendentName;
			}
			else
			{
				//throw new Exception(106133);
			}
		}
		
		function getAttendentPhoneNumber() 
		{
			if($this->attendentPhoneNumber!="")
			{
				return $this->attendentPhoneNumber;
			}
			else
			{
				//throw new Exception(106134);
			}
		}
		
		function getAttendentNIC() 
		{
			if($this->attendentNIC!="")
			{
				return $this->attendentNIC;
			}
			else
			{
				//throw new Exception(106135);
			}
		}
		
		function getBroughtToByName() 
		{
			if($this->broughtToByName!="")
			{
				return $this->broughtToByName;
			}
		}
		
		function getBroughtToByPhoneNumber() 
		{
			if($this->broughtToByPhoneNumber!="")
			{
				return $this->broughtToByPhoneNumber;
			}
		}
		
		function getBroughtToByNIC() 
		{
			if($this->broughtToByNIC!="")
			{
				return $this->broughtToByNIC;
			}
		}
		
		function getReferringDoctor() 
		{
			if($this->referringDoctor!="")
			{
				return $this->referringDoctor;
			}
		}
		
		function getBloodGroup() 
		{
			if($this->bloodGroup!="")
			{
				return $this->bloodGroup;
			}
		}
		
		function getMedicalHistory() 
		{
			if($this->medicalHistory!="")
			{
				return $this->medicalHistory;
			}
		}
		
		function getAllergies() 
		{
			if($this->allergies!="")
			{
				return $this->allergies;
			}
		}
		
		function getFamilyHistory() 
		{
			if($this->familyHistory!="")
			{
				return $this->familyHistory;
			}
		}
		
		function getSocialHistory() 
		{
			if($this->socialHistory!="")
			{
				return $this->socialHistory;
			}
		}
		
		function getFeePerSession() 
		{
			if($this->feePerSession!="")
			{
				return $this->feePerSession;
			}
		}
		
		function getRegDate() 
		{
			if($this->regDate!="")
			{
				return $this->regDate;
			}
		}
		
		
		
		##################################################################### Check FUNCTIONS  #####################
	  
		function checkPatientIDRegistered($patientID,$location_id) 
		{
			$query = "SELECT patient_id FROM patients WHERE patient_id = '$patientID' AND location_id = '$location_id'";
			$queryOb = new classQueryGlobal;
			$array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
			if(isset($array[0]['patient_id']))
			{
				return 0;
			}
			else
			{
				return 1;
			}
			
		}
		function checkPatientPhoneRegistered($phone,$location_id) 
		{
			$query = "SELECT patient_auto_id FROM patients WHERE phone = '$phone' AND location_id = '$location_id'";
			$queryOb = new classQueryGlobal;
			$array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
			if(isset($array[0]['patient_auto_id']))
			{
				return 0;
			}
			else
			{
				return 1;
			}
			
		}
		
	    function age_from_dob($dob) 
	    {
	
			list($y,$m,$d) = explode('-', $dob);
		   
			if (($m = (date('m') - $m)) < 0) 
			{
				$y++;
			} 
			elseif ($m == 0 && date('d') - $d < 0)
			{
				$y++;
			}
	   
			return date('Y') - $y;
		}
		
		##################################################################### MAIN INSERT FUNCTIONS  ###################
		
		function execute() 
		{
			$patient_id = $this->getPatientID();
			$location_id = $this->getLocationID();
			
			if($this->checkPatientIDRegistered($patient_id,$location_id))
			{
				
				
				$reg_date=$this->getRegDate();
				$patient_fname = $this->getFirstName();
				$patient_lname = $this->getLastName();
				$dob = $this->getDOB();
				$gender = $this->getGender();
				$nic = $this->getNIC();
				$address = $this->getAddress();
				$area = $this->getArea();
				$city = $this->getCity();
				$phone = $this->getPhoneNumber();
				$email = $this->getEmail();
				
				
				$parent_name = $this->getAttendentName();
				$parent_phone = $this->getAttendentPhoneNumber();
				$parent_nic = $this->getAttendentNIC();
				
				$emer_name = $this->getBroughtToByName();
				$emer_phone = $this->getBroughtToByPhoneNumber();
				$emer_nic = $this->getBroughtToByNIC();
				
				$ref_doctor = $this->getReferringDoctor();
				$blood_group = $this->getBloodGroup();
				$med_history = $this->getMedicalHistory();
				$fam_history = $this->getFamilyHistory();
				$soc_history = $this->getSocialHistory();
				$allergies = $this->getAllergies();
				
				$fee_per_session = $this->getFeePerSession();
				
				$query="INSERT INTO patients 
				(
					patient_id,
					location_id,
					reg_date,
					patient_fname,
					patient_lname,
					dob,
					gender,
					nic,
					address,
					area,
					city,
					phone,
					email,
					parent_name,
					parent_phone,
					parent_nic,
					emer_name,
					emer_phone,
					emer_nic,
					ref_doctor,
					blood_group,
					med_history,
					fam_history,
					soc_history,
					allergies,
					fee_per_session
				) 
				VALUES
				(
					'$patient_id',
					'$location_id',
					'$reg_date',
					'$patient_fname',
					'$patient_lname',
					'$dob',
					'$gender',
					'$nic',
					'$address',
					'$area',
					'$city',
					'$phone',
					'$email',
					'$parent_name',
					'$parent_phone',
					'$parent_nic',
					'$emer_name',
					'$emer_phone',
					'$emer_nic',
					'$ref_doctor',
					'$blood_group',
					'$med_history',
					'$fam_history',
					'$soc_history',
					'$allergies',
					'$fee_per_session'
				)";
				$queryOb = new classQueryGlobal;
				$this->patientAutoID = $queryOb->check_fetch_query($query,'insert',$server="",$debug_mode="off");
			}
			else
			{
				throw new MyException(106140);
			}
		}
		
		function executeBasicInfo() 
		{
			$phone = $this->getPhoneNumber();
			$location_id = $this->getLocationID();
			
			//if($this->checkPatientPhoneRegistered($phone,$location_id))
			//{
				
				$reg_date=time();
				$patient_fname = $this->getFirstName();
				$patient_lname = $this->getLastName();
				$gender = $this->getGender();
				$age = $this->getAge();
				$address = $this->getAddress();
				$relationship_name = strtolower($this->getRelationShipName());
				$patient_id = $this->nextPatientID($location_id);
				$fee_per_session = $this->getFeePerSession();
				
				$query="INSERT INTO patients 
				(
					location_id,
					patient_id,
					reg_date,
					patient_fname,
					patient_lname,
					gender,
					age,
					address,
					phone,
					relationship_name,
					fee_per_session
					
				) 
				VALUES
				(
					'$location_id',
					'$patient_id',
					'$reg_date',
					'$patient_fname',
					'$patient_lname',
					'$gender',
					'$age',
					'$address',
					'$phone',
					'$relationship_name',
					'$fee_per_session'
					
				)";
				$queryOb = new classQueryGlobal;
				$this->patientAutoID = $queryOb->check_fetch_query($query,'insert',$server="",$debug_mode="off");
			/*}
			else
			{
				throw new MyException(106142);
			}*/
		}
		
		##################################################################### MAIN UPDATE FUNCTIONS  ###################
	  
		function updatePatient() 
		{
			$reg_date=$this->getRegDate();
			
			$patient_auto_id = $this->getPatientAutoID();
			$location_id = $this->getLocationID();
			$patient_id = $this->getPatientID();
			
			$patient_fname = $this->getFirstName();
			$patient_lname = $this->getLastName();
			$dob = $this->getDOB();
			$gender = $this->getGender();
			$nic = $this->getNIC();
			$address = $this->getAddress();
			$area = $this->getArea();
			$city = $this->getCity();
			$phone = $this->getPhoneNumber();
			$email = $this->getEmail();
			
			
			
			$parent_name = $this->getAttendentName();
			$parent_phone = $this->getAttendentPhoneNumber();
			$parent_nic = $this->getAttendentNIC();
			
			$emer_name = $this->getBroughtToByName();
			$emer_phone = $this->getBroughtToByPhoneNumber();
			$emer_nic = $this->getBroughtToByNIC();
			
			$ref_doctor = $this->getReferringDoctor();
			$blood_group = $this->getBloodGroup();
			$med_history = $this->getMedicalHistory();
			$fam_history = $this->getFamilyHistory();
			$soc_history = $this->getSocialHistory();
			$allergies = $this->getAllergies();
			
			$fee_per_session = $this->getFeePerSession();
			
			$query="UPDATE patients SET
			
				patient_id = '$patient_id',
				location_id = '$location_id',
				patient_fname = '$patient_fname',
				patient_lname = '$patient_lname',
				dob = '$dob',
				gender = '$gender',
				nic = '$nic',
				address = '$address',
				area = '$area',
				city = '$city',
				phone = '$phone',
				email = '$email',
				parent_name = '$parent_name',
				parent_phone = '$parent_phone',
				parent_nic = '$parent_nic',
				emer_name = '$emer_name',
				emer_phone = '$emer_phone',
				emer_nic = '$emer_nic',
				ref_doctor = '$ref_doctor',
				blood_group = '$blood_group',
				med_history = '$med_history',
				fam_history = '$fam_history',
				soc_history = '$soc_history',
				allergies = '$allergies',
				fee_per_session = '$fee_per_session'
				WHERE patient_auto_id = '$patient_auto_id'";
				$queryOb = new classQueryGlobal;
				$queryOb->check_fetch_query($query,'update',$server="",$debug_mode="off");
		}
		
		function updatePatientRegistrationFeeStatus($reg_fee,$patient_auto_id)
		{
			$query = "UPDATE patients SET reg_fee = '$reg_fee' , reg_fee_status = 'paid' WHERE patient_auto_id = '$patient_auto_id'";
			$queryOb = new classQueryGlobal;
			$queryOb->check_fetch_query($query,'update',$server="",$debug_mode="off");
		}
		
		##################################################################### MAIN DELETE FUNCTIONS  ###################
	  
		function deletePatient() 
		{
			$patient_auto_id = $this->getPatientAutoID();
			
			$query="DELETE FROM patients WHERE patient_auto_id = '$patient_auto_id'";
			$queryOb = new classQueryGlobal;
			$queryOb->check_fetch_query($query,'delete',$server="",$debug_mode="off");
		}
		
		##################################################################### MAIN RETRIEVE FUNCTIONS  ###################
		
		function nextPatientID($location_id)
		{
			$query="SELECT MAX(patient_id) FROM patients WHERE location_id = '$location_id'"; 
			$queryOb = new classQueryGlobal;
			$patient_id = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
			
			if(isset($patient_id[0]['MAX(patient_id)'])&&$patient_id[0]['MAX(patient_id)']!='')
			{
				$patient_reg_id = $patient_id[0]['MAX(patient_id)']+1;
				
			}
			else
			{
				$patient_reg_id = 1;
			}
			return $patient_reg_id;
		}
		
		function retrievePatientByPatientID()
		{
			$patient_auto_id = $this->getPatientAutoID();
			
			$query="SELECT * FROM patients WHERE patient_auto_id = '$patient_auto_id'";
			$queryOb = new classQueryGlobal;
			$patient_details = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
			
			if(isset($patient_details[0]['patient_auto_id'])&&$patient_details[0]['patient_auto_id']!='')
			{
				return $patient_details;
			}
			
		}
		
		function retrievePatientSessionFeeByPatientID($patient_auto_id)
		{
			$query="SELECT fee_per_session FROM patients WHERE patient_auto_id = '$patient_auto_id'";
			$queryOb = new classQueryGlobal;
			$patient_details = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
			
			if(isset($patient_details[0]['fee_per_session'])&&$patient_details[0]['fee_per_session']!='')
			{
				return $patient_details[0]['fee_per_session'];
			}
			
		}
		
		function retrievePatientNameByPatientID($patient_auto_id)
		{
			$query="SELECT patient_fname,patient_lname FROM patients WHERE patient_auto_id = '$patient_auto_id'";
			$queryOb = new classQueryGlobal;
			$patient_details = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
			
			if(isset($patient_details[0]))
			{
				$patient_name = ucwords($patient_details[0]['patient_fname'].' '.$patient_details[0]['patient_lname']);
				return $patient_name;
			}
			
		}
		
		function retrievePatientByLocationID($limit=NULL)
		{
			$location_id = $this->getLocationID();
			
			if($limit!='')
			{
				$query="SELECT * FROM patients WHERE location_id = '$location_id' order by patient_auto_id DESC limit $limit";	
			}
			else
			{
				$query="SELECT * FROM patients WHERE location_id = '$location_id' order by patient_auto_id DESC";
			}
			
			$queryOb = new classQueryGlobal;
			$patient_details = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
			
			if(isset($patient_details[0]['patient_auto_id'])&&$patient_details[0]['patient_auto_id']!='')
			{
				return $patient_details;
			}
			
		}
		
		function countPatientByLocationID()
		{
			$location_id = $this->getLocationID();
			
			$query="SELECT count(patient_auto_id) FROM patients WHERE location_id = '$location_id' order by reg_date DESC";
			$queryOb = new classQueryGlobal;
			$patient_details = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
			
			if(isset($patient_details[0]['count(patient_auto_id)'])&&$patient_details[0]['count(patient_auto_id)']!='')
			{
				return $patient_details;
			}
			
		}
		
		function searchPatient($by_id=NULL,$by_name=NULL,$by_phone=NULL,$location_id=NULL)
		{
			$query='';
			if($by_id!="")
			{
				$query = "SELECT * FROM patients WHERE  patient_id = '$by_id' AND location_id = '$location_id' order by reg_date DESC";
			}
			elseif($by_name!="")
			{
				$query = "SELECT * FROM patients WHERE  location_id = '$location_id' AND (patient_fname LIKE '%".$by_name."%' OR patient_lname LIKE '%".$by_name."%') order by reg_date DESC"; 
			}
			elseif($by_phone!="")
			{
				$query = "SELECT * FROM patients WHERE  phone LIKE '%".$by_phone."%'  AND location_id = '$location_id' order by reg_date DESC"; 
			}
			
			$queryOb = new classQueryGlobal;
			$patient_details = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
			
			if(isset($patient_details[0]['patient_auto_id'])&&$patient_details[0]['patient_auto_id']!='')
			{
				return $patient_details;
			}
		}
	  
	  	function retrievePatientRegistrationFeeDefault($location_id)
		{
			$query = "SELECT * FROM locations WHERE  location_id = '$location_id'";
			$queryOb = new classQueryGlobal;
			$patient_details = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
			if(isset($patient_details[0]['patient_reg_fee'])&&$patient_details[0]['patient_reg_fee']!='')
			{
				return $patient_details[0]['patient_reg_fee'];
			}
		}
		
}
?>