From 109e6d516749ea70ccb2452ab7ead7ddc61196a9 Mon Sep 17 00:00:00 2001 From: Ashutosh619-sudo Date: Wed, 20 Nov 2024 16:50:10 +0530 Subject: [PATCH] bug fixed --- .../exports/journal_entry/tasks.py | 32 ++++++++++--------- apps/business_central/utils.py | 28 +++++++++++++--- 2 files changed, 40 insertions(+), 20 deletions(-) diff --git a/apps/business_central/exports/journal_entry/tasks.py b/apps/business_central/exports/journal_entry/tasks.py index af69921..1b3d209 100644 --- a/apps/business_central/exports/journal_entry/tasks.py +++ b/apps/business_central/exports/journal_entry/tasks.py @@ -58,6 +58,9 @@ def __construct_journal_entry(self, body: JournalEntry, lineitems: List[JournalE batch_journal_entry_payload.append(journal_entry_payload) for lineitem in lineitems: + for dimension in lineitem.dimensions: + dimension['exported_module_id'] = lineitem.id + dimensions.extend(lineitem.dimensions) journal_entry_lineitem_payload = { 'accountType': lineitem.account_type, @@ -89,20 +92,17 @@ def post(self, accounting_export, item, lineitem): # Post the journal entry to Business Central response = business_central_connection.bulk_post_journal_lineitems(batch_journal_entry_payload, accounting_export) - try: - if dimensions: - dimension_set_line_payloads = self.construct_dimension_set_line_payload(dimensions, response['responses']) - logger.info('WORKSPACE_ID: {0}, ACCOUNTING_EXPORT_ID: {1}, DIMENSION_SET_LINE_PAYLOADS: {2}'.format(accounting_export.workspace_id, accounting_export.id, dimension_set_line_payloads)) - dimension_line_responses = ( - business_central_connection.post_dimension_lines( - dimension_set_line_payloads, "JOURNAL_ENTRY" - ) + + if dimensions: + dimension_set_line_payloads = self.construct_dimension_set_line_payload(dimensions, response['responses']) + logger.info('WORKSPACE_ID: {0}, ACCOUNTING_EXPORT_ID: {1}, DIMENSION_SET_LINE_PAYLOADS: {2}'.format(accounting_export.workspace_id, accounting_export.id, dimension_set_line_payloads)) + dimension_line_responses = ( + business_central_connection.post_dimension_lines( + dimension_set_line_payloads, "JOURNAL_ENTRY" ) - response["dimension_line_responses"] = dimension_line_responses - except Exception as exception: - lineitem.dimension_error_log = str(exception.response) - response['dimension_line_responses'] = str(exception.response) - lineitem.save() + ) + response["dimension_line_responses"] = dimension_line_responses + expenses = accounting_export.expenses.all() @@ -132,14 +132,16 @@ def construct_dimension_set_line_payload(self, dimensions: list, exported_respon "code": dimension['code'], "parentId": exported_response[0]['body']['id'], "valueId": dimension['valueId'], - "valueCode": dimension['valueCode'] + "valueCode": dimension['valueCode'], + "exported_module_id": dimension['exported_module_id'] }, { "id": dimension['id'], "code": dimension['code'], "parentId": exported_response[1]['body']['id'], "valueId": dimension['valueId'], - "valueCode": dimension['valueCode'] + "valueCode": dimension['valueCode'], + "exported_module_id": dimension['exported_module_id'] }] return dimension_payload diff --git a/apps/business_central/utils.py b/apps/business_central/utils.py index b56da06..3c0eab0 100644 --- a/apps/business_central/utils.py +++ b/apps/business_central/utils.py @@ -1,12 +1,16 @@ import base64 import logging from typing import Dict, List +from collections import defaultdict + from datetime import datetime from django.utils import timezone from dynamics.core.client import Dynamics from fyle_accounting_mappings.models import DestinationAttribute +from apps.business_central.exports.journal_entry.models import JournalEntryLineItems +from apps.business_central.exports.purchase_invoice.models import PurchaseInvoiceLineitems from apps.workspaces.models import BusinessCentralCredentials, ExportSetting, Workspace from ms_business_central_api import settings @@ -295,13 +299,27 @@ def post_dimension_lines(self, dimension_line_payloads: List[Dict], export_modul """ Post dimension line for purchase invoice line and Journal line item """ + exception_response = [] + for dimension_line_payload in dimension_line_payloads: - if export_module_type == 'JOURNAL_ENTRY': - response = self.connection.journal_line_items.post_journal_entry_dimensions(journal_line_item_id=dimension_line_payload['parentId'], data=dimension_line_payload) - else: - response = self.connection.purchase_invoice_line_items.post_purchase_invoice_dimensions(purchase_invoice_item_id=dimension_line_payload['parentId'], data=dimension_line_payload) + exported_module_id = dimension_line_payload['exported_module_id'] + dimension_line_payload.pop('exported_module_id') + try: + if export_module_type == 'JOURNAL_ENTRY': + response = self.connection.journal_line_items.post_journal_entry_dimensions(journal_line_item_id=dimension_line_payload['parentId'], data=dimension_line_payload) + else: + response = self.connection.purchase_invoice_line_items.post_purchase_invoice_dimensions(purchase_invoice_item_id=dimension_line_payload['parentId'], data=dimension_line_payload) + except Exception as exception: + if export_module_type == 'JOURNAL_ENTRY': + lineitem = JournalEntryLineItems.objects.get(id=exported_module_id) + else: + lineitem = PurchaseInvoiceLineitems.objects.get(id=exported_module_id) + + lineitem.dimension_error_log = str(exception.response) + lineitem.save() + exception_response.append(str(exception.response)) - return response + return exception_response def post_attachments( self, ref_type: str, ref_id: str, attachments: List[Dict]