<?php
class classWardMgmt
{
	private $organizationID=NULL;
	private $locationID=NULL;
	private $patientID=NULL;
	private $patientName=NULL;
	
	private $wardID=NULL;
	private $wardNo=NULL;
	private $wardType=NULL;
	
	private $wardBedID=NULL;
	private $wardBedNo=NULL;
	private $wardBedFee=NULL;
	private $wardBedType=NULL;
	private $wardBedStatus=NULL;
	private $wardBedAllotTime=NULL;
	private $wardBedAllotID=NULL;
	private $wardBedDischargeDueTime=NULL;
	private $wardBedDischargeTime=NULL;
	
	private $provisionalDiagnosis = NULL;
	private $referedTo = NULL;
	private $finalDiagnosis = NULL;
	private $dischargingDoctor = NULL;
	private $dischargingUnit = NULL;
	private $dischargingFollowUpTime  =NULL;
	
	private $staffID=NULL;
	
	private $totalAmount=NULL;
	private $amountPayable=NULL;
	private $discount=NULL;
	private $payment=NULL;
	private $balance=NULL;
	private $paymentStatus=NULL;
	
	################################################## SET FUNCTIONS #####################################
	
	function setOrganizationID($organizationID)
	{
		if($organizationID!="")
		{
			$this->organizationID = $organizationID;
		}
		else
		{
			throw new MyException(115100);
		}
		
	}
	function setLocationID($locationID)
	{
		if($locationID!='')
		{
			$this->locationID = $locationID;
		}
		else
		{
			throw new MyException(115101);
		}
	}
	function setPatientID($patientID)
	{
		if($patientID!='')
		{
			$this->patientID = $patientID;
		}
		else
		{
			throw new MyException(115102);
		}
	}
	
	function setPatientName($patientName)
	{
		if($patientName!='')
		{
			$this->patientName = $patientName;
		}
	}
	
	function setWardID($wardID)
	{
		if($wardID!="")
		{
			$this->wardID = $wardID;
		}
		else
		{
			throw new MyException(115103);
		}
		
	}
	function setWardNo($wardNo)
	{
		if($wardNo!='')
		{
			$this->wardNo = $wardNo;
		}
		else
		{
			throw new MyException(115104);
		}
	}
	function setWardType($wardType)
	{
		if($wardType!='')
		{
			$this->wardType = $wardType;
		}
		else
		{
			throw new MyException(115105);
		}
	}
	
	function setWardBedID($wardBedID)
	{
		if($wardBedID!="")
		{
			$this->wardBedID = $wardBedID;
		}
		else
		{
			throw new MyException(115106);
		}
		
	}
	function setWardBedNo($wardBedNo)
	{
		if($wardBedNo!='')
		{
			$this->wardBedNo = $wardBedNo;
		}
		else
		{
			throw new MyException(115107);
		}
	}
	function setWardBedFee($wardBedFee)
	{
		if($wardBedFee!='')
		{
			if(is_numeric($wardBedFee))
			{
				$this->wardBedFee = $wardBedFee;
			}
			else
			{
				throw new MyException(115108);
			}
		}
		else
		{
			throw new MyException(115109);
		}
	}
	function setWardBedType($wardBedType)
	{
		if($wardBedType!='')
		{
			$this->wardBedType = $wardBedType;
		}
	}
	function setWardBedStatus($wardBedStatus)
	{
		if($wardBedStatus!='')
		{
			$this->wardBedStatus = $wardBedStatus;
		}
		else
		{
			throw new MyException(115110);
		}
	}
	function setWardBedAllotID($wardBedAllotID)
	{
		if($wardBedAllotID!='')
		{
			$this->wardBedAllotID = $wardBedAllotID;
		}
		else
		{
			throw new MyException(115131);
		}
	}
	function setWardBedAllotTime($wardBedAllotTime)
	{
		if($wardBedAllotTime!='')
		{
			$this->wardBedAllotTime = $wardBedAllotTime;
		}
		else
		{
			throw new MyException(115111);
		}
	}
	function setWardBedDischargeDueTime($wardBedDischargeDueTime)
	{
		if($wardBedDischargeDueTime!='')
		{
			$this->wardBedDischargeDueTime = $wardBedDischargeDueTime;
		}
		else
		{
			throw new MyException(115134);
		}
	}
	
	function setWardBedDischargeTime($wardBedDischargeTime)
	{
		if($wardBedDischargeTime!='')
		{
			$this->wardBedDischargeTime = $wardBedDischargeTime;
		}
		else
		{
			throw new MyException(115112);
		}
	}
	
	function setProvisionalDiagnosis($provisionalDiagnosis)
	{
		if($provisionalDiagnosis!='')
		{
			$this->provisionalDiagnosis = $provisionalDiagnosis;
		}
	}
	function setReferedTo($referedTo)
	{
		if($referedTo!='')
		{
			$this->referedTo = $referedTo;
		}
	}
	function setFinalDiagnosis($finalDiagnosis)
	{
		if($finalDiagnosis!='')
		{
			$this->finalDiagnosis = $finalDiagnosis;
		}
	}
	function setDischargingDoctor($dischargingDoctor)
	{
		if($dischargingDoctor!='')
		{
			$this->dischargingDoctor = $dischargingDoctor;
		}
	}
	function setDischargingUnit($dischargingUnit)
	{
		if($dischargingUnit!='')
		{
			$this->dischargingUnit = $dischargingUnit;
		}
		else
		{
			throw new MyException(115129);
		}
	}
	function setDischargingFollowUpTime($dischargingFollowUpTime)
	{
		if($dischargingFollowUpTime!='')
		{
			$this->dischargingFollowUpTime = $dischargingFollowUpTime;
		}
	}
	
