From 5ac3a3d82ce27fea65fac26ea9f81ec2afc9426a Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Wed, 18 Oct 2023 21:28:17 -0500 Subject: [PATCH] Import product sale pricing from CORE this almost certainly needs improvements for POS sake --- rattail_corepos/importing/corepos/db.py | 62 +++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/rattail_corepos/importing/corepos/db.py b/rattail_corepos/importing/corepos/db.py index 2a1bc8e..67622db 100644 --- a/rattail_corepos/importing/corepos/db.py +++ b/rattail_corepos/importing/corepos/db.py @@ -541,11 +541,55 @@ class ProductImporter(FromCOREPOS, corepos_importing.model.ProductImporter): 'regular_price_price', 'regular_price_multiple', 'regular_price_type', + 'sale_price_price', + 'sale_price_starts', + 'sale_price_ends', + 'sale_price_current', 'food_stampable', # 'tax1', 'tax_code', ] + def setup(self): + super().setup() + + if self.fields_active(self.sale_price_fields): + self.core_batch_items = {} + + # TODO: it seems possible for CORE to have more than one + # batch item for a given product. sort order will + # determine which would "win" but not clear what sort + # order should be used, e.g. CORE does not seem to use one + today = self.app.today() + batches = self.host_session.query(corepos.Batch)\ + .filter(corepos.Batch.start_date <= today)\ + .filter(corepos.Batch.end_date >= today)\ + .filter(corepos.Batch.discount_type > 0)\ + .options(orm.joinedload(corepos.Batch.items))\ + .all() + + def cache(batch, i): + for item in batch.items: + self.core_batch_items.setdefault(item.upc, []).append(item) + + self.progress_loop(cache, batches, + message="Caching CORE-POS batch items") + + def get_core_batch_item(self, upc): + if hasattr(self, 'core_batch_items'): + items = self.core_batch_items.get(upc) + if not items: + return + + sale_price = items[0].sale_price + if any([item.sale_price != sale_price + for item in items[1:]]): + log.warning("ambiguous batch items for upc: %s", upc) + + return items[0] + + raise NotImplementedError("TODO: fetch batch items in real-time") + def normalize_host_object(self, product): try: @@ -577,6 +621,12 @@ class ProductImporter(FromCOREPOS, corepos_importing.model.ProductImporter): 'regular_price_price': price, 'regular_price_multiple': 1 if price is not None else None, 'regular_price_type': self.enum.PRICE_TYPE_REGULAR if price is not None else None, + + # nb. these may get set below + 'sale_price_price': None, + 'sale_price_starts': None, + 'sale_price_ends': None, + 'sale_price_current': False, } if 'tax_code' in self.fields: @@ -591,6 +641,18 @@ class ProductImporter(FromCOREPOS, corepos_importing.model.ProductImporter): 'uom_abbreviation': size_info['uom_abbrev'], }) + # sale price + # nb. CORE discount_type indicates if item is on sale "now" + if self.fields_active(self.sale_price_fields) and product.discount_type: + item = self.get_core_batch_item(product.upc) + if item: + data.update({ + 'sale_price_price': item.sale_price, + 'sale_price_starts': self.app.make_utc(self.app.localtime(item.batch.start_date)), + 'sale_price_ends': self.app.make_utc(self.app.localtime(item.batch.end_date)), + 'sale_price_current': True, + }) + return data