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

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

CREATE OR REPLACE FUNCTION public."udf_dashBoard_fetch_Admissions_Location"(
	"admissionDate" 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[],
	"admissionNo" text DEFAULT NULL::text,
	locationid integer DEFAULT NULL::integer,
	"patientType" text DEFAULT NULL::text,
	"pageIndex" integer DEFAULT 0,
	"pageSize" integer DEFAULT 10)
    RETURNS TABLE("AdmissionNo" text, "PatientId" integer, "PatientName" text, "Age" smallint, "Gender" character, "Mobile" character varying, "ProviderName" character varying, "AdmissionDate" timestamp without time zone, "AdmissionTime" time without time zone, "DepartmentName" character varying, "TotalAdmissions" bigint) 
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE PARALLEL UNSAFE
    ROWS 1000

AS $BODY$
Declare 
TotalAdmissions  bigint;
BEGIN
select count(A."AdmissionNo") into TotalAdmissions from "Admission" 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 "admissionDate" is null then 1=1 else A."AdmissionDate"="admissionDate"::date end
AND case when "patientType" is null then 1=1 else A."PatientType" ="patientType" end;
return query 

select A."AdmissionNo",Pa."PatientId",Pa."FullName" as "PatientName",Pa."Age",Pa."Gender",Pa."Mobile",Pr."FullName" "ProviderName"
,A."AdmissionDate",A."AdmissionTime",D."DepartmentName" ,TotalAdmissions from "Admission" 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 "admissionDate" is null then 1=1 else A."AdmissionDate"::date="admissionDate"::date end
AND case when "patientType" is null then 1=1 else A."PatientType" ="patientType" end 
and case when "fromDate" is null then 1=1 else "fromDate" <=A."AdmissionDate" and A."AdmissionDate" <="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 "admissionNo" is null then 1=1 else  A."AdmissionNo"  ilike'%'|| "admissionNo"||'%' end
AND CASE WHEN (locationId IS NULL OR locationId=0) THEN 1=1 ELSE A."LocationId"=locationId END
order by A."AdmissionId" desc  limit "pageSize" offset ("pageSize"*"pageIndex") ; 
END
$BODY$;

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