-- FUNCTION: public.udf_emloyee_new_revenue(integer, integer, integer, date, date)

DROP FUNCTION public.udf_emloyee_new_revenue(integer, integer, integer, date, date);

CREATE OR REPLACE FUNCTION public.udf_emloyee_new_revenue(
	accountid integer DEFAULT NULL::integer,
	locationid integer DEFAULT NULL::integer,
	roleid integer DEFAULT NULL::integer,
	"fromDate" date DEFAULT NULL::date,
	"toDate" date DEFAULT NULL::date)
    RETURNS TABLE("AccountId" integer, "EmployeeName" text, "RoleName" character varying, "RegistrationCashTotal" numeric, "RegistrationCardTotal" numeric, "RegistrationUPITotal" numeric, "RegistrationOnlineTotal" numeric, "RegistrationChequeTotal" numeric, "RegistrationPaytmTotal" numeric, registrationamount numeric, "AppointmentCashTotal" numeric, "AppointmentCardTotal" numeric, "AppointmentUPITotal" numeric, "AppointmentOnlineTotal" numeric, "AppointmentChequeTotal" numeric, "AppointmentPaytmTotal" numeric, "AppointmentAmount" numeric, "AdmissionCashTotal" numeric, "AdmissionCardTotal" numeric, "AdmissionUPITotal" numeric, "AdmissionOnlineTotal" numeric, "AdmissionChequeTotal" numeric, "AdmissionPaytmTotal" numeric, "AdmissionAmount" numeric, "LabCash" numeric, "LabCard" numeric, "LabUPI" numeric, "LabOnline" numeric, "LabCheque" numeric, "LabPaytm" numeric, "LabAmount" numeric, "PharmacySaleCash" numeric, "PharmacySaleCard" numeric, "PharmacySaleUPI" numeric, "PharmacySaleOnline" numeric, "PharmacySaleCheque" numeric, "PharmacySalePaytm" numeric, "PharmacyAmount" numeric, "TotalCash" numeric, "TotalCard" numeric, "TotalUPI" numeric, "TotalOnline" numeric, "TotalCheque" numeric, "TotalPaytm" numeric, "Total" numeric) 
    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	case when accountid is null then 1=1 else a."AccountId"=accountid end   
and	case when roleid is null then 1=1 else a."RoleId"=roleid end 	
AND CASE WHEN (locationId IS NULL OR locationId=0) THEN 1=1 ELSE LAM."LocationId"=locationId END
)
,patientRegistrationamount as(
 select a."AccountId",sum(A."CashTotal") "RegistrationCashTotal",sum(A."CardTotal") "RegistrationCardTotal",
 	sum(A."UPITotal") "RegistrationUPITotal",sum(A."OnlineTotal") "RegistrationOnlineTotal",
 	sum(A."ChequeTotal") "RegistrationChequeTotal",sum(A."PaytmTotal") "RegistrationPaytmTotal",
 	sum(A."CashTotal")+ sum(A."CardTotal")+ sum(A."UPITotal")+ sum(A."OnlineTotal")+ sum(A."ChequeTotal")+ sum(A."PaytmTotal") registrationamount
 	from ( 
	select  a."CreatedBy"  "AccountId",   
   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",
	case when A."PayTypeId"=3 then coalesce(A."Cost",0) else 0 end  "UPITotal" ,
   case when A."PayTypeId"=4 then coalesce(A."Cost",0) else 0 end "OnlineTotal",
	case when A."PayTypeId"=5 then coalesce(A."Cost",0) else 0 end  "ChequeTotal" ,
   case when A."PayTypeId"=6 then coalesce(A."Cost",0) else 0 end "PaytmTotal"
		from "Receipt" A
   join "Patient" p on  p."PatientId" =A."RespectiveId"          
   where A."ReceiptTypeId"=1 and p."Active" <> false and A."Active"<>false and A."RespectiveId" is not null
		and case when accountid is null then 1=1 else a."CreatedBy"=accountid end  
		and case when (locationId IS NULL OR locationId=0) THEN 1=1 ELSE p."LocationId"=locationId END 
		and		case when "fromDate" is null then 1=1 
	else (A."CreatedDate"::date >= "fromDate" and A."CreatedDate"::date <= "toDate")  end		
	) a group by  a."AccountId"	
)

