<?php
class classUser
{
	private $userID;
	private $userName;
	private $password;
	private $userRoleID;
	
	private $firstName;
	private $lastName;
	private $email;
	private $phone;
	
	private $organizationID;
	private $organizationName;
	private $locationID;
	private $locationName;
	
	
	
	private $diagnostic=NULL;
	private $opd=NULL;
	private $ipd=NULL;
	private $pharmacy=NULL;
	private $ot=NULL;
	private $emergency=NULL;
	private $admin=NULL;
	private $patientRegFee=NULL;
	private $noOfLocations=NULL;
	
	private $locCity=NULL;
	private $locAddress=NULL;
	private $locPhone=NULL;
	private $locContactPerson=NULL;
	
	######################################### SET FUNCTION ###################################################
	
	public function setUserID($userID)
	{
		if($userID!="")
		{
			$this->userID=$userID;	
		}
		else
		{
			throw new MyException(103100);
			
		}
	}
	
	public function setUserName($userName)
	{
		if($userName!="")
		{
			$this->userName=$userName;	
		}
		else
		{
			throw new MyException(103101);
			
		}
	}
	
	public function setOrganizationID($organizationID)
	{
		if($organizationID!="")
		{
			$this->organizationID=$organizationID;	
		}
		else
		{
			throw new MyException(103104);
			
		}
	}
	
	public function setOrganizationName($organizationName)
	{
		if($organizationName!="")
		{
			$this->organizationName=$organizationName;	
		}
		else
		{
			throw new MyException(103105);
			
		}
	}
	
	public function setLocationID($locationID)
	{
		if($locationID!="")
		{
			$this->locationID=$locationID;	
		}
		else
		{
			throw new MyException(103106);
			
		}
	}
	
	public function setLocationName($locationName)
	{
		if($locationName!="")
		{
			$this->locationName=$locationName;	
		}
		else
		{
			throw new MyException(103107);
			
		}
	}
	
	
	
	public function setDiagnostic($diagnostic)
	{
		$this->diagnostic = $diagnostic;	
	}
	public function setOPD($opd)
	{
		$this->opd = $opd;	
	}
	public function setIPD($ipd)
	{
		$this->ipd = $ipd;	
	}
	public function setPharmacy($pharmacy)
	{
		$this->pharmacy = $pharmacy;	
	}
	public function setOT($ot)
	{
		$this->ot = $ot;	
	}
	public function setEmergency($emergency)
	{
		$this->emergency = $emergency;	
	}
	public function setAdmin($admin)
	{
		$this->admin = $admin;	
	}
	
	public function setPatientRegFee($patientRegFee)
	{
		if($patientRegFee!='')
		{
			if(is_numeric($patientRegFee))
			{
				$this->patientRegFee = $patientRegFee;	
			}
			else
			{
				throw new MyException(103112);
			}
		}
		else
		{
			throw new MyException(103113);
		}
	}
	
	public function setNoOfLocations($noOfLocations)
	{
		if($noOfLocations!='')
		{
			if(is_numeric($noOfLocations))
			{
				$this->noOfLocations = $noOfLocations;	
			}
			else
			{
				throw new MyException(103115);
			}
		}
		else
		{
			throw new MyException(103116);
		}
	}
	
	public function setLocCity($locCity)
	{
		$this->locCity = $locCity;	
	}
	public function setLocAddress($locAddress)
	{
		$this->locAddress = $locAddress;	
	}
	public function setLocPhone($locPhone)
	{
		$this->locPhone = $locPhone;	
	}
	public function setLocContactPerson($locContactPerson)
	{
		$this->locContactPerson = $locContactPerson;	
	}
	
	
	
	
	######################################### GET FUNCTION ###################################################
	
	public function getUserID()
	{
		if($this->userID!='')
		{
			return $this->userID;
		}
		else
		{
			throw new MyException(103102);
		}
	}
	
	public function getUserName()
	{
		if($this->userName!='')
		{
			return $this->userName;
		}
		else
		{
			throw new MyException(103103);
		}
	}
	
	public function getOrganizationID()
	{
		if($this->organizationID!="")
		{
			return $this->organizationID;	
		}
		else
		{
			throw new MyException(103108);
			
		}
	}
	
