"-- \n"
"-- FILE: \n"
"-- functions.sql \n"
"-- \n"
"-- FUNCTION: \n"
"-- Define assorted utility functions. \n"
"-- \n"
"-- HISTORY: \n"
"-- Copyright (C) 2001 Linas Vepstas \n"
"-- \n"
" \n"
" \n"
"-- utility functions to compute checkpoint balance subtotals \n"
" \n"
"CREATE FUNCTION gncSubtotalBalance (CHAR(32), TIMESTAMP WITH TIME ZONE, TIMESTAMP WITH TIME ZONE) \n"
"    RETURNS INT8 \n"
"    AS 'SELECT INT8(sum(gncSplit.amount)) \n"
"        FROM gncSplit, gncTransaction \n"
"        WHERE \n"
"        gncSplit.accountGuid = $1 AND \n"
"        gncSplit.transGuid = gncTransaction.transGuid AND \n"
"        gncTransaction.date_posted BETWEEN $2 AND $3' \n"
"    LANGUAGE 'sql'; \n"
" \n"
"CREATE FUNCTION gncSubtotalClearedBalance (CHAR(32), TIMESTAMP WITH TIME ZONE, TIMESTAMP WITH TIME ZONE) \n"
"    RETURNS INT8 \n"
"    AS 'SELECT INT8(sum(gncSplit.amount)) \n"
"        FROM gncSplit, gncTransaction \n"
"        WHERE \n"
"        gncSplit.accountGuid = $1 AND \n"
"        gncSplit.transGuid = gncTransaction.transGuid AND \n"
"        gncTransaction.date_posted BETWEEN $2 AND $3 AND \n"
"        gncSplit.reconciled <> \\'n\\'' \n"
"    LANGUAGE 'sql'; \n"
" \n"
"CREATE FUNCTION gncSubtotalReconedBalance (CHAR(32), TIMESTAMP WITH TIME ZONE, TIMESTAMP WITH TIME ZONE) \n"
"    RETURNS INT8 \n"
"    AS 'SELECT INT8(sum(gncSplit.amount)) \n"
"        FROM gncSplit, gncTransaction \n"
"        WHERE \n"
"        gncSplit.accountGuid = $1 AND \n"
"        gncSplit.transGuid = gncTransaction.transGuid AND \n"
"        gncTransaction.date_posted BETWEEN $2 AND $3 AND \n"
"        (gncSplit.reconciled = \\'y\\' OR \n"
"         gncSplit.reconciled = \\'f\\')' \n"
"    LANGUAGE 'sql'; \n"
" \n"
"-- helper functions.  These intentionally use the 'wrong' fraction.  \n"
"-- This is because value_frac * amount * price = value * amount_frac \n"
" \n"
"CREATE FUNCTION gncHelperPrVal (gncSplit) \n"
"   RETURNS INT8 \n"
"   AS 'SELECT abs($1 . value * gncCommodity.fraction) \n"
"       FROM gncSplit, gncAccount, gncCommodity \n"
"       WHERE \n"
"       $1 . accountGuid = gncAccount.accountGuid AND \n"
"       gncAccount.commodity = gncCommodity.commodity' \n"
"    LANGUAGE 'sql'; \n"
"        \n"
"CREATE FUNCTION gncHelperPrAmt (gncSplit) \n"
"   RETURNS INT8 \n"
"   AS 'SELECT abs($1 . amount * gncCommodity.fraction) \n"
"       FROM gncSplit, gncTransaction, gncCommodity \n"
"       WHERE \n"
"       $1 . transGuid = gncTransaction.transGuid AND \n"
"       gncTransaction.currency = gncCommodity.commodity' \n"
"    LANGUAGE 'sql'; \n"
"        \n"
"-- end of file"
