fix:print: pad/round amounts with inferred costs like the others (#2123)

This commit is contained in:
Simon Michael 2023-12-07 03:37:19 -10:00
parent cf48fbfad8
commit 22ac8d056d

View File

@ -113,55 +113,52 @@ printEntries opts@CliOpts{rawopts_=rawopts, reportspec_=rspec} j =
where styles0 = journalCommodityStyles j where styles0 = journalCommodityStyles j
fmt = outputFormatFromOpts opts fmt = outputFormatFromOpts opts
render | fmt=="txt" = entriesReportAsText opts . styleAmounts styles render | fmt=="txt" = entriesReportAsText . styleAmounts styles . map maybeoriginalamounts
| fmt=="beancount" = entriesReportAsBeancount opts . styleAmounts styles | fmt=="beancount" = entriesReportAsBeancount . styleAmounts styles . map maybeoriginalamounts
| fmt=="csv" = printCSV . entriesReportAsCsv . styleAmounts styles | fmt=="csv" = printCSV . entriesReportAsCsv . styleAmounts styles
| fmt=="tsv" = printTSV . entriesReportAsCsv . styleAmounts styles | fmt=="tsv" = printTSV . entriesReportAsCsv . styleAmounts styles
| fmt=="json" = toJsonText . styleAmounts styles | fmt=="json" = toJsonText . styleAmounts styles
| fmt=="sql" = entriesReportAsSql . styleAmounts styles | fmt=="sql" = entriesReportAsSql . styleAmounts styles
| otherwise = error' $ unsupportedOutputFormatError fmt -- PARTIAL: | otherwise = error' $ unsupportedOutputFormatError fmt -- PARTIAL:
where
entriesReportAsText :: CliOpts -> EntriesReport -> TL.Text maybeoriginalamounts
entriesReportAsText = entriesReportAsTextHelper showTransaction -- Use the fully inferred and amount-styled/rounded transaction in the following situations:
-- with -x/--explicit:
entriesReportAsTextHelper :: (Transaction -> T.Text) -> CliOpts -> EntriesReport -> TL.Text | boolopt "explicit" (rawopts_ opts) = id
entriesReportAsTextHelper showtxn opts = -- with --show-costs:
TB.toLazyText . foldMap (TB.fromText . showtxn . txntransform) | opts ^. infer_costs = id
where -- with -B/-V/-X/--value ("because of #551, and because of print -V valuing only one posting when there's an implicit txn price.")
txntransform | has (value . _Just) opts = id
-- Use the fully inferred and amount-styled/rounded transaction in the following situations: -- Otherwise, keep the transaction's amounts close to how they were written in the journal.
-- with -x/--explicit: | otherwise = transactionWithMostlyOriginalPostings
| boolopt "explicit" (rawopts_ opts) = id
-- with --show-costs:
| opts ^. infer_costs = id
-- with -B/-V/-X/--value ("because of #551, and because of print -V valuing only one posting when there's an implicit txn price.")
| has (value . _Just) opts = id
-- Otherwise, keep the transaction's amounts close to how they were written in the journal.
| otherwise = transactionWithMostlyOriginalPostings
-- | Replace this transaction's postings with the original postings if any, but keep the -- | Replace this transaction's postings with the original postings if any, but keep the
-- current possibly rewritten account names, and the inferred values of any auto postings. -- current possibly rewritten account names, and the inferred values of any auto postings.
-- This is mainly for showing transactions with the amounts in their original journal format. -- This is mainly for showing transactions with the amounts in their original journal format.
transactionWithMostlyOriginalPostings :: Transaction -> Transaction transactionWithMostlyOriginalPostings :: Transaction -> Transaction
transactionWithMostlyOriginalPostings = transactionMapPostings postingMostlyOriginal transactionWithMostlyOriginalPostings = transactionMapPostings postingMostlyOriginal
-- Get the original posting if any, but keep the current (possibly rewritten) account name,
-- and the amounts of any auto postings.
postingMostlyOriginal p = orig
{ paccount = paccount p
, pamount = pamount $ if isGenerated then p else orig }
where where
orig = originalPosting p postingMostlyOriginal p = orig
isGenerated = "_generated-posting" `elem` map fst (ptags p) { paccount = paccount p
, pamount = pamount $ if isGenerated then p else orig }
where
orig = originalPosting p
isGenerated = "_generated-posting" `elem` map fst (ptags p)
entriesReportAsText :: EntriesReport -> TL.Text
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, -- In addition to rendering the transactions in (best effort) Beancount format,
-- this generates an account open directive for each account name used -- this generates an account open directive for each account name used
-- (using the earliest transaction date). -- (using the earliest transaction date).
entriesReportAsBeancount :: CliOpts -> EntriesReport -> TL.Text entriesReportAsBeancount :: EntriesReport -> TL.Text
entriesReportAsBeancount opts 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 opts ts entriesReportAsTextHelper showTransactionBeancount ts
where where
opendirectives opendirectives
| null ts = "" | null ts = ""