Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable sage fields #183

Merged
merged 20 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions apps/sage300/dependent_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def create_dependent_custom_field_in_fyle(workspace_id: int, fyle_attribute_type
return platform.expense_custom_fields.post(expense_custom_field_payload)


def post_dependent_cost_code(dependent_field_setting: DependentFieldSetting, platform: PlatformConnector, filters: Dict, is_enabled: bool = None) -> List[str]:
def post_dependent_cost_code(dependent_field_setting: DependentFieldSetting, platform: PlatformConnector, filters: Dict, is_enabled: bool = True) -> List[str]:
projects = CostCategory.objects.filter(**filters).values('job_name').annotate(cost_codes=ArrayAgg('cost_code_name', distinct=True))
projects_from_categories = [project['job_name'] for project in projects]
posted_cost_codes = []
Expand All @@ -90,7 +90,7 @@ def post_dependent_cost_code(dependent_field_setting: DependentFieldSetting, pla
'parent_expense_field_value': project['job_name'],
'expense_field_id': dependent_field_setting.cost_code_field_id,
'expense_field_value': cost_code,
'is_enabled': is_enabled if is_enabled is not None else True
'is_enabled': is_enabled
})
cost_code_names.append(cost_code)

Expand Down
16 changes: 12 additions & 4 deletions apps/sage300/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ def disable_projects(workspace_id: int, projects_to_disable: Dict):
}

"""
fyle_credentials = FyleCredential.objects.get(workspace_id=workspace_id)
platform = PlatformConnector(fyle_credentials=fyle_credentials)

filters = {
'workspace_id': workspace_id,
'attribute_type': 'PROJECT',
Expand All @@ -107,17 +110,19 @@ def disable_projects(workspace_id: int, projects_to_disable: Dict):
'is_enabled': False,
'id': expense_attribute.source_id
}
else:
logger.error(f"Project with value {expense_attribute.value} not found | WORKSPACE_ID: {workspace_id}")

bulk_payload.append(payload)

sync_after = datetime.now(timezone.utc)

fyle_credentials = FyleCredential.objects.get(workspace_id=workspace_id)
platform = PlatformConnector(fyle_credentials=fyle_credentials)

if bulk_payload:
logger.info(f"Disabling Projects in Fyle | WORKSPACE_ID: {workspace_id} | COUNT: {len(bulk_payload)}")
platform.projects.post_bulk(bulk_payload)
platform.projects.sync(sync_after=sync_after)
else:
logger.info(f"No Projects to Disable in Fyle | WORKSPACE_ID: {workspace_id}")

update_and_disable_cost_code(workspace_id, projects_to_disable, platform)
Hrishabh17 marked this conversation as resolved.
Show resolved Hide resolved

Expand All @@ -135,7 +140,9 @@ def update_and_disable_cost_code(workspace_id: int, cost_codes_to_disable: Dict,
}

# This call will disable the cost codes in Fyle that has old project name
post_dependent_cost_code(dependent_field_setting, platform, filters, is_enabled=False)
posted_cost_codes = post_dependent_cost_code(dependent_field_setting, platform, filters, is_enabled=False)

logger.info(f"Disabling Cost Codes in Fyle | WORKSPACE_ID: {workspace_id} | COUNT: {len(posted_cost_codes)}")
Hrishabh17 marked this conversation as resolved.
Show resolved Hide resolved

# here we are updating the CostCategory with the new project name
bulk_update_payload = []
Expand All @@ -151,4 +158,5 @@ def update_and_disable_cost_code(workspace_id: int, cost_codes_to_disable: Dict,
bulk_update_payload.append(cost_category)

if bulk_update_payload:
logger.info(f"Updating Cost Categories | WORKSPACE_ID: {workspace_id} | COUNT: {len(bulk_update_payload)}")
CostCategory.objects.bulk_update(bulk_update_payload, ['job_name', 'updated_at'], batch_size=50)
Loading