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

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

CREATE OR REPLACE FUNCTION public."widget_Chart_Pharmacy_Sale_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_Pharmacy_Sale_Revenue"('''||"fromDate"||''','''||"filterType"||''', '''||"displayCount"||''','''||coalesce("referenceId",0)||''','''||coalesce("locationId",0)||''')';
   
   ELSE 
     sql := sql || '"widget_PieChart_Pharmacy_Sale_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_Pharmacy_Sale_Revenue"(text, date, text, integer,integer,integer)
    OWNER TO postgres;
