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

-- 
DROP FUNCTION public."udf_uiReport_Receipts_Appointments_Location"(integer[], integer, integer, integer[], character varying, integer, character varying, character varying, integer, integer, integer, timestamp without time zone, timestamp without time zone);

CREATE OR REPLACE FUNCTION public."udf_uiReport_Receipts_Appointments_Location"(
	"accountId" integer[] DEFAULT NULL::integer[],
	"providerId" integer DEFAULT NULL::integer,
	"locationId" integer DEFAULT NULL::integer,
	"roleId" integer[] DEFAULT NULL::integer[],
	"appointmentNo" character varying DEFAULT NULL::text,
	"patientId" integer DEFAULT NULL::integer,
	"uMRNo" character varying DEFAULT NULL::text,
	"patientMobile" character varying DEFAULT NULL::text,
	"receiptId" integer DEFAULT NULL::integer,
	"createdBy" integer DEFAULT NULL::integer,
	"payTypeId" integer DEFAULT NULL::integer,
	"fromDate" timestamp without time zone DEFAULT (now())::timestamp without time zone,
	"toDate" timestamp without time zone DEFAULT (now())::timestamp without time zone)
    RETURNS TABLE("ReceiptCreatedBy" text, "RoleName" character varying,
				  "ReceiptDate" timestamp without time zone, "ReceiptId" text,
				  "AppointmentNo" character varying, "FollowUpForAppointmentId" integer,
				  "AppointmentDate" date, "AppointmentTime" text, "PatientName" text,
				  "UMRNo" character varying, "PatientMobile" character varying,
				  "ProviderName" character varying, "PaymentType" character varying,
				  "PayTypeName" character varying,"PaymentDetails" character varying, "PaidAmount" numeric, "RefundAmount" numeric,
				  "BalanceAmount" numeric, "AppointmentId" integer, "IsAppointmentReceipt" boolean) 
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE PARALLEL UNSAFE
    ROWS 1000

AS $BODY$
BEGIN

return query 

select coalesce(A."FullName",'GrandTotal') "ReceiptCreatedBy",A."RoleName",A."CreatedDate" "ReceiptDate",
coalesce(A."ReceiptId"::text,'Total')"ReceiptId"
,A."AppointmentNo",A."FollowUpForAppointmentId",A."AppointmentDate"::date as "AppointmentDate", to_char( 
	A."AppointmentTime" , 'HH12:MI PM' ) "AppointmentTime",
A."PatientName" "PatientName",A."UMRNo",A."Mobile", A."ProviderName" as "ProviderName",A."PaymentType"
,A."PayTypeName",A."PaymentDetails",sum(A."PaidAmount") "PaidAmount",sum(A."RefundAmount") "RefundAmount" 

,sum(A."PaidAmount")-sum(A."RefundAmount") "BalanceAmount",A."AppointmentId",A."IsAppointmentReceipt"
from (

select R."ReceiptId",A."FullName",Rl."RoleName",R."CreatedDate" "CreatedDate",
coalesce(case when "ReceiptTypeId"=1 then R."Cost" end,0) as "PaidAmount" ,
coalesce(case when "ReceiptTypeId"=2 then R."Cost" end,0) as "RefundAmount" ,Ad."FollowUpForAppointmentId",
	Ad."AppointmentDate",Ad."AppointmentNo", Ad."AppointmentTime",Ad."LocationId",
pa."FullName" "PatientName",Pa."UMRNo",pa."Mobile",pr."FullName" as "ProviderName",Ad."PaymentType",
pt."PayTypeName",R."PaymentDetails" , R."AppointmentId",R."IsAppointmentReceipt" 
	from "Receipt" R
join "Appointment" ad on Ad."AppointmentId"=R."AppointmentId" and ad."Status" <> 'C'  and ad."Active" <> false
join "Provider" pr on pr."ProviderId"= ad."ProviderId"
join "Patient" pa on Ad."PatientId"=pa."PatientId"
join "Account" A on A."AccountId"=R."CreatedBy"
join "Role" Rl on Rl."RoleId"=A."RoleId"
join "PayType" pt on pt."PayTypeId"=R."PayTypeId"
	where  R."Active" <> false  and
case when "accountId" is null then 1=1 else  R."CreatedBy"  = any("accountId") end  
and	case when "providerId" is null then 1=1 else  pr."ProviderId"  = "providerId" end  
and case when "locationId" is null then 1=1 else Ad."LocationId" = "locationId" end
and	case when "roleId" is null then 1=1 else  A."RoleId"  = any("roleId") end  and
		 	case when "appointmentNo" is null then 1=1 else  Ad."AppointmentNo" ilike '%' || "appointmentNo" ||'%' 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 "patientMobile" is null then 1=1 else  pa."Mobile"  ilike'%'|| "patientMobile"||'%' end
	and 
	 			  case when "receiptId" is null then 1=1 else  R."ReceiptId"  = "receiptId" end  
and
	case when "createdBy" is null then 1=1 else  R."CreatedBy"  = "createdBy" end  
and
		case when "payTypeId" is null then 1=1 else  pt."PayTypeId" = "payTypeId" end and
case when "fromDate" is null then 1=1 else "fromDate" <=R."CreatedDate" and R."CreatedDate" <="toDate" and R."Active" is not false end 
	
	
	
) A
GROUP BY GROUPING SETS((A."ReceiptId",A."RoleName",A."FullName",A."CreatedDate",A."PaymentType",A."PayTypeName",A."PaymentDetails"
						,A."FollowUpForAppointmentId",A."AppointmentDate",A."AppointmentNo"
						,A."AppointmentTime",A."PatientName",A."UMRNo",A."Mobile",A."ProviderName",A."AppointmentId",
						A."IsAppointmentReceipt"), (A."FullName"), ())     
order by A."FullName" 
 ;

END
$BODY$;

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