--
-- 0025-load-account.sql
--
-- #25 - stored procedures for account loading
--
-- ISC License
--
-- Copyright 2023-2025 Brmlab, z.s.
-- TMA <tma+hs@jikos.cz>
--
-- Permission to use, copy, modify, and/or distribute this software
-- for any purpose with or without fee is hereby granted, provided
-- that the above copyright notice and this permission notice appear
-- in all copies.
--
-- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
-- WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
-- WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
-- AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
-- CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
-- OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
-- NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
-- CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
--

-- To require fully-qualified names
SELECT pg_catalog.set_config('search_path', '', false);

DO $upgrade_block$
DECLARE
	v INTEGER;
BEGIN

IF brmbar_privileged.has_exact_schema_version(24) THEN

SELECT COUNT(1) INTO v
	FROM pg_catalog.pg_type typ
	INNER JOIN pg_catalog.pg_namespace nsp
	ON nsp.oid = typ.typnamespace
	WHERE nsp.nspname = 'brmbar_privileged'
	AND typ.typname='account_class_initialization_data_type';

IF v>0 THEN
	RAISE NOTICE 'Changing type account_class_initialization_data_type';
	DROP TYPE brmbar_privileged.account_class_initialization_data_type CASCADE;
ELSE
	RAISE NOTICE 'Creating type account_class_initialization_data_type';
END IF;

SELECT COUNT(1) INTO v
	FROM pg_catalog.pg_type typ
	INNER JOIN pg_catalog.pg_namespace nsp
	ON nsp.oid = typ.typnamespace
	WHERE nsp.nspname = 'brmbar_privileged'
	AND typ.typname='account_load_type';

IF v>0 THEN
	RAISE NOTICE 'Changing type account_load_type';
	DROP TYPE brmbar_privileged.account_load_type CASCADE;
ELSE
	RAISE NOTICE 'Creating type account_load_type';
END IF;

CREATE TYPE brmbar_privileged.account_load_type
	AS ENUM ('by_id', 'by_barcode');

CREATE TYPE brmbar_privileged.account_class_initialization_data_type
AS (
	account_id INTEGER, --public.accounts.id%TYPE,
	account_name TEXT, --public.accounts.id%TYPE,
	account_acctype public.accounts_type,
	currency_id INTEGER, --public.currencies.id%TYPE,
	currency_name TEXT
);

CREATE OR REPLACE FUNCTION public.account_class_initialization_data(
	load_by brmbar_privileged.account_load_type,
	i_id INTEGER,
	i_barcode TEXT)
RETURNS brmbar_privileged.account_class_initialization_data_type
LANGUAGE plpgsql
AS
$$
DECLARE
	rv brmbar_privileged.account_class_initialization_data_type;
BEGIN
	IF load_by = 'by_id' THEN
		SELECT a.id, a.name, a.acctype, c.id, c.name
		INTO STRICT rv.account_id, rv.account_name, rv.account_acctype, rv.currency_id, rv.currency_name 
		FROM public.accounts a JOIN public.currencies c ON a.currency = c.id
		WHERE a.id = i_id;
	ELSE -- by_barcode
		SELECT a.id, a.name, a.acctype, c.id, c.name
		INTO STRICT rv.account_id, rv.account_name, rv.account_acctype, rv.currency_id, rv.currency_name 
		FROM public.accounts a JOIN public.currencies c ON a.currency = c.id JOIN public.barcodes b ON b.account = a.id
		WHERE b.barcode = i_barcode;
	END IF;
	--
	RETURN rv;
END;
$$;



PERFORM brmbar_privileged.upgrade_schema_version_to(25);
END IF;

END;
$upgrade_block$;

-- vim: set ft=plsql :