,actappointmentamount as (
	
	select a."AccountId",sum(A."CashTotal") "AppointmentCashTotal",sum(A."CardTotal") "AppointmentCardTotal",
	sum(A."UPITotal") "AppointmentUPITotal",sum(A."OnlineTotal") "AppointmentOnlineTotal",
	sum(A."ChequeTotal") "AppointmentChequeTotal",sum(A."PaytmTotal") "AppointmentPaytmTotal",
	sum(A."CashTotal")+ sum(A."CardTotal")+ sum(A."UPITotal")+ sum(A."OnlineTotal")+ sum(A."ChequeTotal")+ sum(A."PaytmTotal") appointmentamount
	from (
   select  a."CreatedBy"  "AccountId",   
   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",
	case when A."PayTypeId"=3 then coalesce(A."Cost",0) else 0 end  "UPITotal" ,
   case when A."PayTypeId"=4 then coalesce(A."Cost",0) else 0 end "OnlineTotal",
	case when A."PayTypeId"=5 then coalesce(A."Cost",0) else 0 end  "ChequeTotal" ,
   case when A."PayTypeId"=6 then coalesce(A."Cost",0) else 0 end "PaytmTotal"										
		
		from "Receipt" A
    join "Appointment" ap on  ap."AppointmentId" =A."RespectiveId"  
	join "Provider" pr on pr."ProviderId"= ap."ProviderId"
    and ap."Active" =true	
   where case when accountid is null then 1=1 else a."CreatedBy"=accountid end  and A."ReceiptTypeId"=1 	
	and A."Active"<>false and A."RespectiveId" is not null
	AND CASE WHEN (locationId IS NULL OR locationId=0) THEN 1=1 ELSE ap."LocationId"=locationId END 
	and coalesce(A."IsRefunded",false) <>true and ap."Status" <> 'C' and	
	case when "fromDate" is null then 1=1 
	else (A."CreatedDate"::date >= "fromDate" and A."CreatedDate"::date <= "toDate")  end	
	)a 
	group by  a."AccountId"	 
)

,refundappointmentamount as (
	select a."AccountId" ,sum(A."CashTotal") "AppointmentRefCashTotal",sum(A."CardTotal") "AppointmentRefCardTotal",
	sum(A."UPITotal") "AppointmentRefUPITotal",sum(A."OnlineTotal") "AppointmentRefOnlineTotal",
	sum(A."ChequeTotal") "AppointmentRefChequeTotal",sum(A."PaytmTotal") "AppointmentRefPaytmTotal",
	sum(A."CashTotal")+ sum(A."CardTotal")+ sum(A."UPITotal")+ sum(A."OnlineTotal")+ sum(A."ChequeTotal")+ sum(A."PaytmTotal") refappointmentamount 
	from (
    select  a."CreatedBy"  "AccountId",
		   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" ,
		case when A."PayTypeId"=3 then coalesce(A."Cost",0) else 0 end  "UPITotal" ,
          case when A."PayTypeId"=4 then coalesce(A."Cost",0) else 0 end "OnlineTotal",
			case when A."PayTypeId"=5 then coalesce(A."Cost",0) else 0 end  "ChequeTotal" ,
          case when A."PayTypeId"=6 then coalesce(A."Cost",0) else 0 end "PaytmTotal"								
		 from "Receipt" A
    join "Appointment" ap on ap."AppointmentId" =A."RespectiveId"  
	join "Provider" pr on pr."ProviderId"= ap."ProviderId"
   and ap."Status" <> 'C'   and ap."Active" =true
	
   where case when accountid is null then 1=1 else a."CreatedBy"=accountid end   
	and A."Active"<>false and A."RespectiveId" is not null 
	AND CASE WHEN (locationId IS NULL OR locationId=0) THEN 1=1 ELSE ap."LocationId"=locationId END 
	and coalesce(A."IsRefunded",false) = true and 
	case when "fromDate" is null then 1=1 
	else (A."CreatedDate"::date >= "fromDate" and A."CreatedDate"::date <= "toDate")  end
	) a
	group by  a."AccountId"	
	
)

