Skip to content

Commit

Permalink
Another fix for #7
Browse files Browse the repository at this point in the history
  • Loading branch information
lentschi committed Dec 26, 2023
1 parent 240f2d2 commit 34c198f
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 2 deletions.
7 changes: 7 additions & 0 deletions app/helpers/group_orders_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ def get_order_results(order_article, group_order_id)
{ group_order_article: goa, quantity: quantity, tolerance: tolerance, result: result, sub_total: sub_total }
end

def requires_tolerance_input?(order_article, ordering_data)
(
!order_article.article_version.supplier_order_unit_is_si_convertible &&
ordering_data[:order_articles][order_article.id][:ratio_group_order_unit_supplier_unit] != order_article.article_version.group_order_granularity
) || (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';
Expand Down
7 changes: 7 additions & 0 deletions app/models/order_article.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ def calculate_units_to_order(quantity, tolerance = 0)
return price.minimum_order_quantity
end

if price.supplier_order_unit_is_si_convertible
return quantity
end

unit_size = price.convert_quantity(1, price.supplier_order_unit, price.group_order_unit)
units = (quantity / unit_size).floor
remainder = quantity % unit_size
Expand Down Expand Up @@ -258,7 +262,10 @@ def enforce_boxfill
end

def _missing_units(unit_ratio, quantity, tolerance)
return 0 if article_version.supplier_order_unit_is_si_convertible

units = unit_ratio - ((quantity % unit_ratio) + tolerance)

units = 0 if units < 0
units = 0 if units == unit_ratio
units
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 @@ -134,7 +134,7 @@
%i.icon-minus

%td.tolerance.group-order-input{style: ('display:none' if @order.stockit?)}
- if (@ordering_data[:order_articles][order_article.id][:ratio_group_order_unit_supplier_unit] != order_article.article_version.group_order_granularity || (order_article.article_version.minimum_order_quantity.presence || 0) > order_article.article_version.group_order_granularity)
- if (requires_tolerance_input?(order_article, @ordering_data))
%span.used= format_number(@ordering_data[:order_articles][order_article.id][:used_tolerance])
+
%span.unused= format_number(@ordering_data[:order_articles][order_article.id][:tolerance] - @ordering_data[:order_articles][order_article.id][:used_tolerance])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def up
change_table :order_articles do |t|
t.change :quantity, :decimal, precision: 8, scale: 3, null: false
t.change :tolerance, :decimal, precision: 8, scale: 3, null: false
t.change :units_to_order, :decimal, precision: 8, scale: 3, null: false
end

change_table :group_order_articles do |t|
Expand Down Expand Up @@ -88,6 +89,7 @@ def down
change_table :order_articles do |t|
t.change :quantity, :integer, null: false
t.change :tolerance, :integer, null: false
t.change :units_to_order, :integer, null: false
end

change_table :group_order_articles do |t|
Expand Down
2 changes: 1 addition & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@
t.integer "order_id", default: 0, null: false
t.decimal "quantity", precision: 8, scale: 3, default: "0.0", null: false
t.decimal "tolerance", precision: 8, scale: 3, default: "0.0", null: false
t.integer "units_to_order", default: 0, null: false
t.decimal "units_to_order", precision: 8, scale: 3, default: "0.0", null: false
t.integer "lock_version", default: 0, null: false
t.integer "article_version_id", null: false
t.decimal "units_billed", precision: 8, scale: 3
Expand Down

0 comments on commit 34c198f

Please sign in to comment.