2020-11-24 20:17:01 +03:00
|
|
|
{-# LANGUAGE NamedFieldPuns #-}
|
2009-04-03 14:58:05 +04:00
|
|
|
{-|
|
|
|
|
|
2012-05-14 22:52:22 +04:00
|
|
|
A 'Posting' represents a change (by some 'MixedAmount') of the balance in
|
|
|
|
some 'Account'. Each 'Transaction' contains two or more postings which
|
|
|
|
should add up to 0. Postings reference their parent transaction, so we can
|
|
|
|
look up the date or description there.
|
2009-04-03 14:58:05 +04:00
|
|
|
|
|
|
|
-}
|
|
|
|
|
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
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
2018-03-25 01:51:56 +03:00
|
|
|
{-# LANGUAGE CPP #-}
|
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
|
|
|
|
2012-05-07 18:36:40 +04:00
|
|
|
module Hledger.Data.Posting (
|
|
|
|
-- * Posting
|
|
|
|
nullposting,
|
2012-12-06 04:03:07 +04:00
|
|
|
posting,
|
|
|
|
post,
|
2019-02-22 03:20:04 +03:00
|
|
|
vpost,
|
|
|
|
post',
|
|
|
|
vpost',
|
2018-10-12 06:37:20 +03:00
|
|
|
nullsourcepos,
|
|
|
|
nullassertion,
|
2019-02-22 03:20:04 +03:00
|
|
|
balassert,
|
|
|
|
balassertTot,
|
|
|
|
balassertParInc,
|
|
|
|
balassertTotInc,
|
2012-05-07 18:36:40 +04:00
|
|
|
-- * operations
|
2017-01-13 18:25:44 +03:00
|
|
|
originalPosting,
|
2015-05-16 21:51:35 +03:00
|
|
|
postingStatus,
|
2012-05-07 18:36:40 +04:00
|
|
|
isReal,
|
|
|
|
isVirtual,
|
|
|
|
isBalancedVirtual,
|
|
|
|
isEmptyPosting,
|
2019-02-18 23:11:07 +03:00
|
|
|
hasBalanceAssignment,
|
2012-05-07 18:36:40 +04:00
|
|
|
hasAmount,
|
2012-05-28 04:27:55 +04:00
|
|
|
postingAllTags,
|
|
|
|
transactionAllTags,
|
2012-12-22 04:24:38 +04:00
|
|
|
relatedPostings,
|
2016-12-10 18:04:48 +03:00
|
|
|
removePrices,
|
2020-11-24 20:17:01 +03:00
|
|
|
postingApplyAliases,
|
2012-05-07 18:36:40 +04:00
|
|
|
-- * date operations
|
|
|
|
postingDate,
|
2012-12-06 08:43:41 +04:00
|
|
|
postingDate2,
|
2012-05-07 18:36:40 +04:00
|
|
|
isPostingInDateSpan,
|
2014-04-14 01:57:40 +04:00
|
|
|
isPostingInDateSpan',
|
2012-05-14 22:52:22 +04:00
|
|
|
-- * account name operations
|
2012-05-07 18:36:40 +04:00
|
|
|
accountNamesFromPostings,
|
|
|
|
accountNamePostingType,
|
|
|
|
accountNameWithoutPostingType,
|
|
|
|
accountNameWithPostingType,
|
|
|
|
joinAccountNames,
|
|
|
|
concatAccountNames,
|
|
|
|
accountNameApplyAliases,
|
2015-09-27 04:54:46 +03:00
|
|
|
accountNameApplyAliasesMemo,
|
2019-07-17 20:38:14 +03:00
|
|
|
-- * comment/tag operations
|
|
|
|
commentJoin,
|
|
|
|
commentAddTag,
|
|
|
|
commentAddTagNextLine,
|
2012-05-07 18:36:40 +04:00
|
|
|
-- * arithmetic
|
|
|
|
sumPostings,
|
|
|
|
-- * rendering
|
|
|
|
showPosting,
|
|
|
|
-- * misc.
|
2012-05-14 22:52:22 +04:00
|
|
|
showComment,
|
2019-05-23 22:16:20 +03:00
|
|
|
postingTransformAmount,
|
2019-05-28 21:42:32 +03:00
|
|
|
postingApplyValuation,
|
2019-05-23 22:16:20 +03:00
|
|
|
postingToCost,
|
2018-09-06 23:08:26 +03:00
|
|
|
tests_Posting
|
2012-05-07 18:36:40 +04:00
|
|
|
)
|
2009-04-03 14:58:05 +04:00
|
|
|
where
|
2019-11-12 04:14:21 +03:00
|
|
|
|
2020-08-07 01:27:40 +03:00
|
|
|
import Control.Monad (foldM)
|
2019-11-12 04:14:21 +03:00
|
|
|
import Data.Foldable (asum)
|
2020-01-04 09:09:01 +03:00
|
|
|
import Data.List.Extra (nubSort)
|
2019-05-23 22:16:20 +03:00
|
|
|
import qualified Data.Map as M
|
2012-12-06 04:03:07 +04:00
|
|
|
import Data.Maybe
|
2015-09-27 04:45:44 +03:00
|
|
|
import Data.MemoUgly (memo)
|
2018-03-25 01:51:56 +03:00
|
|
|
#if !(MIN_VERSION_base(4,11,0))
|
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 Data.Monoid
|
2018-03-25 01:51:56 +03:00
|
|
|
#endif
|
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
|
|
|
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
|
2011-05-28 08:11:44 +04:00
|
|
|
import Data.Time.Calendar
|
2011-08-03 03:29:13 +04:00
|
|
|
import Safe
|
2011-05-28 08:11:44 +04:00
|
|
|
|
2019-07-15 13:28:52 +03:00
|
|
|
import Hledger.Utils
|
2010-05-20 03:08:53 +04:00
|
|
|
import Hledger.Data.Types
|
|
|
|
import Hledger.Data.Amount
|
|
|
|
import Hledger.Data.AccountName
|
2010-07-11 22:56:36 +04:00
|
|
|
import Hledger.Data.Dates (nulldate, spanContainsDate)
|
2019-06-15 02:17:06 +03:00
|
|
|
import Hledger.Data.Valuation
|
2009-04-03 14:58:05 +04:00
|
|
|
|
|
|
|
|
|
|
|
|
2012-12-06 04:03:07 +04:00
|
|
|
nullposting, posting :: Posting
|
|
|
|
nullposting = Posting
|
2012-12-06 04:28:23 +04:00
|
|
|
{pdate=Nothing
|
2012-12-06 06:41:37 +04:00
|
|
|
,pdate2=Nothing
|
2017-06-16 02:25:37 +03:00
|
|
|
,pstatus=Unmarked
|
2012-12-06 04:03:07 +04:00
|
|
|
,paccount=""
|
|
|
|
,pamount=nullmixedamt
|
|
|
|
,pcomment=""
|
|
|
|
,ptype=RegularPosting
|
|
|
|
,ptags=[]
|
2013-05-29 03:18:15 +04:00
|
|
|
,pbalanceassertion=Nothing
|
2012-12-06 04:03:07 +04:00
|
|
|
,ptransaction=Nothing
|
2019-02-21 07:07:40 +03:00
|
|
|
,poriginal=Nothing
|
2012-12-06 04:03:07 +04:00
|
|
|
}
|
|
|
|
posting = nullposting
|
|
|
|
|
2019-02-22 03:20:04 +03:00
|
|
|
-- constructors
|
|
|
|
|
|
|
|
-- | Make a posting to an account.
|
2012-12-06 04:03:07 +04:00
|
|
|
post :: AccountName -> Amount -> Posting
|
2019-02-22 03:20:04 +03:00
|
|
|
post acc amt = posting {paccount=acc, pamount=Mixed [amt]}
|
|
|
|
|
|
|
|
-- | Make a virtual (unbalanced) posting to an account.
|
|
|
|
vpost :: AccountName -> Amount -> Posting
|
|
|
|
vpost acc amt = (post acc amt){ptype=VirtualPosting}
|
|
|
|
|
|
|
|
-- | Make a posting to an account, maybe with a balance assertion.
|
|
|
|
post' :: AccountName -> Amount -> Maybe BalanceAssertion -> Posting
|
|
|
|
post' acc amt ass = posting {paccount=acc, pamount=Mixed [amt], pbalanceassertion=ass}
|
|
|
|
|
|
|
|
-- | Make a virtual (unbalanced) posting to an account, maybe with a balance assertion.
|
|
|
|
vpost' :: AccountName -> Amount -> Maybe BalanceAssertion -> Posting
|
|
|
|
vpost' acc amt ass = (post' acc amt ass){ptype=VirtualPosting, pbalanceassertion=ass}
|
2009-04-03 14:58:05 +04:00
|
|
|
|
2018-10-12 06:37:20 +03:00
|
|
|
nullsourcepos :: GenericSourcePos
|
|
|
|
nullsourcepos = JournalSourcePos "" (1,1)
|
|
|
|
|
2019-12-07 20:05:41 +03:00
|
|
|
nullassertion :: BalanceAssertion
|
2018-10-12 06:37:20 +03:00
|
|
|
nullassertion = BalanceAssertion
|
|
|
|
{baamount=nullamt
|
2019-02-15 21:34:40 +03:00
|
|
|
,batotal=False
|
2019-02-18 06:50:22 +03:00
|
|
|
,bainclusive=False
|
2018-10-12 06:37:20 +03:00
|
|
|
,baposition=nullsourcepos
|
|
|
|
}
|
|
|
|
|
2019-02-22 03:20:04 +03:00
|
|
|
-- | Make a partial, exclusive balance assertion.
|
|
|
|
balassert :: Amount -> Maybe BalanceAssertion
|
|
|
|
balassert amt = Just $ nullassertion{baamount=amt}
|
|
|
|
|
|
|
|
-- | Make a total, exclusive balance assertion.
|
|
|
|
balassertTot :: Amount -> Maybe BalanceAssertion
|
|
|
|
balassertTot amt = Just $ nullassertion{baamount=amt, batotal=True}
|
|
|
|
|
|
|
|
-- | Make a partial, inclusive balance assertion.
|
|
|
|
balassertParInc :: Amount -> Maybe BalanceAssertion
|
|
|
|
balassertParInc amt = Just $ nullassertion{baamount=amt, bainclusive=True}
|
|
|
|
|
|
|
|
-- | Make a total, inclusive balance assertion.
|
|
|
|
balassertTotInc :: Amount -> Maybe BalanceAssertion
|
|
|
|
balassertTotInc amt = Just $ nullassertion{baamount=amt, batotal=True, bainclusive=True}
|
|
|
|
|
2017-05-30 23:39:53 +03:00
|
|
|
-- Get the original posting, if any.
|
2017-01-13 18:25:44 +03:00
|
|
|
originalPosting :: Posting -> Posting
|
2019-02-21 07:07:40 +03:00
|
|
|
originalPosting p = fromMaybe p $ poriginal p
|
2017-01-13 18:25:44 +03:00
|
|
|
|
2014-05-07 08:35:38 +04:00
|
|
|
-- XXX once rendered user output, but just for debugging now; clean up
|
2009-04-03 14:58:05 +04:00
|
|
|
showPosting :: Posting -> String
|
2012-05-14 22:52:22 +04:00
|
|
|
showPosting p@Posting{paccount=a,pamount=amt,ptype=t} =
|
2014-05-07 08:35:38 +04:00
|
|
|
unlines $ [concatTopPadded [show (postingDate p) ++ " ", showaccountname a ++ " ", showamount amt, showComment (pcomment p)]]
|
2009-04-03 14:58:05 +04:00
|
|
|
where
|
2009-11-25 09:13:35 +03:00
|
|
|
ledger3ishlayout = False
|
|
|
|
acctnamewidth = if ledger3ishlayout then 25 else 22
|
2020-10-27 08:20:38 +03:00
|
|
|
showaccountname = T.unpack . fitText (Just acctnamewidth) Nothing False False . bracket . elideAccountName width
|
2009-12-19 06:44:52 +03:00
|
|
|
(bracket,width) = case t of
|
2020-10-27 08:20:38 +03:00
|
|
|
BalancedVirtualPosting -> (wrap "[" "]", acctnamewidth-2)
|
|
|
|
VirtualPosting -> (wrap "(" ")", acctnamewidth-2)
|
|
|
|
_ -> (id,acctnamewidth)
|
2020-11-02 06:16:46 +03:00
|
|
|
showamount = fst . showMixed showAmount (Just 12) Nothing False
|
2012-05-14 22:52:22 +04:00
|
|
|
|
|
|
|
|
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
|
|
|
showComment :: Text -> String
|
|
|
|
showComment t = if T.null t then "" else " ;" ++ T.unpack t
|
2012-05-14 22:52:22 +04:00
|
|
|
|
2009-04-03 14:58:05 +04:00
|
|
|
isReal :: Posting -> Bool
|
|
|
|
isReal p = ptype p == RegularPosting
|
|
|
|
|
2009-05-17 03:12:42 +04:00
|
|
|
isVirtual :: Posting -> Bool
|
|
|
|
isVirtual p = ptype p == VirtualPosting
|
|
|
|
|
|
|
|
isBalancedVirtual :: Posting -> Bool
|
|
|
|
isBalancedVirtual p = ptype p == BalancedVirtualPosting
|
|
|
|
|
2009-04-03 14:58:05 +04:00
|
|
|
hasAmount :: Posting -> Bool
|
2012-05-27 22:14:20 +04:00
|
|
|
hasAmount = (/= missingmixedamt) . pamount
|
2009-05-25 21:28:41 +04:00
|
|
|
|
2019-02-18 23:11:07 +03:00
|
|
|
hasBalanceAssignment :: Posting -> Bool
|
|
|
|
hasBalanceAssignment p = not (hasAmount p) && isJust (pbalanceassertion p)
|
2016-12-10 18:04:48 +03:00
|
|
|
|
2017-12-29 23:16:46 +03:00
|
|
|
-- | Sorted unique account names referenced by these postings.
|
2009-12-19 08:57:54 +03:00
|
|
|
accountNamesFromPostings :: [Posting] -> [AccountName]
|
2020-01-04 09:09:01 +03:00
|
|
|
accountNamesFromPostings = nubSort . map paccount
|
2009-12-19 08:57:54 +03:00
|
|
|
|
|
|
|
sumPostings :: [Posting] -> MixedAmount
|
2017-01-13 03:24:53 +03:00
|
|
|
sumPostings = sumStrict . map pamount
|
2009-12-19 08:57:54 +03:00
|
|
|
|
2016-12-10 18:04:48 +03:00
|
|
|
-- | Remove all prices of a posting
|
|
|
|
removePrices :: Posting -> Posting
|
|
|
|
removePrices p = p{ pamount = Mixed $ remove <$> amounts (pamount p) }
|
2019-06-08 00:23:19 +03:00
|
|
|
where remove a = a { aprice = Nothing }
|
2016-12-10 18:04:48 +03:00
|
|
|
|
2012-12-06 06:41:37 +04:00
|
|
|
-- | Get a posting's (primary) date - it's own primary date if specified,
|
|
|
|
-- otherwise the parent transaction's primary date, or the null date if
|
|
|
|
-- there is no parent transaction.
|
2009-12-19 08:57:54 +03:00
|
|
|
postingDate :: Posting -> Day
|
2019-11-12 04:14:21 +03:00
|
|
|
postingDate p = fromMaybe nulldate $ asum dates
|
|
|
|
where dates = [ pdate p, tdate <$> ptransaction p ]
|
2009-12-19 08:57:54 +03:00
|
|
|
|
2012-12-06 08:43:41 +04:00
|
|
|
-- | Get a posting's secondary (secondary) date, which is the first of:
|
2012-12-06 06:41:37 +04:00
|
|
|
-- posting's secondary date, transaction's secondary date, posting's
|
|
|
|
-- primary date, transaction's primary date, or the null date if there is
|
|
|
|
-- no parent transaction.
|
2012-12-06 08:43:41 +04:00
|
|
|
postingDate2 :: Posting -> Day
|
2019-11-12 04:14:21 +03:00
|
|
|
postingDate2 p = fromMaybe nulldate $ asum dates
|
|
|
|
where dates = [ pdate2 p
|
|
|
|
, tdate2 =<< ptransaction p
|
|
|
|
, pdate p
|
|
|
|
, tdate <$> ptransaction p
|
2012-12-06 06:41:37 +04:00
|
|
|
]
|
2012-12-06 05:10:15 +04:00
|
|
|
|
2017-06-16 02:25:37 +03:00
|
|
|
-- | Get a posting's status. This is cleared or pending if those are
|
|
|
|
-- explicitly set on the posting, otherwise the status of its parent
|
|
|
|
-- transaction, or unmarked if there is no parent transaction. (Note
|
2019-07-15 13:28:52 +03:00
|
|
|
-- the ambiguity, unmarked can mean "posting and transaction are both
|
2017-06-16 02:25:37 +03:00
|
|
|
-- unmarked" or "posting is unmarked and don't know about the transaction".
|
2017-06-16 02:54:34 +03:00
|
|
|
postingStatus :: Posting -> Status
|
2015-05-16 21:51:35 +03:00
|
|
|
postingStatus Posting{pstatus=s, ptransaction=mt}
|
2017-06-16 02:25:37 +03:00
|
|
|
| s == Unmarked = case mt of Just t -> tstatus t
|
|
|
|
Nothing -> Unmarked
|
2015-05-16 21:51:35 +03:00
|
|
|
| otherwise = s
|
|
|
|
|
2012-05-28 04:27:55 +04:00
|
|
|
-- | Tags for this posting including any inherited from its parent transaction.
|
|
|
|
postingAllTags :: Posting -> [Tag]
|
2014-03-06 02:43:58 +04:00
|
|
|
postingAllTags p = ptags p ++ maybe [] ttags (ptransaction p)
|
2012-05-28 04:27:55 +04:00
|
|
|
|
2014-03-02 05:42:13 +04:00
|
|
|
-- | Tags for this transaction including any from its postings.
|
2012-05-28 04:27:55 +04:00
|
|
|
transactionAllTags :: Transaction -> [Tag]
|
2014-03-02 05:42:13 +04:00
|
|
|
transactionAllTags t = ttags t ++ concatMap ptags (tpostings t)
|
2012-05-28 04:27:55 +04:00
|
|
|
|
2012-12-22 04:24:38 +04:00
|
|
|
-- Get the other postings from this posting's transaction.
|
|
|
|
relatedPostings :: Posting -> [Posting]
|
|
|
|
relatedPostings p@Posting{ptransaction=Just t} = filter (/= p) $ tpostings t
|
|
|
|
relatedPostings _ = []
|
|
|
|
|
2009-12-19 08:57:54 +03:00
|
|
|
-- | Does this posting fall within the given date span ?
|
|
|
|
isPostingInDateSpan :: DateSpan -> Posting -> Bool
|
2019-11-12 04:14:21 +03:00
|
|
|
isPostingInDateSpan = isPostingInDateSpan' PrimaryDate
|
2009-12-21 08:23:07 +03:00
|
|
|
|
2014-04-14 01:57:40 +04:00
|
|
|
-- --date2-sensitive version, separate for now to avoid disturbing multiBalanceReport.
|
|
|
|
isPostingInDateSpan' :: WhichDate -> DateSpan -> Posting -> Bool
|
|
|
|
isPostingInDateSpan' PrimaryDate s = spanContainsDate s . postingDate
|
|
|
|
isPostingInDateSpan' SecondaryDate s = spanContainsDate s . postingDate2
|
|
|
|
|
2009-12-21 08:23:07 +03:00
|
|
|
isEmptyPosting :: Posting -> Bool
|
2020-05-30 04:57:22 +03:00
|
|
|
isEmptyPosting = mixedAmountLooksZero . pamount
|
2009-12-21 08:23:07 +03:00
|
|
|
|
2011-08-05 04:05:39 +04:00
|
|
|
-- AccountName stuff that depends on PostingType
|
2011-08-03 03:29:13 +04:00
|
|
|
|
|
|
|
accountNamePostingType :: AccountName -> PostingType
|
|
|
|
accountNamePostingType a
|
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
|
|
|
| T.null a = RegularPosting
|
|
|
|
| T.head a == '[' && T.last a == ']' = BalancedVirtualPosting
|
|
|
|
| T.head a == '(' && T.last a == ')' = VirtualPosting
|
2011-08-03 03:29:13 +04:00
|
|
|
| otherwise = RegularPosting
|
|
|
|
|
|
|
|
accountNameWithoutPostingType :: AccountName -> AccountName
|
|
|
|
accountNameWithoutPostingType a = case accountNamePostingType a of
|
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
|
|
|
BalancedVirtualPosting -> T.init $ T.tail a
|
|
|
|
VirtualPosting -> T.init $ T.tail a
|
2011-08-03 03:29:13 +04:00
|
|
|
RegularPosting -> a
|
|
|
|
|
|
|
|
accountNameWithPostingType :: PostingType -> AccountName -> AccountName
|
2020-10-27 08:20:38 +03:00
|
|
|
accountNameWithPostingType BalancedVirtualPosting = wrap "[" "]" . accountNameWithoutPostingType
|
|
|
|
accountNameWithPostingType VirtualPosting = wrap "(" ")" . accountNameWithoutPostingType
|
|
|
|
accountNameWithPostingType RegularPosting = accountNameWithoutPostingType
|
2011-08-03 03:29:13 +04:00
|
|
|
|
|
|
|
-- | Prefix one account name to another, preserving posting type
|
|
|
|
-- indicators like concatAccountNames.
|
|
|
|
joinAccountNames :: AccountName -> AccountName -> AccountName
|
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
|
|
|
joinAccountNames a b = concatAccountNames $ filter (not . T.null) [a,b]
|
2011-08-03 03:29:13 +04:00
|
|
|
|
|
|
|
-- | Join account names into one. If any of them has () or [] posting type
|
|
|
|
-- indicators, these (the first type encountered) will also be applied to
|
|
|
|
-- the resulting account name.
|
|
|
|
concatAccountNames :: [AccountName] -> AccountName
|
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
|
|
|
concatAccountNames as = accountNameWithPostingType t $ T.intercalate ":" $ map accountNameWithoutPostingType as
|
2011-08-03 03:29:13 +04:00
|
|
|
where t = headDef RegularPosting $ filter (/= RegularPosting) $ map accountNamePostingType as
|
|
|
|
|
2020-11-24 20:17:01 +03:00
|
|
|
-- | Apply some account aliases to the posting's account name, as described by accountNameApplyAliases.
|
2020-11-26 07:59:07 +03:00
|
|
|
-- This can fail due to a bad replacement pattern in a regular expression alias.
|
|
|
|
postingApplyAliases :: [AccountAlias] -> Posting -> Either RegexError Posting
|
2020-11-24 20:17:01 +03:00
|
|
|
postingApplyAliases aliases p@Posting{paccount} =
|
|
|
|
case accountNameApplyAliases aliases paccount of
|
2020-11-26 07:59:07 +03:00
|
|
|
Right a -> Right p{paccount=a}
|
|
|
|
Left e -> Left err
|
2020-11-24 20:17:01 +03:00
|
|
|
where
|
2020-11-26 07:59:07 +03:00
|
|
|
err = "problem while applying account aliases:\n" ++ pshow aliases
|
|
|
|
++ "\n to account name: "++T.unpack paccount++"\n "++e
|
2020-11-24 20:17:01 +03:00
|
|
|
|
2015-05-14 22:50:32 +03:00
|
|
|
-- | Rewrite an account name using all matching aliases from the given list, in sequence.
|
|
|
|
-- Each alias sees the result of applying the previous aliases.
|
2020-08-07 01:27:40 +03:00
|
|
|
-- Or, return any error arising from a bad regular expression in the aliases.
|
|
|
|
accountNameApplyAliases :: [AccountAlias] -> AccountName -> Either RegexError AccountName
|
|
|
|
accountNameApplyAliases aliases a =
|
|
|
|
let (aname,atype) = (accountNameWithoutPostingType a, accountNamePostingType a)
|
|
|
|
in foldM
|
|
|
|
(\acct alias -> dbg6 "result" $ aliasReplace (dbg6 "alias" alias) (dbg6 "account" acct))
|
|
|
|
aname
|
|
|
|
aliases
|
|
|
|
>>= Right . accountNameWithPostingType atype
|
2015-05-14 22:50:32 +03:00
|
|
|
|
2015-09-27 04:54:46 +03:00
|
|
|
-- | Memoising version of accountNameApplyAliases, maybe overkill.
|
2020-08-07 01:27:40 +03:00
|
|
|
accountNameApplyAliasesMemo :: [AccountAlias] -> AccountName -> Either RegexError AccountName
|
2015-09-27 04:54:46 +03:00
|
|
|
accountNameApplyAliasesMemo aliases = memo (accountNameApplyAliases aliases)
|
2019-08-19 04:16:39 +03:00
|
|
|
-- XXX re-test this memoisation
|
2015-09-27 04:54:46 +03:00
|
|
|
|
2015-05-14 22:50:32 +03:00
|
|
|
-- aliasMatches :: AccountAlias -> AccountName -> Bool
|
|
|
|
-- aliasMatches (BasicAlias old _) a = old `isAccountNamePrefixOf` a
|
|
|
|
-- aliasMatches (RegexAlias re _) a = regexMatchesCI re a
|
|
|
|
|
2020-08-07 01:27:40 +03:00
|
|
|
aliasReplace :: AccountAlias -> AccountName -> Either RegexError AccountName
|
2015-05-28 20:39:12 +03:00
|
|
|
aliasReplace (BasicAlias old new) a
|
2020-08-07 01:27:40 +03:00
|
|
|
| old `isAccountNamePrefixOf` a || old == a =
|
|
|
|
Right $ new <> T.drop (T.length old) a
|
|
|
|
| otherwise = Right a
|
|
|
|
aliasReplace (RegexAlias re repl) a =
|
2020-09-01 04:36:34 +03:00
|
|
|
fmap T.pack . regexReplace re repl $ T.unpack a -- XXX
|
2014-10-29 18:20:27 +03:00
|
|
|
|
2019-09-05 23:41:36 +03:00
|
|
|
-- | Apply a specified valuation to this posting's amount, using the
|
|
|
|
-- provided price oracle, commodity styles, reference dates, and
|
|
|
|
-- whether this is for a multiperiod report or not. See
|
|
|
|
-- amountApplyValuation.
|
2020-12-17 09:13:06 +03:00
|
|
|
postingApplyValuation :: PriceOracle -> M.Map CommoditySymbol AmountStyle -> Day -> Day -> Posting -> ValuationType -> Posting
|
|
|
|
postingApplyValuation priceoracle styles periodlast today p v =
|
2019-05-28 21:42:32 +03:00
|
|
|
case v of
|
2020-12-17 09:13:06 +03:00
|
|
|
AtCost Nothing -> postingToCost styles p
|
|
|
|
AtCost mc -> postingValueAtDate priceoracle styles mc periodlast $ postingToCost styles p
|
|
|
|
AtThen mc -> postingValueAtDate priceoracle styles mc (postingDate p) p
|
|
|
|
AtEnd mc -> postingValueAtDate priceoracle styles mc periodlast p
|
|
|
|
AtNow mc -> postingValueAtDate priceoracle styles mc today p
|
|
|
|
AtDate d mc -> postingValueAtDate priceoracle styles mc d p
|
2019-05-28 21:42:32 +03:00
|
|
|
|
|
|
|
-- | Convert this posting's amount to cost, and apply the appropriate amount styles.
|
|
|
|
postingToCost :: M.Map CommoditySymbol AmountStyle -> Posting -> Posting
|
2020-06-01 01:48:08 +03:00
|
|
|
postingToCost styles p@Posting{pamount=a} = p{pamount=styleMixedAmount styles $ mixedAmountCost a}
|
2019-05-23 22:16:20 +03:00
|
|
|
|
2019-06-02 01:28:10 +03:00
|
|
|
-- | Convert this posting's amount to market value in the given commodity,
|
|
|
|
-- or the default valuation commodity, at the given valuation date,
|
2019-08-19 04:16:39 +03:00
|
|
|
-- using the given market price oracle.
|
2019-06-02 01:28:10 +03:00
|
|
|
-- When market prices available on that date are not sufficient to
|
|
|
|
-- calculate the value, amounts are left unchanged.
|
2019-08-19 04:16:39 +03:00
|
|
|
postingValueAtDate :: PriceOracle -> M.Map CommoditySymbol AmountStyle -> Maybe CommoditySymbol -> Day -> Posting -> Posting
|
|
|
|
postingValueAtDate priceoracle styles mc d p = postingTransformAmount (mixedAmountValueAtDate priceoracle styles mc d) p
|
2019-05-23 22:16:20 +03:00
|
|
|
|
2019-05-28 21:42:32 +03:00
|
|
|
-- | Apply a transform function to this posting's amount.
|
|
|
|
postingTransformAmount :: (MixedAmount -> MixedAmount) -> Posting -> Posting
|
|
|
|
postingTransformAmount f p@Posting{pamount=a} = p{pamount=f a}
|
2019-05-23 22:16:20 +03:00
|
|
|
|
2019-07-17 20:38:14 +03:00
|
|
|
-- | Join two parts of a comment, eg a tag and another tag, or a tag
|
|
|
|
-- and a non-tag, on a single line. Interpolates a comma and space
|
|
|
|
-- unless one of the parts is empty.
|
|
|
|
commentJoin :: Text -> Text -> Text
|
|
|
|
commentJoin c1 c2
|
|
|
|
| T.null c1 = c2
|
|
|
|
| T.null c2 = c1
|
|
|
|
| otherwise = c1 <> ", " <> c2
|
|
|
|
|
|
|
|
-- | Add a tag to a comment, comma-separated from any prior content.
|
2019-10-02 09:30:31 +03:00
|
|
|
-- A space is inserted following the colon, before the value.
|
2019-07-17 20:38:14 +03:00
|
|
|
commentAddTag :: Text -> Tag -> Text
|
|
|
|
commentAddTag c (t,v)
|
|
|
|
| T.null c' = tag
|
|
|
|
| otherwise = c' `commentJoin` tag
|
|
|
|
where
|
2020-07-16 13:51:48 +03:00
|
|
|
c' = T.stripEnd c
|
2019-10-02 09:30:31 +03:00
|
|
|
tag = t <> ": " <> v
|
2019-07-17 20:38:14 +03:00
|
|
|
|
|
|
|
-- | Add a tag on its own line to a comment, preserving any prior content.
|
2019-10-02 09:30:31 +03:00
|
|
|
-- A space is inserted following the colon, before the value.
|
2019-07-17 20:38:14 +03:00
|
|
|
commentAddTagNextLine :: Text -> Tag -> Text
|
|
|
|
commentAddTagNextLine cmt (t,v) =
|
2019-10-02 09:30:31 +03:00
|
|
|
cmt <> if "\n" `T.isSuffixOf` cmt then "" else "\n" <> t <> ": " <> v
|
2019-07-17 20:38:14 +03:00
|
|
|
|
2011-08-05 04:05:39 +04:00
|
|
|
|
2018-09-04 20:26:22 +03:00
|
|
|
-- tests
|
2011-08-03 03:29:13 +04:00
|
|
|
|
2018-09-06 23:08:26 +03:00
|
|
|
tests_Posting = tests "Posting" [
|
2018-09-04 20:26:22 +03:00
|
|
|
|
2019-11-29 02:29:03 +03:00
|
|
|
test "accountNamePostingType" $ do
|
2019-11-27 23:46:29 +03:00
|
|
|
accountNamePostingType "a" @?= RegularPosting
|
|
|
|
accountNamePostingType "(a)" @?= VirtualPosting
|
|
|
|
accountNamePostingType "[a]" @?= BalancedVirtualPosting
|
|
|
|
|
2019-11-29 02:29:03 +03:00
|
|
|
,test "accountNameWithoutPostingType" $ do
|
2019-11-27 23:46:29 +03:00
|
|
|
accountNameWithoutPostingType "(a)" @?= "a"
|
|
|
|
|
2019-11-29 02:29:03 +03:00
|
|
|
,test "accountNameWithPostingType" $ do
|
2019-11-27 23:46:29 +03:00
|
|
|
accountNameWithPostingType VirtualPosting "[a]" @?= "(a)"
|
|
|
|
|
2019-11-29 02:29:03 +03:00
|
|
|
,test "joinAccountNames" $ do
|
2019-11-27 23:46:29 +03:00
|
|
|
"a" `joinAccountNames` "b:c" @?= "a:b:c"
|
|
|
|
"a" `joinAccountNames` "(b:c)" @?= "(a:b:c)"
|
|
|
|
"[a]" `joinAccountNames` "(b:c)" @?= "[a:b:c]"
|
|
|
|
"" `joinAccountNames` "a" @?= "a"
|
|
|
|
|
2019-11-29 02:29:03 +03:00
|
|
|
,test "concatAccountNames" $ do
|
2019-11-27 23:46:29 +03:00
|
|
|
concatAccountNames [] @?= ""
|
|
|
|
concatAccountNames ["a","(b)","[c:d]"] @?= "(a:b:c:d)"
|
|
|
|
|
2019-11-29 02:29:03 +03:00
|
|
|
,test "commentAddTag" $ do
|
2019-11-27 23:46:29 +03:00
|
|
|
commentAddTag "" ("a","") @?= "a: "
|
|
|
|
commentAddTag "[1/2]" ("a","") @?= "[1/2], a: "
|
|
|
|
|
2019-11-29 02:29:03 +03:00
|
|
|
,test "commentAddTagNextLine" $ do
|
2019-11-27 23:46:29 +03:00
|
|
|
commentAddTagNextLine "" ("a","") @?= "\na: "
|
|
|
|
commentAddTagNextLine "[1/2]" ("a","") @?= "[1/2]\na: "
|
|
|
|
|
2010-12-27 23:26:22 +03:00
|
|
|
]
|
2014-09-11 00:07:53 +04:00
|
|
|
|