hledger-lib-1.41: A library providing the core functionality of hledger

Safe Haskell | None
Language | Haskell2010

Hledger.Data.Balancing

Contents

- BalancingOpts
- transaction balancing
- journal balancing
- tests

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)
  }
- class HasBalancingOpts c where
  - balancingOpts :: Lens' c BalancingOpts
  - commodity_styles :: Lens' c (Maybe (Map CommoditySymbol AmountStyle))
  - ignore_assertions :: Lens' c Bool
  - infer_balancing_costs :: Lens' c Bool
- defbalancingopts :: BalancingOpts
- isTransactionBalanced :: BalancingOpts -> Transaction -> Bool
- balanceTransaction :: BalancingOpts -> Transaction -> Either String Transaction
- balanceTransactionHelper :: BalancingOpts -> Transaction -> Either String (Transaction, [(AccountName, MixedAmount)])
- journalBalanceTransactions :: BalancingOpts -> Journal -> Either String Journal
- tests_Balancing :: TestTree

# BalancingOpts

data BalancingOpts #

Constructors

BalancingOpts |
Fields ignore_assertions_ :: Bool should failing balance assertions be ignored ? infer_balancing_costs_ :: Bool Are we permitted to infer missing costs to balance transactions ? Distinct from InputOpts{infer_costs_}. commodity_styles_ :: Maybe (Map CommoditySymbol AmountStyle) commodity display styles

#### Instances

Instances details

Show BalancingOpts # |
Instance details Defined in Hledger.Data.Balancing Methods showsPrec :: Int -> BalancingOpts -> ShowS # show :: BalancingOpts -> String # showList :: [BalancingOpts] -> ShowS #
HasBalancingOpts BalancingOpts # |
Instance details Defined in Hledger.Data.Balancing Methods balancingOpts :: Lens' BalancingOpts BalancingOpts # commodity_styles :: Lens' BalancingOpts (Maybe (Map CommoditySymbol AmountStyle)) # ignore_assertions :: Lens' BalancingOpts Bool # infer_balancing_costs :: Lens' BalancingOpts Bool #

class HasBalancingOpts c where #

Minimal complete definition

balancingOpts

Methods

balancingOpts :: Lens' c BalancingOpts #

commodity_styles :: Lens' c (Maybe (Map CommoditySymbol AmountStyle)) #

ignore_assertions :: Lens' c Bool #

infer_balancing_costs :: Lens' c Bool #

#### Instances

Instances details

HasBalancingOpts BalancingOpts # |
Instance details Defined in Hledger.Data.Balancing Methods balancingOpts :: Lens' BalancingOpts BalancingOpts # commodity_styles :: Lens' BalancingOpts (Maybe (Map CommoditySymbol AmountStyle)) # ignore_assertions :: Lens' BalancingOpts Bool # infer_balancing_costs :: Lens' BalancingOpts Bool #
HasBalancingOpts InputOpts # |
Instance details Defined in Hledger.Read.InputOptions Methods balancingOpts :: Lens' InputOpts BalancingOpts # commodity_styles :: Lens' InputOpts (Maybe (Map CommoditySymbol AmountStyle)) # ignore_assertions :: Lens' InputOpts Bool # infer_balancing_costs :: Lens' InputOpts Bool #

defbalancingopts :: BalancingOpts #

# transaction balancing

isTransactionBalanced :: BalancingOpts -> Transaction -> Bool #

Legacy form of transactionCheckBalanced.

balanceTransaction :: BalancingOpts -> Transaction -> Either String Transaction #

Balance this 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.

Transactions with balance assignments can have more than one missing amount; to balance those you should use the more powerful journalBalanceTransactions.

The "sum to 0" test is done using commodity display precisions, if provided, so that the result agrees with the numbers users can see.

balanceTransactionHelper :: BalancingOpts -> Transaction -> Either String (Transaction, [(AccountName, MixedAmount)]) #

Helper used by balanceTransaction and balanceTransactionWithBalanceAssignmentAndCheckAssertionsB; use one of those instead. It also returns a list of accounts and amounts that were inferred.

# journal balancing

journalBalanceTransactions :: BalancingOpts -> Journal -> Either String Journal #

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.

# tests

tests_Balancing :: TestTree #