Skip to content

Commit

Permalink
Add direct export loggers (#611)
Browse files Browse the repository at this point in the history
* Add direct export loggers

* Import module updated

* Fix failed once
  • Loading branch information
ruuushhh authored May 16, 2024
1 parent 08a2fa3 commit 8716d76
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 8 deletions.
2 changes: 2 additions & 0 deletions apps/quickbooks_online/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

from apps.fyle.actions import update_complete_expenses
from apps.fyle.models import ExpenseGroup
from apps.fyle.tasks import post_accounting_export_summary
from apps.mappings.constants import SYNC_METHODS
from apps.mappings.helpers import get_auto_sync_permission
from apps.quickbooks_online.helpers import generate_export_type_and_id
Expand Down Expand Up @@ -181,3 +182,4 @@ def generate_export_url_and_update_expense(expense_group: ExpenseGroup) -> None:
logger.error('Error while generating export url %s', error)

update_complete_expenses(expense_group.expenses.all(), url)
post_accounting_export_summary(expense_group.workspace.fyle_org_id, expense_group.workspace.id, expense_group.fund_source)
6 changes: 6 additions & 0 deletions apps/quickbooks_online/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from apps.fyle.actions import update_failed_expenses
from apps.fyle.models import ExpenseGroup
from apps.fyle.tasks import post_accounting_export_summary
from apps.quickbooks_online.actions import update_last_export_details
from apps.tasks.models import Error, TaskLog
from apps.workspaces.models import FyleCredential, QBOCredential
Expand Down Expand Up @@ -82,6 +83,7 @@ def new_fn(*args):

if not bill_payment:
update_failed_expenses(expense_group.expenses.all(), True)
post_accounting_export_summary(expense_group.workspace.fyle_org_id, expense_group.workspace.id, expense_group.fund_source)

except (QBOCredential.DoesNotExist, InvalidTokenError):
logger.info('QBO Account not connected / token expired for workspace_id %s / expense group %s', expense_group.workspace_id, expense_group.id)
Expand All @@ -93,12 +95,14 @@ def new_fn(*args):

if not bill_payment:
update_failed_expenses(expense_group.expenses.all(), True)
post_accounting_export_summary(expense_group.workspace.fyle_org_id, expense_group.workspace.id, expense_group.fund_source)

except WrongParamsError as exception:
handle_quickbooks_error(exception, expense_group, task_log, 'Bill')

if not bill_payment:
update_failed_expenses(expense_group.expenses.all(), False)
post_accounting_export_summary(expense_group.workspace.fyle_org_id, expense_group.workspace.id, expense_group.fund_source)

except BulkError as exception:
logger.info(exception.response)
Expand All @@ -110,6 +114,7 @@ def new_fn(*args):

if not bill_payment:
update_failed_expenses(expense_group.expenses.all(), True)
post_accounting_export_summary(expense_group.workspace.fyle_org_id, expense_group.workspace.id, expense_group.fund_source)

except Exception as error:
error = traceback.format_exc()
Expand All @@ -121,6 +126,7 @@ def new_fn(*args):

if not bill_payment:
update_failed_expenses(expense_group.expenses.all(), True)
post_accounting_export_summary(expense_group.workspace.fyle_org_id, expense_group.workspace.id, expense_group.fund_source)

if len(args) > 2 and args[2] == True and not bill_payment:
update_last_export_details(expense_group.workspace_id)
Expand Down
1 change: 0 additions & 1 deletion apps/quickbooks_online/queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ def __create_chain_and_run(fyle_credentials: FyleCredential, in_progress_expense
for task in chain_tasks:
chain.append(task['target'], task['expense_group'], task['task_log_id'], task['last_export'])

chain.append('apps.fyle.tasks.post_accounting_export_summary', fyle_credentials.workspace.fyle_org_id, workspace_id, fund_source)
chain.run()


Expand Down
36 changes: 29 additions & 7 deletions apps/quickbooks_online/tasks.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
import json
from typing import List
import logging
import traceback
from datetime import datetime
from typing import List

from django.db import transaction

from fyle_accounting_mappings.models import DestinationAttribute, EmployeeMapping, ExpenseAttribute, Mapping
from fyle_integrations_platform_connector import PlatformConnector
from qbosdk.exceptions import InvalidTokenError, WrongParamsError

from apps.fyle.models import Expense, ExpenseGroup, Reimbursement
from apps.fyle.actions import update_expenses_in_progress
from apps.fyle.models import Expense, ExpenseGroup, Reimbursement
from apps.fyle.tasks import post_accounting_export_summary
from apps.mappings.models import GeneralMapping
from apps.quickbooks_online.actions import update_last_export_details
from apps.quickbooks_online.actions import generate_export_url_and_update_expense, update_last_export_details
from apps.quickbooks_online.exceptions import handle_qbo_exceptions
from apps.quickbooks_online.models import (
Bill,
Expand All @@ -32,11 +31,9 @@
)
from apps.quickbooks_online.utils import QBOConnector
from apps.tasks.models import Error, TaskLog
from apps.workspaces.models import FyleCredential, QBOCredential, WorkspaceGeneralSettings, Workspace
from apps.workspaces.models import FyleCredential, QBOCredential, Workspace, WorkspaceGeneralSettings
from fyle_qbo_api.exceptions import BulkError

from .actions import generate_export_url_and_update_expense

logger = logging.getLogger(__name__)
logger.level = logging.INFO

Expand Down Expand Up @@ -178,6 +175,8 @@ def create_or_update_employee_mapping(expense_group: ExpenseGroup, qbo_connectio
@handle_qbo_exceptions()
def create_bill(expense_group, task_log_id, last_export: bool):
task_log = TaskLog.objects.get(id=task_log_id)
logger.info('Creating Bill for Expense Group %s, current state is %s', expense_group.id, task_log.status)

if task_log.status not in ['IN_PROGRESS', 'COMPLETE']:
task_log.status = 'IN_PROGRESS'
task_log.save()
Expand All @@ -194,13 +193,15 @@ def create_bill(expense_group, task_log_id, last_export: bool):
create_or_update_employee_mapping(expense_group, qbo_connection, general_settings.auto_map_employees)

__validate_expense_group(expense_group, general_settings)
logger.info('Validated Expense Group %s successfully', expense_group.id)

with transaction.atomic():
bill_object = Bill.create_bill(expense_group)

bill_lineitems_objects = BillLineitem.create_bill_lineitems(expense_group, general_settings)

created_bill = qbo_connection.post_bill(bill_object, bill_lineitems_objects)
logger.info('Created Bill with Expense Group %s successfully', expense_group.id)

task_log.detail = created_bill
task_log.bill = bill_object
Expand All @@ -218,6 +219,7 @@ def create_bill(expense_group, task_log_id, last_export: bool):
update_last_export_details(expense_group.workspace_id)

generate_export_url_and_update_expense(expense_group)
logger.info('Updated Expense Group %s successfully', expense_group.id)

load_attachments(qbo_connection, created_bill['Bill']['Id'], 'Bill', expense_group)

Expand Down Expand Up @@ -348,6 +350,8 @@ def __validate_expense_group(expense_group: ExpenseGroup, general_settings: Work
@handle_qbo_exceptions()
def create_cheque(expense_group, task_log_id, last_export: bool):
task_log = TaskLog.objects.get(id=task_log_id)
logger.info('Creating Cheque for Expense Group %s, current state is %s', expense_group.id, task_log.status)

if task_log.status not in ['IN_PROGRESS', 'COMPLETE']:
task_log.status = 'IN_PROGRESS'
task_log.save()
Expand All @@ -364,13 +368,15 @@ def create_cheque(expense_group, task_log_id, last_export: bool):
create_or_update_employee_mapping(expense_group, qbo_connection, general_settings.auto_map_employees)

__validate_expense_group(expense_group, general_settings)
logger.info('Validated Expense Group %s successfully', expense_group.id)

with transaction.atomic():
cheque_object = Cheque.create_cheque(expense_group)

cheque_line_item_objects = ChequeLineitem.create_cheque_lineitems(expense_group, general_settings)

created_cheque = qbo_connection.post_cheque(cheque_object, cheque_line_item_objects)
logger.info('Created Cheque with Expense Group %s successfully', expense_group.id)

task_log.detail = created_cheque
task_log.cheque = cheque_object
Expand All @@ -389,13 +395,16 @@ def create_cheque(expense_group, task_log_id, last_export: bool):
update_last_export_details(expense_group.workspace_id)

generate_export_url_and_update_expense(expense_group)
logger.info('Updated Expense Group %s successfully', expense_group.id)

load_attachments(qbo_connection, created_cheque['Purchase']['Id'], 'Purchase', expense_group)


@handle_qbo_exceptions()
def create_qbo_expense(expense_group, task_log_id, last_export: bool):
task_log = TaskLog.objects.get(id=task_log_id)
logger.info('Creating QBO Expense for Expense Group %s, current state is %s', expense_group.id, task_log.status)

if task_log.status not in ['IN_PROGRESS', 'COMPLETE']:
task_log.status = 'IN_PROGRESS'
task_log.save()
Expand All @@ -417,13 +426,15 @@ def create_qbo_expense(expense_group, task_log_id, last_export: bool):
get_or_create_credit_card_or_debit_card_vendor(expense_group.workspace_id, merchant, True, general_settings)

__validate_expense_group(expense_group, general_settings)
logger.info('Validated Expense Group %s successfully', expense_group.id)

with transaction.atomic():
qbo_expense_object = QBOExpense.create_qbo_expense(expense_group)

qbo_expense_line_item_objects = QBOExpenseLineitem.create_qbo_expense_lineitems(expense_group, general_settings)

created_qbo_expense = qbo_connection.post_qbo_expense(qbo_expense_object, qbo_expense_line_item_objects)
logger.info('Created QBO Expense with Expense Group %s successfully', expense_group.id)

task_log.detail = created_qbo_expense
task_log.qbo_expense = qbo_expense_object
Expand All @@ -442,13 +453,16 @@ def create_qbo_expense(expense_group, task_log_id, last_export: bool):
update_last_export_details(expense_group.workspace_id)

generate_export_url_and_update_expense(expense_group)
logger.info('Updated Expense Group %s successfully', expense_group.id)

load_attachments(qbo_connection, created_qbo_expense['Purchase']['Id'], 'Purchase', expense_group)


@handle_qbo_exceptions()
def create_credit_card_purchase(expense_group: ExpenseGroup, task_log_id, last_export: bool):
task_log = TaskLog.objects.get(id=task_log_id)
logger.info('Creating Credit Card Purchase for Expense Group %s, current state is %s', expense_group.id, task_log.status)

if task_log.status not in ['IN_PROGRESS', 'COMPLETE']:
task_log.status = 'IN_PROGRESS'
task_log.save()
Expand All @@ -469,13 +483,15 @@ def create_credit_card_purchase(expense_group: ExpenseGroup, task_log_id, last_e
get_or_create_credit_card_or_debit_card_vendor(expense_group.workspace_id, merchant, False, general_settings)

__validate_expense_group(expense_group, general_settings)
logger.info('Validated Expense Group %s successfully', expense_group.id)

with transaction.atomic():
credit_card_purchase_object = CreditCardPurchase.create_credit_card_purchase(expense_group, general_settings.map_merchant_to_vendor)

credit_card_purchase_lineitems_objects = CreditCardPurchaseLineitem.create_credit_card_purchase_lineitems(expense_group, general_settings)

created_credit_card_purchase = qbo_connection.post_credit_card_purchase(credit_card_purchase_object, credit_card_purchase_lineitems_objects)
logger.info('Created Credit Card Purchase with Expense Group %s successfully', expense_group.id)

task_log.detail = created_credit_card_purchase
task_log.credit_card_purchase = credit_card_purchase_object
Expand All @@ -494,13 +510,16 @@ def create_credit_card_purchase(expense_group: ExpenseGroup, task_log_id, last_e
update_last_export_details(expense_group.workspace_id)

generate_export_url_and_update_expense(expense_group)
logger.info('Updated Expense Group %s successfully', expense_group.id)

load_attachments(qbo_connection, created_credit_card_purchase['Purchase']['Id'], 'Purchase', expense_group)


@handle_qbo_exceptions()
def create_journal_entry(expense_group, task_log_id, last_export: bool):
task_log = TaskLog.objects.get(id=task_log_id)
logger.info('Creating Journal Entry for Expense Group %s, current state is %s', expense_group.id, task_log.status)

if task_log.status not in ['IN_PROGRESS', 'COMPLETE']:
task_log.status = 'IN_PROGRESS'
task_log.save()
Expand All @@ -517,6 +536,7 @@ def create_journal_entry(expense_group, task_log_id, last_export: bool):
create_or_update_employee_mapping(expense_group, qbo_connection, general_settings.auto_map_employees)

__validate_expense_group(expense_group, general_settings)
logger.info('Validated Expense Group %s successfully', expense_group.id)

with transaction.atomic():
entity_map = qbo_connection.get_or_create_entity(expense_group, general_settings)
Expand All @@ -526,6 +546,7 @@ def create_journal_entry(expense_group, task_log_id, last_export: bool):
journal_entry_lineitems_objects = JournalEntryLineitem.create_journal_entry_lineitems(expense_group, general_settings, entity_map)

created_journal_entry = qbo_connection.post_journal_entry(journal_entry_object, journal_entry_lineitems_objects, general_settings.je_single_credit_line)
logger.info('Created Journal Entry with Expense Group %s successfully', expense_group.id)

task_log.detail = created_journal_entry
task_log.journal_entry = journal_entry_object
Expand All @@ -544,6 +565,7 @@ def create_journal_entry(expense_group, task_log_id, last_export: bool):
update_last_export_details(expense_group.workspace_id)

generate_export_url_and_update_expense(expense_group)
logger.info('Updated Expense Group %s successfully', expense_group.id)

load_attachments(qbo_connection, created_journal_entry['JournalEntry']['Id'], 'JournalEntry', expense_group)

Expand Down

0 comments on commit 8716d76

Please sign in to comment.