<?php
class classShift
{
	  	private $userName=NULL;
		private $doctorID=NULL;
		private $shiftID=NULL;
		private $shiftStatus=NULL;
		private $shiftType=NULL;
		private $shiftDate=NULL;
		private $shiftStartTime=NULL;
		private $shiftEndTime=NULL;
		private $locationID=NULL;
		private $assistantID=NULL;
		private $createdBy=NULL;
		
		
		########################################## set functions ##############################
	  
	  	function setUserName($userName) 
		{
			if($userName!="")
			{
				$this->userName = $userName;
			}
			else
			{
				throw new MyException(109100);
			}
		}
	  	function setDoctorID($doctorID)
		{
			if($doctorID!="")
			{
				if(classFilter::validateInteger($doctorID))
				{
					$this->doctorID = classFilter::addMySlashes($doctorID);
				}
				else
				{
					throw new MyException(109101);
				}
			}
			else
			{
				throw new MyException(109102);
			}
		}
		function setShiftID($shiftID) 
		{
			if($shiftID!="")
			{
				if(classFilter::validateInteger($shiftID))
				{
					$this->shiftID = classFilter::addMySlashes($shiftID);
				}
				else
				{
					throw new MyException(109103);
				}
			}
			else
			{
				throw new MyException(109104);
			}
		}
		function setShiftStatus($shiftStatus) 
		{
			if($shiftStatus!="")
			{
				if(classFilter::validateStringWithoutSpecialCharacters($shiftStatus))
				{
					$this->shiftStatus = classFilter::addMySlashes($shiftStatus);
				}
				else
				{
					throw new MyException(109105);
				}
			}
			else
			{
				throw new MyException(109106);
			}
		}
		function setShiftType($shiftType) 
		{
			if($shiftType!="")
			{
				$this->shiftType = $shiftType;
			}
			else
			{
				throw new MyException(109121);
			}
		}
		function setShiftDate($shiftDate) 
		{
			if($shiftDate!="")
			{
				$this->shiftDate = $shiftDate;
			}
		}
		function setShiftStartTime($shiftStartTime) 
		{
			if($shiftStartTime!="")
			{
				$this->shiftStartTime = $shiftStartTime;
			}
			else
			{
				throw new MyException(109107);
			}
		}
		function setShiftEndTime($shiftEndTime) 
		{
			if($shiftEndTime!="")
			{
				$this->shiftEndTime = $shiftEndTime;
			}
			else
			{
				throw new MyException(109108);
			}
		}
		
		function setLocationID($locationID)
		{
			if($locationID!="")
			{
				$this->locationID=$locationID;	
			}
			else
			{
				throw new MyException(109109);
				
			}
		}
		
		function setAssistantID($assistantID)
		{
			if($assistantID!="")
			{
				if(classFilter::validateInteger($assistantID))
				{
					$this->assistantID = classFilter::addMySlashes($assistantID);
				}
				else
				{
					throw new MyException(109118);
				}
			}
			else
			{
				throw new MyException(109119);
			}
		}
		function setCreatedBy($createdBy)
		{
			if($createdBy!="")
			{
				$this->createdBy = $createdBy;
			}
			else
			{
				throw new MyException(109122);
			}
		}
		
		
		##################################################################### GET FUNCTIONS  ########################

		function getUserName() 
		{
			if($this->userName!="")
			{
				return $this->userName;
			}
			else
			{
				throw new MyException(109110);
			}
		}
	  	function getDoctorID()
		{
			if($this->doctorID!="")
			{
				return $this->doctorID;
			}
			else
			{
				throw new MyException(109111);
			}
		}
		function getShiftID() 
		{
			if($this->shiftID!="")
			{
				return $this->shiftID;
			}
			else
			{
				throw new MyException(109112);
			}
		}
		function getShiftStatus() 
		{
			if($this->shiftStatus!="")
			{
				return $this->shiftStatus;
			}
			else
			{
				throw new MyException(109113);
			}
		}
		function getShiftType() 
		{
			if($this->shiftType!="")
			{
				return $this->shiftType;
			}
			else
			{
				throw new MyException(109123);
			}
		}
		function getShiftDate() 
		{
			if($this->shiftDate!="")
			{
				return $this->shiftDate;
			}
		}
		function getShiftStartTime() 
		{
			if($this->shiftStartTime!="")
			{
				return $this->shiftStartTime;
			}
			else
			{
				throw new MyException(109114);
			}
		}
		function getShiftEndTime() 
		{
			if($this->shiftEndTime!="")
			{
				return $this->shiftEndTime;
			}
			else
			{
				throw new MyException(109115);
			}
		}
		