	function setStaffID($staffID)
	{
		if($staffID!="")
		{
			$this->staffID = $staffID;
		}
		else
		{
			//throw new MyException(115113);
		}
		
	}
	
	function setTotalAmount($totalAmount)
	{
		if($totalAmount!='')
		{
			$this->totalAmount = $totalAmount;
		}
		else
		{
			throw new MyException('Please enter total amount.');
		}
	}
	function setAmountPayable($amountPayable)
	{
		if($amountPayable!='')
		{
			$this->amountPayable = $amountPayable;
		}
		else
		{
			throw new MyException('Please enter amount payable.');
		}
	}
	function setDiscount($discount)
	{
		if($discount!='')
		{
			$this->discount = $discount;
		}
	}
	function setPayment($payment)
	{
		if($payment!='')
		{
			if(is_numeric($payment))
			{
				$this->payment = $payment;
			}
			else
			{
				throw new MyException('Invalid payment amount.Please enter valid payment amount.');
			}
		}
		else
		{
			throw new MyException('Please enter payment amount.');
		}
	}
	function setBalance($balance)
	{
		if($balance!='')
		{
			$this->balance = $balance;
		}
	}
	function setPaymentStatus($paymentStatus)
	{
		if($paymentStatus!='')
		{
			$this->paymentStatus = $paymentStatus;
		}
	}
	
	################################################## GET FUNCTIONS #####################################
	
	function getOrganizationID()
	{
		if($this->organizationID!="")
		{
			return $this->organizationID;
		}
		else
		{
			throw new MyException(115114);
		}
		
	}
	function getLocationID()
	{
		if($this->locationID!='')
		{
			return $this->locationID;
		}
		else
		{
			throw new MyException(115115);
		}
	}
	function getPatientID()
	{
		if($this->patientID!='')
		{
			return $this->patientID;
		}
		else
		{
			throw new MyException(115116);
		}
	}
	
	function getPatientName()
	{
		if($this->patientName!='')
		{
			return $this->patientName;
		}
	}
	
	function getWardID()
	{
		if($this->wardID!="")
		{
			return $this->wardID;
		}
		else
		{
			throw new MyException(115117);
		}
		
	}
	function getWardNo()
	{
		if($this->wardNo!='')
		{
			return $this->wardNo;
		}
		else
		{
			throw new MyException(115118);
		}
	}
	function getWardType()
	{
		if($this->wardType!='')
		{
			return $this->wardType;
		}
		else
		{
			throw new MyException(115119);
		}
	}
	
	
	function getWardBedID()
	{
		if($this->wardBedID!="")
		{
			return $this->wardBedID;
		}
		else
		{
			throw new MyException(115120);
		}
		
	}
	function getWardBedNo()
	{
		if($this->wardBedNo!='')
		{
			return $this->wardBedNo;
		}
		else
		{
			throw new MyException(115121);
		}
	}
	function getWardBedFee()
	{
		if($this->wardBedFee!='')
		{
			return $this->wardBedFee;
		}
		else
		{
			throw new MyException(115122);
		}
	}
	function getWardBedType()
	{
		if($this->wardBedType!='')
		{
			return $this->wardBedType;
		}
	}
	function getWardBedStatus()
	{
		if($this->wardBedStatus!='')
		{
			return $this->wardBedStatus ;
		}
		else
		{
			throw new MyException(115123);
		}
	}
	function getWardBedAllotID()
	{
		if($this->wardBedAllotID!='')
		{
			return $this->wardBedAllotID;
		}
		else
		{
			throw new MyException(115132);
		}
	}
	function getWardBedAllotTime()
	{
		if($this->wardBedAllotTime!='')
		{
			return $this->wardBedAllotTime;
		}
		else
		{
			throw new MyException(115124);
		}
	}
	function getWardBedDischargeDueTime()
	{
		if($this->wardBedDischargeDueTime!='')
		{
			return $this->wardBedDischargeDueTime;
		}
		else
		{
			throw new MyException(115135);
		}
	}
	function getWardBedDischargeTime()
	{
		if($this->wardBedDischargeTime!='')
		{
			return $this->wardBedDischargeTime;
		}
		else
		{
			throw new MyException(115125);
		}
	}
	
	function getProvisionalDiagnosis()
	{
		if($this->provisionalDiagnosis!='')
		{
			return $this->provisionalDiagnosis;
		}
	}
	function getReferedTo()
	{
		if($this->referedTo!='')
		{
			return $this->referedTo;
		}
	}
	function getFinalDiagnosis()
	{
		if($this->finalDiagnosis!='')
		{
			return $this->finalDiagnosis;
		}
	}
	function getDischargingDoctor()
	{
		if($this->dischargingDoctor!='')
		{
			return $this->dischargingDoctor;
		}
	}
	function getDischargingUnit()
	{
		if($this->dischargingUnit!='')
		{
			return $this->dischargingUnit;
		}
		else
		{
			throw new MyException(115130);
		}
	}
	function getDischargingFollowUpTime()
	{
		if($this->dischargingFollowUpTime!='')
		{
			return $this->dischargingFollowUpTime;
		}
	}
	
	function getStaffID()
	{
		if($this->staffID!="")
		{
			return $this->staffID;
		}
		else
		{
			//throw new MyException(115126);
		}
		
	}
	
