diff --git a/rattail_corepos/corepos/importing/db/exporters/catapult_inventory.py b/rattail_corepos/corepos/importing/db/exporters/catapult_inventory.py index 5f4983b..8372e4e 100644 --- a/rattail_corepos/corepos/importing/db/exporters/catapult_inventory.py +++ b/rattail_corepos/corepos/importing/db/exporters/catapult_inventory.py @@ -92,10 +92,10 @@ class InventoryItemImporter(FromCore, catapult_importing.model.InventoryItemImpo 'alt_pkg_qty', 'alt_pkg_price', # 'auto_discount', - # 'supplier_unit_id', - # 'supplier_id', - # 'unit', - # 'num_pkgs', + 'supplier_unit_id', + 'supplier_id', + 'unit', + 'num_pkgs', # 'cs_pk_multiplier', # 'dsd', 'pf1', @@ -126,11 +126,22 @@ class InventoryItemImporter(FromCore, catapult_importing.model.InventoryItemImpo 'corepos', 'exporting.catapult_inventory.warn_truncated_receipt_alias', default=True) + self.warn_multiple_vendor_items = self.config.getbool( + 'corepos', 'exporting.catapult_inventory.warn_multiple_vendor_items', + default=True) + + self.warn_no_valid_vendor_items = self.config.getbool( + 'corepos', 'exporting.catapult_inventory.warn_no_valid_vendor_items', + default=True) + def query(self): query = self.host_session.query(corepos.Product)\ .order_by(corepos.Product.upc)\ .options(orm.joinedload(corepos.Product.department))\ - .options(orm.joinedload(corepos.Product.subdepartment)) + .options(orm.joinedload(corepos.Product.subdepartment))\ + .options(orm.joinedload(corepos.Product.vendor_items)\ + .joinedload(corepos.VendorItem.vendor))\ + .options(orm.joinedload(corepos.Product.default_vendor)) return query def normalize_host_object(self, product): @@ -237,6 +248,60 @@ class InventoryItemImporter(FromCore, catapult_importing.model.InventoryItemImpo alt_pkg_qty = product.quantity alt_pkg_price = product.group_price + # no supplier info by default + supplier_unit_id = None + supplier_id = None + supplier_unit = None + supplier_num_pkgs = None + + # maybe add supplier info, for "default" `vendorItems` record. we'll + # have to get a little creative to figure out which is the default + vendor_items = [] + + # first we try to narrow down according to product's default vendor + if product.default_vendor: + vendor_items = [item for item in product.vendor_items + if item.vendor is product.default_vendor] + + # but if that didn't work, just use any "valid" vendorItems + if not vendor_items: + # valid in this context means, not missing vendor + vendor_items = [item for item in product.vendor_items + if item.vendor] + if not vendor_items and product.vendor_items: + logger = log.warning if self.warn_no_valid_vendor_items else log.debug + logger("product %s has %s vendorItems but each is missing (valid) vendor: %s", + product.upc, len(product.vendor_items), product) + + if vendor_items: + if len(vendor_items) > 1: + + # try to narrow down a bit further, based on valid 'units' amount + valid_items = [item for item in vendor_items + if item.units] + if valid_items: + vendor_items = valid_items + + # warn if we still have more than one "obvious" vendor item + if len(vendor_items) > 1: + logger = log.warning if self.warn_multiple_vendor_items else log.debug + logger("product %s has %s vendorItems to pick from: %s", + product.upc, len(vendor_items), product) + + # sort the list so most recently modified is first + vendor_items.sort(key=lambda item: item.modified, + reverse=True) + + # use the "first" vendor item available + item = vendor_items[0] + supplier_unit_id = item.sku + supplier_id = item.vendor.name + supplier_num_pkgs = item.units or 1 + if supplier_num_pkgs == 1: + supplier_unit = 'LB' if product.scale else 'EA' + else: + supplier_unit = 'CS' + return { 'item_id': item_id, 'dept_id': department.number, @@ -284,14 +349,13 @@ class InventoryItemImporter(FromCore, catapult_importing.model.InventoryItemImpo # TODO: does CORE have this? # 'auto_discount': None, - # TODO: pretty sure CORE has these, but i'm not sure where - # 'supplier_unit_id': None, - # 'supplier_id': None, - # 'unit': None, - # 'num_pkgs': None, - # 'cs_pk_multiplier': None, + 'supplier_unit_id': supplier_unit_id, + 'supplier_id': supplier_id, + 'unit': supplier_unit, + 'num_pkgs': supplier_num_pkgs, - # TODO: does CORE have this? + # TODO: does CORE have these? + # 'cs_pk_multiplier': None, # 'dsd': None, 'pf1': product.subdepartment.number if product.subdepartment else None,