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

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

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

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

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