<?php
class classAppointment
{
	
	private $appID=NULL;
	private $appDate=NULL;
	private $appTimeIn=NULL;
	private $appTimeOut=NULL;
	private $appPurpose=NULL;
	private $appType=NULL;
	private $patientID=NULL;
	private $doctorID=NULL;
	private $locationID=NULL;
	
	################################# SET FUNCTIONS ############################################
	
	function setAppID($appID)
	{
		if($appID!="")
		{
			$this->appID = $organizationID;
		}
		else
		{
			throw new MyException(116100);
		}
		
	}
	function setAppDate($appDate)
	{
		if($appDate!="")
		{
			$this->appDate = $appDate;
		}
		else
		{
			throw new MyException(116101);
		}
		
	}
	function setAppTimeIn($appTimeIn)
	{
		if($appTimeIn!="")
		{
			$this->appTimeIn = $appTimeIn;
		}
		else
		{
			throw new MyException(116102);
		}
		
	}
	function setAppTimeOut($appTimeOut)
	{
		if($appTimeOut!="")
		{
			$this->appTimeOut = $appTimeOut;
		}
		else
		{
			throw new MyException(116103);
		}
		
	}
	
	function setAppPurpose($appPurpose)
	{
		if($appPurpose!="")
		{
			$this->appPurpose = $appPurpose;
		}
		else
		{
			//throw new MyException(116104);
		}
		
	}
	function setAppType($appType)
	{
		if($appType!="")
		{
			$this->appType = $appType;
		}
		else
		{
			throw new MyException(116105);
		}
		
	}
	
	function setPatientID($patientID)
	{
		if($patientID!="")
		{
			$this->patientID = $patientID;
		}
		else
		{
			throw new MyException(116106);
		}
		
	}
	function setDoctorID($doctorID)
	{
		if($doctorID!="")
		{
			$this->doctorID = $doctorID;
		}
		else
		{
			throw new MyException(116107);
		}
		
	}
	function setLocationID($locationID)
	{
		if($locationID!="")
		{
			$this->locationID = $locationID;
		}
		else
		{
			throw new MyException(116108);
		}
		
	}
	
	################################# GET FUNCTIONS ############################################
	
	function getAppID()
	{
		if($this->appID!="")
		{
			return $this->appID;
		}
		else
		{
			throw new MyException(116109);
		}
		
	}
	function getAppDate()
	{
		if($this->appDate!="")
		{
			return $this->appDate;
		}
		else
		{
			throw new MyException(116110);
		}
		
	}
	function getAppTimeIn()
	{
		if($this->appTimeIn!="")
		{
			return $this->appTimeIn;
		}
		else
		{
			throw new MyException(116111);
		}
		
	}
	function getAppTimeOut()
	{
		if($this->appTimeOut!="")
		{
			return $this->appTimeOut;
		}
		else
		{
			throw new MyException(116112);
		}
		
	}

	function getAppPurpose()
	{
		if($this->appPurpose!="")
		{
			return $this->appPurpose;
		}
		else
		{
			//throw new MyException(116113);
		}
		
	}
	function getAppType()
	{
		if($this->appType!="")
		{
			return $this->appType;
		}
		else
		{
			throw new MyException(116114);
		}
		
	}
	
	function getPatientID()
	{
		if($this->patientID!="")
		{
			return $this->patientID;
		}
		else
		{
			throw new MyException(116115);
		}
		
	}
	function getDoctorID()
	{
		if($this->doctorID!="")
		{
			return $this->doctorID;
		}
		else
		{
			throw new MyException(116116);
		}
		
	}
	function getLocationID()
	{
		if($this->locationID!="")
		{
			return $this->locationID;
		}
		else
		{
			throw new MyException(116117);
		}
		
	}
	
	################################# MAIN CHECK FUNCTIONS ############################################
	
