From 375ab6689da5147868efff6f9745e6ce5f3101ec Mon Sep 17 00:00:00 2001 From: Ashutosh singh <55102089+Ashutosh619-sudo@users.noreply.github.com> Date: Thu, 20 Jun 2024 13:23:30 +0530 Subject: [PATCH] New API for marking expenses as paid on Fyle (#628) --- apps/fyle/models.py | 1 - apps/quickbooks_online/tasks.py | 48 +++++++++------------- requirements.txt | 3 +- tests/test_quickbooks_online/test_tasks.py | 2 +- 4 files changed, 22 insertions(+), 32 deletions(-) diff --git a/apps/fyle/models.py b/apps/fyle/models.py index d083074f..dc466124 100644 --- a/apps/fyle/models.py +++ b/apps/fyle/models.py @@ -147,7 +147,6 @@ def create_expense_objects(expenses: List[Dict], workspace_id: int): 'foreign_currency': expense['foreign_currency'], 'tax_amount': _round_to_currency_fraction(expense['tax_amount'], expense['currency']) if expense['tax_amount'] else None, 'tax_group_id': expense['tax_group_id'], - 'settlement_id': expense['settlement_id'], 'reimbursable': expense['reimbursable'], 'billable': expense['billable'], 'state': expense['state'], diff --git a/apps/quickbooks_online/tasks.py b/apps/quickbooks_online/tasks.py index 236a5c11..eafeb82a 100644 --- a/apps/quickbooks_online/tasks.py +++ b/apps/quickbooks_online/tasks.py @@ -1,7 +1,7 @@ import json import logging import traceback -from datetime import datetime +from datetime import datetime, timezone from typing import List from django.db import transaction @@ -671,39 +671,29 @@ def check_qbo_object_status(workspace_id): def process_reimbursements(workspace_id): fyle_credentials = FyleCredential.objects.get(workspace_id=workspace_id) - platform = PlatformConnector(fyle_credentials) - platform.reimbursements.sync() - - reimbursements = Reimbursement.objects.filter(state='PENDING', workspace_id=workspace_id).all() - reimbursement_ids = [] - - expenses_paid_on_fyle = [] - - if reimbursements: - for reimbursement in reimbursements: - expenses = Expense.objects.filter(settlement_id=reimbursement.settlement_id, fund_source='PERSONAL').all() - paid_expenses = expenses.filter(paid_on_qbo=True) + platform = PlatformConnector(fyle_credentials=fyle_credentials) - all_expense_paid = False - if len(expenses): - all_expense_paid = len(expenses) == len(paid_expenses) + expenses_to_be_marked = [] + payloads = [] - if all_expense_paid: - reimbursement_ids.append(reimbursement.reimbursement_id) - expenses_paid_on_fyle.extend(expenses) + report_ids = Expense.objects.filter(fund_source='PERSONAL', paid_on_fyle=False, workspace_id=workspace_id).values_list('report_id').distinct() + for report_id in report_ids: + report_id = report_id[0] + expenses = Expense.objects.filter(fund_source='PERSONAL', report_id=report_id, workspace_id=workspace_id).all() + paid_expenses = expenses.filter(paid_on_qbo=True) - if reimbursement_ids: - reimbursements_list = [] - for reimbursement_id in reimbursement_ids: - reimbursement_object = {'id': reimbursement_id} - reimbursements_list.append(reimbursement_object) + all_expense_paid = False + if len(expenses): + all_expense_paid = len(expenses) == len(paid_expenses) - platform.reimbursements.bulk_post_reimbursements(reimbursements_list) - platform.reimbursements.sync() + if all_expense_paid: + payloads.append({'id': report_id, 'paid_notify_at': datetime.now(timezone.utc).strftime('%Y-%m-%dT%H:%M:%S.%fZ')}) + expenses_to_be_marked.extend(paid_expenses) - for expense in expenses_paid_on_fyle: - expense.paid_on_fyle = True - expense.save() + platform.reports.bulk_mark_as_paid(payloads) + if expenses_to_be_marked: + expense_ids_to_mark = [expense.id for expense in expenses_to_be_marked] + Expense.objects.filter(id__in=expense_ids_to_mark).update(paid_on_fyle=True) def async_sync_accounts(workspace_id): diff --git a/requirements.txt b/requirements.txt index d60ff86e..6d8d347a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -20,8 +20,9 @@ djangorestframework==3.11.2 django-sendgrid-v5==1.2.0 enum34==1.1.10 future==0.18.2 +fyle==0.37.0 fyle-accounting-mappings==1.32.1 -fyle-integrations-platform-connector==1.37.1 +fyle-integrations-platform-connector==1.38.0 fyle-rest-auth==1.7.2 flake8==4.0.1 gevent==23.9.1 diff --git a/tests/test_quickbooks_online/test_tasks.py b/tests/test_quickbooks_online/test_tasks.py index 16db5d76..dc8ff2a0 100644 --- a/tests/test_quickbooks_online/test_tasks.py +++ b/tests/test_quickbooks_online/test_tasks.py @@ -853,7 +853,7 @@ def test_schedule_reimbursements_sync(db): def test_process_reimbursements(db, mocker): - mocker.patch('fyle_integrations_platform_connector.apis.Reimbursements.bulk_post_reimbursements', return_value=[]) + mocker.patch('fyle_integrations_platform_connector.apis.Reports.bulk_mark_as_paid', return_value=[]) mocker.patch('fyle_integrations_platform_connector.apis.Reimbursements.sync', return_value=[]) workspace_id = 3