support converting amounts to cost, using their saved price.
And do this by default for auto-balancing transactions, as ledger seems to.
This commit is contained in:
parent
54b164a7ab
commit
54ee68976b
@ -71,6 +71,14 @@ amountop :: (Double -> Double -> Double) -> Amount -> Amount -> Amount
|
|||||||
amountop op a@(Amount ac aq ap) b@(Amount bc bq bp) =
|
amountop op a@(Amount ac aq ap) b@(Amount bc bq bp) =
|
||||||
Amount bc ((quantity $ convertAmountTo bc a) `op` bq) Nothing
|
Amount bc ((quantity $ convertAmountTo bc a) `op` bq) Nothing
|
||||||
|
|
||||||
|
-- | Convert an amount to the commodity of its saved price, if any.
|
||||||
|
costOfAmount :: Amount -> Amount
|
||||||
|
costOfAmount a@(Amount _ _ Nothing) = a
|
||||||
|
costOfAmount a@(Amount _ q (Just price))
|
||||||
|
| isZeroMixedAmount price = nullamt
|
||||||
|
| otherwise = Amount pc (pq*q) Nothing
|
||||||
|
where (Amount pc pq _) = head $ amounts price
|
||||||
|
|
||||||
-- | Convert an amount to the specified commodity using the appropriate
|
-- | Convert an amount to the specified commodity using the appropriate
|
||||||
-- exchange rate (which is currently always 1).
|
-- exchange rate (which is currently always 1).
|
||||||
convertAmountTo :: Commodity -> Amount -> Amount
|
convertAmountTo :: Commodity -> Amount -> Amount
|
||||||
@ -137,6 +145,10 @@ normaliseMixedAmount (Mixed as) = Mixed $ map sum $ grouped
|
|||||||
symbols = sort $ nub $ map (symbol . commodity) as
|
symbols = sort $ nub $ map (symbol . commodity) as
|
||||||
hassymbol s a = s == (symbol $ commodity a)
|
hassymbol s a = s == (symbol $ commodity a)
|
||||||
|
|
||||||
|
-- | Convert a mixed amount's component amounts to the commodity of their
|
||||||
|
-- saved price, if any.
|
||||||
|
costOfMixedAmount :: MixedAmount -> MixedAmount
|
||||||
|
costOfMixedAmount (Mixed as) = Mixed $ map costOfAmount as
|
||||||
|
|
||||||
-- | The empty simple amount.
|
-- | The empty simple amount.
|
||||||
nullamt :: Amount
|
nullamt :: Amount
|
||||||
|
|||||||
@ -76,8 +76,9 @@ isEntryBalanced (Entry {etransactions=ts}) =
|
|||||||
-- | Fill in a missing balance in this entry, if we have enough
|
-- | Fill in a missing balance in this entry, if we have enough
|
||||||
-- information to do that. Excluding virtual transactions, there should be
|
-- information to do that. Excluding virtual transactions, there should be
|
||||||
-- at most one missing balance. Otherwise, raise an error.
|
-- at most one missing balance. Otherwise, raise an error.
|
||||||
|
-- The new balance will be converted to cost basis if possible.
|
||||||
balanceEntry :: Entry -> Entry
|
balanceEntry :: Entry -> Entry
|
||||||
balanceEntry e@(Entry{etransactions=ts}) = e{etransactions=ts'}
|
balanceEntry e@Entry{etransactions=ts} = e{etransactions=ts'}
|
||||||
where
|
where
|
||||||
(withamounts, missingamounts) = partition hasAmount $ filter isReal ts
|
(withamounts, missingamounts) = partition hasAmount $ filter isReal ts
|
||||||
ts' = case (length missingamounts) of
|
ts' = case (length missingamounts) of
|
||||||
@ -86,5 +87,5 @@ balanceEntry e@(Entry{etransactions=ts}) = e{etransactions=ts'}
|
|||||||
otherwise -> error $ "could not balance this entry, too many missing amounts:\n" ++ show e
|
otherwise -> error $ "could not balance this entry, too many missing amounts:\n" ++ show e
|
||||||
otherstotal = sum $ map tamount withamounts
|
otherstotal = sum $ map tamount withamounts
|
||||||
balance t
|
balance t
|
||||||
| isReal t && not (hasAmount t) = t{tamount = -otherstotal}
|
| isReal t && not (hasAmount t) = t{tamount = costOfMixedAmount (-otherstotal)}
|
||||||
| otherwise = t
|
| otherwise = t
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user