diff --git a/rattail_corepos/corepos/importing/db/exporters/catapult_inventory.py b/rattail_corepos/corepos/importing/db/exporters/catapult_inventory.py index c9723e0..5d73f68 100644 --- a/rattail_corepos/corepos/importing/db/exporters/catapult_inventory.py +++ b/rattail_corepos/corepos/importing/db/exporters/catapult_inventory.py @@ -125,6 +125,9 @@ class InventoryItemImporter(FromCore, catapult_importing.model.InventoryItemImpo # we want to add a "duplicate" column at the end include_duplicate_column = True + # we want to add an "alternate for" column at the end + include_alt_for_column = True + type2_upc_pattern = re.compile(r'^2(\d{5})00000\d') def setup(self): @@ -212,6 +215,10 @@ class InventoryItemImporter(FromCore, catapult_importing.model.InventoryItemImpo # tax_component_ids_1 = (1,) # tax_component_ids_2 = (2,) + self.cache_bottle_deposits() + self.cache_like_codes() + + def cache_bottle_deposits(self): self.deposits = {} deposits = self.host_session.query(corepos.Product.deposit.distinct())\ .all() @@ -235,6 +242,19 @@ class InventoryItemImporter(FromCore, catapult_importing.model.InventoryItemImpo self.progress_loop(cache, deposits, message="Caching product deposits data") + def cache_like_codes(self): + self.like_codes = {} + mappings = self.host_session.query(corepos.ProductLikeCode)\ + .order_by(corepos.ProductLikeCode.like_code_id, + corepos.ProductLikeCode.upc)\ + .all() + + def cache(mapping, i): + self.like_codes.setdefault(mapping.like_code_id, []).append(mapping) + + self.progress_loop(cache, mappings, + message="Caching like codes data") + def query(self): query = self.host_session.query(corepos.Product)\ .order_by(corepos.Product.upc)\ @@ -245,7 +265,8 @@ class InventoryItemImporter(FromCore, catapult_importing.model.InventoryItemImpo .options(orm.joinedload(corepos.Product.default_vendor))\ .options(orm.joinedload(corepos.Product.scale_item))\ .options(orm.joinedload(corepos.Product.user_info))\ - .options(orm.joinedload(corepos.Product.tax_rate)) + .options(orm.joinedload(corepos.Product.tax_rate))\ + .options(orm.joinedload(corepos.Product._like_code)) if self.floor_sections_exist: query = query.options(orm.joinedload(corepos.Product.physical_location)\ .joinedload(corepos.ProductPhysicalLocation.floor_section)) @@ -289,6 +310,33 @@ class InventoryItemImporter(FromCore, catapult_importing.model.InventoryItemImpo self.progress_loop(inspect, list(items.values()), message="Marking any duplicate Item IDs") + + # finally, we must inspect the like codes and figure out which + # product(s) should potentially be considered "alternate for" another. + # first step here will be to create mapping of item_id values for each + # CORE product in our result set + item_ids = {} + + def mapp(row, i): + product = row['__product__'] + item_ids[product.upc] = row['item_id'] + + self.progress_loop(mapp, normalized, + message="Mapping item_id for CORE products") + + # next step here is to check each product and mark "alt for" as needed + def inspect(row, i): + product = row['__product__'] + if product.like_code: + others = self.like_codes.get(product.like_code.id) + if others: + first = others[0] + if first.upc != product.upc: + row['__alternate_for__'] = item_ids[first.upc] + + self.progress_loop(inspect, normalized, + message="Marking any \"alternate for\" items") + return normalized def normalize_host_object(self, product): @@ -545,6 +593,7 @@ class InventoryItemImporter(FromCore, catapult_importing.model.InventoryItemImpo scale_ingredient_text = scale_ingredient_text.replace("\n", " ") return { + '__product__': product, 'uuid': get_uuid(), 'item_id': item_id, 'dept_id': department.number if department else None,