more balanceTransaction cleanup
Clarify: it's fine to try to infer prices on a transaction that has had an amount inferred, it just won't have any effect.
This commit is contained in:
		
							parent
							
								
									b735107f43
								
							
						
					
					
						commit
						b827f1a146
					
				@ -267,14 +267,12 @@ isTransactionBalanced styles t =
 | 
				
			|||||||
balanceTransaction :: Maybe (Map.Map Commodity AmountStyle) -> Transaction -> Either String Transaction
 | 
					balanceTransaction :: Maybe (Map.Map Commodity AmountStyle) -> Transaction -> Either String Transaction
 | 
				
			||||||
balanceTransaction styles t =
 | 
					balanceTransaction styles t =
 | 
				
			||||||
  case inferBalancingAmount t of
 | 
					  case inferBalancingAmount t of
 | 
				
			||||||
    Left err           -> Left err
 | 
					    Left err -> Left err
 | 
				
			||||||
    Right tWithAmounts ->
 | 
					    Right t' -> let t'' = inferBalancingPrices t'
 | 
				
			||||||
     case isTransactionBalanced styles tWithAmountsAndPrices of
 | 
					                in if isTransactionBalanced styles t''
 | 
				
			||||||
      False -> Left $ printerr $ nonzerobalanceerror tWithAmountsAndPrices
 | 
					                   then Right $ txnTieKnot t''
 | 
				
			||||||
      True  -> Right $ txnTieKnot tWithAmountsAndPrices
 | 
					                   else Left $ printerr $ nonzerobalanceerror t''
 | 
				
			||||||
 | 
					 | 
				
			||||||
     where
 | 
					     where
 | 
				
			||||||
      tWithAmountsAndPrices = (if tWithAmounts==t then inferBalancingPrices else id) tWithAmounts -- XXX unneeded ?
 | 
					 | 
				
			||||||
      printerr s = intercalate "\n" [s, showTransactionUnelided t]
 | 
					      printerr s = intercalate "\n" [s, showTransactionUnelided t]
 | 
				
			||||||
      nonzerobalanceerror :: Transaction -> String
 | 
					      nonzerobalanceerror :: Transaction -> String
 | 
				
			||||||
      nonzerobalanceerror t = printf "could not balance this transaction (%s%s%s)" rmsg sep bvmsg
 | 
					      nonzerobalanceerror t = printf "could not balance this transaction (%s%s%s)" rmsg sep bvmsg
 | 
				
			||||||
@ -321,10 +319,13 @@ inferBalancingAmount t@Transaction{tpostings=ps}
 | 
				
			|||||||
-- of the second posting (with -B), and such that the postings balance.
 | 
					-- of the second posting (with -B), and such that the postings balance.
 | 
				
			||||||
-- 
 | 
					-- 
 | 
				
			||||||
-- In general, we can infer a conversion price when the sum of posting amounts
 | 
					-- In general, we can infer a conversion price when the sum of posting amounts
 | 
				
			||||||
-- contains exactly two different commodities and no explicit prices.  The
 | 
					-- contains exactly two different commodities and no explicit prices.  Also
 | 
				
			||||||
-- transaction could contain additional commodities, and/or prices, if they
 | 
					-- all postings are expected to contain an explicit amount (no missing
 | 
				
			||||||
-- cancel out; what matters is that the sum of posting amounts contains
 | 
					-- amounts) in a single commodity. Otherwise no price inferring is attempted.
 | 
				
			||||||
-- exactly two commodities and zero prices.
 | 
					-- 
 | 
				
			||||||
 | 
					-- The transaction itself could contain more than two commodities, and/or
 | 
				
			||||||
 | 
					-- prices, if they cancel out; what matters is that the sum of posting amounts
 | 
				
			||||||
 | 
					-- contains exactly two commodities and zero prices.
 | 
				
			||||||
-- 
 | 
					-- 
 | 
				
			||||||
-- There can also be more than two postings in either of the commodities.
 | 
					-- There can also be more than two postings in either of the commodities.
 | 
				
			||||||
-- 
 | 
					-- 
 | 
				
			||||||
@ -345,11 +346,6 @@ inferBalancingAmount t@Transaction{tpostings=ps}
 | 
				
			|||||||
-- use any decimal places. The minimum of 2 helps make the prices shown by the
 | 
					-- use any decimal places. The minimum of 2 helps make the prices shown by the
 | 
				
			||||||
-- print command a bit less surprising in this case. Could do better.)
 | 
					-- print command a bit less surprising in this case. Could do better.)
 | 
				
			||||||
-- 
 | 
					-- 
 | 
				
			||||||
-- All postings are expected to contain an explicit amount (no missing
 | 
					 | 
				
			||||||
-- amounts) in a single commodity.  (The code used to avoid inferring prices
 | 
					 | 
				
			||||||
-- when it had previously inferred a missing amount, but it seems harmless to
 | 
					 | 
				
			||||||
-- do that.)
 | 
					 | 
				
			||||||
-- 
 | 
					 | 
				
			||||||
inferBalancingPrices :: Transaction -> Transaction
 | 
					inferBalancingPrices :: Transaction -> Transaction
 | 
				
			||||||
inferBalancingPrices t@Transaction{tpostings=ps} = t{tpostings=ps'}
 | 
					inferBalancingPrices t@Transaction{tpostings=ps} = t{tpostings=ps'}
 | 
				
			||||||
  where
 | 
					  where
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user