diff --git a/apps/xero/tasks.py b/apps/xero/tasks.py index a15b22b2..27a283cb 100644 --- a/apps/xero/tasks.py +++ b/apps/xero/tasks.py @@ -5,13 +5,14 @@ from typing import List from django.db import transaction +from apps.fyle.helpers import get_filter_credit_expenses from fyle_accounting_mappings.models import DestinationAttribute, ExpenseAttribute, Mapping from fyle_integrations_platform_connector import PlatformConnector from xerosdk.exceptions import UnsuccessfulAuthentication, WrongParamsError from apps.fyle.actions import update_complete_expenses, update_expenses_in_progress -from apps.fyle.enums import FundSourceEnum, FyleAttributeEnum, PlatformExpensesEnum -from apps.fyle.models import Expense, ExpenseGroup, Reimbursement +from apps.fyle.enums import FundSourceEnum, FyleAttributeEnum +from apps.fyle.models import Expense, ExpenseGroup, ExpenseGroupSettings from apps.fyle.tasks import post_accounting_export_summary from apps.mappings.models import GeneralMapping, TenantMapping from apps.tasks.enums import ErrorTypeEnum, TaskLogStatusEnum, TaskLogTypeEnum @@ -644,18 +645,27 @@ def __validate_expense_group(expense_group: ExpenseGroup): raise BulkError("Mappings are missing", bulk_errors) -def check_expenses_reimbursement_status(expenses): - all_expenses_paid = True +def check_expenses_reimbursement_status(expenses, workspace_id, platform, filter_credit_expenses): - for expense in expenses: - reimbursement = Reimbursement.objects.filter( - settlement_id=expense.settlement_id - ).first() + if expenses.first().paid_on_fyle: + return True + + report_id = expenses.first().report_id + + expenses = platform.expenses.get( + source_account_type=['PERSONAL_CASH_ACCOUNT'], + filter_credit_expenses=filter_credit_expenses, + report_id=report_id + ) + + is_paid = False + if expenses: + is_paid = expenses[0]['state'] == 'PAID' - if reimbursement.state != PlatformExpensesEnum.REIMBURSEMENT_COMPLETE: - all_expenses_paid = False + if is_paid: + Expense.objects.filter(workspace_id=workspace_id, report_id=report_id, paid_on_fyle=False).update(paid_on_fyle=True) - return all_expenses_paid + return is_paid @handle_xero_exceptions(payment=True) @@ -693,7 +703,8 @@ def create_payment(workspace_id): fyle_credentials = FyleCredential.objects.get(workspace_id=workspace_id) platform = PlatformConnector(fyle_credentials) - platform.reimbursements.sync() + expense_group_settings = ExpenseGroupSettings.objects.get(workspace_id=workspace_id) + filter_credit_expenses = get_filter_credit_expenses(expense_group_settings=expense_group_settings) bills: List[Bill] = Bill.objects.filter( payment_synced=False, @@ -707,7 +718,7 @@ def create_payment(workspace_id): for bill in bills: expense_group_reimbursement_status = check_expenses_reimbursement_status( - bill.expense_group.expenses.all() + bill.expense_group.expenses.all(), workspace_id=workspace_id, platform=platform, filter_credit_expenses=filter_credit_expenses ) if expense_group_reimbursement_status: diff --git a/tests/test_xero/fixtures.py b/tests/test_xero/fixtures.py index 374dfcf9..1621933f 100644 --- a/tests/test_xero/fixtures.py +++ b/tests/test_xero/fixtures.py @@ -1,4 +1,34 @@ data = { + 'expense':[{ + "id": 1, + "employee_email": "ashwin.t@fyle.in", + "employee_name": None, + "category": "Accounts Payable", + "sub_category": "Accounts Payable", + "project": None, + "project_id": None, + "org_id": "or79Cob97KSh", + "expense_id": "txjvDntD9ZXR", + "expense_number": "E/2021/11/T/11", + "claim_number": "C/2021/11/R/5", + "amount": 50.0, + "currency": "USD", + "foreign_amount": None, + "foreign_currency": None, + "tax_amount": None, + "tax_group_id": None, + "settlement_id": "set6GUp6tcEEp", + "reimbursable": True, + "billable": None, + "state": "PAID", + "vendor": None, + "cost_center": "Treasury", + "purpose": None, + "report_id": "rpuN3bgphxbK", + "report_title": None, + "corporate_card_id": None, + "file_ids": None + }], "bill_payload": { "VendorRef": {"value": "43"}, "APAccountRef": {"value": "33"}, diff --git a/tests/test_xero/test_tasks.py b/tests/test_xero/test_tasks.py index 71425535..342c2133 100644 --- a/tests/test_xero/test_tasks.py +++ b/tests/test_xero/test_tasks.py @@ -665,6 +665,8 @@ def test_create_payment(mocker, db): return_value=fyle_data["get_all_reimbursements"], ) + mocker.patch('fyle_integrations_platform_connector.apis.Expenses.get', return_value=data['expense']) + bills = Bill.objects.all() expenses = [] @@ -711,6 +713,8 @@ def test_create_payment_exceptions(mocker, db): return_value=fyle_data["get_all_reimbursements"], ) + mocker.patch('fyle_integrations_platform_connector.apis.Expenses.get', return_value=data['expense']) + bills = Bill.objects.all() expenses = []