DROP FUNCTION IF EXISTS public."allModule_CancelReport"(date, date, integer, integer);

CREATE OR REPLACE FUNCTION public."allModule_CancelReport"(
	"fromDate" date DEFAULT NULL::date,
	"toDate" date DEFAULT NULL::date,
	accountid integer DEFAULT NULL::integer,
	locationid integer DEFAULT NULL::integer)
    RETURNS TABLE("AccountId" integer, "ReceiptCreatedBy" text, "RoleName" character varying, "ReceiptDate" timestamp without time zone, "ReceiptId" integer, "AreaType" character varying, "RefId" character varying, "PayTypeName" character varying, "PaymentDetails" character varying, "RefundAmount" numeric, "PatientName" text, "UMRNo" character varying, "Mobile" character varying) 
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE PARALLEL UNSAFE
    ROWS 1000

AS $BODY$
begin
return query

with accountdata as (
select a."AccountId",a."FullName" "EmployeeName",rol."RoleName"
from "Account" a
JOIN "LocationAccountMap" LAM on LAM."AccountId"=a."AccountId"  
join "Role" rol on rol."RoleId" = a."RoleId" and lower(rol."RoleName") <> lower('Patient')
where a."RoleId"<>4  --and a."AccountId"=6776 and LAM."LocationId"=2
 and	case when accountid is null then 1=1 else a."AccountId"=accountid end   
AND CASE WHEN (locationid IS NULL OR locationid=0) THEN 1=1 ELSE LAM."LocationId"=locationid END
)
, appointment as (
select R."ReceiptId",A."FullName" "ReceiptCreatedBy",Rl."RoleName",
	R."CreatedDate" "ReceiptDate",R."CreatedBy",RT."Name" as "AreaType",R."ReceiptTypeId"
,Ad."AppointmentNo",Ad."LocationId",
pa."FullName" "PatientName",Pa."UMRNo",pa."Mobile",
pt."PayTypeName",R."PaymentDetails" ,coalesce(case when "ReceiptTypeId"=2 then R."Cost" end,0) as "RefundAmount"
	from "Receipt" R
join "Appointment" Ad on Ad."AppointmentId"=R."RespectiveId" 
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"
join "ReceiptAreaType" RT on R."ReceiptAreaTypeId"=RT."ReceiptAreaTypeId"
	where  R."Active" <> false  and R."ReceiptAreaTypeId"=5 and
	--R."CreatedBy" =6776 and Ad."LocationId" = 2;
--and R."CreatedDate"::date ='2023-04-14'
 case when "accountid" is null then 1=1 else  R."CreatedBy"  = "accountid" end  
 and case when "locationid" is null then 1=1 else Ad."LocationId" = "locationid" end

 	and case when "fromDate" is null then 1=1 else "fromDate"<= R."CreatedDate"::date end 
 		and case when "toDate" is null then 1=1 else R."CreatedDate"::date<=  "toDate" end 
)
,patient as(			
						select 
                                R."Cost" as "RefundAmount", PT."PayTypeName",R."PaymentDetails",	
                                A."FullName" as "ReceiptCreatedBy",Rl."RoleName",R."CreatedBy",
                                P."FullName" as "PatientName",P."UMRNo",P."Mobile",P."PatientId",
                                R."ReceiptId",RT."Name" as "AreaType",R."ReceiptTypeId",R."CreatedDate" "ReceiptDate"
				                
	                            FROM "Receipt" R
	                            join "Patient" P on P."PatientId" = R."RespectiveId"  
								join  "Account" PA on PA."ReferenceId"=P."PatientId"
								JOIN "LocationAccountMap" LAM on LAM."AccountId"=PA."AccountId"  
	                            join "Account" A on A."AccountId" = R."CreatedBy"
						        join "Role" Rl on Rl."RoleId"=A."RoleId"
	                            join "PayType" PT on PT."PayTypeId" = R."PayTypeId"
							join "ReceiptAreaType" RT on R."ReceiptAreaTypeId"=RT."ReceiptAreaTypeId"
								where  R."ReceiptAreaTypeId"=6 
--	and LAM."LocationId"=2 and R."CreatedDate"::date='2023-04-14 and R."CreatedBy"=6776
									
 	and case when "fromDate" is null then 1=1 else "fromDate"<= R."CreatedDate"::date end and
 	case when "toDate" is null then 1=1 else R."CreatedDate"::date<=  "toDate" end and
	case when "locationid" is null then 1=1 else  LAM."LocationId" = "locationid"::int end and
	case when "accountid" is null then 1=1 else R."CreatedBy"="accountid" end 
	
)

