split more long transaction balancing errors into lines; refactor
This commit is contained in:
parent
924ec1c98d
commit
0a5fa33f9d
@ -340,8 +340,8 @@ transactionPostingBalances t = (sumPostings $ realPostings t
|
|||||||
,sumPostings $ virtualPostings t
|
,sumPostings $ virtualPostings t
|
||||||
,sumPostings $ balancedVirtualPostings t)
|
,sumPostings $ balancedVirtualPostings t)
|
||||||
|
|
||||||
-- | Check that this transaction would appear balanced to a human when displayed,
|
-- | Check that this transaction would appear balanced to a human when displayed.
|
||||||
-- and return an appropriate error message otherwise.
|
-- On success, returns the empty list, otherwise one or more error messages.
|
||||||
--
|
--
|
||||||
-- In more detail:
|
-- In more detail:
|
||||||
-- For the real postings, and separately for the balanced virtual postings:
|
-- For the real postings, and separately for the balanced virtual postings:
|
||||||
@ -357,10 +357,8 @@ transactionPostingBalances t = (sumPostings $ realPostings t
|
|||||||
-- 3. Does the amounts' sum appear non-zero when displayed ?
|
-- 3. Does the amounts' sum appear non-zero when displayed ?
|
||||||
-- (using the given display styles if provided)
|
-- (using the given display styles if provided)
|
||||||
--
|
--
|
||||||
transactionCheckBalanced :: Maybe (M.Map CommoditySymbol AmountStyle) -> Transaction -> Maybe String
|
transactionCheckBalanced :: Maybe (M.Map CommoditySymbol AmountStyle) -> Transaction -> [String]
|
||||||
transactionCheckBalanced mstyles t
|
transactionCheckBalanced mstyles t = errs
|
||||||
| rsignsok && bvsignsok && rzerosum && bvzerosum = Nothing
|
|
||||||
| otherwise = Just msg
|
|
||||||
where
|
where
|
||||||
(rps, bvps) = (realPostings t, balancedVirtualPostings t)
|
(rps, bvps) = (realPostings t, balancedVirtualPostings t)
|
||||||
canonicalise = maybe id canonicaliseMixedAmount mstyles
|
canonicalise = maybe id canonicaliseMixedAmount mstyles
|
||||||
@ -388,11 +386,11 @@ transactionCheckBalanced mstyles t
|
|||||||
| not bvsignsok = "balanced virtual postings all have the same sign"
|
| not bvsignsok = "balanced virtual postings all have the same sign"
|
||||||
| not bvzerosum = "balanced virtual postings' sum should be 0 but is: " ++ showMixedAmount bvsumcost
|
| not bvzerosum = "balanced virtual postings' sum should be 0 but is: " ++ showMixedAmount bvsumcost
|
||||||
| otherwise = ""
|
| otherwise = ""
|
||||||
msg = intercalate "\n" $ ["could not balance this transaction:"] ++ filter (not.null) [rmsg, bvmsg]
|
errs = filter (not.null) [rmsg, bvmsg]
|
||||||
|
|
||||||
-- | Legacy form of transactionCheckBalanced.
|
-- | Legacy form of transactionCheckBalanced.
|
||||||
isTransactionBalanced :: Maybe (M.Map CommoditySymbol AmountStyle) -> Transaction -> Bool
|
isTransactionBalanced :: Maybe (M.Map CommoditySymbol AmountStyle) -> Transaction -> Bool
|
||||||
isTransactionBalanced mstyles = (==Nothing) . transactionCheckBalanced mstyles
|
isTransactionBalanced mstyles = null . transactionCheckBalanced mstyles
|
||||||
|
|
||||||
-- | Balance this transaction, ensuring that its postings
|
-- | Balance this transaction, ensuring that its postings
|
||||||
-- (and its balanced virtual postings) sum to 0,
|
-- (and its balanced virtual postings) sum to 0,
|
||||||
@ -424,11 +422,19 @@ balanceTransactionHelper mstyles t = do
|
|||||||
(t', inferredamtsandaccts) <-
|
(t', inferredamtsandaccts) <-
|
||||||
inferBalancingAmount (fromMaybe M.empty mstyles) $ inferBalancingPrices t
|
inferBalancingAmount (fromMaybe M.empty mstyles) $ inferBalancingPrices t
|
||||||
case transactionCheckBalanced mstyles t' of
|
case transactionCheckBalanced mstyles t' of
|
||||||
Nothing -> Right (txnTieKnot t', inferredamtsandaccts)
|
[] -> Right (txnTieKnot t', inferredamtsandaccts)
|
||||||
Just err -> Left $ annotateErrorWithTransaction t' err
|
errs -> Left $ transactionBalanceError t' errs
|
||||||
|
|
||||||
|
-- | Generate a transaction balancing error message, given the transaction
|
||||||
|
-- and one or more suberror messages.
|
||||||
|
transactionBalanceError :: Transaction -> [String] -> String
|
||||||
|
transactionBalanceError t errs =
|
||||||
|
annotateErrorWithTransaction t $
|
||||||
|
intercalate "\n" $ "could not balance this transaction:" : errs
|
||||||
|
|
||||||
annotateErrorWithTransaction :: Transaction -> String -> String
|
annotateErrorWithTransaction :: Transaction -> String -> String
|
||||||
annotateErrorWithTransaction t s = unlines [showGenericSourcePos $ tsourcepos t, s, showTransaction t]
|
annotateErrorWithTransaction t s =
|
||||||
|
unlines [showGenericSourcePos $ tsourcepos t, s, rstrip $ showTransaction t]
|
||||||
|
|
||||||
-- | Infer up to one missing amount for this transactions's real postings, and
|
-- | Infer up to one missing amount for this transactions's real postings, and
|
||||||
-- likewise for its balanced virtual postings, if needed; or return an error
|
-- likewise for its balanced virtual postings, if needed; or return an error
|
||||||
@ -444,9 +450,13 @@ inferBalancingAmount ::
|
|||||||
-> Either String (Transaction, [(AccountName, MixedAmount)])
|
-> Either String (Transaction, [(AccountName, MixedAmount)])
|
||||||
inferBalancingAmount styles t@Transaction{tpostings=ps}
|
inferBalancingAmount styles t@Transaction{tpostings=ps}
|
||||||
| length amountlessrealps > 1
|
| length amountlessrealps > 1
|
||||||
= Left $ annotateErrorWithTransaction t "could not balance this transaction - can't have more than one real posting with no amount (remember to put 2 or more spaces before amounts)"
|
= Left $ transactionBalanceError t
|
||||||
|
["can't have more than one real posting with no amount"
|
||||||
|
,"(remember to put two or more spaces between account and amount)"]
|
||||||
| length amountlessbvps > 1
|
| length amountlessbvps > 1
|
||||||
= Left $ annotateErrorWithTransaction t "could not balance this transaction - can't have more than one balanced virtual posting with no amount (remember to put 2 or more spaces before amounts)"
|
= Left $ transactionBalanceError t
|
||||||
|
["can't have more than one balanced virtual posting with no amount"
|
||||||
|
,"(remember to put two or more spaces between account and amount)"]
|
||||||
| otherwise
|
| otherwise
|
||||||
= let psandinferredamts = map inferamount ps
|
= let psandinferredamts = map inferamount ps
|
||||||
inferredacctsandamts = [(paccount p, amt) | (p, Just amt) <- psandinferredamts]
|
inferredacctsandamts = [(paccount p, amt) | (p, Just amt) <- psandinferredamts]
|
||||||
|
|||||||
@ -56,7 +56,7 @@ $ hledger -f - print -x
|
|||||||
c
|
c
|
||||||
|
|
||||||
$ hledger -f journal:- print
|
$ hledger -f journal:- print
|
||||||
>2 /could not balance this transaction - can't have more than one real posting with no amount/
|
>2 /can't have more than one real posting with no amount/
|
||||||
>=1
|
>=1
|
||||||
|
|
||||||
# 6. Two (or more) virtual postings with implicit amount cannot be balanced.
|
# 6. Two (or more) virtual postings with implicit amount cannot be balanced.
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user