From b8389c72bbe6764c48b52bc6d0d4d1c8d7750716 Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Wed, 11 Jan 2023 10:29:36 -0600 Subject: [PATCH] Add support for per-item default discount, for new custorder --- tailbone/forms/core.py | 2 ++ tailbone/templates/custorders/configure.mako | 23 ++++++++++++ tailbone/templates/custorders/create.mako | 37 ++++++++++++++++---- tailbone/views/custorders/orders.py | 18 ++++++++-- 4 files changed, 72 insertions(+), 8 deletions(-) diff --git a/tailbone/forms/core.py b/tailbone/forms/core.py index 99e6ba2d..4f227838 100644 --- a/tailbone/forms/core.py +++ b/tailbone/forms/core.py @@ -891,6 +891,8 @@ class Form(object): return json.dumps({'name': value['filename']}) return 'null' + app = self.request.rattail_config.get_app() + value = app.json_friendly(value) return json.dumps(value) def get_error_messages(self, field): diff --git a/tailbone/templates/custorders/configure.mako b/tailbone/templates/custorders/configure.mako index 6d51e433..ee1f06c5 100644 --- a/tailbone/templates/custorders/configure.mako +++ b/tailbone/templates/custorders/configure.mako @@ -97,6 +97,29 @@ + + + Allow discount even if item is on sale + + + +
+
Default item discount
+
+ + +
+
%
+
+ - + @@ -811,9 +812,10 @@
- - + +
 % @@ -1238,7 +1240,8 @@ % endif % if allow_item_discounts: - productDiscountPercent: null, + productDiscountPercent: ${json.dumps(default_item_discount)|n}, + allowDiscountsIfOnSale: ${json.dumps(allow_item_discounts_if_on_sale)|n}, % endif pendingProduct: {}, @@ -1421,6 +1424,19 @@ return text }, + % if allow_item_discounts: + + allowItemDiscount() { + if (!this.allowDiscountsIfOnSale) { + if (this.productSalePriceDisplay) { + return false + } + } + return true + }, + + % endif + itemDialogSaveDisabled() { if (this.itemDialogSaving) { return true @@ -1912,7 +1928,7 @@ % endif % if allow_item_discounts: - this.productDiscountPercent = null + this.productDiscountPercent = ${json.dumps(default_item_discount)|n} % endif this.itemDialogTabIndex = 0 @@ -2060,6 +2076,10 @@ this.productImageURL = null this.productUnitChoices = this.defaultUnitChoices + % if allow_item_discounts: + this.productDiscountPercent = ${json.dumps(default_item_discount)|n} + % endif + % if product_price_may_be_questionable: this.productPriceNeedsConfirmation = false % endif @@ -2106,6 +2126,11 @@ this.productSalePrice = response.data.sale_price this.productSalePriceDisplay = response.data.sale_price_display this.productSaleEndsDisplay = response.data.sale_ends_display + + % if allow_item_discounts: + this.productDiscountPercent = this.allowItemDiscount ? response.data.default_item_discount : null + % endif + this.productURL = response.data.url this.productImageURL = response.data.image_url this.setProductUnitChoices(response.data.uom_choices) diff --git a/tailbone/views/custorders/orders.py b/tailbone/views/custorders/orders.py index 7d97b47f..8bc53a67 100644 --- a/tailbone/views/custorders/orders.py +++ b/tailbone/views/custorders/orders.py @@ -2,7 +2,7 @@ ################################################################################ # # Rattail -- Retail Software Framework -# Copyright © 2010-2022 Lance Edgar +# Copyright © 2010-2023 Lance Edgar # # This file is part of Rattail. # @@ -293,6 +293,7 @@ class CustomerOrderView(MasterView): submits the order, at which point the batch is converted to a proper order. """ + app = self.get_rattail_app() # TODO: deprecate / remove this self.handler = self.batch_handler batch = self.get_current_batch() @@ -349,6 +350,10 @@ class CustomerOrderView(MasterView): 'default_uom_choices': self.batch_handler.uom_choices_for_product(None), 'default_uom': None, 'allow_item_discounts': self.batch_handler.allow_item_discounts(), + 'allow_item_discounts_if_on_sale': self.batch_handler.allow_item_discounts_if_on_sale(), + # nb. render quantity so that '10.0' => '10' + 'default_item_discount': app.render_quantity( + self.batch_handler.get_default_item_discount()), 'allow_past_item_reorder': self.batch_handler.allow_past_item_reorder(), }) @@ -633,9 +638,11 @@ class CustomerOrderView(MasterView): return {'error': six.text_type(error)} else: info['url'] = self.request.route_url('products.view', uuid=info['uuid']) - return info + app = self.get_rattail_app() + return app.json_friendly(info) def get_past_items(self, batch, data): + app = self.get_rattail_app() past_products = self.batch_handler.get_past_products(batch) past_items = [] @@ -646,6 +653,7 @@ class CustomerOrderView(MasterView): # nb. handler may raise error if product is "unsupported" pass else: + item = app.json_friendly(item) past_items.append(item) return {'past_items': past_items} @@ -987,6 +995,12 @@ class CustomerOrderView(MasterView): {'section': 'rattail.custorders', 'option': 'allow_item_discounts', 'type': bool}, + {'section': 'rattail.custorders', + 'option': 'allow_item_discounts_if_on_sale', + 'type': bool}, + {'section': 'rattail.custorders', + 'option': 'default_item_discount', + 'type': float}, {'section': 'rattail.custorders', 'option': 'allow_past_item_reorder', 'type': bool},