added default values for the start and end date parameters. they default to the start and end date of the current month if not specified.

This commit is contained in:
Peter Wood
2025-07-22 13:56:13 -04:00
parent 07b8185c35
commit 6f7412f528

View File

@@ -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;
$function$
;