-- 42_plpgsql_fn_interest_calc.sql
-- Banking Pattern Pack — PostgreSQL
-- Purpose: PL/pgSQL function to calculate interest.
-- Context: Retail bank example with customers, accounts, transactions, cards, loans.
-- Date: 2025-08-18
-- Notes:
--   * Safe to read; run on a development DB.
--   * Requires: CREATE privileges; some files may need superuser for extensions.
--   * Search path set to 'bank' schema for clarity.
SET search_path = bank, public;

-- Interest calculation function (toy example)
CREATE OR REPLACE FUNCTION bank.calc_monthly_interest(p_account BIGINT, p_rate NUMERIC)
RETURNS NUMERIC LANGUAGE plpgsql AS $$
DECLARE bal NUMERIC(18,2);
BEGIN
  SELECT balance INTO bal FROM bank.accounts WHERE account_id = p_account;
  RETURN round(bal * p_rate / 12, 2);
END $$;

-- SELECT bank.calc_monthly_interest(1, 0.06);
