dev: journalApplyCommodityStyles -> journalStyleAmounts

This commit is contained in:
Simon Michael 2023-09-19 07:47:44 +01:00
parent f8027abb44
commit 94ab8b1ed7
4 changed files with 19 additions and 14 deletions

View File

@ -441,11 +441,12 @@ updateTransactionB t = withRunningBalance $ \BalancingState{bsTransactions} ->
-- and (optional) check that all balance assertions pass.
-- Or, return an error message (just the first error encountered).
--
-- Assumes journalInferCommodityStyles has been called, since those
-- Assumes journalStyleAmounts has been called, since amount styles
-- affect transaction balancing.
--
-- This does multiple things at once because amount inferring, balance
-- assignments, balance assertions and posting dates are interdependent.
--
journalBalanceTransactions :: BalancingOpts -> Journal -> Either String Journal
journalBalanceTransactions bopts' j' =
let

View File

@ -23,7 +23,7 @@ module Hledger.Data.Journal (
addTransaction,
journalInferMarketPricesFromTransactions,
journalInferCommodityStyles,
journalApplyCommodityStyles,
journalStyleAmounts,
commodityStylesFromAmounts,
journalCommodityStyles,
journalToCost,
@ -793,18 +793,19 @@ journalModifyTransactions verbosetags d j =
Right ts -> Right j{jtxns=ts}
Left err -> Left err
-- | Choose and apply a consistent display style to the posting
-- amounts in each commodity (see journalCommodityStyles),
-- keeping all display precisions unchanged.
-- Can return an error message eg if inconsistent number formats are found.
journalApplyCommodityStyles :: Journal -> Either String Journal
journalApplyCommodityStyles = fmap fixjournal . journalInferCommodityStyles
-- | Apply this journal's commodity display styles to all of its amounts.
-- This does soft rounding (adding/removing decimal zeros, but not losing significant decimal digits);
-- it is suitable for an early cleanup pass before calculations.
-- Reports may want to do additional rounding/styling at render time.
-- This can return an error message eg if inconsistent number formats are found.
journalStyleAmounts :: Journal -> Either String Journal
journalStyleAmounts = fmap journalapplystyles . journalInferCommodityStyles
where
fixjournal j@Journal{jpricedirectives=pds} =
journalMapPostings (postingApplyCommodityStylesExceptPrecision styles) j{jpricedirectives=map fixpricedirective pds}
journalapplystyles j@Journal{jpricedirectives=pds} =
journalMapPostings (postingStyleAmounts styles) j{jpricedirectives=map fixpricedirective pds}
where
styles = journalCommodityStyles j
fixpricedirective pd@PriceDirective{pdamount=a} = pd{pdamount=amountSetStylesExceptPrecision styles a}
styles = journalCommodityStylesWith NoRounding j -- defer rounding, in case of print --round=none
fixpricedirective pd@PriceDirective{pdamount=a} = pd{pdamount=styleAmounts styles a}
-- | Get the canonical amount styles for this journal, whether (in order of precedence):
-- set globally in InputOpts,

View File

@ -24,6 +24,7 @@ Some of these might belong in Hledger.Read.JournalReader or Hledger.Read.
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE TypeFamilies #-}
{-# OPTIONS_GHC -Wno-unrecognised-pragmas #-}
--- ** exports
module Hledger.Read.Common (
@ -317,12 +318,13 @@ journalFinalise :: InputOpts -> FilePath -> Text -> ParsedJournal -> ExceptT Str
journalFinalise iopts@InputOpts{..} f txt pj = do
t <- liftIO getPOSIXTime
liftEither $ do
{-# HLINT ignore "Functor law" #-}
j <- pj{jglobalcommoditystyles=fromMaybe mempty $ commodity_styles_ balancingopts_}
& journalSetLastReadTime t -- save the last read time
& journalAddFile (f, txt) -- save the main file's info
& journalReverse -- convert all lists to the order they were parsed
& journalAddAccountTypes -- build a map of all known account types
& journalApplyCommodityStyles -- Infer and apply commodity styles - should be done early
& journalStyleAmounts -- Infer and apply commodity styles - should be done early
<&> journalAddForecast (verbose_tags_) (forecastPeriod iopts pj) -- Add forecast transactions if enabled
<&> journalPostingsAddAccountTags -- Add account tags to postings, so they can be matched by auto postings.
>>= (if auto_ && not (null $ jtxnmodifiers pj)
@ -370,6 +372,7 @@ journalAddForecast :: Bool -> Maybe DateSpan -> Journal -> Journal
journalAddForecast _ Nothing j = j
journalAddForecast verbosetags (Just forecastspan) j = j{jtxns = jtxns j ++ forecasttxns}
where
{-# HLINT ignore "Move concatMap out" #-}
forecasttxns =
map (txnTieKnot . transactionTransformPostings (postingApplyCommodityStyles $ journalCommodityStyles j))
. filter (spanContainsDate forecastspan . tdate)

View File

@ -110,7 +110,7 @@ budgetReport rspec bopts reportspan j = dbg4 "sortedbudgetreport" budgetreport
journalAddBudgetGoalTransactions :: BalancingOpts -> ReportOpts -> DateSpan -> Journal -> Journal
journalAddBudgetGoalTransactions bopts ropts reportspan j =
either error' id $ -- PARTIAL:
(journalApplyCommodityStyles >=> journalBalanceTransactions bopts) j{ jtxns = budgetts }
(journalStyleAmounts >=> journalBalanceTransactions bopts) j{ jtxns = budgetts }
where
budgetspan = dbg3 "budget span" $ DateSpan (Exact <$> mbudgetgoalsstartdate) (Exact <$> spanEnd reportspan)
where