,appointmentamount as (
select  A."AccountId",
	coalesce(A."AppointmentCashTotal",0) - coalesce(B."AppointmentRefCashTotal",0) as "AppointmentCashTotal" ,
	coalesce(A."AppointmentCardTotal",0) - coalesce(B."AppointmentRefCardTotal",0) as "AppointmentCardTotal" ,
	coalesce(A."AppointmentUPITotal",0) - coalesce(B."AppointmentRefUPITotal",0) as "AppointmentUPITotal" ,
	coalesce(A."AppointmentOnlineTotal",0) - coalesce(B."AppointmentRefOnlineTotal",0) as "AppointmentOnlineTotal" ,
	coalesce(A."AppointmentChequeTotal",0) - coalesce(B."AppointmentRefChequeTotal",0) as "AppointmentChequeTotal" ,
	coalesce(A."AppointmentPaytmTotal",0) - coalesce(B."AppointmentRefPaytmTotal",0) as "AppointmentPaytmTotal" ,
	coalesce(A.appointmentamount,0) - coalesce(B.refappointmentamount,0) as "AppointmentAmount" from actappointmentamount A
	left join refundappointmentamount B on A."AccountId"=B."AccountId"
	
	
)

,actadmissionamount as (
	
	select a."AccountId" , sum(A."CashTotal") "AdmissionCashTotal",sum(A."CardTotal") "AdmissionCardTotal",
	 sum(A."UPITotal") "AdmissionUPITotal",sum(A."OnlineTotal") "AdmissionOnlineTotal",
	 sum(A."ChequeTotal") "AdmissionChequeTotal",sum(A."PaytmTotal") "AdmissionPaytmTotal",
	sum(A."CashTotal")+ sum(A."CardTotal")+ sum(A."UPITotal")+ sum(A."OnlineTotal")+ sum(A."ChequeTotal")+ sum(A."PaytmTotal") "AdmissionAmount"
	from (
select adr."CreatedBy" "AccountId" ,
   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"	,
	case when adr."PayTypeId"=3 then coalesce(adr."Cost",0) else 0 end  "UPITotal" ,
   case when adr."PayTypeId"=4 then coalesce(adr."Cost",0) else 0 end "OnlineTotal"	,	
	case when adr."PayTypeId"=5 then coalesce(adr."Cost",0) else 0 end  "ChequeTotal" ,
   case when adr."PayTypeId"=6 then coalesce(adr."Cost",0) else 0 end "PaytmTotal"											
												  
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 accountid is null then 1=1 else adr."CreatedBy"=accountid end 	
	and adr."ReceiptTypeId"=1  and adr."Active"<>false  and adr."AdmissionId" is not null
		AND CASE WHEN (locationId IS NULL OR locationId=0) THEN 1=1 ELSE ad."LocationId"=locationId END 
	and case when "fromDate" is null then 1=1 
	else (adr."CreatedDate"::date >= "fromDate" and adr."CreatedDate"::date <= "toDate")  end	)a
	group by  a."AccountId"	
)

,refadmissionamount as (
	select a."AccountId" , sum(A."CashTotal") "AdmissionRefCashTotal",sum(A."CardTotal") "AdmissionRefCardTotal",
	sum(A."UPITotal") "AdmissionRefUPITotal",sum(A."OnlineTotal") "AdmissionRefOnlineTotal",
	sum(A."ChequeTotal") "AdmissionRefChequeTotal",sum(A."PaytmTotal") "AdmissionRefPaytmTotal",
	sum(A."CashTotal")+ sum(A."CardTotal")+ sum(A."UPITotal")+ sum(A."OnlineTotal")+ sum(A."ChequeTotal")+ sum(A."PaytmTotal") "AdmissionRefAmount" 
	from (
select adr."CreatedBy" "AccountId"  ,  
		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" ,
		case when adr."PayTypeId"=3 then coalesce(adr."Cost",0) else 0 end  "UPITotal" ,
        case when adr."PayTypeId"=4 then coalesce(adr."Cost",0) else 0 end "OnlineTotal" ,
		case when adr."PayTypeId"=5 then coalesce(adr."Cost",0) else 0 end  "ChequeTotal" ,
        case when adr."PayTypeId"=6 then coalesce(adr."Cost",0) else 0 end "PaytmTotal" 									
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 accountid is null then 1=1 else adr."CreatedBy"=accountid end 	
	 and adr."IsRefunded" = true   and adr."Active"<>false  and adr."AdmissionId" is not null
		AND CASE WHEN (locationId IS NULL OR locationId=0) THEN 1=1 ELSE ad."LocationId"=locationId END 
	and case when "fromDate" is null then 1=1 
	else (adr."CreatedDate"::date >= "fromDate" and adr."CreatedDate"::date <= "toDate")  end	)a
	group by  a."AccountId"	
)

