From 94ab8b1ed721ec29d33af82cf9f76594b3b46f2e Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Tue, 19 Sep 2023 07:47:44 +0100 Subject: [PATCH] dev: journalApplyCommodityStyles -> journalStyleAmounts --- hledger-lib/Hledger/Data/Balancing.hs | 3 ++- hledger-lib/Hledger/Data/Journal.hs | 23 +++++++++++---------- hledger-lib/Hledger/Read/Common.hs | 5 ++++- hledger-lib/Hledger/Reports/BudgetReport.hs | 2 +- 4 files changed, 19 insertions(+), 14 deletions(-) diff --git a/hledger-lib/Hledger/Data/Balancing.hs b/hledger-lib/Hledger/Data/Balancing.hs index 77982e332..f773eef6f 100644 --- a/hledger-lib/Hledger/Data/Balancing.hs +++ b/hledger-lib/Hledger/Data/Balancing.hs @@ -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 diff --git a/hledger-lib/Hledger/Data/Journal.hs b/hledger-lib/Hledger/Data/Journal.hs index 9b67b9aa0..f45d3bc4f 100644 --- a/hledger-lib/Hledger/Data/Journal.hs +++ b/hledger-lib/Hledger/Data/Journal.hs @@ -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, diff --git a/hledger-lib/Hledger/Read/Common.hs b/hledger-lib/Hledger/Read/Common.hs index fd2ef41fb..c00d322b7 100644 --- a/hledger-lib/Hledger/Read/Common.hs +++ b/hledger-lib/Hledger/Read/Common.hs @@ -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) diff --git a/hledger-lib/Hledger/Reports/BudgetReport.hs b/hledger-lib/Hledger/Reports/BudgetReport.hs index e3974f482..bf8024618 100644 --- a/hledger-lib/Hledger/Reports/BudgetReport.hs +++ b/hledger-lib/Hledger/Reports/BudgetReport.hs @@ -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