<?php
class classDoctor
{
	  
		private $createdBy=NULL;
		private $userID=NULL;
		private $assistantID=NULL;
		private $doctorAssistantID=NULL;
		private $doctorID=NULL;
		private $locationID=NULL;
		private $joinDate=NULL;
		private $title=NULL;
		private $firstName=NULL;
		private $lastName=NULL;
		private $DOB=NULL;
		private $gender=NULL;
		private $address=NULL;
		private $city=NULL;
		private $phoneNumber=NULL;
		private $NIC=NULL;
		private $email=NULL;
		private $specialization=NULL;
		private $degree=NULL;
		private $graduationYear=NULL;
		private $licenseNumber=NULL;
		private $timings=NULL;
		private $fee=NULL;
		private $hospitalShare=NULL;
		
		
	  ########################################## set functions ##############################
	  
	  	function setCreatedBy($createdBy)
		{
			if($createdBy!="")
			{
				if(classFilter::validateInteger($createdBy))
				{
					$this->createdBy = classFilter::addMySlashes($createdBy);
				}
				else
				{
					throw new MyException(108100);
				}
			}
			else
			{
				throw new MyException(108101);
			}
		}
		
		function setUserID($userID)
		{
			if($userID!="")
			{
				//if(classFilter::validateInteger($userID))
				//{
					//$this->userID = classFilter::addMySlashes($userID);
				//}
				//else
				//{
					//throw new MyException(108112);
				//}
				
				$this->userID = $userID;
			}
			else
			{
				throw new MyException(108113);
			}
		}
		
		function setAssistantID($assistantID)
		{
			if($assistantID!="")
			{
				if(classFilter::validateInteger($assistantID))
				{
					$this->assistantID = classFilter::addMySlashes($assistantID);
				}
				else
				{
					throw new MyException(108102);
				}
			}
			else
			{
				throw new MyException(108103);
			}
		}
		
		function setDoctorAssistantID($doctorAssistantID)
		{
			if($doctorAssistantID!="")
			{
				if(classFilter::validateInteger($doctorAssistantID))
				{
					$this->doctorAssistantID = classFilter::addMySlashes($doctorAssistantID);
				}
				else
				{
					throw new MyException(108104);
				}
			}
			else
			{
				throw new MyException(108105);
			}
		}
		
		function setDoctorID($doctorID)
		{
			if($doctorID!="")
			{
				$this->doctorID = $doctorID;
			}
			else
			{
				throw new MyException(108107);
			}
		}
		
		function setLocationID($locationID)
		{
			if($locationID!="")
			{
				$this->locationID = $locationID;
			}
			
		}
		
		function setJoinDate($joinDate)
		{
			if($joinDate!="")
			{
				$this->joinDate = $joinDate;
			}
		}
		
		function setTitle($title)
		{
			if($title!="")
			{
				$this->title = $title;
			}
		}
		
		function setFirstName($firstName) 
		{
			if($firstName!="")
			{
				if(classFilter::validateStringWithoutSpecialCharacters($firstName))
				{
					$this->firstName = classFilter::addMySlashes($firstName);
				}
				else
				{
					throw new MyException(108115);
				}
			}
			else
			{
				throw new MyException(108116);
			}
		}
		
		function setLastName($lastName) 
		{
			if($lastName!="")
			{
				if(classFilter::validateStringWithoutSpecialCharacters($lastName))
				{
					$this->lastName = classFilter::addMySlashes($lastName);
				}
				else
				{
					throw new MyException(108117);
				}
			}
		}
		
		function setDOB($DOB)
		{
			if($DOB!="")
			{
				$this->DOB = $DOB;
			}
		}
		
		function setGender($gender)
		{
			if($gender!="")
			{
				$this->gender = $gender;
			}
		}
		
		function setAddress($address)
		{
			if($address!="")
			{
				$this->address = $address;
			}
		}
		
		function setCity($userCity) 
		{
			if($userCity!="")
			{
				if(classFilter::validateStringWithoutSpecialCharacters($userCity))
				{
					$this->city = classFilter::addMySlashes($userCity);
				}
				else
				{
					throw new MyException(108118);
				}
			}
		}
		
		function setPhoneNumber($userPhoneNumber) 
		{
			if($userPhoneNumber!="")
			{
				if(classFilter::validatePregInteger($userPhoneNumber))
				{
					$this->phoneNumber = classFilter::addMySlashes($userPhoneNumber);
				}
				else
				{
					throw new MyException(108119);
				}
			}
			else
			{
				throw new MyException(108120);
			}
		}
		
