-- FUNCTION: public.UDF_Provider_Charges_From_Charge_Module(integer, integer, integer, text, integer, integer)

-- DROP FUNCTION IF EXISTS public."UDF_Provider_Charges_From_Charge_Module"(integer, integer, integer, text, integer, integer);

CREATE OR REPLACE FUNCTION public."UDF_Provider_Charges_From_Charge_Module"(
	providerid integer,
	specializationid integer,
	locationid integer,
	appointmentdate text,
	chargetypesid integer,
	consultationtypeid integer)
    RETURNS TABLE("DoctorSpecializationChargeModuleDetailsId" bigint, "ChargeTypesId" integer, "ChargeName" character varying, "Charge" numeric) 
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE PARALLEL UNSAFE
    ROWS 1000

AS $BODY$
begin

IF EXISTS (
Select DSD."DoctorSpecializationChargeModuleDetailsId", DSC."ChargeTypesId", CT."ChargeName", DSD."Amount" as "Charge"
		from "DoctorSpecializationChargeModuleDetails" DSD
Join "DoctorSpecializationChargeModuleCategory" DSC on DSC."DoctorSpecializationChargeModuleCategoryId" = DSD."DoctorSpecializationChargeModuleCategoryId" and DSC."Active" is true
JOIN "ChargeTypes" CT ON CT."ChargeTypesId" = DSC."ChargeTypesId" and CT."Active" is true
JOIN "ModulesMaster" MM ON MM."ModulesMasterId" = DSC."ModulesMasterId" 
JOIN "ChargeModuleTemplate" CMT ON CMT."ChargeModuleTemplateId" = DSC."ChargeModuleTemplateId" and CMT."IsInUse" is true

where DSD."ProviderId" = providerid and DSD."SpecializationId" = specializationid and DSD."LocationId" = locationid
		 AND CMT."StartDate"::DATE <= appointmentdate::DATE and CMT."EndDate"::DATE >= appointmentdate::DATE
		 AND DSC."ChargeTypesId" = chargetypesid And DSD."ConsultationTypeId" = consultationtypeid
--END IF; 1
	) THEN  return query 
Select DSD."DoctorSpecializationChargeModuleDetailsId", DSC."ChargeTypesId", CT."ChargeName", DSD."Amount" as "Charge"
		from "DoctorSpecializationChargeModuleDetails" DSD
Join "DoctorSpecializationChargeModuleCategory" DSC on DSC."DoctorSpecializationChargeModuleCategoryId" = DSD."DoctorSpecializationChargeModuleCategoryId" and DSC."Active" is true
JOIN "ChargeTypes" CT ON CT."ChargeTypesId" = DSC."ChargeTypesId" and CT."Active" is true
JOIN "ModulesMaster" MM ON MM."ModulesMasterId" = DSC."ModulesMasterId" 
JOIN "ChargeModuleTemplate" CMT ON CMT."ChargeModuleTemplateId" = DSC."ChargeModuleTemplateId" and CMT."IsInUse" is true

where DSD."ProviderId" = providerid and DSD."SpecializationId" = specializationid and DSD."LocationId" = locationid
		 AND CMT."StartDate"::DATE <= appointmentdate::DATE and CMT."EndDate"::DATE >= appointmentdate::DATE
		 AND DSC."ChargeTypesId" = chargetypesid And DSD."ConsultationTypeId" = consultationtypeid;
	
ELSEIF EXISTS (
Select DSD."DoctorSpecializationChargeModuleDetailsId", DSC."ChargeTypesId", CT."ChargeName", DSD."Amount" as "Charge"
		from "DoctorSpecializationChargeModuleDetails" DSD
Join "DoctorSpecializationChargeModuleCategory" DSC on DSC."DoctorSpecializationChargeModuleCategoryId" = DSD."DoctorSpecializationChargeModuleCategoryId" and DSC."Active" is true
JOIN "ChargeTypes" CT ON CT."ChargeTypesId" = DSC."ChargeTypesId" and CT."Active" is true
JOIN "ModulesMaster" MM ON MM."ModulesMasterId" = DSC."ModulesMasterId" 
JOIN "ChargeModuleTemplate" CMT ON CMT."ChargeModuleTemplateId" = DSC."ChargeModuleTemplateId" and CMT."IsInUse" is true

where DSD."SpecializationId" = specializationid and DSD."LocationId" = locationid and  DSD."ProviderId" is null
		 AND CMT."StartDate"::DATE <= appointmentdate::DATE and CMT."EndDate"::DATE >= appointmentdate::DATE
		 AND DSC."ChargeTypesId" = chargetypesid And DSD."ConsultationTypeId" = consultationtypeid

) THEN  return query Select DSD."DoctorSpecializationChargeModuleDetailsId", DSC."ChargeTypesId", CT."ChargeName", DSD."Amount" as "Charge"
		from "DoctorSpecializationChargeModuleDetails" DSD
Join "DoctorSpecializationChargeModuleCategory" DSC on DSC."DoctorSpecializationChargeModuleCategoryId" = DSD."DoctorSpecializationChargeModuleCategoryId" and DSC."Active" is true
JOIN "ChargeTypes" CT ON CT."ChargeTypesId" = DSC."ChargeTypesId" and CT."Active" is true
JOIN "ModulesMaster" MM ON MM."ModulesMasterId" = DSC."ModulesMasterId" 
JOIN "ChargeModuleTemplate" CMT ON CMT."ChargeModuleTemplateId" = DSC."ChargeModuleTemplateId" and CMT."IsInUse" is true

