-- FUNCTION: public.udf_uiReport_fetch_Admissions_Locations(text, integer, integer[], integer[], date, integer, character varying, text, text, character varying, integer, date, integer, integer)

-- DROP FUNCTION public."udf_uiReport_fetch_Admissions_Locations"(text, integer, integer[], integer[], date, integer, character varying, text, text, character varying, integer, date, integer, integer);

CREATE OR REPLACE FUNCTION public."udf_uiReport_fetch_Admissions_Locations"(
	"admissionNo" text DEFAULT NULL::text,
	locationid integer DEFAULT NULL::integer,
	"departmentId" integer[] DEFAULT NULL::integer[],
	"providerId" integer[] DEFAULT NULL::integer[],
	"fromDate" date DEFAULT NULL::date,
	"patientId" integer DEFAULT NULL::integer,
	"uMRNo" character varying DEFAULT NULL::text,
	"referredBy" text DEFAULT NULL::text,
	"referredByName" text DEFAULT NULL::text,
	"patientMobile" character varying DEFAULT NULL::text,
	"dischargeStatusId" integer DEFAULT NULL::integer,
	"toDate" date DEFAULT NULL::date,
	"pageIndex" integer DEFAULT 0,
	"pageSize" integer DEFAULT 10)
    RETURNS TABLE("DepartmentName" text, "ProviderName" text, "AdmissionId" text, "AdmissionNo" text, "AdmissionDate" timestamp without time zone, "AdmissionTime" time without time zone, "PatientName" text, "ReferredBy" character varying, "ReferredByName" character varying, "UMRNo" character varying, "PatientMobile" character varying, "PatientAge" smallint, "PatientGender" character, "ProviderAge" smallint, "ProviderGender" character, "WardName" character varying, "RoomName" character varying, "BedNumber" character varying, "AttendantName" character varying, "AttendantRelationWithPatient" character varying, "AttendantContactNo" character varying, "IsDischarged" boolean, "DischargeDate" date, "DischargeTime" time without time zone, "DischargeStatus" character varying, "IsMaternity" boolean, "EncounterId" integer, "SurgeryName" character varying, "ProviderThumbnailUrl" text, "PatientThumbnailUrl" text, "TotalAdmission" bigint) 
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE PARALLEL UNSAFE
    ROWS 1000

AS $BODY$
BEGIN

return query 

select  coalesce(A."DepartmentName"::text,'GrandTotal')"DepartmentName" ,A."ProviderName" "ProviderName",
coalesce(A."AdmissionId"::text,'Total'), A."AdmissionNo", A."AdmissionDate" , A."AdmissionTime", 
A."PatientName" ,A."ReferredBy",A."ReferredByName",A."UMRNo",A."Mobile" as "PatientMobile" ,  A."PatientAge" , A."PatientGender" , 
A."ProviderAge" , A."ProviderGender"  , A."WardName"  , A."RoomName"  , A."BedNumber" 
, A."AttendantName" , A."AttendantRelationWithPatient" , A."AttendantContactNo" , A."IsDischarged", A."DischargeDate" , A."DischargeTime"
, A."DischargeStatus", A."IsMaternity" , A."EncounterId" , A."SurgeryName" , A."ProviderThumbnailUrl" , A."PatientThumbnailUrl" 
,Count(A."AdmissionNo") "TotalAddmissions"