		function setNIC($NIC) 
		{
			if($NIC!="")
			{
				if(classFilter::validatePregInteger($NIC))
				{
					$this->NIC = classFilter::addMySlashes($NIC);
				}
				else
				{
					throw new MyException(108121);
				}
			}
		}
		
		function setEmailAddress($email) 
		{
			if($email!="")
			{
				if(classFilter::validateEmail($email))
				{
					$this->email = classFilter::addMySlashes($email);
				}
				else
				{
					throw new MyException(108122);
				}
			}
		}
		
		function setSpecialization($specialization) 
		{
			if($specialization!="")
			{
				$this->specialization = $specialization;
			}
		}
		
		function setDegree($degree) 
		{
			if($degree!="")
			{
				$this->degree = $degree;
			}
		}
		
		function setGraduationYear($graduationYear) 
		{
			if($graduationYear!="")
			{
				if(classFilter::validatePregInteger($graduationYear))
				{
					$this->graduationYear = classFilter::addMySlashes($graduationYear);
				}
				else
				{
					throw new MyException(108123);
				}
			}
		}
		
		function setLicenseNumber($licenseNumber) 
		{
			if($licenseNumber!="")
			{
				$this->licenseNumber = $licenseNumber;
			}
		}
		
		function setTimings($timings) 
		{
			if($timings!="")
			{
				$this->timings = $timings;
			}
		}
		
		function setFee($fee) 
		{
			if($fee!="")
			{
				if(is_numeric($fee))
				{
					$this->fee = $fee;
				}
				else
				{
					throw new MyException(108124);	
				}
			}
			else
			{
				throw new MyException(108125);
			}
		}
		
		function setHospitalShare($hospitalShare) 
		{
			if($hospitalShare!="")
			{
				$this->hospitalShare = $hospitalShare;
			}
		}
		
		##################################################################### GET FUNCTIONS  ########################

		
		function getCreatedBy()
		{
			if($this->createdBy!="")
			{
				return $this->createdBy;
			}
			else
			{
				throw new MyException(108108);
			}
		}
		
		function getUserID()
		{
			if($this->userID!="")
			{
				return $this->userID;
			}
			else
			{
				throw new MyException(108114);
			}
		}
		
		function getAssistantID()
		{
			if($this->assistantID!="")
			{
				return $this->assistantID;
			}
			else
			{
				throw new MyException(108109);
			}
		}
		
		function getDoctorAssistantID()
		{
			if($this->doctorAssistantID!="")
			{
				
				return $this->doctorAssistantID;
			}
			else
			{
				throw new MyException(108110);
			}
		}
		
		function getDoctorID()
		{
			if($this->doctorID!="")
			{
				return $this->doctorID;
			}
			else
			{
				throw new MyException(108111);
			}
		}
		
		function getLocationID()
		{
			if($this->locationID!="")
			{
				return $this->locationID;
			}
			
		}
		
		
		function getJoinDate()
		{
			if($this->joinDate!="")
			{
				return $this->joinDate;
			}
		}
		
		function getTitle()
		{
			if($this->title!="")
			{
				return $this->title;
			}
		}
		
		function getFirstName() 
		{
			if($this->firstName!="")
			{
				return $this->firstName;
			}
			else
			{
				throw new MyException(108126);
			}
		}
		
		function getLastName() 
		{
			if($this->lastName!="")
			{
				return $this->lastName;
			}
		}
		
		function getDOB()
		{
			if($this->DOB!="")
			{
				return $this->DOB;
			}
		}
		
		function getGender()
		{
			if($this->gender!="")
			{
				return $this->gender;
			}
		}
		
		function getAddress()
		{
			if($this->address!="")
			{
				return $this->address;
			}
		}
		
		function getCity() 
		{
			if($this->city!="")
			{
				return $this->city;
			}
			
		}
		
		function getPhoneNumber() 
		{
			if($this->phoneNumber!="")
			{
				return $this->phoneNumber;
			}
			else
			{
				throw new MyException(108127);
			}
		}
		
		function getNIC() 
		{
			if($this->NIC!="")
			{
				return $this->NIC;
			}
			
		}
		
		function getEmailAddress() 
		{
			if($this->email!="")
			{
				return $this->email;
			}
		}
		
