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

-- DROP FUNCTION IF EXISTS 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, 
				    "RegistrationNotPaidTotal" numeric, "RegistrationOtherTotal" numeric,  "RegistrationCardSwipeTotal" numeric, "RegistrationCardStandAloneTotal" numeric,
				     "RegistrationCardUPITotal" numeric,"RegistrationCardGpayTotal" numeric,"RegistrationCashDrawerTotal" numeric,"RegistrationCashChequeTotal" numeric,
				     "RegistrationCashDDTotal" numeric,"RegistrationWalletPaytmOfflineTotal" numeric,"RegistrationWalletPhonePeOfflineTotal" numeric,"RegistrationCashRemoteDepositTotal" numeric,
				       "RegistrationWalletPaytmDQRTotal" numeric, registrationamount numeric, 
				  "AppointmentCashTotal" numeric, "AppointmentCardTotal" numeric, "AppointmentUPITotal" numeric, "AppointmentOnlineTotal" numeric, "AppointmentChequeTotal" numeric, "AppointmentPaytmTotal" numeric, 
				  "AppointmentNotPaidTotal" numeric, "AppointmentOtherTotal" numeric,  "AppointmentCardSwipeTotal" numeric, "AppointmentCardStandAloneTotal" numeric,
				     "AppointmentCardUPITotal" numeric,"AppointmentCardGpayTotal" numeric,"AppointmentCashDrawerTotal" numeric,"AppointmentCashChequeTotal" numeric,
				     "AppointmentCashDDTotal" numeric,"AppointmentWalletPaytmOfflineTotal" numeric,"AppointmentWalletPhonePeOfflineTotal" numeric,"AppointmentCashRemoteDepositTotal" numeric,
				       "AppointmentWalletPaytmDQRTotal" numeric,
			        "AppointmentAmount" numeric, 
				  "AdmissionCashTotal" numeric, "AdmissionCardTotal" numeric, "AdmissionUPITotal" numeric, "AdmissionOnlineTotal" numeric, "AdmissionChequeTotal" numeric, "AdmissionPaytmTotal" numeric,
				  "AdmissionNotPaidTotal" numeric, "AdmissionOtherTotal" numeric,  "AdmissionCardSwipeTotal" numeric, "AdmissionCardStandAloneTotal" numeric,
				     "AdmissionCardUPITotal" numeric,"AdmissionCardGpayTotal" numeric,"AdmissionCashDrawerTotal" numeric,"AdmissionCashChequeTotal" numeric,
				     "AdmissionCashDDTotal" numeric,"AdmissionWalletPaytmOfflineTotal" numeric,"AdmissionWalletPhonePeOfflineTotal" numeric,"AdmissionCashRemoteDepositTotal" numeric,
				       "AdmissionWalletPaytmDQRTotal" numeric,
				  "AdmissionAmount" numeric,
				  "LabCash" numeric, "LabCard" numeric, "LabUPI" numeric, "LabOnline" numeric, "LabCheque" numeric, "LabPaytm" numeric, 
				  "LabNotPaidTotal" numeric, "LabOtherTotal" numeric,  "LabCardSwipeTotal" numeric, "LabCardStandAloneTotal" numeric,
				     "LabCardUPITotal" numeric,"LabCardGpayTotal" numeric,"LabCashDrawerTotal" numeric,"LabCashChequeTotal" numeric,
				     "LabCashDDTotal" numeric,"LabWalletPaytmOfflineTotal" numeric,"LabWalletPhonePeOfflineTotal" numeric,"LabCashRemoteDepositTotal" numeric,
				       "LabWalletPaytmDQRTotal" numeric,
				  "LabAmount" numeric, 
				  "PharmacySaleCash" numeric, "PharmacySaleCard" numeric, "PharmacySaleUPI" numeric, "PharmacySaleOnline" numeric, "PharmacySaleCheque" numeric, "PharmacySalePaytm" numeric, 
				   "PharmacySaleNotPaidTotal" numeric, "PharmacySaleOtherTotal" numeric,  "PharmacySaleCardSwipeTotal" numeric, "PharmacySaleCardStandAloneTotal" numeric,
				     "PharmacySaleCardUPITotal" numeric,"PharmacySaleCardGpayTotal" numeric,"PharmacySaleCashDrawerTotal" numeric,"PharmacySaleCashChequeTotal" numeric,
				     "PharmacySaleCashDDTotal" numeric,"PharmacySaleWalletPaytmOfflineTotal" numeric,"PharmacySaleWalletPhonePeOfflineTotal" numeric,"PharmacySaleCashRemoteDepositTotal" numeric,
				       "PharmacySaleWalletPaytmDQRTotal" numeric,
				  "PharmacyAmount" numeric, 
				  "TotalCash" numeric, "TotalCard" numeric, "TotalUPI" numeric, "TotalOnline" numeric, "TotalCheque" numeric, "TotalPaytm" numeric, 
				  "NotPaidTotal" numeric, "OtherTotal" numeric,  "CardSwipeTotal" numeric, "CardStandAloneTotal" numeric,
				     "CardUPITotal" numeric,"CardGpayTotal" numeric,"CashDrawerTotal" numeric,"CashChequeTotal" numeric,
				     "CashDDTotal" numeric,"WalletPaytmOfflineTotal" numeric,"WalletPhonePeOfflineTotal" numeric,"CashRemoteDepositTotal" numeric,
				       "WalletPaytmDQRTotal" 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."NotPaidTotal") "RegistrationNotPaidTotal",
	sum(A."OtherTotal") "RegistrationOtherTotal",
	sum(A."CardSwipeTotal") "RegistrationCardSwipeTotal",
	sum(A."CardStandAloneTotal") "RegistrationCardStandAloneTotal",
	sum(A."CardUPITotal") "RegistrationCardUPITotal",
	sum(A."CardGpayTotal") "RegistrationCardGpayTotal",
	sum(A."CashDrawerTotal") "RegistrationCashDrawerTotal",
	sum(A."CashChequeTotal") "RegistrationCashChequeTotal",
	sum(A."CashDDTotal") "RegistrationCashDDTotal",
	sum(A."WalletPaytmOfflineTotal") "RegistrationWalletPaytmOfflineTotal",
	sum(A."WalletPhonePeOfflineTotal") "RegistrationWalletPhonePeOfflineTotal",
	sum(A."CashRemoteDepositTotal") "RegistrationCashRemoteDepositTotal",
	sum(A."WalletPaytmDQRTotal") "RegistrationWalletPaytmDQRTotal",
	sum(A."CashTotal")+ sum(A."CardTotal")+ sum(A."UPITotal")+ sum(A."OnlineTotal")+ sum(A."ChequeTotal")+ sum(A."PaytmTotal")
	
	+sum(A."OtherTotal")+sum(A."CardSwipeTotal")
	+sum(A."CardStandAloneTotal")+sum(A."CardUPITotal")+sum(A."CardGpayTotal")+sum(A."CashDrawerTotal")+sum(A."CashChequeTotal")+sum(A."NotPaidTotal")+
	sum(A."CashDDTotal")+sum(A."WalletPaytmOfflineTotal")+sum(A."WalletPhonePeOfflineTotal")+sum(A."CashRemoteDepositTotal")+sum(A."WalletPaytmDQRTotal")
 	 
	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",
   case when A."PayTypeId"=7 then coalesce(A."Cost",0) else 0 end "NotPaidTotal",
   case when A."PayTypeId"=8 then coalesce(A."Cost",0) else 0 end  "OtherTotal",
   case when A."PayTypeId"=9 then coalesce(A."Cost",0) else 0 end  "CardSwipeTotal",
   case when A."PayTypeId"=10 then coalesce(A."Cost",0) else 0 end  "CardStandAloneTotal",
   case when A."PayTypeId"=11 then coalesce(A."Cost",0) else 0 end "CardUPITotal",
   case when A."PayTypeId"=12 then coalesce(A."Cost",0) else 0 end  "CardGpayTotal",
   case when A."PayTypeId"=13 then coalesce(A."Cost",0) else 0 end  "CashDrawerTotal",
   case when A."PayTypeId"=14 then coalesce(A."Cost",0) else 0 end "CashChequeTotal",
   case when A."PayTypeId"=15 then coalesce(A."Cost",0) else 0 end "CashDDTotal",
   case when A."PayTypeId"=16 then coalesce(A."Cost",0) else 0 end  "WalletPaytmOfflineTotal",
   case when A."PayTypeId"=17 then coalesce(A."Cost",0) else 0 end  "WalletPhonePeOfflineTotal",
   case when A."PayTypeId"=18 then coalesce(A."Cost",0) else 0 end  "CashRemoteDepositTotal",
   case when A."PayTypeId"=19 then coalesce(A."Cost",0) else 0 end   "WalletPaytmDQRTotal"	
		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."NotPaidTotal") "AppointmentNotPaidTotal",
	sum(A."OtherTotal") "AppointmentOtherTotal",
	sum(A."CardSwipeTotal") "AppointmentCardSwipeTotal",
	sum(A."CardStandAloneTotal") "AppointmentCardStandAloneTotal",
	sum(A."CardUPITotal") "AppointmentCardUPITotal",
	sum(A."CardGpayTotal") "AppointmentCardGpayTotal",
	sum(A."CashDrawerTotal") "AppointmentCashDrawerTotal",
	sum(A."CashChequeTotal") "AppointmentCashChequeTotal",
	sum(A."CashDDTotal") "AppointmentCashDDTotal",
	sum(A."WalletPaytmOfflineTotal") "AppointmentWalletPaytmOfflineTotal",
	sum(A."WalletPhonePeOfflineTotal") "AppointmentWalletPhonePeOfflineTotal",
	sum(A."CashRemoteDepositTotal") "AppointmentCashRemoteDepositTotal",
	sum(A."WalletPaytmDQRTotal") "AppointmentWalletPaytmDQRTotal",
	sum(A."CashTotal")+ sum(A."CardTotal")+ sum(A."UPITotal")+ sum(A."OnlineTotal")+ sum(A."ChequeTotal")+ sum(A."PaytmTotal")
	
	+sum(A."OtherTotal")+sum(A."CardSwipeTotal")
	+sum(A."CardStandAloneTotal")+sum(A."CardUPITotal")+sum(A."CardGpayTotal")+sum(A."CashDrawerTotal")+sum(A."CashChequeTotal")+sum(A."NotPaidTotal")+
	sum(A."CashDDTotal")+sum(A."WalletPaytmOfflineTotal")+sum(A."WalletPhonePeOfflineTotal")+sum(A."CashRemoteDepositTotal")+sum(A."WalletPaytmDQRTotal")
	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",
    case when A."PayTypeId"=7 then coalesce(A."Cost",0) else 0 end "NotPaidTotal",
   case when A."PayTypeId"=8 then coalesce(A."Cost",0) else 0 end  "OtherTotal",
   case when A."PayTypeId"=9 then coalesce(A."Cost",0) else 0 end  "CardSwipeTotal",
   case when A."PayTypeId"=10 then coalesce(A."Cost",0) else 0 end  "CardStandAloneTotal",
   case when A."PayTypeId"=11 then coalesce(A."Cost",0) else 0 end "CardUPITotal",
   case when A."PayTypeId"=12 then coalesce(A."Cost",0) else 0 end  "CardGpayTotal",
   case when A."PayTypeId"=13 then coalesce(A."Cost",0) else 0 end  "CashDrawerTotal",
   case when A."PayTypeId"=14 then coalesce(A."Cost",0) else 0 end "CashChequeTotal",
   case when A."PayTypeId"=15 then coalesce(A."Cost",0) else 0 end "CashDDTotal",
   case when A."PayTypeId"=16 then coalesce(A."Cost",0) else 0 end  "WalletPaytmOfflineTotal",
   case when A."PayTypeId"=17 then coalesce(A."Cost",0) else 0 end  "WalletPhonePeOfflineTotal",
   case when A."PayTypeId"=18 then coalesce(A."Cost",0) else 0 end  "CashRemoteDepositTotal",
   case when A."PayTypeId"=19 then coalesce(A."Cost",0) else 0 end   "WalletPaytmDQRTotal"	
	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."NotPaidTotal") "AppointmentRefNotPaidTotal",
	sum(A."OtherTotal") "AppointmentRefOtherTotal",
	sum(A."CardSwipeTotal") "AppointmentRefCardSwipeTotal",
	sum(A."CardStandAloneTotal") "AppointmentRefCardStandAloneTotal",
	sum(A."CardUPITotal") "AppointmentRefCardUPITotal",
	sum(A."CardGpayTotal") "AppointmentRefCardGpayTotal",
	sum(A."CashDrawerTotal") "AppointmentRefCashDrawerTotal",
	sum(A."CashChequeTotal") "AppointmentRefCashChequeTotal",
	sum(A."CashDDTotal") "AppointmentRefCashDDTotal",
	sum(A."WalletPaytmOfflineTotal") "AppointmentRefWalletPaytmOfflineTotal",
	sum(A."WalletPhonePeOfflineTotal") "AppointmentRefWalletPhonePeOfflineTotal",
	sum(A."CashRemoteDepositTotal") "AppointmentRefCashRemoteDepositTotal",
	sum(A."WalletPaytmDQRTotal") "AppointmentRefWalletPaytmDQRTotal",
	sum(A."CashTotal")+ sum(A."CardTotal")+ sum(A."UPITotal")+ sum(A."OnlineTotal")+ sum(A."ChequeTotal")+ sum(A."PaytmTotal")
	+sum(A."OtherTotal")+sum(A."CardSwipeTotal")+sum(A."CardStandAloneTotal")+sum(A."CardUPITotal")+sum(A."CardGpayTotal")+sum(A."CashDrawerTotal")+sum(A."CashChequeTotal")+sum(A."NotPaidTotal")+
	sum(A."CashDDTotal")+sum(A."WalletPaytmOfflineTotal")+sum(A."WalletPhonePeOfflineTotal")+sum(A."CashRemoteDepositTotal")+sum(A."WalletPaytmDQRTotal")
	
	 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",
   case when A."PayTypeId"=7 then coalesce(A."Cost",0) else 0 end "NotPaidTotal",
   case when A."PayTypeId"=8 then coalesce(A."Cost",0) else 0 end  "OtherTotal",
   case when A."PayTypeId"=9 then coalesce(A."Cost",0) else 0 end  "CardSwipeTotal",
   case when A."PayTypeId"=10 then coalesce(A."Cost",0) else 0 end  "CardStandAloneTotal",
   case when A."PayTypeId"=11 then coalesce(A."Cost",0) else 0 end "CardUPITotal",
   case when A."PayTypeId"=12 then coalesce(A."Cost",0) else 0 end  "CardGpayTotal",
   case when A."PayTypeId"=13 then coalesce(A."Cost",0) else 0 end  "CashDrawerTotal",
   case when A."PayTypeId"=14 then coalesce(A."Cost",0) else 0 end "CashChequeTotal",
   case when A."PayTypeId"=15 then coalesce(A."Cost",0) else 0 end "CashDDTotal",
   case when A."PayTypeId"=16 then coalesce(A."Cost",0) else 0 end  "WalletPaytmOfflineTotal",
   case when A."PayTypeId"=17 then coalesce(A."Cost",0) else 0 end  "WalletPhonePeOfflineTotal",
   case when A."PayTypeId"=18 then coalesce(A."Cost",0) else 0 end  "CashRemoteDepositTotal",
   case when A."PayTypeId"=19 then coalesce(A."Cost",0) else 0 end   "WalletPaytmDQRTotal"
		 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."AppointmentNotPaidTotal",0) - coalesce(B."AppointmentRefNotPaidTotal",0) as "AppointmentNotPaidTotal" ,
	coalesce(A."AppointmentOtherTotal",0) - coalesce(B."AppointmentRefOtherTotal",0) as "AppointmentOtherTotal" ,
	coalesce(A."AppointmentCardSwipeTotal",0) - coalesce(B."AppointmentRefCardSwipeTotal",0) as "AppointmentCardSwipeTotal" ,
	coalesce(A."AppointmentCardStandAloneTotal",0) - coalesce(B."AppointmentRefCardStandAloneTotal",0) as "AppointmentCardStandAloneTotal" ,
	coalesce(A."AppointmentCardUPITotal",0) - coalesce(B."AppointmentRefCardUPITotal",0) as "AppointmentCardUPITotal" ,
	coalesce(A."AppointmentCardGpayTotal",0) - coalesce(B."AppointmentRefCardGpayTotal",0) as "AppointmentCardGpayTotal" ,
	coalesce(A."AppointmentCashDrawerTotal",0) - coalesce(B."AppointmentRefCashDrawerTotal",0) as "AppointmentCashDrawerTotal" ,
	coalesce(A."AppointmentCashChequeTotal",0) - coalesce(B."AppointmentRefCashChequeTotal",0) as "AppointmentCashChequeTotal" ,
	coalesce(A."AppointmentCashDDTotal",0) - coalesce(B."AppointmentRefCashDDTotal",0) as "AppointmentCashDDTotal" ,
	coalesce(A."AppointmentWalletPaytmOfflineTotal",0) - coalesce(B."AppointmentRefWalletPaytmOfflineTotal",0) as "AppointmentWalletPaytmOfflineTotal" ,
	coalesce(A."AppointmentWalletPhonePeOfflineTotal",0) - coalesce(B."AppointmentRefWalletPhonePeOfflineTotal",0) as "AppointmentWalletPhonePeOfflineTotal" ,
	coalesce(A."AppointmentCashRemoteDepositTotal",0) - coalesce(B."AppointmentRefCashRemoteDepositTotal",0) as "AppointmentCashRemoteDepositTotal" ,
		coalesce(A."AppointmentWalletPaytmDQRTotal",0) - coalesce(B."AppointmentRefWalletPaytmDQRTotal",0) as "AppointmentWalletPaytmDQRTotal" ,
	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."NotPaidTotal") "AdmissionNotPaidTotal",
	sum(A."OtherTotal") "AdmissionOtherTotal",
	sum(A."CardSwipeTotal") "AdmissionCardSwipeTotal",
	sum(A."CardStandAloneTotal") "AdmissionCardStandAloneTotal",
	sum(A."CardUPITotal") "AdmissionCardUPITotal",
	sum(A."CardGpayTotal") "AdmissionCardGpayTotal",
	sum(A."CashDrawerTotal") "AdmissionCashDrawerTotal",
	sum(A."CashChequeTotal") "AdmissionCashChequeTotal",
	sum(A."CashDDTotal") "AdmissionCashDDTotal",
	sum(A."WalletPaytmOfflineTotal") "AdmissionWalletPaytmOfflineTotal",
	sum(A."WalletPhonePeOfflineTotal") "AdmissionWalletPhonePeOfflineTotal",
	sum(A."CashRemoteDepositTotal") "AdmissionCashRemoteDepositTotal",
	sum(A."WalletPaytmDQRTotal") "AdmissionWalletPaytmDQRTotal",
	sum(A."CashTotal")+ sum(A."CardTotal")+ sum(A."UPITotal")+ sum(A."OnlineTotal")+ sum(A."ChequeTotal")+ sum(A."PaytmTotal")
	+sum(A."OtherTotal")+sum(A."CardSwipeTotal")+sum(A."CardStandAloneTotal")+sum(A."CardUPITotal")+sum(A."CardGpayTotal")+sum(A."CashDrawerTotal")+sum(A."CashChequeTotal")+sum(A."NotPaidTotal")+
	sum(A."CashDDTotal")+sum(A."WalletPaytmOfflineTotal")+sum(A."WalletPhonePeOfflineTotal")+sum(A."CashRemoteDepositTotal")+sum(A."WalletPaytmDQRTotal")
	"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"	,
       case when adr."PayTypeId"=7 then coalesce(adr."Cost",0) else 0 end "NotPaidTotal",
        case when adr."PayTypeId"=8 then coalesce(adr."Cost",0) else 0 end  "OtherTotal",
        case when adr."PayTypeId"=9 then coalesce(adr."Cost",0) else 0 end  "CardSwipeTotal",
        case when adr."PayTypeId"=10 then coalesce(adr."Cost",0) else 0 end  "CardStandAloneTotal",
        case when adr."PayTypeId"=11 then coalesce(adr."Cost",0) else 0 end "CardUPITotal",
        case when adr."PayTypeId"=12 then coalesce(adr."Cost",0) else 0 end  "CardGpayTotal",
        case when adr."PayTypeId"=13 then coalesce(adr."Cost",0) else 0 end  "CashDrawerTotal",
        case when adr."PayTypeId"=14 then coalesce(adr."Cost",0) else 0 end "CashChequeTotal",
        case when adr."PayTypeId"=15 then coalesce(adr."Cost",0) else 0 end "CashDDTotal",
        case when adr."PayTypeId"=16 then coalesce(adr."Cost",0) else 0 end  "WalletPaytmOfflineTotal",
        case when adr."PayTypeId"=17 then coalesce(adr."Cost",0) else 0 end  "WalletPhonePeOfflineTotal",
        case when adr."PayTypeId"=18 then coalesce(adr."Cost",0) else 0 end  "CashRemoteDepositTotal",
        case when adr."PayTypeId"=19 then coalesce(adr."Cost",0) else 0 end   "WalletPaytmDQRTotal"
												  
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."NotPaidTotal") "AdmissionRefNotPaidTotal",
	sum(A."OtherTotal") "AdmissionRefOtherTotal",
	sum(A."CardSwipeTotal") "AdmissionRefCardSwipeTotal",
	sum(A."CardStandAloneTotal") "AdmissionRefCardStandAloneTotal",
	sum(A."CardUPITotal") "AdmissionRefCardUPITotal",
	sum(A."CardGpayTotal") "AdmissionRefCardGpayTotal",
	sum(A."CashDrawerTotal") "AdmissionRefCashDrawerTotal",
	sum(A."CashChequeTotal") "AdmissionRefCashChequeTotal",
	sum(A."CashDDTotal") "AdmissionRefCashDDTotal",
	sum(A."WalletPaytmOfflineTotal") "AdmissionRefWalletPaytmOfflineTotal",
	sum(A."WalletPhonePeOfflineTotal") "AdmissionRefWalletPhonePeOfflineTotal",
	sum(A."CashRemoteDepositTotal") "AdmissionRefCashRemoteDepositTotal",
	sum(A."WalletPaytmDQRTotal") "AdmissionRefWalletPaytmDQRTotal",
	sum(A."CashTotal")+ sum(A."CardTotal")+ sum(A."UPITotal")+ sum(A."OnlineTotal")+ sum(A."ChequeTotal")+ sum(A."PaytmTotal")
	+sum(A."OtherTotal")+sum(A."CardSwipeTotal")+sum(A."CardStandAloneTotal")+sum(A."CardUPITotal")+sum(A."CardGpayTotal")+sum(A."CashDrawerTotal")+sum(A."CashChequeTotal")+sum(A."NotPaidTotal")+
	sum(A."CashDDTotal")+sum(A."WalletPaytmOfflineTotal")+sum(A."WalletPhonePeOfflineTotal")+sum(A."CashRemoteDepositTotal")+sum(A."WalletPaytmDQRTotal")

	"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" ,
		case when adr."PayTypeId"=7 then coalesce(adr."Cost",0) else 0 end "NotPaidTotal",
        case when adr."PayTypeId"=8 then coalesce(adr."Cost",0) else 0 end  "OtherTotal",
        case when adr."PayTypeId"=9 then coalesce(adr."Cost",0) else 0 end  "CardSwipeTotal",
        case when adr."PayTypeId"=10 then coalesce(adr."Cost",0) else 0 end  "CardStandAloneTotal",
        case when adr."PayTypeId"=11 then coalesce(adr."Cost",0) else 0 end "CardUPITotal",
        case when adr."PayTypeId"=12 then coalesce(adr."Cost",0) else 0 end  "CardGpayTotal",
        case when adr."PayTypeId"=13 then coalesce(adr."Cost",0) else 0 end  "CashDrawerTotal",
        case when adr."PayTypeId"=14 then coalesce(adr."Cost",0) else 0 end "CashChequeTotal",
        case when adr."PayTypeId"=15 then coalesce(adr."Cost",0) else 0 end "CashDDTotal",
        case when adr."PayTypeId"=16 then coalesce(adr."Cost",0) else 0 end  "WalletPaytmOfflineTotal",
        case when adr."PayTypeId"=17 then coalesce(adr."Cost",0) else 0 end  "WalletPhonePeOfflineTotal",
        case when adr."PayTypeId"=18 then coalesce(adr."Cost",0) else 0 end  "CashRemoteDepositTotal",
        case when adr."PayTypeId"=19 then coalesce(adr."Cost",0) else 0 end   "WalletPaytmDQRTotal"
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."AdmissionNotPaidTotal",0) - coalesce(B."AdmissionRefNotPaidTotal",0) as "AdmissionNotPaidTotal" ,
	coalesce(A."AdmissionOtherTotal",0) - coalesce(B."AdmissionRefOtherTotal",0) as "AdmissionOtherTotal" ,
	coalesce(A."AdmissionCardSwipeTotal",0) - coalesce(B."AdmissionRefCardSwipeTotal",0) as "AdmissionCardSwipeTotal" ,
	coalesce(A."AdmissionCardStandAloneTotal",0) - coalesce(B."AdmissionRefCardStandAloneTotal",0) as "AdmissionCardStandAloneTotal" ,
	coalesce(A."AdmissionCardUPITotal",0) - coalesce(B."AdmissionRefCardUPITotal",0) as "AdmissionCardUPITotal" ,
	coalesce(A."AdmissionCardGpayTotal",0) - coalesce(B."AdmissionRefCardGpayTotal",0) as "AdmissionCardGpayTotal" ,
	coalesce(A."AdmissionCashDrawerTotal",0) - coalesce(B."AdmissionRefCashDrawerTotal",0) as "AdmissionCashDrawerTotal" ,
	coalesce(A."AdmissionCashChequeTotal",0) - coalesce(B."AdmissionRefCashChequeTotal",0) as "AdmissionCashChequeTotal" ,
	coalesce(A."AdmissionCashDDTotal",0) - coalesce(B."AdmissionRefCashDDTotal",0) as "AdmissionCashDDTotal" ,
	coalesce(A."AdmissionWalletPaytmOfflineTotal",0) - coalesce(B."AdmissionRefWalletPaytmOfflineTotal",0) as "AdmissionWalletPaytmOfflineTotal" ,
	coalesce(A."AdmissionWalletPhonePeOfflineTotal",0) - coalesce(B."AdmissionRefWalletPhonePeOfflineTotal",0) as "AdmissionWalletPhonePeOfflineTotal" ,
	coalesce(A."AdmissionCashRemoteDepositTotal",0) - coalesce(B."AdmissionRefCashRemoteDepositTotal",0) as "AdmissionCashRemoteDepositTotal" ,
		coalesce(A."AdmissionWalletPaytmDQRTotal",0) - coalesce(B."AdmissionRefWalletPaytmDQRTotal",0) as "AdmissionWalletPaytmDQRTotal" ,
	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."NotPaidTotal") "LabNotPaidTotal",
	sum(a."OtherTotal") "LabOtherTotal",
	sum(a."CardSwipeTotal") "LabCardSwipeTotal",
	sum(a."CardStandAloneTotal") "LabCardStandAloneTotal",
	sum(a."CardUPITotal") "LabCardUPITotal",
	sum(a."CardGpayTotal") "LabCardGpayTotal",
	sum(a."CashDrawerTotal") "LabCashDrawerTotal",
	sum(a."CashChequeTotal") "LabCashChequeTotal",
	sum(a."CashDDTotal") "LabCashDDTotal",
	sum(a."WalletPaytmOfflineTotal") "LabWalletPaytmOfflineTotal",
	sum(a."WalletPhonePeOfflineTotal") "LabWalletPhonePeOfflineTotal",
	sum(a."CashRemoteDepositTotal") "LabCashRemoteDepositTotal",
	sum(a."WalletPaytmDQRTotal") "LabWalletPaytmDQRTotal",
	sum(a."Cash")+ sum(a."Card")+ sum(a."UPI")+ sum(a."Online")+ sum(a."Cheque")+ sum(a."Paytm")
	+sum(a."OtherTotal")+sum(a."CardSwipeTotal")+sum(a."CardStandAloneTotal")+sum(a."CardUPITotal")+sum(a."CardGpayTotal")+sum(a."CashDrawerTotal")+sum(a."CashChequeTotal")+sum(a."NotPaidTotal")+
	sum(a."CashDDTotal")+sum(a."WalletPaytmOfflineTotal")+sum(a."WalletPhonePeOfflineTotal")+sum(a."CashRemoteDepositTotal")+sum(a."WalletPaytmDQRTotal")

	"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",
	 case when ar."PayTypeId"=7 then coalesce(ar."OverallNetAmount",0) else 0 end "NotPaidTotal",
     case when ar."PayTypeId"=8 then coalesce(ar."OverallNetAmount",0) else 0 end  "OtherTotal",
     case when ar."PayTypeId"=9 then coalesce(ar."OverallNetAmount",0) else 0 end  "CardSwipeTotal",
     case when ar."PayTypeId"=10 then coalesce(ar."OverallNetAmount",0) else 0 end  "CardStandAloneTotal",
      case when ar."PayTypeId"=11 then coalesce(ar."OverallNetAmount",0) else 0 end "CardUPITotal",
      case when ar."PayTypeId"=12 then coalesce(ar."OverallNetAmount",0) else 0 end  "CardGpayTotal",
      case when ar."PayTypeId"=13 then coalesce(ar."OverallNetAmount",0) else 0 end  "CashDrawerTotal",
       case when ar."PayTypeId"=14 then coalesce(ar."OverallNetAmount",0) else 0 end "CashChequeTotal",
       case when ar."PayTypeId"=15 then coalesce(ar."OverallNetAmount",0) else 0 end "CashDDTotal",
       case when ar."PayTypeId"=16 then coalesce(ar."OverallNetAmount",0) else 0 end  "WalletPaytmOfflineTotal",
       case when ar."PayTypeId"=17 then coalesce(ar."OverallNetAmount",0) else 0 end  "WalletPhonePeOfflineTotal",
       case when ar."PayTypeId"=18 then coalesce(ar."OverallNetAmount",0) else 0 end  "CashRemoteDepositTotal",
       case when ar."PayTypeId"=19 then coalesce(ar."OverallNetAmount",0) else 0 end   "WalletPaytmDQRTotal"
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."NotPaidTotal") "PharmaSaleNotPaidTotal",
	sum(a."OtherTotal") "PharmaSaleOtherTotal",
	sum(a."CardSwipeTotal") "PharmaSaleCardSwipeTotal",
	sum(a."CardStandAloneTotal") "PharmaSaleCardStandAloneTotal",
	sum(a."CardUPITotal") "PharmaSaleCardUPITotal",
	sum(a."CardGpayTotal") "PharmaSaleCardGpayTotal",
	sum(a."CashDrawerTotal") "PharmaSaleCashDrawerTotal",
	sum(a."CashChequeTotal") "PharmaSaleCashChequeTotal",
	sum(a."CashDDTotal") "PharmaSaleCashDDTotal",
	sum(a."WalletPaytmOfflineTotal") "PharmaSaleWalletPaytmOfflineTotal",
	sum(a."WalletPhonePeOfflineTotal") "PharmaSaleWalletPhonePeOfflineTotal",
	sum(a."CashRemoteDepositTotal") "PharmaSaleCashRemoteDepositTotal",
	sum(a."WalletPaytmDQRTotal") "PharmaSaleWalletPaytmDQRTotal",
	 sum(a."Cash")+ sum(a."Card")+ sum(a."UPI")+ sum(a."Online")+ sum(a."Cheque")+ sum(a."Paytm")
	+sum(a."OtherTotal")+sum(a."CardSwipeTotal")+sum(a."CardStandAloneTotal")+sum(a."CardUPITotal")+sum(a."CardGpayTotal")+sum(a."CashDrawerTotal")+sum(a."CashChequeTotal")+sum(a."NotPaidTotal")+
	sum(a."CashDDTotal")+sum(a."WalletPaytmOfflineTotal")+sum(a."WalletPhonePeOfflineTotal")+sum(a."CashRemoteDepositTotal")+sum(a."WalletPaytmDQRTotal")

	"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",
	 case when ar."PayTypeId"=7 then coalesce(ar."OverallNetAmount",0) else 0 end "NotPaidTotal",
     case when ar."PayTypeId"=8 then coalesce(ar."OverallNetAmount",0) else 0 end  "OtherTotal",
     case when ar."PayTypeId"=9 then coalesce(ar."OverallNetAmount",0) else 0 end  "CardSwipeTotal",
     case when ar."PayTypeId"=10 then coalesce(ar."OverallNetAmount",0) else 0 end  "CardStandAloneTotal",
      case when ar."PayTypeId"=11 then coalesce(ar."OverallNetAmount",0) else 0 end "CardUPITotal",
      case when ar."PayTypeId"=12 then coalesce(ar."OverallNetAmount",0) else 0 end  "CardGpayTotal",
      case when ar."PayTypeId"=13 then coalesce(ar."OverallNetAmount",0) else 0 end  "CashDrawerTotal",
       case when ar."PayTypeId"=14 then coalesce(ar."OverallNetAmount",0) else 0 end "CashChequeTotal",
       case when ar."PayTypeId"=15 then coalesce(ar."OverallNetAmount",0) else 0 end "CashDDTotal",
       case when ar."PayTypeId"=16 then coalesce(ar."OverallNetAmount",0) else 0 end  "WalletPaytmOfflineTotal",
       case when ar."PayTypeId"=17 then coalesce(ar."OverallNetAmount",0) else 0 end  "WalletPhonePeOfflineTotal",
       case when ar."PayTypeId"=18 then coalesce(ar."OverallNetAmount",0) else 0 end  "CashRemoteDepositTotal",
       case when ar."PayTypeId"=19 then coalesce(ar."OverallNetAmount",0) else 0 end   "WalletPaytmDQRTotal"
		
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."NotPaidTotal") "PharmaReturnNotPaidTotal",
	sum(a."OtherTotal") "PharmaReturnOtherTotal",
	sum(a."CardSwipeTotal") "PharmaReturnCardSwipeTotal",
	sum(a."CardStandAloneTotal") "PharmaReturnCardStandAloneTotal",
	sum(a."CardUPITotal") "PharmaReturnCardUPITotal",
	sum(a."CardGpayTotal") "PharmaReturnCardGpayTotal",
	sum(a."CashDrawerTotal") "PharmaReturnCashDrawerTotal",
	sum(a."CashChequeTotal") "PharmaReturnCashChequeTotal",
	sum(a."CashDDTotal") "PharmaReturnCashDDTotal",
	sum(a."WalletPaytmOfflineTotal") "PharmaReturnWalletPaytmOfflineTotal",
	sum(a."WalletPhonePeOfflineTotal") "PharmaReturnWalletPhonePeOfflineTotal",
	sum(a."CashRemoteDepositTotal") "PharmaReturnCashRemoteDepositTotal",
	sum(a."WalletPaytmDQRTotal") "PharmaReturnWalletPaytmDQRTotal",
	 sum(a."Cash")+ sum(a."Card")+ sum(a."UPI")+ sum(a."Online")+ sum(a."Cheque")+ sum(a."Paytm")
	+sum(a."OtherTotal")+sum(a."CardSwipeTotal")+sum(a."CardStandAloneTotal")+sum(a."CardUPITotal")+sum(a."CardGpayTotal")+sum(a."CashDrawerTotal")+sum(a."CashChequeTotal")+sum(a."NotPaidTotal")+
	sum(a."CashDDTotal")+sum(a."WalletPaytmOfflineTotal")+sum(a."WalletPhonePeOfflineTotal")+sum(a."CashRemoteDepositTotal")+sum(a."WalletPaytmDQRTotal")
 
	"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",
	 case when srh."PayTypeId"=7 then coalesce(ar."OverallNetAmount",0) else 0 end "NotPaidTotal",
     case when srh."PayTypeId"=8 then coalesce(ar."OverallNetAmount",0) else 0 end  "OtherTotal",
     case when srh."PayTypeId"=9 then coalesce(ar."OverallNetAmount",0) else 0 end  "CardSwipeTotal",
     case when srh."PayTypeId"=10 then coalesce(ar."OverallNetAmount",0) else 0 end  "CardStandAloneTotal",
      case when srh."PayTypeId"=11 then coalesce(ar."OverallNetAmount",0) else 0 end "CardUPITotal",
      case when srh."PayTypeId"=12 then coalesce(ar."OverallNetAmount",0) else 0 end  "CardGpayTotal",
      case when srh."PayTypeId"=13 then coalesce(ar."OverallNetAmount",0) else 0 end  "CashDrawerTotal",
       case when srh."PayTypeId"=14 then coalesce(ar."OverallNetAmount",0) else 0 end "CashChequeTotal",
       case when srh."PayTypeId"=15 then coalesce(ar."OverallNetAmount",0) else 0 end "CashDDTotal",
       case when srh."PayTypeId"=16 then coalesce(ar."OverallNetAmount",0) else 0 end  "WalletPaytmOfflineTotal",
       case when srh."PayTypeId"=17 then coalesce(ar."OverallNetAmount",0) else 0 end  "WalletPhonePeOfflineTotal",
       case when srh."PayTypeId"=18 then coalesce(ar."OverallNetAmount",0) else 0 end  "CashRemoteDepositTotal",
       case when srh."PayTypeId"=19 then coalesce(ar."OverallNetAmount",0) else 0 end   "WalletPaytmDQRTotal"
		
		
							   
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."PharmaSaleNotPaidTotal",0) - coalesce(b."PharmaReturnNotPaidTotal",0) as  "PharmaSaleNotPaidTotal" ,
	 coalesce(a."PharmaSaleOtherTotal",0) - coalesce(b."PharmaReturnOtherTotal",0) as  "PharmaSaleOtherTotal" ,
	 coalesce(a."PharmaSaleCardSwipeTotal",0) - coalesce(b."PharmaReturnCardSwipeTotal",0) as  "PharmaSaleCardSwipeTotal" ,
	 coalesce(a."PharmaSaleCardStandAloneTotal",0) - coalesce(b."PharmaReturnCardStandAloneTotal",0) as  "PharmaSaleCardStandAloneTotal" ,
	 coalesce(a."PharmaSaleCardUPITotal",0) - coalesce(b."PharmaReturnCardUPITotal",0) as  "PharmaSaleCardUPITotal" ,
	 coalesce(a."PharmaSaleCardGpayTotal",0) - coalesce(b."PharmaReturnCardGpayTotal",0) as  "PharmaSaleCardGpayTotal" ,
	 coalesce(a."PharmaSaleCashDrawerTotal",0) - coalesce(b."PharmaReturnCashDrawerTotal",0) as  "PharmaSaleCashDrawerTotal" ,
	 coalesce(a."PharmaSaleCashDDTotal",0) - coalesce(b."PharmaReturnCashDDTotal",0) as  "PharmaSaleCashDDTotal" ,
	 coalesce(a."PharmaSaleCashChequeTotal",0) - coalesce(b."PharmaReturnCashChequeTotal",0) as  "PharmaSaleCashChequeTotal" ,
	 coalesce(a."PharmaSaleWalletPaytmOfflineTotal",0) - coalesce(b."PharmaReturnWalletPaytmOfflineTotal",0) as  "PharmaSaleWalletPaytmOfflineTotal" ,
	 coalesce(a."PharmaSaleWalletPhonePeOfflineTotal",0) - coalesce(b."PharmaReturnWalletPhonePeOfflineTotal",0) as  "PharmaSaleWalletPhonePeOfflineTotal" ,
	 coalesce(a."PharmaSaleCashRemoteDepositTotal",0) - coalesce(b."PharmaReturnCashRemoteDepositTotal",0) as  "PharmaSaleCashRemoteDepositTotal" ,
	 coalesce(a."PharmaSaleWalletPaytmDQRTotal",0) - coalesce(b."PharmaReturnWalletPaytmDQRTotal",0) as  "PharmaSaleWalletPaytmDQRTotal" ,
	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.  "RegistrationNotPaidTotal" ,pr. "RegistrationOtherTotal" ,pr. "RegistrationCardSwipeTotal" ,pr."RegistrationCardStandAloneTotal" ,
				    pr. "RegistrationCardUPITotal" ,pr."RegistrationCardGpayTotal" ,pr."RegistrationCashDrawerTotal" ,pr."RegistrationCashChequeTotal" ,
				    pr. "RegistrationCashDDTotal" ,pr."RegistrationWalletPaytmOfflineTotal" ,pr."RegistrationWalletPhonePeOfflineTotal" ,pr."RegistrationCashRemoteDepositTotal" ,
				      pr. "RegistrationWalletPaytmDQRTotal" ,
						pr."registrationamount",
						  ap."AppointmentCashTotal",ap."AppointmentCardTotal",ap."AppointmentUPITotal",ap."AppointmentOnlineTotal",ap."AppointmentChequeTotal",ap."AppointmentPaytmTotal",
						 ap."AppointmentNotPaidTotal" ,ap."AppointmentOtherTotal" ,ap."AppointmentCardSwipeTotal" ,ap."AppointmentCardStandAloneTotal" ,
				   ap."AppointmentCardUPITotal" ,ap."AppointmentCardGpayTotal" ,ap."AppointmentCashDrawerTotal" ,ap."AppointmentCashChequeTotal" ,
				    ap."AppointmentCashDDTotal" ,ap."AppointmentWalletPaytmOfflineTotal" ,ap."AppointmentWalletPhonePeOfflineTotal" ,ap."AppointmentCashRemoteDepositTotal" ,
				     ap."AppointmentWalletPaytmDQRTotal" ,
						ap."AppointmentAmount",
						  ad."AdmissionCashTotal",ad."AdmissionCardTotal",ad."AdmissionUPITotal",ad."AdmissionOnlineTotal",ad."AdmissionChequeTotal",ad."AdmissionPaytmTotal",
						 ad."AdmissionCardUPITotal" ,ad."AdmissionCardGpayTotal" ,ad."AdmissionCashDrawerTotal" ,ad."AdmissionCashChequeTotal" ,
						 ad."AdmissionNotPaidTotal" ,ad."AdmissionOtherTotal" ,ad."AdmissionCardSwipeTotal" ,ad."AdmissionCardStandAloneTotal" ,
				    ad."AdmissionCashDDTotal" ,ad."AdmissionWalletPaytmOfflineTotal" ,ad."AdmissionWalletPhonePeOfflineTotal" ,ad."AdmissionCashRemoteDepositTotal" ,
				     ad."AdmissionWalletPaytmDQRTotal" , 
						ad."AdmissionAmount",
						  lb."LabCash", lb."LabCard",lb."LabUPI", lb."LabOnline",lb."LabCheque", lb."LabPaytm",
						    lb."LabNotPaidTotal" , lb."LabOtherTotal" , lb."LabCardSwipeTotal" , lb."LabCardStandAloneTotal" ,
						 lb."LabCardUPITotal" ,lb."LabCardGpayTotal" ,lb."LabCashDrawerTotal" ,lb."LabCashChequeTotal" ,
				   lb."LabCashDDTotal" ,lb."LabWalletPaytmOfflineTotal" ,lb."LabWalletPhonePeOfflineTotal" ,lb."LabCashRemoteDepositTotal" ,
				     lb."LabWalletPaytmDQRTotal" 
						,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."PharmaSaleNotPaidTotal") over(partition by a."AccountId") 	"PharmacySaleNotPaidTotal",
