diff --git a/apps/sage300/exports/base_model.py b/apps/sage300/exports/base_model.py index f76f4e7e..73260025 100644 --- a/apps/sage300/exports/base_model.py +++ b/apps/sage300/exports/base_model.py @@ -174,7 +174,7 @@ def get_job_id(accounting_export: AccountingExport, expense: Expense): elif job_settings.source_field == 'COST_CENTER': source_value = expense.cost_center else: - attribute = ExpenseAttribute.objects.filter(attribute_type=job_settings.source_field).first() + attribute = ExpenseAttribute.objects.filter(attribute_type=job_settings.source_field, workspace_id=accounting_export.workspace_id).first() source_value = expense.custom_properties.get(attribute.display_name, None) # Check for a mapping based on the source value @@ -243,7 +243,7 @@ def get_standard_category_id(accounting_export: AccountingExport, expense: Expen if standard_category_setting: # Retrieve the attribute corresponding to the source field - attribute = ExpenseAttribute.objects.filter(attribute_type=standard_category_setting.source_field).first() + attribute = ExpenseAttribute.objects.filter(attribute_type=standard_category_setting.source_field, workspace_id=accounting_export.workspace_id).first() # Determine the source value based on the configured source field source_value = expense.custom_properties.get(attribute.display_name, None) @@ -283,7 +283,7 @@ def get_standard_cost_code_id(accounting_export: AccountingExport, expense: Expe if standard_cost_code_setting: # Retrieve the attribute corresponding to the source field - attribute = ExpenseAttribute.objects.filter(attribute_type=standard_cost_code_setting.source_field).first() + attribute = ExpenseAttribute.objects.filter(attribute_type=standard_cost_code_setting.source_field, workspace_id=accounting_export.workspace_id).first() # Determine the source value based on the configured source field source_value = expense.custom_properties.get(attribute.display_name, None) diff --git a/apps/sage300/exports/purchase_invoice/queues.py b/apps/sage300/exports/purchase_invoice/queues.py index 17a2dd5d..b1a01f11 100644 --- a/apps/sage300/exports/purchase_invoice/queues.py +++ b/apps/sage300/exports/purchase_invoice/queues.py @@ -30,22 +30,20 @@ def check_accounting_export_and_start_import(workspace_id: int, accounting_expor """ Check accounting export group and start export """ - - # fyle_credentials = FyleCredential.objects.filter(workspace_id=workspace_id).first() + fyle_credentials = FyleCredential.objects.filter(workspace_id=workspace_id).first() accounting_exports = AccountingExport.objects.filter( - ~Q(status__in=['IN_PROGRESS', 'COMPLETE', 'EXPORT_QUEUED']), + ~Q(status__in=['ENQUEUED', 'IN_PROGRESS', 'COMPLETE', 'EXPORT_QUEUED']), workspace_id=workspace_id, id__in=accounting_export_ids, purchaseinvoice__id__isnull=True, exported_at__isnull=True ).all() chain = Chain() - # Todo: uncomment this later - # chain.append('apps.fyle.helpers.sync_dimensions', fyle_credentials) + chain.append('apps.fyle.helpers.sync_dimensions', fyle_credentials) for index, accounting_export_group in enumerate(accounting_exports): - accounting_export, _ = AccountingExport.objects.update_or_create( + accounting_export, _ = AccountingExport.objects.get_or_create( workspace_id=accounting_export_group.workspace_id, id=accounting_export_group.id, defaults={ diff --git a/apps/sage300/helpers.py b/apps/sage300/helpers.py index a6dd2d11..4454d62a 100644 --- a/apps/sage300/helpers.py +++ b/apps/sage300/helpers.py @@ -6,7 +6,6 @@ from django.utils.module_loading import import_string from apps.workspaces.models import Workspace, Sage300Credential, FyleCredential -from apps.mappings.models import Version from fyle_accounting_mappings.models import ExpenseAttribute from fyle_integrations_platform_connector import PlatformConnector from apps.sage300.models import CostCategory @@ -50,9 +49,6 @@ def sync_dimensions(sage300_credential: Sage300Credential, workspace_id: int) -> This function syncs dimensions like accounts, vendors, commitments, jobs, categories, and cost codes. """ - - Version.objects.update_or_create(workspace_id=workspace_id) - # Initialize the Sage 300 connection using the provided credentials and workspace ID sage300_connection = import_string('apps.sage300.utils.SageDesktopConnector')(sage300_credential, workspace_id) diff --git a/apps/workspaces/serializers.py b/apps/workspaces/serializers.py index 6a9250be..de572582 100644 --- a/apps/workspaces/serializers.py +++ b/apps/workspaces/serializers.py @@ -30,6 +30,7 @@ AdvancedSetting ) from apps.accounting_exports.models import AccountingExportSummary +from apps.mappings.models import Version from apps.users.models import User from apps.fyle.helpers import get_cluster_domain from apps.workspaces.triggers import ImportSettingsTrigger, AdvancedSettingsTriggers @@ -70,6 +71,7 @@ def create(self, validated_data): name=name, org_id=org_id, ) + Version.objects.create(workspace_id=workspace.id) workspace.user.add(User.objects.get(user_id=user)) diff --git a/apps/workspaces/tasks.py b/apps/workspaces/tasks.py index 1185662d..47ed08b1 100644 --- a/apps/workspaces/tasks.py +++ b/apps/workspaces/tasks.py @@ -78,7 +78,7 @@ def run_import_export(workspace_id: int, export_mode = None): accounting_summary.last_exported_at = last_exported_at accounting_summary.export_mode = export_mode or 'MANUAL' - if advance_settings: + if advance_settings and advance_settings.schedule_is_enabled: accounting_summary.next_export_at = last_exported_at + timedelta(hours=advance_settings.interval_hours) accounting_summary.save() @@ -132,8 +132,7 @@ def export_to_sage300(workspace_id: int): # Retrieve export settings for the given workspace export_settings = ExportSetting.objects.get(workspace_id=workspace_id) - # Update or create an AccountingExportSummary for the workspace - accounting_summary, _ = AccountingExportSummary.objects.update_or_create(workspace_id=workspace_id) + accounting_summary = AccountingExportSummary.objects.get(workspace_id=workspace_id) advance_settings = AdvancedSetting.objects.filter(workspace_id=workspace_id).first() # Set the timestamp for the last export @@ -152,7 +151,7 @@ def export_to_sage300(workspace_id: int): if export_settings.reimbursable_expenses_export_type: # Get IDs of unreexported accounting exports for personal fund source accounting_export_ids = AccountingExport.objects.filter( - fund_source='PERSONAL', exported_at__isnull=True).values_list('id', flat=True) + fund_source='PERSONAL', exported_at__isnull=True, workspace_id=workspace_id).values_list('id', flat=True) if len(accounting_export_ids): # Set the flag indicating expenses are exported @@ -165,7 +164,7 @@ def export_to_sage300(workspace_id: int): if export_settings.credit_card_expense_export_type: # Get IDs of unreexported accounting exports for credit card fund source accounting_export_ids = AccountingExport.objects.filter( - fund_source='CCC', exported_at__isnull=True).values_list('id', flat=True) + fund_source='CCC', exported_at__isnull=True, workspace_id=workspace_id).values_list('id', flat=True) if len(accounting_export_ids): # Set the flag indicating expenses are exported diff --git a/tests/test_workspaces/test_tasks.py b/tests/test_workspaces/test_tasks.py index 008be5c9..887e3596 100644 --- a/tests/test_workspaces/test_tasks.py +++ b/tests/test_workspaces/test_tasks.py @@ -219,6 +219,7 @@ def test_export_to_sage300( add_accounting_export_expenses ): workspace_id = 1 + AccountingExportSummary.objects.create(workspace_id=workspace_id) accounting_export = AccountingExport.objects.filter(workspace_id=workspace_id).first() advanced_settings = AdvancedSetting.objects.get(workspace_id=workspace_id) diff --git a/tests/test_workspaces/test_views.py b/tests/test_workspaces/test_views.py index 5f4d0cab..45a32c26 100644 --- a/tests/test_workspaces/test_views.py +++ b/tests/test_workspaces/test_views.py @@ -4,7 +4,7 @@ from django.urls import reverse from fyle_accounting_mappings.models import MappingSetting -from apps.accounting_exports.models import AccountingExport +from apps.accounting_exports.models import AccountingExport, AccountingExportSummary from apps.workspaces.models import ( Workspace, Sage300Credential, @@ -425,6 +425,8 @@ def test_trigger_export( """ Test Export Trigger API """ + workspace_id = 1 + AccountingExportSummary.objects.create(workspace_id=workspace_id) url = reverse('trigger-exports', kwargs={'workspace_id': 1}) api_client.credentials(HTTP_AUTHORIZATION='Bearer {}'.format(test_connection.access_token))