diff --git a/rattail_corepos/corepos/importing/db/exporters/catapult_inventory.py b/rattail_corepos/corepos/importing/db/exporters/catapult_inventory.py index c2d39fc..932d90c 100644 --- a/rattail_corepos/corepos/importing/db/exporters/catapult_inventory.py +++ b/rattail_corepos/corepos/importing/db/exporters/catapult_inventory.py @@ -29,6 +29,7 @@ import logging from sqlalchemy.exc import ProgrammingError from sqlalchemy import orm +from sqlalchemy.orm.exc import NoResultFound from corepos import enum as corepos_enum from corepos.db.office_op import model as corepos @@ -172,6 +173,29 @@ class InventoryItemImporter(FromCore, catapult_importing.model.InventoryItemImpo # tax_component_ids_1 = (1,) # tax_component_ids_2 = (2,) + self.deposits = {} + deposits = self.host_session.query(corepos.Product.deposit.distinct())\ + .all() + + def cache(deposit, i): + assert isinstance(deposit, tuple) + assert len(deposit) == 1 + deposit = deposit[0] + if deposit: + deposit = int(deposit) + upc = "{:013d}".format(deposit) + try: + product = self.host_session.query(corepos.Product)\ + .filter(corepos.Product.upc == upc)\ + .one() + except NoResultFound: + pass # we will log warnings per-item later + else: + self.deposits[deposit] = product + + self.progress_loop(cache, deposits, + message="Caching product deposits data") + def query(self): query = self.host_session.query(corepos.Product)\ .order_by(corepos.Product.upc)\ @@ -247,6 +271,15 @@ class InventoryItemImporter(FromCore, catapult_importing.model.InventoryItemImpo product.upc, diff, product) price_divider = product.quantity + bottle_deposit = None + if product.deposit: + deposit = int(product.deposit) + if deposit in self.deposits: + bottle_deposit = self.deposits[deposit].normal_price + else: + log.warning("product %s has unknown deposit %s which will be ignored: %s", + product.upc, deposit, product) + sold_by_ea_or_lb = None if is_plu: sold_by_ea_or_lb = 'LB' if product.scale else 'EA' @@ -420,7 +453,7 @@ class InventoryItemImporter(FromCore, catapult_importing.model.InventoryItemImpo # TODO: does CORE have these? # 'disc_mult': None, - 'bottle_deposit': product.deposit or None, + 'bottle_deposit': bottle_deposit, # TODO: does CORE have this? # 'pos_menu_group': None,