sum(pa."PharmaSaleOtherTotal") over(partition by a."AccountId") 	"PharmacySaleOtherTotal",
sum(pa."PharmaSaleCardSwipeTotal") over(partition by a."AccountId") 	"PharmacySaleCardSwipeTotal",
sum(pa."PharmaSaleCardStandAloneTotal") over(partition by a."AccountId") 	"PharmacySaleCardStandAloneTotal",
sum(pa."PharmaSaleCardUPITotal") over(partition by a."AccountId") 	"PharmacySaleCardUPITotal",
sum(pa."PharmaSaleCardGpayTotal") over(partition by a."AccountId") 	"PharmacySaleCardGpayTotal",
sum(pa."PharmaSaleCashDrawerTotal") over(partition by  a."AccountId") 	"PharmacySaleCashDrawerTotal",
sum(pa."PharmaSaleCashChequeTotal") over(partition by a."AccountId") 	"PharmacySaleCashChequeTotal",
sum(pa."PharmaSaleCashDDTotal") over(partition by a."AccountId") 	"PharmacySaleCashDDTotal",
sum(pa."PharmaSaleWalletPaytmOfflineTotal") over(partition by a."AccountId") 	"PharmacySaleWalletPaytmOfflineTotal",
sum(pa."PharmaSaleWalletPhonePeOfflineTotal") over(partition by a."AccountId") 	"PharmacySaleWalletPhonePeOfflineTotal",
sum(pa."PharmaSaleCashRemoteDepositTotal") over(partition by a."AccountId") 	"PharmacySaleCashRemoteDepositTotal",
sum(pa."PharmaSaleWalletPaytmDQRTotal") over(partition by a."AccountId") 	"PharmacySaleWalletPaytmDQRTotal",
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."RegistrationNotPaidTotal",0)+coalesce(ap."AppointmentPaytmTotal",0)+coalesce(ad."AdmissionNotPaidTotal",0)+coalesce(lb."LabNotPaidTotal",0)+coalesce(sum(pa."PharmaSaleNotPaidTotal") over(partition by a."AccountId"),0)  "TotalNotPaid",
coalesce(pr."RegistrationOtherTotal",0)+coalesce(ap."AppointmentOtherTotal",0)+	coalesce(ad."AdmissionOtherTotal",0)+coalesce(lb."LabOtherTotal",0)+coalesce(sum(pa."PharmaSaleOtherTotal") over(partition by a."AccountId"),0)  "TotalOther",
coalesce(pr."RegistrationCardSwipeTotal",0)+coalesce(ap."AppointmentCardSwipeTotal",0)+	coalesce(ad."AdmissionCardSwipeTotal",0)+coalesce(lb."LabCardSwipeTotal",0)+coalesce(sum(pa."PharmaSaleCardSwipeTotal") over(partition by a."AccountId"),0)  "CardSwipeTotal",
coalesce(pr."RegistrationCardStandAloneTotal",0)+coalesce(ap."AppointmentCardStandAloneTotal",0)+coalesce(ad."AdmissionCardStandAloneTotal",0)+coalesce(lb."LabCardStandAloneTotal",0)+coalesce(sum(pa."PharmaSaleCardStandAloneTotal") over(partition by a."AccountId"),0)  "CardStandAloneTotal",
coalesce(pr."RegistrationCardUPITotal",0)+coalesce(ap."AppointmentCardUPITotal",0)+coalesce(ad."AdmissionCardUPITotal",0)+coalesce(lb."LabCardUPITotal",0)+coalesce(sum(pa."PharmaSaleCardUPITotal") over(partition by a."AccountId"),0)  "CardUPITotal",
coalesce(pr."RegistrationCardGpayTotal",0)+coalesce(ap."AppointmentCardGpayTotal",0)+coalesce(ad."AdmissionCardGpayTotal",0)+coalesce(lb."LabCardGpayTotal",0)+coalesce(sum(pa."PharmaSaleCardGpayTotal") over(partition by a."AccountId"),0)  "CardGpayTotal",
coalesce(pr."RegistrationCashDrawerTotal",0)+coalesce(ap."AppointmentCashDrawerTotal",0)+coalesce(ad."AdmissionCashDrawerTotal",0)+coalesce(lb."LabCashDrawerTotal",0)+coalesce(sum(pa."PharmaSaleCashDrawerTotal") over(partition by a."AccountId"),0)  "CashDrawerTotal",
coalesce(pr."RegistrationCashChequeTotal",0)+coalesce(ap."AppointmentCashChequeTotal",0)+coalesce(ad."AdmissionCashChequeTotal",0)+coalesce(lb."LabCashChequeTotal",0)+coalesce(sum(pa."PharmaSaleCashChequeTotal") over(partition by a."AccountId"),0) "CashChequeTotal",
coalesce(pr."RegistrationCashDDTotal",0)+coalesce(ap."AppointmentCashDDTotal",0)+coalesce(ad."AdmissionCashDDTotal",0)+coalesce(lb."LabCashDDTotal",0)+coalesce(sum(pa."PharmaSaleCashDDTotal") over(partition by a."AccountId"),0)  "CashDDTotal",
coalesce(pr."RegistrationWalletPaytmOfflineTotal",0)+coalesce(ap."AppointmentWalletPaytmOfflineTotal",0)+coalesce(ad."AdmissionWalletPaytmOfflineTotal",0)+coalesce(lb."LabWalletPaytmOfflineTotal",0)+coalesce(sum(pa."PharmaSaleWalletPaytmOfflineTotal")over(partition by a."AccountId"),0)  "WalletPaytmOfflineTotal",
coalesce(ap."AppointmentWalletPhonePeOfflineTotal",0)+coalesce(ap."AppointmentWalletPhonePeOfflineTotal",0)+coalesce(ad."AdmissionWalletPhonePeOfflineTotal",0)+coalesce(lb."LabWalletPhonePeOfflineTotal",0)+coalesce(sum(pa."PharmaSaleWalletPhonePeOfflineTotal") over(partition by a."AccountId"),0)  "WalletPhonePeOfflineTotal",
coalesce(pr."RegistrationCashRemoteDepositTotal",0)+coalesce(ap."AppointmentCashRemoteDepositTotal",0)+	coalesce(ad."AdmissionCashRemoteDepositTotal",0)+coalesce(lb."LabCashRemoteDepositTotal",0)+coalesce(sum(pa."PharmaSaleCashRemoteDepositTotal")over(partition by a."AccountId"),0)  "CashRemoteDepositTotal",
coalesce(pr."RegistrationWalletPaytmDQRTotal",0)+coalesce(ap."AppointmentWalletPaytmDQRTotal",0)+coalesce(ad."AdmissionWalletPaytmDQRTotal",0)+coalesce(lb."LabWalletPaytmDQRTotal",0)+coalesce(sum(pa."PharmaSaleWalletPaytmDQRTotal") over(partition by a."AccountId"),0)  "WalletPaytmDQRTotal",

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;


--select * from udf_emloyee_new_revenue(6776, 2, integer, date, date)
Select * from "udf_emloyee_new_revenue"( '6776', '2', null, '2023-02-10T00:00'::Date, '2023-02-10T23:59'::Date)


