Skip to content

Commit

Permalink
New API for marking expense as paid on Fyle
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashutosh619-sudo committed Jun 20, 2024
1 parent b185f31 commit 1633f9b
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 37 deletions.
1 change: 0 additions & 1 deletion apps/fyle/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,6 @@ def create_expense_objects(expenses: List[Dict], workspace_id: int):
"currency": expense["currency"],
"foreign_amount": expense["foreign_amount"],
"foreign_currency": expense["foreign_currency"],
"settlement_id": expense["settlement_id"],
"reimbursable": expense["reimbursable"],
"state": expense["state"],
"vendor": expense["vendor"][:250]
Expand Down
52 changes: 19 additions & 33 deletions apps/xero/tasks.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging
import traceback
from datetime import datetime
from datetime import datetime, timezone
from time import sleep
from typing import List

Expand Down Expand Up @@ -783,43 +783,29 @@ def check_xero_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=PlatformExpensesEnum.REIMBURSEMENT_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=FundSourceEnum.PERSONAL
).all()
paid_expenses = expenses.filter(paid_on_xero=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_xero=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 create_missing_currency(workspace_id: int):
Expand Down
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ djangorestframework==3.11.2
django-sendgrid-v5==1.2.0
enum34==1.1.10
future==0.18.2
fyle==0.36.1
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
gevent==23.9.1
gunicorn==20.1.0
Expand Down
2 changes: 1 addition & 1 deletion tests/test_xero/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -848,7 +848,7 @@ def test_schedule_reimbursements_sync(db):

def test_process_reimbursements(db, mocker):
mocker.patch(
"fyle_integrations_platform_connector.apis.Reimbursements.bulk_post_reimbursements",
"fyle_integrations_platform_connector.apis.Reports.bulk_mark_as_paid",
return_value=[],
)
mocker.patch(
Expand Down

0 comments on commit 1633f9b

Please sign in to comment.