Skip to content

Commit

Permalink
New API for marking expenses as paid on Fyle (#628)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashutosh619-sudo authored Jun 20, 2024
1 parent c30ae5e commit 375ab66
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 32 deletions.
1 change: 0 additions & 1 deletion apps/fyle/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down
48 changes: 19 additions & 29 deletions apps/quickbooks_online/tasks.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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):
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/test_quickbooks_online/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 375ab66

Please sign in to comment.