hledger-lib
Safe HaskellNone
LanguageGHC2021

Hledger.Data.Balancing

Description

Functions for ensuring transactions and journals are balanced.

Synopsis

BalancingOpts

data BalancingOpts Source #

Constructors

BalancingOpts 

Fields

Instances

Instances details
Eq BalancingOpts Source # 
Instance details

Defined in Hledger.Data.Balancing

Ord BalancingOpts Source # 
Instance details

Defined in Hledger.Data.Balancing

Show BalancingOpts Source # 
Instance details

Defined in Hledger.Data.Balancing

Methods

showsPrec :: Int -> BalancingOpts -> ShowS

show :: BalancingOpts -> String

showList :: [BalancingOpts] -> ShowS

HasBalancingOpts BalancingOpts Source # 
Instance details

Defined in Hledger.Data.Balancing

class HasBalancingOpts c where Source #

Minimal complete definition

balancingOpts

Instances

Instances details
HasBalancingOpts BalancingOpts Source # 
Instance details

Defined in Hledger.Data.Balancing

HasBalancingOpts InputOpts Source # 
Instance details

Defined in Hledger.Read.InputOptions

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.

tests