,admissionamount as (
select A."AccountId" ,
	coalesce(A."AdmissionCashTotal",0) - coalesce(B."AdmissionRefCashTotal",0) "AdmissionCashTotal",
	coalesce(A."AdmissionCardTotal",0) - coalesce(B."AdmissionRefCardTotal",0) "AdmissionCardTotal",
	coalesce(A."AdmissionUPITotal",0) - coalesce(B."AdmissionRefUPITotal",0) "AdmissionUPITotal",
	coalesce(A."AdmissionOnlineTotal",0) - coalesce(B."AdmissionRefOnlineTotal",0) "AdmissionOnlineTotal",
	coalesce(A."AdmissionChequeTotal",0) - coalesce(B."AdmissionRefChequeTotal",0) "AdmissionChequeTotal",
	coalesce(A."AdmissionPaytmTotal",0) - coalesce(B."AdmissionRefPaytmTotal",0) "AdmissionPaytmTotal",
	coalesce(A."AdmissionAmount",0) - coalesce(B."AdmissionRefAmount",0) "AdmissionAmount"
   from actadmissionamount A
	left join refadmissionamount B on A."AccountId"=B."AccountId"	 

)
,LabAmount as ( 
select a."AccountId",sum(a."Cash") "LabCash",sum(a."Card") "LabCard" ,
	sum(a."UPI") "LabUPI",sum(a."Online") "LabOnline" ,
	sum(a."Cheque") "LabCheque",sum(a."Paytm") "LabPaytm" ,
	sum(a."Cash")+sum(a."Card")+sum(a."UPI")+sum(a."Online")+sum(a."Cheque")+sum(a."Paytm")"LabAmount"
	from(
select 	a."AccountId",							  
	case when ar."PayTypeId"=1 then  coalesce(ar."OverallNetAmount",0) else 0 end "Cash"
	,case when ar."PayTypeId"=2 then  coalesce(ar."OverallNetAmount",0) else 0 end "Card"
		,case when ar."PayTypeId"=3 then  coalesce(ar."OverallNetAmount",0) else 0 end "UPI"
	,case when ar."PayTypeId"=4 then  coalesce(ar."OverallNetAmount",0) else 0 end "Online"
	,case when ar."PayTypeId"=5 then  coalesce(ar."OverallNetAmount",0) else 0 end "Cheque"
	,case when ar."PayTypeId"=6 then  coalesce(ar."OverallNetAmount",0) else 0 end "Paytm"										
from "Account" a		
join "NewLabBookingHeader" ar on ar."CreatedBy" = a."AccountId" and ar."Active" <> false
where 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 ar."LocationId"=locationId END 
	and case when "fromDate" is null then 1=1 
	else (ar."CreatedDate"::date >= "fromDate" and ar."CreatedDate"::date <= "toDate")  end)a
group by a."AccountId" )

