From 3aa7f503e64e400d41814e16e0abfb3defb9e556 Mon Sep 17 00:00:00 2001 From: Florian Lentsch Date: Fri, 1 Mar 2024 13:37:20 +0100 Subject: [PATCH] Fixes #52 --- app/assets/javascripts/group-order-form.js | 16 +++++++++------- app/helpers/group_orders_helper.rb | 8 ++++---- app/models/order_article.rb | 10 +++++++--- app/views/group_orders/_form.html.haml | 2 +- 4 files changed, 21 insertions(+), 15 deletions(-) diff --git a/app/assets/javascripts/group-order-form.js b/app/assets/javascripts/group-order-form.js index 378fb435..8e9c6dcd 100644 --- a/app/assets/javascripts/group-order-form.js +++ b/app/assets/javascripts/group-order-form.js @@ -181,17 +181,19 @@ class GroupOrderForm { totalPrice$.data('price', round(totalPrice)); missing$.text(round(missing)); - if (packSize > 1 || minimumOrderQuantity > 1) { - this.setRowStyle(row$, missing, granularity); + if (tolerance$.length === 1) { + this.setRowStyle(row$, missing, granularity, quantity); } } - setRowStyle(row$, missing, packSize) { + setRowStyle(row$, missing, granularity, quantity) { row$.removeClass('missing-many missing-few missing-none'); if (missing === 0) { - row$.addClass('missing-none'); + if (quantity !== 0) { + row$.addClass('missing-none'); + } } else { - row$.addClass(missing <= packSize ? 'missing-few' : 'missing-many'); + row$.addClass(missing <= granularity ? 'missing-few' : 'missing-many'); } } @@ -206,8 +208,8 @@ class GroupOrderForm { } calcMissingItems(packSize, quantity, tolerance, minimumOrderQuantity) { - if (Big(quantity).add(tolerance).toNumber() < minimumOrderQuantity) { - return Big(minimumOrderQuantity).sub(quantity).sub(tolerance); + if (quantity !== 0 && Big(quantity).add(tolerance).toNumber() < minimumOrderQuantity) { + return Big(minimumOrderQuantity).sub(quantity).sub(tolerance).toNumber(); } if (isNaN(quantity)) { diff --git a/app/helpers/group_orders_helper.rb b/app/helpers/group_orders_helper.rb index 2b2e9d6a..7d416b6e 100644 --- a/app/helpers/group_orders_helper.rb +++ b/app/helpers/group_orders_helper.rb @@ -51,11 +51,11 @@ def requires_tolerance_input?(order_article, ordering_data) ) || (order_article.article_version.minimum_order_quantity.presence || 0) > order_article.article_version.group_order_granularity end - def get_missing_units_css_class(quantity_missing) - if (quantity_missing == 1) - return 'missing-few'; - elsif (quantity_missing == 0) + def get_missing_units_css_class(quantity_missing, article_version) + if (quantity_missing == 0) return '' + elsif (quantity_missing <= article_version.group_order_granularity) + return 'missing-few'; else return 'missing-many' end diff --git a/app/models/order_article.rb b/app/models/order_article.rb index 936d9bb2..c96a71cd 100644 --- a/app/models/order_article.rb +++ b/app/models/order_article.rb @@ -180,12 +180,12 @@ def update_global_price=(value) # @return [Number] Units missing for the last +unit_quantity+ of the article. def missing_units unit_ratio = price.convert_quantity(1, price.supplier_order_unit, price.group_order_unit) - _missing_units(unit_ratio, quantity, tolerance) + _missing_units(unit_ratio, quantity, tolerance, price.minimum_order_quantity) end def missing_units_was unit_ratio = price.convert_quantity(1, price.supplier_order_unit, price.group_order_unit) - _missing_units(unit_ratio, quantity_was, tolerance_was) + _missing_units(unit_ratio, quantity_was, tolerance_was, price.minimum_order_quantity) end # Check if the result of any associated GroupOrderArticle was overridden manually @@ -264,7 +264,11 @@ def enforce_boxfill end end - def _missing_units(unit_ratio, quantity, tolerance) + def _missing_units(unit_ratio, quantity, tolerance, minimum_order_quantity) + if !minimum_order_quantity.nil? && quantity > 0 && quantity + tolerance < minimum_order_quantity + return minimum_order_quantity - quantity - tolerance + end + return 0 if article_version.supplier_order_unit_is_si_convertible units = unit_ratio - ((quantity % unit_ratio) + tolerance) diff --git a/app/views/group_orders/_form.html.haml b/app/views/group_orders/_form.html.haml index 7ca9d153..d937a8f6 100644 --- a/app/views/group_orders/_form.html.haml +++ b/app/views/group_orders/_form.html.haml @@ -96,7 +96,7 @@ %i.icon-tag %td{colspan: "9"} - order_articles.each do |order_article| - %tr{class: "#{cycle('even', 'odd', name: 'articles')} order-article #{get_missing_units_css_class(@ordering_data[:order_articles][order_article.id][:missing_units])}", valign: "top"} + %tr{class: "#{cycle('even', 'odd', name: 'articles')} order-article #{get_missing_units_css_class(@ordering_data[:order_articles][order_article.id][:missing_units], order_article.article_version)}", valign: "top"} %td.name= order_article.article_version.name - if @order.stockit? %td= truncate order_article.article_version.article.supplier.name, length: 15