and allow parsing of mixed-commodity entries, as long as we don't have to balance them
This commit is contained in:
		
							parent
							
								
									17ceb9039b
								
							
						
					
					
						commit
						0cfbced165
					
				| @ -21,11 +21,10 @@ showDate d = printf "%-10s" d | ||||
| showDescription s = printf "%-20s" (elideRight 20 s) | ||||
| 
 | ||||
| isEntryBalanced :: Entry -> Bool | ||||
| isEntryBalanced (Entry {etransactions=ts}) = isZeroAmount sum && numcommodities==1 | ||||
| isEntryBalanced (Entry {etransactions=ts}) = isZeroMixedAmount sum | ||||
|     where | ||||
|       realts = filter isReal ts | ||||
|       sum = sumRawTransactions realts | ||||
|       numcommodities = length $ nub $ map (symbol . commodity . tamount) realts | ||||
|       realts = filter isReal ts | ||||
| 
 | ||||
| -- | Fill in a missing balance in this entry, if there is one,  | ||||
| -- or raise an error if there is more than one. | ||||
|  | ||||
| @ -40,7 +40,7 @@ cacheLedger l = | ||||
|                (Map.fromList [(account $ head g, g) | g <- groupedts]) | ||||
|                (Map.fromList [(a,[]) | a <- anames]) | ||||
|         txnsof = (txnmap !) | ||||
|         subacctsof a = filter (isAccountNamePrefixOf a) anames | ||||
|         subacctsof a = filter (a `isAccountNamePrefixOf`) anames | ||||
|         subtxnsof a = concat [txnsof a | a <- [a] ++ subacctsof a] | ||||
|         balmap = Map.union  | ||||
|                (Map.fromList [(a, (sumTransactions $ subtxnsof a)) | a <- anames]) | ||||
|  | ||||
| @ -26,16 +26,20 @@ showRawTransaction t = (showaccountname $ taccount t) ++ " " ++ (showamount $ ta | ||||
| -- otherwise return Nothing. | ||||
| autofillTransactions :: [RawTransaction] -> Maybe [RawTransaction] | ||||
| autofillTransactions ts = | ||||
|     case (length missingamounts) of | ||||
|     case (length withmissingamounts) of | ||||
|       0 -> Just ts | ||||
|       1 -> Just $ map balance ts | ||||
|       otherwise -> Nothing | ||||
|     where  | ||||
|       (reals, _) = partition isReal ts | ||||
|       (realamounts, missingamounts) = partition hasAmount reals | ||||
|       (withrealamounts, withmissingamounts) = partition hasAmount reals | ||||
|       balance t = if (isReal t) && (not $ hasAmount t)  | ||||
|                   then t{tamount = -(sumRawTransactions realamounts)} | ||||
|                   then t{tamount = -otherssimpletotal} | ||||
|                   else t | ||||
|       otherstotal = sumRawTransactions withrealamounts | ||||
|       otherssimpletotal | ||||
|           | length otherstotal == 1 = head otherstotal | ||||
|           | otherwise = error "sorry, can't balance a mixed-commodity entry yet" | ||||
| 
 | ||||
| isReal :: RawTransaction -> Bool | ||||
| isReal t = rttype t == RegularTransaction | ||||
| @ -43,5 +47,5 @@ isReal t = rttype t == RegularTransaction | ||||
| hasAmount :: RawTransaction -> Bool | ||||
| hasAmount = ("AUTO" /=) . symbol . commodity . tamount | ||||
| 
 | ||||
| sumRawTransactions :: [RawTransaction] -> Amount | ||||
| sumRawTransactions = sumAmounts . map tamount | ||||
| sumRawTransactions :: [RawTransaction] -> MixedAmount | ||||
| sumRawTransactions = normaliseMixedAmount . map tamount | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user