From 0afa02e8810ca10349d5dafe081d5a8dfd4c15de Mon Sep 17 00:00:00 2001 From: ruuushhh <66899387+ruuushhh@users.noreply.github.com> Date: Wed, 27 Nov 2024 12:17:07 +0530 Subject: [PATCH] feat: add exchage rate support for JE (#700) * feat: add exchage rate support for JE * fix tests --------- Co-authored-by: GitHub Actions --- .../0017_journalentry_exchange_rate.py | 18 ++++++++++++++++++ apps/quickbooks_online/models.py | 3 ++- apps/quickbooks_online/utils.py | 9 +++++++++ .../reset_db_fixtures/reset_db.sql | 12 +++++++----- 4 files changed, 36 insertions(+), 6 deletions(-) create mode 100644 apps/quickbooks_online/migrations/0017_journalentry_exchange_rate.py diff --git a/apps/quickbooks_online/migrations/0017_journalentry_exchange_rate.py b/apps/quickbooks_online/migrations/0017_journalentry_exchange_rate.py new file mode 100644 index 00000000..9d3bc2eb --- /dev/null +++ b/apps/quickbooks_online/migrations/0017_journalentry_exchange_rate.py @@ -0,0 +1,18 @@ +# Generated by Django 3.2.14 on 2024-11-27 05:19 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('quickbooks_online', '0016_bill_is_retired'), + ] + + operations = [ + migrations.AddField( + model_name='journalentry', + name='exchange_rate', + field=models.FloatField(help_text='Exchange rate', null=True), + ), + ] diff --git a/apps/quickbooks_online/models.py b/apps/quickbooks_online/models.py index 18a94dd4..66178ed1 100644 --- a/apps/quickbooks_online/models.py +++ b/apps/quickbooks_online/models.py @@ -6,11 +6,11 @@ from django.conf import settings from django.db import models -from fyle_accounting_mappings.models import DestinationAttribute, EmployeeMapping, ExpenseAttribute, Mapping, MappingSetting from apps.fyle.models import Expense, ExpenseGroup, ExpenseGroupSettings from apps.mappings.models import GeneralMapping from apps.workspaces.models import Workspace, WorkspaceGeneralSettings +from fyle_accounting_mappings.models import DestinationAttribute, EmployeeMapping, ExpenseAttribute, Mapping, MappingSetting def get_transaction_date(expense_group: ExpenseGroup) -> str: @@ -736,6 +736,7 @@ class JournalEntry(models.Model): transaction_date = models.DateField(help_text='JournalEntry transaction date') currency = models.CharField(max_length=255, help_text='JournalEntry Currency') private_note = models.TextField(help_text='JournalEntry Description') + exchange_rate = models.FloatField(help_text='Exchange rate', null=True) created_at = models.DateTimeField(auto_now_add=True, help_text='Created at') updated_at = models.DateTimeField(auto_now=True, help_text='Updated at') diff --git a/apps/quickbooks_online/utils.py b/apps/quickbooks_online/utils.py index a23c00ef..cf5874d0 100644 --- a/apps/quickbooks_online/utils.py +++ b/apps/quickbooks_online/utils.py @@ -1224,6 +1224,8 @@ def __construct_journal_entry(self, journal_entry: JournalEntry, journal_entry_l """ general_mappings = GeneralMapping.objects.filter(workspace_id=self.workspace_id).first() general_settings = WorkspaceGeneralSettings.objects.filter(workspace_id=self.workspace_id).first() + qbo_home_currency = QBOCredential.objects.get(workspace_id=self.workspace_id).currency + fyle_home_currency = journal_entry.currency tax_rate_refs = [] @@ -1235,6 +1237,13 @@ def __construct_journal_entry(self, journal_entry: JournalEntry, journal_entry_l journal_entry_payload = {'TxnDate': journal_entry.transaction_date, 'PrivateNote': journal_entry.private_note, 'Line': lines, 'CurrencyRef': {"value": journal_entry.currency}, 'TxnTaxDetail': {'TaxLine': []}} + if general_settings.is_multi_currency_allowed and fyle_home_currency != qbo_home_currency and qbo_home_currency: + exchange_rate = self.connection.exchange_rates.get_by_source(source_currency_code=fyle_home_currency) + journal_entry_payload['ExchangeRate'] = exchange_rate['Rate'] if "Rate" in exchange_rate else 1 + + journal_entry.exchange_rate = journal_entry_payload['ExchangeRate'] + journal_entry.save(update_fields=['exchange_rate']) + if general_settings.import_tax_codes: journal_entry_payload.update({'GlobalTaxCalculation': 'TaxInclusive'}) diff --git a/tests/sql_fixtures/reset_db_fixtures/reset_db.sql b/tests/sql_fixtures/reset_db_fixtures/reset_db.sql index af4b930c..bb1e9f56 100644 --- a/tests/sql_fixtures/reset_db_fixtures/reset_db.sql +++ b/tests/sql_fixtures/reset_db_fixtures/reset_db.sql @@ -2,7 +2,7 @@ -- PostgreSQL database dump -- --- Dumped from database version 15.9 (Debian 15.9-1.pgdg120+1) +-- Dumped from database version 15.7 (Debian 15.7-1.pgdg120+1) -- Dumped by pg_dump version 15.8 (Debian 15.8-1.pgdg120+1) SET statement_timeout = 0; @@ -1337,7 +1337,8 @@ CREATE TABLE public.journal_entries ( private_note text NOT NULL, created_at timestamp with time zone NOT NULL, updated_at timestamp with time zone NOT NULL, - expense_group_id integer NOT NULL + expense_group_id integer NOT NULL, + exchange_rate double precision ); @@ -4068,9 +4069,11 @@ COPY public.django_migrations (id, app, name, applied) FROM stdin; 187 workspaces 0046_workspacegeneralsettings_import_code_fields 2024-08-02 07:52:56.494868+00 188 fyle_accounting_mappings 0026_destinationattribute_code 2024-08-02 08:35:52.537882+00 189 quickbooks_online 0015_add_bill_number 2024-08-29 14:29:50.588003+00 -190 quickbooks_online 0015_bill_is_retired 2024-09-03 15:08:15.085332+00 +190 quickbooks_online 0016_bill_is_retired 2024-09-03 15:08:15.085332+00 191 fyle 0038_expensegroup_export_url 2024-08-03 14:24:57.600169+00 192 fyle 0039_expense_is_posted_at_null 2024-11-14 11:09:52.364321+00 +193 fyle 0040_expense_masked_corporate_card_number 2024-11-19 11:22:38.855388+00 +194 quickbooks_online 0017_journalentry_exchange_rate 2024-11-27 15:08:15.085332+00 \. @@ -33484,7 +33487,7 @@ COPY public.import_logs (id, attribute_type, status, error_log, total_batches_co -- Data for Name: journal_entries; Type: TABLE DATA; Schema: public; Owner: postgres -- -COPY public.journal_entries (id, transaction_date, currency, private_note, created_at, updated_at, expense_group_id) FROM stdin; +COPY public.journal_entries (id, transaction_date, currency, private_note, created_at, updated_at, expense_group_id, exchange_rate) FROM stdin; \. @@ -35904,4 +35907,3 @@ ALTER TABLE ONLY public.workspaces_user -- -- PostgreSQL database dump complete -- -