Fix CORE API -> Rattail datasync for ProductCost

This commit is contained in:
Lance Edgar 2021-01-31 21:53:26 -06:00
parent f61a0a7dd9
commit 67618d4784
2 changed files with 35 additions and 11 deletions

View file

@ -108,6 +108,14 @@ class FromCOREAPIToRattail(NewDataSyncImportConsumer):
self.process_change(session, self.importers['Product'], self.process_change(session, self.importers['Product'],
host_object=product) host_object=product)
# sync all ProductCost-related changes
types = [
'VendorItem',
]
for change in [c for c in changes if c.payload_type in types]:
# normal logic works fine here
self.invoke_importer(session, change)
def get_host_object(self, session, change): def get_host_object(self, session, change):
if change.payload_type == 'Member': if change.payload_type == 'Member':
return self.api.get_member(change.payload_key) return self.api.get_member(change.payload_key)

View file

@ -648,10 +648,10 @@ class ProductCostImporter(FromCOREPOSAPI, corepos_importing.model.ProductCostImp
""" """
Importer for product cost data from CORE POS API. Importer for product cost data from CORE POS API.
""" """
key = 'corepos_id' key = ('vendor_uuid', 'code')
supported_fields = [ supported_fields = [
'corepos_id', 'corepos_id',
'product_upc', 'product_uuid',
'vendor_uuid', 'vendor_uuid',
'code', 'code',
'case_size', 'case_size',
@ -678,6 +678,9 @@ class ProductCostImporter(FromCOREPOSAPI, corepos_importing.model.ProductCostImp
self.progress_loop(cache, self.api.get_products(), self.progress_loop(cache, self.api.get_products(),
message="Caching Products from CORE-POS API") message="Caching Products from CORE-POS API")
self.products_by_item_id = self.cache_model(model.Product,
key='item_id')
def get_host_objects(self): def get_host_objects(self):
return self.api.get_vendor_items() return self.api.get_vendor_items()
@ -702,25 +705,38 @@ class ProductCostImporter(FromCOREPOSAPI, corepos_importing.model.ProductCostImp
return self.api.get_product(item['upc']) return self.api.get_product(item['upc'])
def normalize_host_object(self, item): def get_product(self, item):
try: item_id = item['upc']
upc = GPC(item['upc'], calc_check_digit='upc')
except (TypeError, ValueError):
log.warning("CORE POS vendor item has invalid UPC: %s", item['upc'])
return
if hasattr(self, 'products_by_item_id'):
return self.products_by_item_id.get(item_id)
model = self.model
try:
return self.session.query(model.Product)\
.filter(model.Product.item_id == item_id)\
.one()
except orm.exc.NoResultFound:
pass
def normalize_host_object(self, item):
vendor = self.get_vendor(item) vendor = self.get_vendor(item)
if not vendor: if not vendor:
log.warning("CORE POS vendor not found for item: %s", item) log.warning("CORE POS vendor not found for item: %s", item)
return return
product = self.get_corepos_product(item) product = self.get_product(item)
if not product:
log.warning("product not found for CORE vendor item: %s", item)
return
core_product = self.get_corepos_product(item)
# if not product: # if not product:
# log.warning("CORE POS product not found for item: %s", item) # log.warning("CORE POS product not found for item: %s", item)
# return # return
preferred = False preferred = False
if product and product['default_vendor_id'] == item['vendorID']: if core_product and core_product['default_vendor_id'] == item['vendorID']:
preferred = True preferred = True
case_size = decimal.Decimal(item['units']) case_size = decimal.Decimal(item['units'])
@ -728,7 +744,7 @@ class ProductCostImporter(FromCOREPOSAPI, corepos_importing.model.ProductCostImp
return { return {
'corepos_id': int(item['vendorItemID']), 'corepos_id': int(item['vendorItemID']),
'product_upc': upc, 'product_uuid': product.uuid,
'vendor_uuid': vendor.uuid, 'vendor_uuid': vendor.uuid,
'code': (item['sku'] or '').strip() or None, 'code': (item['sku'] or '').strip() or None,
'case_size': case_size, 'case_size': case_size,