diff --git a/apps/accounting_exports/models.py b/apps/accounting_exports/models.py index eaae2791..6a376c09 100644 --- a/apps/accounting_exports/models.py +++ b/apps/accounting_exports/models.py @@ -66,14 +66,13 @@ def _group_expenses(expenses: List[Expense], export_setting: ExportSetting, fund group_by_field = fund_source_data.get('group_by') date_field = fund_source_data.get('date_field') - default_fields.extend([group_by_field, fund_source]) + default_fields.extend(group_by_field) if date_field: default_fields.append(date_field) # Extract expense IDs from the provided expenses expense_ids = [expense.id for expense in expenses] - # Retrieve expenses from the database expenses = Expense.objects.filter(id__in=expense_ids).all() @@ -109,7 +108,6 @@ def create_accounting_export(expense_objects: List[Expense], fund_source: str, w """ Group expenses by report_id and fund_source, format date fields, and create AccountingExport objects. """ - # Retrieve the ExportSetting for the workspace export_setting = ExportSetting.objects.get(workspace_id=workspace_id) diff --git a/apps/fyle/exceptions.py b/apps/fyle/exceptions.py index 4d14ae44..469b0b80 100644 --- a/apps/fyle/exceptions.py +++ b/apps/fyle/exceptions.py @@ -16,22 +16,22 @@ def wrapper(*args, **kwargs): try: return func(*args, **kwargs) except FyleCredential.DoesNotExist: - logger.info('Fyle credentials not found %s', args[1]) # args[1] is workspace_id - args[2].detail = {'message': 'Fyle credentials do not exist in workspace'} - args[2].status = 'FAILED' - args[2].save() + logger.info('Fyle credentials not found %s', args[0]) # args[1] is workspace_id + args[1].detail = {'message': 'Fyle credentials do not exist in workspace'} + args[1].status = 'FAILED' + args[1].save() except NoPrivilegeError: logger.info('Invalid Fyle Credentials / Admin is disabled') - args[2].detail = {'message': 'Invalid Fyle Credentials / Admin is disabled'} - args[2].status = 'FAILED' - args[2].save() + args[1].detail = {'message': 'Invalid Fyle Credentials / Admin is disabled'} + args[1].status = 'FAILED' + args[1].save() except Exception: error = traceback.format_exc() - args[2].detail = {'error': error} - args[2].status = 'FATAL' - args[2].save() - logger.exception('Something unexpected happened workspace_id: %s %s', args[1], args[2].detail) + args[1].detail = {'error': error} + args[1].status = 'FATAL' + args[1].save() + logger.exception('Something unexpected happened workspace_id: %s %s', args[0], args[1].detail) return wrapper diff --git a/apps/fyle/models.py b/apps/fyle/models.py index 293967fd..fb8aefc4 100644 --- a/apps/fyle/models.py +++ b/apps/fyle/models.py @@ -121,7 +121,6 @@ def create_expense_objects(expenses: List[Dict], workspace_id: int): # Create an empty list to store expense objects expense_objects = [] - for expense in expenses: # Iterate through custom property fields and handle empty values for custom_property_field in expense['custom_properties']: @@ -171,7 +170,7 @@ def create_expense_objects(expenses: List[Dict], workspace_id: int): ) # Check if an AccountingExport related to the expense object already exists - if not Expense.objects.filter(accountingexport__isnull=False).distinct(): + if not expense_object.accountingexport_set.exists(): expense_objects.append(expense_object) return expense_objects diff --git a/apps/fyle/tasks.py b/apps/fyle/tasks.py index 33d4c041..c9549436 100644 --- a/apps/fyle/tasks.py +++ b/apps/fyle/tasks.py @@ -39,18 +39,18 @@ def import_expenses(workspace_id, accounting_export: AccountingExport, source_ac } export_settings = ExportSetting.objects.get(workspace_id=workspace_id) workspace = Workspace.objects.get(pk=workspace_id) - last_synced_at = getattr(workspace, f"{fund_source_key.lower()}_last_synced_at", None) + last_synced_at = getattr(workspace, f"{fund_source_map.get(fund_source_key)}_last_synced_at", None) fyle_credentials = FyleCredential.objects.get(workspace_id=workspace_id) platform = PlatformConnector(fyle_credentials) expenses = platform.expenses.get( source_account_type=[source_account_type], - state=getattr(export_settings, f"{fund_source_key.lower()}_expense_state"), + state=getattr(export_settings, f"{fund_source_map.get(fund_source_key)}_expense_state"), settled_at=last_synced_at if getattr(export_settings, f"{fund_source_map.get(fund_source_key)}_expense_state") == 'PAYMENT_PROCESSING' else None, - approved_at=last_synced_at if getattr(export_settings, f"{fund_source_map.get(fund_source_key)}_expense_state") == 'APPROVED' else None, + # approved_at=last_synced_at if getattr(export_settings, f"{fund_source_map.get(fund_source_key)}_expense_state") == 'APPROVED' else None, filter_credit_expenses=(fund_source_key == 'CCC'), - last_paid_at=last_synced_at if getattr(export_settings, f"{fund_source_key.lower()}_expense_state") == 'PAID' else None + last_paid_at=last_synced_at if getattr(export_settings, f"{fund_source_map.get(fund_source_key)}_expense_state") == 'PAID' else None ) if expenses: @@ -59,7 +59,7 @@ def import_expenses(workspace_id, accounting_export: AccountingExport, source_ac with transaction.atomic(): expenses_object = Expense.create_expense_objects(expenses, workspace_id) - AccountingExport.create_accounting_export_report_id( + AccountingExport.create_accounting_export( expenses_object, fund_source=fund_source_key, workspace_id=workspace_id diff --git a/apps/workspaces/models.py b/apps/workspaces/models.py index 653b9bb1..42a27b81 100644 --- a/apps/workspaces/models.py +++ b/apps/workspaces/models.py @@ -38,8 +38,8 @@ class Workspace(models.Model): name = StringNotNullField(help_text='Name of the workspace') user = models.ManyToManyField(User, help_text='Reference to users table') org_id = models.CharField(max_length=255, help_text='org id', unique=True) - last_synced_at = CustomDateTimeField(help_text='Datetime when expenses were pulled last') - ccc_last_synced_at = CustomDateTimeField(help_text='Datetime when ccc expenses were pulled last') + reimbursable_last_synced_at = CustomDateTimeField(help_text='Datetime when expenses were pulled last') + credit_card_last_synced_at = CustomDateTimeField(help_text='Datetime when ccc expenses were pulled last') source_synced_at = CustomDateTimeField(help_text='Datetime when source dimensions were pulled') destination_synced_at = CustomDateTimeField(help_text='Datetime when destination dimensions were pulled') onboarding_state = StringOptionsField(