2013-09-18 12:44:41 +02:00
|
|
|
require_relative '../spec_helper'
|
2013-07-15 21:01:46 +02:00
|
|
|
|
|
|
|
describe GroupOrderArticle do
|
2023-05-12 13:01:12 +02:00
|
|
|
let(:user) { create(:user, groups: [create(:ordergroup)]) }
|
2013-09-18 12:44:41 +02:00
|
|
|
let(:order) { create(:order) }
|
2023-05-12 13:01:12 +02:00
|
|
|
let(:go) { create(:group_order, order: order, ordergroup: user.ordergroup) }
|
|
|
|
let(:goa) { create(:group_order_article, group_order: go, order_article: order.order_articles.first) }
|
2013-07-15 21:01:46 +02:00
|
|
|
|
2013-07-24 22:46:25 +02:00
|
|
|
it 'has zero quantity by default' do expect(goa.quantity).to eq(0) end
|
|
|
|
it 'has zero tolerance by default' do expect(goa.tolerance).to eq(0) end
|
|
|
|
it 'has zero result by default' do expect(goa.result).to eq(0) end
|
|
|
|
it 'has zero total price by default' do expect(goa.total_price).to eq(0) end
|
2013-07-15 21:01:46 +02:00
|
|
|
|
|
|
|
describe do
|
2023-05-12 13:01:12 +02:00
|
|
|
let(:article) { create(:article, supplier: order.supplier, unit_quantity: 1) }
|
|
|
|
let(:oa) { order.order_articles.create(article: article) }
|
|
|
|
let(:goa) { create(:group_order_article, group_order: go, order_article: oa) }
|
2013-07-15 21:01:46 +02:00
|
|
|
|
|
|
|
it 'can be ordered by piece' do
|
|
|
|
goa.update_quantities(1, 0)
|
2013-07-24 22:46:25 +02:00
|
|
|
expect(goa.quantity).to eq(1)
|
|
|
|
expect(goa.tolerance).to eq(0)
|
2013-07-15 21:01:46 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'can be ordered in larger amounts' do
|
2023-05-12 13:01:12 +02:00
|
|
|
quantity = rand(13..99)
|
|
|
|
tolerance = rand(0..99)
|
2013-07-15 21:01:46 +02:00
|
|
|
goa.update_quantities(quantity, tolerance)
|
2013-07-24 22:46:25 +02:00
|
|
|
expect(goa.quantity).to eq(quantity)
|
|
|
|
expect(goa.tolerance).to eq(tolerance)
|
2013-07-15 21:01:46 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'has a proper total price' do
|
2013-07-25 13:21:24 +02:00
|
|
|
quantity = rand(1..99)
|
2013-07-15 21:01:46 +02:00
|
|
|
goa.update_quantities(quantity, 0)
|
2013-07-24 22:46:25 +02:00
|
|
|
expect(goa.total_price).to eq(quantity * goa.order_article.price.fc_price)
|
2013-07-15 21:01:46 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'can unorder a product' do
|
2013-07-25 13:21:24 +02:00
|
|
|
goa.update_quantities(rand(1..99), rand(0..99))
|
2013-07-15 21:01:46 +02:00
|
|
|
goa.update_quantities(0, 0)
|
2014-08-19 11:39:33 +02:00
|
|
|
expect(GroupOrderArticle.exists?(goa.id)).to be false
|
2013-07-15 21:01:46 +02:00
|
|
|
end
|
2022-10-31 16:36:06 +01:00
|
|
|
|
|
|
|
it 'updates quantity and tolerance' do
|
|
|
|
goa.update_quantities(2, 2)
|
|
|
|
goa.update_quantities(1, 1)
|
|
|
|
expect(goa.quantity).to eq(1)
|
|
|
|
expect(goa.tolerance).to eq(1)
|
|
|
|
goa.update_quantities(1, 2)
|
|
|
|
expect(goa.tolerance).to eq(2)
|
|
|
|
end
|
2013-07-15 21:01:46 +02:00
|
|
|
end
|
|
|
|
|
2021-02-05 16:19:05 +01:00
|
|
|
describe 'distribution strategy' do
|
2023-05-12 13:01:12 +02:00
|
|
|
let(:article) { create(:article, supplier: order.supplier, unit_quantity: 1) }
|
|
|
|
let(:oa) { order.order_articles.create(article: article) }
|
|
|
|
let(:goa) { create(:group_order_article, group_order: go, order_article: oa) }
|
|
|
|
let!(:goaq) { create(:group_order_article_quantity, group_order_article: goa, quantity: 4, tolerance: 6) }
|
2021-02-05 16:19:05 +01:00
|
|
|
|
|
|
|
it 'can calculate the result for the distribution strategy "first order first serve"' do
|
|
|
|
res = goa.calculate_result(2)
|
|
|
|
expect(res).to eq(quantity: 2, tolerance: 0, total: 2)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'can calculate the result for the distribution strategy "no automatic distribution"' do
|
|
|
|
FoodsoftConfig[:distribution_strategy] = FoodsoftConfig::DistributionStrategy::NO_AUTOMATIC_DISTRIBUTION
|
|
|
|
|
|
|
|
res = goa.calculate_result(2)
|
|
|
|
expect(res).to eq(quantity: 4, tolerance: 0, total: 4)
|
|
|
|
end
|
2022-10-31 16:36:06 +01:00
|
|
|
|
|
|
|
it 'determines tolerance correctly' do
|
|
|
|
res = goa.calculate_result(6)
|
|
|
|
expect(res).to eq(quantity: 4, tolerance: 2, total: 6)
|
|
|
|
end
|
2021-02-05 16:19:05 +01:00
|
|
|
end
|
2013-07-15 21:01:46 +02:00
|
|
|
end
|