diff --git a/postgres/functions/get_transactions_for_period.sql b/postgres/functions/get_transactions_for_period.sql index 94173c3..f41700c 100644 --- a/postgres/functions/get_transactions_for_period.sql +++ b/postgres/functions/get_transactions_for_period.sql @@ -1,22 +1,29 @@ -drop function if exists public.get_transactions_for_period; +-- DROP FUNCTION public.get_transactions_for_period(text, date, date); + +CREATE OR REPLACE FUNCTION public.get_transactions_for_period(p_account_name text, p_start date DEFAULT NULL::date, p_end date DEFAULT NULL::date) + RETURNS TABLE(bank_name text, account_type text, account_number text, account_friendly_name text, transaction_date date, transaction_description text, transaction_amount numeric, transaction_day_of_week text, running_bal numeric) + LANGUAGE plpgsql +AS $function$ +DECLARE + v_start_date date; + v_end_date date; -create -or replace function public.get_transactions_for_period (p_account_name text, p_start date, p_end date) returns table ( - bank_name text, - account_type text, - account_number text, - account_friendly_name text, - transaction_date date, - transaction_description text, - transaction_amount numeric(8, 2), - transaction_day_of_week text, - running_bal numeric -) LANGUAGE plpgsql as $$ begin + -- Set default values for start and end dates if not provided by the caller + IF p_start IS NULL THEN + v_start_date := make_date(date_part('year', now())::int, 1, 1); + ELSE + v_start_date := p_start; + END IF; -raise notice 'FUNCTION: get_transactions'; + IF p_end IS NULL THEN + v_end_date := (SELECT (date_trunc('month', NOW()) + interval '1 month - 1 day')::date); + ELSE + v_end_date := p_end; + END IF; +raise notice 'FUNCTION: get_transactions_for_period'; -- E'\n' is new line -raise notice 'INPUT: % p_start = %; % p_end = %;', E'\n', p_start, E'\n', p_end; +raise notice 'INPUT: % v_start_date = %; % v_end_date = %;', E'\n', v_start_date, E'\n', v_end_date; return query select @@ -26,12 +33,7 @@ return query from public.runbal bl where lower(bl.account_friendly_name) = trim(lower(p_account_name)) - and bl.transaction_date between p_start and p_end; + and bl.transaction_date between v_start_date and v_end_date; end; -$$; - -grant -execute on function public.get_transactions_for_period (text, date, date) to acedanger; - -grant -execute on function public.get_transactions_for_period (text, date, date) to budgetuser; \ No newline at end of file +$function$ +;