From 2cb05b792a909173fcc619a5120f5e5e3976bd35 Mon Sep 17 00:00:00 2001 From: Hrishabh Tiwari <74908943+Hrishabh17@users.noreply.github.com> Date: Fri, 12 Jul 2024 09:40:10 +0530 Subject: [PATCH] fix edit expenses (#148) --- apps/fyle/models.py | 82 +++++++++++++---------- apps/fyle/tasks.py | 2 +- ms_business_central_api/settings.py | 2 +- ms_business_central_api/tests/settings.py | 2 +- requirements.txt | 2 +- tests/test_fyle/fixtures.py | 24 ------- 6 files changed, 50 insertions(+), 64 deletions(-) diff --git a/apps/fyle/models.py b/apps/fyle/models.py index 74529ad..56d1e49 100644 --- a/apps/fyle/models.py +++ b/apps/fyle/models.py @@ -96,7 +96,7 @@ class Meta: db_table = 'expenses' @staticmethod - def create_expense_objects(expenses: List[Dict], workspace_id: int): + def create_expense_objects(expenses: List[Dict], workspace_id: int, skip_update: bool = False): """ Bulk create expense objects """ @@ -110,45 +110,55 @@ def create_expense_objects(expenses: List[Dict], workspace_id: int): if expense['custom_properties'][custom_property_field] == '': expense['custom_properties'][custom_property_field] = None - # Create or update an Expense object based on expense_id - expense_object, _ = Expense.objects.update_or_create( - expense_id=expense['id'], - defaults={ - 'employee_email': expense['employee_email'], - 'employee_name': expense['employee_name'], - 'category': expense['category'], - 'sub_category': expense['sub_category'], - 'project': expense['project'], - 'expense_number': expense['expense_number'], - 'org_id': expense['org_id'], + expense_data_to_append = None + if not skip_update: + expense_data_to_append = { 'claim_number': expense['claim_number'], - 'amount': round(expense['amount'], 2), - 'currency': expense['currency'], - 'foreign_amount': expense['foreign_amount'], - 'foreign_currency': expense['foreign_currency'], - 'tax_amount': expense['tax_amount'], - 'tax_group_id': expense['tax_group_id'], - 'reimbursable': expense['reimbursable'], - 'billable': expense['billable'] if expense['billable'] else False, - 'state': expense['state'], - 'vendor': expense['vendor'][:250] if expense['vendor'] else None, - 'cost_center': expense['cost_center'], - 'purpose': expense['purpose'], - 'report_id': expense['report_id'], 'report_title': expense['report_title'], - 'spent_at': expense['spent_at'], 'approved_at': expense['approved_at'], - 'posted_at': expense['posted_at'], - 'expense_created_at': expense['expense_created_at'], - 'expense_updated_at': expense['expense_updated_at'], - 'fund_source': SOURCE_ACCOUNT_MAP[expense['source_account_type']], - 'verified_at': expense['verified_at'], - 'custom_properties': expense['custom_properties'], - 'payment_number': expense['payment_number'], - 'file_ids': expense['file_ids'], - 'corporate_card_id': expense['corporate_card_id'], - 'workspace_id': workspace_id + 'payment_number': expense['payment_number'] } + + defaults = { + 'employee_email': expense['employee_email'], + 'employee_name': expense['employee_name'], + 'category': expense['category'], + 'sub_category': expense['sub_category'], + 'project': expense['project'], + 'expense_number': expense['expense_number'], + 'org_id': expense['org_id'], + 'amount': round(expense['amount'], 2), + 'currency': expense['currency'], + 'foreign_amount': expense['foreign_amount'], + 'foreign_currency': expense['foreign_currency'], + 'tax_amount': expense['tax_amount'], + 'tax_group_id': expense['tax_group_id'], + 'reimbursable': expense['reimbursable'], + 'billable': expense['billable'] if expense['billable'] else False, + 'state': expense['state'], + 'vendor': expense['vendor'][:250] if expense['vendor'] else None, + 'cost_center': expense['cost_center'], + 'purpose': expense['purpose'], + 'report_id': expense['report_id'], + 'spent_at': expense['spent_at'], + 'posted_at': expense['posted_at'], + 'expense_created_at': expense['expense_created_at'], + 'expense_updated_at': expense['expense_updated_at'], + 'fund_source': SOURCE_ACCOUNT_MAP[expense['source_account_type']], + 'verified_at': expense['verified_at'], + 'custom_properties': expense['custom_properties'], + 'file_ids': expense['file_ids'], + 'corporate_card_id': expense['corporate_card_id'], + 'workspace_id': workspace_id + } + + if expense_data_to_append: + defaults.update(expense_data_to_append) + + # Create or update an Expense object based on expense_id + expense_object, _ = Expense.objects.update_or_create( + expense_id=expense['id'], + defaults=defaults ) # Check if an AccountingExport related to the expense object already exists diff --git a/apps/fyle/tasks.py b/apps/fyle/tasks.py index f6fcb1e..45ec182 100644 --- a/apps/fyle/tasks.py +++ b/apps/fyle/tasks.py @@ -117,5 +117,5 @@ def update_non_exported_expenses(data: Dict) -> None: expense_obj.append(data) expense_objects = FyleExpenses().construct_expense_object(expense_obj, expense.workspace_id) Expense.create_expense_objects( - expense_objects, expense.workspace_id + expense_objects, expense.workspace_id, skip_update=True ) diff --git a/ms_business_central_api/settings.py b/ms_business_central_api/settings.py index b2062b8..0fb4444 100644 --- a/ms_business_central_api/settings.py +++ b/ms_business_central_api/settings.py @@ -198,7 +198,7 @@ 'cached': False, 'orm': 'default', 'ack_failures': True, - 'poll': 1, + 'poll': 5, 'max_attempts': 1, 'attempt_count': 1, # The number of tasks a worker will process before recycling. diff --git a/ms_business_central_api/tests/settings.py b/ms_business_central_api/tests/settings.py index 33fc678..74884ab 100644 --- a/ms_business_central_api/tests/settings.py +++ b/ms_business_central_api/tests/settings.py @@ -128,7 +128,7 @@ 'cached': False, 'orm': 'default', 'ack_failures': True, - 'poll': 1, + 'poll': 5, 'max_attempts': 1, 'attempt_count': 1, # The number of tasks a worker will process before recycling. diff --git a/requirements.txt b/requirements.txt index 19f10d5..9465828 100644 --- a/requirements.txt +++ b/requirements.txt @@ -33,7 +33,7 @@ ms-dynamics-business-central-sdk==1.4.2 # Reusable Fyle Packages fyle-rest-auth==1.7.2 fyle-accounting-mappings==1.33.1 -fyle-integrations-platform-connector==1.38.1 +fyle-integrations-platform-connector==1.38.4 # Postgres Dependincies psycopg2-binary==2.9.9 diff --git a/tests/test_fyle/fixtures.py b/tests/test_fyle/fixtures.py index adf52c8..80d7861 100644 --- a/tests/test_fyle/fixtures.py +++ b/tests/test_fyle/fixtures.py @@ -159,30 +159,6 @@ }, 'project_id': 330241, 'purpose': None, - 'report': { - 'amount': 12, - 'approvals': [ - { - 'approver_user': { - 'email': 'admin1@fyleforimporrttest.in', - 'full_name': 'Theresa Brown', - 'id': 'usVN2WTtPqE7' - }, - 'approver_user_id': 'usVN2WTtPqE7', - 'state': 'APPROVAL_DONE' - } - ], - 'id': 'rpN41rGGnxNI', - 'last_approved_at': '2024-05-10T07:53:25.774+00:00', - 'last_paid_at': None, - 'last_submitted_at': '2024-05-10T07:53:09.457+00:00', - 'last_verified_at': '2024-05-10T07:55:02.32928+00:00', - 'reimbursement_id': 'reimYNNUkKQiWp', - 'reimbursement_seq_num': 'P/2024/05/T/P/2024/05/R/30', - 'seq_num': 'C/2024/05/R/45', - 'state': 'PAYMENT_PROCESSING', - 'title': '#5: May 2024' - }, 'report_id': 'rpN41rGGnxNI', 'report_last_approved_at': '2024-05-10T07:53:25.774000+00:00', 'report_last_paid_at': None,