| Safe Haskell | None |
|---|---|
| Language | GHC2021 |
Hledger.Data.Balancing
Description
Functions for ensuring transactions and journals are balanced.
Synopsis
- data BalancingOpts = BalancingOpts {
- ignore_assertions_ :: Bool
- infer_balancing_costs_ :: Bool
- commodity_styles_ :: Maybe (Map CommoditySymbol AmountStyle)
- txn_balancing_ :: TransactionBalancingPrecision
- infer_imbalance_ :: Bool
- imbalance_account_ :: AccountName
- account_types_ :: Map AccountName AccountType
- lotful_commodities_ :: Set CommoditySymbol
- account_lots_tags_ :: Map AccountName Text
- lenient_lots_ :: Bool
- verbose_balancing_tags_ :: Bool
- class HasBalancingOpts c where
- balancingOpts :: Lens' c BalancingOpts
- account_lots_tags :: Lens' c (Map AccountName Text)
- account_types :: Lens' c (Map AccountName AccountType)
- commodity_styles :: Lens' c (Maybe (Map CommoditySymbol AmountStyle))
- ignore_assertions :: Lens' c Bool
- imbalance_account :: Lens' c AccountName
- infer_balancing_costs :: Lens' c Bool
- infer_imbalance :: Lens' c Bool
- lenient_lots :: Lens' c Bool
- lotful_commodities :: Lens' c (Set CommoditySymbol)
- txn_balancing :: Lens' c TransactionBalancingPrecision
- verbose_balancing_tags :: Lens' c Bool
- defbalancingopts :: BalancingOpts
- isTransactionBalanced :: BalancingOpts -> Transaction -> Bool
- balanceSingleTransaction :: BalancingOpts -> Transaction -> Either String Transaction
- balanceTransactionHelper :: BalancingOpts -> Transaction -> Either String (Transaction, [(AccountName, MixedAmount)])
- transactionBalanceError :: Transaction -> [String] -> String
- transactionBalancingSum :: Transaction -> MixedAmount
- roundedPostingTagName :: TagName
- transactionRoundCalculatedAmounts :: Transaction -> Transaction
- postingRoundCalculatedAmounts :: Map CommoditySymbol AmountStyle -> Posting -> (Posting, Bool)
- transactionCheckAssertions :: BalancingOpts -> Journal -> Transaction -> Either String Transaction
- journalBalanceTransactions :: BalancingOpts -> Journal -> Either String Journal
- journalBalanceTransactionsAndDeferAssertions :: BalancingOpts -> Journal -> Either String (Journal, Maybe String)
- tests_Balancing :: TestTree
BalancingOpts
data BalancingOpts Source #
Constructors
| BalancingOpts | |
Fields
| |
Instances
class HasBalancingOpts c where Source #
Minimal complete definition
Methods
balancingOpts :: Lens' c BalancingOpts Source #
account_lots_tags :: Lens' c (Map AccountName Text) Source #
account_types :: Lens' c (Map AccountName AccountType) Source #
commodity_styles :: Lens' c (Maybe (Map CommoditySymbol AmountStyle)) Source #
ignore_assertions :: Lens' c Bool Source #
imbalance_account :: Lens' c AccountName Source #
infer_balancing_costs :: Lens' c Bool Source #
infer_imbalance :: Lens' c Bool Source #
lenient_lots :: Lens' c Bool Source #
lotful_commodities :: Lens' c (Set CommoditySymbol) Source #
txn_balancing :: Lens' c TransactionBalancingPrecision Source #
verbose_balancing_tags :: Lens' c Bool Source #
Instances
transaction balancing
isTransactionBalanced :: BalancingOpts -> Transaction -> Bool Source #
Legacy form of transactionCheckBalanced.
balanceSingleTransaction :: BalancingOpts -> Transaction -> Either String Transaction Source #
Balance this isolated transaction, ensuring that its postings (and its balanced virtual postings) sum to 0, by inferring a missing amount or conversion price(s) if needed. Or if balancing is not possible, because the amounts don't sum to 0 or because there's more than one missing amount, return an error message.
Note this is not as accurate as balanceTransactionInJournal,
which considers the whole journal when calculating balance assignments and balance assertions.
balanceTransactionHelper :: BalancingOpts -> Transaction -> Either String (Transaction, [(AccountName, MixedAmount)]) Source #
Helper used by balanceSingleTransaction and balanceTransactionWithBalanceAssignmentAndCheckAssertionsB; use one of those instead. It also returns a list of accounts and amounts that were inferred.
transactionBalanceError :: Transaction -> [String] -> String Source #
Generate a transaction balancing error message, given the transaction and one or more suberror messages.
transactionBalancingSum :: Transaction -> MixedAmount Source #
The sum of all of this transaction's postings, converted to cost as when balancing it, including any unbalanced virtual postings. For a balanced transaction this is zero (or a tolerated imbalance; see transactionCheckBalanced) plus the unbalanced virtual postings' amounts. This is the amount a sum: query tests for a transaction.
roundedPostingTagName :: TagName Source #
An internal tag added by the journal reader to postings whose amount was calculated with * or / in an amount expression, and changed by rounding (see transactionRoundCalculatedAmounts) (#2648). If the transaction doesn't balance, the error mentions the rounding. The tag is removed when the transaction balances, so it is not seen by users or queries.
transactionRoundCalculatedAmounts :: Transaction -> Transaction Source #
Round the amounts in a transaction's postings which were calculated with * or / in an amount expression, and which are exact until now (see amountexprp) (#2648). Each is rounded to the transaction's balancing precision for its commodity, ie the most decimal places of that commodity's posting amounts in the entry, as written, including any amounts written in expressions, not counting costs (see transactionCommodityStyles). As in balancing, amounts to be generated by balance assignments count too; their assigned amounts are used for this. This is done after all postings have been read, so it doesn't depend on their order. Postings whose amount is changed by this get the internal roundedPostingTagName tag, so that if the transaction doesn't balance, the error can mention the rounding. The balancer removes the tag (see balanceTransactionHelper).
postingRoundCalculatedAmounts :: Map CommoditySymbol AmountStyle -> Posting -> (Posting, Bool) Source #
Round the amounts in this posting which were calculated with * or / in an amount expression (see amountexprp) (#2648). These are the amounts with more decimal places than their precision, which is the most decimal places written in the amounts they were calculated from; other parsed amounts never have more. Each is rounded to the precision of its commodity's style in the given map, or to its own precision if that is larger or the commodity is not in the map. This uses banker's rounding, like other hledger rounding, and the rounded amount gets that precision. Also returns whether this changed any amount's quantity.
assertion validation
transactionCheckAssertions :: BalancingOpts -> Journal -> Transaction -> Either String Transaction Source #
Verify that any assertions in this transaction hold when included in the larger journal.
journal balancing
journalBalanceTransactions :: BalancingOpts -> Journal -> Either String Journal Source #
Infer any missing amounts and/or conversion costs (as needed to balance transactions and satisfy balance assignments); and check that all transactions are balanced; and (optional) check that all balance assertions pass. Or, return an error message (just the first error encountered).
Assumes journalStyleAmounts has been called, since amount styles affect transaction balancing.
This does multiple things at once because amount inferring, balance assignments, balance assertions and posting dates are interdependent.
journalBalanceTransactionsAndDeferAssertions :: BalancingOpts -> Journal -> Either String (Journal, Maybe String) Source #
Like journalBalanceTransactions, but instead of erroring at the first failed balance assertion, keep going and also return the first assertion failure's error message, if any. This lets callers (journalFinalise) run further checks, eg the lot stages, and report their errors first.