-- FUNCTION: public.udf_Doctor_Revenue_location(integer, integer, timestamp without time zone, timestamp without time zone)

-- DROP FUNCTION IF EXISTS public."udf_Doctor_Revenue_location"(integer, integer, timestamp without time zone, timestamp without time zone);

CREATE OR REPLACE FUNCTION public."udf_Doctor_Revenue_location"(
	providerid integer DEFAULT NULL::integer,
	locationid 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("AccountId" integer, "DoctorName" text, "AppointmentCashTotal" numeric, "AppointmentCardTotal" numeric, "AppointmentAmount" numeric, "AdmissionCashTotal" numeric, "AdmissionCardTotal" numeric, "AdmissionAmount" numeric, "LabCash" numeric, "LabCard" numeric, "LabAmount" numeric, "PharmacySaleCash" numeric, "PharmacySaleCard" numeric, "PharmacyAmount" numeric, "TotalCash" numeric, "TotalCard" numeric, "Total" numeric) 
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE PARALLEL UNSAFE
    ROWS 1000

AS $BODY$
 
begin
 return query 
 with accountdata as (
select a."ProviderId",a."FullName" "DoctorName",ac."RoleId"
from "Provider" a
JOIN "Account" ac on ac."ReferenceId"=a."ProviderId" and ac."RoleId"=3
JOIN "LocationAccountMap" LAM on LAM."AccountId"=ac."AccountId" 
where case when providerId is null then 1=1 else a."ProviderId"=providerId end 
	AND CASE WHEN (locationId IS NULL OR locationId=0) THEN 1=1 ELSE LAM."LocationId"=locationId END
)

,actappointmentamount as (
	
	select a."ProviderId",sum(A."CashTotal") "AppointmentCashTotal",sum(A."CardTotal") "AppointmentCardTotal",sum(A."CashTotal")+ sum(A."CardTotal") appointmentamount from (
   select  ap."ProviderId"  "ProviderId",   
   case when A."PayTypeId"=1 then coalesce(A."Cost",0) else 0 end  "CashTotal" ,
   case when A."PayTypeId"=2 then coalesce(A."Cost",0) else 0 end "CardTotal"
		
		from "Receipt" A
    join "Appointment" ap on  ap."AppointmentId" =A."AppointmentId"  
	join "Provider" pr on pr."ProviderId"= ap."ProviderId"
    and ap."Active" =true	
   where 
		case when providerId is null then 1=1 else ap."ProviderId"=providerId end    
		AND CASE WHEN (locationId IS NULL OR locationId=0) THEN 1=1 ELSE ap."LocationId"=locationId END 
		AND  A."ReceiptTypeId"=1 	
	and A."Active"<>false and A."AppointmentId" is not null  
	and coalesce(A."IsRefunded",false) <>true and ap."Status" <> 'C' and	
	case when "fromDate" is null then 1=1 
	else (A."CreatedDate" >= "fromDate" and A."CreatedDate" <= "toDate")  end	)a
	
	
	group by  a."ProviderId"	
	
	
)

,refundappointmentamount as (
	
	select a."ProviderId" ,sum(A."CashTotal") "AppointmentRefCashTotal",sum(A."CardTotal") "AppointmentRefCardTotal",sum(A."CashTotal")+ sum(A."CardTotal") refappointmentamount  from (
    select  ap."ProviderId"  "ProviderId",
		   case when A."PayTypeId"=1 then coalesce(A."Cost",0) else 0 end  "CashTotal" ,
          case when A."PayTypeId"=2 then coalesce(A."Cost",0) else 0 end "CardTotal"
		
		 from "Receipt" A
    join "Appointment" ap on ap."AppointmentId" =A."AppointmentId"  
	join "Provider" pr on pr."ProviderId"= ap."ProviderId"
   and ap."Status" <> 'C'   and ap."Active" =true
	
   where case when providerId is null then 1=1 else ap."ProviderId"=providerId end    
	AND CASE WHEN (locationId IS NULL OR locationId=0) THEN 1=1 ELSE ap."LocationId"=locationId END 
	and A."Active"<>false and A."AppointmentId" is not null  
	and coalesce(A."IsRefunded",false) = true and
	
	case when "fromDate" is null then 1=1 
	else (A."CreatedDate" >= "fromDate" and A."CreatedDate" <= "toDate")  end	) a
	group by  a."ProviderId"	
	
)

,appointmentamount as (
select  A."ProviderId",
	coalesce(A."AppointmentCashTotal",0) - coalesce(B."AppointmentRefCashTotal",0) as "AppointmentCashTotal" ,
	coalesce(A."AppointmentCardTotal",0) - coalesce(B."AppointmentRefCardTotal",0) as "AppointmentCardTotal" ,	
	coalesce(A.appointmentamount,0) - coalesce(B.refappointmentamount,0) as "AppointmentAmount" from actappointmentamount A
	left join refundappointmentamount B on A."ProviderId"=B."ProviderId"
	
	
)  
,actadmissionamount as (
	
	select a."ProviderId" , sum(A."CashTotal") "AdmissionCashTotal",sum(A."CardTotal") "AdmissionCardTotal",sum(A."CashTotal")+ sum(A."CardTotal") "AdmissionAmount" from (
select ad."ProviderId" "ProviderId" ,
   case when adr."PayTypeId"=1 then coalesce(adr."Cost",0) else 0 end  "CashTotal" ,
   case when adr."PayTypeId"=2 then coalesce(adr."Cost",0) else 0 end "CardTotal"									  
												  
from  "Receipt" adr 	 
	 join "Admission" ad on  adr."AdmissionId" = ad."AdmissionId" and ad."Active" <> false
	join "Provider" pr on pr."ProviderId"= ad."ProviderId"
	where case when providerId is null then 1=1 else ad."ProviderId"=providerId end  
		AND   CASE WHEN (locationId IS NULL OR locationId=0) THEN 1=1 ELSE ad."LocationId"=locationId END 
	and adr."ReceiptTypeId"=1  and adr."Active"<>false  and adr."AdmissionId" is not null
	and case when "fromDate" is null then 1=1 
	else (adr."CreatedDate" >= "fromDate" and adr."CreatedDate" <= "toDate")  end	)a
	group by  a."ProviderId"	
)

,refadmissionamount as (
	select a."ProviderId" , sum(A."CashTotal") "AdmissionRefCashTotal",sum(A."CardTotal") "AdmissionRefCardTotal",sum(A."CashTotal")+ sum(A."CardTotal") "AdmissionRefAmount" from (
select ad."ProviderId" "ProviderId"  ,  
		case when adr."PayTypeId"=1 then coalesce(adr."Cost",0) else 0 end  "CashTotal" ,
        case when adr."PayTypeId"=2 then coalesce(adr."Cost",0) else 0 end "CardTotal" 
from  "Receipt" adr 	 
	 join "Admission" ad on  adr."AdmissionId" = ad."AdmissionId" and ad."Active" <> false
	join "Provider" pr on pr."ProviderId"= ad."ProviderId"
	where case when providerId is null then 1=1 else ad."ProviderId"=providerId end  
		AND   CASE WHEN (locationId IS NULL OR locationId=0) THEN 1=1 ELSE ad."LocationId"=locationId END
	 and adr."IsRefunded" = true   and adr."Active"<>false  and adr."AdmissionId" is not null
	and case when "fromDate" is null then 1=1 
	else (adr."CreatedDate" >= "fromDate" and adr."CreatedDate" <= "toDate")  end	)a
	group by  a."ProviderId"	
)
,admissionamount as (
select A."ProviderId" ,
	coalesce(A."AdmissionCashTotal",0) - coalesce(B."AdmissionRefCashTotal",0) "AdmissionCashTotal",
	coalesce(A."AdmissionCardTotal",0) - coalesce(B."AdmissionRefCardTotal",0) "AdmissionCardTotal",
	coalesce(A."AdmissionAmount",0) - coalesce(B."AdmissionRefAmount",0) "AdmissionAmount"
   from actadmissionamount A
	left join refadmissionamount B on A."ProviderId"=B."ProviderId"	 

)

,LabAmount as ( 
select a."ProviderId",sum(a."Cash") "LabCash",sum(a."Card") "LabCard" ,sum(a."Cash")+sum(a."Card")"LabAmount"  from(
select 	a."ProviderId",							  
	case when "PaidVia"='Cash' then  coalesce(ar."NetAmount",0) else 0 end "Cash"
	,case when "PaidVia"<>'Cash' then  coalesce(ar."NetAmount",0) else 0 end "Card"
from "Provider" a
join "LabBookingHeader" ar on trim(lower(ar."DoctorName")) = trim(lower(a."FullName")) and ar."Active" <> false
where case when providerId is null then 1=1 else a."ProviderId"=providerId end    
	AND   CASE WHEN (locationId IS NULL OR locationId=0) THEN 1=1 ELSE ar."LocationId"=locationId END
	AND case when "fromDate" is null then 1=1 
	else (ar."CreatedDate" >= "fromDate" and ar."CreatedDate" <= "toDate")  end)a
group by a."ProviderId" ) 

,PharmaSaleAmount as (
	
select a."ProviderId", sum(a."Cash") "PharmaSaleCash",sum(a."Card") "PharmaSaleCard" , sum(a."Cash")+ sum(a."Card")
	"PharmaSaleAmount" from (
select a."ProviderId", 
	 case when "PaidVia"='Cash' then  coalesce(ar."OverallNetAmount",0) else 0 end "Cash"
	,case when "PaidVia"<>'Cash' then  coalesce(ar."OverallNetAmount",0) else 0 end "Card"
		
from "Provider" a  
join "PharmacySaleHeader" ar on ar."ProviderId"=a."ProviderId"
where case when providerId is null then 1=1 else a."ProviderId"=providerId end 
	AND   CASE WHEN (locationId IS NULL OR locationId=0) THEN 1=1 ELSE ar."LocationId"=locationId END
	and case when "fromDate" is null then 1=1 
else (ar."SaleDate" >= "fromDate" and ar."SaleDate" <= "toDate")  end	)a
group by a."ProviderId"
)
,PharmaReturnAmount as (
select a."ProviderId", sum(a."Cash") "PharmaReturnCash",sum(a."Card") "PharmaReturnCard" ,sum(a."Cash")+sum(a."Card")   "PharmaReturnAmount" from(
select a."ProviderId", case when ar."PaidVia"='Cash' then  coalesce(srh."OverallNetAmount",0) else 0 end "Cash"
	,case when ar."PaidVia"='Card' then  coalesce(srh."OverallNetAmount",0) else 0 end "Card"
								   
				   
from "Provider" a
join "PharmacySaleHeader" ar on ar."ProviderId"=a."ProviderId"
left join "SaleReturnHeader" srh on srh."PharmacySaleHeaderId" = ar."PharmacySaleHeaderId"
where case when providerId is null then 1=1 else a."ProviderId"=providerId end
	AND   CASE WHEN (locationId IS NULL OR locationId=0) THEN 1=1 ELSE ar."LocationId"=locationId END
	and case when "fromDate" is null then 1=1 
else (srh."ReturnDate" >= "fromDate" and srh."ReturnDate" <= "toDate")  end	)a
group by a."ProviderId"
)

,PharmaAmount as (
select a."ProviderId", 
	
	 coalesce(a."PharmaSaleCash",0) - coalesce(b. "PharmaReturnCash",0) as  "PharmaSaleCash" ,
	 coalesce(a."PharmaSaleCard",0) - coalesce(b. "PharmaReturnCard",0) as  "PharmaSaleCard" ,
	coalesce(a."PharmaSaleAmount",0) - coalesce(b. "PharmaReturnAmount",0) as  "PharmaAmount" 
from PharmaSaleAmount a
	left join PharmaReturnAmount b on a."ProviderId"=b."ProviderId"

)

select distinct  a."ProviderId",a."DoctorName"::text "DoctorName",
						  ap."AppointmentCashTotal",ap."AppointmentCardTotal",ap."AppointmentAmount",
						  ad."AdmissionCashTotal",ad."AdmissionCardTotal",ad."AdmissionAmount",
						  lb."LabCash", lb."LabCard",lb."LabAmount",
sum(pa."PharmaSaleCash") over(partition by a."ProviderId")  "PharmacySaleCash",
sum(pa."PharmaSaleCard") over(partition by a."ProviderId") 	"PharmacySaleCard",
sum(pa."PharmaAmount") over(partition by a."ProviderId") "PharmacyAmount",
coalesce(ap."AppointmentCashTotal",0)+	coalesce(ad."AdmissionCashTotal",0)+coalesce(lb."LabCash",0)+coalesce(sum(pa."PharmaSaleCash") over(partition by a."ProviderId") ,0) "TotalCash",
coalesce(ap."AppointmentCardTotal",0)+	coalesce(ad."AdmissionCardTotal",0)+coalesce(lb."LabCard",0)+coalesce(sum(pa."PharmaSaleCard") over(partition by a."ProviderId"),0)  "TotalCard",	
coalesce(ap."AppointmentAmount",0)+coalesce(ad."AdmissionAmount",0)+coalesce(lb."LabAmount",0)
+coalesce(sum(pa."PharmaAmount") over(partition by a."ProviderId"),0) "Total"

from accountdata a
 left join appointmentamount ap on ap."ProviderId"=a."ProviderId"
left join admissionamount ad on ad."ProviderId"=a."ProviderId"
left join LabAmount lb on lb."ProviderId"=a."ProviderId"
left join PharmaAmount pa on pa."ProviderId"=a."ProviderId"
;
	end
$BODY$;

ALTER FUNCTION public."udf_Doctor_Revenue_location"(integer, integer, timestamp without time zone, timestamp without time zone)
    OWNER TO postgres;
