imp: nicer invalid balance assignment errors

This commit is contained in:
Simon Michael 2022-01-21 17:32:59 -10:00
parent 3135753e94
commit 30080a9aa1

View File

@ -350,7 +350,7 @@ type Balancing s = ReaderT (BalancingState s) (ExceptT String (ST s))
data BalancingState s = BalancingState { data BalancingState s = BalancingState {
-- read only -- read only
bsStyles :: Maybe (M.Map CommoditySymbol AmountStyle) -- ^ commodity display styles bsStyles :: Maybe (M.Map CommoditySymbol AmountStyle) -- ^ commodity display styles
,bsUnassignable :: S.Set AccountName -- ^ accounts in which balance assignments may not be used ,bsUnassignable :: S.Set AccountName -- ^ accounts where balance assignments may not be used (because of auto posting rules)
,bsAssrt :: Bool -- ^ whether to check balance assertions ,bsAssrt :: Bool -- ^ whether to check balance assertions
-- mutable -- mutable
,bsBalances :: H.HashTable s AccountName MixedAmount -- ^ running account balances, initially empty ,bsBalances :: H.HashTable s AccountName MixedAmount -- ^ running account balances, initially empty
@ -422,8 +422,8 @@ journalBalanceTransactions bopts' j' =
-- display precisions used in balanced checking -- display precisions used in balanced checking
styles = Just $ journalCommodityStyles j styles = Just $ journalCommodityStyles j
bopts = bopts'{commodity_styles_=styles} bopts = bopts'{commodity_styles_=styles}
-- balance assignments will not be allowed on these -- balance assignments are not allowed on accounts affected by auto postings
txnmodifieraccts = S.fromList . map (paccount . tmprPosting) . concatMap tmpostingrules $ jtxnmodifiers j autopostingaccts = S.fromList . map (paccount . tmprPosting) . concatMap tmpostingrules $ jtxnmodifiers j
in in
runST $ do runST $ do
-- We'll update a mutable array of transactions as we balance them, -- We'll update a mutable array of transactions as we balance them,
@ -448,7 +448,7 @@ journalBalanceTransactions bopts' j' =
-- 2. Sort these items by date, preserving the order of same-day items, -- 2. Sort these items by date, preserving the order of same-day items,
-- and step through them while keeping running account balances, -- and step through them while keeping running account balances,
runningbals <- lift $ H.newSized (length $ journalAccountNamesUsed j) runningbals <- lift $ H.newSized (length $ journalAccountNamesUsed j)
flip runReaderT (BalancingState styles txnmodifieraccts (not $ ignore_assertions_ bopts) runningbals balancedtxns) $ do flip runReaderT (BalancingState styles autopostingaccts (not $ ignore_assertions_ bopts) runningbals balancedtxns) $ do
-- performing balance assignments in, and balancing, the remaining transactions, -- performing balance assignments in, and balancing, the remaining transactions,
-- and checking balance assertions as each posting is processed. -- and checking balance assertions as each posting is processed.
void $ mapM' balanceTransactionAndCheckAssertionsB $ sortOn (either postingDate tdate) psandts void $ mapM' balanceTransactionAndCheckAssertionsB $ sortOn (either postingDate tdate) psandts
@ -618,30 +618,29 @@ checkIllegalBalanceAssignmentB p = do
checkBalanceAssignmentPostingDateB :: Posting -> Balancing s () checkBalanceAssignmentPostingDateB :: Posting -> Balancing s ()
checkBalanceAssignmentPostingDateB p = checkBalanceAssignmentPostingDateB p =
when (hasBalanceAssignment p && isJust (pdate p)) $ when (hasBalanceAssignment p && isJust (pdate p)) $
throwError . T.unpack $ T.unlines throwError $ chomp $ unlines [
["postings which are balance assignments may not have a custom date." "can't use balance assignment with custom posting date"
,"Please write the posting amount explicitly, or remove the posting date:"
,"" ,""
,maybe (T.unlines $ showPostingLines p) showTransaction $ ptransaction p ,chomp1 $ T.unpack $ maybe (T.unlines $ showPostingLines p) showTransaction $ ptransaction p
,"Balance assignments may not be used on postings with a custom posting date"
,"(it makes balancing the journal impossible)."
,"Please write the posting amount explicitly (or remove the posting date)."
] ]
-- | Throw an error if this posting is trying to do a balance assignment and -- | Throw an error if this posting is trying to do a balance assignment and
-- the account does not allow balance assignments (eg because it is referenced -- the account does not allow balance assignments (eg because it is referenced
-- by a transaction modifier, which might generate additional postings to it). -- by an auto posting rule, which might generate additional postings to it).
checkBalanceAssignmentUnassignableAccountB :: Posting -> Balancing s () checkBalanceAssignmentUnassignableAccountB :: Posting -> Balancing s ()
checkBalanceAssignmentUnassignableAccountB p = do checkBalanceAssignmentUnassignableAccountB p = do
unassignable <- R.asks bsUnassignable unassignable <- R.asks bsUnassignable
when (hasBalanceAssignment p && paccount p `S.member` unassignable) $ when (hasBalanceAssignment p && paccount p `S.member` unassignable) $
throwError . T.unpack $ T.unlines throwError $ chomp $ unlines [
["balance assignments cannot be used with accounts which are" "can't use balance assignment with auto postings"
,"posted to by transaction modifier rules (auto postings)."
,"Please write the posting amount explicitly, or remove the rule."
,"" ,""
,"account: " <> paccount p ,chomp1 $ T.unpack $ maybe (T.unlines $ showPostingLines p) (showTransaction) $ ptransaction p
,"" ,"Balance assignments may not be used on accounts affected by auto posting rules"
,"transaction:" ,"(it makes balancing the journal impossible)."
,"" ,"Please write the posting amount explicitly (or remove the auto posting rule)."
,maybe (T.unlines $ showPostingLines p) showTransaction $ ptransaction p
] ]
-- lenses -- lenses