Skip to content

Commit

Permalink
Fixes #99
Browse files Browse the repository at this point in the history
  • Loading branch information
lentschi committed Sep 1, 2024
1 parent 99bc201 commit 0fd983a
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/models/order_article.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ def update_results!
# 4 | 5 | 4 | 2
#
def calculate_units_to_order(quantity, tolerance = 0)
return 0 if !price.minimum_order_quantity.nil? && quantity + tolerance < price.minimum_order_quantity
return price.minimum_order_quantity if quantity > 0 && !price.minimum_order_quantity.nil? && quantity < price.minimum_order_quantity && quantity + tolerance >= price.minimum_order_quantity

unit_size = price.convert_quantity(1, price.supplier_order_unit, price.group_order_unit)
Expand Down
2 changes: 2 additions & 0 deletions spec/factories/article.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
order_number { nil }
unit_quantity { nil }
unit { nil }
minimum_order_quantity { nil }
supplier_order_unit { 'XPK' }
group_order_unit { 'XPK' }
billing_unit { 'XPK' }
Expand All @@ -23,6 +24,7 @@
order_number: evaluator.order_number,
unit_quantity: evaluator.unit_quantity,
unit: evaluator.unit,
minimum_order_quantity: evaluator.minimum_order_quantity,
supplier_order_unit: evaluator.supplier_order_unit,
group_order_unit: evaluator.group_order_unit,
billing_unit: evaluator.billing_unit,
Expand Down
71 changes: 71 additions & 0 deletions spec/models/order_article_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -213,4 +213,75 @@ def goa_reload
# end
end
end

describe 'minimum order quantity' do
let(:order) { create(:order, article_ids: [article.id], starts: 1.week.ago) }
let(:oa) { order.order_articles.first }
let(:go) { create(:group_order, order: order) }
let(:goa) { create(:group_order_article, group_order: go, order_article: oa) }

describe 'without unit_quantity' do
let(:article) { create(:article, minimum_order_quantity: 10) }

it 'doesn\'t order anything, if the minimum order quantity hasn\'t been reached' do
goa.update_quantities(4, 0)
oa.update_results!
oa.reload

expect(oa.units_to_order).to eq 0
end

it 'orders the minimum order quantity, if tolerance allows for it' do
goa.update_quantities(9, 1)
oa.update_results!
oa.reload

expect(oa.units_to_order).to eq 10
end

it 'orders the desired amount, if minimum order quantity has been surpassed' do
goa.update_quantities(12, 1)
oa.update_results!
oa.reload

expect(oa.units_to_order).to eq 12
end
end

describe 'with unit_quantity' do
let(:article) { create(:article, minimum_order_quantity: 10, unit_quantity: 5) }

it 'doesn\'t order anything if the minimum order quantity hasn\'t been reached' do
goa.update_quantities(4, 0)
oa.update_results!
oa.reload

expect(oa.units_to_order).to eq 0
end

it 'orders the minimum order quantity if tolerance allows for it' do
goa.update_quantities(9, 1)
oa.update_results!
oa.reload

expect(oa.units_to_order).to eq 2
end

it 'orders a lower amount, even if minimum order quantity has been surpassed, but unit_quantity demands it' do
goa.update_quantities(12, 1)
oa.update_results!
oa.reload

expect(oa.units_to_order).to eq 2
end

it 'orders the exact amount, if minimum order quantity has been surpassed and unit_quantity allows for it' do
goa.update_quantities(14, 1)
oa.update_results!
oa.reload

expect(oa.units_to_order).to eq 3
end
end
end
end

0 comments on commit 0fd983a

Please sign in to comment.