Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Currency conversion table #426

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions commcare_connect/opportunity/migrations/0061_exchangerate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Generated by Django 4.2.5 on 2024-11-06 05:16
import logging

from django.db import migrations, models
from django.db.models.functions import TruncDate

from commcare_connect.opportunity.visit_import import get_exchange_rate

logger = logging.getLogger(__name__)


def update_exchange_rate(apps, schema_editor):
Payment = apps.get_model("opportunity.Payment")
payments = (
Payment.objects.annotate(date_only=TruncDate("date_paid"))
.values("id", "date_only", "amount", "opportunity_access__opportunity__currency")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Payments without opportunity access (to program managers) will need to be accounted for as well, but wont have an opportunity access

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed - 63bc638)

.distinct()
)

for payment in payments:
date_paid = payment["date_only"]
currency = payment["opportunity_access__opportunity__currency"]

if currency in ["USD", None]:
hemant10yadav marked this conversation as resolved.
Show resolved Hide resolved
exchange_rate = 1
else:
exchange_rate = get_exchange_rate(currency, date_paid)
logger.info(
f"Payment ID: {payment.id}, original USD: {payment.amount_usd}, USD acc. to new rate: {payment.amount / exchange_rate}"
)
if not exchange_rate:
raise Exception(f"Invalid currency code {currency}")


class Migration(migrations.Migration):
dependencies = [
("opportunity", "0060_completedwork_payment_date"),
]

operations = [
migrations.CreateModel(
name="ExchangeRate",
fields=[
("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
("currency_code", models.CharField(max_length=3)),
("rate", models.DecimalField(decimal_places=6, max_digits=10)),
("rate_date", models.DateField()),
("fetched_at", models.DateTimeField(auto_now_add=True)),
],
),
migrations.RunPython(update_exchange_rate, migrations.RunPython.noop),
]
7 changes: 7 additions & 0 deletions commcare_connect/opportunity/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -685,3 +685,10 @@ class CatchmentArea(models.Model):

class Meta:
unique_together = ("site_code", "opportunity")


class ExchangeRate(models.Model):
hemant10yadav marked this conversation as resolved.
Show resolved Hide resolved
currency_code = models.CharField(max_length=3)
rate = models.DecimalField(max_digits=10, decimal_places=6)
rate_date = models.DateField()
fetched_at = models.DateTimeField(auto_now_add=True)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the use of this date?

35 changes: 28 additions & 7 deletions commcare_connect/opportunity/visit_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
from django.utils.timezone import now
from tablib import Dataset

from commcare_connect.cache import quickcache
from commcare_connect.opportunity.models import (
CatchmentArea,
CompletedWork,
CompletedWorkStatus,
ExchangeRate,
Opportunity,
OpportunityAccess,
Payment,
Expand Down Expand Up @@ -277,11 +277,6 @@ def _bulk_update_payments(opportunity: Opportunity, imported_data: Dataset) -> P
return PaymentImportStatus(seen_users, missing_users)


def _cache_key(currency_code, date=None):
return [currency_code, date.toordinal() if date else None]


@quickcache(vary_on=_cache_key, timeout=12 * 60 * 60)
hemant10yadav marked this conversation as resolved.
Show resolved Hide resolved
def get_exchange_rate(currency_code, date=None):
# date should be a date object or None for latest rate

Expand All @@ -290,14 +285,40 @@ def get_exchange_rate(currency_code, date=None):
if currency_code == "USD":
return 1
hemant10yadav marked this conversation as resolved.
Show resolved Hide resolved

rate = get_exchange_rate_from_db(currency_code, date)
hemant10yadav marked this conversation as resolved.
Show resolved Hide resolved
if rate:
return rate

base_url = "https://openexchangerates.org/api"

if date:
url = f"{base_url}/historical/{date.strftime('%Y-%m-%d')}.json"
else:
url = f"{base_url}/latest.json"
url = f"{url}?app_id={settings.OPEN_EXCHANGE_RATES_API_ID}"
rates = json.load(urllib.request.urlopen(url))
return rates["rates"].get(currency_code)

rate = rates["rates"].get(currency_code)

if rate:
rate_date = date if date else now().date()
sravfeyn marked this conversation as resolved.
Show resolved Hide resolved
ExchangeRate.objects.create(currency_code=currency_code, rate=rate, rate_date=rate_date)

return rate


def get_exchange_rate_from_db(currency_code, date=None):
hemant10yadav marked this conversation as resolved.
Show resolved Hide resolved
if currency_code == "USD":
return 1

if not date:
date = now().date()
hemant10yadav marked this conversation as resolved.
Show resolved Hide resolved

rate = (
ExchangeRate.objects.filter(currency_code=currency_code, rate_date=date).values_list("rate", flat=True).first()
)
hemant10yadav marked this conversation as resolved.
Show resolved Hide resolved

return rate


def bulk_update_completed_work_status(opportunity: Opportunity, file: UploadedFile) -> CompletedWorkImportStatus:
Expand Down
Loading