Skip to content

Commit

Permalink
Merge pull request #385 from codeforIATI/currency_conversion_to_usd_p…
Browse files Browse the repository at this point in the history
…art3

Output converted currency columns (USD + EUR)
  • Loading branch information
radix0000 authored Nov 25, 2022
2 parents afd7b43 + abcc2c1 commit 35509f0
Show file tree
Hide file tree
Showing 5 changed files with 307 additions and 5 deletions.
101 changes: 97 additions & 4 deletions iati_datastore/iatilib/frontend/serialize/csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,20 @@
from flask import request


def total(column):
USD = codelists.by_major_version['2'].Currency.from_string("USD")
EUR = codelists.by_major_version['2'].Currency.from_string("EUR")


def total(column, currency=None):
def accessor(activity):
if len(set(t.value.currency for t in getattr(activity, column))) > 1:
return "!Mixed currency"
return sum(t.value.amount for t in getattr(activity, column) if t.value.amount)
if currency == USD:
return sum(t.value_usd for t in getattr(activity, column) if t.value_usd)
elif currency == EUR:
return sum(t.value_eur for t in getattr(activity, column) if t.value_eur)
else:
if len(set(t.value.currency for t in getattr(activity, column))) > 1:
return "!Mixed currency"
return sum(t.value.amount for t in getattr(activity, column) if t.value.amount)
return accessor


Expand Down Expand Up @@ -90,6 +99,14 @@ def transaction_value(transaction):
return transaction.value.amount


def transaction_value_usd(transaction):
return transaction.value_usd


def transaction_value_eur(transaction):
return transaction.value_eur


def transaction_flow_type(transaction):
return transaction.flow_type.value if transaction.flow_type else ""

Expand Down Expand Up @@ -283,6 +300,14 @@ def budget_value(budget):
return budget.value_amount


def budget_value_usd(budget):
return budget.value_usd


def budget_value_eur(budget):
return budget.value_eur


def budget_type(budget):
try:
return budget.type.name
Expand Down Expand Up @@ -404,6 +429,20 @@ class FieldDict(OrderedDict):
u"total-Interest Repayment": total("interest_repayment"),
u"total-Loan Repayment": total("loan_repayments"),
u"total-Reimbursement": total("reembursements"),
u'total-Commitment-USD': total("commitments", currency=USD),
u"total-Disbursement-USD": total("disbursements", currency=USD),
u"total-Expenditure-USD": total("expenditures", currency=USD),
u"total-Incoming Funds-USD": total("incoming_funds", currency=USD),
u"total-Interest Repayment-USD": total("interest_repayment", currency=USD),
u"total-Loan Repayment-USD": total("loan_repayments", currency=USD),
u"total-Reimbursement-USD": total("reembursements", currency=USD),
u'total-Commitment-EUR': total("commitments", currency=EUR),
u"total-Disbursement-EUR": total("disbursements", currency=EUR),
u"total-Expenditure-EUR": total("expenditures", currency=EUR),
u"total-Incoming Funds-EUR": total("incoming_funds", currency=EUR),
u"total-Interest Repayment-EUR": total("interest_repayment", currency=EUR),
u"total-Loan Repayment-EUR": total("loan_repayments", currency=EUR),
u"total-Reimbursement-EUR": total("reembursements", currency=EUR),
}

def __init__(self, itr, *args, **kw):
Expand Down Expand Up @@ -566,6 +605,20 @@ def __call__(self, data, wrapped=True):
u"total-Interest Repayment",
u"total-Loan Repayment",
u"total-Reimbursement",
u'total-Commitment-USD',
u"total-Disbursement-USD",
u"total-Expenditure-USD",
u"total-Incoming Funds-USD",
u"total-Interest Repayment-USD",
u"total-Loan Repayment-USD",
u"total-Reimbursement-USD",
u'total-Commitment-EUR',
u"total-Disbursement-EUR",
u"total-Expenditure-EUR",
u"total-Incoming Funds-EUR",
u"total-Interest Repayment-EUR",
u"total-Loan Repayment-EUR",
u"total-Reimbursement-EUR",
)

