Skip to content

Commit

Permalink
Remove settlement from create payment (#383)
Browse files Browse the repository at this point in the history
* Remove settlement from create payment

* flake fixture fixed
  • Loading branch information
Ashutosh619-sudo committed Jun 28, 2024
1 parent 2490934 commit 0be9b58
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 13 deletions.
37 changes: 24 additions & 13 deletions apps/xero/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand All @@ -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:
Expand Down
30 changes: 30 additions & 0 deletions tests/test_xero/fixtures.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,34 @@
data = {
'expense':[{
"id": 1,
"employee_email": "[email protected]",
"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"},
Expand Down
4 changes: 4 additions & 0 deletions tests/test_xero/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []

Expand Down Expand Up @@ -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 = []

Expand Down

0 comments on commit 0be9b58

Please sign in to comment.