	function checkAppointmentAlreadyExist($location_id,$doctor_id,$app_date,$app_timein,$app_timeout)
	{
		$from = (date('Y-m-d',$app_date));
		$to = (date('Y-m-d',$app_date));
		
		$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 app_id FROM appointments WHERE location_id = '$location_id' AND doctor_id = '$doctor_id'";
		
		//$query .= " AND app_timein >= '$app_timein' AND app_timein  <= '$app_timeout'";
		
		$query .= " AND app_timeout > '$app_timein' AND app_timeout  <= '$app_timeout'";
		
		if($startTime!="")
		{
			if(($endTime!="" && $endTime > $startTime) || $endTime="")
			{
				$query .= " AND app_date  >= $startTime";
			}
		}
		
		if($endTime!="")
		{
			if(($startTime!="" && $startTime < $endTime) || $startTime="")
			{
				$query .= " AND app_date  <= $endTime";
			}
		}
		
		$queryOb = new classQueryGlobal;
		$app_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($app_array[0]['app_id'])&&$app_array[0]['app_id']!='')
		{
			throw new MyException(116118);
		}
		else
		{
			return 1;
		}
	}
	
	################################# MAIN INSERT FUNCTIONS ############################################
	
	function insertAppoitment()
	{
		$location_id = $this->getLocationID();
		$patient_id = $this->getPatientID();
		$doctor_id = $this->getDoctorID();
		
		$app_date = strtotime($this->getAppDate());
		$app_timein = strtotime($this->getAppTimeIn());
		$app_timeout = strtotime($this->getAppTimeOut());
		
		$app_purpose = $this->getAppPurpose();
		$app_type = $this->getAppType();
		
		$this->checkAppointmentAlreadyExist($location_id,$doctor_id,$app_date,$app_timein,$app_timeout);
		
		$query = "
		INSERT INTO appointments
		(
			location_id,
			patient_id,
			doctor_id,
			app_date,
			app_timein,
			app_timeout,
			app_purpose,
			app_type
			
		)
		VALUES
		(
			'$location_id',
			'$patient_id',
			'$doctor_id',
			'$app_date',
			'$app_timein',
			'$app_timeout',
			'$app_purpose',
			'$app_type'
		)
		";
		$queryOb = new classQueryGlobal;
		$this->appID = $queryOb->check_fetch_query($query,'insert',$server="",$debug_mode="off");
	}
	
	################################# MAIN RETRIEVE FUNCTIONS ############################################
	
	function retrieveAllAppointments()
	{
		$location_id = $this->getLocationID();
		
		$query = "SELECT * FROM appointments WHERE location_id = '$location_id'";
		$queryOb = new classQueryGlobal;
		$app_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($app_array[0]['app_id'])&&$app_array[0]['app_id']!='')
		{
			return $app_array;
		}
		
	}
	
	function retrieveAllAppointmentsDetail()
	{
		$location_id = $this->getLocationID();
		$app_date = $this->getAppDate();
		
		$startDateArray = explode('-',$app_date);
		$startTime = mktime(0,0,0,$startDateArray[1],$startDateArray[2],$startDateArray[0]);
		
		$endDateArray = explode('-',$app_date);
		$endTime = mktime(23,59,59,$endDateArray[1],$endDateArray[2],$endDateArray[0]);
		
		$query = "SELECT doctor_id FROM appointments WHERE location_id = '$location_id'";
		if($startTime!="")
		{
			if(($endTime!="" && $endTime > $startTime) || $endTime="")
			{
				$query .= " AND app_date  >= $startTime";
			}
		}
		
		if($endTime!="")
		{
			if(($startTime!="" && $startTime < $endTime) || $startTime="")
			{
				$query .= " AND app_date  <= $endTime";
			}
		}
		
		$query .= " GROUP BY doctor_id ORDER BY app_id DESC";
		$queryOb = new classQueryGlobal;
		$app_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($app_array[0]['doctor_id'])&&$app_array[0]['doctor_id']!='')
		{
			$old = 0;
			for($a=0;$a<count($app_array);$a++)
			{
				$this->doctorID = $app_array[$a]['doctor_id'];
				$doc_app_array = $this->retrieveDoctorAppointmentsByDate();
				
				$app_array[$a]['apps'] = $doc_app_array;
				
			}
			
		}
		
		return $app_array;
		
	}
	
	function retrieveAllDoctorAppointmentsDetail()
	{
		$location_id = $this->getLocationID();
		$app_date = $this->getAppDate();
		
		$retrieveDocOB = new classDoctor;
		$retrieveDocOB->setLocationID($location_id);
	
		if($_SESSION['security']['userrole_id']==1||$_SESSION['security']['userrole_id']==2||$_SESSION['security']['userrole_id']==3||$_SESSION['security']['userrole_id']==9)
		{
			$doc_array = $retrieveDocOB->retrieveAllDoctors();
		}
		elseif($_SESSION['security']['userrole_id']==4)
		{
			$retrieveDocOB->setCreatedBy($_SESSION['security']['user_id']);
			$doc_array = $retrieveDocOB->retrieveSpecificDoctors();
		}
		elseif($_SESSION['security']['userrole_id']==5)
		{
			
			$retrieveDocOB->setAssistantID($_SESSION['security']['user_id']);
			$doc_array = $retrieveDocOB->retrieveAssistantDoctors();
		}
		
	  	if(isset($doc_array[0]['doctor_id'])&&$doc_array[0]['doctor_id']!='')
		{
			for($d=0;$d<count($doc_array);$d++)
			{
				$this->doctorID = $doc_array[$d]['doctor_id'];
				$doc_app_array = $this->retrieveDoctorAppointmentsByDate();
				$doc_array[$d]['apps'] = $doc_app_array;

			}
		}
		
		return $doc_array;
		
	}
	
	function retrieveDoctorAppointments()
	{
		$location_id = $this->getLocationID();
		$doctor_id = $this->getDoctorID();
		$query = "SELECT * FROM appointments WHERE location_id = '$location_id' AND doctor_id = '$doctor_id' ";
		$queryOb = new classQueryGlobal;
		$app_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($app_array[0]['app_id'])&&$app_array[0]['app_id']!='')
		{
			return $app_array;
		}
		
	}
	
	function retrieveDoctorAppointmentsByDate()
	{
		$location_id = $this->getLocationID();
		$doctor_id = $this->getDoctorID();
		$app_date = $this->getAppDate();
		
		$startDateArray = explode('-',$app_date);
		$startTime = mktime(0,0,0,$startDateArray[1],$startDateArray[2],$startDateArray[0]);
		
		$endDateArray = explode('-',$app_date);
		$endTime = mktime(23,59,59,$endDateArray[1],$endDateArray[2],$endDateArray[0]);
		
		$query = "SELECT * FROM appointments WHERE location_id = '$location_id' AND doctor_id = '$doctor_id'";
		if($startTime!="")
		{
			if(($endTime!="" && $endTime > $startTime) || $endTime="")
			{
				$query .= " AND app_date  >= $startTime";
			}
		}
		
		if($endTime!="")
		{
			if(($startTime!="" && $startTime < $endTime) || $startTime="")
			{
				$query .= " AND app_date  <= $endTime";
			}
		}
		$query .= " ORDER BY app_timeout ASC";
		$queryOb = new classQueryGlobal;
		$app_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($app_array[0]['app_id'])&&$app_array[0]['app_id']!='')
		{
			return $app_array;
		}
		
	}
	
}
?>