		function getLocationID()
		{
			if($this->locationID!="")
			{
				return $this->locationID;	
			}
			else
			{
				throw new MyException(109116);
				
			}
		}
		
		function getAssistantID()
		{
			if($this->assistantID!="")
			{
				return $this->assistantID;
			}
			else
			{
				throw new MyException(109120);
			}
		}
		
		function getCreatedBy()
		{
			if($this->createdBy!="")
			{
				return $this->createdBy;
			}
			else
			{
				throw new MyException(109124);
			}
		}
		
		
		##################################################################### Check FUNCTIONS  #####################
	  
		function checkShiftAlreadyCreated($doctorID,$location_id) 
		{
			/*$by_date = date('Y-m-d',$by_date);
			$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]);
			*/
			
			$query = "SELECT doctor_id FROM shifts WHERE doctor_id = '$doctorID' AND  location_id = '$location_id' AND shift_status = 'Open'";
			/*if($startTime!="")
			{
				if(($endTime!="" && $endTime > $startTime) || $endTime="")
				{
					$query .= " AND date  > $startTime";
				}
			}
			
			if($endTime!="")
			{
				if(($startTime!="" && $startTime < $endTime) || $startTime="")
				{
					$query .= " AND date  <= $endTime";
				}
			}*/
			$queryOb = new classQueryGlobal;
			$array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
			
			if(isset($array[0]['doctor_id'])&&$array[0]['doctor_id']!= "")
			{
				throw new MyException(109117);
			}
			else
			{
				return 1;
			}
		}
		
		function checkShiftAlreadyCreatedAgainstLocation($shift_type,$location_id) 
		{
			$query = "SELECT shift_id FROM shifts WHERE shift_type = '$shift_type' AND location_id = '$location_id' AND shift_status = 'Open'";
			$queryOb = new classQueryGlobal;
			$array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
			
			if(isset($array[0]['shift_id'])&&$array[0]['shift_id']!= "")
			{
				throw new MyException(109125);
			}
			else
			{
				return 1;
			}
			
			
		}
		
		##################################################################### Main INSERT FUNCTIONS  ###################
		
		function execute() 
		{
			
				$date=$this->getShiftDate();
				$shift_status = $this->getShiftStatus();
				$shift_type = $this->getShiftType();
				$created_by = $this->getCreatedBy();
				$username=$this->getUserName();
				$start_time=time();
				$location_id = $this->getLocationID();
				
				
				if($shift_type=='opd')
				{
					//$this->checkShiftAlreadyCreated($this->getDoctorID(),$date);
					$this->checkShiftAlreadyCreated($this->getDoctorID(),$location_id);
					$doctor_id=$this->getDoctorID();
				}
				else
				{
					$this->checkShiftAlreadyCreatedAgainstLocation($shift_type,$location_id);
					$doctor_id=0;
				}
				$query="INSERT INTO shifts 
				(
					
					username,
					doctor_id,
					shift_status,
					start_time,
					date,
					location_id,
					shift_type,
					created_by
				) 
				VALUES
				(
					'$username',
					'$doctor_id',
					'$shift_status',
					'$start_time',
					'$date',
					'$location_id',
					'$shift_type',
					'$created_by'
				)";
				$queryOb = new classQueryGlobal;
				$this->shiftID = $queryOb->check_fetch_query($query,'insert',$server="",$debug_mode="off");
			
		}
		
		##################################################################### Main UPDATE FUNCTIONS  ###################
	  