	public function getOrganizationName()
	{
		if($this->organizationName!="")
		{
			return $this->organizationName;	
		}
		else
		{
			throw new MyException(103109);
			
		}
	}
	
	public function getLocationID()
	{
		if($this->locationID!="")
		{
			return $this->locationID;	
		}
		else
		{
			throw new MyException(103110);
			
		}
	}
	
	public function getLocationName()
	{
		if($this->locationName!="")
		{
			return $this->locationName;	
		}
		else
		{
			throw new MyException(103111);
			
		}
	}
	
	public function getPassword()
	{
		return $this->password;
	}
	
	public function getUserRoleID()
	{
		return $this->userRoleID;
	}
	
	public function getFirstName()
	{
		return $this->firstName;
	}
	
	public function getLastName()
	{
		return $this->lastName;
	}
	
	public function getEmail()
	{
		return $this->email;
	}
	
	public function getPhone()
	{
		return $this->phone;
	}
	
	
	public function getDiagnostic()
	{
		return $this->diagnostic;	
	}
	public function getOPD()
	{
		return $this->opd ;	
	}
	public function getIPD()
	{
		return $this->ipd;	
	}
	public function getPharmacy()
	{
		return $this->pharmacy;	
	}
	public function getOT()
	{
		return $this->ot;	
	}
	public function getEmergency()
	{
		return $this->emergency;	
	}
	public function getAdmin()
	{
		return $this->admin;	
	}
	
	public function getPatientRegFee()
	{
		if($this->patientRegFee!='')
		{
			return $this->patientRegFee;	
		}
		else
		{
			throw new MyException(103114);
		}
	}
	
	public function getNoOfLocations()
	{
		if($this->noOfLocations!='')
		{
			return $this->noOfLocations ;	
		}
		else
		{
			throw new MyException(103117);
		}
	}
	
	
	public function getLocCity()
	{
		return $this->locCity;	
	}
	public function getLocAddress()
	{
		return $this->locAddress;	
	}
	public function getLocPhone()
	{
		return $this->locPhone;	
	}
	public function getLocContactPerson()
	{
		return $this->locContactPerson;	
	}
	
	######################################### RETRIEVE FUNCTION ###################################################
	
