Annotation: transactions.get_cash_repository_balance

CREATE OR REPLACE FUNCTION transactions.get_cash_repository_balance(_cash_repository_id integer)
RETURNS money_strict2

Information: transactions.get_cash_repository_balance

Schema transactions
Function Name get_cash_repository_balance
Arguments _cash_repository_id integer
Owner postgres
Result Type money_strict2
Description

Implementation: transactions.get_cash_repository_balance

CREATE OR REPLACE FUNCTION transactions.get_cash_repository_balance(_cash_repository_id integer)
 RETURNS money_strict2
 LANGUAGE plpgsql
AS $function$
    DECLARE _local_currency_code national character varying(12) = transactions.get_default_currency_code($1);
    DECLARE _debit money_strict2;
    DECLARE _credit money_strict2;
BEGIN
    SELECT COALESCE(SUM(amount_in_currency), 0::money_strict2) INTO _debit
    FROM transactions.verified_transaction_view
    WHERE cash_repository_id=$1
    AND currency_code=_local_currency_code
    AND tran_type='Dr';

    SELECT COALESCE(SUM(amount_in_currency), 0::money_strict2) INTO _credit
    FROM transactions.verified_transaction_view
    WHERE cash_repository_id=$1
    AND currency_code=_local_currency_code
    AND tran_type='Cr';

    RETURN _debit - _credit;
END
$function$