diff --git a/hledger-lib/Hledger/Data/Balancing.hs b/hledger-lib/Hledger/Data/Balancing.hs index fe6337e42..4e54d4b18 100644 --- a/hledger-lib/Hledger/Data/Balancing.hs +++ b/hledger-lib/Hledger/Data/Balancing.hs @@ -19,7 +19,7 @@ module Hledger.Data.Balancing , balanceTransaction , balanceTransactionHelper -- * assertion validation -, checkAssertions +, transactionCheckAssertions -- * journal balancing , journalBalanceTransactions -- * tests @@ -150,12 +150,14 @@ isTransactionBalanced bopts = null . transactionCheckBalanced bopts -- | Verify that any assertions in this transaction hold -- when included in the larger journal. -checkAssertions :: BalancingOpts -> Journal -> Transaction -> Either String Transaction -checkAssertions bopts j t = +transactionCheckAssertions :: BalancingOpts -> Journal -> Transaction -> Either String Transaction +transactionCheckAssertions bopts j t = if (ignore_assertions_ bopts) || noassertions t then Right t else do j' <- journalStyleAmounts j let newtxns = sortOn tdate (jtxns j' ++ [ t ]) - fmap (\_ -> t) $ journalBalanceTransactions defbalancingopts j'{jtxns = newtxns} + case journalBalanceTransactions defbalancingopts j'{jtxns = newtxns} of + Right _ -> Right t + Left e -> Left e where noassertions = all (isNothing . pbalanceassertion) . tpostings @@ -1085,16 +1087,16 @@ tests_Balancing = ] - ,testGroup "checkAssertions" $ [ + ,testGroup "transactionCheckAssertions" $ [ testCase "simple assertion on same day" $ do assertRight $ - checkAssertions defbalancingopts nulljournal{ jtxns = [ + transactionCheckAssertions defbalancingopts nulljournal{ jtxns = [ transaction (fromGregorian 2025 01 01) [ vpost' "a" (usd 1) Nothing ] ] } (transaction (fromGregorian 2025 01 01) [ vpost' "a" (usd 1) (balassert (usd 2)) ]) ,testCase "inclusive assertions" $ do assertRight $ - checkAssertions defbalancingopts nulljournal{ jtxns = [ + transactionCheckAssertions defbalancingopts nulljournal{ jtxns = [ transaction (fromGregorian 2025 01 01) [ vpost' "a:a" (usd 1) Nothing ] ,transaction (fromGregorian 2025 01 02) [ vpost' "a:b" (usd 2) Nothing] ,transaction (fromGregorian 2025 01 02) [ vpost' "a:c" (usd 5) Nothing] @@ -1103,7 +1105,7 @@ tests_Balancing = ,testCase "multicommodity assertion" $ do assertRight $ - checkAssertions defbalancingopts nulljournal{ jtxns = [ + transactionCheckAssertions defbalancingopts nulljournal{ jtxns = [ transaction (fromGregorian 2025 01 01) [ vpost' "a" (usd 1) Nothing ] ,transaction (fromGregorian 2025 01 02) [ vpost' "a:b" (eur 2) Nothing ] ,transaction (fromGregorian 2025 01 02) [ vpost' "a:c" (usd 5) Nothing ] diff --git a/hledger/Hledger/Cli/Commands/Add.hs b/hledger/Hledger/Cli/Commands/Add.hs index 7ef9a6a3d..76c68a901 100644 --- a/hledger/Hledger/Cli/Commands/Add.hs +++ b/hledger/Hledger/Cli/Commands/Add.hs @@ -232,12 +232,9 @@ confirmedTransactionWizard prevInput es@EntryState{..} stack@(currentStage : _) EnterAmountAndComment txnParams account -> amountAndCommentWizard prevInput es >>= \case Just (amt, assertion, (comment, tags, pdate1, pdate2)) -> do - -- This check is necessary because we cons a ';' in the comment parser above, - -- and we don't want to add an empty comment here if it wasn't given. - let pcomment = if T.length comment == 1 then "" else comment let p = nullposting{paccount=T.pack $ stripbrackets account ,pamount=mixedAmount amt - ,pcomment=pcomment + ,pcomment=T.dropAround isNewline comment ,ptype=accountNamePostingType $ T.pack account ,pbalanceassertion = assertion ,pdate=pdate1 @@ -251,7 +248,7 @@ confirmedTransactionWizard prevInput es@EntryState{..} stack@(currentStage : _) dummytxn = nulltransaction{tpostings = esPostings ++ [p, post "" missingamt] ,tdate = txnDate txnParams ,tdescription = txnDesc txnParams } - validated = balanceTransaction defbalancingopts dummytxn >>= checkAssertions defbalancingopts esJournal + validated = balanceTransaction defbalancingopts dummytxn >>= transactionCheckAssertions defbalancingopts esJournal case validated of Left err -> do liftIO (hPutStrLn stderr err) @@ -388,7 +385,8 @@ amountAndCommentWizard previnput@PrevInput{..} entrystate@EntryState{..} = do com <- T.pack <$> fromMaybe "" `fmap` optional (char ';' >> many anySingle) case rtp (postingcommentp Nothing) (T.cons ';' com) of Left err -> fail $ customErrorBundlePretty err - Right comment -> return $ (a, assertion, comment) + -- Keep our original comment string from the user to add to the journal + Right (_, tags, date1', date2') -> return $ (a, assertion, (com, tags, date1', date2')) balancingamt = maNegate . sumPostings $ filter isReal esPostings balancingamtfirstcommodity = mixed . take 1 $ amounts balancingamt showamt = wbUnpack . showMixedAmountB defaultFmt . mixedAmountSetPrecision