<?php
	class Attendance
	{
		
		private $attendanceID = NULL;
		private $staffID = NULL;
		private $adate= NULL;
		private $hrsWorked = NULL;
		private $attendanceType = NULL;
		
	
		############################################# SET FUNCTIONS ###########################################
		function setAttendanceID()
		{
			if($attendanceID!="")
			{
			if(classFilter::validateInteger($attendanceID))
			{
			$this->attendanceID = classFilter::addMySlashes($attendanceID);
			}
			else
			{
			throw new MyException(123100);
			}
			}
			else
			{
			throw new MyException(123101);
			}
			
		}
		
		function setStaffID()
		{
			if($staffID!="")
			{
			if(classFilter::validateInteger($staffID))
			{
			$this->staffID = classFilter::addMySlashes($staffID);
			}
			else
			{
			throw new MyException(123102);
			}
			}
			else
			{
			throw new MyException(123103);
			}
		
		}
		
		function setADate()
		{
			if($aDate!="")
			{
			//	if(classFilter::validateDate($jDate))
			//{
			
			$this->aDate = aDate;
			//}
			//else
			//{
			//throw new MyException(122110);	
			//}
			}
			else
			{
			//	throw new MyException(122111);
			}
			
		}
	
		
		function setTYype()
		{
			if($type!="")
			{
			$this->attendanceType = $type;
			}
			
		}
		
			
		function setHrsWorked()
		{
			if($hrsWorked!="")
			{
			if(classFilter::validateNumber($hrsWorked))
			{
			$this->phone = classFilter::addMySlashes($hrsWorkedphone);
			}
			else
			{
			throw new MyException(122113);
			}
			}
			else
			{
			throw new MyException(122114);
			}
			
		}
		
		############################################# GET FUNCTIONS ##########################################
		
		function getattendanceID()
	{
		if($this->attendanceID!="")
		{
			return $this->attendanceID;
		}
		else
		{
			//throw new MyException(122118);
		}
	}
	
	function getStaffID()
	{
		if($this->staffID!="")
		{
			return $this->staffID;
		}
		else
		{
			//throw new MyException(122118);
		}
	}
	
	function getADate()
	{
		if($this->adate!="")
		{
			return $this->adate;
		}
		else
		{
			//throw new MyException(122119);
		}
	}
	
	function getType()
	{
		if($this->attendanceType!="")
		{
			return $this->attendanceType;
		}
	}
	
	function getHrsWorked() 
	{
		if($this->hrsWorked!="")
		{
			return $this->hrsWorked;
		}
		else
		{
		
		}
	}
	
		
		############################################# RETRIEVE FUNCITONS ######################################
		
		
		function retrieveStaffAllAttendanceByStaffID($staffID)
	{
		$query="SELECT * FROM attendance WHERE staff_id = '$staffID'";
		$queryOb = new classQueryGlobal;
		$row = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($row[0]))
		{
			$this->attendanceID=$row[0]['attendance_id'];
			$this->staffID=$row[0]['staff_id']; 
			$this->adate=$row[0]['date'];
			$this->attendanceType=$row[0]['type'];
			$this->hrsWorked=$row[0]['hrs_worked'];
			
		}
	}
	
		
		function retrieveAllStaffAttendanceData()
	{
		$query="SELECT * FROM attendance";
		$queryOb = new classQueryGlobal;
		$row = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($row[0]))
		{
			$this->attendanceID=$row[0]['attendance_id'];
			$this->staffID=$row[0]['staff_id']; 
			$this->adate=$row[0]['date'];
			$this->attendanceType=$row[0]['type'];
			$this->hrsWorked=$row[0]['hrs_worked'];
			
		}
	}
	
		
		function retrieveStaffAttendanceByDate($adate)
	{
		$query="SELECT * FROM attendance WHERE date = '$adate'";
		$queryOb = new classQueryGlobal;
		$row = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($row[0]))
		{
			$this->attendanceID=$row[0]['attendance_id'];
			$this->staffID=$row[0]['staff_id']; 
			$this->adate=$row[0]['date'];
			$this->attendanceType=$row[0]['type'];
			$this->hrsWorked=$row[0]['hrs_worked'];
			
		}
	}
	
		
		function retrieveStaffAttendanceByType($type)
		{
		$query="SELECT * FROM attendance WHERE type = '$type'";
		$queryOb = new classQueryGlobal;
		$row = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($row[0]))
		{
			$this->attendanceID=$row[0]['attendance_id'];
			$this->staffID=$row[0]['staff_id']; 
			$this->adate=$row[0]['date'];
			$this->attendanceType=$row[0]['type'];
			$this->hrsWorked=$row[0]['hrs_worked'];
			
		}
	}
		
		
		############################################# CHECK FUNTIONS #########################################
			function Check($adate,$attendanceID)
		{
			$query = "SELECT date FROM attendance WHERE date = '$adate' AND attendance_id = '$attendanceID'";
			$queryOb = new classQueryGlobal;
			$array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
			if(isset($array[0]['attendance_id']))
			{
				return 0;
			}
			else
			{
				return 1;
			}
			
		}
		
		#############################################INSERT FUNCTIONS ########################################
		
		function executeAttendance() 
			{
						if(Check)
						{
								$staffID=$this->getStaffID();
								$adate=$this->getADate();
								$type = $this->attendanceType();
								$hrsWorked = $this->getHrsWorked();
								
									foreach ($type as $atype)
									{
										$query="INSERT INTO attendance
													(
														staff_id,
														date,
														type,
														hrs_worked
													) 
													VALUES
													(
														'$staffID',
														'$adate',
														'$type',
														'$hrsWorked'
													)";
													$queryOb = new classQueryGlobal;
													$this->attendanceID = $queryOb->check_fetch_query($query,'insert',$server="",$debug_mode="off");
										}
						
						}
								
			
				
				else
				{
					throw new MyException(12124);
				}
			}

		
		############################################# UPDATE FUNCTIONS #######################################
		
		
				function updateAttendance() 
		{
							$attendanceID = $this->getattendanceID();
							$staffID=$this->getStaffID();
							$adate=$this->getADate();
							$type = $this->attendanceType();
							$hrsWorked = $this->getHrsWorked();
			
							$query="UPDATE attendance SET
							
								staff_id = '$staffID',
								date = '$adate',
								type ='$type',
								hrs_worked = '$hrsWorked'
								WEHERE attendance_id = '$attendanceID' AND staff_id='$staffID';
								";
							$queryOb = new classQueryGlobal;
							$this->attendanceID = $queryOb->check_fetch_query($query,'insert',$server="",$debug_mode="off");
			
			
		}
		
		
		############################################# DELETE FUNCTIONS #######################################
		
		function deleteAttendance() 
		{
			$attendanceID = $this->getattendanceID();	
			$staffID = $this->getStaffID();
			$query="DELETE FROM attendance WHERE attendance ='$attendanceID' AND staff_id = '$staffID'";
			$queryOb = new classQueryGlobal;
			$queryOb->check_fetch_query($query,'delete',$server="",$debug_mode="off");
		}
		
		######################################## COUNT FUCTIONS ##################################################
		
			function countAbsentsByStaffID()
		{
			$staffID = $this->getStaffID();
			
			$query="SELECT count(type) FROM attendance WHERE type = 'Absent' AND staff_id = '$staffID'";
			$queryOb = new classQueryGlobal;
			$attendance_details = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
			
			if(isset($attendance_details[0]['count(type)'])&&$attendance_details[0]['count(type)']!='')
			{
				return $attendance_details;
			}
			
		}
		
			function countPresentByStaffID()
		{
			$staffID = $this->getStaffID();
			
			$query="SELECT count(type) FROM attendance WHERE type = 'Present' AND staff_id = '$staffID'";
			$queryOb = new classQueryGlobal;
			$attendance_details = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
			
			if(isset($attendance_details[0]['count(type)'])&&$attendance_details[0]['count(type)']!='')
			{
				return $attendance_details;
			}
			
		}
		
		function counthrsWorkedByStaffID()
		{
			$staffID = $this->getStaffID();
			$adate = $this->getADate();
			
			$query="SELECT count(hrs_worked) FROM attendance WHERE date = '$adate' AND staff_id = '$staffID'";
			$queryOb = new classQueryGlobal;
			$attendance_details = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
			
			if(isset($attendance_details[0]['count(hrs_worked)'])&&$attendance_details[0]['count(hrs_worked)']!='')
			{
				return $attendance_details;
			}
			
		}
		
		
	} // Class Ends Here
	
?>