	function getTotalAmount()
	{
		if($this->totalAmount!='')
		{
			return $this->totalAmount;
		}
		else
		{
			throw new MyException('Please enter total amount.');
		}
	}
	function getAmountPayable()
	{
		if($this->amountPayable!='')
		{
			return $this->amountPayable;
		}
		else
		{
			throw new MyException('Please enter amount payable.');
		}
	}
	function getDiscount()
	{
		if($this->discount!='')
		{
			return $this->discount;
		}
	}
	function getPayment()
	{
		if($this->payment!='')
		{
			return $this->payment;
		}
		else
		{
			throw new MyException('Please enter payment amount.');
		}
	}
	function getBalance()
	{
		if($this->balance!='')
		{
			return $this->balance;
		}
	}
	function getPaymentStatus()
	{
		if($this->paymentStatus!='')
		{
			return $this->paymentStatus;
		}
	}
	
	
	################################################## CHECK FUNCTIONS #####################################
	
	function checkWardNoAlreadyExist($ipd_ward_no,$location_id)
	{
		$query = "SELECT ipd_ward_id FROM ipd_wards WHERE ipd_ward_no = '$ipd_ward_no' AND location_id = '$location_id'";
		$queryOb = new classQueryGlobal;
		$ward_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		if(isset($ward_array[0]['ipd_ward_id'])&&$ward_array[0]['ipd_ward_id']!='')
		{
			throw new MyException(115127);
		}
		else
		{
			return 1;
		}
		
	}
	
	function checkWardBedNoAlreadyExist($ipd_bed_no,$ipd_ward_id)
	{
		$query = "SELECT ipd_bed_id FROM ipd_beds WHERE ipd_bed_no = '$ipd_bed_no' AND ipd_ward_id = '$ipd_ward_id'";
		$queryOb = new classQueryGlobal;
		$bed_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		if(isset($bed_array[0]['ipd_bed_id'])&&$bed_array[0]['ipd_bed_id']!='')
		{
			throw new MyException(115128);
		}
		else
		{
			return 1;
		}
		
	}
	
	function checkWardBedAllotmentExist($ipd_bed_id)
	{
		$query = "SELECT ipd_bed_allotment_id FROM ipd_bed_allotment WHERE ipd_bed_id = '$ipd_bed_id'";
		$queryOb = new classQueryGlobal;
		$bed_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		if(isset($bed_array[0]['ipd_bed_allotment_id'])&&$bed_array[0]['ipd_bed_allotment_id']!='')
		{
			return 0;
		}
		else
		{
			return 1;
		}
		
	}
	