,admission as (

select 
	R."ReceiptId",R."CreatedDate" "ReceiptDate",RT."Name" as "AreaType",
	Rl."RoleName",R."CreatedBy",A."FullName" as "ReceiptCreatedBy",R."ReceiptTypeId"
,pt."PayTypeName",R."PaymentDetails",R."Cost" as "RefundAmount" 
,Ad."AdmissionNo",	
pa."FullName" "PatientName",pa."UMRNo",pa."Mobile"
	from "Receipt" R
join "Admission" ad on Ad."AdmissionId"=R."RespectiveId" and ad."Active" is not 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"
	join "ReceiptAreaType" RT on R."ReceiptAreaTypeId"=RT."ReceiptAreaTypeId"
	where R."Active" is not false and R."ReceiptAreaTypeId"=13 and
	--and  Ad."LocationId"=2 and  R."CreatedBy"=6776
case when "accountid" is null then 1=1 else  R."CreatedBy"  = "accountid" end 
and case when "locationid" is null then 1=1 else Ad."LocationId" = "locationid" end 
and case when "fromDate" is null then 1=1 else "fromDate"<= R."CreatedDate"::date end 
 		and case when "toDate" is null then 1=1 else R."CreatedDate"::date<=  "toDate" end 

)
,lab as (
SELECT 
	 R."Cost" as "RefundAmount",PT."PayTypeName",R."PaymentDetails",
	 AR."FullName" as "ReceiptCreatedBy",Rl."RoleName",R."CreatedBy",
	nlbh."NewLabBookingHeaderId", nlbh."RequisitionNumber",	
	 COALESCE (nlbh."PatientName" ,p."FullName" ) as "PatientName",COALESCE (p."Mobile" ,nlbh."Mobile" ) as "Mobile", p."UMRNo",           
     R."CreatedDate" "ReceiptDate", R."ReceiptId",RT."Name" as "AreaType",R."ReceiptTypeId"
			 from  "NewLabBookingHeader" nlbh 	
	 		join "NewLabCancelBookingHeader" SH on SH."NewLabBookingHeaderId"=nlbh."NewLabBookingHeaderId"
	 	 join "Receipt" R on R."RespectiveId"= SH."NewLabCancelBookingHeaderId" 					                               
			join "PayType" pt ON PT."PayTypeId" = R."PayTypeId"                                                                      
			left join "Patient" p ON p."PatientId" =nlbh."PatientId"
			left join "Account" AR on AR."AccountId" = R."CreatedBy"
			join "Role" Rl on Rl."RoleId"=AR."RoleId"
			join "ReceiptAreaType" RT on R."ReceiptAreaTypeId"=RT."ReceiptAreaTypeId"
		where R."ReceiptAreaTypeId"=9
		--and nlbh."LocationId"=2 and R."CreatedDate"::date='2023-04-18'
 	and case when "fromDate" is null then 1=1 else "fromDate"<= R."CreatedDate"::date end and
	case when "toDate" is null then 1=1 else R."CreatedDate"::date<=  "toDate" end and
 	case when "locationid" is null then 1=1 else  nlbh."LocationId" = "locationid"::int end and
	case when "accountid" is null then 1=1 else nlbh."CreatedBy"="accountid" end 
											  								   
)