from (
select A."AdmissionId",A."AdmissionNo",A."AdmissionDate",A."AdmissionTime",
	Pa."FullName" "PatientName",Pa."ReferredBy",Pa."ReferredByName",Pa."UMRNo",pa."Mobile",Pa."Age" "PatientAge",Pa."Gender" as "PatientGender",Pr."Age" "ProviderAge",Pr."Gender" as "ProviderGender",
	Pr."FullName"::text "ProviderName",D."DepartmentName"::text "DepartmentName" , w."WardName", R."RoomName", B."BedNumber"
	,A."AttendantName" "AttendantName",A."AttendantRelationWithPatient" "AttendantRelationWithPatient",A."AttendantContactNo" "AttendantContactNo"
	,case when ds."DischargeId" is not null then true else false end as "IsDischarged",ds."DischargeDate",ds."DischargeTime",
	dss."DischargeStatus"
	
	,A."IsMaternity",A."EncounterId",st."SurgeryName",
	(CASE WHEN pr."ThumbnailUrl" IS NOT NULL THEN CONCAT('{this.amazonS3Configuration.BucketURL}', pr."Guid", '/', pr."ThumbnailUrl") ELSE NULL END) AS "ProviderThumbnailUrl",
(CASE WHEN pa."ThumbnailUrl" IS NOT NULL THEN CONCAT('{this.amazonS3Configuration.BucketURL}', pa."Guid", '/', pa."ThumbnailUrl") ELSE NULL END) AS "PatientThumbnailUrl"
	from "Admission" A
	join "Patient" Pa on A."PatientId"=Pa."PatientId"
	join "Provider" Pr on A."ProviderId"=Pr."ProviderId"
	join "Department" D on D."DepartmentId"=A."DepartmentId"
	left join "Bed" B on B."BedId"=A."BedId"
	left join "Room" R on B."RoomId"=R."RoomId"
	left join "Ward" W on w."WardId"=R."WardId"
	---left join "PatientFamily" pf on pf."PatientFamilyId"=A."PatientFamilyId"
	left join "SurgeryType" st on st."SurgeryTypeId"=A."SurgeryTypeId"
	left join "Discharge" ds on ds."AdmissionId"=A."AdmissionId"
	left join "DischargeStatus" dss on dss."DischargeStatusId"=ds."DischargeStatusId"

where 
	   CASE WHEN (locationId IS NULL OR locationId=0) THEN 1=1 ELSE A."LocationId"=locationId END AND
case when "departmentId" is null then 1=1 else  A."DepartmentId"  = any("departmentId") end and 
case when "providerId" is null then 1=1 else  A."ProviderId"  = any("providerId") end and 
case when "admissionNo" is null then 1=1 when "admissionNo" ='' then 1=1 else A."AdmissionNo" ilike'%'||"admissionNo" ||'%' end 
and case when "patientId" is null then 1=1 else Pa."PatientId"="patientId" end and
	case when "uMRNo" is null then 1=1 else  Pa."UMRNo" ilike'%'||"uMRNo"||'%' end and 
	case when "referredBy" is null then 1=1 when "referredBy" ='' then 1=1 else Pa."ReferredBy" ilike'%'||"referredBy" ||'%'  end and
case when "referredByName" is null then 1=1 when "referredByName" ='' then 1=1 else Pa."ReferredByName" ilike'%'||"referredByName" ||'%' end and 
 case when "patientMobile" is null then 1=1 else  Pa."Mobile" ilike'%'||"patientMobile"||'%' end and
 case when "dischargeStatusId" is null then 1=1 else dss."DischargeStatusId"="dischargeStatusId" end and

case when "fromDate" is null then 1=1 else "fromDate" <=A."AdmissionDate"::date and A."AdmissionDate"::date <="toDate" end 			 

) A
GROUP BY GROUPING SETS((A."AdmissionId" , A."AdmissionNo" , A."AdmissionDate" , A."AdmissionTime",
						A."PatientName" ,A."ReferredBy",A."ReferredByName", A."PatientAge" , A."PatientGender" , 
A."ProviderAge" , A."ProviderGender" , A."ProviderName" , A."DepartmentName" , A."WardName"  , A."RoomName"  , A."BedNumber" 
, A."AttendantName" , A."AttendantRelationWithPatient" , A."AttendantContactNo" , A."IsDischarged", A."DischargeDate" , A."DischargeTime"
, A."DischargeStatus", A."IsMaternity" , A."EncounterId" , A."SurgeryName" , A."ProviderThumbnailUrl" , A."PatientThumbnailUrl",A."UMRNo",A."Mobile" ), (A."DepartmentName",A."ProviderName"), ())     
-- order by A."DepartmentName",A."ProviderName"
ORDER BY A."AdmissionNo"

 ;

END
$BODY$;

ALTER FUNCTION public."udf_uiReport_fetch_Admissions_Locations"(text, integer, integer[], integer[], date, integer, character varying, text, text, character varying, integer, date, integer, integer)
    OWNER TO postgres;
