-
-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathgenerate_base_ccy_prices.py
More file actions
43 lines (32 loc) · 1.28 KB
/
generate_base_ccy_prices.py
File metadata and controls
43 lines (32 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
"""A plugin that inserts an additional price to the base rate by applying
fx rate to a price.
"""
from beancount.core import amount, data, prices
__plugins__ = ["generate"]
def generate(entries, options_map, baseCcy):
errors = []
priceMap = prices.build_price_map(entries)
additionalEntries = []
for entry in entries:
if (
isinstance(entry, data.Price)
and entry.amount.currency != baseCcy
and entry.currency != baseCcy
):
fxTuple = tuple([entry.amount.currency, baseCcy])
fxRate = prices.get_price(priceMap, fxTuple, entry.date)
if fxRate[1] and not _alreadyExistingPrice(
priceMap, tuple([entry.currency, baseCcy]), entry.date
):
priceInBaseCcy = amount.Amount(entry.amount.number * fxRate[1], baseCcy)
additionalEntries.append(
data.Price(entry.meta, entry.date, entry.currency, priceInBaseCcy)
)
entries.extend(additionalEntries)
return entries, errors
def _alreadyExistingPrice(priceMap, fxTuple, date):
if fxTuple in priceMap:
for alreadyExistingPriceDates in priceMap[fxTuple]:
if date == alreadyExistingPriceDates[0]:
return True
return False