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

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

CREATE OR REPLACE FUNCTION public."widget_Chart_Pharmacy_LowSellingProducts"(
	"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_Pharmacy_LowSellingProducts"('''||"fromDate"||''','''||"filterType"||''', '''||"displayCount"||''','''||coalesce("locationId",0)||''')';
   
   ELSE 
     sql := sql || '"widget_PieChart_Pharmacy_LowSellingProducts"('''||"fromDate"||''','''||"filterType"||''','''||coalesce("locationId",0)||''')';
    
   END IF;
   raise notice 'query %', sql;
   RETURN QUERY EXECUTE sql
   ;
END
$BODY$;

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