imp:print:beancount: remove virtual postings automatically

This commit is contained in:
Simon Michael 2024-11-08 20:16:33 -10:00
parent b68ebc221b
commit 6e3dfd6703
5 changed files with 24 additions and 16 deletions

View File

@ -21,7 +21,3 @@
# Print beancount output by default. # Print beancount output by default.
[print] [print]
-O beancount -O beancount
# Exclude transactions containing unbalanced/virtual postings.
# You might need to do more if you have mixed balanced/unbalanced transactions.
--real

View File

@ -43,7 +43,7 @@ import Data.Function ((&))
-- $setup -- $setup
-- >>> :set -XOverloadedStrings -- >>> :set -XOverloadedStrings
-- | Like showTransaction, but generates Beancount journal format. -- | Like showTransaction, but applies various adjustments to produce valid Beancount journal data.
showTransactionBeancount :: Transaction -> Text showTransactionBeancount :: Transaction -> Text
showTransactionBeancount t = showTransactionBeancount t =
-- https://beancount.github.io/docs/beancount_language_syntax.html -- https://beancount.github.io/docs/beancount_language_syntax.html

View File

@ -181,23 +181,22 @@ entriesReportAsText = entriesReportAsTextHelper showTransaction
entriesReportAsTextHelper :: (Transaction -> T.Text) -> EntriesReport -> TL.Text entriesReportAsTextHelper :: (Transaction -> T.Text) -> EntriesReport -> TL.Text
entriesReportAsTextHelper showtxn = TB.toLazyText . foldMap (TB.fromText . showtxn) entriesReportAsTextHelper showtxn = TB.toLazyText . foldMap (TB.fromText . showtxn)
-- In addition to rendering the transactions in (best effort) Beancount format, -- This transforms transactions in various ways (see Beancount.hs) to make them Beancount-compatible.
-- this generates an account open directive for each account name used -- It also generates an account open directive for each account used (on their earliest transaction dates).
-- (using the earliest transaction date).
entriesReportAsBeancount :: EntriesReport -> TL.Text entriesReportAsBeancount :: EntriesReport -> TL.Text
entriesReportAsBeancount ts = entriesReportAsBeancount ts =
-- PERF: gathers and converts all account names, then repeats that work when showing each transaction -- PERF: gathers and converts all account names, then repeats that work when showing each transaction
opendirectives <> "\n" <> opendirectives <> "\n" <> entriesReportAsTextHelper showTransactionBeancount allrealts
entriesReportAsTextHelper showTransactionBeancount ts
where where
allrealts = [t{tpostings=filter isReal $ tpostings t} | t <- ts]
opendirectives opendirectives
| null ts = "" | null ts = ""
| otherwise = TL.fromStrict $ T.unlines [ | otherwise = TL.fromStrict $ T.unlines [
firstdate <> " open " <> accountNameToBeancount a firstdate <> " open " <> accountNameToBeancount a
| a <- nubSort $ concatMap (map paccount.tpostings) ts | a <- nubSort $ concatMap (map paccount.tpostings) allrealts
] ]
where where
firstdate = showDate $ minimumDef err $ map tdate ts firstdate = showDate $ minimumDef err $ map tdate allrealts
where err = error' "entriesReportAsBeancount: should not happen" where err = error' "entriesReportAsBeancount: should not happen"
entriesReportAsSql :: EntriesReport -> TL.Text entriesReportAsSql :: EntriesReport -> TL.Text

View File

@ -860,10 +860,8 @@ and prepend/append a "C" when needed.
#### Beancount virtual postings #### Beancount virtual postings
Beancount doesn't allow [unbalanced/virtual postings](#virtual-postings), Beancount doesn't allow [virtual postings](#virtual-postings);
so you will need to comment those, if you have any, they will not appear in beancount output.
or use `--real` to exclude transactions that use them.
(If you have transactions which are a mixture of balanced and unbalanced postings, you'll have to do something more.)
#### Beancount costs #### Beancount costs

View File

@ -44,3 +44,18 @@ $ hledger -f- print -O beancount
>= >=
# ** 4. Virtual postings are dropped.
<
2000-01-01
Assets 0 USD
(a) 1
[b] 2
[c]
$ hledger -f- print -O beancount
2000-01-01 open Assets
2000-01-01 *
Assets 0 USD
>=