		function getSpecialization() 
		{
			if($this->specialization!="")
			{
				return $this->specialization;
			}
		}
		
		function getDegree() 
		{
			if($this->degree!="")
			{
				return $this->degree;
			}
		}
		
		function getGraduationYear() 
		{
			if($this->graduationYear!="")
			{
				return $this->graduationYear;
			}
			
		}
		
		function getLicenseNumber() 
		{
			if($this->licenseNumber!="")
			{
				return $this->licenseNumber;
			}
		}
		
		function getTimings() 
		{
			if($this->timings!="")
			{
				return $this->timings;
			}
		}
		
		function getFee() 
		{
			if($this->fee!="")
			{
				return $this->fee;
			}
			else
			{
				throw new MyException(108128);
			}
		}
		
		function getHospitalShare() 
		{
			if($this->hospitalShare!="")
			{
				return $this->hospitalShare;
			}
		}
		
		##################################################################### Check FUNCTIONS  #####################
	  
		function checkPhoneNumberRegistered($phone) 
		{
			$query = "SELECT doc_phone FROM doctors WHERE doc_phone = '$phone'";
			$result = mysql_query($query) or die(mysql_error());
			$array = mysql_fetch_assoc($result);
			
			if(isset($array['doc_phone'])&&$array['doc_phone']!= "")
			{
				throw new MyException(108129);
			}
			else
			{
				return 1;
			}
			
			
		}
		
		##################################################################### Main FUNCTIONS  ###################
		
		function execute() 
		{
			if($this->checkPhoneNumberRegistered($this->getPhoneNumber()))
			{
				$created_by = $this->getCreatedBy();
				$user_id = $this->getUserID();
				$location_id = $this->getLocationID();
			
				$join_date = $this->getJoinDate();
				$title = $this->getTitle();
				$doc_fname = $this->getFirstName();
				$doc_lname = $this->getLastName();
				$doc_dob = $this->getDOB();
				$doc_gender = $this->getGender();
				$doc_nic = $this->getNIC();
				$doc_address = $this->getAddress();
				$doc_city = $this->getCity();
				$doc_phone = $this->getPhoneNumber();
				$doc_email = $this->getEmailAddress();
				$specialization = $this->getSpecialization();
				$degree = $this->getDegree();
				$grad_year = $this->getGraduationYear();
				$license_no = $this->getLicenseNumber();
				$timings = $this->getTimings();
				$consultancy_fee = $this->getFee();
				$hosp_share = $this->getHospitalShare();
				
				############# create account of dcotor ##################
				$account_name = 'Consultancy Fee: '.$title.' '.$doc_fname.' '.$doc_lname;
				
				$query = "INSERT INTO accounts (acchead_id,account_name,hosp_share,type) VALUES ('1','$account_name','$hosp_share','Income')";
				$queryOb = new classQueryGlobal;
				$account_id = $queryOb->check_fetch_query($query,'insert',$server="",$debug_mode="off");
				############# create account of dcotor ##################
					
				$query="INSERT INTO doctors 
				(
					join_date,
					title,
					doc_fname,
					doc_lname,
					doc_dob,
					doc_gender,
					doc_nic,
					doc_address,
					doc_city,
					doc_phone,
					doc_email,
					specialization,
					degree,
					grad_year,
					license_no,
					timings,
					consultancy_fee,
					hosp_share,
					account_id,
					created_by,
					user_id,
					location_id,
					active
				)  
				VALUES
				(
					'$join_date',
					'$title',
					'$doc_fname',
					'$doc_lname',
					'$doc_dob',
					'$doc_gender',
					'$doc_nic',
					'$doc_address',
					'$doc_city',
					'$doc_phone',
					'$doc_email',
					'$specialization',
					'$degree',
					'$grad_year',
					'$license_no',
					'$timings',
					'$consultancy_fee',
					'$hosp_share',
					'$account_id',
					'$created_by',
					'$user_id',
					'$location_id',
					'1'
				)";
				$queryOb = new classQueryGlobal;
				$this->doctorID = $queryOb->check_fetch_query($query,'insert',$server="",$debug_mode="off");
			}
			
		}
		
