
CREATE OR REPLACE FUNCTION public."UDF_TEST"(
	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"

	) 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;
--END IF; 1

ELSEIF EXISTS (select PA."ProviderAvailabilityChargeTypeId", PA."ChargeTypesId", CT."ChargeName",PA."Charge"
FROM "ProviderAvailabilityChargeType" PA

JOIN "ChargeTypes" CT ON CT."ChargeTypesId" = PA."ChargeTypesId"

) 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."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"

) 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."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"

) 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."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_TEST"(integer, integer, integer, text, integer, integer)
    OWNER TO postgres;