,PharmaSaleAmount as (
	
select a."AccountId", sum(a."Cash") "PharmaSaleCash",sum(a."Card") "PharmaSaleCard" ,
	sum(a."UPI") "PharmaSaleUPI",sum(a."Online") "PharmaSaleOnline" ,
	sum(a."Cheque") "PharmaSaleCheque",sum(a."Paytm") "PharmaSalePaytm" ,
	sum(a."Cash")+ sum(a."Card")+ sum(a."UPI")+ sum(a."Online")+ sum(a."Cheque")+ sum(a."Paytm") "PharmaSaleAmount" 
	from (
select a."AccountId", 
	 case when ar."PayTypeId"=1 then  coalesce(ar."OverallNetAmount",0) else 0 end "Cash"
	,case when ar."PayTypeId"=2 then  coalesce(ar."OverallNetAmount",0) else 0 end "Card"
	,case when ar."PayTypeId"=3 then  coalesce(ar."OverallNetAmount",0) else 0 end "UPI"
	,case when ar."PayTypeId"=4 then  coalesce(ar."OverallNetAmount",0) else 0 end "Online"
	,case when ar."PayTypeId"=5 then  coalesce(ar."OverallNetAmount",0) else 0 end "Cheque"
	,case when ar."PayTypeId"=6 then  coalesce(ar."OverallNetAmount",0) else 0 end "Paytm"										
		
from "Account" a
join "PharmacySaleHeader" ar on ar."CreatedBy" = a."AccountId"
where 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 ar."LocationId"=locationId END 
		and case when "fromDate" is null then 1=1 
else (ar."SaleDate"::date >= "fromDate" and ar."SaleDate"::date <= "toDate")  end	
	)a
group by a."AccountId"
)
,PharmaReturnAmount as (
select a."AccountId", sum(a."Cash") "PharmaReturnCash",sum(a."Card") "PharmaReturnCard" ,
	sum(a."UPI") "PharmaReturnUPI",sum(a."Online") "PharmaReturnOnline" ,
	sum(a."Cheque") "PharmaReturnCheque",sum(a."Paytm") "PharmaReturnPaytm" ,
	sum(a."Cash")+sum(a."Card")+sum(a."UPI")  +sum(a."Online") +sum(a."Cheque")+sum(a."Paytm") "PharmaReturnAmount"
	from(
select a."AccountId", 
	case when srh."PayTypeId"=1 then  coalesce(ar."OverallNetAmount",0) else 0 end "Cash"
	,case when srh."PayTypeId"=2 then  coalesce(ar."OverallNetAmount",0) else 0 end "Card"
	,case when srh."PayTypeId"=3 then  coalesce(ar."OverallNetAmount",0) else 0 end "UPI"
	,case when srh."PayTypeId"=4 then  coalesce(ar."OverallNetAmount",0) else 0 end "Online"										
	,case when srh."PayTypeId"=5 then  coalesce(ar."OverallNetAmount",0) else 0 end "Cheque"
	,case when srh."PayTypeId"=6 then  coalesce(ar."OverallNetAmount",0) else 0 end "Paytm"							   
							   
from "Account" a
JOIN "LocationAccountMap" LAM on LAM."AccountId"=a."AccountId"
left join "SaleReturnHeader" ar on ar."CreatedBy" = a."AccountId"
left join "PharmacySaleHeader" srh on srh."PharmacySaleHeaderId" = ar."PharmacySaleHeaderId"
where case when accountid is null then 1=1 else ar."CreatedBy"=accountid end 
	AND   CASE WHEN (locationId IS NULL OR locationId=0) THEN 1=1 ELSE LAM."LocationId"=locationId END
	and case when "fromDate" is null then 1=1 
else (ar."ReturnDate"::date >= "fromDate" and ar."ReturnDate"::date <= "toDate")  end	
	)a
group by a."AccountId"
)

 ,PharmaAmount as (
 select a."AccountId", 	
 	 coalesce(a."PharmaSaleCash",0) - coalesce(b."PharmaReturnCash",0) as  "PharmaSaleCash" ,
	 coalesce(a."PharmaSaleCard",0) - coalesce(b."PharmaReturnCard",0) as  "PharmaSaleCard" ,
	 coalesce(a."PharmaSaleUPI",0) - coalesce(b."PharmaReturnUPI",0) as  "PharmaSaleUPI" ,
	 coalesce(a."PharmaSaleOnline",0) - coalesce(b."PharmaReturnOnline",0) as  "PharmaSaleOnline" ,
	 coalesce(a."PharmaSaleCheque",0) - coalesce(b."PharmaReturnCheque",0) as  "PharmaSaleCheque" ,
	 coalesce(a."PharmaSalePaytm",0) - coalesce(b."PharmaReturnPaytm",0) as  "PharmaSalePaytm" ,
	coalesce(a."PharmaSaleAmount",0) - coalesce(b."PharmaReturnAmount",0) as  "PharmaAmount" 
 from PharmaSaleAmount a
 	left join PharmaReturnAmount b on a."AccountId"=b."AccountId"
 )
 