csv = CSVSerializer(_activity_fields)
Expand Down Expand Up @@ -653,6 +706,20 @@ def wrapper(args):
u"total-Interest Repayment",
u"total-Loan Repayment",
u"total-Reimbursement",
u'total-Commitment-USD',
u"total-Disbursement-USD",
u"total-Expenditure-USD",
u"total-Incoming Funds-USD",
u"total-Interest Repayment-USD",
u"total-Loan Repayment-USD",
u"total-Reimbursement-USD",
u'total-Commitment-EUR',
u"total-Disbursement-EUR",
u"total-Expenditure-EUR",
u"total-Incoming Funds-EUR",
u"total-Interest Repayment-EUR",
u"total-Loan Repayment-EUR",
u"total-Reimbursement-EUR",
)

csv_activity_by_country = CSVSerializer(_activity_by_country_fields, adapter=adapt_activity_other)
Expand Down Expand Up @@ -716,6 +783,20 @@ def wrapper(args):
u"total-Interest Repayment",
u"total-Loan Repayment",
u"total-Reimbursement",
u'total-Commitment-USD',
u"total-Disbursement-USD",
u"total-Expenditure-USD",
u"total-Incoming Funds-USD",
u"total-Interest Repayment-USD",
u"total-Loan Repayment-USD",
u"total-Reimbursement-USD",
u'total-Commitment-EUR',
u"total-Disbursement-EUR",
u"total-Expenditure-EUR",
u"total-Incoming Funds-EUR",
u"total-Interest Repayment-EUR",
u"total-Loan Repayment-EUR",
u"total-Reimbursement-EUR",
)

