From 5ee22e0ebeadf5e22bb86e11289f8c09b5379945 Mon Sep 17 00:00:00 2001 From: anishfyle Date: Mon, 16 Dec 2024 16:13:20 +0530 Subject: [PATCH 1/7] fix: exchange rate for ccp --- apps/quickbooks_online/utils.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/apps/quickbooks_online/utils.py b/apps/quickbooks_online/utils.py index 618449a5..2c60fe6e 100644 --- a/apps/quickbooks_online/utils.py +++ b/apps/quickbooks_online/utils.py @@ -1062,6 +1062,8 @@ def __construct_credit_card_purchase(self, credit_card_purchase: CreditCardPurch :return: constructed credit_card_purchase """ general_mappings = GeneralMapping.objects.filter(workspace_id=self.workspace_id).first() + qbo_home_currency = QBOCredential.objects.get(workspace_id=self.workspace_id).currency + fyle_home_currency = credit_card_purchase.currency line = self.__construct_credit_card_purchase_lineitems(credit_card_purchase_lineitems, general_mappings) credit = False From c8e8a1c05bcc074f5b54a59be0e28e413d729d83 Mon Sep 17 00:00:00 2001 From: anishfyle Date: Mon, 16 Dec 2024 16:21:26 +0530 Subject: [PATCH 2/7] fix --- apps/quickbooks_online/utils.py | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/apps/quickbooks_online/utils.py b/apps/quickbooks_online/utils.py index 2c60fe6e..ff111a7c 100644 --- a/apps/quickbooks_online/utils.py +++ b/apps/quickbooks_online/utils.py @@ -712,7 +712,6 @@ def get_override_tax_details(self, lines, is_journal_entry_export: bool = False) def purchase_object_payload(self, purchase_object, line, payment_type, account_ref, doc_number: str = None, credit=None): general_settings = WorkspaceGeneralSettings.objects.filter(workspace_id=self.workspace_id).first() - qbo_credentials = QBOCredential.objects.get(workspace_id=self.workspace_id) purchase_object_payload = { 'DocNumber': doc_number if doc_number else None, @@ -727,22 +726,10 @@ def purchase_object_payload(self, purchase_object, line, payment_type, account_r 'Line': line, } - # Add exchange rate for foreign currency transactions - if general_settings.is_multi_currency_allowed and purchase_object.currency != qbo_credentials.currency and qbo_credentials.currency: - exchange_rate = self.connection.exchange_rates.get_by_source(source_currency_code=purchase_object.currency) - purchase_object_payload['ExchangeRate'] = exchange_rate['Rate'] if "Rate" in exchange_rate else 1 - - if isinstance(purchase_object, CreditCardPurchase): - purchase_object.exchange_rate = purchase_object_payload['ExchangeRate'] - purchase_object.save(update_fields=['exchange_rate']) - if general_settings.import_tax_codes: if general_settings.is_tax_override_enabled: tax_details = self.get_override_tax_details(line) - purchase_object_payload.update({ - 'GlobalTaxCalculation': 'TaxExcluded', - 'TxnTaxDetail': {"TaxLine": tax_details} - }) + purchase_object_payload.update({'GlobalTaxCalculation': 'TaxExcluded', 'TxnTaxDetail': {"TaxLine": tax_details}}) else: purchase_object_payload.update({'GlobalTaxCalculation': 'TaxInclusive'}) @@ -1062,6 +1049,7 @@ def __construct_credit_card_purchase(self, credit_card_purchase: CreditCardPurch :return: constructed credit_card_purchase """ 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 = credit_card_purchase.currency @@ -1077,6 +1065,13 @@ def __construct_credit_card_purchase(self, credit_card_purchase: CreditCardPurch credit_card_purchase_payload = self.purchase_object_payload(credit_card_purchase, line, account_ref=credit_card_purchase.ccc_account_id, payment_type='CreditCard', doc_number=credit_card_purchase.credit_card_purchase_number, credit=credit) + 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) + credit_card_purchase_payload['ExchangeRate'] = exchange_rate['Rate'] if "Rate" in exchange_rate else 1 + + credit_card_purchase.exchange_rate = credit_card_purchase_payload['ExchangeRate'] + credit_card_purchase.save(update_fields=['exchange_rate']) + logger.info("| Payload for Credit Card Purchase creation | Content: {{WORKSPACE_ID: {} EXPENSE_GROUP_ID: {} CREDIT_CARD_PURCHASE_PAYLOAD: {}}}".format(self.workspace_id, credit_card_purchase.expense_group.id, credit_card_purchase_payload)) return credit_card_purchase_payload From 9d908e442f1eedb6ffd1bd533d3351c59710a248 Mon Sep 17 00:00:00 2001 From: anishfyle Date: Mon, 16 Dec 2024 17:28:13 +0530 Subject: [PATCH 3/7] revert --- apps/quickbooks_online/utils.py | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/apps/quickbooks_online/utils.py b/apps/quickbooks_online/utils.py index ff111a7c..618449a5 100644 --- a/apps/quickbooks_online/utils.py +++ b/apps/quickbooks_online/utils.py @@ -712,6 +712,7 @@ def get_override_tax_details(self, lines, is_journal_entry_export: bool = False) def purchase_object_payload(self, purchase_object, line, payment_type, account_ref, doc_number: str = None, credit=None): general_settings = WorkspaceGeneralSettings.objects.filter(workspace_id=self.workspace_id).first() + qbo_credentials = QBOCredential.objects.get(workspace_id=self.workspace_id) purchase_object_payload = { 'DocNumber': doc_number if doc_number else None, @@ -726,10 +727,22 @@ def purchase_object_payload(self, purchase_object, line, payment_type, account_r 'Line': line, } + # Add exchange rate for foreign currency transactions + if general_settings.is_multi_currency_allowed and purchase_object.currency != qbo_credentials.currency and qbo_credentials.currency: + exchange_rate = self.connection.exchange_rates.get_by_source(source_currency_code=purchase_object.currency) + purchase_object_payload['ExchangeRate'] = exchange_rate['Rate'] if "Rate" in exchange_rate else 1 + + if isinstance(purchase_object, CreditCardPurchase): + purchase_object.exchange_rate = purchase_object_payload['ExchangeRate'] + purchase_object.save(update_fields=['exchange_rate']) + if general_settings.import_tax_codes: if general_settings.is_tax_override_enabled: tax_details = self.get_override_tax_details(line) - purchase_object_payload.update({'GlobalTaxCalculation': 'TaxExcluded', 'TxnTaxDetail': {"TaxLine": tax_details}}) + purchase_object_payload.update({ + 'GlobalTaxCalculation': 'TaxExcluded', + 'TxnTaxDetail': {"TaxLine": tax_details} + }) else: purchase_object_payload.update({'GlobalTaxCalculation': 'TaxInclusive'}) @@ -1049,9 +1062,6 @@ def __construct_credit_card_purchase(self, credit_card_purchase: CreditCardPurch :return: constructed credit_card_purchase """ 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 = credit_card_purchase.currency line = self.__construct_credit_card_purchase_lineitems(credit_card_purchase_lineitems, general_mappings) credit = False @@ -1065,13 +1075,6 @@ def __construct_credit_card_purchase(self, credit_card_purchase: CreditCardPurch credit_card_purchase_payload = self.purchase_object_payload(credit_card_purchase, line, account_ref=credit_card_purchase.ccc_account_id, payment_type='CreditCard', doc_number=credit_card_purchase.credit_card_purchase_number, credit=credit) - 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) - credit_card_purchase_payload['ExchangeRate'] = exchange_rate['Rate'] if "Rate" in exchange_rate else 1 - - credit_card_purchase.exchange_rate = credit_card_purchase_payload['ExchangeRate'] - credit_card_purchase.save(update_fields=['exchange_rate']) - logger.info("| Payload for Credit Card Purchase creation | Content: {{WORKSPACE_ID: {} EXPENSE_GROUP_ID: {} CREDIT_CARD_PURCHASE_PAYLOAD: {}}}".format(self.workspace_id, credit_card_purchase.expense_group.id, credit_card_purchase_payload)) return credit_card_purchase_payload From e48baf41da746d481497308019b5f509ea4a877e Mon Sep 17 00:00:00 2001 From: anishfyle Date: Mon, 16 Dec 2024 17:53:39 +0530 Subject: [PATCH 4/7] fix --- apps/quickbooks_online/utils.py | 48 ++++++++++++++++----------------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/apps/quickbooks_online/utils.py b/apps/quickbooks_online/utils.py index 618449a5..c0b4b5b1 100644 --- a/apps/quickbooks_online/utils.py +++ b/apps/quickbooks_online/utils.py @@ -727,15 +727,6 @@ def purchase_object_payload(self, purchase_object, line, payment_type, account_r 'Line': line, } - # Add exchange rate for foreign currency transactions - if general_settings.is_multi_currency_allowed and purchase_object.currency != qbo_credentials.currency and qbo_credentials.currency: - exchange_rate = self.connection.exchange_rates.get_by_source(source_currency_code=purchase_object.currency) - purchase_object_payload['ExchangeRate'] = exchange_rate['Rate'] if "Rate" in exchange_rate else 1 - - if isinstance(purchase_object, CreditCardPurchase): - purchase_object.exchange_rate = purchase_object_payload['ExchangeRate'] - purchase_object.save(update_fields=['exchange_rate']) - if general_settings.import_tax_codes: if general_settings.is_tax_override_enabled: tax_details = self.get_override_tax_details(line) @@ -1057,25 +1048,34 @@ def __construct_credit_card_purchase_lineitems(self, credit_card_purchase_lineit def __construct_credit_card_purchase(self, credit_card_purchase: CreditCardPurchase, credit_card_purchase_lineitems: List[CreditCardPurchaseLineitem]) -> Dict: """ - Create a credit_card_purchase - :param credit_card_purchase: credit_card_purchase object extracted from database - :return: constructed credit_card_purchase + Create a credit card purchase + :param credit_card_purchase: credit card purchase object extracted from database + :return: constructed credit card purchase """ - general_mappings = GeneralMapping.objects.filter(workspace_id=self.workspace_id).first() - - line = self.__construct_credit_card_purchase_lineitems(credit_card_purchase_lineitems, general_mappings) - credit = False + 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 = credit_card_purchase.currency + + credit_card_purchase_payload = { + 'DocNumber': credit_card_purchase_lineitems[0].expense.expense_number, + 'PaymentType': 'CreditCard', + 'AccountRef': {'value': credit_card_purchase.ccc_account_id}, + 'EntityRef': {'value': credit_card_purchase.entity_id}, + 'DepartmentRef': {'value': credit_card_purchase.department_id}, + 'TxnDate': credit_card_purchase.transaction_date, + 'CurrencyRef': {'value': credit_card_purchase.currency}, + 'PrivateNote': credit_card_purchase.private_note, + 'Credit': False, + 'Line': self.__construct_credit_card_purchase_lineitems(credit_card_purchase_lineitems) + } - for i in range(len(line)): - if line[i]['Amount'] < 0: - credit = True - tax_amount = line[i][credit_card_purchase_lineitems[i].detail_type]['TaxAmount'] - line[i]['Amount'] = abs(line[i]['Amount']) - line[i][credit_card_purchase_lineitems[i].detail_type]['TaxAmount'] = abs(tax_amount) if tax_amount else None + 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) + credit_card_purchase_payload['ExchangeRate'] = exchange_rate['Rate'] if "Rate" in exchange_rate else 1 - credit_card_purchase_payload = self.purchase_object_payload(credit_card_purchase, line, account_ref=credit_card_purchase.ccc_account_id, payment_type='CreditCard', doc_number=credit_card_purchase.credit_card_purchase_number, credit=credit) + credit_card_purchase.exchange_rate = credit_card_purchase_payload['ExchangeRate'] + credit_card_purchase.save(update_fields=['exchange_rate']) - logger.info("| Payload for Credit Card Purchase creation | Content: {{WORKSPACE_ID: {} EXPENSE_GROUP_ID: {} CREDIT_CARD_PURCHASE_PAYLOAD: {}}}".format(self.workspace_id, credit_card_purchase.expense_group.id, credit_card_purchase_payload)) return credit_card_purchase_payload def post_credit_card_purchase(self, credit_card_purchase: CreditCardPurchase, credit_card_purchase_lineitems: List[CreditCardPurchaseLineitem]): From b10d25fcf3f0a53276614e4bbbbe36b96a604e93 Mon Sep 17 00:00:00 2001 From: anishfyle Date: Tue, 17 Dec 2024 10:03:47 +0530 Subject: [PATCH 5/7] remove unused var --- apps/quickbooks_online/utils.py | 1 - 1 file changed, 1 deletion(-) diff --git a/apps/quickbooks_online/utils.py b/apps/quickbooks_online/utils.py index c0b4b5b1..fd655890 100644 --- a/apps/quickbooks_online/utils.py +++ b/apps/quickbooks_online/utils.py @@ -712,7 +712,6 @@ def get_override_tax_details(self, lines, is_journal_entry_export: bool = False) def purchase_object_payload(self, purchase_object, line, payment_type, account_ref, doc_number: str = None, credit=None): general_settings = WorkspaceGeneralSettings.objects.filter(workspace_id=self.workspace_id).first() - qbo_credentials = QBOCredential.objects.get(workspace_id=self.workspace_id) purchase_object_payload = { 'DocNumber': doc_number if doc_number else None, From dbc009e85e7bbee8d196f891b7a2caa743206b1c Mon Sep 17 00:00:00 2001 From: anishfyle Date: Tue, 17 Dec 2024 10:08:28 +0530 Subject: [PATCH 6/7] revert --- apps/quickbooks_online/utils.py | 49 +++++++++++++++++---------------- 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/apps/quickbooks_online/utils.py b/apps/quickbooks_online/utils.py index fd655890..618449a5 100644 --- a/apps/quickbooks_online/utils.py +++ b/apps/quickbooks_online/utils.py @@ -712,6 +712,7 @@ def get_override_tax_details(self, lines, is_journal_entry_export: bool = False) def purchase_object_payload(self, purchase_object, line, payment_type, account_ref, doc_number: str = None, credit=None): general_settings = WorkspaceGeneralSettings.objects.filter(workspace_id=self.workspace_id).first() + qbo_credentials = QBOCredential.objects.get(workspace_id=self.workspace_id) purchase_object_payload = { 'DocNumber': doc_number if doc_number else None, @@ -726,6 +727,15 @@ def purchase_object_payload(self, purchase_object, line, payment_type, account_r 'Line': line, } + # Add exchange rate for foreign currency transactions + if general_settings.is_multi_currency_allowed and purchase_object.currency != qbo_credentials.currency and qbo_credentials.currency: + exchange_rate = self.connection.exchange_rates.get_by_source(source_currency_code=purchase_object.currency) + purchase_object_payload['ExchangeRate'] = exchange_rate['Rate'] if "Rate" in exchange_rate else 1 + + if isinstance(purchase_object, CreditCardPurchase): + purchase_object.exchange_rate = purchase_object_payload['ExchangeRate'] + purchase_object.save(update_fields=['exchange_rate']) + if general_settings.import_tax_codes: if general_settings.is_tax_override_enabled: tax_details = self.get_override_tax_details(line) @@ -1047,34 +1057,25 @@ def __construct_credit_card_purchase_lineitems(self, credit_card_purchase_lineit def __construct_credit_card_purchase(self, credit_card_purchase: CreditCardPurchase, credit_card_purchase_lineitems: List[CreditCardPurchaseLineitem]) -> Dict: """ - Create a credit card purchase - :param credit_card_purchase: credit card purchase object extracted from database - :return: constructed credit card purchase + Create a credit_card_purchase + :param credit_card_purchase: credit_card_purchase object extracted from database + :return: constructed credit_card_purchase """ - 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 = credit_card_purchase.currency - - credit_card_purchase_payload = { - 'DocNumber': credit_card_purchase_lineitems[0].expense.expense_number, - 'PaymentType': 'CreditCard', - 'AccountRef': {'value': credit_card_purchase.ccc_account_id}, - 'EntityRef': {'value': credit_card_purchase.entity_id}, - 'DepartmentRef': {'value': credit_card_purchase.department_id}, - 'TxnDate': credit_card_purchase.transaction_date, - 'CurrencyRef': {'value': credit_card_purchase.currency}, - 'PrivateNote': credit_card_purchase.private_note, - 'Credit': False, - 'Line': self.__construct_credit_card_purchase_lineitems(credit_card_purchase_lineitems) - } + general_mappings = GeneralMapping.objects.filter(workspace_id=self.workspace_id).first() - 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) - credit_card_purchase_payload['ExchangeRate'] = exchange_rate['Rate'] if "Rate" in exchange_rate else 1 + line = self.__construct_credit_card_purchase_lineitems(credit_card_purchase_lineitems, general_mappings) + credit = False + + for i in range(len(line)): + if line[i]['Amount'] < 0: + credit = True + tax_amount = line[i][credit_card_purchase_lineitems[i].detail_type]['TaxAmount'] + line[i]['Amount'] = abs(line[i]['Amount']) + line[i][credit_card_purchase_lineitems[i].detail_type]['TaxAmount'] = abs(tax_amount) if tax_amount else None - credit_card_purchase.exchange_rate = credit_card_purchase_payload['ExchangeRate'] - credit_card_purchase.save(update_fields=['exchange_rate']) + credit_card_purchase_payload = self.purchase_object_payload(credit_card_purchase, line, account_ref=credit_card_purchase.ccc_account_id, payment_type='CreditCard', doc_number=credit_card_purchase.credit_card_purchase_number, credit=credit) + logger.info("| Payload for Credit Card Purchase creation | Content: {{WORKSPACE_ID: {} EXPENSE_GROUP_ID: {} CREDIT_CARD_PURCHASE_PAYLOAD: {}}}".format(self.workspace_id, credit_card_purchase.expense_group.id, credit_card_purchase_payload)) return credit_card_purchase_payload def post_credit_card_purchase(self, credit_card_purchase: CreditCardPurchase, credit_card_purchase_lineitems: List[CreditCardPurchaseLineitem]): From 0d58d503e56d7d9cf84f897187bc17ffe0be1838 Mon Sep 17 00:00:00 2001 From: anishfyle Date: Tue, 17 Dec 2024 10:15:16 +0530 Subject: [PATCH 7/7] fix --- apps/quickbooks_online/utils.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/apps/quickbooks_online/utils.py b/apps/quickbooks_online/utils.py index 618449a5..2796d3e5 100644 --- a/apps/quickbooks_online/utils.py +++ b/apps/quickbooks_online/utils.py @@ -732,9 +732,8 @@ def purchase_object_payload(self, purchase_object, line, payment_type, account_r exchange_rate = self.connection.exchange_rates.get_by_source(source_currency_code=purchase_object.currency) purchase_object_payload['ExchangeRate'] = exchange_rate['Rate'] if "Rate" in exchange_rate else 1 - if isinstance(purchase_object, CreditCardPurchase): - purchase_object.exchange_rate = purchase_object_payload['ExchangeRate'] - purchase_object.save(update_fields=['exchange_rate']) + purchase_object.exchange_rate = purchase_object_payload['ExchangeRate'] + purchase_object.save(update_fields=['exchange_rate']) if general_settings.import_tax_codes: if general_settings.is_tax_override_enabled: