diff --git a/apps/mappings/imports/modules/projects.py b/apps/mappings/imports/modules/projects.py new file mode 100644 index 0000000..314d6e3 --- /dev/null +++ b/apps/mappings/imports/modules/projects.py @@ -0,0 +1,63 @@ +from datetime import datetime +from typing import List + +from fyle_accounting_mappings.models import DestinationAttribute + +from apps.mappings.imports.modules.base import Base + + +class Project(Base): + """ + Class for Project module + """ + + def __init__(self, workspace_id: int, destination_field: str, sync_after: datetime): + super().__init__( + workspace_id=workspace_id, + source_field="PROJECT", + destination_field=destination_field, + platform_class_name="projects", + sync_after=sync_after, + ) + + def trigger_import(self): + """ + Trigger import for Project module + """ + self.check_import_log_and_start_import() + + def construct_fyle_payload( + self, + paginated_destination_attributes: List[DestinationAttribute], + existing_fyle_attributes_map: object, + is_auto_sync_status_allowed: bool + ): + """ + Construct Fyle payload for Project module + :param paginated_destination_attributes: List of paginated destination attributes + :param existing_fyle_attributes_map: Existing Fyle attributes map + :param is_auto_sync_status_allowed: Is auto sync status allowed + :return: Fyle payload + """ + payload = [] + + for attribute in paginated_destination_attributes: + project = { + 'name': attribute.value, + 'code': attribute.destination_id, + 'description': 'Sage 300 Project - {0}, Id - {1}'.format( + attribute.value, + attribute.destination_id + ), + 'is_enabled': True if attribute.active is None else attribute.active + } + + # Create a new project if it does not exist in Fyle + if attribute.value.lower() not in existing_fyle_attributes_map: + payload.append(project) + # Disable the existing project in Fyle if auto-sync status is allowed and the destination_attributes is inactive + elif is_auto_sync_status_allowed and not attribute.active: + project['id'] = existing_fyle_attributes_map[attribute.value.lower()] + payload.append(project) + + return payload diff --git a/apps/mappings/imports/tasks.py b/apps/mappings/imports/tasks.py index 98f796d..685a9ca 100644 --- a/apps/mappings/imports/tasks.py +++ b/apps/mappings/imports/tasks.py @@ -2,12 +2,14 @@ from apps.mappings.imports.modules.cost_centers import CostCenter from apps.mappings.imports.modules.expense_custom_fields import ExpenseCustomField from apps.mappings.imports.modules.merchants import Merchant +from apps.mappings.imports.modules.projects import Project from apps.mappings.models import ImportLog SOURCE_FIELD_CLASS_MAP = { 'CATEGORY': Category, 'MERCHANT': Merchant, 'COST_CENTER': CostCenter, + 'PROJECT': Project, }