csv_activity_by_sector = CSVSerializer(_activity_by_sector_fields, adapter=adapt_activity_other)
Expand Down Expand Up @@ -757,6 +838,8 @@ def wrapper(args):
(u'transaction-date', transaction_date),
(u"default-currency", default_currency),
(u"transaction-value", transaction_value),
(u"transaction-value-USD", transaction_value_usd),
(u"transaction-value-EUR", transaction_value_eur),
) + common_transaction_csv + (
"iati-identifier",
"hierarchy",
Expand Down Expand Up @@ -835,6 +918,8 @@ def wrapper(args):
(u'transaction-date', trans(transaction_date)),
(u"default-currency", trans(default_currency)),
(u'transaction-value', trans(transaction_value)),
(u"transaction-value-USD", trans(transaction_value_usd)),
(u"transaction-value-EUR", trans(transaction_value_eur)),
) + tuple([(i[0], trans(i[1])) for i in common_transaction_csv]) + (
"iati-identifier",
"hierarchy",
Expand Down Expand Up @@ -897,6 +982,8 @@ def wrapper(args):
(u'transaction-date', trans(transaction_date)),
(u"default-currency", trans(default_currency)),
(u'transaction-value', trans(transaction_value)),
(u"transaction-value-USD", trans(transaction_value_usd)),
(u"transaction-value-EUR", trans(transaction_value_eur)),
) + tuple([(i[0], trans(i[1])) for i in common_transaction_csv]) + (
"iati-identifier",
"hierarchy",
Expand Down Expand Up @@ -951,6 +1038,8 @@ def wrapper(args):
(u'budget-period-start-date', period_start_date),
(u'budget-period-end-date', period_end_date),
(u"budget-value", budget_value),
(u"budget-value-USD", budget_value_usd),
(u"budget-value-EUR", budget_value_eur),
(u"budget-type", budget_type),
u"iati-identifier",
u"title",
Expand All @@ -974,6 +1063,8 @@ def wrapper(args):
(u'budget-period-start-date', trans(period_start_date)),
(u'budget-period-end-date', trans(period_end_date)),
(u"budget-value", trans(budget_value)),
(u"budget-value-USD", trans(budget_value_usd)),
(u"budget-value-EUR", trans(budget_value_eur)),
(u"budget-type", trans(budget_type)),
u"iati-identifier",
u"title",
Expand All @@ -995,6 +1086,8 @@ def wrapper(args):
(u'budget-period-start-date', trans(period_start_date)),
(u'budget-period-end-date', trans(period_end_date)),
(u"budget-value", trans(budget_value)),
(u"budget-value-USD", trans(budget_value_usd)),
(u"budget-value-EUR", trans(budget_value_eur)),
(u"budget-type", trans(budget_type)),
u"iati-identifier",
u"title",
Expand Down
2 changes: 1 addition & 1 deletion iati_datastore/iatilib/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ def convert_currency(xml, conversion, resource=None, major_version='1'):
transaction_date = iso_date
else:
return None
if value_amount is not None and value_amount >= 0:
if value_amount is not None: # and value_amount >= 0:
return conversion(value_amount, transaction_date, input_currency)
else:
return None
Expand Down
34 changes: 34 additions & 0 deletions iati_datastore/iatilib/test/fixtures/budget-currencies.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<iati-activities version="2.03">
<iati-activity default-currency="GBP" hierarchy="1" xml:lang="de">
<iati-identifier>DE-1-198966376</iati-identifier>
<iati-identifier>DE-1-198966376</iati-identifier>
<reporting-org ref="DE-1" secondary-reporter="0" type="10">
<narrative xml:lang="de">Bundesministerium für wirtschaftliche Zusammenarbeit und Entwicklung (BMZ)</narrative>
</reporting-org>
<budget type="2" status="2">
<period-start iso-date="2016-01-01"/>
<period-end iso-date="2016-12-31"/>
<value currency="GBP" value-date="2016-12-23">1916543</value>
</budget>
<budget type="2" status="2">
<period-start iso-date="2017-01-01"/>
<period-end iso-date="2017-12-31"/>
<value currency="GBP" value-date="2017-12-10">3024387</value>
</budget>
<budget type="2" status="2">
<period-start iso-date="2018-01-01"/>
<period-end iso-date="2018-12-31"/>
<value currency="GBP" value-date="2018-12-10">2935782</value>
</budget>
<budget type="2" status="2">
<period-start iso-date="2019-01-01"/>
<period-end iso-date="2019-12-31"/>
<value currency="GBP" value-date="2019-12-10">3343511</value>
</budget>
<budget type="2" status="2">
<period-start iso-date="2020-01-01"/>
<period-end iso-date="2020-12-31"/>
<value currency="GBP" value-date="2020-12-10">3587780</value>
</budget>
</iati-activity>
</iati-activities>
44 changes: 44 additions & 0 deletions iati_datastore/iatilib/test/fixtures/transaction-currencies.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<iati-activities version="2.03">
<iati-activity default-currency="GBP" hierarchy="1" xml:lang="de">
<iati-identifier>DE-1-198966376</iati-identifier>
<iati-identifier>DE-1-198966376</iati-identifier>
<reporting-org ref="DE-1" secondary-reporter="0" type="10">
<narrative xml:lang="de">Bundesministerium für wirtschaftliche Zusammenarbeit und Entwicklung (BMZ)</narrative>
</reporting-org>
<transaction ref="ACET RS12-A">
<transaction-type code="1"/>
<transaction-date iso-date="2017-06-30"/>
<value currency="GBP" value-date="2017-06-30">8797.16</value>
</transaction>
<transaction ref="ACET RS13-A">
<transaction-type code="2"/>
<transaction-date iso-date="2017-06-30"/>
<value currency="GBP" value-date="2017-06-30">9797.16</value>
</transaction>
<transaction ref="ACET RS14-A">
<transaction-type code="3"/>
<transaction-date iso-date="2017-06-30"/>
<value currency="GBP" value-date="2017-06-30">5797.16</value>
</transaction>
<transaction ref="ACET RS15-A">
<transaction-type code="4"/>
<transaction-date iso-date="2017-06-30"/>
<value currency="GBP" value-date="2017-06-30">6797.16</value>
</transaction>
<transaction ref="ACET RS16-A">
<transaction-type code="5"/>
<transaction-date iso-date="2017-06-30"/>
<value currency="GBP" value-date="2017-06-30">16797.16</value>
</transaction>
<transaction ref="ACET RS17-A">
<transaction-type code="6"/>
<transaction-date iso-date="2017-06-30"/>
<value currency="GBP" value-date="2017-06-30">26797.16</value>
</transaction>
<transaction ref="ACET RS17-A">
<transaction-type code="7"/>
<transaction-date iso-date="2017-06-30"/>
<value currency="GBP" value-date="2017-06-30">56797.16</value>
</transaction>
</iati-activity>
</iati-activities>
Loading

0 comments on commit 35509f0

Please sign in to comment.