	function checkPatientAlreadyAllotedExist($patient_id,$location_id)
	{
		$query = "SELECT ipd_bed_allotment_id FROM ipd_beds  
		JOIN ipd_wards ON ipd_wards.ipd_ward_id = ipd_beds.ipd_ward_id
		JOIN ipd_bed_allotment ON ipd_beds.ipd_bed_id = ipd_bed_allotment.ipd_bed_id
		WHERE patient_id = '$patient_id' AND ipd_bed_allotment.status = '' AND location_id = '$location_id'";
		$queryOb = new classQueryGlobal;
		$bed_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($bed_array[0]['ipd_bed_allotment_id'])&&$bed_array[0]['ipd_bed_allotment_id']!='')
		{
			throw new MyException(115133);
		}
		else
		{
			return 1;
		}
		
	}
	
	################################################## INSERT FUNCTIONS #####################################
	
	function insertWard()
	{
		$ipd_ward_no = $this->getWardNo();
		$ipd_ward_type = $this->getWardType();
		$staff_id = $this->getStaffID();
		$location_id = $this->getLocationID();
		
		$this->checkWardNoAlreadyExist($ipd_ward_no,$location_id);
		
		$query = "INSERT INTO ipd_wards (ipd_ward_type,ipd_ward_no,staff_id,location_id) VALUES ('$ipd_ward_type','$ipd_ward_no','$staff_id','$location_id')";
		$queryOb = new classQueryGlobal;
		$this->wardID = $queryOb->check_fetch_query($query,'insert',$server="",$debug_mode="off");
		
	}
	
	function insertWardBed()
	{
		$ipd_bed_no = $this->getWardBedNo();
		$ipd_bed_fee = $this->getWardBedFee();
		$status = $this->getWardBedStatus();
		$ipd_ward_id = $this->getWardID();
		$ipd_bed_type = mysql_real_escape_string($this->getWardBedType());
		
		$this->checkWardBedNoAlreadyExist($ipd_bed_no,$ipd_ward_id);
		
		$query = "INSERT INTO ipd_beds (ipd_bed_no,ipd_bed_fee,status,ipd_ward_id,ipd_bed_type) VALUES ('$ipd_bed_no','$ipd_bed_fee','$status','$ipd_ward_id','$ipd_bed_type')";
		$queryOb = new classQueryGlobal;
		$this->wardBedID = $queryOb->check_fetch_query($query,'insert',$server="",$debug_mode="off");
		
	}
	
	function allocateWardBed()
	{
		$location_id = $this->getLocationID();
		$status = $this->getWardBedStatus();
		$ipd_ward_id = $this->getWardID();
		$ipd_bed_id = $this->getWardBedID();
		$patient_id = $this->getPatientID();
		$allot_time = strtotime($this->getWardBedAllotTime());
		$discharge_due_time = strtotime($this->getWardBedDischargeDueTime());
		
		if($this->checkPatientAlreadyAllotedExist($patient_id,$location_id))
		{
			$query = "INSERT INTO  ipd_bed_allotment (ipd_ward_id,ipd_bed_id,patient_id,allot_time,discharge_due_time) VALUES ('$ipd_ward_id','$ipd_bed_id','$patient_id','$allot_time','$discharge_due_time')";
			$queryOb = new classQueryGlobal;
			$queryOb->check_fetch_query($query,'insert',$server="",$debug_mode="off");
			
			$this->updateWardBedStatus();
		}
		
	}
	
	################################################## UPDATE FUNCTIONS #####################################
	
	function updateWard()
	{
		$ipd_ward_id = $this->getWardID();
		$ipd_ward_no = $this->getWardNo();
		$ipd_ward_type = $this->getWardType();
		$staff_id = $this->getStaffID();
		$location_id = $this->getLocationID();
		
		//$this->checkWardNoAlreadyExist($ipd_ward_no,$location_id);
		
		$query = "UPDATE ipd_wards SET ipd_ward_type = '$ipd_ward_type' , ipd_ward_no = '$ipd_ward_no' , staff_id = '$staff_id' , location_id = '$location_id' WHERE ipd_ward_id = '$ipd_ward_id'";
		$queryOb = new classQueryGlobal;
		$queryOb->check_fetch_query($query,'update',$server="",$debug_mode="off");
		
	}
	
	function updateWardBed()
	{
		$ipd_bed_id = $this->getWardBedID();
		$ipd_bed_no = $this->getWardBedNo();
		$ipd_bed_fee = $this->getWardBedFee();
		//$status = $this->getWardBedStatus();
		$ipd_ward_id = $this->getWardID();
		$ipd_bed_type = mysql_real_escape_string($this->getWardBedType());
		
		//$this->checkWardBedNoAlreadyExist($ipd_bed_no,$ipd_ward_id);
		
		$query = "UPDATE ipd_beds SET ipd_bed_no = '$ipd_bed_no' , ipd_bed_fee = '$ipd_bed_fee' , ipd_ward_id = '$ipd_ward_id' , ipd_bed_type = '$ipd_bed_type' WHERE ipd_bed_id = '$ipd_bed_id'";
		$queryOb = new classQueryGlobal;
		$queryOb->check_fetch_query($query,'update',$server="",$debug_mode="off");
		
	}
	
	function updateWardBedStatus()
	{
		$ipd_bed_id = $this->getWardBedID();
		$status = $this->getWardBedStatus();
		
		$query = "UPDATE ipd_beds SET status = '$status' WHERE ipd_bed_id = '$ipd_bed_id'";
		$queryOb = new classQueryGlobal;
		$queryOb->check_fetch_query($query,'update',$server="",$debug_mode="off");
		
	}
	
	function updateAllotmentWardBed()
	{
		$status = $this->getWardBedStatus();
		$ipd_ward_id = $this->getWardID();
		$ipd_bed_id = $this->getWardBedID();
		$patient_id = $this->getPatientID();
		$allot_time = strtotime($this->getWardBedAllotTime());
		$discharge_due_time = strtotime($this->getWardBedDischargeDueTime());
		$ipd_bed_allotment_id = $this->getWardBedAllotID();
		
		$query = "UPDATE ipd_bed_allotment SET 
		ipd_ward_id = '$ipd_ward_id' ,
		ipd_bed_id = '$ipd_bed_id' ,
		patient_id = '$patient_id' ,
		allot_time = '$allot_time' ,
		discharge_due_time = '$discharge_due_time'
		WHERE ipd_bed_allotment_id = '$ipd_bed_allotment_id'";
		$queryOb = new classQueryGlobal;
		$queryOb->check_fetch_query($query,'update',$server="",$debug_mode="off");
		
		$this->updateWardBedStatus();
		
	}
	
	function dischargeFromBed()
	{
		
		$prov_diagnosis = $this->getProvisionalDiagnosis();
		$refered_to = $this->getReferedTo();
		$final_diagnosis = $this->getFinalDiagnosis();
		$discharging_doctor = $this->getDischargingDoctor();
		$discharging_unit = $this->getDischargingUnit();
		$discharging_followuptime = $this->getDischargingFollowUpTime();
		$discharge_time = strtotime($this->getWardBedDischargeTime());
		$ipd_bed_allotment_id = $this->getWardBedAllotID();
		$discharge_slip_created = '1';
		
		$query = "UPDATE ipd_bed_allotment SET 
		prov_diagnosis = '$prov_diagnosis' ,
		refered_to = '$refered_to' ,
		final_diagnosis = '$final_diagnosis' ,
		discharging_doctor = '$discharging_doctor' ,
		discharging_unit = '$discharging_unit' ,
		discharging_followuptime = '$discharging_followuptime' ,
		discharge_time = '$discharge_time' ,
		discharge_slip_created = '$discharge_slip_created'
		WHERE ipd_bed_allotment_id = '$ipd_bed_allotment_id'";
		$queryOb = new classQueryGlobal;
		$queryOb->check_fetch_query($query,'update',$server="",$debug_mode="off");
		
	}
	
	function TransferAllocation()
	{
		$ipd_ward_id = $this->getWardID();
		$ipd_bed_id = $this->getWardBedID();
		$ipd_bed_allotment_id = $this->getWardBedAllotID();
		
		$allot_detail = $this->retrieveBedAllotmentDetailsByAllotID();
		$old_bed_id = $allot_detail[0]['ipd_bed_id'];
		
		$query = "UPDATE ipd_bed_allotment SET 
		ipd_ward_id = '$ipd_ward_id' ,
		ipd_bed_id = '$ipd_bed_id'
		WHERE ipd_bed_allotment_id = '$ipd_bed_allotment_id'";
		$queryOb = new classQueryGlobal;
		$queryOb->check_fetch_query($query,'update',$server="",$debug_mode="off");
		
		
		
		$this->wardBedStatus = 'Occupied';
		$this->updateWardBedStatus();
		
		################# update old alloted bed status ###############
		$this->wardBedStatus = 'Empty';
		$this->wardBedID = $old_bed_id;
		$this->updateWardBedStatus();
		
	}
	
	function updatePaymentDetails()
	{
		$charges_array = $this->calculateBedAllotmentRoomCharges();
		
		$ipd_bed_allotment_id  = $this->getWardBedAllotID();
		$total_amount = $charges_array[0]['room_charges'];
		
		$discount = $charges_array[0]['discount'];
		
		if($discount!='' && $discount!='0')
		{		
			$amount_payable = $total_amount-($total_amount*$discount/100);
		}
		else
		{
			$amount_payable = $total_amount;
		}
		
		$query = "UPDATE ipd_bed_allotment SET total_amount = '$total_amount' , amount_payable = '$amount_payable' WHERE ipd_bed_allotment_id = '$ipd_bed_allotment_id'";
		$queryOb = new classQueryGlobal;
		$queryOb->check_fetch_query($query,'update',$server="",$debug_mode="off");
		
		
	}
	
	function updatePaymentsAfterDiscount()
	{
		$ipd_bed_allotment_id  = $this->getWardBedAllotID();
		$discount = $this->getDiscount();
		
		$total_amount = $this->retrieveTotalAmountByAllotID();
		
		if($discount!='' && $discount!='0')
		{		
			$amount_payable = $total_amount-($total_amount*$discount/100);
		}
		else
		{
			$amount_payable = $total_amount;
		}
		
		$query = "UPDATE ipd_bed_allotment SET discount='$discount',amount_payable='$amount_payable' where ipd_bed_allotment_id = '$ipd_bed_allotment_id'";
		$queryOb = new classQueryGlobal;
		$queryOb->check_fetch_query($query,'update',$server="",$debug_mode="off");
	}
	
	function updatePayment()
	{
		$ipd_bed_allotment_id  = $this->getWardBedAllotID();
		$pay_status = $this->getPaymentStatus();
		$amount_payable = $this->getAmountPayable();
		$payment = $this->getPayment();
		$balance = $this->getBalance();
		
		
		$query = "UPDATE ipd_bed_allotment SET 
		pay_status = '$pay_status' ,
		fee = '$payable' ,
		amount_payable = '$payable' ,
		payment = '$payment' ,
		balance = '$balance' 
		WHERE ipd_bed_allotment_id = '$ipd_bed_allotment_id'";
		$queryOb = new classQueryGlobal;
		$queryOb->check_fetch_query($query,'update',$server="",$debug_mode="off");
		
		
	}
	
	function updatePaymentAfterBalanceTransaction()
	{
		$ipd_bed_allotment_id  = $this->getWardBedAllotID();
		$payment_status = $this->getPaymentStatus();
		$payment = $this->getPayment();
		$balance = $this->getBalance();
		
		
		$query = "UPDATE ipd_bed_allotment SET 
		 pay_status='$payment_status',
		 payment = payment+'$payment',
		 balance = '$balance'
		 where ipd_bed_allotment = '$ipd_bed_allotment'";
		$queryOb = new classQueryGlobal;
		$queryOb->check_fetch_query($query,'update',$server="",$debug_mode="off");
	}
		
	
	################################################## DELETE FUNCTIONS #####################################
	
	function deleteWard()
	{
		$ipd_ward_id = $this->getWardID();
		$query = "DELETE FROM ipd_wards WHERE ipd_ward_id = '$ipd_ward_id'";
		$queryOb = new classQueryGlobal;
		$queryOb->check_fetch_query($query,'delete',$server="",$debug_mode="off");
	}
	
	function deleteBed()
	{
		$ipd_bed_id = $this->getWardBedID();
		$query = "DELETE FROM ipd_beds WHERE ipd_bed_id = '$ipd_bed_id'";
		$queryOb = new classQueryGlobal;
		$queryOb->check_fetch_query($query,'delete',$server="",$debug_mode="off");
	}
	
	function deleteBedAllotment()
	{
		$ipd_bed_allotment_id = $this->getWardBedAllotID();
		$query = "DELETE FROM ipd_bed_allotment WHERE ipd_bed_allotment_id = '$ipd_bed_allotment_id'";
		$queryOb = new classQueryGlobal;
		$queryOb->check_fetch_query($query,'delete',$server="",$debug_mode="off");
	}
	
	################################################## RETRIEVE FUNCTIONS #####################################
	
	
	
	function calculateTime($timeStamp)
	{
		$diff = $timeStamp;
		$days = floor($diff/86400);
		$hours = floor(($diff-$days*86400)/(60 * 60));
		$min = floor(($diff-($days*86400+$hours*3600))/60);
		$second = $diff - ($days*86400+$hours*3600+$min*60);
		
		if($days > 0) {$type_of_time = $days." Days ago";}
		elseif($hours > 0) {$type_of_time = $hours." Hours ago";}
		elseif($min > 0) {$type_of_time = $min." Minutes ago";}
		else {$type_of_time = "Just now";}
		
		return $type_of_time;
	}
	
	function calculateBedAllotmentRoomCharges()
	{
		$ipd_bed_allotment_id  = $this->getWardBedAllotID();
		
		$query = "SELECT allot_time , discharge_time ,ipd_bed_fee , discount  FROM ipd_bed_allotment JOIN ipd_beds ON  ipd_beds.ipd_bed_id = ipd_bed_allotment.ipd_bed_id WHERE ipd_bed_allotment_id = '$ipd_bed_allotment_id'";
		$queryOb = new classQueryGlobal;
		$bed_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($bed_array[0]['ipd_bed_fee'])&&$bed_array[0]['ipd_bed_fee']!='')
		{
			$start_date = $bed_array[0]['allot_time'];
			$end_date = $bed_array[0]['discharge_time'];

			$diff = $end_date - $start_date;
			
			$hours = floor(floor($diff/60)/60);
			
			$room_charges = $hours*$bed_array[0]['ipd_bed_fee'];
			
			$bed_array[0]['hours'] = $hours;
			$bed_array[0]['room_charges'] = $room_charges;
			
			return $bed_array;
		}
	}
	
	function retrieveAllStaff()
	{
		$location_id = $this->getLocationID();
		
		$query = "SELECT * FROM staff WHERE location_id = '$location_id'";
		$queryOb = new classQueryGlobal;
		$staff_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($staff_array[0]['staff_id'])&&$staff_array[0]['staff_id']!='')
		{
			return $staff_array;
		}
		
	}
	
	
	function retrieveAllWards()
	{
		$location_id = $this->getLocationID();
		
		$query = "SELECT * FROM ipd_wards WHERE location_id = '$location_id' ORDER BY ipd_ward_no ASC";
		$queryOb = new classQueryGlobal;
		$ward_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($ward_array[0]['ipd_ward_id'])&&$ward_array[0]['ipd_ward_id']!='')
		{
			return $ward_array;
		}
		
	}
	function retrieveWardByWardID()
	{
		$ipd_ward_id = $this->getWardID();
		
		$query = "SELECT * FROM ipd_wards WHERE ipd_ward_id = '$ipd_ward_id'";
		$queryOb = new classQueryGlobal;
		$ward_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($ward_array[0]['ipd_ward_id'])&&$ward_array[0]['ipd_ward_id']!='')
		{
			return $ward_array[0];
		}
		
	}
	
	
	function retrieveWardAndBeds($location_id,$ipd_bed_no=NULL,$patient_name=NULL,$ipd_bed_status=NULL)
	{
		$query = "SELECT 
		DISTINCT ipd_beds.ipd_ward_id , 
		GROUP_CONCAT(DISTINCT ipd_beds.ipd_bed_id ORDER BY ipd_bed_no) AS child_id_list ,
		ipd_wards.ipd_ward_id
		FROM ipd_beds 
		RIGHT JOIN ipd_wards ON ipd_beds.ipd_ward_id = ipd_wards.ipd_ward_id"; 
		$query .= " WHERE ipd_wards.location_id = '$location_id'";
		
		if(!is_null($ipd_bed_status))
		{
			$query .= " AND ipd_beds.status = '$ipd_bed_status'";
		}
		
		if(!is_null($ipd_bed_no))
		{
			$query .= " AND ipd_bed_no = '$ipd_bed_no'";
		}
		
		$query .= " group by ipd_wards.ipd_ward_id ORDER BY ipd_ward_no";
		
		$queryOb = new classQueryGlobal;
		$bed_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		/*echo '<pre>';
		print_r($bed_array);
		echo '</pre>';*/
		
		$ward_bed_array = array();
		if(isset($bed_array[0]['ipd_ward_id']))
		{
			for($wb=0;$wb<count($bed_array);$wb++)
			{
				$this->wardID = $bed_array[$wb]['ipd_ward_id'];
				$ward_bed_array[$wb] = $this->retrieveWardByWardID();
				
				$ward_bed_array[$wb]['no_of_beds'] = $this->countBedsOfWard($this->getWardID());
				
				if($bed_array[$wb]['child_id_list']!='')
				{
					
					$bed_ids = explode(',',$bed_array[$wb]['child_id_list']);	
					foreach($bed_ids as $bed_id)
					{	
						/*$this->wardBedID = $bed_id;
						$this->patientName = $patient_name;
						$this->retrieveBedAllotmentDetailsByPatientName();
						if(count($this->retrieveBedAllotmentDetailsByPatientName())>0)
						{*/
							$ward_bed_array[$wb]['bed_details'][] = $this->retrieveBedAndAllotmentDetailsByBedID($bed_id,$patient_name);	
						/*}
						else
						{
							unset($ward_bed_array[$wb]);
						}*/
						
					}
				}
				else
				{
					$ward_bed_array[$wb]['bed_details'] = NULL;
				}
			}
		}
		return $ward_bed_array;
		
	}
	
	
	function countBedsOfWard($ipd_ward_id)
	{
		$query = "SELECT count(*) FROM ipd_beds WHERE ipd_ward_id = '$ipd_ward_id'";
		$queryOb = new classQueryGlobal;
		$bed_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($bed_array[0]['count(*)'])&&$bed_array[0]['count(*)']!='')
		{
			return $bed_array[0]['count(*)'];
		}
		
	}
	function retrieveAllbedOfWard()
	{
		$ipd_ward_id  = $this->getWardID();
		
		$query = "SELECT * FROM ipd_beds JOIN ipd_wards ON ipd_beds.ipd_ward_id = ipd_wards.ipd_ward_id WHERE ipd_beds.ipd_ward_id = '$ipd_ward_id'";
		$queryOb = new classQueryGlobal;
		$bed_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($bed_array[0]['ipd_bed_id'])&&$bed_array[0]['ipd_bed_id']!='')
		{
			return $bed_array;
		}
		
	}
	function retrieveEmptybedOfWard()
	{
		$ipd_ward_id  = $this->getWardID();
		
		$query = "SELECT * FROM ipd_beds JOIN ipd_wards ON ipd_beds.ipd_ward_id = ipd_wards.ipd_ward_id WHERE ipd_beds.ipd_ward_id = '$ipd_ward_id' AND ipd_beds.status = 'Empty'";
		$queryOb = new classQueryGlobal;
		$bed_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($bed_array[0]['ipd_bed_id'])&&$bed_array[0]['ipd_bed_id']!='')
		{
			return $bed_array;
		}
		
	}
	function retrieveAllEmptybedOfWard()
	{
		$location_id  = $this->getLocationID();
		
		$query = "SELECT * FROM ipd_beds JOIN ipd_wards ON ipd_beds.ipd_ward_id = ipd_wards.ipd_ward_id WHERE location_id = '$location_id' AND status = 'Empty'";
		$queryOb = new classQueryGlobal;
		$bed_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($bed_array[0]['ipd_bed_id'])&&$bed_array[0]['ipd_bed_id']!='')
		{
			return $bed_array;
		}
		
	}
	function retrieveBedAndAllotmentDetailsByBedID($ipd_bed_id,$patient_name=NULL)
	{
		$query = "SELECT * FROM ipd_beds WHERE ipd_bed_id = '$ipd_bed_id'";
		$queryOb = new classQueryGlobal;
		$bed_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($bed_array[0]['ipd_bed_id'])&&$bed_array[0]['ipd_bed_id']!='')
		{
			$this->wardBedID = $bed_array[0]['ipd_bed_id'];
			
			if(!is_null($patient_name))
			{
				$this->patientName = $patient_name;
				$bed_allot_array = $this->retrieveBedAllotmentDetailsByPatientName();
			}
			else
			{
				$bed_allot_array = $this->retrieveBedAllotmentDetails();
			}
			
			//echo '<pre>';print_r($bed_allot_array);echo '</pre>';
			
			$patient_id = $bed_allot_array[0]['patient_id'];
			if($patient_id!=0)
			{
				$patientOb = new classPatient;
				$patientOb->setPatientAutoID($patient_id);
				$patient_info = $patientOb->retrievePatientByPatientID();
				
				$patient_name = ucwords($patient_info[0]['patient_fname'].' '.$patient_info[0]['patient_lname']);
			}
			else
			{
				$patient_name = '';
			}
			
			if(isset($bed_allot_array[0]['allot_time'])&&isset($bed_allot_array[0]['discharge_due_time'])&&isset($bed_allot_array[0]['ipd_bed_allotment_id']))
			{
				$allot_time = @date('d M Y h:i A',$bed_allot_array[0]['allot_time']);
				$discharge_due_time = @date('d M Y  h:i A',$bed_allot_array[0]['discharge_due_time']);
				$ipd_bed_allotment_id = $bed_allot_array[0]['ipd_bed_allotment_id'];
			}
			else
			{
				$allot_time = '';
				$discharge_due_time = '';
				$ipd_bed_allotment_id = 0;
			}
			
			$bed_array[0]['patient_name'] = $patient_name;
			$bed_array[0]['patient_id'] = $patient_id;
			$bed_array[0]['allot_time'] = $allot_time;
			$bed_array[0]['discharge_due_time'] = $discharge_due_time;
			$bed_array[0]['ipd_bed_allotment_id'] = $ipd_bed_allotment_id;
			$bed_array[0]['discharge_slip_created'] = $bed_allot_array[0]['discharge_slip_created'];
			
		}
		return $bed_array[0];
	}
	
	
	function retrieveAllbedsAndWards()
	{
		$location_id  = $this->getLocationID();
		
		$query = "SELECT * FROM ipd_beds JOIN ipd_wards ON ipd_beds.ipd_ward_id = ipd_wards.ipd_ward_id WHERE location_id = '$location_id'";
		$queryOb = new classQueryGlobal;
		$bed_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($bed_array[0]['ipd_bed_id'])&&$bed_array[0]['ipd_bed_id']!='')
		{
			return $bed_array;
		}
		
	}
	function countAllbedsAndWards()
	{
		$location_id  = $this->getLocationID();
		
		$query = "SELECT count(ipd_bed_id) FROM ipd_beds JOIN ipd_wards ON ipd_beds.ipd_ward_id = ipd_wards.ipd_ward_id WHERE location_id = '$location_id'";
		$queryOb = new classQueryGlobal;
		$bed_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($bed_array[0]['count(ipd_bed_id)'])&&$bed_array[0]['count(ipd_bed_id)']!='')
		{
			return $bed_array[0]['count(ipd_bed_id)'];
		}
		
	}
	
	
	function retrieveBedAllotmentDetails()
	{
		$ipd_bed_id  = $this->getWardBedID();
		
		$query = "SELECT * FROM ipd_bed_allotment WHERE ipd_bed_id = '$ipd_bed_id' AND status != 'discharged'";
		$queryOb = new classQueryGlobal;
		$bed_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($bed_array[0]['ipd_bed_allotment_id'])&&$bed_array[0]['ipd_bed_allotment_id']!='')
		{
			return $bed_array;
		}
		
	}
	function retrieveBedAllotmentDetailsByPatientName()
	{
		$ipd_bed_id  = $this->getWardBedID();
		$patient_name = $this->getPatientName();
		
		$query = "SELECT ipd_bed_allotment.* FROM ipd_bed_allotment JOIN patients ON ipd_bed_allotment.patient_id = patients.patient_auto_id  WHERE ipd_bed_id = '$ipd_bed_id' AND status != 'discharged'";
		
		$query .= " AND (patient_fname LIKE '%".$patient_name."%' OR patient_lname LIKE '%".$patient_name."%') "; 
		
		$queryOb = new classQueryGlobal;
		$bed_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		/*echo '<pre>';
		print_r($bed_array);
		echo '</pre>';*/
		
		if(isset($bed_array[0]['ipd_bed_allotment_id'])&&$bed_array[0]['ipd_bed_allotment_id']!='')
		{
			return $bed_array;
		}
		
	}
	function retrieveBedAllotmentDetailsByAllotID()
	{
		$ipd_bed_allotment_id  = $this->getWardBedAllotID();
		
		$query = "SELECT * FROM ipd_bed_allotment JOIN ipd_beds ON  ipd_beds.ipd_bed_id = ipd_bed_allotment.ipd_bed_id WHERE ipd_bed_allotment_id = '$ipd_bed_allotment_id'";
		$queryOb = new classQueryGlobal;
		$bed_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($bed_array[0]['ipd_bed_allotment_id'])&&$bed_array[0]['ipd_bed_allotment_id']!='')
		{
			return $bed_array;
		}
		
	}
	
	
	function countAllBedAllotmentDetails()
	{
		$location_id  = $this->getLocationID();
		
		$query = "SELECT count(*) FROM ipd_beds 
		JOIN ipd_wards ON ipd_beds.ipd_ward_id = ipd_wards.ipd_ward_id
		JOIN ipd_bed_allotment ON ipd_beds.ipd_bed_id = ipd_bed_allotment.ipd_bed_id
		WHERE location_id = '$location_id'";
		$queryOb = new classQueryGlobal;
		$bed_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($bed_array[0]['count(*)'])&&$bed_array[0]['count(*)']!='')
		{
			return $bed_array[0]['count(*)'];
		}
		
	}
	function retrieveAllBedAllotmentDetails()
	{
		$location_id  = $this->getLocationID();
		
		$query = "SELECT * FROM ipd_beds 
		JOIN ipd_wards ON ipd_beds.ipd_ward_id = ipd_wards.ipd_ward_id
		JOIN ipd_bed_allotment ON ipd_beds.ipd_bed_id = ipd_bed_allotment.ipd_bed_id
		WHERE location_id = '$location_id'";
		$queryOb = new classQueryGlobal;
		$bed_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($bed_array[0]['ipd_bed_allotment_id'])&&$bed_array[0]['ipd_bed_allotment_id']!='')
		{
			return $bed_array;
		}
		
	}
	
	
	function countAllocatedBedAllotmentDetails()
	{
		$location_id  = $this->getLocationID();
		
		$query = "SELECT count(*) FROM ipd_beds 
		JOIN ipd_wards ON ipd_beds.ipd_ward_id = ipd_wards.ipd_ward_id
		JOIN ipd_bed_allotment ON ipd_beds.ipd_bed_id = ipd_bed_allotment.ipd_bed_id
		WHERE location_id = '$location_id' AND ipd_bed_allotment.status!='discharged'";
		$queryOb = new classQueryGlobal;
		$bed_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($bed_array[0]['count(*)'])&&$bed_array[0]['count(*)']!='')
		{
			return $bed_array[0]['count(*)'];
		}
		
	}
	function retrieveAllocatedBedAllotmentDetails()
	{
		$location_id  = $this->getLocationID();
		
		$query = "SELECT * FROM ipd_beds 
		JOIN ipd_wards ON ipd_beds.ipd_ward_id = ipd_wards.ipd_ward_id
		JOIN ipd_bed_allotment ON ipd_beds.ipd_bed_id = ipd_bed_allotment.ipd_bed_id
		WHERE location_id = '$location_id' AND ipd_bed_allotment.status!='discharged'";
		$queryOb = new classQueryGlobal;
		$bed_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($bed_array[0]['ipd_bed_allotment_id'])&&$bed_array[0]['ipd_bed_allotment_id']!='')
		{
			return $bed_array;
		}
		
	}
	
	
	function retrieveWardAndBedNo($ipd_bed_allotment_id)
	{
		$query = "SELECT ipd_ward_no,ipd_bed_no FROM ipd_beds 
		JOIN ipd_wards ON ipd_beds.ipd_ward_id = ipd_wards.ipd_ward_id
		JOIN ipd_bed_allotment ON ipd_beds.ipd_bed_id = ipd_bed_allotment.ipd_bed_id
		WHERE ipd_bed_allotment_id = '$ipd_bed_allotment_id'";
		$queryOb = new classQueryGlobal;
		$bed_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($bed_array[0]['ipd_ward_no'])&&isset($bed_array[0]['ipd_bed_no']))
		{
			$wardbedno = $bed_array[0]['ipd_ward_no'].'-'.$bed_array[0]['ipd_bed_no'];
			return $wardbedno;
		}
		
	}
	
	
	################################################## RETRIEVE PAYMENT DETAILS #####################################
		
	function retrieveTotalAmountByAllotID()
	{
		$ipd_bed_allotment_id  = $this->getWardBedAllotID();
		
		$query = "SELECT total_amount FROM ipd_bed_allotment WHERE ipd_bed_allotment_id = '$ipd_bed_allotment_id'";
		$queryOb = new classQueryGlobal;
		$bed_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($bed_array[0]['total_amount'])&&$bed_array[0]['total_amount']!='')
		{
			return $bed_array[0]['total_amount'];
		}
		
	}
	
}

?>