{-# LANGUAGE PackageImports #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE Rank2Types #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
module Hledger.Data.Journal (
addPriceDirective,
addTransactionModifier,
addPeriodicTransaction,
addTransaction,
journalBalanceTransactions,
journalInferMarketPricesFromTransactions,
journalApplyCommodityStyles,
commodityStylesFromAmounts,
journalCommodityStyles,
journalToCost,
journalReverse,
journalSetLastReadTime,
journalPivot,
filterJournalTransactions,
filterJournalPostings,
filterJournalAmounts,
filterTransactionAmounts,
filterTransactionPostings,
filterPostingAmount,
journalMapTransactions,
journalMapPostings,
journalMapPostingAmounts,
journalAccountNamesUsed,
journalAccountNamesImplied,
journalAccountNamesDeclared,
journalAccountNamesDeclaredOrUsed,
journalAccountNamesDeclaredOrImplied,
journalAccountNames,
journalPayeesDeclared,
journalPayeesUsed,
journalPayeesDeclaredOrUsed,
journalCommoditiesDeclared,
journalDateSpan,
journalDateSpanBothDates,
journalStartDate,
journalEndDate,
journalDescriptions,
journalFilePath,
journalFilePaths,
journalTransactionAt,
journalNextTransaction,
journalPrevTransaction,
journalPostings,
journalTransactionsSimilarTo,
journalBalanceSheetAccountQuery,
journalProfitAndLossAccountQuery,
journalRevenueAccountQuery,
journalExpenseAccountQuery,
journalAssetAccountQuery,
journalLiabilityAccountQuery,
journalEquityAccountQuery,
journalCashAccountQuery,
canonicalStyleFrom,
nulljournal,
journalCheckBalanceAssertions,
journalNumberAndTieTransactions,
journalUntieTransactions,
journalModifyTransactions,
journalApplyAliases,
samplejournal,
tests_Journal,
)
where
import Control.Applicative ((<|>))
import Control.Monad.Except (ExceptT(..), runExceptT, throwError)
import "extra" Control.Monad.Extra (whenM)
import Control.Monad.Reader as R
import Control.Monad.ST (ST, runST)
import Data.Array.ST (STArray, getElems, newListArray, writeArray)
import Data.Char (toUpper, isDigit)
import Data.Default (Default(..))
import Data.Function ((&))
import qualified Data.HashTable.Class as H (toList)
import qualified Data.HashTable.ST.Cuckoo as H
import Data.List (find, foldl', sortOn)
import Data.List.Extra (nubSort)
import qualified Data.Map.Strict as M
import Data.Maybe (catMaybes, fromJust, fromMaybe, isJust, mapMaybe, maybeToList)
#if !(MIN_VERSION_base(4,11,0))
import Data.Semigroup (Semigroup(..))
#endif
import qualified Data.Set as S
import Data.Text (Text)
import qualified Data.Text as T
import Safe (headMay, headDef, maximumMay, minimumMay)
import Data.Time.Calendar (Day, addDays, fromGregorian)
import Data.Tree (Tree, flatten)
import System.Time (ClockTime(TOD))
import Text.Printf (printf)
import Hledger.Utils
import Hledger.Data.Types
import Hledger.Data.AccountName
import Hledger.Data.Amount
import Hledger.Data.Dates
import Hledger.Data.Transaction
import Hledger.Data.TransactionModifier
import Hledger.Data.Posting
import Hledger.Query
import Data.List (sortBy)
instance Show Journal where
show :: Journal -> String
show Journal
j
| Int
debugLevel Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
3 = String -> String -> Int -> Int -> String
forall r. PrintfType r => String -> r
printf String
"Journal %s with %d transactions, %d accounts"
(Journal -> String
journalFilePath Journal
j)
([Transaction] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length ([Transaction] -> Int) -> [Transaction] -> Int
forall a b. (a -> b) -> a -> b
$ Journal -> [Transaction]
jtxns Journal
j)
([AccountName] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [AccountName]
accounts)
| Int
debugLevel Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
6 = String -> String -> Int -> Int -> ShowS
forall r. PrintfType r => String -> r
printf String
"Journal %s with %d transactions, %d accounts: %s"
(Journal -> String
journalFilePath Journal
j)
([Transaction] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length ([Transaction] -> Int) -> [Transaction] -> Int
forall a b. (a -> b) -> a -> b
$ Journal -> [Transaction]
jtxns Journal
j)
([AccountName] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [AccountName]
accounts)
([AccountName] -> String
forall a. Show a => a -> String
show [AccountName]
accounts)
| Bool
otherwise = String -> String -> Int -> Int -> String -> ShowS
forall r. PrintfType r => String -> r
printf String
"Journal %s with %d transactions, %d accounts: %s, commodity styles: %s"
(Journal -> String
journalFilePath Journal
j)
([Transaction] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length ([Transaction] -> Int) -> [Transaction] -> Int
forall a b. (a -> b) -> a -> b
$ Journal -> [Transaction]
jtxns Journal
j)
([AccountName] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [AccountName]
accounts)
([AccountName] -> String
forall a. Show a => a -> String
show [AccountName]
accounts)
(Map AccountName AmountStyle -> String
forall a. Show a => a -> String
show (Map AccountName AmountStyle -> String)
-> Map AccountName AmountStyle -> String
forall a b. (a -> b) -> a -> b
$ Journal -> Map AccountName AmountStyle
jinferredcommodities Journal
j)
where accounts :: [AccountName]
accounts = (AccountName -> Bool) -> [AccountName] -> [AccountName]
forall a. (a -> Bool) -> [a] -> [a]
filter (AccountName -> AccountName -> Bool
forall a. Eq a => a -> a -> Bool
/= AccountName
"root") ([AccountName] -> [AccountName]) -> [AccountName] -> [AccountName]
forall a b. (a -> b) -> a -> b
$ Tree AccountName -> [AccountName]
forall a. Tree a -> [a]
flatten (Tree AccountName -> [AccountName])
-> Tree AccountName -> [AccountName]
forall a b. (a -> b) -> a -> b
$ Journal -> Tree AccountName
journalAccountNameTree Journal
j
instance Semigroup Journal where
Journal
j1 <> :: Journal -> Journal -> Journal
<> Journal
j2 = Journal :: Maybe Year
-> Maybe (AccountName, AmountStyle)
-> Maybe DecimalMark
-> [AccountName]
-> [AccountAlias]
-> [TimeclockEntry]
-> [String]
-> [(AccountName, PayeeDeclarationInfo)]
-> [(AccountName, AccountDeclarationInfo)]
-> Map AccountType [AccountName]
-> Map AccountName AmountStyle
-> Map AccountName Commodity
-> Map AccountName AmountStyle
-> [PriceDirective]
-> [MarketPrice]
-> [TransactionModifier]
-> [PeriodicTransaction]
-> [Transaction]
-> AccountName
-> [(String, AccountName)]
-> ClockTime
-> Journal
Journal {
jparsedefaultyear :: Maybe Year
jparsedefaultyear = Journal -> Maybe Year
jparsedefaultyear Journal
j2
,jparsedefaultcommodity :: Maybe (AccountName, AmountStyle)
jparsedefaultcommodity = Journal -> Maybe (AccountName, AmountStyle)
jparsedefaultcommodity Journal
j2
,jparsedecimalmark :: Maybe DecimalMark
jparsedecimalmark = Journal -> Maybe DecimalMark
jparsedecimalmark Journal
j2
,jparseparentaccounts :: [AccountName]
jparseparentaccounts = Journal -> [AccountName]
jparseparentaccounts Journal
j2
,jparsealiases :: [AccountAlias]
jparsealiases = Journal -> [AccountAlias]
jparsealiases Journal
j2
,jparsetimeclockentries :: [TimeclockEntry]
jparsetimeclockentries = Journal -> [TimeclockEntry]
jparsetimeclockentries Journal
j1 [TimeclockEntry] -> [TimeclockEntry] -> [TimeclockEntry]
forall a. Semigroup a => a -> a -> a
<> Journal -> [TimeclockEntry]
jparsetimeclockentries Journal
j2
,jincludefilestack :: [String]
jincludefilestack = Journal -> [String]
jincludefilestack Journal
j2
,jdeclaredpayees :: [(AccountName, PayeeDeclarationInfo)]
jdeclaredpayees = Journal -> [(AccountName, PayeeDeclarationInfo)]
jdeclaredpayees Journal
j1 [(AccountName, PayeeDeclarationInfo)]
-> [(AccountName, PayeeDeclarationInfo)]
-> [(AccountName, PayeeDeclarationInfo)]
forall a. Semigroup a => a -> a -> a
<> Journal -> [(AccountName, PayeeDeclarationInfo)]
jdeclaredpayees Journal
j2
,jdeclaredaccounts :: [(AccountName, AccountDeclarationInfo)]
jdeclaredaccounts = Journal -> [(AccountName, AccountDeclarationInfo)]
jdeclaredaccounts Journal
j1 [(AccountName, AccountDeclarationInfo)]
-> [(AccountName, AccountDeclarationInfo)]
-> [(AccountName, AccountDeclarationInfo)]
forall a. Semigroup a => a -> a -> a
<> Journal -> [(AccountName, AccountDeclarationInfo)]
jdeclaredaccounts Journal
j2
,jdeclaredaccounttypes :: Map AccountType [AccountName]
jdeclaredaccounttypes = Journal -> Map AccountType [AccountName]
jdeclaredaccounttypes Journal
j1 Map AccountType [AccountName]
-> Map AccountType [AccountName] -> Map AccountType [AccountName]
forall a. Semigroup a => a -> a -> a
<> Journal -> Map AccountType [AccountName]
jdeclaredaccounttypes Journal
j2
,jglobalcommoditystyles :: Map AccountName AmountStyle
jglobalcommoditystyles = Journal -> Map AccountName AmountStyle
jglobalcommoditystyles Journal
j1 Map AccountName AmountStyle
-> Map AccountName AmountStyle -> Map AccountName AmountStyle
forall a. Semigroup a => a -> a -> a
<> Journal -> Map AccountName AmountStyle
jglobalcommoditystyles Journal
j2
,jcommodities :: Map AccountName Commodity
jcommodities = Journal -> Map AccountName Commodity
jcommodities Journal
j1 Map AccountName Commodity
-> Map AccountName Commodity -> Map AccountName Commodity
forall a. Semigroup a => a -> a -> a
<> Journal -> Map AccountName Commodity
jcommodities Journal
j2
,jinferredcommodities :: Map AccountName AmountStyle
jinferredcommodities = Journal -> Map AccountName AmountStyle
jinferredcommodities Journal
j1 Map AccountName AmountStyle
-> Map AccountName AmountStyle -> Map AccountName AmountStyle
forall a. Semigroup a => a -> a -> a
<> Journal -> Map AccountName AmountStyle
jinferredcommodities Journal
j2
,jpricedirectives :: [PriceDirective]
jpricedirectives = Journal -> [PriceDirective]
jpricedirectives Journal
j1 [PriceDirective] -> [PriceDirective] -> [PriceDirective]
forall a. Semigroup a => a -> a -> a
<> Journal -> [PriceDirective]
jpricedirectives Journal
j2
,jinferredmarketprices :: [MarketPrice]
jinferredmarketprices = Journal -> [MarketPrice]
jinferredmarketprices Journal
j1 [MarketPrice] -> [MarketPrice] -> [MarketPrice]
forall a. Semigroup a => a -> a -> a
<> Journal -> [MarketPrice]
jinferredmarketprices Journal
j2
,jtxnmodifiers :: [TransactionModifier]
jtxnmodifiers = Journal -> [TransactionModifier]
jtxnmodifiers Journal
j1 [TransactionModifier]
-> [TransactionModifier] -> [TransactionModifier]
forall a. Semigroup a => a -> a -> a
<> Journal -> [TransactionModifier]
jtxnmodifiers Journal
j2
,jperiodictxns :: [PeriodicTransaction]
jperiodictxns = Journal -> [PeriodicTransaction]
jperiodictxns Journal
j1 [PeriodicTransaction]
-> [PeriodicTransaction] -> [PeriodicTransaction]
forall a. Semigroup a => a -> a -> a
<> Journal -> [PeriodicTransaction]
jperiodictxns Journal
j2
,jtxns :: [Transaction]
jtxns = Journal -> [Transaction]
jtxns Journal
j1 [Transaction] -> [Transaction] -> [Transaction]
forall a. Semigroup a => a -> a -> a
<> Journal -> [Transaction]
jtxns Journal
j2
,jfinalcommentlines :: AccountName
jfinalcommentlines = Journal -> AccountName
jfinalcommentlines Journal
j2
,jfiles :: [(String, AccountName)]
jfiles = Journal -> [(String, AccountName)]
jfiles Journal
j1 [(String, AccountName)]
-> [(String, AccountName)] -> [(String, AccountName)]
forall a. Semigroup a => a -> a -> a
<> Journal -> [(String, AccountName)]
jfiles Journal
j2
,jlastreadtime :: ClockTime
jlastreadtime = ClockTime -> ClockTime -> ClockTime
forall a. Ord a => a -> a -> a
max (Journal -> ClockTime
jlastreadtime Journal
j1) (Journal -> ClockTime
jlastreadtime Journal
j2)
}
instance Default Journal where
def :: Journal
def = Journal
nulljournal
nulljournal :: Journal
nulljournal :: Journal
nulljournal = Journal :: Maybe Year
-> Maybe (AccountName, AmountStyle)
-> Maybe DecimalMark
-> [AccountName]
-> [AccountAlias]
-> [TimeclockEntry]
-> [String]
-> [(AccountName, PayeeDeclarationInfo)]
-> [(AccountName, AccountDeclarationInfo)]
-> Map AccountType [AccountName]
-> Map AccountName AmountStyle
-> Map AccountName Commodity
-> Map AccountName AmountStyle
-> [PriceDirective]
-> [MarketPrice]
-> [TransactionModifier]
-> [PeriodicTransaction]
-> [Transaction]
-> AccountName
-> [(String, AccountName)]
-> ClockTime
-> Journal
Journal {
jparsedefaultyear :: Maybe Year
jparsedefaultyear = Maybe Year
forall a. Maybe a
Nothing
,jparsedefaultcommodity :: Maybe (AccountName, AmountStyle)
jparsedefaultcommodity = Maybe (AccountName, AmountStyle)
forall a. Maybe a
Nothing
,jparsedecimalmark :: Maybe DecimalMark
jparsedecimalmark = Maybe DecimalMark
forall a. Maybe a
Nothing
,jparseparentaccounts :: [AccountName]
jparseparentaccounts = []
,jparsealiases :: [AccountAlias]
jparsealiases = []
,jparsetimeclockentries :: [TimeclockEntry]
jparsetimeclockentries = []
,jincludefilestack :: [String]
jincludefilestack = []
,jdeclaredpayees :: [(AccountName, PayeeDeclarationInfo)]
jdeclaredpayees = []
,jdeclaredaccounts :: [(AccountName, AccountDeclarationInfo)]
jdeclaredaccounts = []
,jdeclaredaccounttypes :: Map AccountType [AccountName]
jdeclaredaccounttypes = Map AccountType [AccountName]
forall k a. Map k a
M.empty
,jglobalcommoditystyles :: Map AccountName AmountStyle
jglobalcommoditystyles = Map AccountName AmountStyle
forall k a. Map k a
M.empty
,jcommodities :: Map AccountName Commodity
jcommodities = Map AccountName Commodity
forall k a. Map k a
M.empty
,jinferredcommodities :: Map AccountName AmountStyle
jinferredcommodities = Map AccountName AmountStyle
forall k a. Map k a
M.empty
,jpricedirectives :: [PriceDirective]
jpricedirectives = []
,jinferredmarketprices :: [MarketPrice]
jinferredmarketprices = []
,jtxnmodifiers :: [TransactionModifier]
jtxnmodifiers = []
,jperiodictxns :: [PeriodicTransaction]
jperiodictxns = []
,jtxns :: [Transaction]
jtxns = []
,jfinalcommentlines :: AccountName
jfinalcommentlines = AccountName
""
,jfiles :: [(String, AccountName)]
jfiles = []
,jlastreadtime :: ClockTime
jlastreadtime = Year -> Year -> ClockTime
TOD Year
0 Year
0
}
journalFilePath :: Journal -> FilePath
journalFilePath :: Journal -> String
journalFilePath = (String, AccountName) -> String
forall a b. (a, b) -> a
fst ((String, AccountName) -> String)
-> (Journal -> (String, AccountName)) -> Journal -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Journal -> (String, AccountName)
mainfile
journalFilePaths :: Journal -> [FilePath]
journalFilePaths :: Journal -> [String]
journalFilePaths = ((String, AccountName) -> String)
-> [(String, AccountName)] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map (String, AccountName) -> String
forall a b. (a, b) -> a
fst ([(String, AccountName)] -> [String])
-> (Journal -> [(String, AccountName)]) -> Journal -> [String]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Journal -> [(String, AccountName)]
jfiles
mainfile :: Journal -> (FilePath, Text)
mainfile :: Journal -> (String, AccountName)
mainfile = (String, AccountName)
-> [(String, AccountName)] -> (String, AccountName)
forall a. a -> [a] -> a
headDef (String
"", AccountName
"") ([(String, AccountName)] -> (String, AccountName))
-> (Journal -> [(String, AccountName)])
-> Journal
-> (String, AccountName)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Journal -> [(String, AccountName)]
jfiles
addTransaction :: Transaction -> Journal -> Journal
addTransaction :: Transaction -> Journal -> Journal
addTransaction Transaction
t Journal
j = Journal
j { jtxns :: [Transaction]
jtxns = Transaction
t Transaction -> [Transaction] -> [Transaction]
forall a. a -> [a] -> [a]
: Journal -> [Transaction]
jtxns Journal
j }
addTransactionModifier :: TransactionModifier -> Journal -> Journal
addTransactionModifier :: TransactionModifier -> Journal -> Journal
addTransactionModifier TransactionModifier
mt Journal
j = Journal
j { jtxnmodifiers :: [TransactionModifier]
jtxnmodifiers = TransactionModifier
mt TransactionModifier
-> [TransactionModifier] -> [TransactionModifier]
forall a. a -> [a] -> [a]
: Journal -> [TransactionModifier]
jtxnmodifiers Journal
j }
addPeriodicTransaction :: PeriodicTransaction -> Journal -> Journal
addPeriodicTransaction :: PeriodicTransaction -> Journal -> Journal
addPeriodicTransaction PeriodicTransaction
pt Journal
j = Journal
j { jperiodictxns :: [PeriodicTransaction]
jperiodictxns = PeriodicTransaction
pt PeriodicTransaction
-> [PeriodicTransaction] -> [PeriodicTransaction]
forall a. a -> [a] -> [a]
: Journal -> [PeriodicTransaction]
jperiodictxns Journal
j }
addPriceDirective :: PriceDirective -> Journal -> Journal
addPriceDirective :: PriceDirective -> Journal -> Journal
addPriceDirective PriceDirective
h Journal
j = Journal
j { jpricedirectives :: [PriceDirective]
jpricedirectives = PriceDirective
h PriceDirective -> [PriceDirective] -> [PriceDirective]
forall a. a -> [a] -> [a]
: Journal -> [PriceDirective]
jpricedirectives Journal
j }
journalTransactionAt :: Journal -> Integer -> Maybe Transaction
journalTransactionAt :: Journal -> Year -> Maybe Transaction
journalTransactionAt Journal{jtxns :: Journal -> [Transaction]
jtxns=[Transaction]
ts} Year
i =
[Transaction] -> Maybe Transaction
forall a. [a] -> Maybe a
headMay [Transaction
t | Transaction
t <- [Transaction]
ts, Transaction -> Year
tindex Transaction
t Year -> Year -> Bool
forall a. Eq a => a -> a -> Bool
== Year
i]
journalNextTransaction :: Journal -> Transaction -> Maybe Transaction
journalNextTransaction :: Journal -> Transaction -> Maybe Transaction
journalNextTransaction Journal
j Transaction
t = Journal -> Year -> Maybe Transaction
journalTransactionAt Journal
j (Transaction -> Year
tindex Transaction
t Year -> Year -> Year
forall a. Num a => a -> a -> a
+ Year
1)
journalPrevTransaction :: Journal -> Transaction -> Maybe Transaction
journalPrevTransaction :: Journal -> Transaction -> Maybe Transaction
journalPrevTransaction Journal
j Transaction
t = Journal -> Year -> Maybe Transaction
journalTransactionAt Journal
j (Transaction -> Year
tindex Transaction
t Year -> Year -> Year
forall a. Num a => a -> a -> a
- Year
1)
journalPostings :: Journal -> [Posting]
journalPostings :: Journal -> [Posting]
journalPostings = (Transaction -> [Posting]) -> [Transaction] -> [Posting]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap Transaction -> [Posting]
tpostings ([Transaction] -> [Posting])
-> (Journal -> [Transaction]) -> Journal -> [Posting]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Journal -> [Transaction]
jtxns
journalCommoditiesDeclared :: Journal -> [AccountName]
journalCommoditiesDeclared :: Journal -> [AccountName]
journalCommoditiesDeclared = [AccountName] -> [AccountName]
forall a. Ord a => [a] -> [a]
nubSort ([AccountName] -> [AccountName])
-> (Journal -> [AccountName]) -> Journal -> [AccountName]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Map AccountName Commodity -> [AccountName]
forall k a. Map k a -> [k]
M.keys (Map AccountName Commodity -> [AccountName])
-> (Journal -> Map AccountName Commodity)
-> Journal
-> [AccountName]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Journal -> Map AccountName Commodity
jcommodities
journalDescriptions :: Journal -> [Text]
journalDescriptions :: Journal -> [AccountName]
journalDescriptions = [AccountName] -> [AccountName]
forall a. Ord a => [a] -> [a]
nubSort ([AccountName] -> [AccountName])
-> (Journal -> [AccountName]) -> Journal -> [AccountName]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Transaction -> AccountName) -> [Transaction] -> [AccountName]
forall a b. (a -> b) -> [a] -> [b]
map Transaction -> AccountName
tdescription ([Transaction] -> [AccountName])
-> (Journal -> [Transaction]) -> Journal -> [AccountName]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Journal -> [Transaction]
jtxns
journalPayeesDeclared :: Journal -> [Payee]
journalPayeesDeclared :: Journal -> [AccountName]
journalPayeesDeclared = [AccountName] -> [AccountName]
forall a. Ord a => [a] -> [a]
nubSort ([AccountName] -> [AccountName])
-> (Journal -> [AccountName]) -> Journal -> [AccountName]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((AccountName, PayeeDeclarationInfo) -> AccountName)
-> [(AccountName, PayeeDeclarationInfo)] -> [AccountName]
forall a b. (a -> b) -> [a] -> [b]
map (AccountName, PayeeDeclarationInfo) -> AccountName
forall a b. (a, b) -> a
fst ([(AccountName, PayeeDeclarationInfo)] -> [AccountName])
-> (Journal -> [(AccountName, PayeeDeclarationInfo)])
-> Journal
-> [AccountName]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Journal -> [(AccountName, PayeeDeclarationInfo)]
jdeclaredpayees
journalPayeesUsed :: Journal -> [Payee]
journalPayeesUsed :: Journal -> [AccountName]
journalPayeesUsed = [AccountName] -> [AccountName]
forall a. Ord a => [a] -> [a]
nubSort ([AccountName] -> [AccountName])
-> (Journal -> [AccountName]) -> Journal -> [AccountName]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Transaction -> AccountName) -> [Transaction] -> [AccountName]
forall a b. (a -> b) -> [a] -> [b]
map Transaction -> AccountName
transactionPayee ([Transaction] -> [AccountName])
-> (Journal -> [Transaction]) -> Journal -> [AccountName]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Journal -> [Transaction]
jtxns
journalPayeesDeclaredOrUsed :: Journal -> [Payee]
journalPayeesDeclaredOrUsed :: Journal -> [AccountName]
journalPayeesDeclaredOrUsed Journal
j = [AccountName] -> [AccountName]
forall a. Ord a => [a] -> [a]
nubSort ([AccountName] -> [AccountName]) -> [AccountName] -> [AccountName]
forall a b. (a -> b) -> a -> b
$ Journal -> [AccountName]
journalPayeesDeclared Journal
j [AccountName] -> [AccountName] -> [AccountName]
forall a. [a] -> [a] -> [a]
++ Journal -> [AccountName]
journalPayeesUsed Journal
j
journalAccountNamesUsed :: Journal -> [AccountName]
journalAccountNamesUsed :: Journal -> [AccountName]
journalAccountNamesUsed = [Posting] -> [AccountName]
accountNamesFromPostings ([Posting] -> [AccountName])
-> (Journal -> [Posting]) -> Journal -> [AccountName]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Journal -> [Posting]
journalPostings
journalAccountNamesImplied :: Journal -> [AccountName]
journalAccountNamesImplied :: Journal -> [AccountName]
journalAccountNamesImplied = [AccountName] -> [AccountName]
expandAccountNames ([AccountName] -> [AccountName])
-> (Journal -> [AccountName]) -> Journal -> [AccountName]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Journal -> [AccountName]
journalAccountNamesUsed
journalAccountNamesDeclared :: Journal -> [AccountName]
journalAccountNamesDeclared :: Journal -> [AccountName]
journalAccountNamesDeclared = [AccountName] -> [AccountName]
forall a. Ord a => [a] -> [a]
nubSort ([AccountName] -> [AccountName])
-> (Journal -> [AccountName]) -> Journal -> [AccountName]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((AccountName, AccountDeclarationInfo) -> AccountName)
-> [(AccountName, AccountDeclarationInfo)] -> [AccountName]
forall a b. (a -> b) -> [a] -> [b]
map (AccountName, AccountDeclarationInfo) -> AccountName
forall a b. (a, b) -> a
fst ([(AccountName, AccountDeclarationInfo)] -> [AccountName])
-> (Journal -> [(AccountName, AccountDeclarationInfo)])
-> Journal
-> [AccountName]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Journal -> [(AccountName, AccountDeclarationInfo)]
jdeclaredaccounts
journalAccountNamesDeclaredOrUsed :: Journal -> [AccountName]
journalAccountNamesDeclaredOrUsed :: Journal -> [AccountName]
journalAccountNamesDeclaredOrUsed Journal
j = [AccountName] -> [AccountName]
forall a. Ord a => [a] -> [a]
nubSort ([AccountName] -> [AccountName]) -> [AccountName] -> [AccountName]
forall a b. (a -> b) -> a -> b
$ Journal -> [AccountName]
journalAccountNamesDeclared Journal
j [AccountName] -> [AccountName] -> [AccountName]
forall a. [a] -> [a] -> [a]
++ Journal -> [AccountName]
journalAccountNamesUsed Journal
j
journalAccountNamesDeclaredOrImplied :: Journal -> [AccountName]
journalAccountNamesDeclaredOrImplied :: Journal -> [AccountName]
journalAccountNamesDeclaredOrImplied Journal
j = [AccountName] -> [AccountName]
forall a. Ord a => [a] -> [a]
nubSort ([AccountName] -> [AccountName]) -> [AccountName] -> [AccountName]
forall a b. (a -> b) -> a -> b
$ Journal -> [AccountName]
journalAccountNamesDeclared Journal
j [AccountName] -> [AccountName] -> [AccountName]
forall a. [a] -> [a] -> [a]
++ Journal -> [AccountName]
journalAccountNamesImplied Journal
j
journalAccountNames :: Journal -> [AccountName]
journalAccountNames :: Journal -> [AccountName]
journalAccountNames = Journal -> [AccountName]
journalAccountNamesDeclaredOrImplied
journalAccountNameTree :: Journal -> Tree AccountName
journalAccountNameTree :: Journal -> Tree AccountName
journalAccountNameTree = [AccountName] -> Tree AccountName
accountNameTreeFrom ([AccountName] -> Tree AccountName)
-> (Journal -> [AccountName]) -> Journal -> Tree AccountName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Journal -> [AccountName]
journalAccountNames
journalTransactionsSimilarTo :: Journal -> Query -> Text -> Int -> [(Double,Transaction)]
journalTransactionsSimilarTo :: Journal -> Query -> AccountName -> Int -> [(Double, Transaction)]
journalTransactionsSimilarTo Journal{[Transaction]
jtxns :: [Transaction]
jtxns :: Journal -> [Transaction]
jtxns} Query
q AccountName
desc Int
n =
Int -> [(Double, Transaction)] -> [(Double, Transaction)]
forall a. Int -> [a] -> [a]
take Int
n ([(Double, Transaction)] -> [(Double, Transaction)])
-> [(Double, Transaction)] -> [(Double, Transaction)]
forall a b. (a -> b) -> a -> b
$
((Double, Transaction) -> (Double, Transaction) -> Ordering)
-> [(Double, Transaction)] -> [(Double, Transaction)]
forall a. (a -> a -> Ordering) -> [a] -> [a]
sortBy (\(Double
s1,Transaction
t1) (Double
s2,Transaction
t2) -> (Double, Day) -> (Double, Day) -> Ordering
forall a. Ord a => a -> a -> Ordering
compare (Double
s2,Transaction -> Day
tdate Transaction
t2) (Double
s1,Transaction -> Day
tdate Transaction
t1)) ([(Double, Transaction)] -> [(Double, Transaction)])
-> [(Double, Transaction)] -> [(Double, Transaction)]
forall a b. (a -> b) -> a -> b
$
((Double, Transaction) -> Bool)
-> [(Double, Transaction)] -> [(Double, Transaction)]
forall a. (a -> Bool) -> [a] -> [a]
filter ((Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
> Double
threshold)(Double -> Bool)
-> ((Double, Transaction) -> Double)
-> (Double, Transaction)
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Double, Transaction) -> Double
forall a b. (a, b) -> a
fst)
[(AccountName -> AccountName -> Double
compareDescriptions AccountName
desc (AccountName -> Double) -> AccountName -> Double
forall a b. (a -> b) -> a -> b
$ Transaction -> AccountName
tdescription Transaction
t, Transaction
t) | Transaction
t <- [Transaction]
jtxns, Query
q Query -> Transaction -> Bool
`matchesTransaction` Transaction
t]
where
threshold :: Double
threshold = Double
0
compareDescriptions :: Text -> Text -> Double
compareDescriptions :: AccountName -> AccountName -> Double
compareDescriptions AccountName
a AccountName
b =
(if AccountName
a AccountName -> AccountName -> Bool
`T.isInfixOf` AccountName
b then (Double
0.5Double -> Double -> Double
forall a. Num a => a -> a -> a
+) else Double -> Double
forall a. a -> a
id) (Double -> Double) -> Double -> Double
forall a b. (a -> b) -> a -> b
$
String -> String -> Double
compareStrings (AccountName -> String
simplify AccountName
a) (AccountName -> String
simplify AccountName
b)
where
simplify :: AccountName -> String
simplify = AccountName -> String
T.unpack (AccountName -> String)
-> (AccountName -> AccountName) -> AccountName -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (DecimalMark -> Bool) -> AccountName -> AccountName
T.filter (Bool -> Bool
not(Bool -> Bool) -> (DecimalMark -> Bool) -> DecimalMark -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
.DecimalMark -> Bool
isDigit)
compareStrings :: String -> String -> Double
compareStrings :: String -> String -> Double
compareStrings String
"" String
"" = Double
1
compareStrings [DecimalMark
_] String
"" = Double
0
compareStrings String
"" [DecimalMark
_] = Double
0
compareStrings [DecimalMark
a] [DecimalMark
b] = if DecimalMark -> DecimalMark
toUpper DecimalMark
a DecimalMark -> DecimalMark -> Bool
forall a. Eq a => a -> a -> Bool
== DecimalMark -> DecimalMark
toUpper DecimalMark
b then Double
1 else Double
0
compareStrings String
s1 String
s2 = Double
2 Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
commonpairs Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
totalpairs
where
pairs1 :: Set String
pairs1 = [String] -> Set String
forall a. Ord a => [a] -> Set a
S.fromList ([String] -> Set String) -> [String] -> Set String
forall a b. (a -> b) -> a -> b
$ String -> [String]
wordLetterPairs (String -> [String]) -> String -> [String]
forall a b. (a -> b) -> a -> b
$ ShowS
uppercase String
s1
pairs2 :: Set String
pairs2 = [String] -> Set String
forall a. Ord a => [a] -> Set a
S.fromList ([String] -> Set String) -> [String] -> Set String
forall a b. (a -> b) -> a -> b
$ String -> [String]
wordLetterPairs (String -> [String]) -> String -> [String]
forall a b. (a -> b) -> a -> b
$ ShowS
uppercase String
s2
commonpairs :: Double
commonpairs = Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Double) -> Int -> Double
forall a b. (a -> b) -> a -> b
$ Set String -> Int
forall a. Set a -> Int
S.size (Set String -> Int) -> Set String -> Int
forall a b. (a -> b) -> a -> b
$ Set String -> Set String -> Set String
forall a. Ord a => Set a -> Set a -> Set a
S.intersection Set String
pairs1 Set String
pairs2
totalpairs :: Double
totalpairs = Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Double) -> Int -> Double
forall a b. (a -> b) -> a -> b
$ Set String -> Int
forall a. Set a -> Int
S.size Set String
pairs1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Set String -> Int
forall a. Set a -> Int
S.size Set String
pairs2
wordLetterPairs :: String -> [String]
wordLetterPairs :: String -> [String]
wordLetterPairs = (String -> [String]) -> [String] -> [String]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap String -> [String]
letterPairs ([String] -> [String])
-> (String -> [String]) -> String -> [String]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> [String]
words
letterPairs :: String -> [String]
letterPairs :: String -> [String]
letterPairs (DecimalMark
a:DecimalMark
b:String
rest) = [DecimalMark
a,DecimalMark
b] String -> [String] -> [String]
forall a. a -> [a] -> [a]
: String -> [String]
letterPairs (DecimalMark
bDecimalMark -> ShowS
forall a. a -> [a] -> [a]
:String
rest)
letterPairs String
_ = []
journalAccountTypeQuery :: [AccountType] -> Regexp -> Journal -> Query
journalAccountTypeQuery :: [AccountType] -> Regexp -> Journal -> Query
journalAccountTypeQuery [AccountType]
atypes Regexp
fallbackregex Journal{Map AccountType [AccountName]
jdeclaredaccounttypes :: Map AccountType [AccountName]
jdeclaredaccounttypes :: Journal -> Map AccountType [AccountName]
jdeclaredaccounttypes} =
let
[AccountName]
declaredacctsoftype :: [AccountName] =
[[AccountName]] -> [AccountName]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat ([[AccountName]] -> [AccountName])
-> [[AccountName]] -> [AccountName]
forall a b. (a -> b) -> a -> b
$ (AccountType -> Maybe [AccountName])
-> [AccountType] -> [[AccountName]]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe (AccountType -> Map AccountType [AccountName] -> Maybe [AccountName]
forall k a. Ord k => k -> Map k a -> Maybe a
`M.lookup` Map AccountType [AccountName]
jdeclaredaccounttypes) [AccountType]
atypes
in case [AccountName]
declaredacctsoftype of
[] -> Regexp -> Query
Acct Regexp
fallbackregex
[AccountName]
as -> [Query] -> Query
And ([Query] -> Query) -> [Query] -> Query
forall a b. (a -> b) -> a -> b
$ [ [Query] -> Query
Or [Query]
acctnameRegexes ]
[Query] -> [Query] -> [Query]
forall a. [a] -> [a] -> [a]
++ if [Query] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Query]
differentlyTypedRegexes then [] else [ Query -> Query
Not (Query -> Query) -> Query -> Query
forall a b. (a -> b) -> a -> b
$ [Query] -> Query
Or [Query]
differentlyTypedRegexes ]
where
acctnameRegexes :: [Query]
acctnameRegexes = (AccountName -> Query) -> [AccountName] -> [Query]
forall a b. (a -> b) -> [a] -> [b]
map (Regexp -> Query
Acct (Regexp -> Query)
-> (AccountName -> Regexp) -> AccountName -> Query
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AccountName -> Regexp
accountNameToAccountRegex) [AccountName]
as
differentlyTypedRegexes :: [Query]
differentlyTypedRegexes = (AccountName -> Query) -> [AccountName] -> [Query]
forall a b. (a -> b) -> [a] -> [b]
map (Regexp -> Query
Acct (Regexp -> Query)
-> (AccountName -> Regexp) -> AccountName -> Query
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AccountName -> Regexp
accountNameToAccountRegex) [AccountName]
differentlytypedsubs
differentlytypedsubs :: [AccountName]
differentlytypedsubs = [[AccountName]] -> [AccountName]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat
[[AccountName]
subs | (AccountType
t,[AccountName]
bs) <- Map AccountType [AccountName] -> [(AccountType, [AccountName])]
forall k a. Map k a -> [(k, a)]
M.toList Map AccountType [AccountName]
jdeclaredaccounttypes
, Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ AccountType
t AccountType -> [AccountType] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [AccountType]
atypes
, let subs :: [AccountName]
subs = [AccountName
b | AccountName
b <- [AccountName]
bs, (AccountName -> Bool) -> [AccountName] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (AccountName -> AccountName -> Bool
`isAccountNamePrefixOf` AccountName
b) [AccountName]
as]
]
journalAssetAccountQuery :: Journal -> Query
journalAssetAccountQuery :: Journal -> Query
journalAssetAccountQuery Journal
j =
[Query] -> Query
Or [
[AccountType] -> Regexp -> Journal -> Query
journalAccountTypeQuery [AccountType
Asset] (AccountName -> Regexp
toRegexCI' AccountName
"^assets?(:|$)") Journal
j
,Journal -> Query
journalCashAccountOnlyQuery Journal
j
]
journalAssetNonCashAccountQuery :: Journal -> Query
journalAssetNonCashAccountQuery :: Journal -> Query
journalAssetNonCashAccountQuery Journal
j =
[AccountType] -> Regexp -> Journal -> Query
journalAccountTypeQuery [AccountType
Asset] (AccountName -> Regexp
toRegexCI' AccountName
"^assets?(:|$)") Journal
j
journalCashAccountQuery :: Journal -> Query
journalCashAccountQuery :: Journal -> Query
journalCashAccountQuery Journal
j =
case AccountType -> Map AccountType [AccountName] -> Maybe [AccountName]
forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup AccountType
Cash (Journal -> Map AccountType [AccountName]
jdeclaredaccounttypes Journal
j) of
Just [AccountName]
_ -> Journal -> Query
journalCashAccountOnlyQuery Journal
j
Maybe [AccountName]
Nothing ->
[Query] -> Query
And [ Journal -> Query
journalAssetNonCashAccountQuery Journal
j, Query -> Query
Not (Query -> Query) -> (Regexp -> Query) -> Regexp -> Query
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Regexp -> Query
Acct (Regexp -> Query) -> Regexp -> Query
forall a b. (a -> b) -> a -> b
$ AccountName -> Regexp
toRegexCI' AccountName
"(investment|receivable|:A/R|:fixed)" ]
journalCashAccountOnlyQuery :: Journal -> Query
journalCashAccountOnlyQuery :: Journal -> Query
journalCashAccountOnlyQuery Journal
j =
case AccountType -> Map AccountType [AccountName] -> Maybe [AccountName]
forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup AccountType
Cash (Journal -> Map AccountType [AccountName]
jdeclaredaccounttypes Journal
j) of
Just [AccountName]
_ ->
[AccountType] -> Regexp -> Journal -> Query
journalAccountTypeQuery [AccountType
Cash] Regexp
forall a. a
notused Journal
j
where notused :: a
notused = String -> a
forall a. String -> a
error' String
"journalCashAccountOnlyQuery: this should not have happened!"
Maybe [AccountName]
Nothing -> Query
None
journalLiabilityAccountQuery :: Journal -> Query
journalLiabilityAccountQuery :: Journal -> Query
journalLiabilityAccountQuery = [AccountType] -> Regexp -> Journal -> Query
journalAccountTypeQuery [AccountType
Liability] (AccountName -> Regexp
toRegexCI' AccountName
"^(debts?|liabilit(y|ies))(:|$)")
journalEquityAccountQuery :: Journal -> Query
journalEquityAccountQuery :: Journal -> Query
journalEquityAccountQuery = [AccountType] -> Regexp -> Journal -> Query
journalAccountTypeQuery [AccountType
Equity] (AccountName -> Regexp
toRegexCI' AccountName
"^equity(:|$)")
journalRevenueAccountQuery :: Journal -> Query
journalRevenueAccountQuery :: Journal -> Query
journalRevenueAccountQuery = [AccountType] -> Regexp -> Journal -> Query
journalAccountTypeQuery [AccountType
Revenue] (AccountName -> Regexp
toRegexCI' AccountName
"^(income|revenue)s?(:|$)")
journalExpenseAccountQuery :: Journal -> Query
journalExpenseAccountQuery :: Journal -> Query
journalExpenseAccountQuery = [AccountType] -> Regexp -> Journal -> Query
journalAccountTypeQuery [AccountType
Expense] (AccountName -> Regexp
toRegexCI' AccountName
"^expenses?(:|$)")
journalBalanceSheetAccountQuery :: Journal -> Query
journalBalanceSheetAccountQuery :: Journal -> Query
journalBalanceSheetAccountQuery Journal
j = [Query] -> Query
Or [Journal -> Query
journalAssetAccountQuery Journal
j
,Journal -> Query
journalLiabilityAccountQuery Journal
j
,Journal -> Query
journalEquityAccountQuery Journal
j
]
journalProfitAndLossAccountQuery :: Journal -> Query
journalProfitAndLossAccountQuery :: Journal -> Query
journalProfitAndLossAccountQuery Journal
j = [Query] -> Query
Or [Journal -> Query
journalRevenueAccountQuery Journal
j
,Journal -> Query
journalExpenseAccountQuery Journal
j
]
filterJournalTransactions :: Query -> Journal -> Journal
filterJournalTransactions :: Query -> Journal -> Journal
filterJournalTransactions Query
q j :: Journal
j@Journal{jtxns :: Journal -> [Transaction]
jtxns=[Transaction]
ts} = Journal
j{jtxns :: [Transaction]
jtxns=(Transaction -> Bool) -> [Transaction] -> [Transaction]
forall a. (a -> Bool) -> [a] -> [a]
filter (Query
q Query -> Transaction -> Bool
`matchesTransaction`) [Transaction]
ts}
filterJournalPostings :: Query -> Journal -> Journal
filterJournalPostings :: Query -> Journal -> Journal
filterJournalPostings Query
q j :: Journal
j@Journal{jtxns :: Journal -> [Transaction]
jtxns=[Transaction]
ts} = Journal
j{jtxns :: [Transaction]
jtxns=(Transaction -> Transaction) -> [Transaction] -> [Transaction]
forall a b. (a -> b) -> [a] -> [b]
map (Query -> Transaction -> Transaction
filterTransactionPostings Query
q) [Transaction]
ts}
filterJournalAmounts :: Query -> Journal -> Journal
filterJournalAmounts :: Query -> Journal -> Journal
filterJournalAmounts Query
q j :: Journal
j@Journal{jtxns :: Journal -> [Transaction]
jtxns=[Transaction]
ts} = Journal
j{jtxns :: [Transaction]
jtxns=(Transaction -> Transaction) -> [Transaction] -> [Transaction]
forall a b. (a -> b) -> [a] -> [b]
map (Query -> Transaction -> Transaction
filterTransactionAmounts Query
q) [Transaction]
ts}
filterTransactionAmounts :: Query -> Transaction -> Transaction
filterTransactionAmounts :: Query -> Transaction -> Transaction
filterTransactionAmounts Query
q t :: Transaction
t@Transaction{tpostings :: Transaction -> [Posting]
tpostings=[Posting]
ps} = Transaction
t{tpostings :: [Posting]
tpostings=(Posting -> Posting) -> [Posting] -> [Posting]
forall a b. (a -> b) -> [a] -> [b]
map (Query -> Posting -> Posting
filterPostingAmount Query
q) [Posting]
ps}
filterPostingAmount :: Query -> Posting -> Posting
filterPostingAmount :: Query -> Posting -> Posting
filterPostingAmount Query
q p :: Posting
p@Posting{pamount :: Posting -> MixedAmount
pamount=Mixed [Amount]
as} = Posting
p{pamount :: MixedAmount
pamount=[Amount] -> MixedAmount
Mixed ([Amount] -> MixedAmount) -> [Amount] -> MixedAmount
forall a b. (a -> b) -> a -> b
$ (Amount -> Bool) -> [Amount] -> [Amount]
forall a. (a -> Bool) -> [a] -> [a]
filter (Query
q Query -> Amount -> Bool
`matchesAmount`) [Amount]
as}
filterTransactionPostings :: Query -> Transaction -> Transaction
filterTransactionPostings :: Query -> Transaction -> Transaction
filterTransactionPostings Query
q t :: Transaction
t@Transaction{tpostings :: Transaction -> [Posting]
tpostings=[Posting]
ps} = Transaction
t{tpostings :: [Posting]
tpostings=(Posting -> Bool) -> [Posting] -> [Posting]
forall a. (a -> Bool) -> [a] -> [a]
filter (Query
q Query -> Posting -> Bool
`matchesPosting`) [Posting]
ps}
journalMapTransactions :: (Transaction -> Transaction) -> Journal -> Journal
journalMapTransactions :: (Transaction -> Transaction) -> Journal -> Journal
journalMapTransactions Transaction -> Transaction
f j :: Journal
j@Journal{jtxns :: Journal -> [Transaction]
jtxns=[Transaction]
ts} = Journal
j{jtxns :: [Transaction]
jtxns=(Transaction -> Transaction) -> [Transaction] -> [Transaction]
forall a b. (a -> b) -> [a] -> [b]
map Transaction -> Transaction
f [Transaction]
ts}
journalMapPostings :: (Posting -> Posting) -> Journal -> Journal
journalMapPostings :: (Posting -> Posting) -> Journal -> Journal
journalMapPostings Posting -> Posting
f j :: Journal
j@Journal{jtxns :: Journal -> [Transaction]
jtxns=[Transaction]
ts} = Journal
j{jtxns :: [Transaction]
jtxns=(Transaction -> Transaction) -> [Transaction] -> [Transaction]
forall a b. (a -> b) -> [a] -> [b]
map ((Posting -> Posting) -> Transaction -> Transaction
transactionMapPostings Posting -> Posting
f) [Transaction]
ts}
journalMapPostingAmounts :: (Amount -> Amount) -> Journal -> Journal
journalMapPostingAmounts :: (Amount -> Amount) -> Journal -> Journal
journalMapPostingAmounts Amount -> Amount
f = (Posting -> Posting) -> Journal -> Journal
journalMapPostings ((MixedAmount -> MixedAmount) -> Posting -> Posting
postingTransformAmount ((Amount -> Amount) -> MixedAmount -> MixedAmount
mapMixedAmount Amount -> Amount
f))
journalReverse :: Journal -> Journal
journalReverse :: Journal -> Journal
journalReverse Journal
j =
Journal
j {jfiles :: [(String, AccountName)]
jfiles = [(String, AccountName)] -> [(String, AccountName)]
forall a. [a] -> [a]
reverse ([(String, AccountName)] -> [(String, AccountName)])
-> [(String, AccountName)] -> [(String, AccountName)]
forall a b. (a -> b) -> a -> b
$ Journal -> [(String, AccountName)]
jfiles Journal
j
,jdeclaredaccounts :: [(AccountName, AccountDeclarationInfo)]
jdeclaredaccounts = [(AccountName, AccountDeclarationInfo)]
-> [(AccountName, AccountDeclarationInfo)]
forall a. [a] -> [a]
reverse ([(AccountName, AccountDeclarationInfo)]
-> [(AccountName, AccountDeclarationInfo)])
-> [(AccountName, AccountDeclarationInfo)]
-> [(AccountName, AccountDeclarationInfo)]
forall a b. (a -> b) -> a -> b
$ Journal -> [(AccountName, AccountDeclarationInfo)]
jdeclaredaccounts Journal
j
,jtxns :: [Transaction]
jtxns = [Transaction] -> [Transaction]
forall a. [a] -> [a]
reverse ([Transaction] -> [Transaction]) -> [Transaction] -> [Transaction]
forall a b. (a -> b) -> a -> b
$ Journal -> [Transaction]
jtxns Journal
j
,jtxnmodifiers :: [TransactionModifier]
jtxnmodifiers = [TransactionModifier] -> [TransactionModifier]
forall a. [a] -> [a]
reverse ([TransactionModifier] -> [TransactionModifier])
-> [TransactionModifier] -> [TransactionModifier]
forall a b. (a -> b) -> a -> b
$ Journal -> [TransactionModifier]
jtxnmodifiers Journal
j
,jperiodictxns :: [PeriodicTransaction]
jperiodictxns = [PeriodicTransaction] -> [PeriodicTransaction]
forall a. [a] -> [a]
reverse ([PeriodicTransaction] -> [PeriodicTransaction])
-> [PeriodicTransaction] -> [PeriodicTransaction]
forall a b. (a -> b) -> a -> b
$ Journal -> [PeriodicTransaction]
jperiodictxns Journal
j
,jpricedirectives :: [PriceDirective]
jpricedirectives = [PriceDirective] -> [PriceDirective]
forall a. [a] -> [a]
reverse ([PriceDirective] -> [PriceDirective])
-> [PriceDirective] -> [PriceDirective]
forall a b. (a -> b) -> a -> b
$ Journal -> [PriceDirective]
jpricedirectives Journal
j
}
journalSetLastReadTime :: ClockTime -> Journal -> Journal
journalSetLastReadTime :: ClockTime -> Journal -> Journal
journalSetLastReadTime ClockTime
t Journal
j = Journal
j{ jlastreadtime :: ClockTime
jlastreadtime = ClockTime
t }
journalNumberAndTieTransactions :: Journal -> Journal
journalNumberAndTieTransactions = Journal -> Journal
journalTieTransactions (Journal -> Journal) -> (Journal -> Journal) -> Journal -> Journal
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Journal -> Journal
journalNumberTransactions
journalNumberTransactions :: Journal -> Journal
journalNumberTransactions :: Journal -> Journal
journalNumberTransactions j :: Journal
j@Journal{jtxns :: Journal -> [Transaction]
jtxns=[Transaction]
ts} = Journal
j{jtxns :: [Transaction]
jtxns=((Year, Transaction) -> Transaction)
-> [(Year, Transaction)] -> [Transaction]
forall a b. (a -> b) -> [a] -> [b]
map (\(Year
i,Transaction
t) -> Transaction
t{tindex :: Year
tindex=Year
i}) ([(Year, Transaction)] -> [Transaction])
-> [(Year, Transaction)] -> [Transaction]
forall a b. (a -> b) -> a -> b
$ [Year] -> [Transaction] -> [(Year, Transaction)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Year
1..] [Transaction]
ts}
journalTieTransactions :: Journal -> Journal
journalTieTransactions :: Journal -> Journal
journalTieTransactions j :: Journal
j@Journal{jtxns :: Journal -> [Transaction]
jtxns=[Transaction]
ts} = Journal
j{jtxns :: [Transaction]
jtxns=(Transaction -> Transaction) -> [Transaction] -> [Transaction]
forall a b. (a -> b) -> [a] -> [b]
map Transaction -> Transaction
txnTieKnot [Transaction]
ts}
journalUntieTransactions :: Transaction -> Transaction
journalUntieTransactions :: Transaction -> Transaction
journalUntieTransactions t :: Transaction
t@Transaction{tpostings :: Transaction -> [Posting]
tpostings=[Posting]
ps} = Transaction
t{tpostings :: [Posting]
tpostings=(Posting -> Posting) -> [Posting] -> [Posting]
forall a b. (a -> b) -> [a] -> [b]
map (\Posting
p -> Posting
p{ptransaction :: Maybe Transaction
ptransaction=Maybe Transaction
forall a. Maybe a
Nothing}) [Posting]
ps}
journalModifyTransactions :: Day -> Journal -> Either String Journal
journalModifyTransactions :: Day -> Journal -> Either String Journal
journalModifyTransactions Day
d Journal
j =
case Day
-> [TransactionModifier]
-> [Transaction]
-> Either String [Transaction]
modifyTransactions Day
d (Journal -> [TransactionModifier]
jtxnmodifiers Journal
j) (Journal -> [Transaction]
jtxns Journal
j) of
Right [Transaction]
ts -> Journal -> Either String Journal
forall a b. b -> Either a b
Right Journal
j{jtxns :: [Transaction]
jtxns=[Transaction]
ts}
Left String
err -> String -> Either String Journal
forall a b. a -> Either a b
Left String
err
journalCheckBalanceAssertions :: Journal -> Maybe String
journalCheckBalanceAssertions :: Journal -> Maybe String
journalCheckBalanceAssertions = (String -> Maybe String)
-> (Journal -> Maybe String)
-> Either String Journal
-> Maybe String
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either String -> Maybe String
forall a. a -> Maybe a
Just (Maybe String -> Journal -> Maybe String
forall a b. a -> b -> a
const Maybe String
forall a. Maybe a
Nothing) (Either String Journal -> Maybe String)
-> (Journal -> Either String Journal) -> Journal -> Maybe String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool -> Journal -> Either String Journal
journalBalanceTransactions Bool
True
type Balancing s = ReaderT (BalancingState s) (ExceptT String (ST s))
data BalancingState s = BalancingState {
BalancingState s -> Maybe (Map AccountName AmountStyle)
bsStyles :: Maybe (M.Map CommoditySymbol AmountStyle)
,BalancingState s -> Set AccountName
bsUnassignable :: S.Set AccountName
,BalancingState s -> Bool
bsAssrt :: Bool
,BalancingState s -> HashTable s AccountName MixedAmount
bsBalances :: H.HashTable s AccountName MixedAmount
,BalancingState s -> STArray s Year Transaction
bsTransactions :: STArray s Integer Transaction
}
withRunningBalance :: (BalancingState s -> ST s a) -> Balancing s a
withRunningBalance :: (BalancingState s -> ST s a) -> Balancing s a
withRunningBalance BalancingState s -> ST s a
f = ReaderT
(BalancingState s) (ExceptT String (ST s)) (BalancingState s)
forall r (m :: * -> *). MonadReader r m => m r
ask ReaderT
(BalancingState s) (ExceptT String (ST s)) (BalancingState s)
-> (BalancingState s -> Balancing s a) -> Balancing s a
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ExceptT String (ST s) a -> Balancing s a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (ExceptT String (ST s) a -> Balancing s a)
-> (BalancingState s -> ExceptT String (ST s) a)
-> BalancingState s
-> Balancing s a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ST s a -> ExceptT String (ST s) a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (ST s a -> ExceptT String (ST s) a)
-> (BalancingState s -> ST s a)
-> BalancingState s
-> ExceptT String (ST s) a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. BalancingState s -> ST s a
f
getRunningBalanceB :: AccountName -> Balancing s MixedAmount
getRunningBalanceB :: AccountName -> Balancing s MixedAmount
getRunningBalanceB AccountName
acc = (BalancingState s -> ST s MixedAmount) -> Balancing s MixedAmount
forall s a. (BalancingState s -> ST s a) -> Balancing s a
withRunningBalance ((BalancingState s -> ST s MixedAmount) -> Balancing s MixedAmount)
-> (BalancingState s -> ST s MixedAmount)
-> Balancing s MixedAmount
forall a b. (a -> b) -> a -> b
$ \BalancingState{HashTable s AccountName MixedAmount
bsBalances :: HashTable s AccountName MixedAmount
bsBalances :: forall s. BalancingState s -> HashTable s AccountName MixedAmount
bsBalances} -> do
MixedAmount -> Maybe MixedAmount -> MixedAmount
forall a. a -> Maybe a -> a
fromMaybe MixedAmount
0 (Maybe MixedAmount -> MixedAmount)
-> ST s (Maybe MixedAmount) -> ST s MixedAmount
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> HashTable s AccountName MixedAmount
-> AccountName -> ST s (Maybe MixedAmount)
forall k s v.
(Eq k, Hashable k) =>
HashTable s k v -> k -> ST s (Maybe v)
H.lookup HashTable s AccountName MixedAmount
bsBalances AccountName
acc
addToRunningBalanceB :: AccountName -> MixedAmount -> Balancing s MixedAmount
addToRunningBalanceB :: AccountName -> MixedAmount -> Balancing s MixedAmount
addToRunningBalanceB AccountName
acc MixedAmount
amt = (BalancingState s -> ST s MixedAmount) -> Balancing s MixedAmount
forall s a. (BalancingState s -> ST s a) -> Balancing s a
withRunningBalance ((BalancingState s -> ST s MixedAmount) -> Balancing s MixedAmount)
-> (BalancingState s -> ST s MixedAmount)
-> Balancing s MixedAmount
forall a b. (a -> b) -> a -> b
$ \BalancingState{HashTable s AccountName MixedAmount
bsBalances :: HashTable s AccountName MixedAmount
bsBalances :: forall s. BalancingState s -> HashTable s AccountName MixedAmount
bsBalances} -> do
MixedAmount
old <- MixedAmount -> Maybe MixedAmount -> MixedAmount
forall a. a -> Maybe a -> a
fromMaybe MixedAmount
0 (Maybe MixedAmount -> MixedAmount)
-> ST s (Maybe MixedAmount) -> ST s MixedAmount
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> HashTable s AccountName MixedAmount
-> AccountName -> ST s (Maybe MixedAmount)
forall k s v.
(Eq k, Hashable k) =>
HashTable s k v -> k -> ST s (Maybe v)
H.lookup HashTable s AccountName MixedAmount
bsBalances AccountName
acc
let new :: MixedAmount
new = MixedAmount
old MixedAmount -> MixedAmount -> MixedAmount
forall a. Num a => a -> a -> a
+ MixedAmount
amt
HashTable s AccountName MixedAmount
-> AccountName -> MixedAmount -> ST s ()
forall k s v.
(Eq k, Hashable k) =>
HashTable s k v -> k -> v -> ST s ()
H.insert HashTable s AccountName MixedAmount
bsBalances AccountName
acc MixedAmount
new
MixedAmount -> ST s MixedAmount
forall (m :: * -> *) a. Monad m => a -> m a
return MixedAmount
new
setRunningBalanceB :: AccountName -> MixedAmount -> Balancing s MixedAmount
setRunningBalanceB :: AccountName -> MixedAmount -> Balancing s MixedAmount
setRunningBalanceB AccountName
acc MixedAmount
amt = (BalancingState s -> ST s MixedAmount) -> Balancing s MixedAmount
forall s a. (BalancingState s -> ST s a) -> Balancing s a
withRunningBalance ((BalancingState s -> ST s MixedAmount) -> Balancing s MixedAmount)
-> (BalancingState s -> ST s MixedAmount)
-> Balancing s MixedAmount
forall a b. (a -> b) -> a -> b
$ \BalancingState{HashTable s AccountName MixedAmount
bsBalances :: HashTable s AccountName MixedAmount
bsBalances :: forall s. BalancingState s -> HashTable s AccountName MixedAmount
bsBalances} -> do
MixedAmount
old <- MixedAmount -> Maybe MixedAmount -> MixedAmount
forall a. a -> Maybe a -> a
fromMaybe MixedAmount
0 (Maybe MixedAmount -> MixedAmount)
-> ST s (Maybe MixedAmount) -> ST s MixedAmount
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> HashTable s AccountName MixedAmount
-> AccountName -> ST s (Maybe MixedAmount)
forall k s v.
(Eq k, Hashable k) =>
HashTable s k v -> k -> ST s (Maybe v)
H.lookup HashTable s AccountName MixedAmount
bsBalances AccountName
acc
HashTable s AccountName MixedAmount
-> AccountName -> MixedAmount -> ST s ()
forall k s v.
(Eq k, Hashable k) =>
HashTable s k v -> k -> v -> ST s ()
H.insert HashTable s AccountName MixedAmount
bsBalances AccountName
acc MixedAmount
amt
MixedAmount -> ST s MixedAmount
forall (m :: * -> *) a. Monad m => a -> m a
return (MixedAmount -> ST s MixedAmount)
-> MixedAmount -> ST s MixedAmount
forall a b. (a -> b) -> a -> b
$ MixedAmount
amt MixedAmount -> MixedAmount -> MixedAmount
forall a. Num a => a -> a -> a
- MixedAmount
old
setInclusiveRunningBalanceB :: AccountName -> MixedAmount -> Balancing s MixedAmount
setInclusiveRunningBalanceB :: AccountName -> MixedAmount -> Balancing s MixedAmount
setInclusiveRunningBalanceB AccountName
acc MixedAmount
newibal = (BalancingState s -> ST s MixedAmount) -> Balancing s MixedAmount
forall s a. (BalancingState s -> ST s a) -> Balancing s a
withRunningBalance ((BalancingState s -> ST s MixedAmount) -> Balancing s MixedAmount)
-> (BalancingState s -> ST s MixedAmount)
-> Balancing s MixedAmount
forall a b. (a -> b) -> a -> b
$ \BalancingState{HashTable s AccountName MixedAmount
bsBalances :: HashTable s AccountName MixedAmount
bsBalances :: forall s. BalancingState s -> HashTable s AccountName MixedAmount
bsBalances} -> do
MixedAmount
oldebal <- MixedAmount -> Maybe MixedAmount -> MixedAmount
forall a. a -> Maybe a -> a
fromMaybe MixedAmount
0 (Maybe MixedAmount -> MixedAmount)
-> ST s (Maybe MixedAmount) -> ST s MixedAmount
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> HashTable s AccountName MixedAmount
-> AccountName -> ST s (Maybe MixedAmount)
forall k s v.
(Eq k, Hashable k) =>
HashTable s k v -> k -> ST s (Maybe v)
H.lookup HashTable s AccountName MixedAmount
bsBalances AccountName
acc
[(AccountName, MixedAmount)]
allebals <- HashTable s AccountName MixedAmount
-> ST s [(AccountName, MixedAmount)]
forall (h :: * -> * -> * -> *) s k v.
HashTable h =>
h s k v -> ST s [(k, v)]
H.toList HashTable s AccountName MixedAmount
bsBalances
let subsibal :: MixedAmount
subsibal =
[MixedAmount] -> MixedAmount
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum ([MixedAmount] -> MixedAmount) -> [MixedAmount] -> MixedAmount
forall a b. (a -> b) -> a -> b
$ ((AccountName, MixedAmount) -> MixedAmount)
-> [(AccountName, MixedAmount)] -> [MixedAmount]
forall a b. (a -> b) -> [a] -> [b]
map (AccountName, MixedAmount) -> MixedAmount
forall a b. (a, b) -> b
snd ([(AccountName, MixedAmount)] -> [MixedAmount])
-> [(AccountName, MixedAmount)] -> [MixedAmount]
forall a b. (a -> b) -> a -> b
$ ((AccountName, MixedAmount) -> Bool)
-> [(AccountName, MixedAmount)] -> [(AccountName, MixedAmount)]
forall a. (a -> Bool) -> [a] -> [a]
filter ((AccountName
acc AccountName -> AccountName -> Bool
`isAccountNamePrefixOf`)(AccountName -> Bool)
-> ((AccountName, MixedAmount) -> AccountName)
-> (AccountName, MixedAmount)
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(AccountName, MixedAmount) -> AccountName
forall a b. (a, b) -> a
fst) [(AccountName, MixedAmount)]
allebals
let newebal :: MixedAmount
newebal = MixedAmount
newibal MixedAmount -> MixedAmount -> MixedAmount
forall a. Num a => a -> a -> a
- MixedAmount
subsibal
HashTable s AccountName MixedAmount
-> AccountName -> MixedAmount -> ST s ()
forall k s v.
(Eq k, Hashable k) =>
HashTable s k v -> k -> v -> ST s ()
H.insert HashTable s AccountName MixedAmount
bsBalances AccountName
acc MixedAmount
newebal
MixedAmount -> ST s MixedAmount
forall (m :: * -> *) a. Monad m => a -> m a
return (MixedAmount -> ST s MixedAmount)
-> MixedAmount -> ST s MixedAmount
forall a b. (a -> b) -> a -> b
$ MixedAmount
newebal MixedAmount -> MixedAmount -> MixedAmount
forall a. Num a => a -> a -> a
- MixedAmount
oldebal
updateTransactionB :: Transaction -> Balancing s ()
updateTransactionB :: Transaction -> Balancing s ()
updateTransactionB Transaction
t = (BalancingState s -> ST s ()) -> Balancing s ()
forall s a. (BalancingState s -> ST s a) -> Balancing s a
withRunningBalance ((BalancingState s -> ST s ()) -> Balancing s ())
-> (BalancingState s -> ST s ()) -> Balancing s ()
forall a b. (a -> b) -> a -> b
$ \BalancingState{STArray s Year Transaction
bsTransactions :: STArray s Year Transaction
bsTransactions :: forall s. BalancingState s -> STArray s Year Transaction
bsTransactions} ->
ST s () -> ST s ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (ST s () -> ST s ()) -> ST s () -> ST s ()
forall a b. (a -> b) -> a -> b
$ STArray s Year Transaction -> Year -> Transaction -> ST s ()
forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> e -> m ()
writeArray STArray s Year Transaction
bsTransactions (Transaction -> Year
tindex Transaction
t) Transaction
t
journalBalanceTransactions :: Bool -> Journal -> Either String Journal
journalBalanceTransactions :: Bool -> Journal -> Either String Journal
journalBalanceTransactions Bool
assrt Journal
j' =
let
j :: Journal
j@Journal{jtxns :: Journal -> [Transaction]
jtxns=[Transaction]
ts} = Journal -> Journal
journalNumberTransactions Journal
j'
styles :: Maybe (Map AccountName AmountStyle)
styles = Map AccountName AmountStyle -> Maybe (Map AccountName AmountStyle)
forall a. a -> Maybe a
Just (Map AccountName AmountStyle
-> Maybe (Map AccountName AmountStyle))
-> Map AccountName AmountStyle
-> Maybe (Map AccountName AmountStyle)
forall a b. (a -> b) -> a -> b
$ Journal -> Map AccountName AmountStyle
journalCommodityStyles Journal
j
txnmodifieraccts :: Set AccountName
txnmodifieraccts = [AccountName] -> Set AccountName
forall a. Ord a => [a] -> Set a
S.fromList ([AccountName] -> Set AccountName)
-> [AccountName] -> Set AccountName
forall a b. (a -> b) -> a -> b
$ (Posting -> AccountName) -> [Posting] -> [AccountName]
forall a b. (a -> b) -> [a] -> [b]
map Posting -> AccountName
paccount ([Posting] -> [AccountName]) -> [Posting] -> [AccountName]
forall a b. (a -> b) -> a -> b
$ (TransactionModifier -> [Posting])
-> [TransactionModifier] -> [Posting]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap TransactionModifier -> [Posting]
tmpostingrules ([TransactionModifier] -> [Posting])
-> [TransactionModifier] -> [Posting]
forall a b. (a -> b) -> a -> b
$ Journal -> [TransactionModifier]
jtxnmodifiers Journal
j
in
(forall s. ST s (Either String Journal)) -> Either String Journal
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (Either String Journal)) -> Either String Journal)
-> (forall s. ST s (Either String Journal))
-> Either String Journal
forall a b. (a -> b) -> a -> b
$ do
STArray s Year Transaction
balancedtxns <- (Year, Year) -> [Transaction] -> ST s (STArray s Year Transaction)
forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
(i, i) -> [e] -> m (a i e)
newListArray (Year
1, Int -> Year
forall a. Integral a => a -> Year
toInteger (Int -> Year) -> Int -> Year
forall a b. (a -> b) -> a -> b
$ [Transaction] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Transaction]
ts) [Transaction]
ts
ExceptT String (ST s) Journal -> ST s (Either String Journal)
forall e (m :: * -> *) a. ExceptT e m a -> m (Either e a)
runExceptT (ExceptT String (ST s) Journal -> ST s (Either String Journal))
-> ExceptT String (ST s) Journal -> ST s (Either String Journal)
forall a b. (a -> b) -> a -> b
$ do
[Either Posting Transaction]
psandts :: [Either Posting Transaction] <- ([[Either Posting Transaction]] -> [Either Posting Transaction])
-> ExceptT String (ST s) [[Either Posting Transaction]]
-> ExceptT String (ST s) [Either Posting Transaction]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [[Either Posting Transaction]] -> [Either Posting Transaction]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat (ExceptT String (ST s) [[Either Posting Transaction]]
-> ExceptT String (ST s) [Either Posting Transaction])
-> ExceptT String (ST s) [[Either Posting Transaction]]
-> ExceptT String (ST s) [Either Posting Transaction]
forall a b. (a -> b) -> a -> b
$ [Transaction]
-> (Transaction
-> ExceptT String (ST s) [Either Posting Transaction])
-> ExceptT String (ST s) [[Either Posting Transaction]]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM [Transaction]
ts ((Transaction
-> ExceptT String (ST s) [Either Posting Transaction])
-> ExceptT String (ST s) [[Either Posting Transaction]])
-> (Transaction
-> ExceptT String (ST s) [Either Posting Transaction])
-> ExceptT String (ST s) [[Either Posting Transaction]]
forall a b. (a -> b) -> a -> b
$ \case
Transaction
t | [Posting] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null ([Posting] -> Bool) -> [Posting] -> Bool
forall a b. (a -> b) -> a -> b
$ Transaction -> [Posting]
assignmentPostings Transaction
t -> case Maybe (Map AccountName AmountStyle)
-> Transaction -> Either String Transaction
balanceTransaction Maybe (Map AccountName AmountStyle)
styles Transaction
t of
Left String
e -> String -> ExceptT String (ST s) [Either Posting Transaction]
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError String
e
Right Transaction
t' -> do
ST s () -> ExceptT String (ST s) ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (ST s () -> ExceptT String (ST s) ())
-> ST s () -> ExceptT String (ST s) ()
forall a b. (a -> b) -> a -> b
$ STArray s Year Transaction -> Year -> Transaction -> ST s ()
forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> e -> m ()
writeArray STArray s Year Transaction
balancedtxns (Transaction -> Year
tindex Transaction
t') Transaction
t'
[Either Posting Transaction]
-> ExceptT String (ST s) [Either Posting Transaction]
forall (m :: * -> *) a. Monad m => a -> m a
return ([Either Posting Transaction]
-> ExceptT String (ST s) [Either Posting Transaction])
-> [Either Posting Transaction]
-> ExceptT String (ST s) [Either Posting Transaction]
forall a b. (a -> b) -> a -> b
$ (Posting -> Either Posting Transaction)
-> [Posting] -> [Either Posting Transaction]
forall a b. (a -> b) -> [a] -> [b]
map Posting -> Either Posting Transaction
forall a b. a -> Either a b
Left ([Posting] -> [Either Posting Transaction])
-> [Posting] -> [Either Posting Transaction]
forall a b. (a -> b) -> a -> b
$ Transaction -> [Posting]
tpostings Transaction
t'
Transaction
t -> [Either Posting Transaction]
-> ExceptT String (ST s) [Either Posting Transaction]
forall (m :: * -> *) a. Monad m => a -> m a
return [Transaction -> Either Posting Transaction
forall a b. b -> Either a b
Right Transaction
t]
HashTable s AccountName MixedAmount
runningbals <- ST s (HashTable s AccountName MixedAmount)
-> ExceptT String (ST s) (HashTable s AccountName MixedAmount)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (ST s (HashTable s AccountName MixedAmount)
-> ExceptT String (ST s) (HashTable s AccountName MixedAmount))
-> ST s (HashTable s AccountName MixedAmount)
-> ExceptT String (ST s) (HashTable s AccountName MixedAmount)
forall a b. (a -> b) -> a -> b
$ Int -> ST s (HashTable s AccountName MixedAmount)
forall s k v. Int -> ST s (HashTable s k v)
H.newSized ([AccountName] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length ([AccountName] -> Int) -> [AccountName] -> Int
forall a b. (a -> b) -> a -> b
$ Journal -> [AccountName]
journalAccountNamesUsed Journal
j)
(ReaderT (BalancingState s) (ExceptT String (ST s)) ()
-> BalancingState s -> ExceptT String (ST s) ())
-> BalancingState s
-> ReaderT (BalancingState s) (ExceptT String (ST s)) ()
-> ExceptT String (ST s) ()
forall a b c. (a -> b -> c) -> b -> a -> c
flip ReaderT (BalancingState s) (ExceptT String (ST s)) ()
-> BalancingState s -> ExceptT String (ST s) ()
forall r (m :: * -> *) a. ReaderT r m a -> r -> m a
runReaderT (Maybe (Map AccountName AmountStyle)
-> Set AccountName
-> Bool
-> HashTable s AccountName MixedAmount
-> STArray s Year Transaction
-> BalancingState s
forall s.
Maybe (Map AccountName AmountStyle)
-> Set AccountName
-> Bool
-> HashTable s AccountName MixedAmount
-> STArray s Year Transaction
-> BalancingState s
BalancingState Maybe (Map AccountName AmountStyle)
styles Set AccountName
txnmodifieraccts Bool
assrt HashTable s AccountName MixedAmount
runningbals STArray s Year Transaction
balancedtxns) (ReaderT (BalancingState s) (ExceptT String (ST s)) ()
-> ExceptT String (ST s) ())
-> ReaderT (BalancingState s) (ExceptT String (ST s)) ()
-> ExceptT String (ST s) ()
forall a b. (a -> b) -> a -> b
$ do
ReaderT (BalancingState s) (ExceptT String (ST s)) [()]
-> ReaderT (BalancingState s) (ExceptT String (ST s)) ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (ReaderT (BalancingState s) (ExceptT String (ST s)) [()]
-> ReaderT (BalancingState s) (ExceptT String (ST s)) ())
-> ReaderT (BalancingState s) (ExceptT String (ST s)) [()]
-> ReaderT (BalancingState s) (ExceptT String (ST s)) ()
forall a b. (a -> b) -> a -> b
$ (Either Posting Transaction
-> ReaderT (BalancingState s) (ExceptT String (ST s)) ())
-> [Either Posting Transaction]
-> ReaderT (BalancingState s) (ExceptT String (ST s)) [()]
forall (f :: * -> *) a b. Monad f => (a -> f b) -> [a] -> f [b]
mapM' Either Posting Transaction
-> ReaderT (BalancingState s) (ExceptT String (ST s)) ()
forall s. Either Posting Transaction -> Balancing s ()
balanceTransactionAndCheckAssertionsB ([Either Posting Transaction]
-> ReaderT (BalancingState s) (ExceptT String (ST s)) [()])
-> [Either Posting Transaction]
-> ReaderT (BalancingState s) (ExceptT String (ST s)) [()]
forall a b. (a -> b) -> a -> b
$ (Either Posting Transaction -> Day)
-> [Either Posting Transaction] -> [Either Posting Transaction]
forall b a. Ord b => (a -> b) -> [a] -> [a]
sortOn ((Posting -> Day)
-> (Transaction -> Day) -> Either Posting Transaction -> Day
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either Posting -> Day
postingDate Transaction -> Day
tdate) [Either Posting Transaction]
psandts
[Transaction]
ts' <- ST s [Transaction] -> ExceptT String (ST s) [Transaction]
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (ST s [Transaction] -> ExceptT String (ST s) [Transaction])
-> ST s [Transaction] -> ExceptT String (ST s) [Transaction]
forall a b. (a -> b) -> a -> b
$ STArray s Year Transaction -> ST s [Transaction]
forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> m [e]
getElems STArray s Year Transaction
balancedtxns
Journal -> ExceptT String (ST s) Journal
forall (m :: * -> *) a. Monad m => a -> m a
return Journal
j{jtxns :: [Transaction]
jtxns=[Transaction]
ts'}
balanceTransactionAndCheckAssertionsB :: Either Posting Transaction -> Balancing s ()
balanceTransactionAndCheckAssertionsB :: Either Posting Transaction -> Balancing s ()
balanceTransactionAndCheckAssertionsB (Left p :: Posting
p@Posting{}) =
ReaderT (BalancingState s) (ExceptT String (ST s)) Posting
-> Balancing s ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (ReaderT (BalancingState s) (ExceptT String (ST s)) Posting
-> Balancing s ())
-> ReaderT (BalancingState s) (ExceptT String (ST s)) Posting
-> Balancing s ()
forall a b. (a -> b) -> a -> b
$ Posting
-> ReaderT (BalancingState s) (ExceptT String (ST s)) Posting
forall s. Posting -> Balancing s Posting
addAmountAndCheckAssertionB (Posting
-> ReaderT (BalancingState s) (ExceptT String (ST s)) Posting)
-> Posting
-> ReaderT (BalancingState s) (ExceptT String (ST s)) Posting
forall a b. (a -> b) -> a -> b
$ Posting -> Posting
removePrices Posting
p
balanceTransactionAndCheckAssertionsB (Right t :: Transaction
t@Transaction{tpostings :: Transaction -> [Posting]
tpostings=[Posting]
ps}) = do
(Posting -> Balancing s ()) -> [Posting] -> Balancing s ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ Posting -> Balancing s ()
forall s. Posting -> Balancing s ()
checkIllegalBalanceAssignmentB [Posting]
ps
[Posting]
ps' <- [Posting]
-> (Posting
-> ReaderT (BalancingState s) (ExceptT String (ST s)) Posting)
-> ReaderT (BalancingState s) (ExceptT String (ST s)) [Posting]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM [Posting]
ps ((Posting
-> ReaderT (BalancingState s) (ExceptT String (ST s)) Posting)
-> ReaderT (BalancingState s) (ExceptT String (ST s)) [Posting])
-> (Posting
-> ReaderT (BalancingState s) (ExceptT String (ST s)) Posting)
-> ReaderT (BalancingState s) (ExceptT String (ST s)) [Posting]
forall a b. (a -> b) -> a -> b
$ \Posting
p -> Posting
-> ReaderT (BalancingState s) (ExceptT String (ST s)) Posting
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Posting -> Posting
removePrices Posting
p) ReaderT (BalancingState s) (ExceptT String (ST s)) Posting
-> (Posting
-> ReaderT (BalancingState s) (ExceptT String (ST s)) Posting)
-> ReaderT (BalancingState s) (ExceptT String (ST s)) Posting
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Posting
-> ReaderT (BalancingState s) (ExceptT String (ST s)) Posting
forall s. Posting -> Balancing s Posting
addOrAssignAmountAndCheckAssertionB
Maybe (Map AccountName AmountStyle)
styles <- (BalancingState s -> Maybe (Map AccountName AmountStyle))
-> ReaderT
(BalancingState s)
(ExceptT String (ST s))
(Maybe (Map AccountName AmountStyle))
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
R.reader BalancingState s -> Maybe (Map AccountName AmountStyle)
forall s. BalancingState s -> Maybe (Map AccountName AmountStyle)
bsStyles
case Maybe (Map AccountName AmountStyle)
-> Transaction
-> Either String (Transaction, [(AccountName, MixedAmount)])
balanceTransactionHelper Maybe (Map AccountName AmountStyle)
styles Transaction
t{tpostings :: [Posting]
tpostings=[Posting]
ps'} of
Left String
err -> String -> Balancing s ()
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError String
err
Right (Transaction
t', [(AccountName, MixedAmount)]
inferredacctsandamts) -> do
((AccountName, MixedAmount)
-> ReaderT (BalancingState s) (ExceptT String (ST s)) MixedAmount)
-> [(AccountName, MixedAmount)] -> Balancing s ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ ((AccountName
-> MixedAmount
-> ReaderT (BalancingState s) (ExceptT String (ST s)) MixedAmount)
-> (AccountName, MixedAmount)
-> ReaderT (BalancingState s) (ExceptT String (ST s)) MixedAmount
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry AccountName
-> MixedAmount
-> ReaderT (BalancingState s) (ExceptT String (ST s)) MixedAmount
forall s. AccountName -> MixedAmount -> Balancing s MixedAmount
addToRunningBalanceB) [(AccountName, MixedAmount)]
inferredacctsandamts
Transaction -> Balancing s ()
forall s. Transaction -> Balancing s ()
updateTransactionB Transaction
t'
addOrAssignAmountAndCheckAssertionB :: Posting -> Balancing s Posting
addOrAssignAmountAndCheckAssertionB :: Posting -> Balancing s Posting
addOrAssignAmountAndCheckAssertionB p :: Posting
p@Posting{paccount :: Posting -> AccountName
paccount=AccountName
acc, pamount :: Posting -> MixedAmount
pamount=MixedAmount
amt, pbalanceassertion :: Posting -> Maybe BalanceAssertion
pbalanceassertion=Maybe BalanceAssertion
mba}
| Posting -> Bool
hasAmount Posting
p = do
MixedAmount
newbal <- AccountName -> MixedAmount -> Balancing s MixedAmount
forall s. AccountName -> MixedAmount -> Balancing s MixedAmount
addToRunningBalanceB AccountName
acc MixedAmount
amt
ReaderT (BalancingState s) (ExceptT String (ST s)) Bool
-> ReaderT (BalancingState s) (ExceptT String (ST s)) ()
-> ReaderT (BalancingState s) (ExceptT String (ST s)) ()
forall (m :: * -> *). Monad m => m Bool -> m () -> m ()
whenM ((BalancingState s -> Bool)
-> ReaderT (BalancingState s) (ExceptT String (ST s)) Bool
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
R.reader BalancingState s -> Bool
forall s. BalancingState s -> Bool
bsAssrt) (ReaderT (BalancingState s) (ExceptT String (ST s)) ()
-> ReaderT (BalancingState s) (ExceptT String (ST s)) ())
-> ReaderT (BalancingState s) (ExceptT String (ST s)) ()
-> ReaderT (BalancingState s) (ExceptT String (ST s)) ()
forall a b. (a -> b) -> a -> b
$ Posting
-> MixedAmount
-> ReaderT (BalancingState s) (ExceptT String (ST s)) ()
forall s. Posting -> MixedAmount -> Balancing s ()
checkBalanceAssertionB Posting
p MixedAmount
newbal
Posting -> Balancing s Posting
forall (m :: * -> *) a. Monad m => a -> m a
return Posting
p
| Just BalanceAssertion{Amount
baamount :: BalanceAssertion -> Amount
baamount :: Amount
baamount,Bool
batotal :: BalanceAssertion -> Bool
batotal :: Bool
batotal,Bool
bainclusive :: BalanceAssertion -> Bool
bainclusive :: Bool
bainclusive} <- Maybe BalanceAssertion
mba = do
(MixedAmount
diff,MixedAmount
newbal) <- case Bool
batotal of
Bool
True -> do
let newbal :: MixedAmount
newbal = [Amount] -> MixedAmount
Mixed [Amount
baamount]
MixedAmount
diff <- (if Bool
bainclusive then AccountName -> MixedAmount -> Balancing s MixedAmount
forall s. AccountName -> MixedAmount -> Balancing s MixedAmount
setInclusiveRunningBalanceB else AccountName -> MixedAmount -> Balancing s MixedAmount
forall s. AccountName -> MixedAmount -> Balancing s MixedAmount
setRunningBalanceB) AccountName
acc MixedAmount
newbal
(MixedAmount, MixedAmount)
-> ReaderT
(BalancingState s)
(ExceptT String (ST s))
(MixedAmount, MixedAmount)
forall (m :: * -> *) a. Monad m => a -> m a
return (MixedAmount
diff,MixedAmount
newbal)
Bool
False -> do
MixedAmount
oldbalothercommodities <- (Amount -> Bool) -> MixedAmount -> MixedAmount
filterMixedAmount ((Amount -> AccountName
acommodity Amount
baamount AccountName -> AccountName -> Bool
forall a. Eq a => a -> a -> Bool
/=) (AccountName -> Bool) -> (Amount -> AccountName) -> Amount -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Amount -> AccountName
acommodity) (MixedAmount -> MixedAmount)
-> Balancing s MixedAmount -> Balancing s MixedAmount
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> AccountName -> Balancing s MixedAmount
forall s. AccountName -> Balancing s MixedAmount
getRunningBalanceB AccountName
acc
let assignedbalthiscommodity :: MixedAmount
assignedbalthiscommodity = [Amount] -> MixedAmount
Mixed [Amount
baamount]
newbal :: MixedAmount
newbal = MixedAmount
oldbalothercommodities MixedAmount -> MixedAmount -> MixedAmount
forall a. Num a => a -> a -> a
+ MixedAmount
assignedbalthiscommodity
MixedAmount
diff <- (if Bool
bainclusive then AccountName -> MixedAmount -> Balancing s MixedAmount
forall s. AccountName -> MixedAmount -> Balancing s MixedAmount
setInclusiveRunningBalanceB else AccountName -> MixedAmount -> Balancing s MixedAmount
forall s. AccountName -> MixedAmount -> Balancing s MixedAmount
setRunningBalanceB) AccountName
acc MixedAmount
newbal
(MixedAmount, MixedAmount)
-> ReaderT
(BalancingState s)
(ExceptT String (ST s))
(MixedAmount, MixedAmount)
forall (m :: * -> *) a. Monad m => a -> m a
return (MixedAmount
diff,MixedAmount
newbal)
let p' :: Posting
p' = Posting
p{pamount :: MixedAmount
pamount=MixedAmount
diff, poriginal :: Maybe Posting
poriginal=Posting -> Maybe Posting
forall a. a -> Maybe a
Just (Posting -> Maybe Posting) -> Posting -> Maybe Posting
forall a b. (a -> b) -> a -> b
$ Posting -> Posting
originalPosting Posting
p}
ReaderT (BalancingState s) (ExceptT String (ST s)) Bool
-> ReaderT (BalancingState s) (ExceptT String (ST s)) ()
-> ReaderT (BalancingState s) (ExceptT String (ST s)) ()
forall (m :: * -> *). Monad m => m Bool -> m () -> m ()
whenM ((BalancingState s -> Bool)
-> ReaderT (BalancingState s) (ExceptT String (ST s)) Bool
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
R.reader BalancingState s -> Bool
forall s. BalancingState s -> Bool
bsAssrt) (ReaderT (BalancingState s) (ExceptT String (ST s)) ()
-> ReaderT (BalancingState s) (ExceptT String (ST s)) ())
-> ReaderT (BalancingState s) (ExceptT String (ST s)) ()
-> ReaderT (BalancingState s) (ExceptT String (ST s)) ()
forall a b. (a -> b) -> a -> b
$ Posting
-> MixedAmount
-> ReaderT (BalancingState s) (ExceptT String (ST s)) ()
forall s. Posting -> MixedAmount -> Balancing s ()
checkBalanceAssertionB Posting
p' MixedAmount
newbal
Posting -> Balancing s Posting
forall (m :: * -> *) a. Monad m => a -> m a
return Posting
p'
| Bool
otherwise = Posting -> Balancing s Posting
forall (m :: * -> *) a. Monad m => a -> m a
return Posting
p
addAmountAndCheckAssertionB :: Posting -> Balancing s Posting
addAmountAndCheckAssertionB :: Posting -> Balancing s Posting
addAmountAndCheckAssertionB Posting
p | Posting -> Bool
hasAmount Posting
p = do
MixedAmount
newbal <- AccountName -> MixedAmount -> Balancing s MixedAmount
forall s. AccountName -> MixedAmount -> Balancing s MixedAmount
addToRunningBalanceB (Posting -> AccountName
paccount Posting
p) (Posting -> MixedAmount
pamount Posting
p)
ReaderT (BalancingState s) (ExceptT String (ST s)) Bool
-> ReaderT (BalancingState s) (ExceptT String (ST s)) ()
-> ReaderT (BalancingState s) (ExceptT String (ST s)) ()
forall (m :: * -> *). Monad m => m Bool -> m () -> m ()
whenM ((BalancingState s -> Bool)
-> ReaderT (BalancingState s) (ExceptT String (ST s)) Bool
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
R.reader BalancingState s -> Bool
forall s. BalancingState s -> Bool
bsAssrt) (ReaderT (BalancingState s) (ExceptT String (ST s)) ()
-> ReaderT (BalancingState s) (ExceptT String (ST s)) ())
-> ReaderT (BalancingState s) (ExceptT String (ST s)) ()
-> ReaderT (BalancingState s) (ExceptT String (ST s)) ()
forall a b. (a -> b) -> a -> b
$ Posting
-> MixedAmount
-> ReaderT (BalancingState s) (ExceptT String (ST s)) ()
forall s. Posting -> MixedAmount -> Balancing s ()
checkBalanceAssertionB Posting
p MixedAmount
newbal
Posting -> Balancing s Posting
forall (m :: * -> *) a. Monad m => a -> m a
return Posting
p
addAmountAndCheckAssertionB Posting
p = Posting -> Balancing s Posting
forall (m :: * -> *) a. Monad m => a -> m a
return Posting
p
checkBalanceAssertionB :: Posting -> MixedAmount -> Balancing s ()
checkBalanceAssertionB :: Posting -> MixedAmount -> Balancing s ()
checkBalanceAssertionB p :: Posting
p@Posting{pbalanceassertion :: Posting -> Maybe BalanceAssertion
pbalanceassertion=Just (BalanceAssertion{Amount
baamount :: Amount
baamount :: BalanceAssertion -> Amount
baamount,Bool
batotal :: Bool
batotal :: BalanceAssertion -> Bool
batotal})} MixedAmount
actualbal =
[Amount] -> (Amount -> Balancing s ()) -> Balancing s ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [Amount]
assertedamts ((Amount -> Balancing s ()) -> Balancing s ())
-> (Amount -> Balancing s ()) -> Balancing s ()
forall a b. (a -> b) -> a -> b
$ \Amount
amt -> Posting -> Amount -> MixedAmount -> Balancing s ()
forall s. Posting -> Amount -> MixedAmount -> Balancing s ()
checkBalanceAssertionOneCommodityB Posting
p Amount
amt MixedAmount
actualbal
where
assertedamts :: [Amount]
assertedamts = Amount
baamount Amount -> [Amount] -> [Amount]
forall a. a -> [a] -> [a]
: [Amount]
otheramts
where
assertedcomm :: AccountName
assertedcomm = Amount -> AccountName
acommodity Amount
baamount
otheramts :: [Amount]
otheramts | Bool
batotal = (Amount -> Amount) -> [Amount] -> [Amount]
forall a b. (a -> b) -> [a] -> [b]
map (\Amount
a -> Amount
a{aquantity :: Quantity
aquantity=Quantity
0}) ([Amount] -> [Amount]) -> [Amount] -> [Amount]
forall a b. (a -> b) -> a -> b
$ MixedAmount -> [Amount]
amounts (MixedAmount -> [Amount]) -> MixedAmount -> [Amount]
forall a b. (a -> b) -> a -> b
$ (Amount -> Bool) -> MixedAmount -> MixedAmount
filterMixedAmount ((AccountName -> AccountName -> Bool
forall a. Eq a => a -> a -> Bool
/=AccountName
assertedcomm)(AccountName -> Bool) -> (Amount -> AccountName) -> Amount -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
.Amount -> AccountName
acommodity) MixedAmount
actualbal
| Bool
otherwise = []
checkBalanceAssertionB Posting
_ MixedAmount
_ = () -> Balancing s ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
checkBalanceAssertionOneCommodityB :: Posting -> Amount -> MixedAmount -> Balancing s ()
checkBalanceAssertionOneCommodityB :: Posting -> Amount -> MixedAmount -> Balancing s ()
checkBalanceAssertionOneCommodityB p :: Posting
p@Posting{paccount :: Posting -> AccountName
paccount=AccountName
assertedacct} Amount
assertedamt MixedAmount
actualbal = do
let isinclusive :: Bool
isinclusive = Bool
-> (BalanceAssertion -> Bool) -> Maybe BalanceAssertion -> Bool
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Bool
False BalanceAssertion -> Bool
bainclusive (Maybe BalanceAssertion -> Bool) -> Maybe BalanceAssertion -> Bool
forall a b. (a -> b) -> a -> b
$ Posting -> Maybe BalanceAssertion
pbalanceassertion Posting
p
MixedAmount
actualbal' <-
if Bool
isinclusive
then
(BalancingState s -> ST s MixedAmount) -> Balancing s MixedAmount
forall s a. (BalancingState s -> ST s a) -> Balancing s a
withRunningBalance ((BalancingState s -> ST s MixedAmount) -> Balancing s MixedAmount)
-> (BalancingState s -> ST s MixedAmount)
-> Balancing s MixedAmount
forall a b. (a -> b) -> a -> b
$ \BalancingState{HashTable s AccountName MixedAmount
bsBalances :: HashTable s AccountName MixedAmount
bsBalances :: forall s. BalancingState s -> HashTable s AccountName MixedAmount
bsBalances} ->
(MixedAmount -> (AccountName, MixedAmount) -> ST s MixedAmount)
-> MixedAmount
-> HashTable s AccountName MixedAmount
-> ST s MixedAmount
forall a k v s.
(a -> (k, v) -> ST s a) -> a -> HashTable s k v -> ST s a
H.foldM
(\MixedAmount
ibal (AccountName
acc, MixedAmount
amt) -> MixedAmount -> ST s MixedAmount
forall (m :: * -> *) a. Monad m => a -> m a
return (MixedAmount -> ST s MixedAmount)
-> MixedAmount -> ST s MixedAmount
forall a b. (a -> b) -> a -> b
$ MixedAmount
ibal MixedAmount -> MixedAmount -> MixedAmount
forall a. Num a => a -> a -> a
+
if AccountName
assertedacctAccountName -> AccountName -> Bool
forall a. Eq a => a -> a -> Bool
==AccountName
acc Bool -> Bool -> Bool
|| AccountName
assertedacct AccountName -> AccountName -> Bool
`isAccountNamePrefixOf` AccountName
acc then MixedAmount
amt else MixedAmount
0)
MixedAmount
0
HashTable s AccountName MixedAmount
bsBalances
else MixedAmount -> Balancing s MixedAmount
forall (m :: * -> *) a. Monad m => a -> m a
return MixedAmount
actualbal
let
assertedcomm :: AccountName
assertedcomm = Amount -> AccountName
acommodity Amount
assertedamt
actualbalincomm :: Amount
actualbalincomm = Amount -> [Amount] -> Amount
forall a. a -> [a] -> a
headDef Amount
0 ([Amount] -> Amount) -> [Amount] -> Amount
forall a b. (a -> b) -> a -> b
$ MixedAmount -> [Amount]
amounts (MixedAmount -> [Amount]) -> MixedAmount -> [Amount]
forall a b. (a -> b) -> a -> b
$ AccountName -> MixedAmount -> MixedAmount
filterMixedAmountByCommodity AccountName
assertedcomm (MixedAmount -> MixedAmount) -> MixedAmount -> MixedAmount
forall a b. (a -> b) -> a -> b
$ MixedAmount
actualbal'
pass :: Bool
pass =
Amount -> Quantity
aquantity
Amount
assertedamt Quantity -> Quantity -> Bool
forall a. Eq a => a -> a -> Bool
==
Amount -> Quantity
aquantity
Amount
actualbalincomm
errmsg :: String
errmsg = String
-> String
-> AccountName
-> String
-> String
-> AccountName
-> String
-> String
-> ShowS
forall r. PrintfType r => String -> r
printf ([String] -> String
unlines
[ String
"balance assertion: %s",
String
"\nassertion details:",
String
"date: %s",
String
"account: %s%s",
String
"commodity: %s",
String
"calculated: %s",
String
"asserted: %s",
String
"difference: %s"
])
(case Posting -> Maybe Transaction
ptransaction Posting
p of
Maybe Transaction
Nothing -> String
"?"
Just Transaction
t -> String -> String -> AccountName -> String
forall r. PrintfType r => String -> r
printf String
"%s\ntransaction:\n%s"
(GenericSourcePos -> String
showGenericSourcePos GenericSourcePos
pos)
(AccountName -> AccountName
textChomp (AccountName -> AccountName) -> AccountName -> AccountName
forall a b. (a -> b) -> a -> b
$ Transaction -> AccountName
showTransaction Transaction
t)
:: String
where
pos :: GenericSourcePos
pos = BalanceAssertion -> GenericSourcePos
baposition (BalanceAssertion -> GenericSourcePos)
-> BalanceAssertion -> GenericSourcePos
forall a b. (a -> b) -> a -> b
$ Maybe BalanceAssertion -> BalanceAssertion
forall a. HasCallStack => Maybe a -> a
fromJust (Maybe BalanceAssertion -> BalanceAssertion)
-> Maybe BalanceAssertion -> BalanceAssertion
forall a b. (a -> b) -> a -> b
$ Posting -> Maybe BalanceAssertion
pbalanceassertion Posting
p
)
(Day -> AccountName
showDate (Day -> AccountName) -> Day -> AccountName
forall a b. (a -> b) -> a -> b
$ Posting -> Day
postingDate Posting
p)
(AccountName -> String
T.unpack (AccountName -> String) -> AccountName -> String
forall a b. (a -> b) -> a -> b
$ Posting -> AccountName
paccount Posting
p)
(if Bool
isinclusive then String
" (and subs)" else String
"" :: String)
AccountName
assertedcomm
(Quantity -> String
forall a. Show a => a -> String
show (Quantity -> String) -> Quantity -> String
forall a b. (a -> b) -> a -> b
$ Amount -> Quantity
aquantity Amount
actualbalincomm)
(Quantity -> String
forall a. Show a => a -> String
show (Quantity -> String) -> Quantity -> String
forall a b. (a -> b) -> a -> b
$ Amount -> Quantity
aquantity Amount
assertedamt)
(Quantity -> String
forall a. Show a => a -> String
show (Quantity -> String) -> Quantity -> String
forall a b. (a -> b) -> a -> b
$ Amount -> Quantity
aquantity Amount
assertedamt Quantity -> Quantity -> Quantity
forall a. Num a => a -> a -> a
- Amount -> Quantity
aquantity Amount
actualbalincomm)
Bool -> Balancing s () -> Balancing s ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Bool -> Bool
not Bool
pass) (Balancing s () -> Balancing s ())
-> Balancing s () -> Balancing s ()
forall a b. (a -> b) -> a -> b
$ String -> Balancing s ()
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError String
errmsg
checkIllegalBalanceAssignmentB :: Posting -> Balancing s ()
checkIllegalBalanceAssignmentB :: Posting -> Balancing s ()
checkIllegalBalanceAssignmentB Posting
p = do
Posting -> Balancing s ()
forall s. Posting -> Balancing s ()
checkBalanceAssignmentPostingDateB Posting
p
Posting -> Balancing s ()
forall s. Posting -> Balancing s ()
checkBalanceAssignmentUnassignableAccountB Posting
p
checkBalanceAssignmentPostingDateB :: Posting -> Balancing s ()
checkBalanceAssignmentPostingDateB :: Posting -> Balancing s ()
checkBalanceAssignmentPostingDateB Posting
p =
Bool -> Balancing s () -> Balancing s ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Posting -> Bool
hasBalanceAssignment Posting
p Bool -> Bool -> Bool
&& Maybe Day -> Bool
forall a. Maybe a -> Bool
isJust (Posting -> Maybe Day
pdate Posting
p)) (Balancing s () -> Balancing s ())
-> Balancing s () -> Balancing s ()
forall a b. (a -> b) -> a -> b
$
String -> Balancing s ()
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (String -> Balancing s ())
-> (AccountName -> String) -> AccountName -> Balancing s ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AccountName -> String
T.unpack (AccountName -> Balancing s ()) -> AccountName -> Balancing s ()
forall a b. (a -> b) -> a -> b
$ [AccountName] -> AccountName
T.unlines
[AccountName
"postings which are balance assignments may not have a custom date."
,AccountName
"Please write the posting amount explicitly, or remove the posting date:"
,AccountName
""
,AccountName
-> (Transaction -> AccountName) -> Maybe Transaction -> AccountName
forall b a. b -> (a -> b) -> Maybe a -> b
maybe ([AccountName] -> AccountName
T.unlines ([AccountName] -> AccountName) -> [AccountName] -> AccountName
forall a b. (a -> b) -> a -> b
$ Posting -> [AccountName]
showPostingLines Posting
p) Transaction -> AccountName
showTransaction (Maybe Transaction -> AccountName)
-> Maybe Transaction -> AccountName
forall a b. (a -> b) -> a -> b
$ Posting -> Maybe Transaction
ptransaction Posting
p
]
checkBalanceAssignmentUnassignableAccountB :: Posting -> Balancing s ()
checkBalanceAssignmentUnassignableAccountB :: Posting -> Balancing s ()
checkBalanceAssignmentUnassignableAccountB Posting
p = do
Set AccountName
unassignable <- (BalancingState s -> Set AccountName)
-> ReaderT
(BalancingState s) (ExceptT String (ST s)) (Set AccountName)
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
R.asks BalancingState s -> Set AccountName
forall s. BalancingState s -> Set AccountName
bsUnassignable
Bool -> Balancing s () -> Balancing s ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Posting -> Bool
hasBalanceAssignment Posting
p Bool -> Bool -> Bool
&& Posting -> AccountName
paccount Posting
p AccountName -> Set AccountName -> Bool
forall a. Ord a => a -> Set a -> Bool
`S.member` Set AccountName
unassignable) (Balancing s () -> Balancing s ())
-> Balancing s () -> Balancing s ()
forall a b. (a -> b) -> a -> b
$
String -> Balancing s ()
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (String -> Balancing s ())
-> (AccountName -> String) -> AccountName -> Balancing s ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AccountName -> String
T.unpack (AccountName -> Balancing s ()) -> AccountName -> Balancing s ()
forall a b. (a -> b) -> a -> b
$ [AccountName] -> AccountName
T.unlines
[AccountName
"balance assignments cannot be used with accounts which are"
,AccountName
"posted to by transaction modifier rules (auto postings)."
,AccountName
"Please write the posting amount explicitly, or remove the rule."
,AccountName
""
,AccountName
"account: " AccountName -> AccountName -> AccountName
forall a. Semigroup a => a -> a -> a
<> Posting -> AccountName
paccount Posting
p
,AccountName
""
,AccountName
"transaction:"
,AccountName
""
,AccountName
-> (Transaction -> AccountName) -> Maybe Transaction -> AccountName
forall b a. b -> (a -> b) -> Maybe a -> b
maybe ([AccountName] -> AccountName
T.unlines ([AccountName] -> AccountName) -> [AccountName] -> AccountName
forall a b. (a -> b) -> a -> b
$ Posting -> [AccountName]
showPostingLines Posting
p) Transaction -> AccountName
showTransaction (Maybe Transaction -> AccountName)
-> Maybe Transaction -> AccountName
forall a b. (a -> b) -> a -> b
$ Posting -> Maybe Transaction
ptransaction Posting
p
]
journalApplyCommodityStyles :: Journal -> Either String Journal
journalApplyCommodityStyles :: Journal -> Either String Journal
journalApplyCommodityStyles j :: Journal
j@Journal{jtxns :: Journal -> [Transaction]
jtxns=[Transaction]
ts, jpricedirectives :: Journal -> [PriceDirective]
jpricedirectives=[PriceDirective]
pds} =
case Journal -> Either String Journal
journalInferCommodityStyles Journal
j of
Left String
e -> String -> Either String Journal
forall a b. a -> Either a b
Left String
e
Right Journal
j' -> Journal -> Either String Journal
forall a b. b -> Either a b
Right Journal
j''
where
styles :: Map AccountName AmountStyle
styles = Journal -> Map AccountName AmountStyle
journalCommodityStyles Journal
j'
j'' :: Journal
j'' = Journal
j'{jtxns :: [Transaction]
jtxns=(Transaction -> Transaction) -> [Transaction] -> [Transaction]
forall a b. (a -> b) -> [a] -> [b]
map Transaction -> Transaction
fixtransaction [Transaction]
ts
,jpricedirectives :: [PriceDirective]
jpricedirectives=(PriceDirective -> PriceDirective)
-> [PriceDirective] -> [PriceDirective]
forall a b. (a -> b) -> [a] -> [b]
map PriceDirective -> PriceDirective
fixpricedirective [PriceDirective]
pds
}
fixtransaction :: Transaction -> Transaction
fixtransaction t :: Transaction
t@Transaction{tpostings :: Transaction -> [Posting]
tpostings=[Posting]
ps} = Transaction
t{tpostings :: [Posting]
tpostings=(Posting -> Posting) -> [Posting] -> [Posting]
forall a b. (a -> b) -> [a] -> [b]
map Posting -> Posting
fixposting [Posting]
ps}
fixposting :: Posting -> Posting
fixposting Posting
p = Posting
p{pamount :: MixedAmount
pamount=Map AccountName AmountStyle -> MixedAmount -> MixedAmount
styleMixedAmount Map AccountName AmountStyle
styles (MixedAmount -> MixedAmount) -> MixedAmount -> MixedAmount
forall a b. (a -> b) -> a -> b
$ Posting -> MixedAmount
pamount Posting
p
,pbalanceassertion :: Maybe BalanceAssertion
pbalanceassertion=BalanceAssertion -> BalanceAssertion
fixbalanceassertion (BalanceAssertion -> BalanceAssertion)
-> Maybe BalanceAssertion -> Maybe BalanceAssertion
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Posting -> Maybe BalanceAssertion
pbalanceassertion Posting
p}
fixbalanceassertion :: BalanceAssertion -> BalanceAssertion
fixbalanceassertion BalanceAssertion
ba = BalanceAssertion
ba{baamount :: Amount
baamount=Map AccountName AmountStyle -> Amount -> Amount
styleAmountExceptPrecision Map AccountName AmountStyle
styles (Amount -> Amount) -> Amount -> Amount
forall a b. (a -> b) -> a -> b
$ BalanceAssertion -> Amount
baamount BalanceAssertion
ba}
fixpricedirective :: PriceDirective -> PriceDirective
fixpricedirective pd :: PriceDirective
pd@PriceDirective{pdamount :: PriceDirective -> Amount
pdamount=Amount
a} = PriceDirective
pd{pdamount :: Amount
pdamount=Map AccountName AmountStyle -> Amount -> Amount
styleAmountExceptPrecision Map AccountName AmountStyle
styles Amount
a}
journalCommodityStyles :: Journal -> M.Map CommoditySymbol AmountStyle
journalCommodityStyles :: Journal -> Map AccountName AmountStyle
journalCommodityStyles Journal
j =
Map AccountName AmountStyle
globalstyles Map AccountName AmountStyle
-> Map AccountName AmountStyle -> Map AccountName AmountStyle
forall a. Semigroup a => a -> a -> a
<> Map AccountName AmountStyle
declaredstyles Map AccountName AmountStyle
-> Map AccountName AmountStyle -> Map AccountName AmountStyle
forall a. Semigroup a => a -> a -> a
<> Map AccountName AmountStyle
defaultcommoditystyle Map AccountName AmountStyle
-> Map AccountName AmountStyle -> Map AccountName AmountStyle
forall a. Semigroup a => a -> a -> a
<> Map AccountName AmountStyle
inferredstyles
where
globalstyles :: Map AccountName AmountStyle
globalstyles = Journal -> Map AccountName AmountStyle
jglobalcommoditystyles Journal
j
declaredstyles :: Map AccountName AmountStyle
declaredstyles = (Commodity -> Maybe AmountStyle)
-> Map AccountName Commodity -> Map AccountName AmountStyle
forall a b k. (a -> Maybe b) -> Map k a -> Map k b
M.mapMaybe Commodity -> Maybe AmountStyle
cformat (Map AccountName Commodity -> Map AccountName AmountStyle)
-> Map AccountName Commodity -> Map AccountName AmountStyle
forall a b. (a -> b) -> a -> b
$ Journal -> Map AccountName Commodity
jcommodities Journal
j
defaultcommoditystyle :: Map AccountName AmountStyle
defaultcommoditystyle = [(AccountName, AmountStyle)] -> Map AccountName AmountStyle
forall k a. Ord k => [(k, a)] -> Map k a
M.fromList ([(AccountName, AmountStyle)] -> Map AccountName AmountStyle)
-> [(AccountName, AmountStyle)] -> Map AccountName AmountStyle
forall a b. (a -> b) -> a -> b
$ [Maybe (AccountName, AmountStyle)] -> [(AccountName, AmountStyle)]
forall a. [Maybe a] -> [a]
catMaybes [Journal -> Maybe (AccountName, AmountStyle)
jparsedefaultcommodity Journal
j]
inferredstyles :: Map AccountName AmountStyle
inferredstyles = Journal -> Map AccountName AmountStyle
jinferredcommodities Journal
j
journalInferCommodityStyles :: Journal -> Either String Journal
journalInferCommodityStyles :: Journal -> Either String Journal
journalInferCommodityStyles Journal
j =
case
[Amount] -> Either String (Map AccountName AmountStyle)
commodityStylesFromAmounts ([Amount] -> Either String (Map AccountName AmountStyle))
-> [Amount] -> Either String (Map AccountName AmountStyle)
forall a b. (a -> b) -> a -> b
$ Journal -> [Amount]
journalStyleInfluencingAmounts Journal
j
of
Left String
e -> String -> Either String Journal
forall a b. a -> Either a b
Left String
e
Right Map AccountName AmountStyle
cs -> Journal -> Either String Journal
forall a b. b -> Either a b
Right Journal
j{jinferredcommodities :: Map AccountName AmountStyle
jinferredcommodities = String
-> Map AccountName AmountStyle -> Map AccountName AmountStyle
forall a. Show a => String -> a -> a
dbg7 String
"journalInferCommodityStyles" Map AccountName AmountStyle
cs}
commodityStylesFromAmounts :: [Amount] -> Either String (M.Map CommoditySymbol AmountStyle)
commodityStylesFromAmounts :: [Amount] -> Either String (Map AccountName AmountStyle)
commodityStylesFromAmounts =
Map AccountName AmountStyle
-> Either String (Map AccountName AmountStyle)
forall a b. b -> Either a b
Right (Map AccountName AmountStyle
-> Either String (Map AccountName AmountStyle))
-> ([Amount] -> Map AccountName AmountStyle)
-> [Amount]
-> Either String (Map AccountName AmountStyle)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Amount
-> Map AccountName AmountStyle -> Map AccountName AmountStyle)
-> Map AccountName AmountStyle
-> [Amount]
-> Map AccountName AmountStyle
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (\Amount
a -> (AmountStyle -> AmountStyle -> AmountStyle)
-> AccountName
-> AmountStyle
-> Map AccountName AmountStyle
-> Map AccountName AmountStyle
forall k a. Ord k => (a -> a -> a) -> k -> a -> Map k a -> Map k a
M.insertWith AmountStyle -> AmountStyle -> AmountStyle
canonicalStyle (Amount -> AccountName
acommodity Amount
a) (Amount -> AmountStyle
astyle Amount
a)) Map AccountName AmountStyle
forall a. Monoid a => a
mempty
canonicalStyleFrom :: [AmountStyle] -> AmountStyle
canonicalStyleFrom :: [AmountStyle] -> AmountStyle
canonicalStyleFrom [AmountStyle]
ss = (AmountStyle -> AmountStyle -> AmountStyle)
-> AmountStyle -> [AmountStyle] -> AmountStyle
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' AmountStyle -> AmountStyle -> AmountStyle
canonicalStyle AmountStyle
amountstyle [AmountStyle]
ss
canonicalStyle :: AmountStyle -> AmountStyle -> AmountStyle
canonicalStyle :: AmountStyle -> AmountStyle -> AmountStyle
canonicalStyle AmountStyle
a AmountStyle
b = AmountStyle
a{asprecision :: AmountPrecision
asprecision=AmountPrecision
prec, asdecimalpoint :: Maybe DecimalMark
asdecimalpoint=Maybe DecimalMark
decmark, asdigitgroups :: Maybe DigitGroupStyle
asdigitgroups=Maybe DigitGroupStyle
mgrps}
where
prec :: AmountPrecision
prec = AmountPrecision -> AmountPrecision -> AmountPrecision
forall a. Ord a => a -> a -> a
max (AmountStyle -> AmountPrecision
asprecision AmountStyle
a) (AmountStyle -> AmountPrecision
asprecision AmountStyle
b)
mgrps :: Maybe DigitGroupStyle
mgrps = AmountStyle -> Maybe DigitGroupStyle
asdigitgroups AmountStyle
a Maybe DigitGroupStyle
-> Maybe DigitGroupStyle -> Maybe DigitGroupStyle
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> AmountStyle -> Maybe DigitGroupStyle
asdigitgroups AmountStyle
b
defdecmark :: DecimalMark
defdecmark = case Maybe DigitGroupStyle
mgrps of
Just (DigitGroups DecimalMark
'.' [Word8]
_) -> DecimalMark
','
Maybe DigitGroupStyle
_ -> DecimalMark
'.'
decmark :: Maybe DecimalMark
decmark = case Maybe DigitGroupStyle
mgrps of
Just DigitGroupStyle
_ -> DecimalMark -> Maybe DecimalMark
forall a. a -> Maybe a
Just DecimalMark
defdecmark
Maybe DigitGroupStyle
Nothing -> AmountStyle -> Maybe DecimalMark
asdecimalpoint AmountStyle
a Maybe DecimalMark -> Maybe DecimalMark -> Maybe DecimalMark
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> AmountStyle -> Maybe DecimalMark
asdecimalpoint AmountStyle
b Maybe DecimalMark -> Maybe DecimalMark -> Maybe DecimalMark
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> DecimalMark -> Maybe DecimalMark
forall a. a -> Maybe a
Just DecimalMark
defdecmark
journalInferMarketPricesFromTransactions :: Journal -> Journal
journalInferMarketPricesFromTransactions :: Journal -> Journal
journalInferMarketPricesFromTransactions Journal
j =
Journal
j{jinferredmarketprices :: [MarketPrice]
jinferredmarketprices =
String -> [MarketPrice] -> [MarketPrice]
forall a. Show a => String -> a -> a
dbg4 String
"jinferredmarketprices" ([MarketPrice] -> [MarketPrice]) -> [MarketPrice] -> [MarketPrice]
forall a b. (a -> b) -> a -> b
$
(Posting -> Maybe MarketPrice) -> [Posting] -> [MarketPrice]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe Posting -> Maybe MarketPrice
postingInferredmarketPrice ([Posting] -> [MarketPrice]) -> [Posting] -> [MarketPrice]
forall a b. (a -> b) -> a -> b
$ Journal -> [Posting]
journalPostings Journal
j
}
postingInferredmarketPrice :: Posting -> Maybe MarketPrice
postingInferredmarketPrice :: Posting -> Maybe MarketPrice
postingInferredmarketPrice p :: Posting
p@Posting{MixedAmount
pamount :: MixedAmount
pamount :: Posting -> MixedAmount
pamount} =
case MixedAmount -> MixedAmount
mixedAmountTotalPriceToUnitPrice MixedAmount
pamount of
Mixed ( Amount{acommodity :: Amount -> AccountName
acommodity=AccountName
fromcomm, aprice :: Amount -> Maybe AmountPrice
aprice = Just (UnitPrice Amount{acommodity :: Amount -> AccountName
acommodity=AccountName
tocomm, aquantity :: Amount -> Quantity
aquantity=Quantity
rate})} : [Amount]
_) ->
MarketPrice -> Maybe MarketPrice
forall a. a -> Maybe a
Just MarketPrice :: Day -> AccountName -> AccountName -> Quantity -> MarketPrice
MarketPrice {
mpdate :: Day
mpdate = Posting -> Day
postingDate Posting
p
,mpfrom :: AccountName
mpfrom = AccountName
fromcomm
,mpto :: AccountName
mpto = AccountName
tocomm
,mprate :: Quantity
mprate = Quantity
rate
}
MixedAmount
_ -> Maybe MarketPrice
forall a. Maybe a
Nothing
journalToCost :: Journal -> Journal
journalToCost :: Journal -> Journal
journalToCost j :: Journal
j@Journal{jtxns :: Journal -> [Transaction]
jtxns=[Transaction]
ts} = Journal
j{jtxns :: [Transaction]
jtxns=(Transaction -> Transaction) -> [Transaction] -> [Transaction]
forall a b. (a -> b) -> [a] -> [b]
map (Map AccountName AmountStyle -> Transaction -> Transaction
transactionToCost Map AccountName AmountStyle
styles) [Transaction]
ts}
where
styles :: Map AccountName AmountStyle
styles = Journal -> Map AccountName AmountStyle
journalCommodityStyles Journal
j
journalStyleInfluencingAmounts :: Journal -> [Amount]
journalStyleInfluencingAmounts :: Journal -> [Amount]
journalStyleInfluencingAmounts Journal
j =
String -> [Amount] -> [Amount]
forall a. Show a => String -> a -> a
dbg7 String
"journalStyleInfluencingAmounts" ([Amount] -> [Amount]) -> [Amount] -> [Amount]
forall a b. (a -> b) -> a -> b
$
[Maybe Amount] -> [Amount]
forall a. [Maybe a] -> [a]
catMaybes ([Maybe Amount] -> [Amount]) -> [Maybe Amount] -> [Amount]
forall a b. (a -> b) -> a -> b
$ [[Maybe Amount]] -> [Maybe Amount]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [
[Maybe Amount
mdefaultcommodityamt]
,(PriceDirective -> Maybe Amount)
-> [PriceDirective] -> [Maybe Amount]
forall a b. (a -> b) -> [a] -> [b]
map (Amount -> Maybe Amount
forall a. a -> Maybe a
Just (Amount -> Maybe Amount)
-> (PriceDirective -> Amount) -> PriceDirective -> Maybe Amount
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PriceDirective -> Amount
pdamount) ([PriceDirective] -> [Maybe Amount])
-> [PriceDirective] -> [Maybe Amount]
forall a b. (a -> b) -> a -> b
$ Journal -> [PriceDirective]
jpricedirectives Journal
j
,(Amount -> Maybe Amount) -> [Amount] -> [Maybe Amount]
forall a b. (a -> b) -> [a] -> [b]
map Amount -> Maybe Amount
forall a. a -> Maybe a
Just ([Amount] -> [Maybe Amount]) -> [Amount] -> [Maybe Amount]
forall a b. (a -> b) -> a -> b
$ (MixedAmount -> [Amount]) -> [MixedAmount] -> [Amount]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap MixedAmount -> [Amount]
amounts ([MixedAmount] -> [Amount]) -> [MixedAmount] -> [Amount]
forall a b. (a -> b) -> a -> b
$ (Posting -> MixedAmount) -> [Posting] -> [MixedAmount]
forall a b. (a -> b) -> [a] -> [b]
map Posting -> MixedAmount
pamount ([Posting] -> [MixedAmount]) -> [Posting] -> [MixedAmount]
forall a b. (a -> b) -> a -> b
$ Journal -> [Posting]
journalPostings Journal
j
]
where
mdefaultcommodityamt :: Maybe Amount
mdefaultcommodityamt =
case Journal -> Maybe (AccountName, AmountStyle)
jparsedefaultcommodity Journal
j of
Just (AccountName
symbol,AmountStyle
style) -> Amount -> Maybe Amount
forall a. a -> Maybe a
Just Amount
nullamt{acommodity :: AccountName
acommodity=AccountName
symbol,astyle :: AmountStyle
astyle=AmountStyle
style}
Maybe (AccountName, AmountStyle)
Nothing -> Maybe Amount
forall a. Maybe a
Nothing
journalDateSpan :: Bool -> Journal -> DateSpan
journalDateSpan :: Bool -> Journal -> DateSpan
journalDateSpan Bool
False = Maybe WhichDate -> Journal -> DateSpan
journalDateSpanHelper (Maybe WhichDate -> Journal -> DateSpan)
-> Maybe WhichDate -> Journal -> DateSpan
forall a b. (a -> b) -> a -> b
$ WhichDate -> Maybe WhichDate
forall a. a -> Maybe a
Just WhichDate
PrimaryDate
journalDateSpan Bool
True = Maybe WhichDate -> Journal -> DateSpan
journalDateSpanHelper (Maybe WhichDate -> Journal -> DateSpan)
-> Maybe WhichDate -> Journal -> DateSpan
forall a b. (a -> b) -> a -> b
$ WhichDate -> Maybe WhichDate
forall a. a -> Maybe a
Just WhichDate
SecondaryDate
journalDateSpanBothDates :: Journal -> DateSpan
journalDateSpanBothDates :: Journal -> DateSpan
journalDateSpanBothDates = Maybe WhichDate -> Journal -> DateSpan
journalDateSpanHelper Maybe WhichDate
forall a. Maybe a
Nothing
journalDateSpanHelper :: Maybe WhichDate -> Journal -> DateSpan
journalDateSpanHelper :: Maybe WhichDate -> Journal -> DateSpan
journalDateSpanHelper Maybe WhichDate
whichdate Journal
j =
Maybe Day -> Maybe Day -> DateSpan
DateSpan ([Day] -> Maybe Day
forall a. Ord a => [a] -> Maybe a
minimumMay [Day]
dates) (Year -> Day -> Day
addDays Year
1 (Day -> Day) -> Maybe Day -> Maybe Day
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Day] -> Maybe Day
forall a. Ord a => [a] -> Maybe a
maximumMay [Day]
dates)
where
dates :: [Day]
dates = [Day]
pdates [Day] -> [Day] -> [Day]
forall a. [a] -> [a] -> [a]
++ [Day]
tdates
tdates :: [Day]
tdates = (Transaction -> [Day]) -> [Transaction] -> [Day]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap Transaction -> [Day]
gettdate [Transaction]
ts
pdates :: [Day]
pdates = (Posting -> [Day]) -> [Posting] -> [Day]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap Posting -> [Day]
getpdate ([Posting] -> [Day]) -> [Posting] -> [Day]
forall a b. (a -> b) -> a -> b
$ (Transaction -> [Posting]) -> [Transaction] -> [Posting]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap Transaction -> [Posting]
tpostings [Transaction]
ts
ts :: [Transaction]
ts = Journal -> [Transaction]
jtxns Journal
j
gettdate :: Transaction -> [Day]
gettdate Transaction
t = case Maybe WhichDate
whichdate of
Just WhichDate
PrimaryDate -> [Transaction -> Day
tdate Transaction
t]
Just WhichDate
SecondaryDate -> [Day -> Maybe Day -> Day
forall a. a -> Maybe a -> a
fromMaybe (Transaction -> Day
tdate Transaction
t) (Maybe Day -> Day) -> Maybe Day -> Day
forall a b. (a -> b) -> a -> b
$ Transaction -> Maybe Day
tdate2 Transaction
t]
Maybe WhichDate
Nothing -> Transaction -> Day
tdate Transaction
t Day -> [Day] -> [Day]
forall a. a -> [a] -> [a]
: Maybe Day -> [Day]
forall a. Maybe a -> [a]
maybeToList (Transaction -> Maybe Day
tdate2 Transaction
t)
getpdate :: Posting -> [Day]
getpdate Posting
p = case Maybe WhichDate
whichdate of
Just WhichDate
PrimaryDate -> Maybe Day -> [Day]
forall a. Maybe a -> [a]
maybeToList (Maybe Day -> [Day]) -> Maybe Day -> [Day]
forall a b. (a -> b) -> a -> b
$ Posting -> Maybe Day
pdate Posting
p
Just WhichDate
SecondaryDate -> Maybe Day -> [Day]
forall a. Maybe a -> [a]
maybeToList (Maybe Day -> [Day]) -> Maybe Day -> [Day]
forall a b. (a -> b) -> a -> b
$ Posting -> Maybe Day
pdate2 Posting
p Maybe Day -> Maybe Day -> Maybe Day
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Posting -> Maybe Day
pdate Posting
p
Maybe WhichDate
Nothing -> [Maybe Day] -> [Day]
forall a. [Maybe a] -> [a]
catMaybes [Posting -> Maybe Day
pdate Posting
p, Posting -> Maybe Day
pdate2 Posting
p]
journalStartDate :: Bool -> Journal -> Maybe Day
journalStartDate :: Bool -> Journal -> Maybe Day
journalStartDate Bool
secondary Journal
j = Maybe Day
b where DateSpan Maybe Day
b Maybe Day
_ = Bool -> Journal -> DateSpan
journalDateSpan Bool
secondary Journal
j
journalEndDate :: Bool -> Journal -> Maybe Day
journalEndDate :: Bool -> Journal -> Maybe Day
journalEndDate Bool
secondary Journal
j = Maybe Day
e where DateSpan Maybe Day
_ Maybe Day
e = Bool -> Journal -> DateSpan
journalDateSpan Bool
secondary Journal
j
journalPivot :: Text -> Journal -> Journal
journalPivot :: AccountName -> Journal -> Journal
journalPivot AccountName
fieldortagname Journal
j = Journal
j{jtxns :: [Transaction]
jtxns = (Transaction -> Transaction) -> [Transaction] -> [Transaction]
forall a b. (a -> b) -> [a] -> [b]
map (AccountName -> Transaction -> Transaction
transactionPivot AccountName
fieldortagname) ([Transaction] -> [Transaction])
-> (Journal -> [Transaction]) -> Journal -> [Transaction]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Journal -> [Transaction]
jtxns (Journal -> [Transaction]) -> Journal -> [Transaction]
forall a b. (a -> b) -> a -> b
$ Journal
j}
transactionPivot :: Text -> Transaction -> Transaction
transactionPivot :: AccountName -> Transaction -> Transaction
transactionPivot AccountName
fieldortagname Transaction
t = Transaction
t{tpostings :: [Posting]
tpostings = (Posting -> Posting) -> [Posting] -> [Posting]
forall a b. (a -> b) -> [a] -> [b]
map (AccountName -> Posting -> Posting
postingPivot AccountName
fieldortagname) ([Posting] -> [Posting])
-> (Transaction -> [Posting]) -> Transaction -> [Posting]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Transaction -> [Posting]
tpostings (Transaction -> [Posting]) -> Transaction -> [Posting]
forall a b. (a -> b) -> a -> b
$ Transaction
t}
postingPivot :: Text -> Posting -> Posting
postingPivot :: AccountName -> Posting -> Posting
postingPivot AccountName
fieldortagname Posting
p = Posting
p{paccount :: AccountName
paccount = AccountName
pivotedacct, poriginal :: Maybe Posting
poriginal = Posting -> Maybe Posting
forall a. a -> Maybe a
Just (Posting -> Maybe Posting) -> Posting -> Maybe Posting
forall a b. (a -> b) -> a -> b
$ Posting -> Posting
originalPosting Posting
p}
where
pivotedacct :: AccountName
pivotedacct
| Just Transaction
t <- Posting -> Maybe Transaction
ptransaction Posting
p, AccountName
fieldortagname AccountName -> AccountName -> Bool
forall a. Eq a => a -> a -> Bool
== AccountName
"code" = Transaction -> AccountName
tcode Transaction
t
| Just Transaction
t <- Posting -> Maybe Transaction
ptransaction Posting
p, AccountName
fieldortagname AccountName -> AccountName -> Bool
forall a. Eq a => a -> a -> Bool
== AccountName
"description" = Transaction -> AccountName
tdescription Transaction
t
| Just Transaction
t <- Posting -> Maybe Transaction
ptransaction Posting
p, AccountName
fieldortagname AccountName -> AccountName -> Bool
forall a. Eq a => a -> a -> Bool
== AccountName
"payee" = Transaction -> AccountName
transactionPayee Transaction
t
| Just Transaction
t <- Posting -> Maybe Transaction
ptransaction Posting
p, AccountName
fieldortagname AccountName -> AccountName -> Bool
forall a. Eq a => a -> a -> Bool
== AccountName
"note" = Transaction -> AccountName
transactionNote Transaction
t
| Just (AccountName
_, AccountName
value) <- AccountName -> Posting -> Maybe (AccountName, AccountName)
postingFindTag AccountName
fieldortagname Posting
p = AccountName
value
| Bool
otherwise = AccountName
""
postingFindTag :: TagName -> Posting -> Maybe (TagName, TagValue)
postingFindTag :: AccountName -> Posting -> Maybe (AccountName, AccountName)
postingFindTag AccountName
tagname Posting
p = ((AccountName, AccountName) -> Bool)
-> [(AccountName, AccountName)] -> Maybe (AccountName, AccountName)
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
find ((AccountName
tagnameAccountName -> AccountName -> Bool
forall a. Eq a => a -> a -> Bool
==) (AccountName -> Bool)
-> ((AccountName, AccountName) -> AccountName)
-> (AccountName, AccountName)
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (AccountName, AccountName) -> AccountName
forall a b. (a, b) -> a
fst) ([(AccountName, AccountName)] -> Maybe (AccountName, AccountName))
-> [(AccountName, AccountName)] -> Maybe (AccountName, AccountName)
forall a b. (a -> b) -> a -> b
$ Posting -> [(AccountName, AccountName)]
postingAllTags Posting
p
journalApplyAliases :: [AccountAlias] -> Journal -> Either RegexError Journal
journalApplyAliases :: [AccountAlias] -> Journal -> Either String Journal
journalApplyAliases [] Journal
j = Journal -> Either String Journal
forall a b. b -> Either a b
Right Journal
j
journalApplyAliases [AccountAlias]
aliases Journal
j =
case (Transaction -> Either String Transaction)
-> [Transaction] -> Either String [Transaction]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM ([AccountAlias] -> Transaction -> Either String Transaction
transactionApplyAliases [AccountAlias]
aliases) ([Transaction] -> Either String [Transaction])
-> [Transaction] -> Either String [Transaction]
forall a b. (a -> b) -> a -> b
$ Journal -> [Transaction]
jtxns Journal
j of
Right [Transaction]
ts -> Journal -> Either String Journal
forall a b. b -> Either a b
Right Journal
j{jtxns :: [Transaction]
jtxns = [Transaction]
ts}
Left String
err -> String -> Either String Journal
forall a b. a -> Either a b
Left String
err
Right Journal
samplejournal = Bool -> Journal -> Either String Journal
journalBalanceTransactions Bool
False (Journal -> Either String Journal)
-> Journal -> Either String Journal
forall a b. (a -> b) -> a -> b
$
Journal
nulljournal
{jtxns :: [Transaction]
jtxns = [
Transaction -> Transaction
txnTieKnot (Transaction -> Transaction) -> Transaction -> Transaction
forall a b. (a -> b) -> a -> b
$ Transaction :: Year
-> AccountName
-> GenericSourcePos
-> Day
-> Maybe Day
-> Status
-> AccountName
-> AccountName
-> AccountName
-> [(AccountName, AccountName)]
-> [Posting]
-> Transaction
Transaction {
tindex :: Year
tindex=Year
0,
tsourcepos :: GenericSourcePos
tsourcepos=GenericSourcePos
nullsourcepos,
tdate :: Day
tdate=Year -> Int -> Int -> Day
fromGregorian Year
2008 Int
01 Int
01,
tdate2 :: Maybe Day
tdate2=Maybe Day
forall a. Maybe a
Nothing,
tstatus :: Status
tstatus=Status
Unmarked,
tcode :: AccountName
tcode=AccountName
"",
tdescription :: AccountName
tdescription=AccountName
"income",
tcomment :: AccountName
tcomment=AccountName
"",
ttags :: [(AccountName, AccountName)]
ttags=[],
tpostings :: [Posting]
tpostings=
[AccountName
"assets:bank:checking" AccountName -> Amount -> Posting
`post` Quantity -> Amount
usd Quantity
1
,AccountName
"income:salary" AccountName -> Amount -> Posting
`post` Amount
missingamt
],
tprecedingcomment :: AccountName
tprecedingcomment=AccountName
""
}
,
Transaction -> Transaction
txnTieKnot (Transaction -> Transaction) -> Transaction -> Transaction
forall a b. (a -> b) -> a -> b
$ Transaction :: Year
-> AccountName
-> GenericSourcePos
-> Day
-> Maybe Day
-> Status
-> AccountName
-> AccountName
-> AccountName
-> [(AccountName, AccountName)]
-> [Posting]
-> Transaction
Transaction {
tindex :: Year
tindex=Year
0,
tsourcepos :: GenericSourcePos
tsourcepos=GenericSourcePos
nullsourcepos,
tdate :: Day
tdate=Year -> Int -> Int -> Day
fromGregorian Year
2008 Int
06 Int
01,
tdate2 :: Maybe Day
tdate2=Maybe Day
forall a. Maybe a
Nothing,
tstatus :: Status
tstatus=Status
Unmarked,
tcode :: AccountName
tcode=AccountName
"",
tdescription :: AccountName
tdescription=AccountName
"gift",
tcomment :: AccountName
tcomment=AccountName
"",
ttags :: [(AccountName, AccountName)]
ttags=[],
tpostings :: [Posting]
tpostings=
[AccountName
"assets:bank:checking" AccountName -> Amount -> Posting
`post` Quantity -> Amount
usd Quantity
1
,AccountName
"income:gifts" AccountName -> Amount -> Posting
`post` Amount
missingamt
],
tprecedingcomment :: AccountName
tprecedingcomment=AccountName
""
}
,
Transaction -> Transaction
txnTieKnot (Transaction -> Transaction) -> Transaction -> Transaction
forall a b. (a -> b) -> a -> b
$ Transaction :: Year
-> AccountName
-> GenericSourcePos
-> Day
-> Maybe Day
-> Status
-> AccountName
-> AccountName
-> AccountName
-> [(AccountName, AccountName)]
-> [Posting]
-> Transaction
Transaction {
tindex :: Year
tindex=Year
0,
tsourcepos :: GenericSourcePos
tsourcepos=GenericSourcePos
nullsourcepos,
tdate :: Day
tdate=Year -> Int -> Int -> Day
fromGregorian Year
2008 Int
06 Int
02,
tdate2 :: Maybe Day
tdate2=Maybe Day
forall a. Maybe a
Nothing,
tstatus :: Status
tstatus=Status
Unmarked,
tcode :: AccountName
tcode=AccountName
"",
tdescription :: AccountName
tdescription=AccountName
"save",
tcomment :: AccountName
tcomment=AccountName
"",
ttags :: [(AccountName, AccountName)]
ttags=[],
tpostings :: [Posting]
tpostings=
[AccountName
"assets:bank:saving" AccountName -> Amount -> Posting
`post` Quantity -> Amount
usd Quantity
1
,AccountName
"assets:bank:checking" AccountName -> Amount -> Posting
`post` Quantity -> Amount
usd (-Quantity
1)
],
tprecedingcomment :: AccountName
tprecedingcomment=AccountName
""
}
,
Transaction -> Transaction
txnTieKnot (Transaction -> Transaction) -> Transaction -> Transaction
forall a b. (a -> b) -> a -> b
$ Transaction :: Year
-> AccountName
-> GenericSourcePos
-> Day
-> Maybe Day
-> Status
-> AccountName
-> AccountName
-> AccountName
-> [(AccountName, AccountName)]
-> [Posting]
-> Transaction
Transaction {
tindex :: Year
tindex=Year
0,
tsourcepos :: GenericSourcePos
tsourcepos=GenericSourcePos
nullsourcepos,
tdate :: Day
tdate=Year -> Int -> Int -> Day
fromGregorian Year
2008 Int
06 Int
03,
tdate2 :: Maybe Day
tdate2=Maybe Day
forall a. Maybe a
Nothing,
tstatus :: Status
tstatus=Status
Cleared,
tcode :: AccountName
tcode=AccountName
"",
tdescription :: AccountName
tdescription=AccountName
"eat & shop",
tcomment :: AccountName
tcomment=AccountName
"",
ttags :: [(AccountName, AccountName)]
ttags=[],
tpostings :: [Posting]
tpostings=[AccountName
"expenses:food" AccountName -> Amount -> Posting
`post` Quantity -> Amount
usd Quantity
1
,AccountName
"expenses:supplies" AccountName -> Amount -> Posting
`post` Quantity -> Amount
usd Quantity
1
,AccountName
"assets:cash" AccountName -> Amount -> Posting
`post` Amount
missingamt
],
tprecedingcomment :: AccountName
tprecedingcomment=AccountName
""
}
,
Transaction -> Transaction
txnTieKnot (Transaction -> Transaction) -> Transaction -> Transaction
forall a b. (a -> b) -> a -> b
$ Transaction :: Year
-> AccountName
-> GenericSourcePos
-> Day
-> Maybe Day
-> Status
-> AccountName
-> AccountName
-> AccountName
-> [(AccountName, AccountName)]
-> [Posting]
-> Transaction
Transaction {
tindex :: Year
tindex=Year
0,
tsourcepos :: GenericSourcePos
tsourcepos=GenericSourcePos
nullsourcepos,
tdate :: Day
tdate=Year -> Int -> Int -> Day
fromGregorian Year
2008 Int
10 Int
01,
tdate2 :: Maybe Day
tdate2=Maybe Day
forall a. Maybe a
Nothing,
tstatus :: Status
tstatus=Status
Unmarked,
tcode :: AccountName
tcode=AccountName
"",
tdescription :: AccountName
tdescription=AccountName
"take a loan",
tcomment :: AccountName
tcomment=AccountName
"",
ttags :: [(AccountName, AccountName)]
ttags=[],
tpostings :: [Posting]
tpostings=[AccountName
"assets:bank:checking" AccountName -> Amount -> Posting
`post` Quantity -> Amount
usd Quantity
1
,AccountName
"liabilities:debts" AccountName -> Amount -> Posting
`post` Quantity -> Amount
usd (-Quantity
1)
],
tprecedingcomment :: AccountName
tprecedingcomment=AccountName
""
}
,
Transaction -> Transaction
txnTieKnot (Transaction -> Transaction) -> Transaction -> Transaction
forall a b. (a -> b) -> a -> b
$ Transaction :: Year
-> AccountName
-> GenericSourcePos
-> Day
-> Maybe Day
-> Status
-> AccountName
-> AccountName
-> AccountName
-> [(AccountName, AccountName)]
-> [Posting]
-> Transaction
Transaction {
tindex :: Year
tindex=Year
0,
tsourcepos :: GenericSourcePos
tsourcepos=GenericSourcePos
nullsourcepos,
tdate :: Day
tdate=Year -> Int -> Int -> Day
fromGregorian Year
2008 Int
12 Int
31,
tdate2 :: Maybe Day
tdate2=Maybe Day
forall a. Maybe a
Nothing,
tstatus :: Status
tstatus=Status
Unmarked,
tcode :: AccountName
tcode=AccountName
"",
tdescription :: AccountName
tdescription=AccountName
"pay off",
tcomment :: AccountName
tcomment=AccountName
"",
ttags :: [(AccountName, AccountName)]
ttags=[],
tpostings :: [Posting]
tpostings=[AccountName
"liabilities:debts" AccountName -> Amount -> Posting
`post` Quantity -> Amount
usd Quantity
1
,AccountName
"assets:bank:checking" AccountName -> Amount -> Posting
`post` Quantity -> Amount
usd (-Quantity
1)
],
tprecedingcomment :: AccountName
tprecedingcomment=AccountName
""
}
]
}
tests_Journal :: TestTree
tests_Journal = String -> [TestTree] -> TestTree
tests String
"Journal" [
String -> Assertion -> TestTree
test String
"journalDateSpan" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$
Bool -> Journal -> DateSpan
journalDateSpan Bool
True Journal
nulljournal{
jtxns :: [Transaction]
jtxns = [Transaction
nulltransaction{tdate :: Day
tdate = Year -> Int -> Int -> Day
fromGregorian Year
2014 Int
02 Int
01
,tpostings :: [Posting]
tpostings = [Posting
posting{pdate :: Maybe Day
pdate=Day -> Maybe Day
forall a. a -> Maybe a
Just (Year -> Int -> Int -> Day
fromGregorian Year
2014 Int
01 Int
10)}]
}
,Transaction
nulltransaction{tdate :: Day
tdate = Year -> Int -> Int -> Day
fromGregorian Year
2014 Int
09 Int
01
,tpostings :: [Posting]
tpostings = [Posting
posting{pdate2 :: Maybe Day
pdate2=Day -> Maybe Day
forall a. a -> Maybe a
Just (Year -> Int -> Int -> Day
fromGregorian Year
2014 Int
10 Int
10)}]
}
]
}
DateSpan -> DateSpan -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= (Maybe Day -> Maybe Day -> DateSpan
DateSpan (Day -> Maybe Day
forall a. a -> Maybe a
Just (Day -> Maybe Day) -> Day -> Maybe Day
forall a b. (a -> b) -> a -> b
$ Year -> Int -> Int -> Day
fromGregorian Year
2014 Int
1 Int
10) (Day -> Maybe Day
forall a. a -> Maybe a
Just (Day -> Maybe Day) -> Day -> Maybe Day
forall a b. (a -> b) -> a -> b
$ Year -> Int -> Int -> Day
fromGregorian Year
2014 Int
10 Int
11))
,String -> [TestTree] -> TestTree
tests String
"standard account type queries" ([TestTree] -> TestTree) -> [TestTree] -> TestTree
forall a b. (a -> b) -> a -> b
$
let
j :: Journal
j = Journal
samplejournal
journalAccountNamesMatching :: Query -> Journal -> [AccountName]
journalAccountNamesMatching :: Query -> Journal -> [AccountName]
journalAccountNamesMatching Query
q = (AccountName -> Bool) -> [AccountName] -> [AccountName]
forall a. (a -> Bool) -> [a] -> [a]
filter (Query
q Query -> AccountName -> Bool
`matchesAccount`) ([AccountName] -> [AccountName])
-> (Journal -> [AccountName]) -> Journal -> [AccountName]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Journal -> [AccountName]
journalAccountNames
namesfrom :: (Journal -> Query) -> [AccountName]
namesfrom Journal -> Query
qfunc = Query -> Journal -> [AccountName]
journalAccountNamesMatching (Journal -> Query
qfunc Journal
j) Journal
j
in [
String -> Assertion -> TestTree
test String
"assets" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$ String -> [AccountName] -> [AccountName] -> Assertion
forall a.
(Eq a, Show a, HasCallStack) =>
String -> a -> a -> Assertion
assertEqual String
"" [AccountName
"assets",AccountName
"assets:bank",AccountName
"assets:bank:checking",AccountName
"assets:bank:saving",AccountName
"assets:cash"]
((Journal -> Query) -> [AccountName]
namesfrom Journal -> Query
journalAssetAccountQuery)
,String -> Assertion -> TestTree
test String
"cash" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$ String -> [AccountName] -> [AccountName] -> Assertion
forall a.
(Eq a, Show a, HasCallStack) =>
String -> a -> a -> Assertion
assertEqual String
"" [AccountName
"assets",AccountName
"assets:bank",AccountName
"assets:bank:checking",AccountName
"assets:bank:saving",AccountName
"assets:cash"]
((Journal -> Query) -> [AccountName]
namesfrom Journal -> Query
journalCashAccountQuery)
,String -> Assertion -> TestTree
test String
"liabilities" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$ String -> [AccountName] -> [AccountName] -> Assertion
forall a.
(Eq a, Show a, HasCallStack) =>
String -> a -> a -> Assertion
assertEqual String
"" [AccountName
"liabilities",AccountName
"liabilities:debts"]
((Journal -> Query) -> [AccountName]
namesfrom Journal -> Query
journalLiabilityAccountQuery)
,String -> Assertion -> TestTree
test String
"equity" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$ String -> [AccountName] -> [AccountName] -> Assertion
forall a.
(Eq a, Show a, HasCallStack) =>
String -> a -> a -> Assertion
assertEqual String
"" []
((Journal -> Query) -> [AccountName]
namesfrom Journal -> Query
journalEquityAccountQuery)
,String -> Assertion -> TestTree
test String
"income" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$ String -> [AccountName] -> [AccountName] -> Assertion
forall a.
(Eq a, Show a, HasCallStack) =>
String -> a -> a -> Assertion
assertEqual String
"" [AccountName
"income",AccountName
"income:gifts",AccountName
"income:salary"]
((Journal -> Query) -> [AccountName]
namesfrom Journal -> Query
journalRevenueAccountQuery)
,String -> Assertion -> TestTree
test String
"expenses" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$ String -> [AccountName] -> [AccountName] -> Assertion
forall a.
(Eq a, Show a, HasCallStack) =>
String -> a -> a -> Assertion
assertEqual String
"" [AccountName
"expenses",AccountName
"expenses:food",AccountName
"expenses:supplies"]
((Journal -> Query) -> [AccountName]
namesfrom Journal -> Query
journalExpenseAccountQuery)
]
,String -> [TestTree] -> TestTree
tests String
"journalBalanceTransactions" [
String -> Assertion -> TestTree
test String
"balance-assignment" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$ do
let ej :: Either String Journal
ej = Bool -> Journal -> Either String Journal
journalBalanceTransactions Bool
True (Journal -> Either String Journal)
-> Journal -> Either String Journal
forall a b. (a -> b) -> a -> b
$
Journal
nulljournal{ jtxns :: [Transaction]
jtxns = [
Day -> [Posting] -> Transaction
transaction (Year -> Int -> Int -> Day
fromGregorian Year
2019 Int
01 Int
01) [ AccountName -> Amount -> Maybe BalanceAssertion -> Posting
vpost' AccountName
"a" Amount
missingamt (Amount -> Maybe BalanceAssertion
balassert (Quantity -> Amount
num Quantity
1)) ]
]}
Either String Journal -> Assertion
forall a b. (HasCallStack, Eq a, Show a) => Either a b -> Assertion
assertRight Either String Journal
ej
let Right Journal
j = Either String Journal
ej
(Journal -> [Transaction]
jtxns Journal
j [Transaction] -> ([Transaction] -> Transaction) -> Transaction
forall a b. a -> (a -> b) -> b
& [Transaction] -> Transaction
forall a. [a] -> a
head Transaction -> (Transaction -> [Posting]) -> [Posting]
forall a b. a -> (a -> b) -> b
& Transaction -> [Posting]
tpostings [Posting] -> ([Posting] -> Posting) -> Posting
forall a b. a -> (a -> b) -> b
& [Posting] -> Posting
forall a. [a] -> a
head Posting -> (Posting -> MixedAmount) -> MixedAmount
forall a b. a -> (a -> b) -> b
& Posting -> MixedAmount
pamount) MixedAmount -> MixedAmount -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= [Amount] -> MixedAmount
Mixed [Quantity -> Amount
num Quantity
1]
,String -> Assertion -> TestTree
test String
"same-day-1" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$ do
Either String Journal -> Assertion
forall a b. (HasCallStack, Eq a, Show a) => Either a b -> Assertion
assertRight (Either String Journal -> Assertion)
-> Either String Journal -> Assertion
forall a b. (a -> b) -> a -> b
$ Bool -> Journal -> Either String Journal
journalBalanceTransactions Bool
True (Journal -> Either String Journal)
-> Journal -> Either String Journal
forall a b. (a -> b) -> a -> b
$
Journal
nulljournal{ jtxns :: [Transaction]
jtxns = [
Day -> [Posting] -> Transaction
transaction (Year -> Int -> Int -> Day
fromGregorian Year
2019 Int
01 Int
01) [ AccountName -> Amount -> Maybe BalanceAssertion -> Posting
vpost' AccountName
"a" Amount
missingamt (Amount -> Maybe BalanceAssertion
balassert (Quantity -> Amount
num Quantity
1)) ]
,Day -> [Posting] -> Transaction
transaction (Year -> Int -> Int -> Day
fromGregorian Year
2019 Int
01 Int
01) [ AccountName -> Amount -> Maybe BalanceAssertion -> Posting
vpost' AccountName
"a" (Quantity -> Amount
num Quantity
1) (Amount -> Maybe BalanceAssertion
balassert (Quantity -> Amount
num Quantity
2)) ]
]}
,String -> Assertion -> TestTree
test String
"same-day-2" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$ do
Either String Journal -> Assertion
forall a b. (HasCallStack, Eq a, Show a) => Either a b -> Assertion
assertRight (Either String Journal -> Assertion)
-> Either String Journal -> Assertion
forall a b. (a -> b) -> a -> b
$ Bool -> Journal -> Either String Journal
journalBalanceTransactions Bool
True (Journal -> Either String Journal)
-> Journal -> Either String Journal
forall a b. (a -> b) -> a -> b
$
Journal
nulljournal{ jtxns :: [Transaction]
jtxns = [
Day -> [Posting] -> Transaction
transaction (Year -> Int -> Int -> Day
fromGregorian Year
2019 Int
01 Int
01) [ AccountName -> Amount -> Maybe BalanceAssertion -> Posting
vpost' AccountName
"a" (Quantity -> Amount
num Quantity
2) (Amount -> Maybe BalanceAssertion
balassert (Quantity -> Amount
num Quantity
2)) ]
,Day -> [Posting] -> Transaction
transaction (Year -> Int -> Int -> Day
fromGregorian Year
2019 Int
01 Int
01) [
AccountName -> Amount -> Maybe BalanceAssertion -> Posting
post' AccountName
"b" (Quantity -> Amount
num Quantity
1) Maybe BalanceAssertion
forall a. Maybe a
Nothing
,AccountName -> Amount -> Maybe BalanceAssertion -> Posting
post' AccountName
"a" Amount
missingamt Maybe BalanceAssertion
forall a. Maybe a
Nothing
]
,Day -> [Posting] -> Transaction
transaction (Year -> Int -> Int -> Day
fromGregorian Year
2019 Int
01 Int
01) [ AccountName -> Amount -> Maybe BalanceAssertion -> Posting
post' AccountName
"a" (Quantity -> Amount
num Quantity
0) (Amount -> Maybe BalanceAssertion
balassert (Quantity -> Amount
num Quantity
1)) ]
]}
,String -> Assertion -> TestTree
test String
"out-of-order" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$ do
Either String Journal -> Assertion
forall a b. (HasCallStack, Eq a, Show a) => Either a b -> Assertion
assertRight (Either String Journal -> Assertion)
-> Either String Journal -> Assertion
forall a b. (a -> b) -> a -> b
$ Bool -> Journal -> Either String Journal
journalBalanceTransactions Bool
True (Journal -> Either String Journal)
-> Journal -> Either String Journal
forall a b. (a -> b) -> a -> b
$
Journal
nulljournal{ jtxns :: [Transaction]
jtxns = [
Day -> [Posting] -> Transaction
transaction (Year -> Int -> Int -> Day
fromGregorian Year
2019 Int
01 Int
02) [ AccountName -> Amount -> Maybe BalanceAssertion -> Posting
vpost' AccountName
"a" (Quantity -> Amount
num Quantity
1) (Amount -> Maybe BalanceAssertion
balassert (Quantity -> Amount
num Quantity
2)) ]
,Day -> [Posting] -> Transaction
transaction (Year -> Int -> Int -> Day
fromGregorian Year
2019 Int
01 Int
01) [ AccountName -> Amount -> Maybe BalanceAssertion -> Posting
vpost' AccountName
"a" (Quantity -> Amount
num Quantity
1) (Amount -> Maybe BalanceAssertion
balassert (Quantity -> Amount
num Quantity
1)) ]
]}
]
,String -> [TestTree] -> TestTree
tests String
"commodityStylesFromAmounts" ([TestTree] -> TestTree) -> [TestTree] -> TestTree
forall a b. (a -> b) -> a -> b
$ [
String -> Assertion -> TestTree
test String
"1091a" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$ do
[Amount] -> Either String (Map AccountName AmountStyle)
commodityStylesFromAmounts [
Amount
nullamt{aquantity :: Quantity
aquantity=Quantity
1000, astyle :: AmountStyle
astyle=Side
-> Bool
-> AmountPrecision
-> Maybe DecimalMark
-> Maybe DigitGroupStyle
-> AmountStyle
AmountStyle Side
L Bool
False (Word8 -> AmountPrecision
Precision Word8
3) (DecimalMark -> Maybe DecimalMark
forall a. a -> Maybe a
Just DecimalMark
',') Maybe DigitGroupStyle
forall a. Maybe a
Nothing}
,Amount
nullamt{aquantity :: Quantity
aquantity=Quantity
1000, astyle :: AmountStyle
astyle=Side
-> Bool
-> AmountPrecision
-> Maybe DecimalMark
-> Maybe DigitGroupStyle
-> AmountStyle
AmountStyle Side
L Bool
False (Word8 -> AmountPrecision
Precision Word8
2) (DecimalMark -> Maybe DecimalMark
forall a. a -> Maybe a
Just DecimalMark
'.') (DigitGroupStyle -> Maybe DigitGroupStyle
forall a. a -> Maybe a
Just (DecimalMark -> [Word8] -> DigitGroupStyle
DigitGroups DecimalMark
',' [Word8
3]))}
]
Either String (Map AccountName AmountStyle)
-> Either String (Map AccountName AmountStyle) -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?=
Map AccountName AmountStyle
-> Either String (Map AccountName AmountStyle)
forall a b. b -> Either a b
Right ([(AccountName, AmountStyle)] -> Map AccountName AmountStyle
forall k a. Ord k => [(k, a)] -> Map k a
M.fromList [
(AccountName
"", Side
-> Bool
-> AmountPrecision
-> Maybe DecimalMark
-> Maybe DigitGroupStyle
-> AmountStyle
AmountStyle Side
L Bool
False (Word8 -> AmountPrecision
Precision Word8
3) (DecimalMark -> Maybe DecimalMark
forall a. a -> Maybe a
Just DecimalMark
'.') (DigitGroupStyle -> Maybe DigitGroupStyle
forall a. a -> Maybe a
Just (DecimalMark -> [Word8] -> DigitGroupStyle
DigitGroups DecimalMark
',' [Word8
3])))
])
,String -> Assertion -> TestTree
test String
"1091b" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$ do
[Amount] -> Either String (Map AccountName AmountStyle)
commodityStylesFromAmounts [
Amount
nullamt{aquantity :: Quantity
aquantity=Quantity
1000, astyle :: AmountStyle
astyle=Side
-> Bool
-> AmountPrecision
-> Maybe DecimalMark
-> Maybe DigitGroupStyle
-> AmountStyle
AmountStyle Side
L Bool
False (Word8 -> AmountPrecision
Precision Word8
2) (DecimalMark -> Maybe DecimalMark
forall a. a -> Maybe a
Just DecimalMark
'.') (DigitGroupStyle -> Maybe DigitGroupStyle
forall a. a -> Maybe a
Just (DecimalMark -> [Word8] -> DigitGroupStyle
DigitGroups DecimalMark
',' [Word8
3]))}
,Amount
nullamt{aquantity :: Quantity
aquantity=Quantity
1000, astyle :: AmountStyle
astyle=Side
-> Bool
-> AmountPrecision
-> Maybe DecimalMark
-> Maybe DigitGroupStyle
-> AmountStyle
AmountStyle Side
L Bool
False (Word8 -> AmountPrecision
Precision Word8
3) (DecimalMark -> Maybe DecimalMark
forall a. a -> Maybe a
Just DecimalMark
',') Maybe DigitGroupStyle
forall a. Maybe a
Nothing}
]
Either String (Map AccountName AmountStyle)
-> Either String (Map AccountName AmountStyle) -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?=
Map AccountName AmountStyle
-> Either String (Map AccountName AmountStyle)
forall a b. b -> Either a b
Right ([(AccountName, AmountStyle)] -> Map AccountName AmountStyle
forall k a. Ord k => [(k, a)] -> Map k a
M.fromList [
(AccountName
"", Side
-> Bool
-> AmountPrecision
-> Maybe DecimalMark
-> Maybe DigitGroupStyle
-> AmountStyle
AmountStyle Side
L Bool
False (Word8 -> AmountPrecision
Precision Word8
3) (DecimalMark -> Maybe DecimalMark
forall a. a -> Maybe a
Just DecimalMark
'.') (DigitGroupStyle -> Maybe DigitGroupStyle
forall a. a -> Maybe a
Just (DecimalMark -> [Word8] -> DigitGroupStyle
DigitGroups DecimalMark
',' [Word8
3])))
])
]
]