select distinct  a."AccountId",a."EmployeeName",a."RoleName",
                           pr."RegistrationCashTotal",pr."RegistrationCardTotal",pr."RegistrationUPITotal",pr."RegistrationOnlineTotal",pr."RegistrationChequeTotal",pr."RegistrationPaytmTotal"
						,pr."registrationamount",
						  ap."AppointmentCashTotal",ap."AppointmentCardTotal",ap."AppointmentUPITotal",ap."AppointmentOnlineTotal",ap."AppointmentChequeTotal",ap."AppointmentPaytmTotal"
						,ap."AppointmentAmount",
						  ad."AdmissionCashTotal",ad."AdmissionCardTotal",ad."AdmissionUPITotal",ad."AdmissionOnlineTotal",ad."AdmissionChequeTotal",ad."AdmissionPaytmTotal"
						,ad."AdmissionAmount",
						  lb."LabCash", lb."LabCard",lb."LabUPI", lb."LabOnline",lb."LabCheque", lb."LabPaytm"
						,lb."LabAmount",
sum(pa."PharmaSaleCash") over(partition by a."AccountId")  "PharmacySaleCash",
sum(pa."PharmaSaleCard") over(partition by a."AccountId") 	"PharmacySaleCard",
sum(pa."PharmaSaleUPI") over(partition by a."AccountId")  "PharmacySaleUPI",
sum(pa."PharmaSaleOnline") over(partition by a."AccountId") 	"PharmacySaleOnline",
sum(pa."PharmaSaleCheque") over(partition by a."AccountId")  "PharmacySaleCheque",
sum(pa."PharmaSalePaytm") over(partition by a."AccountId") 	"PharmacySalePaytm",
sum(pa."PharmaAmount") over(partition by a."AccountId") "PharmacyAmount",
coalesce(pr."RegistrationCashTotal",0)+coalesce(ap."AppointmentCashTotal",0)+	coalesce(ad."AdmissionCashTotal",0)+coalesce(lb."LabCash",0)+coalesce(sum(pa."PharmaSaleCash") over(partition by a."AccountId") ,0) "TotalCash",
coalesce(pr."RegistrationCardTotal",0)+	coalesce(ap."AppointmentCardTotal",0)+	coalesce(ad."AdmissionCardTotal",0)+coalesce(lb."LabCard",0)+coalesce(sum(pa."PharmaSaleCard") over(partition by a."AccountId"),0)  "TotalCard",	
coalesce(pr."RegistrationUPITotal",0)+	coalesce(ap."AppointmentUPITotal",0)+	coalesce(ad."AdmissionUPITotal",0)+coalesce(lb."LabUPI",0)+coalesce(sum(pa."PharmaSaleUPI") over(partition by a."AccountId") ,0) "TotalUPI",
coalesce(pr."RegistrationOnlineTotal",0)+coalesce(ap."AppointmentOnlineTotal",0)+	coalesce(ad."AdmissionOnlineTotal",0)+coalesce(lb."LabOnline",0)+coalesce(sum(pa."PharmaSaleOnline") over(partition by a."AccountId"),0)  "TotalOnline",	
coalesce(pr."RegistrationChequeTotal",0)+coalesce(ap."AppointmentChequeTotal",0)+	coalesce(ad."AdmissionChequeTotal",0)+coalesce(lb."LabCheque",0)+coalesce(sum(pa."PharmaSaleCheque") over(partition by a."AccountId") ,0) "TotalCheque",
coalesce(pr."RegistrationPaytmTotal",0)+coalesce(ap."AppointmentPaytmTotal",0)+	coalesce(ad."AdmissionPaytmTotal",0)+coalesce(lb."LabPaytm",0)+coalesce(sum(pa."PharmaSalePaytm") over(partition by a."AccountId"),0)  "TotalPaytm",	

coalesce(pr."registrationamount",0)+coalesce(ap."AppointmentAmount",0)+coalesce(ad."AdmissionAmount",0)+coalesce(lb."LabAmount",0)
+coalesce(sum(pa."PharmaAmount") over(partition by a."AccountId"),0) "Total"

from accountdata a

left join patientRegistrationamount pr on pr."AccountId"=a."AccountId"
left join appointmentamount ap on ap."AccountId"=a."AccountId"
left join admissionamount ad on ad."AccountId"=a."AccountId"
left join LabAmount lb on lb."AccountId"=a."AccountId"
left join PharmaAmount pa on pa."AccountId"=a."AccountId"
;
	end
$BODY$;

ALTER FUNCTION public.udf_emloyee_new_revenue(integer, integer, integer, date, date)
    OWNER TO postgres;