	  	function closeShift()
		{
			$end_time = $this->getShiftEndTime();
			$shift_status =$this->getShiftStatus();
			$shift_id = $this->getShiftID();
			
			$query = "UPDATE shifts SET end_time = '$end_time' , shift_status = '$shift_status' WHERE shift_id = '$shift_id'";
			$queryOb = new classQueryGlobal;
			$queryOb->check_fetch_query($query,'update',$server="",$debug_mode="off");
			
			
		}
		
		function reOpenShift()
		{
			$shift_status =$this->getShiftStatus();
			$shift_id = $this->getShiftID();
			
			$query = "UPDATE shifts SET shift_status = '$shift_status' WHERE shift_id = '$shift_id'";
			$queryOb = new classQueryGlobal;
			$queryOb->check_fetch_query($query,'update',$server="",$debug_mode="off");
			
			
		}
		
		##################################################################### Main DELETE  FUNCTIONS  ###################
		
		function deleteShift()
		{
			$shift_id = $this->getShiftID();
			
			$query = "DELETE FROM shifts WHERE shift_id = '$shift_id'";
			$queryOb = new classQueryGlobal;
			$queryOb->check_fetch_query($query,'delete',$server="",$debug_mode="off");
			
			
		}
		
		##################################################################### Main RETRIEVE FUNCTIONS  ##########
		
		function retrieveAllShifts($limit=NULL)
		{
			$location_id = $this->getLocationID();
			$shift_type = $this->getShiftType();
			$query = "SELECT * FROM  shifts WHERE location_id = '$location_id' AND shift_type = '$shift_type' order by date DESC ,  start_time DESC";
			if(!is_null($limit))
			{
				$query .= " LIMIT $limit";
			}
			$queryOb = new classQueryGlobal;
			$shift_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
			
			if(isset($shift_array[0]))
			{
				return $shift_array;
			}
		}
		
		function retrieveShiftByShiftID()
		{
			$shift_id = $this->getShiftID();
			$query = "SELECT * FROM  shifts WHERE shift_id = '$shift_id'";
			$queryOb = new classQueryGlobal;
			$shift_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
			
			if(isset($shift_array[0]))
			{
				return $shift_array;
			}
		}
		
		function retrieveShiftDateByShiftID($shift_id)
		{
			$query = "SELECT date FROM  shifts WHERE shift_id = '$shift_id'";
			$queryOb = new classQueryGlobal;
			$shift_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
			
			if(isset($shift_array[0]['date']))
			{
				return $shift_array[0]['date'];
			}
		}
		
		function retrieveDoctorShifts($limit=NULL)
		{
			$doctor_id = $this->getDoctorID();
			$location_id = $this->getLocationID();
			
			$query = "SELECT * FROM  shifts JOIN doctors ON shifts.doctor_id = doctors.doctor_id WHERE doctors.user_id = '$doctor_id'  AND doctors.location_id = '$location_id' order by start_time DESC";
			if(!is_null($limit))
			{
				$query .= " LIMIT $limit";
			}
			$queryOb = new classQueryGlobal;
			$shift_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
			
			if(isset($shift_array[0]))
			{
				return $shift_array;
			}
		}
		
		function retrieveAssistantDoctorShifts()
		{
			$assistant_id = $this->getAssistantID();
			$location_id = $this->getLocationID();
			
			$query = "SELECT * FROM shifts JOIN doctor_assistant_relationship ON shifts.doctor_id = doctor_assistant_relationship.doctor_id WHERE assistant_id = '$assistant_id' AND location_id = '$location_id' order by start_time DESC";
			$queryOb = new classQueryGlobal;
			$shift_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
			
			if(isset($shift_array[0]))
			{
				return $shift_array;
			}
		}
		
		function retrieveShiftByLocationIDTypeAndStatus($location_id,$status,$type)
		{
			$query = "SELECT * FROM  shifts WHERE location_id = '$location_id' AND shift_type = '$type'  AND shift_status = '$status' order by start_time DESC";
			$queryOb = new classQueryGlobal;
			$shift_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
			
			if(isset($shift_array[0]))
			{
				return $shift_array;
			}
		}
		
	
		
	  
}
?>