,pharma as (
select 
 R."Cost" as "RefundAmount", PT."PayTypeName",R."PaymentDetails",
	PH."PharmacySaleHeaderId" ,PH."BillNumber",
	R."CreatedBy",A."FullName" "ReceiptCreatedBy" ,AR."RoleName" 
	,RT."Name" as "AreaType",R."CreatedDate" "ReceiptDate",R."ReceiptId", R."ReceiptTypeId",
	PH."PatientName",PH."Mobile" ,Pa."UMRNo"	
from "PharmacySaleHeader" PH 
join "SaleReturnHeader" SH on SH."PharmacySaleHeaderId"=PH."PharmacySaleHeaderId"
join "Receipt" R on R."RespectiveId"= SH."SaleReturnHeaderId"
join "Account" A on A."AccountId"=R."CreatedBy"
join "Role" AR on AR."RoleId"=A."RoleId"
 join "PayType" PT ON PT."PayTypeId" = R."PayTypeId"  
join "ReceiptAreaType" RT on R."ReceiptAreaTypeId"=RT."ReceiptAreaTypeId"	 	
left join "Patient" Pa on Pa."PatientId"::text=PH."PatientId"::text
	where R."ReceiptAreaTypeId"=7
  
 		--and  PH."LocationId"=2 and R."CreatedDate"::date='2023-04-18' 	
 and case when "accountid" is null then 1=1 else R."CreatedBy"="accountid" end and	
 case when "fromDate" is null then 1=1 else "fromDate"<= R."CreatedDate"::date end and
 	case when "toDate" is null then 1=1 else R."CreatedDate"::date<=  "toDate" end and
  	case when "locationid" is null then 1=1 else PH."LocationId" = "locationid" end
)
,scan as(

	select 
		R."Cost" as "RefundAmount",PT."PayTypeName",R."PaymentDetails",
			 PH."RequisitionNumber", PH."BookScanAppointmentId",
			R."CreatedBy",R."ReceiptId",RT."Name" as "AreaType",R."CreatedDate" "ReceiptDate",
	A."FullName" "ReceiptCreatedBy",AR."RoleName" ,
			PA."FullName" as "PatientName",PA."Mobile" ,PA."UMRNo"
	
		from "Receipt" R
		join "BookScanAppointment" PH on PH."BookScanAppointmentId"=R."RespectiveId" 
		join "Account" A on A."AccountId"=R."CreatedBy"
		join "Role" AR on AR."RoleId"=A."RoleId"
		 join "PayType" PT ON PT."PayTypeId" = R."PayTypeId"  
		join "ReceiptAreaType" RT on R."ReceiptAreaTypeId"=RT."ReceiptAreaTypeId"		
		left join "Patient" PA on PA."PatientId"=PH."PatientId"
		where R."ReceiptAreaTypeId" =11   and
		--PH."LocationId"=2  and R."CreatedDate"::date='2023-04-14' 		
		case when "accountid" is null then 1=1 else R."CreatedBy"="accountid" end and	
		case when "fromDate" is null then 1=1 else "fromDate"<= R."CreatedDate"::date end and
		case when "toDate" is null then 1=1 else R."CreatedDate"::date<=  "toDate" end and
		case when "locationid" is null then 1=1 else PH."LocationId" = "locationid" end
	
	
)
SELECT O."AccountId",O."ReceiptCreatedBy",O."RoleName",O."ReceiptDate",O."ReceiptId",
O."AreaType",O."RefId",O."PayTypeName",O."PaymentDetails"
,O."RefundAmount",
O."PatientName",O."UMRNo",O."Mobile"
from 
(
	Select  A."AccountId",AP."ReceiptCreatedBy",AP."RoleName",AP."ReceiptDate",AP."ReceiptId",
AP."AreaType",AP."AppointmentNo" as "RefId",AP."PayTypeName",AP."PaymentDetails"
,AP."RefundAmount",
AP."PatientName",AP."UMRNo",AP."Mobile"
from accountdata A
join appointment AP on AP."CreatedBy"=A."AccountId"
UNION
	Select  A."AccountId",AP."ReceiptCreatedBy",AP."RoleName",AP."ReceiptDate",AP."ReceiptId",
AP."AreaType",AP."AdmissionNo" as "RefId",AP."PayTypeName",AP."PaymentDetails"
,AP."RefundAmount",
AP."PatientName",AP."UMRNo",AP."Mobile"
from accountdata A
join admission AP on AP."CreatedBy"=A."AccountId"
UNION
Select  A."AccountId",AP."ReceiptCreatedBy",AP."RoleName",AP."ReceiptDate",AP."ReceiptId",
AP."AreaType",AP."PatientId"::character varying as "RefId",AP."PayTypeName",AP."PaymentDetails"
,AP."RefundAmount",
AP."PatientName",AP."UMRNo",AP."Mobile"
from accountdata A
join patient AP on AP."CreatedBy"=A."AccountId"
UNION
Select  A."AccountId",AP."ReceiptCreatedBy",AP."RoleName",AP."ReceiptDate",AP."ReceiptId",
AP."AreaType",AP."RequisitionNumber"::character varying as "RefId",AP."PayTypeName",AP."PaymentDetails"
,AP."RefundAmount",
AP."PatientName",AP."UMRNo",AP."Mobile"
from accountdata A
join lab AP on AP."CreatedBy"=A."AccountId"
UNION
Select  A."AccountId",AP."ReceiptCreatedBy",AP."RoleName",AP."ReceiptDate",AP."ReceiptId",
AP."AreaType",AP."BillNumber"::character varying as "RefId",AP."PayTypeName",AP."PaymentDetails"
,AP."RefundAmount",
AP."PatientName",AP."UMRNo",AP."Mobile"
from accountdata A
join pharma AP on AP."CreatedBy"=A."AccountId"
	UNION
Select  A."AccountId",AP."ReceiptCreatedBy",AP."RoleName",AP."ReceiptDate",AP."ReceiptId",
AP."AreaType",AP."RequisitionNumber"::character varying as "RefId",AP."PayTypeName",AP."PaymentDetails"
,AP."RefundAmount",
AP."PatientName",AP."UMRNo",AP."Mobile"
from accountdata A
join scan AP on AP."CreatedBy"=A."AccountId"	
	
)O
order by O."AreaType" asc,O."AccountId";

end
$BODY$;