lib: negatePostingAmount -> postingNegateMainAmount

This commit is contained in:
Simon Michael 2025-01-29 09:41:51 -10:00
parent 3e838e4d0f
commit 0dca0911f3
3 changed files with 7 additions and 7 deletions

View File

@ -63,7 +63,7 @@ module Hledger.Data.Posting (
-- * arithmetic
sumPostings,
negatePostingAmount,
postingNegateMainAmount,
-- * rendering
showPosting,
showPostingLines,
@ -381,9 +381,9 @@ accountNamesFromPostings = S.toList . S.fromList . map paccount
sumPostings :: [Posting] -> MixedAmount
sumPostings = foldl' (\amt p -> maPlus amt $ pamount p) nullmixedamt
-- | Negate amount in a posting.
negatePostingAmount :: Posting -> Posting
negatePostingAmount = postingTransformAmount negate
-- | Negate the posting's main amount but not the balance assertion amount.
postingNegateMainAmount :: Posting -> Posting
postingNegateMainAmount p@Posting{pamount=a} = p{pamount=negate a}
-- | Strip all prices from a Posting.
postingStripCosts :: Posting -> Posting
@ -532,7 +532,7 @@ postingPriceDirectivesFromCost :: Posting -> [PriceDirective]
postingPriceDirectivesFromCost p@Posting{pamount} =
mapMaybe (amountPriceDirectiveFromCost $ postingDate p) $ amountsRaw pamount
-- | Apply a transform function to this posting's amount.
-- | Apply a transform function to this posting's main amount (but not its balance assertion amount).
postingTransformAmount :: (MixedAmount -> MixedAmount) -> Posting -> Posting
postingTransformAmount f p@Posting{pamount=a} = p{pamount=f a}

View File

@ -162,7 +162,7 @@ accountTransactionsReport rspec@ReportSpec{_rsReportOpts=ropts} j thisacctq = it
. traceAtWith 5 (("ts4:\n"++).pshowTransactions.map snd)
. sortBy (comparing (Down . fst) <> comparing (Down . tindex . snd))
. map (\t -> (transactionRegisterDate wd reportq thisacctq t, t))
. map (if invert_ ropts then (\t -> t{tpostings = map negatePostingAmount $ tpostings t}) else id)
. map (if invert_ ropts then (\t -> t{tpostings = map postingNegateMainAmount $ tpostings t}) else id)
$ jtxns acctJournal
pshowTransactions :: [Transaction] -> String

View File

@ -158,7 +158,7 @@ matchedPostingsBeforeAndDuring rspec@ReportSpec{_rsReportOpts=ropts,_rsQuery=q}
beforestartq = dbg3 "beforestartq" $ dateqtype $ DateSpan Nothing (Exact <$> spanStart reportspan)
beforeandduringps =
sortOn (postingDateOrDate2 (whichDate ropts)) -- sort postings by date or date2
. (if invert_ ropts then map negatePostingAmount else id) -- with --invert, invert amounts
. (if invert_ ropts then map postingNegateMainAmount else id) -- with --invert, invert amounts
. journalPostings
-- With most calls we will not require transaction prices past this point, and can get a big
-- speed improvement by stripping them early. In some cases, such as in hledger-ui, we still