-
Notifications
You must be signed in to change notification settings - Fork 1
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
hemant10yadav
wants to merge
7
commits into
main
Choose a base branch
from
hy/currency-conversion-table
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+100
−10
Open
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f846f5a
added the exchange rate table
hemant10yadav c70fa93
removed cache
hemant10yadav 1ff0ff0
added logs
hemant10yadav abed581
refactor code
hemant10yadav a0eab5e
Merge remote-tracking branch 'refs/remotes/origin/main' into hy/curre…
hemant10yadav 817a959
Merge branch 'main' into hy/currency-conversion-table
hemant10yadav 63bc638
fixed migration sequence and added code review fixes
hemant10yadav File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
commcare_connect/opportunity/migrations/0061_exchangerate.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
.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), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the use of this date? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed - 63bc638)