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

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

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

AS $BODY$
begin

IF EXISTS (select PA."ProviderAvailabilityChargeTypeId", PA."ChargeTypesId", CT."ChargeName", PA."Charge"
	FROM "ProviderAvailabilityChargeType" PA
	JOIN "ChargeTypes" CT ON CT."ChargeTypesId" = PA."ChargeTypesId"
	where PA."ProviderId" = providerid and PA."SpecializationId" = specializationid and PA."LocationId" = locationid
		 AND PA."StartDate"::DATE <= appointmentdate::DATE and PA."EndDate"::DATE >= appointmentdate::DATE
		 AND PA."ChargeTypesId" = chargetypesid And PA."ConsultationTypeId" = consultationtypeid
--END IF; 1
	) THEN  return query select PA."ProviderAvailabilityChargeTypeId", PA."ChargeTypesId", CT."ChargeName", PA."Charge"
	FROM "ProviderAvailabilityChargeType" PA
	JOIN "ChargeTypes" CT ON CT."ChargeTypesId" = PA."ChargeTypesId"
	
where PA."ProviderId" = providerid and PA."SpecializationId" = specializationId and PA."LocationId" = locationId
		 AND PA."StartDate"::DATE <= appointmentDate::DATE and PA."EndDate"::DATE >= appointmentDate::DATE
		 AND PA."ChargeTypesId" = chargeTypesId And PA."ConsultationTypeId" = consultationTypeId;
	
ELSEIF EXISTS (select PA."ProviderAvailabilityChargeTypeId", PA."ChargeTypesId", CT."ChargeName", PA."Charge"
FROM "ProviderAvailabilityChargeType" PA
JOIN "ChargeTypes" CT ON CT."ChargeTypesId" = PA."ChargeTypesId" 
where  PA."SpecializationId" = specializationId and PA."LocationId" = locationId and PA."ProviderId" is null
	 AND PA."StartDate"::DATE <= appointmentDate::DATE and PA."EndDate"::DATE >= appointmentDate::DATE
	 AND PA."ChargeTypesId" = chargeTypesId And PA."ConsultationTypeId" = consultationTypeId

) THEN  return query select PA."ProviderAvailabilityChargeTypeId", PA."ChargeTypesId", CT."ChargeName", PA."Charge"
FROM "ProviderAvailabilityChargeType" PA
JOIN "ChargeTypes" CT ON CT."ChargeTypesId" = PA."ChargeTypesId" 
where  PA."SpecializationId" = specializationId and PA."LocationId" = locationId and PA."ProviderId" is null
	 AND PA."StartDate"::DATE <= appointmentDate::DATE and PA."EndDate"::DATE >= appointmentDate::DATE
	 AND PA."ChargeTypesId" = chargeTypesId And PA."ConsultationTypeId" = consultationTypeId;
--END IF; 2
	
ELSEIF EXISTS (select PA."ProviderAvailabilityChargeTypeId", PA."ChargeTypesId", CT."ChargeName",PA."Charge"
FROM "ProviderAvailabilityChargeType" PA

JOIN "ChargeTypes" CT ON CT."ChargeTypesId" = PA."ChargeTypesId"
where PA."ProviderId" = providerid and PA."LocationId" = locationId and PA."SpecializationId" is null
	 AND PA."StartDate"::DATE <= appointmentDate::DATE and PA."EndDate"::DATE >= appointmentDate::DATE
	 AND PA."ChargeTypesId" = chargeTypesId And PA."ConsultationTypeId" = consultationTypeId

) THEN  return query select PA."ProviderAvailabilityChargeTypeId", PA."ChargeTypesId", CT."ChargeName", PA."Charge"
FROM "ProviderAvailabilityChargeType" PA
JOIN "ChargeTypes" CT ON CT."ChargeTypesId" = PA."ChargeTypesId"
where PA."ProviderId" = providerid and PA."LocationId" = locationId and PA."SpecializationId" is null
	 AND PA."StartDate"::DATE <= appointmentDate::DATE and PA."EndDate"::DATE >= appointmentDate::DATE
	 AND PA."ChargeTypesId" = chargeTypesId And PA."ConsultationTypeId" = consultationTypeId;
--END IF; 3
	
	
ELSEIF EXISTS (select PA."ProviderAvailabilityChargeTypeId", PA."ChargeTypesId", CT."ChargeName",PA."Charge"
FROM "ProviderAvailabilityChargeType" PA

JOIN "ChargeTypes" CT ON CT."ChargeTypesId" = PA."ChargeTypesId"
			   
where PA."LocationId" = locationId and PA."ProviderId" is null and PA."SpecializationId" is null
	 AND PA."StartDate"::DATE <= appointmentDate::DATE and PA."EndDate"::DATE >= appointmentDate::DATE
	 AND PA."ChargeTypesId" = chargeTypesId And PA."ConsultationTypeId" = consultationTypeId

) THEN  return query select PA."ProviderAvailabilityChargeTypeId", PA."ChargeTypesId", CT."ChargeName", PA."Charge"
FROM "ProviderAvailabilityChargeType" PA
JOIN "ChargeTypes" CT ON CT."ChargeTypesId" = PA."ChargeTypesId"
where PA."LocationId" = locationId and PA."ProviderId" is null and PA."SpecializationId" is null
	 AND PA."StartDate"::DATE <= appointmentDate::DATE and PA."EndDate"::DATE >= appointmentDate::DATE
	 AND PA."ChargeTypesId" = chargeTypesId And PA."ConsultationTypeId" = consultationTypeId;
END IF; -- 1
end

$BODY$;

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