From 331828e9ced1446c87aac4c13c47422e70c11dca 2021-11-06 20:31:02 From: Lance Edgar Date: 2021-11-06 20:31:02 Subject: [PATCH] Add support for finding past items, for new custorder also misc. tweaks for new custorder feature --- diff --git a/rattail/app.py b/rattail/app.py index cc916cb00e4597d7136a7291ff33d65d11261b9a..c8a372451d4baeea17700408a1eead1705158645 100644 --- a/rattail/app.py +++ b/rattail/app.py @@ -119,6 +119,10 @@ class AppHandler(object): self.auth_handler = factory(self.config, **kwargs) return self.auth_handler + def get_batch_handler(self, key, **kwargs): + from rattail.batch import get_batch_handler + return get_batch_handler(self.config, key, **kwargs) + def get_board_handler(self, **kwargs): if not hasattr(self, 'board_handler'): from rattail.board import get_board_handler diff --git a/rattail/batch/custorder.py b/rattail/batch/custorder.py index 22347256647032a4438e0c02683fa94c760af7e0..05b544410807fe8c0ce258778ef6ef75b4b9fc81 100644 --- a/rattail/batch/custorder.py +++ b/rattail/batch/custorder.py @@ -35,6 +35,7 @@ from sqlalchemy import orm from rattail.db import model from rattail.batch import BatchHandler +from rattail.util import OrderedDict class CustomerOrderBatchHandler(BatchHandler): @@ -410,14 +411,43 @@ class CustomerOrderBatchHandler(BatchHandler): "{}.custom_product_autocomplete() " "method.".format(__class__.__name__)) + def get_past_orders(self, batch, **kwargs): + """ + Retrieve a list of past orders for the batch contact. + """ + session = self.app.get_session(batch) + model = self.model + orders = session.query(model.CustomerOrder) + + contact = self.get_contact(batch) + if isinstance(contact, model.Customer): + orders = orders.filter(model.CustomerOrder.customer == contact) + else: + orders = orders.filter(model.CustomerOrder.person == contact) + + orders = orders.order_by(model.CustomerOrder.created.desc()) + return orders.all() + def get_past_products(self, batch, **kwargs): """ Should return a (possibly empty) list of products which have been ordered in the past by the customer who is associated with the given batch. """ - # TODO: should crawl the rattail order history here - return [] + session = self.app.get_session(batch) + model = self.model + products = OrderedDict() + + # track down all order items for batch contact + orders = self.get_past_orders(batch) + for order in orders: + for item in order.items: + product = item.product + if product: + # we only want the first match for each product + products.setdefault(product.uuid, product) + + return list(products.values()) def get_product_info(self, batch, product, **kwargs): """ @@ -438,6 +468,7 @@ class CustomerOrderBatchHandler(BatchHandler): 'unit_price_display': products.render_price(product.regular_price), 'department_name': product.department.name if product.department else None, 'vendor_name': vendor.name if vendor else None, + 'url': products.get_url(product), 'image_url': products.get_image_url(product), 'uom_choices': self.uom_choices_for_product(product), } @@ -445,7 +476,7 @@ class CustomerOrderBatchHandler(BatchHandler): case_price = None if product.regular_price and product.regular_price is not None: case_size = self.get_case_size_for_product(product) - case_price = case_size * product.regular_price.price + case_price = (case_size or 1) * product.regular_price.price case_price = case_price.quantize(decimal.Decimal('0.01')) info['case_price'] = six.text_type(case_price) if case_price is not None else None info['case_price_display'] = self.app.render_currency(case_price) diff --git a/rattail/products.py b/rattail/products.py index 5c3de3ed660b56e9d2ef871f8dab5ccc024c8ba6..2f0683b8815bfba0af112a8fe1f54213df47f11e 100644 --- a/rattail/products.py +++ b/rattail/products.py @@ -180,6 +180,14 @@ class ProductsHandler(GenericHandler): return products + def get_url(self, product, **kwargs): + """ + Return the Tailbone "view" URL for the given product. + """ + base_url = self.config.base_url() + if base_url: + return '{}/products/{}'.format(base_url, product.uuid) + def get_image_url(self, product=None, upc=None, **kwargs): """ Return the preferred image URL for the given UPC or product.