		function insertDoctorBasicInfo()
		{
			$join_date = time();
			$doc_fname = $this->getFirstName();
			$doc_lname = $this->getLastName();
			$doc_phone = $this->getPhoneNumber();
			$doc_email = $this->getEmailAddress();
			$consultancy_fee = $this->getFee();
			$hosp_share = $this->getHospitalShare();
			$created_by = $this->getCreatedBy();
			$user_id = $this->getUserID();
			$location_id = $this->getLocationID();
			$title = 'Dr.';
			
			############# create account of dcotor ##################
			$account_name = 'Consultancy Fee: Dr. '.$doc_fname.' '.$doc_lname;
			
			$query = "INSERT INTO accounts (acchead_id,account_name,hosp_share,type) VALUES ('1','$account_name','$hosp_share','Income')";
			$queryOb = new classQueryGlobal;
			$account_id = $queryOb->check_fetch_query($query,'insert',$server="",$debug_mode="off");
			
			############# create basic info of dcotor ##################
			$query="INSERT INTO doctors 
				(
					join_date,
					title,
					doc_fname,
					doc_lname,
					doc_phone,
					doc_email,
					consultancy_fee,
					hosp_share,
					account_id,
					created_by,
					user_id,
					location_id,
					active
				)  
				VALUES
				(
					'$join_date',
					'$title',
					'$doc_fname',
					'$doc_lname',
					'$doc_phone',
					'$doc_email',
					'$consultancy_fee',
					'$hosp_share',
					'$account_id',
					'$created_by',
					'$user_id',
					'$location_id',
					'1'
					
				)";
				
				$queryOb = new classQueryGlobal;
				$queryOb->check_fetch_query($query,'insert',$server="",$debug_mode="off");
			
		}
		
		function insertDoctorAssistantRelationship()
		{	
			$doctor_ids = $this->getDoctorID();
			$assistant_id = $this->getAssistantID();
			
			$this->deleteDoctorAssistantRelationShip($assistant_id);
			
			for($r=0;$r<count($doctor_ids);$r++)
			{
				$doctor_id = $doctor_ids[$r];
				
				$query = "INSERT INTO doctor_assistant_relationship (doctor_id,assistant_id) VALUES ('$doctor_id','$assistant_id')";
				$queryOb = new classQueryGlobal;
				$queryOb->check_fetch_query($query,'insert',$server="",$debug_mode="off");
			}
		}
		
		##################################################################### Main UPDATE FUNCTIONS  ###################
		
		function updateDoctor() 
		{
			
				$created_by = $this->getCreatedBy();
				$user_id = $this->getUserID();
				$location_id = $this->getLocationID();
			
				$join_date = $this->getJoinDate();
				$title = $this->getTitle();
				$doc_fname = $this->getFirstName();
				$doc_lname = $this->getLastName();
				$doc_dob = $this->getDOB();
				$doc_gender = $this->getGender();
				$doc_nic = $this->getNIC();
				$doc_address = $this->getAddress();
				$doc_city = $this->getCity();
				$doc_phone = $this->getPhoneNumber();
				$doc_email = $this->getEmailAddress();
				$specialization = $this->getSpecialization();
				$degree = $this->getDegree();
				$grad_year = $this->getGraduationYear();
				$license_no = $this->getLicenseNumber();
				$timings = $this->getTimings();
				$consultancy_fee = $this->getFee();
				$hosp_share = $this->getHospitalShare();
				
				$doctor_id = $this->getDoctorID();
					
				$query="UPDATE doctors SET
				
					join_date = '$join_date',
					title = '$title',
					doc_fname = '$doc_fname',
					doc_lname = '$doc_lname',
					doc_dob = '$doc_dob',
					doc_gender = '$doc_gender',
					doc_nic = '$doc_nic',
					doc_address = '$doc_address',
					doc_city = '$doc_city',
					doc_phone = '$doc_phone',
					doc_email = '$doc_email',
					specialization = '$specialization',
					degree = '$degree',
					grad_year = '$grad_year',
					license_no = '$license_no',
					timings = '$timings',
					consultancy_fee = '$consultancy_fee',
					hosp_share = '$hosp_share',
					created_by = '$created_by',
					user_id = '$user_id',
					location_id = '$location_id'
					WHERE doctor_id = '$doctor_id'
				";
				$queryOb = new classQueryGlobal;
				$queryOb->check_fetch_query($query,'update',$server="",$debug_mode="off");
		}
		
		##################################################################### Main RETRIEVE FUNCTIONS  ##########
		
