-- FUNCTION: public.widget_Chart_Appointment_Revenue(text, date, text, integer,integer,integer)

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

CREATE OR REPLACE FUNCTION public."widget_Chart_Appointment_Revenue"(
	"chartType" text,
	"fromDate" date,
	"filterType" text,
	"displayCount" integer DEFAULT NULL::integer,
	"referenceId" integer DEFAULT NULL::integer,
	"locationId" integer DEFAULT NULL::integer)
    RETURNS TABLE("Date" date, "Count" numeric) 
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE PARALLEL UNSAFE
    ROWS 1000

AS $BODY$
DECLARE
sql text := 'select "Date", "Count"::numeric from ';
BEGIN
   IF "chartType"='Bar' THEN
      sql := sql || '"widget_BarChart_Appointment_Revenue"('''||"fromDate"||''','''||"filterType"||''', '''||"displayCount"||''','''||coalesce("referenceId",0)||''','''||coalesce("locationId",0)||''')';
   
   ELSE 
     sql := sql || '"widget_PieChart_Appointment_Revenue"('''||"fromDate"||''','''||"filterType"||''','''||coalesce("referenceId",0)||''','''||coalesce("locationId",0)||''')';
    
   END IF;
   raise notice 'query %', sql;
   RETURN QUERY EXECUTE sql
   ;
END
$BODY$;

ALTER FUNCTION public."widget_Chart_Appointment_Revenue"(text, date, text, integer,integer,integer)
    OWNER TO postgres;
select * from "widget_Chart_Appointment_Revenue"('Pie','2022-09-01','day',2,null,5)