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

Import Categories support added #40

Merged
merged 2 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
26 changes: 16 additions & 10 deletions apps/business_central/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,20 @@ def _sync_data(self, data, attribute_type, display_name, workspace_id, field_nam
"""

destination_attributes = []

for item in data:
detail = {field: getattr(item, field) for field in field_names}
detail = {field: item[field] for field in field_names}
if (attribute_type == 'EMPLOYEE' and item['status'] == 'Active') or attribute_type == 'LOCATION' or item['blocked'] != True:
active = True
else:
active = False
destination_attributes.append(self._create_destination_attribute(
attribute_type,
display_name,
item.name,
item.id,
item.is_active,
item['displayName'],
item['id'],
active,
detail
))

DestinationAttribute.bulk_create_or_update_destination_attributes(
destination_attributes, attribute_type, workspace_id, True)

Expand All @@ -89,9 +91,10 @@ def sync_accounts(self):
"""
workspace = Workspace.objects.get(id=self.workspace_id)
self.connection.company_id = workspace.business_central_company_id
field_names = ['category', 'subCategory', 'accountType', 'directPosting', 'lastModifiedDateTime']

accounts = self.connection.accounts.get_all()
self._sync_data(accounts, 'ACCOUNT', 'accounts', self.workspace_id)
self._sync_data(accounts, 'ACCOUNT', 'accounts', self.workspace_id, field_names)
return []

def sync_vendors(self):
Expand All @@ -100,9 +103,10 @@ def sync_vendors(self):
"""
workspace = Workspace.objects.get(id=self.workspace_id)
self.connection.company_id = workspace.business_central_company_id
field_names = ['email', 'currencyId', 'currencyCode', 'lastModifiedDateTime']

vendors = self.connection.vendors.get_all()
self._sync_data(vendors, 'VENDOR', 'vendor', self.workspace_id)
self._sync_data(vendors, 'VENDOR', 'vendor', self.workspace_id, field_names)
return []

def sync_employees(self):
Expand All @@ -111,9 +115,10 @@ def sync_employees(self):
"""
workspace = Workspace.objects.get(id=self.workspace_id)
self.connection.company_id = workspace.business_central_company_id
field_names = ['email', 'email', 'personalEmail', 'lastModifiedDateTime']

employees = self.connection.employees.get_all()
self._sync_data(employees, 'EMPLOYEE', 'employee', self.workspace_id)
self._sync_data(employees, 'EMPLOYEE', 'employee', self.workspace_id, field_names)
return []

def sync_locations(self):
Expand All @@ -122,7 +127,8 @@ def sync_locations(self):
"""
workspace = Workspace.objects.get(id=self.workspace_id)
self.connection.company_id = workspace.business_central_company_id
field_names = ['code', 'city', 'country']

locations = self.connection.locations.get_all()
self._sync_data(locations, 'LOCATION', 'location', self.workspace_id)
self._sync_data(locations, 'LOCATION', 'location', self.workspace_id, field_names)
return []
4 changes: 4 additions & 0 deletions apps/mappings/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@
class MappingsConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "apps.mappings"

def ready(self):
super(MappingsConfig, self).ready()
import apps.mappings.signals # noqa
85 changes: 85 additions & 0 deletions apps/mappings/imports/modules/categories.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
from datetime import datetime
from typing import List

from fyle_accounting_mappings.models import CategoryMapping, DestinationAttribute

from apps.mappings.imports.modules.base import Base


class Category(Base):
"""
Class for Category module
"""

def __init__(self, workspace_id: int, destination_field: str, sync_after: datetime):
super().__init__(
workspace_id=workspace_id,
source_field="CATEGORY",
destination_field=destination_field,
platform_class_name="categories",
sync_after=sync_after,
)

def trigger_import(self):
"""
Trigger import for Category 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 Category 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:
category = {
"name": attribute.value,
"code": attribute.destination_id,
"is_enabled": attribute.active,
}

# Create a new category if it does not exist in Fyle
if attribute.value.lower() not in existing_fyle_attributes_map:
payload.append(category)
# Disable the existing category in Fyle if auto-sync status is allowed and the destination_attributes is inactive
elif is_auto_sync_status_allowed and not attribute.active:
category['id'] = existing_fyle_attributes_map[attribute.value.lower()]
payload.append(category)

return payload

def create_mappings(self):
"""
Create mappings for Category module
"""
filters = {
"workspace_id": self.workspace_id,
"attribute_type": self.destination_field,
"destination_account__isnull": True
}

# get all the destination attributes that have category mappings as null
destination_attributes: List[
DestinationAttribute
] = DestinationAttribute.objects.filter(**filters)

destination_attributes_without_duplicates = []
destination_attributes_without_duplicates = self.remove_duplicate_attributes(
destination_attributes
)

CategoryMapping.bulk_create_mappings(
destination_attributes_without_duplicates,
self.destination_field,
self.workspace_id,
)
59 changes: 59 additions & 0 deletions apps/mappings/imports/modules/cost_centers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
from datetime import datetime
from typing import List

from fyle_accounting_mappings.models import DestinationAttribute

from apps.mappings.imports.modules.base import Base


class CostCenter(Base):
"""
Class for Cost Center module
"""

def __init__(self, workspace_id: int, destination_field: str, sync_after: datetime):
super().__init__(
workspace_id=workspace_id,
source_field="COST_CENTER",
destination_field=destination_field,
platform_class_name="cost_centers",
sync_after=sync_after,
)

def trigger_import(self):
"""
Trigger import for Cost Center 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 CostCenter 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:
cost_center = {
'name': attribute.value,
'code': attribute.destination_id,
'is_enabled': True if attribute.active is None else attribute.active,
'description': 'Cost Center - {0}, Id - {1}'.format(
attribute.value,
attribute.destination_id
)
}

# Create a new cost-center if it does not exist in Fyle
if attribute.value.lower() not in existing_fyle_attributes_map:
payload.append(cost_center)

return payload
Loading
Loading