Skip to content

Commit

Permalink
Fix vendor sync (#174)
Browse files Browse the repository at this point in the history
* Delete purchase_invoice, line_items on failed exports from hh2, type-check for current_state

* Added type in vendor, delete the credit card vendor

* remove print

* Added logger
  • Loading branch information
Hrishabh17 authored May 27, 2024
1 parent 40b7322 commit 312eb99
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 7 deletions.
1 change: 1 addition & 0 deletions apps/sage300/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class Sage300FieldSerializer(serializers.Serializer):
def format_sage300_fields(self, workspace_id):
attribute_types = [
'VENDOR',
'VENDOR_TYPE',
'ACCOUNT',
'JOB',
'CATEGORY',
Expand Down
44 changes: 38 additions & 6 deletions apps/sage300/utils.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import logging
from django.utils.module_loading import import_string
from fyle_accounting_mappings.models import DestinationAttribute
from apps.workspaces.models import Sage300Credential
from sage_desktop_sdk.sage_desktop_sdk import SageDesktopSDK
from apps.sage300.models import CostCategory
from apps.mappings.models import Version

logger = logging.getLogger(__name__)
logger.level = logging.INFO


class SageDesktopConnector:
"""
Expand Down Expand Up @@ -67,9 +71,14 @@ def _update_latest_version(self, attribute_type: str):

return []

def _add_to_destination_attributes(self, item, attribute_type, display_name, field_names):
def _add_to_destination_attributes(self, item, attribute_type, display_name, field_names, vendor_type_mapping = None):

detail = {field: getattr(item, field) for field in field_names}
if vendor_type_mapping:
if item.type_id in vendor_type_mapping:
detail['type'] = vendor_type_mapping[item.type_id]
else:
detail['type'] = None
if item.name:
return self._create_destination_attribute(
attribute_type,
Expand All @@ -94,12 +103,23 @@ def _get_attribute_class(self, attribute_type: str):
'STANDARD_CATEGORY': 'StandardCategory',
'COMMITMENT': 'Commitment',
'COMMITMENT_ITEM': 'CommitmentItem',
'COST_CODE': 'CostCode'
'COST_CODE': 'CostCode',
'VENDOR_TYPE': 'VendorType'
}

return ATTRIBUTE_CLASS_MAP[attribute_type]

def _sync_data(self, data_gen, attribute_type, display_name, workspace_id, field_names, is_generator: bool = True):
def _remove_credit_card_vendors(self):
credit_card_vendor = DestinationAttribute.objects.filter(
workspace_id=self.workspace_id,
attribute_type='VENDOR',
detail__type='Credit Card'
)
vendor_count = credit_card_vendor.count()
logger.info(f'Deleting {vendor_count} credit card vendors')
credit_card_vendor.delete()

def _sync_data(self, data_gen, attribute_type, display_name, workspace_id, field_names, is_generator: bool = True, vendor_type_mapping = None):
"""
Synchronize data from Sage Desktop SDK to your application
:param data: Data to synchronize
Expand All @@ -116,7 +136,7 @@ def _sync_data(self, data_gen, attribute_type, display_name, workspace_id, field
for _item in items:
attribute_class = self._get_attribute_class(attribute_type)
item = import_string(f'sage_desktop_sdk.core.schema.read_only.{attribute_class}').from_dict(_item)
destination_attr = self._add_to_destination_attributes(item, attribute_type, display_name, field_names)
destination_attr = self._add_to_destination_attributes(item, attribute_type, display_name, field_names, vendor_type_mapping)
if destination_attr:
destination_attributes.append(destination_attr)

Expand All @@ -132,7 +152,10 @@ def _sync_data(self, data_gen, attribute_type, display_name, workspace_id, field
DestinationAttribute.bulk_create_or_update_destination_attributes(
destination_attributes, attribute_type, workspace_id, True)

self._update_latest_version(attribute_type)
if attribute_type != 'VENDOR_TYPE':
self._update_latest_version(attribute_type)
if attribute_type == 'VENDOR':
self._remove_credit_card_vendors()

def sync_accounts(self):
"""
Expand All @@ -153,8 +176,17 @@ def sync_vendors(self):
'code', 'version', 'default_expense_account', 'default_standard_category',
'default_standard_costcode', 'type_id', 'created_on_utc'
]
vendor_types = None
vendor_type_mapping = None

if not DestinationAttribute.objects.filter(workspace_id=self.workspace_id, attribute_type='VENDOR_TYPE').exists():
vendor_types = self.connection.vendors.get_vendor_types()
self._sync_data(vendor_types, 'VENDOR_TYPE', 'vendor_type', self.workspace_id, ['version'])

vendor_types = DestinationAttribute.objects.filter(workspace_id=self.workspace_id, attribute_type='VENDOR_TYPE').values('destination_id', 'value').distinct()
vendor_type_mapping = {vendor_type['destination_id']: vendor_type['value'] for vendor_type in vendor_types}

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

def sync_jobs(self):
Expand Down
4 changes: 3 additions & 1 deletion sage_desktop_sdk/core/schema/read_only.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,15 @@ class VendorType:
id: str
version: int
name: str
is_active: bool

@classmethod
def from_dict(cls, vendor_type):
return cls(
id=vendor_type.get('Id'),
version=vendor_type.get('Version'),
name=vendor_type.get('Name')
name=vendor_type.get('Name'),
is_active=True
)


Expand Down

0 comments on commit 312eb99

Please sign in to comment.