-- FUNCTION: public.udf_dashBoard_fetch_Appointments_Location(date, timestamp without time zone, timestamp without time zone, integer[], integer[], character varying, integer, text, integer, integer)

-- DROP FUNCTION IF EXISTS public."udf_dashBoard_fetch_Appointments_Location"(date, timestamp without time zone, timestamp without time zone, integer[], integer[], character varying, integer, text, integer, integer);

CREATE OR REPLACE FUNCTION public."udf_dashBoard_fetch_Appointments_Location"(
	"appointmentDate" date DEFAULT NULL::date,
	"fromDate" timestamp without time zone DEFAULT (now())::timestamp without time zone,
	"toDate" timestamp without time zone DEFAULT (now())::timestamp without time zone,
	"providerId" integer[] DEFAULT NULL::integer[],
	"patientId" integer[] DEFAULT NULL::integer[],
	"appointmentNo" character varying DEFAULT NULL::text,
	locationid integer DEFAULT NULL::integer,
	"visitType" text DEFAULT NULL::text,
	"pageIndex" integer DEFAULT 0,
	"pageSize" integer DEFAULT 10)
    RETURNS TABLE("AppointmentNo" character varying, "PatientId" integer, "PatientName" text, "Age" smallint, "Gender" character, "Mobile" character varying, "ProviderName" character varying, "AppointmentDate" date, "AppointmentTime" time without time zone, "DepartmentName" character varying, "VisitType" character, "TotalAppointments" bigint) 
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE PARALLEL UNSAFE
    ROWS 1000

AS $BODY$
Declare 
TotalAppointments  bigint;
BEGIN
select count(A."AppointmentNo") into TotalAppointments from "Appointment" A 
join "Patient" Pa on Pa."PatientId"=A."PatientId"
join "Provider" pr on Pr."ProviderId"=A."ProviderId"
join "Department" D on D."DepartmentId"=A."DepartmentId"
where 
A."Active"=true and case when "appointmentDate" is null then 1=1 else A."AppointmentDate"="appointmentDate"::date end
and case when "visitType" is null then 1=1 else trim(upper(A."VisitType"))=trim(upper("visitType")) end 
;
return query 

select A."AppointmentNo",Pa."PatientId",Pa."FullName" as "PatientName",Pa."Age",Pa."Gender",Pa."Mobile",Pr."FullName" "ProviderName"
,A."AppointmentDate",A."AppointmentTime",D."DepartmentName" ,A."VisitType",TotalAppointments from "Appointment" A
join "Patient" Pa on Pa."PatientId"=A."PatientId"
join "Provider" pr on Pr."ProviderId"=A."ProviderId"
join "Department" D on D."DepartmentId"=A."DepartmentId"
where A."Active"=true 

and case when "appointmentDate" is null then 1=1 else A."AppointmentDate"="appointmentDate"::date end 
and case when "visitType" is null then 1=1 else trim(upper(A."VisitType"))=trim(upper("visitType")) end 
and case when "fromDate" is null then 1=1 else "fromDate" <=A."AppointmentDate" and A."AppointmentDate" <="toDate" end
and	case when "providerId" is null then 1=1 else  pr."ProviderId"  = any("providerId") end  
and case when "patientId" is null then 1=1 else  pa."PatientId"  = any("patientId") end  
and case when "appointmentNo" is null then 1=1 else  A."AppointmentNo" ilike '%' || "appointmentNo" ||'%' end  
AND CASE WHEN (locationId IS NULL OR locationId=0) THEN 1=1 ELSE A."LocationId"=locationId END

order by A."AppointmentDate" desc,A."AppointmentTime" desc  limit "pageSize" offset ("pageSize"*"pageIndex") ; 
END
$BODY$;

ALTER FUNCTION public."udf_dashBoard_fetch_Appointments_Location"(date, timestamp without time zone, timestamp without time zone, integer[], integer[], character varying, integer, text, integer, integer)
    OWNER TO postgres;
