Skip to content

Commit

Permalink
Some more minimum order quantity fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
lentschi committed Jun 5, 2022
1 parent cde1f1d commit 7b8f1b0
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
6 changes: 3 additions & 3 deletions app/assets/javascripts/group-order-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ class GroupOrderForm {

const unused = quantity - used;

const availableForTolerance = Math.max(0, available - used - othersTolerance);
const availableForTolerance = quantity < minimumOrderQuantity ? minimumOrderQuantity - quantity : Math.max(0, available - used - othersTolerance);
const usedTolerance = Math.min(availableForTolerance, tolerance);
const unusedTolerance = tolerance - usedTolerance;

Expand All @@ -169,7 +169,7 @@ class GroupOrderForm {
usedTolerance$.text(usedTolerance);
unusedTolerance$.text(unusedTolerance);

totalPacks$.text(isNaN(totalPacks) ? '?' : totalPacks);
totalPacks$.text(totalPacks);

totalPacks$.css('color', this.packCompletedFromTolerance(packSize, totalQuantity, totalTolerance) ? 'grey' : 'auto');

Expand Down Expand Up @@ -205,7 +205,7 @@ class GroupOrderForm {

calcMissingItems(packSize, quantity, tolerance, minimumOrderQuantity) {
if (quantity < minimumOrderQuantity) {
return minimumOrderQuantity - quantity;
return minimumOrderQuantity - quantity + tolerance;
}

var remainder = quantity % packSize
Expand Down
7 changes: 3 additions & 4 deletions app/models/order_article.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,11 @@ 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
if quantity > 0 && !price.minimum_order_quantity.nil? && quantity < price.minimum_order_quantity && quantity + tolerance >= price.minimum_order_quantity
return price.minimum_order_quantity
end

unit_size = price.convert_quantity(1, price.supplier_order_unit, price.group_order_unit)
units = quantity / unit_size
remainder = quantity % unit_size
units += ((remainder > 0) && (remainder + tolerance >= unit_size) ? 1 : 0)
Expand Down

0 comments on commit 7b8f1b0

Please sign in to comment.