		function retrieveAllDoctors()
		{
			$location_id = $this->getLocationID();
			$query = "SELECT * FROM  doctors WHERE location_id = '$location_id' AND active = 1 ORDER BY doc_fname ASC";
			$queryOb = new classQueryGlobal;
			$doc_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
			
			if(isset($doc_array[0]))
			{
				return $doc_array;
			}
		}
		
		function searchDoctor($by_id=NULL,$by_name=NULL,$by_phone=NULL,$location_id=NULL)
		{
			if($by_id!="")
			{
				$query = "SELECT * FROM doctors WHERE  doctor_id = '$by_id' AND location_id = '$location_id'  AND active = 1 order by doc_fname ASC";
			}
			elseif($by_name!="")
			{
				$query = "SELECT * FROM doctors WHERE  active = 1  AND location_id = '$location_id' AND (doc_fname LIKE '%".$by_name."%') OR (doc_lname LIKE '%".$by_name."%') order by doc_fname ASC"; 
			}
			elseif($by_phone!="")
			{
				$query = "SELECT * FROM doctors WHERE  doc_phone LIKE '%".$by_phone."%'  AND location_id = '$location_id'  AND active = 1 order by doc_fname ASC"; 
			}
			
			$queryOb = new classQueryGlobal;
			$doc_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
			
			if(isset($doc_array[0]['doctor_id'])&&$doc_array[0]['doctor_id']!='')
			{
				return $doc_array;
			}
		}
		
		function retrieveSpecificDoctors()
		{
			$user_id = $this->getCreatedBy();
			$location_id = $this->getLocationID();
			$query = "SELECT * FROM doctors WHERE user_id = '$user_id' AND location_id = '$location_id' ORDER BY doc_fname ASC";
			$queryOb = new classQueryGlobal;
			$doc_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
			
			if(isset($doc_array[0]))
			{
				return $doc_array;
			}
		}
		
		function retrieveDoctorByDoctorID()
		{
			$doctor_id = $this->getDoctorID();
			$query = "SELECT * FROM doctors WHERE doctor_id = '$doctor_id'";
			$queryOb = new classQueryGlobal;
			$doc_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
			
			if(isset($doc_array[0]))
			{
				return $doc_array;
			}
		}
		
		function retrieveDoctorNameByDoctorID($doctor_id)
		{
			$query = "SELECT title,doc_fname,doc_lname FROM doctors WHERE doctor_id = '$doctor_id'";
			$queryOb = new classQueryGlobal;
			$doc_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
			
			if(isset($doc_array[0]))
			{
				$doc_name = ucwords($doc_array[0]['title'].'. '.$doc_array[0]['doc_fname'].' '.$doc_array[0]['doc_lname']);
				return $doc_name;
			}
		}
		
		function retrieveAssistantDoctors()
		{
			$assistant_id = $this->getAssistantID();
			$location_id = $this->getLocationID();
			
			$query = "SELECT * FROM  doctors JOIN doctor_assistant_relationship ON doctors.doctor_id = doctor_assistant_relationship.doctor_id WHERE assistant_id = '$assistant_id' AND location_id = '$location_id'";
			$queryOb = new classQueryGlobal;
			$doc_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
			
			if(isset($doc_array[0]))
			{
				return $doc_array;
			}
		}
		
		function retrieveAssistantDoctorIDs()
		{
			$assistant_id = $this->getAssistantID();
			
			$query = "SELECT doctor_id FROM doctor_assistant_relationship WHERE assistant_id = '$assistant_id'";
			$queryOb = new classQueryGlobal;
			$doc_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
			
			if(isset($doc_array[0]))
			{
				return $doc_array;
			}
		}
		
		
		
		##################################################################### Main DELETE  FUNCTIONS  ##########
	  
		function deleteDoctorAssistantRelationShip($assistant_id)
		{
			$query = "DELETE FROM doctor_assistant_relationship WHERE assistant_id = '$assistant_id'";
			$queryOb = new classQueryGlobal;
			$queryOb->check_fetch_query($query,'delete',$server="",$debug_mode="off");
			
		}
		
		function deleteDoctor()
		{
			$doctor_id = $this->getDoctorID();
			
			$query = "UPDATE doctors SET active = 0 WHERE doctor_id = '$doctor_id'";
			$queryOb = new classQueryGlobal;
			$queryOb->check_fetch_query($query,'update',$server="",$debug_mode="off");
		}
	  
}
?>