From cde1f1d76af2b694e438d0fb372bea108d0c9420 Mon Sep 17 00:00:00 2001 From: Florian Lentsch Date: Sun, 5 Jun 2022 15:22:22 +0200 Subject: [PATCH] Minimum order quantity fixes --- app/assets/javascripts/group-order-form.js | 46 +++++++++++++++------- app/models/group_order.rb | 3 +- app/models/group_order_article.rb | 2 +- app/models/order_article.rb | 5 +++ app/views/group_orders/_form.html.haml | 2 +- 5 files changed, 41 insertions(+), 17 deletions(-) diff --git a/app/assets/javascripts/group-order-form.js b/app/assets/javascripts/group-order-form.js index 1798c867..5ad44e18 100644 --- a/app/assets/javascripts/group-order-form.js +++ b/app/assets/javascripts/group-order-form.js @@ -83,6 +83,10 @@ class GroupOrderForm { } let value = parseFloat(field$.val()); + if (isNaN(value)) { + value = 0; + } + value += step; let remainder = value % step; if (remainder !== 0) { @@ -120,9 +124,15 @@ class GroupOrderForm { const missing$ = row$.find('.missing-units'); - const quantity = parseFloat(quantity$.val().trim().replace(',', '.')); + let quantity = parseFloat(quantity$.val().trim().replace(',', '.')); + if (isNaN(quantity)) { + quantity = 0; + } const granularity = parseFloat(quantity$.attr('step')); - const tolerance = tolerance$.length === 1 ? parseFloat(tolerance$.val().trim().replace(',', '.')) : 0; + let tolerance = tolerance$.length === 1 ? parseFloat(tolerance$.val().trim().replace(',', '.')) : 0; + if (isNaN(tolerance)) { + tolerance = 0; + } const packSize = quantity$.data('ratio-group-order-unit-supplier-unit'); const othersQuantity = quantity$.data('others-quantity'); const othersTolerance = quantity$.data('others-tolerance'); @@ -133,14 +143,14 @@ class GroupOrderForm { const totalQuantity = quantity + othersQuantity; const totalTolerance = tolerance + othersTolerance; - const totalPacks = this.calculatePacks(packSize, totalQuantity, totalTolerance) + const totalPacks = this.calculatePacks(packSize, totalQuantity, totalTolerance, minimumOrderQuantity) const totalPrice = price * (quantity + (this.toleranceIsCostly ? tolerance : 0)); // update used/unused quantity const available = Math.max(0, totalPacks * packSize - othersQuantity); let used = Math.min(available, quantity); - // ensure that at least the amout of items this group has already been allocated is used + // ensure that at least the amount of items this group has already been allocated is used if (quantity >= usedQuantity && used < usedQuantity) { used = usedQuantity; } @@ -151,15 +161,15 @@ class GroupOrderForm { const usedTolerance = Math.min(availableForTolerance, tolerance); const unusedTolerance = tolerance - usedTolerance; - const missing = this.calcMissingItems(packSize, totalQuantity, totalTolerance) + const missing = this.calcMissingItems(packSize, totalQuantity, totalTolerance, minimumOrderQuantity); - used$.text(isNaN(used) ? '?' : used); - unused$.text(isNaN(unused) ? '?' : round(unused, 2)); + used$.text(used); + unused$.text(unused); - usedTolerance$.text(isNaN(usedTolerance) ? '?' : usedTolerance); - unusedTolerance$.text(isNaN(unusedTolerance) ? '?' : unusedTolerance); + usedTolerance$.text(usedTolerance); + unusedTolerance$.text(unusedTolerance); - totalPacks$.text(totalPacks); + totalPacks$.text(isNaN(totalPacks) ? '?' : totalPacks); totalPacks$.css('color', this.packCompletedFromTolerance(packSize, totalQuantity, totalTolerance) ? 'grey' : 'auto'); @@ -168,8 +178,8 @@ class GroupOrderForm { totalPrice$.text(I18n.l('currency', totalPrice)); totalPrice$.data('price', totalPrice); - missing$.text(round(missing, 2)); - if (packSize > 1) { + missing$.text(missing); + if (packSize > 1 || minimumOrderQuantity > 1) { this.setRowStyle(row$, missing, granularity); } } @@ -183,13 +193,21 @@ class GroupOrderForm { } } - calculatePacks(packSize, quantity, tolerance) { + calculatePacks(packSize, quantity, tolerance, minimumOrderQuantity) { + if (quantity < minimumOrderQuantity) { + return 0; + } + const used = Math.floor(quantity / packSize) const remainder = quantity % packSize return used + ((remainder > 0) && (remainder + tolerance >= packSize) ? 1 : 0) } - calcMissingItems(packSize, quantity, tolerance) { + calcMissingItems(packSize, quantity, tolerance, minimumOrderQuantity) { + if (quantity < minimumOrderQuantity) { + return minimumOrderQuantity - quantity; + } + var remainder = quantity % packSize if (isNaN(remainder)) { return remainder; diff --git a/app/models/group_order.rb b/app/models/group_order.rb index 8c5f43af..dad9f177 100644 --- a/app/models/group_order.rb +++ b/app/models/group_order.rb @@ -55,7 +55,8 @@ def load_data :total_price => (goa ? goa.total_price : 0), :missing_units => order_article.missing_units, :ratio_group_order_unit_supplier_unit => order_article.article.convert_quantity(1, order_article.article.supplier_order_unit, order_article.article.group_order_unit), - :quantity_available => (order.stockit? ? order_article.article.quantity_available : 0) + :quantity_available => (order.stockit? ? order_article.article.quantity_available : 0), + :minimum_order_quantity => order_article.price.minimum_order_quantity ? order_article.price.convert_quantity(order_article.price.minimum_order_quantity, order_article.article.supplier_order_unit, order_article.article.group_order_unit) : nil } end end diff --git a/app/models/group_order_article.rb b/app/models/group_order_article.rb index 8df4fa59..ae66ccba 100644 --- a/app/models/group_order_article.rb +++ b/app/models/group_order_article.rb @@ -125,7 +125,7 @@ def calculate_result(total = nil) total = order_article.article.quantity logger.debug "<#{order_article.article.name}> (stock) => #{total}" else - total = order_article.units_to_order * order_article.price.unit_quantity + total = order_article.price.convert_quantity(order_article.units_to_order, order_article.article.supplier_order_unit, order_article.article.group_order_unit) logger.debug "<#{order_article.article.name}> units_to_order #{order_article.units_to_order} => #{total}" end diff --git a/app/models/order_article.rb b/app/models/order_article.rb index f2c26330..851a311f 100644 --- a/app/models/order_article.rb +++ b/app/models/order_article.rb @@ -81,7 +81,12 @@ def update_results! # 4 | 5 | 4 | 2 # def calculate_units_to_order(quantity, tolerance = 0) + minimum_order_quantity = price.minimum_order_quantity.nil? ? nil : price.convert_quantity(price.minimum_order_quantity, price.supplier_order_unit, price.group_order_unit) unit_size = price.convert_quantity(1, price.supplier_order_unit, price.group_order_unit) + if quantity > 0 && !price.minimum_order_quantity.nil? && quantity < price.minimum_order_quantity && quantity + tolerance >= minimum_order_quantity + return price.minimum_order_quantity / unit_size + end + units = quantity / unit_size remainder = quantity % unit_size units += ((remainder > 0) && (remainder + tolerance >= unit_size) ? 1 : 0) diff --git a/app/views/group_orders/_form.html.haml b/app/views/group_orders/_form.html.haml index 8062ac33..45573431 100644 --- a/app/views/group_orders/_form.html.haml +++ b/app/views/group_orders/_form.html.haml @@ -121,7 +121,7 @@ - quantity_data['others_tolerance'] = @ordering_data[:order_articles][order_article.id][:others_tolerance] - quantity_data['used_quantity'] = @ordering_data[:order_articles][order_article.id][:used_quantity] - quantity_data['price'] = @ordering_data[:order_articles][order_article.id][:price] - - quantity_data['minimum_order_quantity'] = order_article.price.minimum_order_quantity unless order_article.price.minimum_order_quantity.nil? + - quantity_data['minimum_order_quantity'] = @ordering_data[:order_articles][order_article.id][:minimum_order_quantity] unless @ordering_data[:order_articles][order_article.id][:minimum_order_quantity].nil? %td.quantity.group-order-input %span.used= @ordering_data[:order_articles][order_article.id][:used_quantity] +