	function retrieveUserAllDataByUserName($user_name)
	{
		$query="SELECT * FROM users WHERE user_name = '$user_name'";
		$queryOb = new classQueryGlobal;
		$row = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($row[0]))
		{
			$this->userID=$row[0]['user_id'];
			
			$this->userName=$row[0]['user_name']; 
			$this->password=$row[0]['password'];
			$this->userRoleID=$row[0]['userrole_id'];
			
			$this->firstName=$row[0]['fname'];
			$this->lastName=$row[0]['lname'];
			$this->email=$row[0]['email'];
			$this->phone=$row[0]['phone'];
			$this->organizationID=$row[0]['org_id'];
			$this->locationID=$row[0]['location_id'];
			
		}
	}
	
	function retrieveUserAllDataByUserID($user_id)
	{
		$query="SELECT * FROM users WHERE user_id = '$user_id'";
		$queryOb = new classQueryGlobal;
		$row = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($row[0]))
		{
			$this->userID=$row[0]['user_id'];
			
			$this->userName=$row[0]['user_name']; 
			$this->password=$row[0]['password'];
			$this->userRoleID=$row[0]['userrole_id'];
			
			$this->firstName=$row[0]['fname'];
			$this->lastName=$row[0]['lname'];
			$this->email=$row[0]['email'];
			$this->phone=$row[0]['phone'];
			$this->organizationID=$row[0]['org_id'];
			$this->locationID=$row[0]['location_id'];
		}
	}
	
	function retrieveAllOrganizations()
	{
		$query="SELECT * FROM organizations ORDER BY org_id ASC";
		$queryOb = new classQueryGlobal;
		$org_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		if(isset($org_array[0]['org_name'])&&$org_array[0]['org_name']!='')
		{
			return $org_array;
		}
	}
	
	function retrieveLocationsOfOrganization()
	{
		$org_id = $this->getOrganizationID();
		
		$query="SELECT * FROM locations WHERE org_id = '$org_id' AND active = 1";
		$queryOb = new classQueryGlobal;
		$location_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		
		return $location_array;
	}
	
	function retrieveLocationsByLocationID()
	{
		$location_id = $this->getLocationID();
		
		$query="SELECT * FROM locations WHERE location_id = '$location_id'";
		$queryOb = new classQueryGlobal;
		$location_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		
		return $location_array;
	}
	
	function retrieveOrgNameByOrgID($org_id)
	{
		$query="SELECT org_name FROM organizations WHERE org_id = '$org_id'";
		$queryOb = new classQueryGlobal;
		$org_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		if(isset($org_array[0]['org_name'])&&$org_array[0]['org_name']!='')
		{
			return $org_array[0]['org_name'];
		}
	}
	
	function retrieveNoOfLocationsOfOrgID($org_id)
	{
		$query="SELECT no_of_locations FROM organizations WHERE org_id = '$org_id'";
		$queryOb = new classQueryGlobal;
		$org_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		if(isset($org_array[0]['no_of_locations'])&&$org_array[0]['no_of_locations']!='')
		{
			return $org_array[0]['no_of_locations'];
		}
	}
	
	function retrieveOrgDataByOrgID($org_id)
	{
		$query="SELECT * FROM organizations WHERE org_id = '$org_id' ";
		$queryOb = new classQueryGlobal;
		$org_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		if(isset($org_array[0]['org_name'])&&$org_array[0]['org_name']!='')
		{
			$this->organizationName = $org_array[0]['org_name'];
			$this->diagnostic = $org_array[0]['diagnostic'];
			$this->opd = $org_array[0]['opd'];
			$this->ipd = $org_array[0]['ipd'];
			$this->ot = $org_array[0]['ot'];
			$this->pharmacy = $org_array[0]['pharmacy'];
			$this->emergency = $org_array[0]['emergency'];
			$this->admin = $org_array[0]['admin'];
			$this->noOfLocations = $org_array[0]['no_of_locations'];
			
		}
	}
	
	function retrieveUsersByOrgID($org_id)
	{
		$query="SELECT * FROM users WHERE org_id = '$org_id' AND active = 1 ORDER BY fname ASC";
		$queryOb = new classQueryGlobal;
		$users_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		if(isset($users_array[0]['user_id'])&&$users_array[0]['user_id']!='')
		{
			return $users_array;
		}
	}
	
	function retrieveUserRoleByRoleID($userrole_id)
	{
		$query="SELECT userrole FROM user_role WHERE userrole_id = '$userrole_id'";
		$queryOb = new classQueryGlobal;
		$users_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		if(isset($users_array[0]['userrole'])&&$users_array[0]['userrole']!='')
		{
			return $users_array[0]['userrole'];
		}
	}
	
	function searchUser($by_id=NULL,$by_name=NULL,$by_phone=NULL)
	{
		if($by_id!="")
		{
			$query = "SELECT * FROM users WHERE  user_id = '$by_id'  AND active = 1 order by time_stamp DESC";
		}
		elseif($by_name!="")
		{
			$query = "SELECT * FROM users WHERE  active = 1 AND (fname LIKE '%".$by_name."%') OR (lname LIKE '%".$by_name."%') order by time_stamp DESC"; 
		}
		elseif($by_phone!="")
		{
			$query = "SELECT * FROM users WHERE  phone LIKE '%".$by_phone."%'  AND active = 1 order by time_stamp DESC"; 
		}
		
		$queryOb = new classQueryGlobal;
		$user_details = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($user_details[0]['user_id'])&&$user_details[0]['user_id']!='')
		{
			return $user_details;
		}
	}
	  
	
	
	######################################### INSERT FUNCTION ###################################################
	
	function insertOrganization($post_array)
	{
		$org_name = $this->getOrganizationName();
		
		$no_of_locations = $this->getNoOfLocations();
		
		if(isset($post_array['opd'])){$opd = 1;}else{$opd = 0;}
		
		if(isset($post_array['ipd'])){$ipd = 1;}else{$ipd = 0;}
		
		if(isset($post_array['diagnostic'])){$diagnostic = 1;}else{$diagnostic = 0;}
		
		if(isset($post_array['ot'])){$ot = 1;}else{$ot = 0;}
		
		if(isset($post_array['pharmacy'])){$pharmacy = 1;}else{$pharmacy = 0;}
		
		if(isset($post_array['emergency'])){$emergency = 1;}else{$emergency = 0;}
		
		if(isset($post_array['admin'])){$admin = 1;}else{$admin = 0;}
			
		$query = "INSERT INTO organizations (org_name,diagnostic,opd,ipd,pharmacy,ot,emergency,admin,no_of_locations) VALUES ('$org_name','$diagnostic','$opd','$ipd','$pharmacy','$ot','$emergency','$admin','$no_of_locations')";
		$queryOb = new classQueryGlobal;
		$this->organizationID = $queryOb->check_fetch_query($query,'insert',$server="",$debug_mode="off");
	}
	
	function insertLocation()
	{
		$org_id = $this->getOrganizationID();
		$location_name = $this->getLocationName();
		$city = $this->getLocCity();
		$address = $this->getLocAddress();
		$phone = $this->getLocPhone();
		$contact_person = $this->getLocContactPerson();
		$patient_reg_fee = $this->getPatientRegFee();
		
		$query = "INSERT INTO locations (org_id,location_name,city,address,phone,contact_person,active,patient_reg_fee) VALUES ('$org_id','$location_name','$city','$address','$phone','$contact_person','1','$patient_reg_fee')";
		$queryOb = new classQueryGlobal;
		$this->locationID = $queryOb->check_fetch_query($query,'insert',$server="",$debug_mode="off");
	}
	
	######################################### UPDATE FUNCTION ###################################################
	
	function updateOrganization()
	{
		$org_id = $this->getOrganizationID();
		$org_name = $this->getOrganizationName();
		
		$query = "UPDATE organizations SET org_name = '$org_name' WHERE org_id = '$org_id'";
		$queryOb = new classQueryGlobal;
		$queryOb->check_fetch_query($query,'update',$server="",$debug_mode="off");
	}
	
	function updateOrganizationInfo($post_array)
	{
		$org_name = $this->getOrganizationName();
		
		$org_id = $this->getOrganizationID();
		$no_of_locations = $this->getNoOfLocations();
		
		if(isset($post_array['opd'])){$opd = 1;}else{$opd = 0;}
		
		if(isset($post_array['ipd'])){$ipd = 1;}else{$ipd = 0;}
		
		if(isset($post_array['diagnostic'])){$diagnostic = 1;}else{$diagnostic = 0;}
		
		if(isset($post_array['ot'])){$ot = 1;}else{$ot = 0;}
		
		if(isset($post_array['pharmacy'])){$pharmacy = 1;}else{$pharmacy = 0;}
		
		if(isset($post_array['emergency'])){$emergency = 1;}else{$emergency = 0;}
		
		if(isset($post_array['admin'])){$admin = 1;}else{$admin = 0;}
			
		$query = "UPDATE organizations SET 
		org_name = '$org_name',
		diagnostic = '$diagnostic',
		opd = '$opd',
		ipd = '$ipd',
		pharmacy = '$pharmacy',
		ot = '$ot',
		emergency = '$emergency',
		admin = '$admin',
		no_of_locations = '$no_of_locations'
		WHERE org_id = '$org_id'";
		$queryOb = new classQueryGlobal;
		$queryOb->check_fetch_query($query,'update',$server="",$debug_mode="off");
	}
	
	function updateLocation()
	{
		$location_id = $this->getLocationID();
		$location_name = $this->getLocationName();
		$city = $this->getLocCity();
		$address = $this->getLocAddress();
		$phone = $this->getLocPhone();
		$contact_person = $this->getLocContactPerson();
		$patient_reg_fee = $this->getPatientRegFee();
		
		$query = "UPDATE locations SET location_name = '$location_name' , city = '$city' , address = '$address' , phone = '$phone', contact_person = '$contact_person' , patient_reg_fee = '$patient_reg_fee'  WHERE location_id = '$location_id'";
		$queryOb = new classQueryGlobal;
		$queryOb->check_fetch_query($query,'update',$server="",$debug_mode="off");
	}
	
	######################################### DELETE FUNCTION ###################################################
	
	function deleteUser()
	{
		$user_id = $this->getUserID();
		
		$query = "UPDATE users SET active = 0 WHERE user_id = '$user_id'";
		$queryOb = new classQueryGlobal;
		$queryOb->check_fetch_query($query,'update',$server="",$debug_mode="off");
	}
	
}
?>