Skip to content

Commit

Permalink
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 ec53546 commit cde1f1d
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 17 deletions.
46 changes: 32 additions & 14 deletions app/assets/javascripts/group-order-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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');
Expand All @@ -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;
}
Expand All @@ -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');

Expand All @@ -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);
}
}
Expand All @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion app/models/group_order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/models/group_order_article.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 5 additions & 0 deletions app/models/order_article.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion app/views/group_orders/_form.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
+
Expand Down

0 comments on commit cde1f1d

Please sign in to comment.