2016-12-10 18:04:48 +03:00
|
|
|
{-# LANGUAGE Rank2Types #-}
|
2019-01-06 10:05:14 +03:00
|
|
|
{-# LANGUAGE NamedFieldPuns #-}
|
|
|
|
{-# LANGUAGE StandaloneDeriving #-}
|
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
2018-03-12 12:29:24 +03:00
|
|
|
{-# LANGUAGE CPP #-}
|
|
|
|
|
2008-10-03 06:04:15 +04:00
|
|
|
{-|
|
|
|
|
|
2012-04-14 02:24:55 +04:00
|
|
|
A 'Journal' is a set of transactions, plus optional related data. This is
|
|
|
|
hledger's primary data object. It is usually parsed from a journal file or
|
|
|
|
other data format (see "Hledger.Read").
|
2008-10-03 06:04:15 +04:00
|
|
|
|
|
|
|
-}
|
|
|
|
|
2012-04-14 02:24:55 +04:00
|
|
|
module Hledger.Data.Journal (
|
|
|
|
-- * Parsing helpers
|
2015-08-10 02:20:02 +03:00
|
|
|
addMarketPrice,
|
2018-07-30 21:29:45 +03:00
|
|
|
addTransactionModifier,
|
2012-04-14 02:24:55 +04:00
|
|
|
addPeriodicTransaction,
|
|
|
|
addTransaction,
|
2012-05-27 22:14:20 +04:00
|
|
|
journalBalanceTransactions,
|
2015-11-22 20:21:36 +03:00
|
|
|
journalApplyCommodityStyles,
|
|
|
|
commodityStylesFromAmounts,
|
2018-04-20 22:18:28 +03:00
|
|
|
journalCommodityStyles,
|
2012-04-14 02:24:55 +04:00
|
|
|
journalConvertAmountsToCost,
|
2018-10-12 17:01:39 +03:00
|
|
|
journalReverse,
|
2018-10-16 18:51:51 +03:00
|
|
|
journalSetLastReadTime,
|
2017-09-05 20:48:35 +03:00
|
|
|
journalPivot,
|
2012-04-14 02:24:55 +04:00
|
|
|
-- * Filtering
|
|
|
|
filterJournalTransactions,
|
2014-02-28 05:47:47 +04:00
|
|
|
filterJournalPostings,
|
2014-05-24 00:10:36 +04:00
|
|
|
filterJournalAmounts,
|
|
|
|
filterTransactionAmounts,
|
2016-06-01 22:09:16 +03:00
|
|
|
filterTransactionPostings,
|
2014-02-28 05:47:47 +04:00
|
|
|
filterPostingAmount,
|
2012-04-14 02:24:55 +04:00
|
|
|
-- * Querying
|
|
|
|
journalAccountNamesUsed,
|
2017-12-29 23:18:04 +03:00
|
|
|
journalAccountNamesImplied,
|
|
|
|
journalAccountNamesDeclared,
|
|
|
|
journalAccountNamesDeclaredOrUsed,
|
|
|
|
journalAccountNamesDeclaredOrImplied,
|
|
|
|
journalAccountNames,
|
2012-11-20 01:20:10 +04:00
|
|
|
-- journalAmountAndPriceCommodities,
|
2012-04-14 02:24:55 +04:00
|
|
|
journalAmounts,
|
2017-02-04 11:08:00 +03:00
|
|
|
overJournalAmounts,
|
|
|
|
traverseJournalAmounts,
|
2012-11-20 01:20:10 +04:00
|
|
|
-- journalCanonicalCommodities,
|
2012-04-14 02:24:55 +04:00
|
|
|
journalDateSpan,
|
2014-02-28 05:47:47 +04:00
|
|
|
journalDescriptions,
|
2012-04-14 02:24:55 +04:00
|
|
|
journalFilePath,
|
|
|
|
journalFilePaths,
|
2015-10-30 06:12:46 +03:00
|
|
|
journalTransactionAt,
|
|
|
|
journalNextTransaction,
|
|
|
|
journalPrevTransaction,
|
2012-04-14 02:24:55 +04:00
|
|
|
journalPostings,
|
2012-04-14 05:12:42 +04:00
|
|
|
-- * Standard account types
|
2012-05-16 11:37:24 +04:00
|
|
|
journalBalanceSheetAccountQuery,
|
|
|
|
journalProfitAndLossAccountQuery,
|
2018-09-27 23:54:16 +03:00
|
|
|
journalRevenueAccountQuery,
|
2012-04-15 04:05:10 +04:00
|
|
|
journalExpenseAccountQuery,
|
|
|
|
journalAssetAccountQuery,
|
|
|
|
journalLiabilityAccountQuery,
|
|
|
|
journalEquityAccountQuery,
|
2012-04-17 21:32:56 +04:00
|
|
|
journalCashAccountQuery,
|
2012-04-14 02:24:55 +04:00
|
|
|
-- * Misc
|
2015-11-22 20:21:36 +03:00
|
|
|
canonicalStyleFrom,
|
2012-04-14 02:24:55 +04:00
|
|
|
matchpats,
|
|
|
|
nulljournal,
|
2016-07-07 01:08:57 +03:00
|
|
|
journalCheckBalanceAssertions,
|
2016-08-14 22:44:19 +03:00
|
|
|
journalNumberAndTieTransactions,
|
|
|
|
journalUntieTransactions,
|
2019-02-01 22:31:04 +03:00
|
|
|
journalModifyTransactions,
|
2012-04-14 02:24:55 +04:00
|
|
|
-- * Tests
|
2012-05-27 22:14:20 +04:00
|
|
|
samplejournal,
|
2018-09-06 23:08:26 +03:00
|
|
|
tests_Journal,
|
2012-04-14 02:24:55 +04:00
|
|
|
)
|
2007-07-02 23:15:39 +04:00
|
|
|
where
|
2017-02-04 11:08:00 +03:00
|
|
|
import Control.Applicative (Const(..))
|
2016-12-10 18:04:48 +03:00
|
|
|
import Control.Arrow
|
2013-05-29 03:25:00 +04:00
|
|
|
import Control.Monad
|
2016-12-10 18:04:48 +03:00
|
|
|
import Control.Monad.Except
|
|
|
|
import qualified Control.Monad.Reader as R
|
|
|
|
import Control.Monad.ST
|
|
|
|
import Data.Array.ST
|
2017-02-04 11:08:00 +03:00
|
|
|
import Data.Functor.Identity (Identity(..))
|
2016-12-10 18:04:48 +03:00
|
|
|
import qualified Data.HashTable.ST.Cuckoo as HT
|
2011-05-28 08:11:44 +04:00
|
|
|
import Data.List
|
2017-11-28 04:22:44 +03:00
|
|
|
import Data.List.Extra (groupSort)
|
2013-05-29 03:25:00 +04:00
|
|
|
import Data.Maybe
|
2018-03-25 01:51:56 +03:00
|
|
|
#if !(MIN_VERSION_base(4,11,0))
|
2016-05-19 01:08:50 +03:00
|
|
|
import Data.Monoid
|
2018-03-25 01:51:56 +03:00
|
|
|
#endif
|
2011-05-28 08:11:44 +04:00
|
|
|
import Data.Ord
|
2018-03-12 12:29:24 +03:00
|
|
|
import qualified Data.Semigroup as Sem
|
lib: textification: saved journal source
Slightly worse on small files, better on large ones.
hledger -f data/100x100x10.journal stats
<<ghc: 39305392 bytes, 77 GCs, 196354/268584 avg/max bytes residency (3 samples), 2M in use, 0.000 INIT (0.007 elapsed), 0.014 MUT (0.027 elapsed), 0.011 GC (0.111 elapsed) :ghc>>
<<ghc: 39307728 bytes, 77 GCs, 196909/270248 avg/max bytes residency (3 samples), 2M in use, 0.001 INIT (0.010 elapsed), 0.015 MUT (0.028 elapsed), 0.012 GC (0.116 elapsed) :ghc>>
hledger -f data/1000x1000x10.journal stats
<<ghc: 314285912 bytes, 612 GCs, 2064811/6597608 avg/max bytes residency (7 samples), 16M in use, 0.000 INIT (0.002 elapsed), 0.126 MUT (0.134 elapsed), 0.059 GC (0.069 elapsed) :ghc>>
<<ghc: 314271368 bytes, 612 GCs, 2070227/6628024 avg/max bytes residency (7 samples), 16M in use, 0.000 INIT (0.000 elapsed), 0.137 MUT (0.143 elapsed), 0.059 GC (0.068 elapsed) :ghc>>
hledger -f data/10000x1000x10.journal stats
<<ghc: 3070033264 bytes, 5965 GCs, 12699294/62962464 avg/max bytes residency (10 samples), 124M in use, 0.000 INIT (0.000 elapsed), 1.245 MUT (1.300 elapsed), 0.498 GC (0.558 elapsed) :ghc>>
<<ghc: 3070006752 bytes, 5973 GCs, 12687314/62848920 avg/max bytes residency (10 samples), 124M in use, 0.000 INIT (0.003 elapsed), 1.257 MUT (1.281 elapsed), 0.496 GC (0.554 elapsed) :ghc>>
hledger -f data/100000x1000x10.journal stats
<<ghc: 30753465088 bytes, 59763 GCs, 117723618/666643528 avg/max bytes residency (14 samples), 1589M in use, 0.000 INIT (0.002 elapsed), 12.536 MUT (12.793 elapsed), 5.978 GC (7.155 elapsed) :ghc>>
<<ghc: 30753367256 bytes, 59811 GCs, 117723236/666627528 avg/max bytes residency (14 samples), 1590M in use, 0.001 INIT (0.012 elapsed), 12.923 MUT (13.169 elapsed), 5.981 GC (6.860 elapsed) :ghc>>
2016-05-24 05:24:39 +03:00
|
|
|
import Data.Text (Text)
|
lib: textification begins! account names
The first of several conversions from String to (strict) Text, hopefully
reducing space and time usage.
This one shows a small improvement, with GHC 7.10.3 and text-1.2.2.1:
hledger -f data/100x100x10.journal stats
string: <<ghc: 39471064 bytes, 77 GCs, 198421/275048 avg/max bytes residency (3 samples), 2M in use, 0.000 INIT (0.001 elapsed), 0.015 MUT (0.020 elapsed), 0.010 GC (0.014 elapsed) :ghc>>
text: <<ghc: 39268024 bytes, 77 GCs, 197018/270840 avg/max bytes residency (3 samples), 2M in use, 0.000 INIT (0.002 elapsed), 0.016 MUT (0.022 elapsed), 0.009 GC (0.011 elapsed) :ghc>>
hledger -f data/1000x100x10.journal stats
string: <<ghc: 318555920 bytes, 617 GCs, 2178997/7134472 avg/max bytes residency (7 samples), 16M in use, 0.000 INIT (0.001 elapsed), 0.129 MUT (0.136 elapsed), 0.067 GC (0.077 elapsed) :ghc>>
text: <<ghc: 314248496 bytes, 612 GCs, 2074045/6617960 avg/max bytes residency (7 samples), 16M in use, 0.000 INIT (0.003 elapsed), 0.137 MUT (0.145 elapsed), 0.067 GC (0.079 elapsed) :ghc>>
hledger -f data/10000x100x10.journal stats
string: <<ghc: 3114763608 bytes, 6026 GCs, 18858950/75552024 avg/max bytes residency (11 samples), 201M in use, 0.000 INIT (0.000 elapsed), 1.331 MUT (1.372 elapsed), 0.699 GC (0.812 elapsed) :ghc>>
text: <<ghc: 3071468920 bytes, 5968 GCs, 14120344/62951360 avg/max bytes residency (9 samples), 124M in use, 0.000 INIT (0.003 elapsed), 1.272 MUT (1.349 elapsed), 0.513 GC (0.578 elapsed) :ghc>>
hledger -f data/100000x100x10.journal stats
string: <<ghc: 31186579432 bytes, 60278 GCs, 135332581/740228992 avg/max bytes residency (13 samples), 1697M in use, 0.000 INIT (0.008 elapsed), 14.677 MUT (15.508 elapsed), 7.081 GC (8.074 elapsed) :ghc>>
text: <<ghc: 30753427672 bytes, 59763 GCs, 117595958/666457240 avg/max bytes residency (14 samples), 1588M in use, 0.000 INIT (0.008 elapsed), 13.713 MUT (13.966 elapsed), 6.220 GC (7.108 elapsed) :ghc>>
2016-05-24 04:16:21 +03:00
|
|
|
import qualified Data.Text as T
|
2016-02-21 13:37:59 +03:00
|
|
|
import Safe (headMay, headDef)
|
2011-05-28 08:11:44 +04:00
|
|
|
import Data.Time.Calendar
|
|
|
|
import Data.Tree
|
2009-08-12 13:21:46 +04:00
|
|
|
import System.Time (ClockTime(TOD))
|
2011-05-28 08:11:44 +04:00
|
|
|
import Text.Printf
|
2012-11-20 01:20:10 +04:00
|
|
|
import qualified Data.Map as M
|
2018-10-18 19:54:55 +03:00
|
|
|
import qualified Data.Set as S
|
2011-05-28 08:11:44 +04:00
|
|
|
|
2018-09-04 23:52:36 +03:00
|
|
|
import Hledger.Utils
|
2010-05-20 03:08:53 +04:00
|
|
|
import Hledger.Data.Types
|
|
|
|
import Hledger.Data.AccountName
|
|
|
|
import Hledger.Data.Amount
|
2012-05-27 22:14:20 +04:00
|
|
|
import Hledger.Data.Dates
|
|
|
|
import Hledger.Data.Transaction
|
2019-02-01 22:31:04 +03:00
|
|
|
import Hledger.Data.TransactionModifier
|
2010-05-20 03:08:53 +04:00
|
|
|
import Hledger.Data.Posting
|
2012-05-16 11:57:10 +04:00
|
|
|
import Hledger.Query
|
2007-07-02 23:15:39 +04:00
|
|
|
|
|
|
|
|
2016-05-23 10:32:55 +03:00
|
|
|
-- try to make Journal ppShow-compatible
|
|
|
|
-- instance Show ClockTime where
|
|
|
|
-- show t = "<ClockTime>"
|
|
|
|
-- deriving instance Show Journal
|
|
|
|
|
2009-12-16 10:00:43 +03:00
|
|
|
instance Show Journal where
|
2013-12-07 01:51:19 +04:00
|
|
|
show j
|
|
|
|
| debugLevel < 3 = printf "Journal %s with %d transactions, %d accounts"
|
|
|
|
(journalFilePath j)
|
2018-03-29 18:45:34 +03:00
|
|
|
(length $ jtxns j)
|
2013-12-07 01:51:19 +04:00
|
|
|
(length accounts)
|
|
|
|
| debugLevel < 6 = printf "Journal %s with %d transactions, %d accounts: %s"
|
|
|
|
(journalFilePath j)
|
2018-03-29 18:45:34 +03:00
|
|
|
(length $ jtxns j)
|
2013-12-07 01:51:19 +04:00
|
|
|
(length accounts)
|
|
|
|
(show accounts)
|
|
|
|
| otherwise = printf "Journal %s with %d transactions, %d accounts: %s, commodity styles: %s"
|
2010-09-24 05:56:11 +04:00
|
|
|
(journalFilePath j)
|
2018-03-29 18:45:34 +03:00
|
|
|
(length $ jtxns j)
|
2008-10-09 13:25:58 +04:00
|
|
|
(length accounts)
|
|
|
|
(show accounts)
|
2016-05-23 10:32:55 +03:00
|
|
|
(show $ jinferredcommodities j)
|
2009-12-16 10:00:43 +03:00
|
|
|
-- ++ (show $ journalTransactions l)
|
2015-06-29 00:14:56 +03:00
|
|
|
where accounts = filter (/= "root") $ flatten $ journalAccountNameTree j
|
2008-10-03 16:10:05 +04:00
|
|
|
|
2012-04-14 02:24:55 +04:00
|
|
|
-- showJournalDebug j = unlines [
|
|
|
|
-- show j
|
|
|
|
-- ,show (jtxns j)
|
2018-07-30 21:29:45 +03:00
|
|
|
-- ,show (jtxnmodifiers j)
|
2012-04-14 02:24:55 +04:00
|
|
|
-- ,show (jperiodictxns j)
|
2016-05-23 10:32:55 +03:00
|
|
|
-- ,show $ jparsetimeclockentries j
|
2015-08-10 02:20:02 +03:00
|
|
|
-- ,show $ jmarketprices j
|
2016-05-23 10:32:55 +03:00
|
|
|
-- ,show $ jfinalcommentlines j
|
2016-05-19 06:57:34 +03:00
|
|
|
-- ,show $ jparsestate j
|
2016-05-23 10:32:55 +03:00
|
|
|
-- ,show $ map fst $ jfiles j
|
2012-04-14 02:24:55 +04:00
|
|
|
-- ]
|
2011-08-03 03:27:41 +04:00
|
|
|
|
2016-05-23 10:32:55 +03:00
|
|
|
-- The monoid instance for Journal is useful for two situations.
|
|
|
|
--
|
|
|
|
-- 1. concatenating finalised journals, eg with multiple -f options:
|
|
|
|
-- FIRST <> SECOND. The second's list fields are appended to the
|
|
|
|
-- first's, map fields are combined, transaction counts are summed,
|
|
|
|
-- the parse state of the second is kept.
|
|
|
|
--
|
|
|
|
-- 2. merging a child parsed journal, eg with the include directive:
|
|
|
|
-- CHILD <> PARENT. A parsed journal's data is in reverse order, so
|
|
|
|
-- this gives what we want.
|
|
|
|
--
|
2018-03-12 12:29:24 +03:00
|
|
|
instance Sem.Semigroup Journal where
|
|
|
|
j1 <> j2 = Journal {
|
2016-05-23 10:32:55 +03:00
|
|
|
jparsedefaultyear = jparsedefaultyear j2
|
|
|
|
,jparsedefaultcommodity = jparsedefaultcommodity j2
|
|
|
|
,jparseparentaccounts = jparseparentaccounts j2
|
|
|
|
,jparsealiases = jparsealiases j2
|
2016-08-14 22:44:19 +03:00
|
|
|
-- ,jparsetransactioncount = jparsetransactioncount j1 + jparsetransactioncount j2
|
journal: account directives can declare account types
Previously you had to use one of the standard english account names
(assets, liabilities..) for top-level accounts, if you wanted to use
the bs/bse/cf/is commands.
Now, account directives can specify which of the big five categories
an account belongs to - asset, liability, equity, revenue or expense -
by writing one of the letters A, L, E, R or X two or more spaces after
the account name (where the numeric account code used to be).
This might change. Some thoughts influencing the current syntax:
- easy to type and read
- does not require multiple lines
- does not depend on any particular account numbering scheme
- allows more types later if needed
- still anglocentric, but only a little
- could be treated as syntactic sugar for account tags later
- seems to be compatible with (ignored by) current Ledger
The current design permits unlimited account type declarations anywhere
in the account tree. So you could declare a liability account somewhere
under assets, and maybe a revenue account under that, and another asset
account even further down. In such cases you start to see oddities like
accounts appearing in multiple places in a tree-mode report. In theory
the reports will still behave reasonably, but this has not been tested
too hard. In any case this is clearly too much freedom. I have left it
this way, for now, in case it helps with:
- modelling contra accounts ?
- multiple files. I suspect the extra expressiveness may come in handy
when combining multiple files with account type declarations,
rewriting account names, apply parent accounts etc.
If we only allowed type declarations on top-level accounts, or
only allowed a single account of each type, complications seem likely.
2018-09-27 04:34:48 +03:00
|
|
|
,jparsetimeclockentries = jparsetimeclockentries j1 <> jparsetimeclockentries j2
|
2018-09-30 05:33:17 +03:00
|
|
|
,jincludefilestack = jincludefilestack j2
|
journal: account directives can declare account types
Previously you had to use one of the standard english account names
(assets, liabilities..) for top-level accounts, if you wanted to use
the bs/bse/cf/is commands.
Now, account directives can specify which of the big five categories
an account belongs to - asset, liability, equity, revenue or expense -
by writing one of the letters A, L, E, R or X two or more spaces after
the account name (where the numeric account code used to be).
This might change. Some thoughts influencing the current syntax:
- easy to type and read
- does not require multiple lines
- does not depend on any particular account numbering scheme
- allows more types later if needed
- still anglocentric, but only a little
- could be treated as syntactic sugar for account tags later
- seems to be compatible with (ignored by) current Ledger
The current design permits unlimited account type declarations anywhere
in the account tree. So you could declare a liability account somewhere
under assets, and maybe a revenue account under that, and another asset
account even further down. In such cases you start to see oddities like
accounts appearing in multiple places in a tree-mode report. In theory
the reports will still behave reasonably, but this has not been tested
too hard. In any case this is clearly too much freedom. I have left it
this way, for now, in case it helps with:
- modelling contra accounts ?
- multiple files. I suspect the extra expressiveness may come in handy
when combining multiple files with account type declarations,
rewriting account names, apply parent accounts etc.
If we only allowed type declarations on top-level accounts, or
only allowed a single account of each type, complications seem likely.
2018-09-27 04:34:48 +03:00
|
|
|
,jdeclaredaccounts = jdeclaredaccounts j1 <> jdeclaredaccounts j2
|
|
|
|
,jdeclaredaccounttypes = jdeclaredaccounttypes j1 <> jdeclaredaccounttypes j2
|
2016-05-23 10:32:55 +03:00
|
|
|
,jcommodities = jcommodities j1 <> jcommodities j2
|
|
|
|
,jinferredcommodities = jinferredcommodities j1 <> jinferredcommodities j2
|
|
|
|
,jmarketprices = jmarketprices j1 <> jmarketprices j2
|
2018-07-30 21:29:45 +03:00
|
|
|
,jtxnmodifiers = jtxnmodifiers j1 <> jtxnmodifiers j2
|
2016-05-23 10:32:55 +03:00
|
|
|
,jperiodictxns = jperiodictxns j1 <> jperiodictxns j2
|
|
|
|
,jtxns = jtxns j1 <> jtxns j2
|
journal: account directives can declare account types
Previously you had to use one of the standard english account names
(assets, liabilities..) for top-level accounts, if you wanted to use
the bs/bse/cf/is commands.
Now, account directives can specify which of the big five categories
an account belongs to - asset, liability, equity, revenue or expense -
by writing one of the letters A, L, E, R or X two or more spaces after
the account name (where the numeric account code used to be).
This might change. Some thoughts influencing the current syntax:
- easy to type and read
- does not require multiple lines
- does not depend on any particular account numbering scheme
- allows more types later if needed
- still anglocentric, but only a little
- could be treated as syntactic sugar for account tags later
- seems to be compatible with (ignored by) current Ledger
The current design permits unlimited account type declarations anywhere
in the account tree. So you could declare a liability account somewhere
under assets, and maybe a revenue account under that, and another asset
account even further down. In such cases you start to see oddities like
accounts appearing in multiple places in a tree-mode report. In theory
the reports will still behave reasonably, but this has not been tested
too hard. In any case this is clearly too much freedom. I have left it
this way, for now, in case it helps with:
- modelling contra accounts ?
- multiple files. I suspect the extra expressiveness may come in handy
when combining multiple files with account type declarations,
rewriting account names, apply parent accounts etc.
If we only allowed type declarations on top-level accounts, or
only allowed a single account of each type, complications seem likely.
2018-09-27 04:34:48 +03:00
|
|
|
,jfinalcommentlines = jfinalcommentlines j2 -- XXX discards j1's ?
|
2016-05-23 10:32:55 +03:00
|
|
|
,jfiles = jfiles j1 <> jfiles j2
|
|
|
|
,jlastreadtime = max (jlastreadtime j1) (jlastreadtime j2)
|
|
|
|
}
|
2016-05-19 01:08:50 +03:00
|
|
|
|
2018-03-12 12:29:24 +03:00
|
|
|
instance Monoid Journal where
|
|
|
|
mempty = nulljournal
|
|
|
|
#if !(MIN_VERSION_base(4,11,0))
|
|
|
|
-- This is redundant starting with base-4.11 / GHC 8.4.
|
|
|
|
mappend = (Sem.<>)
|
|
|
|
#endif
|
|
|
|
|
2009-12-20 18:50:54 +03:00
|
|
|
nulljournal :: Journal
|
2016-05-23 10:32:55 +03:00
|
|
|
nulljournal = Journal {
|
|
|
|
jparsedefaultyear = Nothing
|
|
|
|
,jparsedefaultcommodity = Nothing
|
|
|
|
,jparseparentaccounts = []
|
|
|
|
,jparsealiases = []
|
2016-08-14 22:44:19 +03:00
|
|
|
-- ,jparsetransactioncount = 0
|
2018-09-30 05:33:17 +03:00
|
|
|
,jparsetimeclockentries = []
|
|
|
|
,jincludefilestack = []
|
|
|
|
,jdeclaredaccounts = []
|
journal: account directives can declare account types
Previously you had to use one of the standard english account names
(assets, liabilities..) for top-level accounts, if you wanted to use
the bs/bse/cf/is commands.
Now, account directives can specify which of the big five categories
an account belongs to - asset, liability, equity, revenue or expense -
by writing one of the letters A, L, E, R or X two or more spaces after
the account name (where the numeric account code used to be).
This might change. Some thoughts influencing the current syntax:
- easy to type and read
- does not require multiple lines
- does not depend on any particular account numbering scheme
- allows more types later if needed
- still anglocentric, but only a little
- could be treated as syntactic sugar for account tags later
- seems to be compatible with (ignored by) current Ledger
The current design permits unlimited account type declarations anywhere
in the account tree. So you could declare a liability account somewhere
under assets, and maybe a revenue account under that, and another asset
account even further down. In such cases you start to see oddities like
accounts appearing in multiple places in a tree-mode report. In theory
the reports will still behave reasonably, but this has not been tested
too hard. In any case this is clearly too much freedom. I have left it
this way, for now, in case it helps with:
- modelling contra accounts ?
- multiple files. I suspect the extra expressiveness may come in handy
when combining multiple files with account type declarations,
rewriting account names, apply parent accounts etc.
If we only allowed type declarations on top-level accounts, or
only allowed a single account of each type, complications seem likely.
2018-09-27 04:34:48 +03:00
|
|
|
,jdeclaredaccounttypes = M.empty
|
|
|
|
,jcommodities = M.empty
|
|
|
|
,jinferredcommodities = M.empty
|
2016-05-23 10:32:55 +03:00
|
|
|
,jmarketprices = []
|
2018-07-30 21:29:45 +03:00
|
|
|
,jtxnmodifiers = []
|
2016-05-23 10:32:55 +03:00
|
|
|
,jperiodictxns = []
|
|
|
|
,jtxns = []
|
lib: textification: comments and tags
No change.
hledger -f data/100x100x10.journal stats
<<ghc: 42859576 bytes, 84 GCs, 193781/269984 avg/max bytes residency (3 samples), 2M in use, 0.000 INIT (0.001 elapsed), 0.016 MUT (0.020 elapsed), 0.009 GC (0.011 elapsed) :ghc>>
<<ghc: 42859576 bytes, 84 GCs, 193781/269984 avg/max bytes residency (3 samples), 2M in use, 0.000 INIT (0.001 elapsed), 0.015 MUT (0.018 elapsed), 0.009 GC (0.013 elapsed) :ghc>>
hledger -f data/1000x1000x10.journal stats
<<ghc: 349576344 bytes, 681 GCs, 1407388/4091680 avg/max bytes residency (7 samples), 11M in use, 0.000 INIT (0.000 elapsed), 0.124 MUT (0.130 elapsed), 0.047 GC (0.055 elapsed) :ghc>>
<<ghc: 349576280 bytes, 681 GCs, 1407388/4091680 avg/max bytes residency (7 samples), 11M in use, 0.000 INIT (0.000 elapsed), 0.126 MUT (0.132 elapsed), 0.049 GC (0.058 elapsed) :ghc>>
hledger -f data/10000x1000x10.journal stats
<<ghc: 3424030664 bytes, 6658 GCs, 11403359/41071624 avg/max bytes residency (11 samples), 111M in use, 0.000 INIT (0.000 elapsed), 1.207 MUT (1.228 elapsed), 0.473 GC (0.528 elapsed) :ghc>>
<<ghc: 3424030760 bytes, 6658 GCs, 11403874/41077288 avg/max bytes residency (11 samples), 111M in use, 0.000 INIT (0.002 elapsed), 1.234 MUT (1.256 elapsed), 0.470 GC (0.520 elapsed) :ghc>>
hledger -f data/100000x1000x10.journal stats
<<ghc: 34306547448 bytes, 66727 GCs, 76805504/414629288 avg/max bytes residency (14 samples), 1009M in use, 0.000 INIT (0.003 elapsed), 12.615 MUT (12.813 elapsed), 4.656 GC (5.291 elapsed) :ghc>>
<<ghc: 34306547320 bytes, 66727 GCs, 76805504/414629288 avg/max bytes residency (14 samples), 1009M in use, 0.000 INIT (0.009 elapsed), 12.802 MUT (13.065 elapsed), 4.774 GC (5.441 elapsed) :ghc>>
2016-05-25 03:09:20 +03:00
|
|
|
,jfinalcommentlines = ""
|
2016-05-23 10:32:55 +03:00
|
|
|
,jfiles = []
|
|
|
|
,jlastreadtime = TOD 0 0
|
|
|
|
}
|
2010-11-13 18:03:40 +03:00
|
|
|
|
2010-09-24 05:56:11 +04:00
|
|
|
journalFilePath :: Journal -> FilePath
|
|
|
|
journalFilePath = fst . mainfile
|
|
|
|
|
|
|
|
journalFilePaths :: Journal -> [FilePath]
|
2016-05-23 10:32:55 +03:00
|
|
|
journalFilePaths = map fst . jfiles
|
2010-09-24 05:56:11 +04:00
|
|
|
|
lib: textification: saved journal source
Slightly worse on small files, better on large ones.
hledger -f data/100x100x10.journal stats
<<ghc: 39305392 bytes, 77 GCs, 196354/268584 avg/max bytes residency (3 samples), 2M in use, 0.000 INIT (0.007 elapsed), 0.014 MUT (0.027 elapsed), 0.011 GC (0.111 elapsed) :ghc>>
<<ghc: 39307728 bytes, 77 GCs, 196909/270248 avg/max bytes residency (3 samples), 2M in use, 0.001 INIT (0.010 elapsed), 0.015 MUT (0.028 elapsed), 0.012 GC (0.116 elapsed) :ghc>>
hledger -f data/1000x1000x10.journal stats
<<ghc: 314285912 bytes, 612 GCs, 2064811/6597608 avg/max bytes residency (7 samples), 16M in use, 0.000 INIT (0.002 elapsed), 0.126 MUT (0.134 elapsed), 0.059 GC (0.069 elapsed) :ghc>>
<<ghc: 314271368 bytes, 612 GCs, 2070227/6628024 avg/max bytes residency (7 samples), 16M in use, 0.000 INIT (0.000 elapsed), 0.137 MUT (0.143 elapsed), 0.059 GC (0.068 elapsed) :ghc>>
hledger -f data/10000x1000x10.journal stats
<<ghc: 3070033264 bytes, 5965 GCs, 12699294/62962464 avg/max bytes residency (10 samples), 124M in use, 0.000 INIT (0.000 elapsed), 1.245 MUT (1.300 elapsed), 0.498 GC (0.558 elapsed) :ghc>>
<<ghc: 3070006752 bytes, 5973 GCs, 12687314/62848920 avg/max bytes residency (10 samples), 124M in use, 0.000 INIT (0.003 elapsed), 1.257 MUT (1.281 elapsed), 0.496 GC (0.554 elapsed) :ghc>>
hledger -f data/100000x1000x10.journal stats
<<ghc: 30753465088 bytes, 59763 GCs, 117723618/666643528 avg/max bytes residency (14 samples), 1589M in use, 0.000 INIT (0.002 elapsed), 12.536 MUT (12.793 elapsed), 5.978 GC (7.155 elapsed) :ghc>>
<<ghc: 30753367256 bytes, 59811 GCs, 117723236/666627528 avg/max bytes residency (14 samples), 1590M in use, 0.001 INIT (0.012 elapsed), 12.923 MUT (13.169 elapsed), 5.981 GC (6.860 elapsed) :ghc>>
2016-05-24 05:24:39 +03:00
|
|
|
mainfile :: Journal -> (FilePath, Text)
|
2016-05-23 10:32:55 +03:00
|
|
|
mainfile = headDef ("", "") . jfiles
|
2010-09-24 05:56:11 +04:00
|
|
|
|
2009-12-16 11:07:26 +03:00
|
|
|
addTransaction :: Transaction -> Journal -> Journal
|
2014-11-03 09:00:02 +03:00
|
|
|
addTransaction t j = j { jtxns = t : jtxns j }
|
2008-12-08 04:48:03 +03:00
|
|
|
|
2018-07-30 21:29:45 +03:00
|
|
|
addTransactionModifier :: TransactionModifier -> Journal -> Journal
|
|
|
|
addTransactionModifier mt j = j { jtxnmodifiers = mt : jtxnmodifiers j }
|
2008-12-08 04:48:03 +03:00
|
|
|
|
2009-12-16 10:00:43 +03:00
|
|
|
addPeriodicTransaction :: PeriodicTransaction -> Journal -> Journal
|
2014-11-03 09:00:02 +03:00
|
|
|
addPeriodicTransaction pt j = j { jperiodictxns = pt : jperiodictxns j }
|
2008-12-08 04:48:03 +03:00
|
|
|
|
2015-08-10 02:20:02 +03:00
|
|
|
addMarketPrice :: MarketPrice -> Journal -> Journal
|
|
|
|
addMarketPrice h j = j { jmarketprices = h : jmarketprices j }
|
2008-12-16 13:54:20 +03:00
|
|
|
|
2015-10-30 06:12:46 +03:00
|
|
|
-- | Get the transaction with this index (its 1-based position in the input stream), if any.
|
|
|
|
journalTransactionAt :: Journal -> Integer -> Maybe Transaction
|
|
|
|
journalTransactionAt Journal{jtxns=ts} i =
|
|
|
|
-- it's probably ts !! (i+1), but we won't assume
|
|
|
|
headMay [t | t <- ts, tindex t == i]
|
|
|
|
|
|
|
|
-- | Get the transaction that appeared immediately after this one in the input stream, if any.
|
|
|
|
journalNextTransaction :: Journal -> Transaction -> Maybe Transaction
|
|
|
|
journalNextTransaction j t = journalTransactionAt j (tindex t + 1)
|
2016-02-21 13:37:59 +03:00
|
|
|
|
2015-10-30 06:12:46 +03:00
|
|
|
-- | Get the transaction that appeared immediately before this one in the input stream, if any.
|
|
|
|
journalPrevTransaction :: Journal -> Transaction -> Maybe Transaction
|
|
|
|
journalPrevTransaction j t = journalTransactionAt j (tindex t - 1)
|
2016-02-21 13:37:59 +03:00
|
|
|
|
2014-02-28 05:47:47 +04:00
|
|
|
-- | Unique transaction descriptions used in this journal.
|
lib: textification: descriptions & codes
Slightly higher (with small files) and lower (with large files) maximum
residency, and slightly quicker for all.
hledger -f data/100x100x10.journal stats
<<ghc: 42858472 bytes, 84 GCs, 193712/269608 avg/max bytes residency (3 samples), 2M in use, 0.000 INIT (0.015 elapsed), 0.016 MUT (0.042 elapsed), 0.011 GC (0.119 elapsed) :ghc>>
<<ghc: 42891776 bytes, 84 GCs, 190816/260920 avg/max bytes residency (3 samples), 2M in use, 0.000 INIT (0.004 elapsed), 0.017 MUT (0.025 elapsed), 0.010 GC (0.015 elapsed) :ghc>>
hledger -f data/1000x1000x10.journal stats
<<ghc: 349575240 bytes, 681 GCs, 1396425/4091680 avg/max bytes residency (7 samples), 11M in use, 0.000 INIT (0.000 elapsed), 0.137 MUT (0.146 elapsed), 0.050 GC (0.057 elapsed) :ghc>>
<<ghc: 349927568 bytes, 681 GCs, 1397825/4097248 avg/max bytes residency (7 samples), 11M in use, 0.000 INIT (0.000 elapsed), 0.126 MUT (0.133 elapsed), 0.050 GC (0.057 elapsed) :ghc>>
hledger -f data/10000x1000x10.journal stats
<<ghc: 3424029496 bytes, 6658 GCs, 11403141/41077288 avg/max bytes residency (11 samples), 111M in use, 0.000 INIT (0.000 elapsed), 1.278 MUT (1.310 elapsed), 0.493 GC (0.546 elapsed) :ghc>>
<<ghc: 3427418064 bytes, 6665 GCs, 11127869/37790168 avg/max bytes residency (11 samples), 109M in use, 0.000 INIT (0.001 elapsed), 1.212 MUT (1.229 elapsed), 0.466 GC (0.519 elapsed) :ghc>>
hledger -f data/100000x1000x10.journal stats
<<ghc: 34306546248 bytes, 66727 GCs, 77030638/414617944 avg/max bytes residency (14 samples), 1012M in use, 0.000 INIT (0.000 elapsed), 12.965 MUT (13.164 elapsed), 4.771 GC (5.447 elapsed) :ghc>>
<<ghc: 34340246056 bytes, 66779 GCs, 76983178/416011480 avg/max bytes residency (14 samples), 1011M in use, 0.000 INIT (0.008 elapsed), 12.666 MUT (12.836 elapsed), 4.595 GC (5.175 elapsed) :ghc>>
2016-05-25 04:51:52 +03:00
|
|
|
journalDescriptions :: Journal -> [Text]
|
2014-02-28 05:47:47 +04:00
|
|
|
journalDescriptions = nub . sort . map tdescription . jtxns
|
|
|
|
|
|
|
|
-- | All postings from this journal's transactions, in order.
|
2009-12-19 08:57:54 +03:00
|
|
|
journalPostings :: Journal -> [Posting]
|
|
|
|
journalPostings = concatMap tpostings . jtxns
|
2008-10-03 16:10:05 +04:00
|
|
|
|
2017-12-29 23:18:04 +03:00
|
|
|
-- | Sorted unique account names posted to by this journal's transactions.
|
2009-12-16 10:00:43 +03:00
|
|
|
journalAccountNamesUsed :: Journal -> [AccountName]
|
2017-12-29 23:18:04 +03:00
|
|
|
journalAccountNamesUsed = accountNamesFromPostings . journalPostings
|
|
|
|
|
|
|
|
-- | Sorted unique account names implied by this journal's transactions -
|
|
|
|
-- accounts posted to and all their implied parent accounts.
|
|
|
|
journalAccountNamesImplied :: Journal -> [AccountName]
|
|
|
|
journalAccountNamesImplied = expandAccountNames . journalAccountNamesUsed
|
|
|
|
|
|
|
|
-- | Sorted unique account names declared by account directives in this journal.
|
|
|
|
journalAccountNamesDeclared :: Journal -> [AccountName]
|
2019-01-14 15:43:13 +03:00
|
|
|
journalAccountNamesDeclared = nub . sort . map fst . jdeclaredaccounts
|
2017-12-29 23:18:04 +03:00
|
|
|
|
|
|
|
-- | Sorted unique account names declared by account directives or posted to
|
|
|
|
-- by transactions in this journal.
|
|
|
|
journalAccountNamesDeclaredOrUsed :: Journal -> [AccountName]
|
|
|
|
journalAccountNamesDeclaredOrUsed j = nub $ sort $ journalAccountNamesDeclared j ++ journalAccountNamesUsed j
|
|
|
|
|
|
|
|
-- | Sorted unique account names declared by account directives, or posted to
|
|
|
|
-- or implied as parents by transactions in this journal.
|
|
|
|
journalAccountNamesDeclaredOrImplied :: Journal -> [AccountName]
|
|
|
|
journalAccountNamesDeclaredOrImplied j = nub $ sort $ journalAccountNamesDeclared j ++ journalAccountNamesImplied j
|
2008-10-03 16:10:05 +04:00
|
|
|
|
2017-12-29 23:34:51 +03:00
|
|
|
-- | Convenience/compatibility alias for journalAccountNamesDeclaredOrImplied.
|
2009-12-16 10:00:43 +03:00
|
|
|
journalAccountNames :: Journal -> [AccountName]
|
2017-12-29 23:18:04 +03:00
|
|
|
journalAccountNames = journalAccountNamesDeclaredOrImplied
|
2008-10-03 16:10:05 +04:00
|
|
|
|
2009-12-16 10:00:43 +03:00
|
|
|
journalAccountNameTree :: Journal -> Tree AccountName
|
|
|
|
journalAccountNameTree = accountNameTreeFrom . journalAccountNames
|
2008-10-10 08:23:25 +04:00
|
|
|
|
2018-09-27 23:54:16 +03:00
|
|
|
-- queries for standard account types
|
|
|
|
|
|
|
|
-- | Get a query for accounts of a certain type (Asset, Liability..) in this journal.
|
|
|
|
-- The query will match all accounts which were declared as that type by account directives,
|
|
|
|
-- plus all their subaccounts which have not been declared as a different type.
|
|
|
|
-- If no accounts were declared as this type, the query will instead match accounts
|
|
|
|
-- with names matched by the provided case-insensitive regular expression.
|
|
|
|
journalAccountTypeQuery :: AccountType -> Regexp -> Journal -> Query
|
|
|
|
journalAccountTypeQuery atype fallbackregex j =
|
|
|
|
case M.lookup atype (jdeclaredaccounttypes j) of
|
|
|
|
Nothing -> Acct fallbackregex
|
|
|
|
Just as ->
|
|
|
|
-- XXX Query isn't able to match account type since that requires extra info from the journal.
|
|
|
|
-- So we do a hacky search by name instead.
|
|
|
|
And [
|
|
|
|
Or $ map (Acct . accountNameToAccountRegex) as
|
|
|
|
,Not $ Or $ map (Acct . accountNameToAccountRegex) differentlytypedsubs
|
|
|
|
]
|
|
|
|
where
|
|
|
|
differentlytypedsubs = concat
|
|
|
|
[subs | (t,bs) <- M.toList (jdeclaredaccounttypes j)
|
|
|
|
, t /= atype
|
|
|
|
, let subs = [b | b <- bs, any (`isAccountNamePrefixOf` b) as]
|
|
|
|
]
|
2012-04-15 04:05:10 +04:00
|
|
|
|
2018-09-27 23:54:16 +03:00
|
|
|
-- | A query for accounts in this journal which have been
|
|
|
|
-- declared as Asset by account directives, or otherwise for
|
|
|
|
-- accounts with names matched by the case-insensitive regular expression
|
|
|
|
-- @^assets?(:|$)@.
|
|
|
|
journalAssetAccountQuery :: Journal -> Query
|
|
|
|
journalAssetAccountQuery = journalAccountTypeQuery Asset "^assets?(:|$)"
|
|
|
|
|
|
|
|
-- | A query for accounts in this journal which have been
|
|
|
|
-- declared as Liability by account directives, or otherwise for
|
|
|
|
-- accounts with names matched by the case-insensitive regular expression
|
|
|
|
-- @^(debts?|liabilit(y|ies))(:|$)@.
|
|
|
|
journalLiabilityAccountQuery :: Journal -> Query
|
|
|
|
journalLiabilityAccountQuery = journalAccountTypeQuery Liability "^(debts?|liabilit(y|ies))(:|$)"
|
|
|
|
|
|
|
|
-- | A query for accounts in this journal which have been
|
|
|
|
-- declared as Equity by account directives, or otherwise for
|
|
|
|
-- accounts with names matched by the case-insensitive regular expression
|
|
|
|
-- @^equity(:|$)@.
|
|
|
|
journalEquityAccountQuery :: Journal -> Query
|
|
|
|
journalEquityAccountQuery = journalAccountTypeQuery Equity "^equity(:|$)"
|
|
|
|
|
|
|
|
-- | A query for accounts in this journal which have been
|
|
|
|
-- declared as Revenue by account directives, or otherwise for
|
|
|
|
-- accounts with names matched by the case-insensitive regular expression
|
|
|
|
-- @^(income|revenue)s?(:|$)@.
|
|
|
|
journalRevenueAccountQuery :: Journal -> Query
|
|
|
|
journalRevenueAccountQuery = journalAccountTypeQuery Revenue "^(income|revenue)s?(:|$)"
|
|
|
|
|
|
|
|
-- | A query for accounts in this journal which have been
|
|
|
|
-- declared as Expense by account directives, or otherwise for
|
|
|
|
-- accounts with names matched by the case-insensitive regular expression
|
|
|
|
-- @^(income|revenue)s?(:|$)@.
|
2012-05-16 11:12:49 +04:00
|
|
|
journalExpenseAccountQuery :: Journal -> Query
|
2018-09-27 23:54:16 +03:00
|
|
|
journalExpenseAccountQuery = journalAccountTypeQuery Expense "^expenses?(:|$)"
|
2012-04-14 05:12:42 +04:00
|
|
|
|
2012-05-16 11:37:24 +04:00
|
|
|
-- | A query for Asset, Liability & Equity accounts in this journal.
|
2012-04-14 05:12:42 +04:00
|
|
|
-- Cf <http://en.wikipedia.org/wiki/Chart_of_accounts#Balance_Sheet_Accounts>.
|
2012-05-16 11:12:49 +04:00
|
|
|
journalBalanceSheetAccountQuery :: Journal -> Query
|
|
|
|
journalBalanceSheetAccountQuery j = Or [journalAssetAccountQuery j
|
2018-09-27 05:14:54 +03:00
|
|
|
,journalLiabilityAccountQuery j
|
|
|
|
,journalEquityAccountQuery j
|
|
|
|
]
|
2012-04-15 04:05:10 +04:00
|
|
|
|
2018-09-27 23:54:16 +03:00
|
|
|
-- | A query for Profit & Loss accounts in this journal.
|
|
|
|
-- Cf <http://en.wikipedia.org/wiki/Chart_of_accounts#Profit_.26_Loss_accounts>.
|
|
|
|
journalProfitAndLossAccountQuery :: Journal -> Query
|
|
|
|
journalProfitAndLossAccountQuery j = Or [journalRevenueAccountQuery j
|
|
|
|
,journalExpenseAccountQuery j
|
|
|
|
]
|
2012-04-14 05:12:42 +04:00
|
|
|
|
2012-04-17 21:32:56 +04:00
|
|
|
-- | A query for Cash (-equivalent) accounts in this journal (ie,
|
|
|
|
-- accounts which appear on the cashflow statement.) This is currently
|
2018-09-27 23:54:16 +03:00
|
|
|
-- hard-coded to be all the Asset accounts except for those with names
|
|
|
|
-- containing the case-insensitive regular expression @(receivable|:A/R|:fixed)@.
|
2012-04-17 21:32:56 +04:00
|
|
|
journalCashAccountQuery :: Journal -> Query
|
2017-08-27 18:26:47 +03:00
|
|
|
journalCashAccountQuery j = And [journalAssetAccountQuery j, Not $ Acct "(receivable|:A/R|:fixed)"]
|
2012-04-17 21:32:56 +04:00
|
|
|
|
2009-12-21 08:23:07 +03:00
|
|
|
-- Various kinds of filtering on journals. We do it differently depending
|
|
|
|
-- on the command.
|
|
|
|
|
2011-06-04 03:14:26 +04:00
|
|
|
-------------------------------------------------------------------------------
|
|
|
|
-- filtering V2
|
|
|
|
|
2014-05-24 00:10:36 +04:00
|
|
|
-- | Keep only transactions matching the query expression.
|
|
|
|
filterJournalTransactions :: Query -> Journal -> Journal
|
|
|
|
filterJournalTransactions q j@Journal{jtxns=ts} = j{jtxns=filter (q `matchesTransaction`) ts}
|
|
|
|
|
2011-06-04 03:14:26 +04:00
|
|
|
-- | Keep only postings matching the query expression.
|
|
|
|
-- This can leave unbalanced transactions.
|
2012-05-27 22:14:20 +04:00
|
|
|
filterJournalPostings :: Query -> Journal -> Journal
|
2016-06-02 06:47:27 +03:00
|
|
|
filterJournalPostings q j@Journal{jtxns=ts} = j{jtxns=map (filterTransactionPostings q) ts}
|
2011-06-05 22:36:32 +04:00
|
|
|
|
2014-05-24 00:10:36 +04:00
|
|
|
-- | Within each posting's amount, keep only the parts matching the query.
|
2014-02-28 05:47:47 +04:00
|
|
|
-- This can leave unbalanced transactions.
|
2014-05-24 00:10:36 +04:00
|
|
|
filterJournalAmounts :: Query -> Journal -> Journal
|
|
|
|
filterJournalAmounts q j@Journal{jtxns=ts} = j{jtxns=map (filterTransactionAmounts q) ts}
|
|
|
|
|
|
|
|
-- | Filter out all parts of this transaction's amounts which do not match the query.
|
|
|
|
-- This can leave the transaction unbalanced.
|
|
|
|
filterTransactionAmounts :: Query -> Transaction -> Transaction
|
|
|
|
filterTransactionAmounts q t@Transaction{tpostings=ps} = t{tpostings=map (filterPostingAmount q) ps}
|
2014-02-28 05:47:47 +04:00
|
|
|
|
|
|
|
-- | Filter out all parts of this posting's amount which do not match the query.
|
|
|
|
filterPostingAmount :: Query -> Posting -> Posting
|
|
|
|
filterPostingAmount q p@Posting{pamount=Mixed as} = p{pamount=Mixed $ filter (q `matchesAmount`) as}
|
|
|
|
|
2016-06-01 22:09:16 +03:00
|
|
|
filterTransactionPostings :: Query -> Transaction -> Transaction
|
2016-06-02 06:47:27 +03:00
|
|
|
filterTransactionPostings q t@Transaction{tpostings=ps} = t{tpostings=filter (q `matchesPosting`) ps}
|
2016-06-01 22:09:16 +03:00
|
|
|
|
|
|
|
|
2012-05-27 22:14:20 +04:00
|
|
|
{-
|
2011-06-04 03:14:26 +04:00
|
|
|
-------------------------------------------------------------------------------
|
|
|
|
-- filtering V1
|
|
|
|
|
2011-06-03 06:14:36 +04:00
|
|
|
-- | Keep only transactions we are interested in, as described by the
|
|
|
|
-- filter specification.
|
2009-12-21 08:23:07 +03:00
|
|
|
filterJournalTransactions :: FilterSpec -> Journal -> Journal
|
|
|
|
filterJournalTransactions FilterSpec{datespan=datespan
|
|
|
|
,cleared=cleared
|
|
|
|
-- ,real=real
|
|
|
|
-- ,empty=empty
|
|
|
|
,acctpats=apats
|
|
|
|
,descpats=dpats
|
|
|
|
,depth=depth
|
2012-05-09 19:34:05 +04:00
|
|
|
,fMetadata=md
|
2009-12-21 08:23:07 +03:00
|
|
|
} =
|
2017-06-16 02:54:34 +03:00
|
|
|
filterJournalTransactionsByStatus cleared .
|
2009-12-21 08:23:07 +03:00
|
|
|
filterJournalPostingsByDepth depth .
|
|
|
|
filterJournalTransactionsByAccount apats .
|
2012-04-09 00:43:48 +04:00
|
|
|
filterJournalTransactionsByMetadata md .
|
2009-12-21 08:23:07 +03:00
|
|
|
filterJournalTransactionsByDescription dpats .
|
2011-06-03 06:14:36 +04:00
|
|
|
filterJournalTransactionsByDate datespan
|
2009-12-21 08:23:07 +03:00
|
|
|
|
2011-06-03 06:14:36 +04:00
|
|
|
-- | Keep only postings we are interested in, as described by the filter
|
|
|
|
-- specification. This can leave unbalanced transactions.
|
2009-12-21 08:23:07 +03:00
|
|
|
filterJournalPostings :: FilterSpec -> Journal -> Journal
|
|
|
|
filterJournalPostings FilterSpec{datespan=datespan
|
|
|
|
,cleared=cleared
|
|
|
|
,real=real
|
|
|
|
,empty=empty
|
|
|
|
,acctpats=apats
|
|
|
|
,descpats=dpats
|
|
|
|
,depth=depth
|
2012-05-09 19:34:05 +04:00
|
|
|
,fMetadata=md
|
2009-12-21 08:23:07 +03:00
|
|
|
} =
|
|
|
|
filterJournalPostingsByRealness real .
|
2017-06-16 02:54:34 +03:00
|
|
|
filterJournalPostingsByStatus cleared .
|
2009-12-21 08:23:07 +03:00
|
|
|
filterJournalPostingsByEmpty empty .
|
|
|
|
filterJournalPostingsByDepth depth .
|
|
|
|
filterJournalPostingsByAccount apats .
|
2012-04-09 00:43:48 +04:00
|
|
|
filterJournalTransactionsByMetadata md .
|
2009-12-21 08:23:07 +03:00
|
|
|
filterJournalTransactionsByDescription dpats .
|
2011-06-03 06:14:36 +04:00
|
|
|
filterJournalTransactionsByDate datespan
|
2009-04-03 14:58:05 +04:00
|
|
|
|
2012-04-09 00:43:48 +04:00
|
|
|
-- | Keep only transactions whose metadata matches all metadata specifications.
|
|
|
|
filterJournalTransactionsByMetadata :: [(String,String)] -> Journal -> Journal
|
|
|
|
filterJournalTransactionsByMetadata pats j@Journal{jtxns=ts} = j{jtxns=filter matchmd ts}
|
|
|
|
where matchmd t = all (`elem` tmetadata t) pats
|
|
|
|
|
2010-07-13 10:30:06 +04:00
|
|
|
-- | Keep only transactions whose description matches the description patterns.
|
2009-12-16 11:07:26 +03:00
|
|
|
filterJournalTransactionsByDescription :: [String] -> Journal -> Journal
|
2009-12-21 08:43:10 +03:00
|
|
|
filterJournalTransactionsByDescription pats j@Journal{jtxns=ts} = j{jtxns=filter matchdesc ts}
|
2009-12-16 20:58:51 +03:00
|
|
|
where matchdesc = matchpats pats . tdescription
|
2009-04-03 14:58:05 +04:00
|
|
|
|
2010-07-13 10:30:06 +04:00
|
|
|
-- | Keep only transactions which fall between begin and end dates.
|
2009-04-03 14:58:05 +04:00
|
|
|
-- We include transactions on the begin date and exclude transactions on the end
|
2008-10-10 08:23:25 +04:00
|
|
|
-- date, like ledger. An empty date string means no restriction.
|
2009-12-16 11:07:26 +03:00
|
|
|
filterJournalTransactionsByDate :: DateSpan -> Journal -> Journal
|
2009-12-21 08:43:10 +03:00
|
|
|
filterJournalTransactionsByDate (DateSpan begin end) j@Journal{jtxns=ts} = j{jtxns=filter match ts}
|
|
|
|
where match t = maybe True (tdate t>=) begin && maybe True (tdate t<) end
|
2008-10-10 08:23:25 +04:00
|
|
|
|
2010-07-13 10:30:06 +04:00
|
|
|
-- | Keep only transactions which have the requested cleared/uncleared
|
|
|
|
-- status, if there is one.
|
2017-06-16 02:54:34 +03:00
|
|
|
filterJournalTransactionsByStatus :: Maybe Bool -> Journal -> Journal
|
|
|
|
filterJournalTransactionsByStatus Nothing j = j
|
|
|
|
filterJournalTransactionsByStatus (Just val) j@Journal{jtxns=ts} = j{jtxns=filter match ts}
|
2009-12-21 08:43:10 +03:00
|
|
|
where match = (==val).tstatus
|
2009-12-21 08:23:07 +03:00
|
|
|
|
|
|
|
-- | Keep only postings which have the requested cleared/uncleared status,
|
|
|
|
-- if there is one.
|
2017-06-16 02:54:34 +03:00
|
|
|
filterJournalPostingsByStatus :: Maybe Bool -> Journal -> Journal
|
|
|
|
filterJournalPostingsByStatus Nothing j = j
|
|
|
|
filterJournalPostingsByStatus (Just c) j@Journal{jtxns=ts} = j{jtxns=map filterpostings ts}
|
2009-12-21 08:23:07 +03:00
|
|
|
where filterpostings t@Transaction{tpostings=ps} = t{tpostings=filter ((==c) . postingCleared) ps}
|
2008-10-15 04:34:02 +04:00
|
|
|
|
2009-04-03 14:58:05 +04:00
|
|
|
-- | Strip out any virtual postings, if the flag is true, otherwise do
|
2008-10-17 04:57:00 +04:00
|
|
|
-- no filtering.
|
2009-12-16 10:00:43 +03:00
|
|
|
filterJournalPostingsByRealness :: Bool -> Journal -> Journal
|
2012-04-16 20:43:58 +04:00
|
|
|
filterJournalPostingsByRealness False j = j
|
2009-12-21 08:43:10 +03:00
|
|
|
filterJournalPostingsByRealness True j@Journal{jtxns=ts} = j{jtxns=map filterpostings ts}
|
2009-12-21 08:23:07 +03:00
|
|
|
where filterpostings t@Transaction{tpostings=ps} = t{tpostings=filter isReal ps}
|
|
|
|
|
|
|
|
-- | Strip out any postings with zero amount, unless the flag is true.
|
|
|
|
filterJournalPostingsByEmpty :: Bool -> Journal -> Journal
|
2012-04-16 20:43:58 +04:00
|
|
|
filterJournalPostingsByEmpty True j = j
|
2009-12-21 08:43:10 +03:00
|
|
|
filterJournalPostingsByEmpty False j@Journal{jtxns=ts} = j{jtxns=map filterpostings ts}
|
2009-12-21 08:23:07 +03:00
|
|
|
where filterpostings t@Transaction{tpostings=ps} = t{tpostings=filter (not . isEmptyPosting) ps}
|
|
|
|
|
2012-04-14 02:24:55 +04:00
|
|
|
-- -- | Keep only transactions which affect accounts deeper than the specified depth.
|
|
|
|
-- filterJournalTransactionsByDepth :: Maybe Int -> Journal -> Journal
|
|
|
|
-- filterJournalTransactionsByDepth Nothing j = j
|
|
|
|
-- filterJournalTransactionsByDepth (Just d) j@Journal{jtxns=ts} =
|
|
|
|
-- j{jtxns=(filter (any ((<= d+1) . accountNameLevel . paccount) . tpostings) ts)}
|
2009-04-03 14:58:05 +04:00
|
|
|
|
|
|
|
-- | Strip out any postings to accounts deeper than the specified depth
|
2010-07-13 10:30:06 +04:00
|
|
|
-- (and any transactions which have no postings as a result).
|
2009-12-21 08:23:07 +03:00
|
|
|
filterJournalPostingsByDepth :: Maybe Int -> Journal -> Journal
|
|
|
|
filterJournalPostingsByDepth Nothing j = j
|
2009-12-21 08:43:10 +03:00
|
|
|
filterJournalPostingsByDepth (Just d) j@Journal{jtxns=ts} =
|
|
|
|
j{jtxns=filter (not . null . tpostings) $ map filtertxns ts}
|
2009-12-16 20:58:51 +03:00
|
|
|
where filtertxns t@Transaction{tpostings=ps} =
|
2009-12-21 08:23:07 +03:00
|
|
|
t{tpostings=filter ((<= d) . accountNameLevel . paccount) ps}
|
2009-04-03 14:58:05 +04:00
|
|
|
|
2012-05-27 22:14:20 +04:00
|
|
|
-- | Keep only postings which affect accounts matched by the account patterns.
|
|
|
|
-- This can leave transactions unbalanced.
|
|
|
|
filterJournalPostingsByAccount :: [String] -> Journal -> Journal
|
|
|
|
filterJournalPostingsByAccount apats j@Journal{jtxns=ts} = j{jtxns=map filterpostings ts}
|
|
|
|
where filterpostings t@Transaction{tpostings=ps} = t{tpostings=filter (matchpats apats . paccount) ps}
|
|
|
|
|
2009-12-21 08:23:07 +03:00
|
|
|
-- | Keep only transactions which affect accounts matched by the account patterns.
|
2010-04-16 03:08:27 +04:00
|
|
|
-- More precisely: each positive account pattern excludes transactions
|
|
|
|
-- which do not contain a posting to a matched account, and each negative
|
|
|
|
-- account pattern excludes transactions containing a posting to a matched
|
|
|
|
-- account.
|
2009-12-21 08:23:07 +03:00
|
|
|
filterJournalTransactionsByAccount :: [String] -> Journal -> Journal
|
2010-04-16 03:08:27 +04:00
|
|
|
filterJournalTransactionsByAccount apats j@Journal{jtxns=ts} = j{jtxns=filter tmatch ts}
|
|
|
|
where
|
|
|
|
tmatch t = (null positives || any positivepmatch ps) && (null negatives || not (any negativepmatch ps)) where ps = tpostings t
|
|
|
|
positivepmatch p = any (`amatch` a) positives where a = paccount p
|
|
|
|
negativepmatch p = any (`amatch` a) negatives where a = paccount p
|
2011-06-09 01:52:10 +04:00
|
|
|
amatch pat a = regexMatchesCI (abspat pat) a
|
2010-04-16 03:08:27 +04:00
|
|
|
(negatives,positives) = partition isnegativepat apats
|
2008-11-22 09:35:10 +03:00
|
|
|
|
2012-05-27 22:14:20 +04:00
|
|
|
-}
|
2009-12-21 08:23:07 +03:00
|
|
|
|
2018-10-16 18:51:51 +03:00
|
|
|
-- | Reverse parsed data to normal order. This is used for post-parse
|
|
|
|
-- processing, since data is added to the head of the list during
|
|
|
|
-- parsing.
|
2018-10-12 17:01:39 +03:00
|
|
|
journalReverse :: Journal -> Journal
|
|
|
|
journalReverse j =
|
|
|
|
j {jfiles = reverse $ jfiles j
|
|
|
|
,jdeclaredaccounts = reverse $ jdeclaredaccounts j
|
|
|
|
,jtxns = reverse $ jtxns j
|
|
|
|
,jtxnmodifiers = reverse $ jtxnmodifiers j
|
|
|
|
,jperiodictxns = reverse $ jperiodictxns j
|
|
|
|
,jmarketprices = reverse $ jmarketprices j
|
|
|
|
}
|
|
|
|
|
2018-10-16 18:51:51 +03:00
|
|
|
-- | Set this journal's last read time, ie when its files were last read.
|
|
|
|
journalSetLastReadTime :: ClockTime -> Journal -> Journal
|
|
|
|
journalSetLastReadTime t j = j{ jlastreadtime = t }
|
2018-10-11 19:35:18 +03:00
|
|
|
|
2013-05-29 03:25:00 +04:00
|
|
|
|
2016-08-14 22:44:19 +03:00
|
|
|
journalNumberAndTieTransactions = journalTieTransactions . journalNumberTransactions
|
|
|
|
|
|
|
|
-- | Number (set the tindex field) this journal's transactions, counting upward from 1.
|
|
|
|
journalNumberTransactions :: Journal -> Journal
|
|
|
|
journalNumberTransactions j@Journal{jtxns=ts} = j{jtxns=map (\(i,t) -> t{tindex=i}) $ zip [1..] ts}
|
|
|
|
|
|
|
|
-- | Tie the knot in all of this journal's transactions, ensuring their postings
|
|
|
|
-- refer to them. This should be done last, after any other transaction-modifying operations.
|
|
|
|
journalTieTransactions :: Journal -> Journal
|
|
|
|
journalTieTransactions j@Journal{jtxns=ts} = j{jtxns=map txnTieKnot ts}
|
|
|
|
|
|
|
|
-- | Untie all transaction-posting knots in this journal, so that eg
|
|
|
|
-- recursiveSize and GHCI's :sprint can work on it.
|
|
|
|
journalUntieTransactions :: Transaction -> Transaction
|
|
|
|
journalUntieTransactions t@Transaction{tpostings=ps} = t{tpostings=map (\p -> p{ptransaction=Nothing}) ps}
|
|
|
|
|
2019-02-01 22:31:04 +03:00
|
|
|
-- | Apply any transaction modifier rules in the journal
|
|
|
|
-- (adding automated postings to transactions, eg).
|
|
|
|
journalModifyTransactions :: Journal -> Journal
|
|
|
|
journalModifyTransactions j = j{ jtxns = modifyTransactions (jtxnmodifiers j) (jtxns j) }
|
|
|
|
|
2013-05-29 03:25:00 +04:00
|
|
|
-- | Check any balance assertions in the journal and return an error
|
|
|
|
-- message if any of them fail.
|
|
|
|
journalCheckBalanceAssertions :: Journal -> Either String Journal
|
2016-12-10 18:04:48 +03:00
|
|
|
journalCheckBalanceAssertions j =
|
2018-04-20 02:49:05 +03:00
|
|
|
runST $ journalBalanceTransactionsST
|
|
|
|
True
|
|
|
|
j
|
|
|
|
(return ())
|
|
|
|
(\_ _ -> return ())
|
|
|
|
(const $ return j)
|
2016-12-10 18:04:48 +03:00
|
|
|
|
|
|
|
-- | Check a posting's balance assertion and return an error if it
|
|
|
|
-- fails.
|
|
|
|
checkBalanceAssertion :: Posting -> MixedAmount -> Either String ()
|
2019-01-06 10:05:14 +03:00
|
|
|
checkBalanceAssertion p@Posting{pbalanceassertion=Just (BalanceAssertion{baamount,baexact})} actualbal =
|
|
|
|
foldl' f (Right ()) assertedamts
|
|
|
|
where
|
|
|
|
f (Right _) assertedamt = checkBalanceAssertionCommodity p assertedamt actualbal
|
|
|
|
f err _ = err
|
|
|
|
assertedamts = baamount : otheramts
|
|
|
|
where
|
|
|
|
assertedcomm = acommodity baamount
|
|
|
|
otheramts | baexact = map (\a -> a{ aquantity = 0 }) $ amounts $ filterMixedAmount (\a -> acommodity a /= assertedcomm) actualbal
|
|
|
|
| otherwise = []
|
2018-10-12 09:17:16 +03:00
|
|
|
checkBalanceAssertion _ _ = Right ()
|
|
|
|
|
2019-01-08 19:51:11 +03:00
|
|
|
-- | Are the asserted balance and the actual balance
|
|
|
|
-- exactly equal (disregarding display precision) ?
|
|
|
|
-- The posting is used for creating an error message.
|
2018-10-12 09:17:16 +03:00
|
|
|
checkBalanceAssertionCommodity :: Posting -> Amount -> MixedAmount -> Either String ()
|
2019-01-06 10:05:14 +03:00
|
|
|
checkBalanceAssertionCommodity p assertedamt actualbal
|
2019-01-08 19:51:11 +03:00
|
|
|
| pass = Right ()
|
|
|
|
| otherwise = Left err
|
2019-01-06 10:05:14 +03:00
|
|
|
where
|
2019-01-08 19:51:11 +03:00
|
|
|
assertedcomm = acommodity assertedamt
|
|
|
|
actualbalincommodity = fromMaybe nullamt $ find ((== assertedcomm) . acommodity) (amounts actualbal)
|
|
|
|
pass =
|
|
|
|
aquantity
|
2019-01-06 21:30:43 +03:00
|
|
|
-- traceWith (("asserted:"++).showAmountDebug)
|
2019-01-08 19:51:11 +03:00
|
|
|
assertedamt ==
|
|
|
|
aquantity
|
2019-01-06 21:30:43 +03:00
|
|
|
-- traceWith (("actual:"++).showAmountDebug)
|
|
|
|
actualbalincommodity
|
2019-01-08 19:51:11 +03:00
|
|
|
diff = aquantity assertedamt - aquantity actualbalincommodity
|
2019-01-06 10:05:14 +03:00
|
|
|
err = printf (unlines
|
2019-01-08 19:51:11 +03:00
|
|
|
[ "balance assertion: %s",
|
|
|
|
"\nassertion details:",
|
2019-01-06 10:05:14 +03:00
|
|
|
"date: %s",
|
|
|
|
"account: %s",
|
|
|
|
"commodity: %s",
|
2019-01-08 19:51:11 +03:00
|
|
|
-- "display precision: %d",
|
|
|
|
"calculated: %s", -- (at display precision: %s)",
|
|
|
|
"asserted: %s", -- (at display precision: %s)",
|
|
|
|
"difference: %s"
|
2019-01-06 10:05:14 +03:00
|
|
|
])
|
|
|
|
(case ptransaction p of
|
2019-01-08 19:51:11 +03:00
|
|
|
Nothing -> "?" -- shouldn't happen
|
|
|
|
Just t -> printf "%s\ntransaction:\n%s"
|
|
|
|
(showGenericSourcePos pos)
|
|
|
|
(chomp $ showTransaction t)
|
|
|
|
:: String
|
|
|
|
where
|
|
|
|
pos = baposition $ fromJust $ pbalanceassertion p
|
|
|
|
)
|
2019-01-06 10:05:14 +03:00
|
|
|
(showDate $ postingDate p)
|
|
|
|
(T.unpack $ paccount p) -- XXX pack
|
|
|
|
assertedcomm
|
2019-01-08 19:51:11 +03:00
|
|
|
-- (asprecision $ astyle actualbalincommodity) -- should be the standard display precision I think
|
|
|
|
(show $ aquantity actualbalincommodity)
|
|
|
|
-- (showAmount actualbalincommodity)
|
|
|
|
(show $ aquantity assertedamt)
|
|
|
|
-- (showAmount assertedamt)
|
|
|
|
(show diff)
|
2016-12-10 18:04:48 +03:00
|
|
|
|
2010-11-15 01:44:37 +03:00
|
|
|
-- | Fill in any missing amounts and check that all journal transactions
|
2019-01-06 10:05:14 +03:00
|
|
|
-- balance and all balance assertions pass, or return an error message.
|
|
|
|
-- This is done after parsing all amounts and applying canonical
|
|
|
|
-- commodity styles, since balancing depends on display precision.
|
|
|
|
-- Reports only the first error encountered.
|
2016-12-10 18:04:48 +03:00
|
|
|
journalBalanceTransactions :: Bool -> Journal -> Either String Journal
|
|
|
|
journalBalanceTransactions assrt j =
|
2018-04-20 02:49:05 +03:00
|
|
|
runST $ journalBalanceTransactionsST
|
|
|
|
assrt -- check balance assertions also ?
|
|
|
|
(journalNumberTransactions j) -- journal to process
|
|
|
|
(newArray_ (1, genericLength $ jtxns j) :: forall s. ST s (STArray s Integer Transaction)) -- initialise state
|
|
|
|
(\arr tx -> writeArray arr (tindex tx) tx) -- update state
|
|
|
|
(fmap (\txns -> j{ jtxns = txns}) . getElems) -- summarise state
|
|
|
|
|
|
|
|
-- | Helper used by 'journalBalanceTransactions' and 'journalCheckBalanceAssertions'.
|
2019-01-06 10:05:14 +03:00
|
|
|
-- Balances transactions, applies balance assignments, and checks balance assertions
|
|
|
|
-- at the same time.
|
2016-12-10 18:04:48 +03:00
|
|
|
journalBalanceTransactionsST ::
|
|
|
|
Bool
|
|
|
|
-> Journal
|
2018-04-20 02:49:05 +03:00
|
|
|
-> ST s txns -- ^ initialise state
|
|
|
|
-> (txns -> Transaction -> ST s ()) -- ^ update state
|
|
|
|
-> (txns -> ST s a) -- ^ summarise state
|
2016-12-10 18:04:48 +03:00
|
|
|
-> ST s (Either String a)
|
|
|
|
journalBalanceTransactionsST assrt j createStore storeIn extract =
|
|
|
|
runExceptT $ do
|
|
|
|
bals <- lift $ HT.newSized size
|
|
|
|
txStore <- lift $ createStore
|
2018-04-20 02:49:05 +03:00
|
|
|
let env = Env bals
|
|
|
|
(storeIn txStore)
|
|
|
|
assrt
|
2018-04-20 22:18:28 +03:00
|
|
|
(Just $ journalCommodityStyles j)
|
2018-10-18 22:26:13 +03:00
|
|
|
(getModifierAccountNames j)
|
2018-04-20 02:49:05 +03:00
|
|
|
flip R.runReaderT env $ do
|
2016-12-10 18:04:48 +03:00
|
|
|
dated <- fmap snd . sortBy (comparing fst) . concat
|
2018-04-20 02:49:05 +03:00
|
|
|
<$> mapM' discriminateByDate (jtxns j)
|
2017-01-13 03:24:53 +03:00
|
|
|
mapM' checkInferAndRegisterAmounts dated
|
2016-12-10 18:04:48 +03:00
|
|
|
lift $ extract txStore
|
2018-04-20 02:49:05 +03:00
|
|
|
where
|
|
|
|
size = genericLength $ journalPostings j
|
2016-12-10 18:04:48 +03:00
|
|
|
|
2018-10-18 22:26:13 +03:00
|
|
|
|
|
|
|
-- | Collect account names in account modifiers into a set
|
|
|
|
getModifierAccountNames :: Journal -> S.Set AccountName
|
|
|
|
getModifierAccountNames j = S.fromList $
|
|
|
|
map paccount $
|
|
|
|
concatMap tmpostingrules $
|
|
|
|
jtxnmodifiers j
|
|
|
|
|
2018-04-20 02:49:05 +03:00
|
|
|
-- | Monad transformer stack with a reference to a mutable hashtable
|
|
|
|
-- of current account balances and a mutable array of finished
|
|
|
|
-- transactions in original parsing order.
|
|
|
|
type CurrentBalancesModifier s = R.ReaderT (Env s) (ExceptT String (ST s))
|
|
|
|
|
|
|
|
-- | Environment for 'CurrentBalancesModifier'
|
2018-10-18 19:54:55 +03:00
|
|
|
data Env s = Env { eBalances :: HT.HashTable s AccountName MixedAmount
|
|
|
|
, eStoreTx :: Transaction -> ST s ()
|
|
|
|
, eAssrt :: Bool
|
|
|
|
, eStyles :: Maybe (M.Map CommoditySymbol AmountStyle)
|
|
|
|
, eUnassignable :: S.Set AccountName
|
2018-04-20 02:49:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
-- | This converts a transaction into a list of transactions or
|
|
|
|
-- postings whose dates have to be considered when checking
|
|
|
|
-- balance assertions and handled by 'checkInferAndRegisterAmounts'.
|
2016-12-10 18:04:48 +03:00
|
|
|
--
|
|
|
|
-- Transaction without balance assignments can be balanced and stored
|
|
|
|
-- immediately and their (possibly) dated postings are returned.
|
|
|
|
--
|
|
|
|
-- Transaction with balance assignments are only supported if no
|
|
|
|
-- posting has a 'pdate' value. Supported transactions will be
|
|
|
|
-- returned unchanged and balanced and stored later in 'checkInferAndRegisterAmounts'.
|
|
|
|
discriminateByDate :: Transaction
|
|
|
|
-> CurrentBalancesModifier s [(Day, Either Posting Transaction)]
|
|
|
|
discriminateByDate tx
|
|
|
|
| null (assignmentPostings tx) = do
|
|
|
|
styles <- R.reader $ eStyles
|
2018-04-20 02:49:05 +03:00
|
|
|
balanced <- lift $ ExceptT $ return $ balanceTransaction styles tx
|
2016-12-10 18:04:48 +03:00
|
|
|
storeTransaction balanced
|
2018-04-20 02:49:05 +03:00
|
|
|
return $
|
|
|
|
fmap (postingDate &&& (Left . removePrices)) $ tpostings $ balanced
|
2016-12-10 18:04:48 +03:00
|
|
|
| True = do
|
|
|
|
when (any (isJust . pdate) $ tpostings tx) $
|
|
|
|
throwError $ unlines $
|
2018-10-10 02:08:47 +03:00
|
|
|
["postings may not have both a custom date and a balance assignment."
|
|
|
|
,"Write the posting amount explicitly, or remove the posting date:\n"
|
2016-12-10 18:04:48 +03:00
|
|
|
, showTransaction tx]
|
2018-04-20 02:49:05 +03:00
|
|
|
return
|
|
|
|
[(tdate tx, Right $ tx { tpostings = removePrices <$> tpostings tx })]
|
|
|
|
|
2018-10-18 20:28:13 +03:00
|
|
|
-- | Throw an error if a posting is in the unassignable set.
|
|
|
|
checkUnassignablePosting :: Posting -> CurrentBalancesModifier s ()
|
|
|
|
checkUnassignablePosting p = do
|
|
|
|
unassignable <- R.asks eUnassignable
|
|
|
|
if (isAssignment p && paccount p `S.member` unassignable)
|
2018-10-18 23:02:06 +03:00
|
|
|
then throwError $ unlines $
|
|
|
|
[ "cannot assign amount to account "
|
|
|
|
, ""
|
|
|
|
, " " ++ (T.unpack $ paccount p)
|
|
|
|
, ""
|
|
|
|
, "because it is also included in transaction modifiers."
|
|
|
|
]
|
2018-10-18 20:28:13 +03:00
|
|
|
else return ()
|
|
|
|
|
|
|
|
|
2018-04-20 02:49:05 +03:00
|
|
|
-- | This function takes an object describing changes to
|
|
|
|
-- account balances on a single day - either a single posting
|
|
|
|
-- (from an already balanced transaction without assignments)
|
|
|
|
-- or a whole transaction with assignments (which is required to
|
|
|
|
-- have no posting with pdate set).
|
2016-12-10 18:04:48 +03:00
|
|
|
--
|
|
|
|
-- For a single posting, there is not much to do. Only add its amount
|
|
|
|
-- to its account and check the assertion, if there is one. This
|
|
|
|
-- functionality is provided by 'addAmountAndCheckBalance'.
|
|
|
|
--
|
|
|
|
-- For a whole transaction, it loops over all postings, and performs
|
|
|
|
-- 'addAmountAndCheckBalance', if there is an amount. If there is no
|
|
|
|
-- amount, the amount is inferred by the assertion or left empty if
|
|
|
|
-- there is no assertion. Then, the transaction is balanced, the
|
2018-04-20 02:49:05 +03:00
|
|
|
-- inferred amount added to the balance (all in 'balanceTransactionUpdate')
|
|
|
|
-- and the resulting transaction with no missing amounts is stored
|
|
|
|
-- in the array, for later retrieval.
|
2016-12-10 18:04:48 +03:00
|
|
|
--
|
|
|
|
-- Again in short:
|
|
|
|
--
|
|
|
|
-- 'Left Posting': Check the balance assertion and update the
|
|
|
|
-- account balance. If the amount is empty do nothing. this can be
|
|
|
|
-- the case e.g. for virtual postings
|
|
|
|
--
|
|
|
|
-- 'Right Transaction': Loop over all postings, infer their amounts
|
|
|
|
-- and then balance and store the transaction.
|
|
|
|
checkInferAndRegisterAmounts :: Either Posting Transaction
|
|
|
|
-> CurrentBalancesModifier s ()
|
2018-10-18 20:28:13 +03:00
|
|
|
checkInferAndRegisterAmounts (Left p) = do
|
|
|
|
checkUnassignablePosting p
|
2016-12-10 18:04:48 +03:00
|
|
|
void $ addAmountAndCheckBalance return p
|
|
|
|
checkInferAndRegisterAmounts (Right oldTx) = do
|
|
|
|
let ps = tpostings oldTx
|
2018-10-18 20:28:13 +03:00
|
|
|
mapM_ checkUnassignablePosting ps
|
2016-12-10 18:04:48 +03:00
|
|
|
styles <- R.reader $ eStyles
|
|
|
|
newPostings <- forM ps $ addAmountAndCheckBalance inferFromAssignment
|
|
|
|
storeTransaction =<< balanceTransactionUpdate
|
|
|
|
(fmap void . addToBalance) styles oldTx { tpostings = newPostings }
|
|
|
|
where
|
|
|
|
inferFromAssignment :: Posting -> CurrentBalancesModifier s Posting
|
2018-10-13 07:28:46 +03:00
|
|
|
inferFromAssignment p = do
|
|
|
|
let acc = paccount p
|
|
|
|
case pbalanceassertion p of
|
2018-10-12 09:17:16 +03:00
|
|
|
Just ba | baexact ba -> do
|
|
|
|
diff <- setMixedBalance acc $ Mixed [baamount ba]
|
|
|
|
fullPosting diff p
|
|
|
|
Just ba | otherwise -> do
|
2018-10-13 07:28:46 +03:00
|
|
|
old <- liftModifier $ \Env{ eBalances = bals } -> HT.lookup bals acc
|
|
|
|
let amt = baamount ba
|
|
|
|
assertedcomm = acommodity amt
|
|
|
|
diff <- setMixedBalance acc $
|
|
|
|
Mixed [amt] + filterMixedAmount (\a -> acommodity a /= assertedcomm) (fromMaybe nullmixedamt old)
|
|
|
|
fullPosting diff p
|
|
|
|
Nothing -> return p
|
|
|
|
fullPosting amt p = return p
|
|
|
|
{ pamount = amt
|
|
|
|
, porigin = Just $ originalPosting p
|
|
|
|
}
|
2016-12-10 18:04:48 +03:00
|
|
|
|
2018-04-20 02:49:05 +03:00
|
|
|
-- | Adds a posting's amount to the posting's account balance and
|
|
|
|
-- checks a possible balance assertion. Or if there is no amount,
|
|
|
|
-- runs the supplied fallback action.
|
|
|
|
addAmountAndCheckBalance ::
|
|
|
|
(Posting -> CurrentBalancesModifier s Posting) -- ^ action if posting has no amount
|
|
|
|
-> Posting
|
|
|
|
-> CurrentBalancesModifier s Posting
|
2016-12-10 18:04:48 +03:00
|
|
|
addAmountAndCheckBalance _ p | hasAmount p = do
|
|
|
|
newAmt <- addToBalance (paccount p) $ pamount p
|
|
|
|
assrt <- R.reader eAssrt
|
2018-04-20 02:49:05 +03:00
|
|
|
lift $ when assrt $ ExceptT $ return $ checkBalanceAssertion p newAmt
|
2016-12-10 18:04:48 +03:00
|
|
|
return p
|
|
|
|
addAmountAndCheckBalance fallback p = fallback p
|
|
|
|
|
2018-10-13 07:28:46 +03:00
|
|
|
-- | Sets all commodities comprising an account's balance to the given
|
|
|
|
-- amounts and returns the difference from the previous balance.
|
|
|
|
setMixedBalance :: AccountName -> MixedAmount -> CurrentBalancesModifier s MixedAmount
|
|
|
|
setMixedBalance acc amt = liftModifier $ \Env{ eBalances = bals } -> do
|
2016-12-10 18:04:48 +03:00
|
|
|
old <- HT.lookup bals acc
|
2018-10-13 07:28:46 +03:00
|
|
|
HT.insert bals acc amt
|
|
|
|
return $ maybe amt (amt -) old
|
2016-12-10 18:04:48 +03:00
|
|
|
|
2018-04-20 02:49:05 +03:00
|
|
|
-- | Adds an amount to an account's balance and returns the resulting balance.
|
2016-12-10 18:04:48 +03:00
|
|
|
addToBalance :: AccountName -> MixedAmount -> CurrentBalancesModifier s MixedAmount
|
|
|
|
addToBalance acc amt = liftModifier $ \Env{ eBalances = bals } -> do
|
|
|
|
new <- maybe amt (+ amt) <$> HT.lookup bals acc
|
|
|
|
HT.insert bals acc new
|
|
|
|
return new
|
|
|
|
|
2018-04-20 02:49:05 +03:00
|
|
|
-- | Stores a transaction in the transaction array in original parsing order.
|
2016-12-10 18:04:48 +03:00
|
|
|
storeTransaction :: Transaction -> CurrentBalancesModifier s ()
|
|
|
|
storeTransaction tx = liftModifier $ ($tx) . eStoreTx
|
|
|
|
|
2018-04-20 02:49:05 +03:00
|
|
|
-- | Helper function.
|
2016-12-10 18:04:48 +03:00
|
|
|
liftModifier :: (Env s -> ST s a) -> CurrentBalancesModifier s a
|
|
|
|
liftModifier f = R.ask >>= lift . lift . f
|
|
|
|
|
2016-05-08 04:25:28 +03:00
|
|
|
-- | Choose and apply a consistent display format to the posting
|
|
|
|
-- amounts in each commodity. Each commodity's format is specified by
|
|
|
|
-- a commodity format directive, or otherwise inferred from posting
|
|
|
|
-- amounts as in hledger < 0.28.
|
2015-11-22 20:21:36 +03:00
|
|
|
journalApplyCommodityStyles :: Journal -> Journal
|
|
|
|
journalApplyCommodityStyles j@Journal{jtxns=ts, jmarketprices=mps} = j''
|
2010-05-23 03:35:34 +04:00
|
|
|
where
|
2016-05-08 04:25:28 +03:00
|
|
|
j' = journalInferCommodityStyles j
|
2018-04-20 22:18:28 +03:00
|
|
|
styles = journalCommodityStyles j'
|
2015-08-10 02:20:02 +03:00
|
|
|
j'' = j'{jtxns=map fixtransaction ts, jmarketprices=map fixmarketprice mps}
|
2010-05-23 03:35:34 +04:00
|
|
|
fixtransaction t@Transaction{tpostings=ps} = t{tpostings=map fixposting ps}
|
2018-04-20 22:18:28 +03:00
|
|
|
fixposting p@Posting{pamount=a} = p{pamount=styleMixedAmount styles a}
|
|
|
|
fixmarketprice mp@MarketPrice{mpamount=a} = mp{mpamount=styleAmount styles a}
|
|
|
|
|
2018-04-21 07:56:06 +03:00
|
|
|
-- | Get all the amount styles defined in this journal, either declared by
|
|
|
|
-- a commodity directive or inferred from amounts, as a map from symbol to style.
|
|
|
|
-- Styles declared by commodity directives take precedence, and these also are
|
|
|
|
-- guaranteed to know their decimal point character.
|
2018-04-20 22:18:28 +03:00
|
|
|
journalCommodityStyles :: Journal -> M.Map CommoditySymbol AmountStyle
|
|
|
|
journalCommodityStyles j = declaredstyles <> inferredstyles
|
|
|
|
where
|
|
|
|
declaredstyles = M.mapMaybe cformat $ jcommodities j
|
|
|
|
inferredstyles = jinferredcommodities j
|
2016-05-08 04:25:28 +03:00
|
|
|
|
2018-04-21 07:56:06 +03:00
|
|
|
-- | Collect and save inferred amount styles for each commodity based on
|
|
|
|
-- the posting amounts in that commodity (excluding price amounts), ie:
|
|
|
|
-- "the format of the first amount, adjusted to the highest precision of all amounts".
|
2016-05-08 04:25:28 +03:00
|
|
|
journalInferCommodityStyles :: Journal -> Journal
|
|
|
|
journalInferCommodityStyles j =
|
2016-05-23 10:32:55 +03:00
|
|
|
j{jinferredcommodities =
|
2018-04-20 22:18:28 +03:00
|
|
|
commodityStylesFromAmounts $
|
|
|
|
dbg8 "journalInferCommmodityStyles using amounts" $ journalAmounts j}
|
2015-11-22 20:21:36 +03:00
|
|
|
|
|
|
|
-- | Given a list of amounts in parse order, build a map from their commodity names
|
|
|
|
-- to standard commodity display formats.
|
2016-05-08 02:18:04 +03:00
|
|
|
commodityStylesFromAmounts :: [Amount] -> M.Map CommoditySymbol AmountStyle
|
2015-11-22 20:21:36 +03:00
|
|
|
commodityStylesFromAmounts amts = M.fromList commstyles
|
look harder for decimal point & digit groups (fixes #196)
Amount display styles have been reworked a bit; they are now calculated
after journal parsing, not during it. This allows the fix for #196:
we now search through the amounts until a decimal point is detected,
instead of just looking at the first one; likewise for digit groups.
Digit groups are now implemented with a better type.
Digit group size detection has been improved a little:
1000,000 now gives group sizes [3,4,4,...], not [3,3,...], and
10,000 gives groups sizes [3,3,...] not [3,2,2,..].
(To get [3,2,2,...] you'd use eg 00,00,000.)
There are still some old (or new ?) issues; I don't think we handle
inconsistent decimal points & digit groups too well. But for now all
tests pass.
2014-07-03 10:26:16 +04:00
|
|
|
where
|
2017-11-28 04:22:44 +03:00
|
|
|
commamts = groupSort [(acommodity as, as) | as <- amts]
|
look harder for decimal point & digit groups (fixes #196)
Amount display styles have been reworked a bit; they are now calculated
after journal parsing, not during it. This allows the fix for #196:
we now search through the amounts until a decimal point is detected,
instead of just looking at the first one; likewise for digit groups.
Digit groups are now implemented with a better type.
Digit group size detection has been improved a little:
1000,000 now gives group sizes [3,4,4,...], not [3,3,...], and
10,000 gives groups sizes [3,3,...] not [3,2,2,..].
(To get [3,2,2,...] you'd use eg 00,00,000.)
There are still some old (or new ?) issues; I don't think we handle
inconsistent decimal points & digit groups too well. But for now all
tests pass.
2014-07-03 10:26:16 +04:00
|
|
|
commstyles = [(c, canonicalStyleFrom $ map astyle as) | (c,as) <- commamts]
|
|
|
|
|
2015-11-22 20:21:36 +03:00
|
|
|
-- | Given an ordered list of amount styles, choose a canonical style.
|
2018-08-04 18:44:05 +03:00
|
|
|
-- That is: the style of the first, and the maximum precision of all.
|
look harder for decimal point & digit groups (fixes #196)
Amount display styles have been reworked a bit; they are now calculated
after journal parsing, not during it. This allows the fix for #196:
we now search through the amounts until a decimal point is detected,
instead of just looking at the first one; likewise for digit groups.
Digit groups are now implemented with a better type.
Digit group size detection has been improved a little:
1000,000 now gives group sizes [3,4,4,...], not [3,3,...], and
10,000 gives groups sizes [3,3,...] not [3,2,2,..].
(To get [3,2,2,...] you'd use eg 00,00,000.)
There are still some old (or new ?) issues; I don't think we handle
inconsistent decimal points & digit groups too well. But for now all
tests pass.
2014-07-03 10:26:16 +04:00
|
|
|
canonicalStyleFrom :: [AmountStyle] -> AmountStyle
|
|
|
|
canonicalStyleFrom [] = amountstyle
|
|
|
|
canonicalStyleFrom ss@(first:_) =
|
|
|
|
first{asprecision=prec, asdecimalpoint=mdec, asdigitgroups=mgrps}
|
|
|
|
where
|
2015-11-22 20:21:36 +03:00
|
|
|
mgrps = maybe Nothing Just $ headMay $ catMaybes $ map asdigitgroups ss
|
|
|
|
-- precision is maximum of all precisions
|
2017-01-13 03:24:53 +03:00
|
|
|
prec = maximumStrict $ map asprecision ss
|
look harder for decimal point & digit groups (fixes #196)
Amount display styles have been reworked a bit; they are now calculated
after journal parsing, not during it. This allows the fix for #196:
we now search through the amounts until a decimal point is detected,
instead of just looking at the first one; likewise for digit groups.
Digit groups are now implemented with a better type.
Digit group size detection has been improved a little:
1000,000 now gives group sizes [3,4,4,...], not [3,3,...], and
10,000 gives groups sizes [3,3,...] not [3,2,2,..].
(To get [3,2,2,...] you'd use eg 00,00,000.)
There are still some old (or new ?) issues; I don't think we handle
inconsistent decimal points & digit groups too well. But for now all
tests pass.
2014-07-03 10:26:16 +04:00
|
|
|
mdec = Just $ headDef '.' $ catMaybes $ map asdecimalpoint ss
|
2015-11-22 20:21:36 +03:00
|
|
|
-- precision is that of first amount with a decimal point
|
|
|
|
-- (mdec, prec) =
|
|
|
|
-- case filter (isJust . asdecimalpoint) ss of
|
|
|
|
-- (s:_) -> (asdecimalpoint s, asprecision s)
|
|
|
|
-- [] -> (Just '.', 0)
|
2010-05-22 23:00:20 +04:00
|
|
|
|
2012-04-14 02:24:55 +04:00
|
|
|
-- -- | Apply this journal's historical price records to unpriced amounts where possible.
|
2015-08-10 02:20:02 +03:00
|
|
|
-- journalApplyMarketPrices :: Journal -> Journal
|
|
|
|
-- journalApplyMarketPrices j@Journal{jtxns=ts} = j{jtxns=map fixtransaction ts}
|
2012-04-14 02:24:55 +04:00
|
|
|
-- where
|
|
|
|
-- fixtransaction t@Transaction{tdate=d, tpostings=ps} = t{tpostings=map fixposting ps}
|
|
|
|
-- where
|
|
|
|
-- fixposting p@Posting{pamount=a} = p{pamount=fixmixedamount a}
|
|
|
|
-- fixmixedamount (Mixed as) = Mixed $ map fixamount as
|
|
|
|
-- fixamount = fixprice
|
|
|
|
-- fixprice a@Amount{price=Just _} = a
|
2015-08-10 02:20:02 +03:00
|
|
|
-- fixprice a@Amount{commodity=c} = a{price=maybe Nothing (Just . UnitPrice) $ journalMarketPriceFor j d c}
|
2012-04-14 02:24:55 +04:00
|
|
|
|
|
|
|
-- -- | Get the price for a commodity on the specified day from the price database, if known.
|
|
|
|
-- -- Does only one lookup step, ie will not look up the price of a price.
|
2016-05-08 02:18:04 +03:00
|
|
|
-- journalMarketPriceFor :: Journal -> Day -> CommoditySymbol -> Maybe MixedAmount
|
|
|
|
-- journalMarketPriceFor j d CommoditySymbol{symbol=s} = do
|
2015-08-10 02:20:02 +03:00
|
|
|
-- let ps = reverse $ filter ((<= d).mpdate) $ filter ((s==).hsymbol) $ sortBy (comparing mpdate) $ jmarketprices j
|
|
|
|
-- case ps of (MarketPrice{mpamount=a}:_) -> Just a
|
2012-04-14 02:24:55 +04:00
|
|
|
-- _ -> Nothing
|
2010-05-23 00:23:36 +04:00
|
|
|
|
|
|
|
-- | Convert all this journal's amounts to cost by applying their prices, if any.
|
|
|
|
journalConvertAmountsToCost :: Journal -> Journal
|
|
|
|
journalConvertAmountsToCost j@Journal{jtxns=ts} = j{jtxns=map fixtransaction ts}
|
|
|
|
where
|
2015-11-22 20:21:36 +03:00
|
|
|
-- similar to journalApplyCommodityStyles
|
2010-05-23 00:23:36 +04:00
|
|
|
fixtransaction t@Transaction{tpostings=ps} = t{tpostings=map fixposting ps}
|
|
|
|
fixposting p@Posting{pamount=a} = p{pamount=fixmixedamount a}
|
|
|
|
fixmixedamount (Mixed as) = Mixed $ map fixamount as
|
2018-04-20 22:18:28 +03:00
|
|
|
fixamount = styleAmount styles . costOfAmount
|
|
|
|
styles = journalCommodityStyles j
|
2012-11-20 01:20:10 +04:00
|
|
|
|
|
|
|
-- -- | Get this journal's unique, display-preference-canonicalised commodities, by symbol.
|
2016-05-08 02:18:04 +03:00
|
|
|
-- journalCanonicalCommodities :: Journal -> M.Map String CommoditySymbol
|
2012-11-20 01:20:10 +04:00
|
|
|
-- journalCanonicalCommodities j = canonicaliseCommodities $ journalAmountCommodities j
|
2010-05-23 00:23:36 +04:00
|
|
|
|
2012-11-20 01:20:10 +04:00
|
|
|
-- -- | Get all this journal's amounts' commodities, in the order parsed.
|
2016-05-08 02:18:04 +03:00
|
|
|
-- journalAmountCommodities :: Journal -> [CommoditySymbol]
|
2012-11-20 01:20:10 +04:00
|
|
|
-- journalAmountCommodities = map acommodity . concatMap amounts . journalAmounts
|
2010-05-23 00:23:36 +04:00
|
|
|
|
2012-11-20 01:20:10 +04:00
|
|
|
-- -- | Get all this journal's amount and price commodities, in the order parsed.
|
2016-05-08 02:18:04 +03:00
|
|
|
-- journalAmountAndPriceCommodities :: Journal -> [CommoditySymbol]
|
2012-11-20 01:20:10 +04:00
|
|
|
-- journalAmountAndPriceCommodities = concatMap amountCommodities . concatMap amounts . journalAmounts
|
2010-05-23 00:23:36 +04:00
|
|
|
|
2012-11-20 01:20:10 +04:00
|
|
|
-- -- | Get this amount's commodity and any commodities referenced in its price.
|
2016-05-08 02:18:04 +03:00
|
|
|
-- amountCommodities :: Amount -> [CommoditySymbol]
|
2012-11-20 01:20:10 +04:00
|
|
|
-- amountCommodities Amount{acommodity=c,aprice=p} =
|
|
|
|
-- case p of Nothing -> [c]
|
|
|
|
-- Just (UnitPrice ma) -> c:(concatMap amountCommodities $ amounts ma)
|
|
|
|
-- Just (TotalPrice ma) -> c:(concatMap amountCommodities $ amounts ma)
|
2010-05-23 00:23:36 +04:00
|
|
|
|
2015-10-12 02:07:31 +03:00
|
|
|
-- | Get an ordered list of the amounts in this journal which will
|
|
|
|
-- influence amount style canonicalisation. These are:
|
|
|
|
--
|
|
|
|
-- * amounts in market price directives (in parse order)
|
|
|
|
-- * amounts in postings (in parse order)
|
|
|
|
--
|
|
|
|
-- Amounts in default commodity directives also influence
|
|
|
|
-- canonicalisation, but earlier, as amounts are parsed.
|
|
|
|
-- Amounts in posting prices are not used for canonicalisation.
|
|
|
|
--
|
2012-11-20 01:20:10 +04:00
|
|
|
journalAmounts :: Journal -> [Amount]
|
2017-02-04 11:08:00 +03:00
|
|
|
journalAmounts = getConst . traverseJournalAmounts (Const . (:[]))
|
|
|
|
|
|
|
|
-- | Maps over all of the amounts in the journal
|
|
|
|
overJournalAmounts :: (Amount -> Amount) -> Journal -> Journal
|
|
|
|
overJournalAmounts f = runIdentity . traverseJournalAmounts (Identity . f)
|
|
|
|
|
|
|
|
-- | Traverses over all ofthe amounts in the journal, in the order
|
|
|
|
-- indicated by 'journalAmounts'.
|
|
|
|
traverseJournalAmounts
|
|
|
|
:: Applicative f
|
|
|
|
=> (Amount -> f Amount)
|
|
|
|
-> Journal -> f Journal
|
|
|
|
traverseJournalAmounts f j =
|
|
|
|
recombine <$> (traverse . mpa) f (jmarketprices j)
|
|
|
|
<*> (traverse . tp . traverse . pamt . maa . traverse) f (jtxns j)
|
|
|
|
where
|
|
|
|
recombine mps txns = j { jmarketprices = mps, jtxns = txns }
|
|
|
|
-- a bunch of traversals
|
|
|
|
mpa g mp = (\amt -> mp { mpamount = amt }) <$> g (mpamount mp)
|
|
|
|
tp g t = (\ps -> t { tpostings = ps }) <$> g (tpostings t)
|
|
|
|
pamt g p = (\amt -> p { pamount = amt }) <$> g (pamount p)
|
|
|
|
maa g (Mixed as) = Mixed <$> g as
|
2008-11-22 23:35:17 +03:00
|
|
|
|
2014-04-25 01:44:30 +04:00
|
|
|
-- | The fully specified date span enclosing the dates (primary or secondary)
|
|
|
|
-- of all this journal's transactions and postings, or DateSpan Nothing Nothing
|
2014-04-25 01:28:20 +04:00
|
|
|
-- if there are none.
|
2014-04-25 01:44:30 +04:00
|
|
|
journalDateSpan :: Bool -> Journal -> DateSpan
|
|
|
|
journalDateSpan secondary j
|
2014-04-25 01:28:20 +04:00
|
|
|
| null ts = DateSpan Nothing Nothing
|
|
|
|
| otherwise = DateSpan (Just earliest) (Just $ addDays 1 latest)
|
2009-04-04 15:19:15 +04:00
|
|
|
where
|
2017-01-13 03:24:53 +03:00
|
|
|
earliest = minimumStrict dates
|
|
|
|
latest = maximumStrict dates
|
2014-04-25 01:28:20 +04:00
|
|
|
dates = pdates ++ tdates
|
2014-04-25 01:44:30 +04:00
|
|
|
tdates = map (if secondary then transactionDate2 else tdate) ts
|
|
|
|
pdates = concatMap (catMaybes . map (if secondary then (Just . postingDate2) else pdate) . tpostings) ts
|
2014-04-25 01:28:20 +04:00
|
|
|
ts = jtxns j
|
|
|
|
|
2017-09-05 20:48:35 +03:00
|
|
|
-- | Apply the pivot transformation to all postings in a journal,
|
|
|
|
-- replacing their account name by their value for the given field or tag.
|
|
|
|
journalPivot :: Text -> Journal -> Journal
|
|
|
|
journalPivot fieldortagname j = j{jtxns = map (transactionPivot fieldortagname) . jtxns $ j}
|
|
|
|
|
|
|
|
-- | Replace this transaction's postings' account names with the value
|
|
|
|
-- of the given field or tag, if any.
|
|
|
|
transactionPivot :: Text -> Transaction -> Transaction
|
|
|
|
transactionPivot fieldortagname t = t{tpostings = map (postingPivot fieldortagname) . tpostings $ t}
|
|
|
|
|
|
|
|
-- | Replace this posting's account name with the value
|
|
|
|
-- of the given field or tag, if any, otherwise the empty string.
|
|
|
|
postingPivot :: Text -> Posting -> Posting
|
|
|
|
postingPivot fieldortagname p = p{paccount = pivotedacct, porigin = Just $ originalPosting p}
|
|
|
|
where
|
|
|
|
pivotedacct
|
|
|
|
| Just t <- ptransaction p, fieldortagname == "code" = tcode t
|
|
|
|
| Just t <- ptransaction p, fieldortagname == "description" = tdescription t
|
|
|
|
| Just t <- ptransaction p, fieldortagname == "payee" = transactionPayee t
|
|
|
|
| Just t <- ptransaction p, fieldortagname == "note" = transactionNote t
|
|
|
|
| Just (_, value) <- postingFindTag fieldortagname p = value
|
|
|
|
| otherwise = ""
|
|
|
|
|
|
|
|
postingFindTag :: TagName -> Posting -> Maybe (TagName, TagValue)
|
|
|
|
postingFindTag tagname p = find ((tagname==) . fst) $ postingAllTags p
|
|
|
|
|
2012-04-14 05:12:42 +04:00
|
|
|
-- Misc helpers
|
|
|
|
|
2010-07-13 10:30:06 +04:00
|
|
|
-- | Check if a set of hledger account/description filter patterns matches the
|
2009-05-29 15:31:51 +04:00
|
|
|
-- given account name or entry description. Patterns are case-insensitive
|
2010-04-14 20:19:01 +04:00
|
|
|
-- regular expressions. Prefixed with not:, they become anti-patterns.
|
2009-05-29 15:31:51 +04:00
|
|
|
matchpats :: [String] -> String -> Bool
|
|
|
|
matchpats pats str =
|
|
|
|
(null positives || any match positives) && (null negatives || not (any match negatives))
|
|
|
|
where
|
|
|
|
(negatives,positives) = partition isnegativepat pats
|
|
|
|
match "" = True
|
2011-06-09 01:52:10 +04:00
|
|
|
match pat = regexMatchesCI (abspat pat) str
|
2010-04-16 03:08:27 +04:00
|
|
|
|
|
|
|
negateprefix = "not:"
|
|
|
|
|
|
|
|
isnegativepat = (negateprefix `isPrefixOf`)
|
|
|
|
|
|
|
|
abspat pat = if isnegativepat pat then drop (length negateprefix) pat else pat
|
2009-12-21 08:23:07 +03:00
|
|
|
|
2011-08-30 15:37:36 +04:00
|
|
|
-- debug helpers
|
2012-11-20 01:20:10 +04:00
|
|
|
-- traceAmountPrecision a = trace (show $ map (precision . acommodity) $ amounts a) a
|
|
|
|
-- tracePostingsCommodities ps = trace (show $ map ((map (precision . acommodity) . amounts) . pamount) ps) ps
|
2011-08-30 15:37:36 +04:00
|
|
|
|
2012-04-14 05:12:42 +04:00
|
|
|
-- tests
|
|
|
|
|
2017-01-08 16:20:04 +03:00
|
|
|
-- A sample journal for testing, similar to examples/sample.journal:
|
2012-05-27 22:14:20 +04:00
|
|
|
--
|
|
|
|
-- 2008/01/01 income
|
|
|
|
-- assets:bank:checking $1
|
|
|
|
-- income:salary
|
|
|
|
--
|
|
|
|
-- 2008/06/01 gift
|
|
|
|
-- assets:bank:checking $1
|
|
|
|
-- income:gifts
|
|
|
|
--
|
|
|
|
-- 2008/06/02 save
|
|
|
|
-- assets:bank:saving $1
|
|
|
|
-- assets:bank:checking
|
|
|
|
--
|
|
|
|
-- 2008/06/03 * eat & shop
|
|
|
|
-- expenses:food $1
|
|
|
|
-- expenses:supplies $1
|
|
|
|
-- assets:cash
|
|
|
|
--
|
2017-07-10 19:43:46 +03:00
|
|
|
-- 2008/10/01 take a loan
|
|
|
|
-- assets:bank:checking $1
|
|
|
|
-- liabilities:debts $-1
|
|
|
|
--
|
2012-05-27 22:14:20 +04:00
|
|
|
-- 2008/12/31 * pay off
|
|
|
|
-- liabilities:debts $1
|
|
|
|
-- assets:bank:checking
|
|
|
|
--
|
2016-12-10 18:04:48 +03:00
|
|
|
Right samplejournal = journalBalanceTransactions False $
|
2012-11-20 01:20:10 +04:00
|
|
|
nulljournal
|
|
|
|
{jtxns = [
|
2012-05-27 22:14:20 +04:00
|
|
|
txnTieKnot $ Transaction {
|
2015-10-30 06:12:46 +03:00
|
|
|
tindex=0,
|
2014-08-01 04:32:42 +04:00
|
|
|
tsourcepos=nullsourcepos,
|
2012-05-27 22:14:20 +04:00
|
|
|
tdate=parsedate "2008/01/01",
|
2012-12-06 08:43:41 +04:00
|
|
|
tdate2=Nothing,
|
2017-06-16 02:25:37 +03:00
|
|
|
tstatus=Unmarked,
|
2012-05-27 22:14:20 +04:00
|
|
|
tcode="",
|
|
|
|
tdescription="income",
|
|
|
|
tcomment="",
|
2012-05-28 02:59:06 +04:00
|
|
|
ttags=[],
|
2012-12-06 04:03:07 +04:00
|
|
|
tpostings=
|
|
|
|
["assets:bank:checking" `post` usd 1
|
|
|
|
,"income:salary" `post` missingamt
|
|
|
|
],
|
2019-01-04 23:01:45 +03:00
|
|
|
tprecedingcomment=""
|
2012-05-27 22:14:20 +04:00
|
|
|
}
|
|
|
|
,
|
|
|
|
txnTieKnot $ Transaction {
|
2015-10-30 06:12:46 +03:00
|
|
|
tindex=0,
|
2014-08-01 04:32:42 +04:00
|
|
|
tsourcepos=nullsourcepos,
|
2012-05-27 22:14:20 +04:00
|
|
|
tdate=parsedate "2008/06/01",
|
2012-12-06 08:43:41 +04:00
|
|
|
tdate2=Nothing,
|
2017-06-16 02:25:37 +03:00
|
|
|
tstatus=Unmarked,
|
2012-05-27 22:14:20 +04:00
|
|
|
tcode="",
|
|
|
|
tdescription="gift",
|
|
|
|
tcomment="",
|
2012-05-28 02:59:06 +04:00
|
|
|
ttags=[],
|
2012-12-06 04:03:07 +04:00
|
|
|
tpostings=
|
|
|
|
["assets:bank:checking" `post` usd 1
|
|
|
|
,"income:gifts" `post` missingamt
|
|
|
|
],
|
2019-01-04 23:01:45 +03:00
|
|
|
tprecedingcomment=""
|
2012-05-27 22:14:20 +04:00
|
|
|
}
|
|
|
|
,
|
|
|
|
txnTieKnot $ Transaction {
|
2015-10-30 06:12:46 +03:00
|
|
|
tindex=0,
|
2014-08-01 04:32:42 +04:00
|
|
|
tsourcepos=nullsourcepos,
|
2012-05-27 22:14:20 +04:00
|
|
|
tdate=parsedate "2008/06/02",
|
2012-12-06 08:43:41 +04:00
|
|
|
tdate2=Nothing,
|
2017-06-16 02:25:37 +03:00
|
|
|
tstatus=Unmarked,
|
2012-05-27 22:14:20 +04:00
|
|
|
tcode="",
|
|
|
|
tdescription="save",
|
|
|
|
tcomment="",
|
2012-05-28 02:59:06 +04:00
|
|
|
ttags=[],
|
2012-12-06 04:03:07 +04:00
|
|
|
tpostings=
|
|
|
|
["assets:bank:saving" `post` usd 1
|
|
|
|
,"assets:bank:checking" `post` usd (-1)
|
|
|
|
],
|
2019-01-04 23:01:45 +03:00
|
|
|
tprecedingcomment=""
|
2012-05-27 22:14:20 +04:00
|
|
|
}
|
|
|
|
,
|
|
|
|
txnTieKnot $ Transaction {
|
2015-10-30 06:12:46 +03:00
|
|
|
tindex=0,
|
2014-08-01 04:32:42 +04:00
|
|
|
tsourcepos=nullsourcepos,
|
2012-05-27 22:14:20 +04:00
|
|
|
tdate=parsedate "2008/06/03",
|
2012-12-06 08:43:41 +04:00
|
|
|
tdate2=Nothing,
|
2015-05-16 21:51:35 +03:00
|
|
|
tstatus=Cleared,
|
2012-05-27 22:14:20 +04:00
|
|
|
tcode="",
|
|
|
|
tdescription="eat & shop",
|
|
|
|
tcomment="",
|
2012-05-28 02:59:06 +04:00
|
|
|
ttags=[],
|
2012-12-06 04:03:07 +04:00
|
|
|
tpostings=["expenses:food" `post` usd 1
|
|
|
|
,"expenses:supplies" `post` usd 1
|
|
|
|
,"assets:cash" `post` missingamt
|
|
|
|
],
|
2019-01-04 23:01:45 +03:00
|
|
|
tprecedingcomment=""
|
2012-05-27 22:14:20 +04:00
|
|
|
}
|
2017-07-10 19:43:46 +03:00
|
|
|
,
|
|
|
|
txnTieKnot $ Transaction {
|
|
|
|
tindex=0,
|
|
|
|
tsourcepos=nullsourcepos,
|
|
|
|
tdate=parsedate "2008/10/01",
|
|
|
|
tdate2=Nothing,
|
|
|
|
tstatus=Unmarked,
|
|
|
|
tcode="",
|
|
|
|
tdescription="take a loan",
|
|
|
|
tcomment="",
|
|
|
|
ttags=[],
|
|
|
|
tpostings=["assets:bank:checking" `post` usd 1
|
|
|
|
,"liabilities:debts" `post` usd (-1)
|
|
|
|
],
|
2019-01-04 23:01:45 +03:00
|
|
|
tprecedingcomment=""
|
2017-07-10 19:43:46 +03:00
|
|
|
}
|
2012-05-27 22:14:20 +04:00
|
|
|
,
|
|
|
|
txnTieKnot $ Transaction {
|
2015-10-30 06:12:46 +03:00
|
|
|
tindex=0,
|
2014-08-01 04:32:42 +04:00
|
|
|
tsourcepos=nullsourcepos,
|
2012-05-27 22:14:20 +04:00
|
|
|
tdate=parsedate "2008/12/31",
|
2012-12-06 08:43:41 +04:00
|
|
|
tdate2=Nothing,
|
2017-06-16 02:25:37 +03:00
|
|
|
tstatus=Unmarked,
|
2012-05-27 22:14:20 +04:00
|
|
|
tcode="",
|
|
|
|
tdescription="pay off",
|
|
|
|
tcomment="",
|
2012-05-28 02:59:06 +04:00
|
|
|
ttags=[],
|
2012-12-06 04:03:07 +04:00
|
|
|
tpostings=["liabilities:debts" `post` usd 1
|
|
|
|
,"assets:bank:checking" `post` usd (-1)
|
|
|
|
],
|
2019-01-04 23:01:45 +03:00
|
|
|
tprecedingcomment=""
|
2012-05-27 22:14:20 +04:00
|
|
|
}
|
|
|
|
]
|
2012-11-20 01:20:10 +04:00
|
|
|
}
|
2012-05-27 22:14:20 +04:00
|
|
|
|
2018-09-06 23:08:26 +03:00
|
|
|
tests_Journal = tests "Journal" [
|
2018-09-04 20:10:10 +03:00
|
|
|
|
|
|
|
test "journalDateSpan" $
|
|
|
|
journalDateSpan True nulljournal{
|
|
|
|
jtxns = [nulltransaction{tdate = parsedate "2014/02/01"
|
|
|
|
,tpostings = [posting{pdate=Just (parsedate "2014/01/10")}]
|
|
|
|
}
|
|
|
|
,nulltransaction{tdate = parsedate "2014/09/01"
|
|
|
|
,tpostings = [posting{pdate2=Just (parsedate "2014/10/10")}]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
`is` (DateSpan (Just $ fromGregorian 2014 1 10) (Just $ fromGregorian 2014 10 11))
|
|
|
|
|
|
|
|
,tests "standard account type queries" $
|
2018-08-15 21:43:29 +03:00
|
|
|
let
|
|
|
|
j = samplejournal
|
|
|
|
journalAccountNamesMatching :: Query -> Journal -> [AccountName]
|
|
|
|
journalAccountNamesMatching q = filter (q `matchesAccount`) . journalAccountNames
|
|
|
|
namesfrom qfunc = journalAccountNamesMatching (qfunc j) j
|
2018-09-04 20:10:10 +03:00
|
|
|
in [
|
|
|
|
test "assets" $ expectEq (namesfrom journalAssetAccountQuery) ["assets","assets:bank","assets:bank:checking","assets:bank:saving","assets:cash"]
|
|
|
|
,test "liabilities" $ expectEq (namesfrom journalLiabilityAccountQuery) ["liabilities","liabilities:debts"]
|
|
|
|
,test "equity" $ expectEq (namesfrom journalEquityAccountQuery) []
|
2018-09-27 23:54:16 +03:00
|
|
|
,test "income" $ expectEq (namesfrom journalRevenueAccountQuery) ["income","income:gifts","income:salary"]
|
2018-09-04 20:10:10 +03:00
|
|
|
,test "expenses" $ expectEq (namesfrom journalExpenseAccountQuery) ["expenses","expenses:food","expenses:supplies"]
|
|
|
|
]
|
|
|
|
|
2018-08-15 21:43:29 +03:00
|
|
|
]
|