From 6e3dfd6703c3a2217ec21afdb9baa05125e8dcc5 Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Fri, 8 Nov 2024 20:16:33 -1000 Subject: [PATCH] imp:print:beancount: remove virtual postings automatically --- examples/hledger2beancount.conf | 4 ---- hledger-lib/Hledger/Write/Beancount.hs | 2 +- hledger/Hledger/Cli/Commands/Print.hs | 13 ++++++------- hledger/hledger.m4.md | 6 ++---- hledger/test/print/beancount.test | 15 +++++++++++++++ 5 files changed, 24 insertions(+), 16 deletions(-) diff --git a/examples/hledger2beancount.conf b/examples/hledger2beancount.conf index 314c3657d..c3e2448cb 100755 --- a/examples/hledger2beancount.conf +++ b/examples/hledger2beancount.conf @@ -21,7 +21,3 @@ # Print beancount output by default. [print] -O beancount - -# Exclude transactions containing unbalanced/virtual postings. -# You might need to do more if you have mixed balanced/unbalanced transactions. ---real diff --git a/hledger-lib/Hledger/Write/Beancount.hs b/hledger-lib/Hledger/Write/Beancount.hs index b51536105..c8e149742 100644 --- a/hledger-lib/Hledger/Write/Beancount.hs +++ b/hledger-lib/Hledger/Write/Beancount.hs @@ -43,7 +43,7 @@ import Data.Function ((&)) -- $setup -- >>> :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 t = -- https://beancount.github.io/docs/beancount_language_syntax.html diff --git a/hledger/Hledger/Cli/Commands/Print.hs b/hledger/Hledger/Cli/Commands/Print.hs index 3c81ebb5c..3de850bdf 100644 --- a/hledger/Hledger/Cli/Commands/Print.hs +++ b/hledger/Hledger/Cli/Commands/Print.hs @@ -181,23 +181,22 @@ entriesReportAsText = entriesReportAsTextHelper showTransaction entriesReportAsTextHelper :: (Transaction -> T.Text) -> EntriesReport -> TL.Text entriesReportAsTextHelper showtxn = TB.toLazyText . foldMap (TB.fromText . showtxn) --- In addition to rendering the transactions in (best effort) Beancount format, --- this generates an account open directive for each account name used --- (using the earliest transaction date). +-- This transforms transactions in various ways (see Beancount.hs) to make them Beancount-compatible. +-- It also generates an account open directive for each account used (on their earliest transaction dates). entriesReportAsBeancount :: EntriesReport -> TL.Text entriesReportAsBeancount ts = -- PERF: gathers and converts all account names, then repeats that work when showing each transaction - opendirectives <> "\n" <> - entriesReportAsTextHelper showTransactionBeancount ts + opendirectives <> "\n" <> entriesReportAsTextHelper showTransactionBeancount allrealts where + allrealts = [t{tpostings=filter isReal $ tpostings t} | t <- ts] opendirectives | null ts = "" | otherwise = TL.fromStrict $ T.unlines [ firstdate <> " open " <> accountNameToBeancount a - | a <- nubSort $ concatMap (map paccount.tpostings) ts + | a <- nubSort $ concatMap (map paccount.tpostings) allrealts ] where - firstdate = showDate $ minimumDef err $ map tdate ts + firstdate = showDate $ minimumDef err $ map tdate allrealts where err = error' "entriesReportAsBeancount: should not happen" entriesReportAsSql :: EntriesReport -> TL.Text diff --git a/hledger/hledger.m4.md b/hledger/hledger.m4.md index 1a007ef5d..8dcbd569b 100644 --- a/hledger/hledger.m4.md +++ b/hledger/hledger.m4.md @@ -860,10 +860,8 @@ and prepend/append a "C" when needed. #### Beancount virtual postings -Beancount doesn't allow [unbalanced/virtual postings](#virtual-postings), -so you will need to comment those, -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 doesn't allow [virtual postings](#virtual-postings); +if you have any, they will not appear in beancount output. #### Beancount costs diff --git a/hledger/test/print/beancount.test b/hledger/test/print/beancount.test index ce1333fe2..e87cd1c7e 100644 --- a/hledger/test/print/beancount.test +++ b/hledger/test/print/beancount.test @@ -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 + +>=