where DSD."SpecializationId" = specializationid and DSD."LocationId" = locationid and  DSD."ProviderId" is null
		 AND CMT."StartDate"::DATE <= appointmentdate::DATE and CMT."EndDate"::DATE >= appointmentdate::DATE
		 AND DSC."ChargeTypesId" = chargetypesid And DSD."ConsultationTypeId" = consultationtypeid;
--END IF; 2
	
ELSEIF EXISTS (Select DSD."DoctorSpecializationChargeModuleDetailsId", DSC."ChargeTypesId", CT."ChargeName", DSD."Amount" as "Charge"
		from "DoctorSpecializationChargeModuleDetails" DSD
Join "DoctorSpecializationChargeModuleCategory" DSC on DSC."DoctorSpecializationChargeModuleCategoryId" = DSD."DoctorSpecializationChargeModuleCategoryId" and DSC."Active" is true
JOIN "ChargeTypes" CT ON CT."ChargeTypesId" = DSC."ChargeTypesId" and CT."Active" is true
JOIN "ModulesMaster" MM ON MM."ModulesMasterId" = DSC."ModulesMasterId" 
JOIN "ChargeModuleTemplate" CMT ON CMT."ChargeModuleTemplateId" = DSC."ChargeModuleTemplateId" and CMT."IsInUse" is true

where  DSD."LocationId" = locationid and  DSD."ProviderId" = providerid  and DSD."SpecializationId" is null
		 AND CMT."StartDate"::DATE <= appointmentdate::DATE and CMT."EndDate"::DATE >= appointmentdate::DATE
		 AND DSC."ChargeTypesId" = chargetypesid And DSD."ConsultationTypeId" = consultationtypeid

) THEN  return query Select DSD."DoctorSpecializationChargeModuleDetailsId", DSC."ChargeTypesId", CT."ChargeName", DSD."Amount" as "Charge"
		from "DoctorSpecializationChargeModuleDetails" DSD
Join "DoctorSpecializationChargeModuleCategory" DSC on DSC."DoctorSpecializationChargeModuleCategoryId" = DSD."DoctorSpecializationChargeModuleCategoryId" and DSC."Active" is true
JOIN "ChargeTypes" CT ON CT."ChargeTypesId" = DSC."ChargeTypesId" and CT."Active" is true
JOIN "ModulesMaster" MM ON MM."ModulesMasterId" = DSC."ModulesMasterId" 
JOIN "ChargeModuleTemplate" CMT ON CMT."ChargeModuleTemplateId" = DSC."ChargeModuleTemplateId" and CMT."IsInUse" is true

where  DSD."LocationId" = locationid and  DSD."ProviderId" = providerid  and DSD."SpecializationId" is null
		 AND CMT."StartDate"::DATE <= appointmentdate::DATE and CMT."EndDate"::DATE >= appointmentdate::DATE
		 AND DSC."ChargeTypesId" = chargetypesid And DSD."ConsultationTypeId" = consultationtypeid;
--END IF; 3
	
	
ELSEIF EXISTS (Select DSD."DoctorSpecializationChargeModuleDetailsId", DSC."ChargeTypesId", CT."ChargeName", DSD."Amount" as "Charge"
		from "DoctorSpecializationChargeModuleDetails" DSD
Join "DoctorSpecializationChargeModuleCategory" DSC on DSC."DoctorSpecializationChargeModuleCategoryId" = DSD."DoctorSpecializationChargeModuleCategoryId" and DSC."Active" is true
JOIN "ChargeTypes" CT ON CT."ChargeTypesId" = DSC."ChargeTypesId" and CT."Active" is true
JOIN "ModulesMaster" MM ON MM."ModulesMasterId" = DSC."ModulesMasterId" 
JOIN "ChargeModuleTemplate" CMT ON CMT."ChargeModuleTemplateId" = DSC."ChargeModuleTemplateId" and CMT."IsInUse" is true

where  DSD."LocationId" = locationid and  DSD."ProviderId" is null and DSD."SpecializationId" is null
		 AND CMT."StartDate"::DATE <= appointmentdate::DATE and CMT."EndDate"::DATE >= appointmentdate::DATE
		 AND DSC."ChargeTypesId" = chargetypesid And DSD."ConsultationTypeId" = consultationtypeid

) THEN  return query Select DSD."DoctorSpecializationChargeModuleDetailsId", DSC."ChargeTypesId", CT."ChargeName", DSD."Amount" as "Charge"
		from "DoctorSpecializationChargeModuleDetails" DSD
Join "DoctorSpecializationChargeModuleCategory" DSC on DSC."DoctorSpecializationChargeModuleCategoryId" = DSD."DoctorSpecializationChargeModuleCategoryId" and DSC."Active" is true
JOIN "ChargeTypes" CT ON CT."ChargeTypesId" = DSC."ChargeTypesId" and CT."Active" is true
JOIN "ModulesMaster" MM ON MM."ModulesMasterId" = DSC."ModulesMasterId" 
JOIN "ChargeModuleTemplate" CMT ON CMT."ChargeModuleTemplateId" = DSC."ChargeModuleTemplateId" and CMT."IsInUse" is true

where  DSD."LocationId" = locationid and  DSD."ProviderId" is null and DSD."SpecializationId" is null
		 AND CMT."StartDate"::DATE <= appointmentdate::DATE and CMT."EndDate"::DATE >= appointmentdate::DATE
		 AND DSC."ChargeTypesId" = chargetypesid And DSD."ConsultationTypeId" = consultationtypeid;
END IF; -- 1
end

$BODY$;

ALTER FUNCTION public."UDF_Provider_Charges_From_Charge_Module"(integer, integer, integer, text, integer, integer)
    OWNER TO postgres;
