2013-09-18 12:44:41 +02:00
|
|
|
require_relative '../spec_helper'
|
2013-07-15 00:17:07 +02:00
|
|
|
|
|
|
|
describe Order do
|
2023-05-12 13:01:12 +02:00
|
|
|
let!(:ftt) { create(:financial_transaction_type) }
|
|
|
|
let(:user) { create(:user, groups: [create(:ordergroup)]) }
|
2013-07-15 00:17:07 +02:00
|
|
|
|
2017-10-12 00:20:30 +02:00
|
|
|
it 'automaticly finishes ended' do
|
2023-05-12 13:01:12 +02:00
|
|
|
create(:order, created_by: user, starts: Date.yesterday, ends: 1.hour.from_now)
|
|
|
|
create(:order, created_by: user, starts: Date.yesterday, ends: 1.hour.ago)
|
|
|
|
create(:order, created_by: user, starts: Date.yesterday, ends: 1.hour.from_now, end_action: :auto_close)
|
|
|
|
order = create(:order, created_by: user, starts: Date.yesterday, ends: 1.hour.ago, end_action: :auto_close)
|
2017-10-12 00:20:30 +02:00
|
|
|
|
|
|
|
Order.finish_ended!
|
|
|
|
order.reload
|
|
|
|
|
|
|
|
expect(Order.open.count).to eq 3
|
|
|
|
expect(Order.finished.count).to eq 1
|
|
|
|
expect(order).to be_finished
|
|
|
|
end
|
|
|
|
|
2021-02-05 14:15:50 +01:00
|
|
|
describe 'state scopes and boolean getters' do
|
2023-05-12 13:01:12 +02:00
|
|
|
let!(:open_order) { create(:order, state: 'open') }
|
|
|
|
let!(:finished_order) { create(:order, state: 'finished') }
|
|
|
|
let!(:received_order) { create(:order, state: 'received') }
|
|
|
|
let!(:closed_order) { create(:order, state: 'closed') }
|
2021-02-05 13:16:57 +01:00
|
|
|
|
2022-02-20 16:15:22 +01:00
|
|
|
it 'retrieves open orders in the "open" scope' do
|
2021-02-05 13:16:57 +01:00
|
|
|
expect(Order.open.count).to eq(1)
|
2021-02-05 13:58:20 +01:00
|
|
|
expect(Order.open.where(id: open_order.id)).to exist
|
2021-02-05 13:16:57 +01:00
|
|
|
end
|
|
|
|
|
2022-02-20 16:15:22 +01:00
|
|
|
it 'retrieves finished, received and closed orders in the "finished" scope' do
|
2021-02-05 13:16:57 +01:00
|
|
|
expect(Order.finished.count).to eq(3)
|
2021-02-05 13:58:20 +01:00
|
|
|
expect(Order.finished.where(id: finished_order.id)).to exist
|
|
|
|
expect(Order.finished.where(id: received_order.id)).to exist
|
|
|
|
expect(Order.finished.where(id: closed_order.id)).to exist
|
2021-02-05 13:16:57 +01:00
|
|
|
end
|
|
|
|
|
2022-02-20 16:15:22 +01:00
|
|
|
it 'retrieves finished and received orders in the "finished_not_closed" scope' do
|
2021-02-05 13:16:57 +01:00
|
|
|
expect(Order.finished_not_closed.count).to eq(2)
|
2021-02-05 13:58:20 +01:00
|
|
|
expect(Order.finished_not_closed.where(id: finished_order.id)).to exist
|
|
|
|
expect(Order.finished_not_closed.where(id: received_order.id)).to exist
|
2021-02-05 13:16:57 +01:00
|
|
|
end
|
2021-02-05 14:15:50 +01:00
|
|
|
|
2022-02-20 16:15:22 +01:00
|
|
|
it 'returns valid boolean states for open orders' do
|
2021-02-05 14:15:50 +01:00
|
|
|
expect(open_order.open?).to be(true)
|
|
|
|
expect(open_order.finished?).to be(false)
|
|
|
|
expect(open_order.received?).to be(false)
|
|
|
|
expect(open_order.closed?).to be(false)
|
|
|
|
end
|
|
|
|
|
2022-02-20 16:15:22 +01:00
|
|
|
it 'returns valid boolean states for finished orders' do
|
2021-02-05 14:15:50 +01:00
|
|
|
expect(finished_order.open?).to be(false)
|
|
|
|
expect(finished_order.finished?).to be(true)
|
|
|
|
expect(finished_order.received?).to be(false)
|
|
|
|
expect(finished_order.closed?).to be(false)
|
|
|
|
end
|
|
|
|
|
2022-02-20 16:15:22 +01:00
|
|
|
it 'returns valid boolean states for received orders' do
|
2021-02-05 14:15:50 +01:00
|
|
|
expect(received_order.open?).to be(false)
|
|
|
|
expect(received_order.finished?).to be(true)
|
|
|
|
expect(received_order.received?).to be(true)
|
|
|
|
expect(received_order.closed?).to be(false)
|
|
|
|
end
|
|
|
|
|
2022-02-20 16:15:22 +01:00
|
|
|
it 'returns valid boolean states for closed orders' do
|
2021-02-05 14:15:50 +01:00
|
|
|
expect(closed_order.open?).to be(false)
|
|
|
|
expect(closed_order.finished?).to be(false)
|
|
|
|
expect(closed_order.received?).to be(false)
|
|
|
|
expect(closed_order.closed?).to be(true)
|
|
|
|
end
|
2021-02-05 13:16:57 +01:00
|
|
|
end
|
|
|
|
|
2018-06-19 22:12:34 +02:00
|
|
|
it 'sends mail if min_order_quantity has been reached' do
|
2023-05-12 13:01:12 +02:00
|
|
|
create(:user, groups: [create(:ordergroup)])
|
|
|
|
create(:order, created_by: user, starts: Date.yesterday, ends: 1.hour.ago,
|
|
|
|
end_action: :auto_close_and_send_min_quantity)
|
2018-06-19 22:12:34 +02:00
|
|
|
|
|
|
|
Order.finish_ended!
|
|
|
|
expect(ActionMailer::Base.deliveries.count).to eq 1
|
|
|
|
end
|
|
|
|
|
2013-07-15 00:17:07 +02:00
|
|
|
it 'needs a supplier' do
|
2013-09-18 12:44:41 +02:00
|
|
|
expect(build(:order, supplier: nil)).to be_invalid
|
2013-07-15 00:17:07 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'needs order articles' do
|
2023-05-12 13:01:12 +02:00
|
|
|
supplier = create(:supplier, article_count: 0)
|
2013-09-18 12:44:41 +02:00
|
|
|
expect(build(:order, supplier: supplier)).to be_invalid
|
2013-07-15 00:17:07 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'can be created' do
|
2013-09-18 12:44:41 +02:00
|
|
|
expect(build(:order, article_count: 1)).to be_valid
|
2013-07-15 00:17:07 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'with articles' do
|
2023-05-12 13:01:12 +02:00
|
|
|
let(:order) { create(:order) }
|
2013-07-15 00:17:07 +02:00
|
|
|
|
2013-07-24 22:46:25 +02:00
|
|
|
it 'is open by default' do expect(order).to be_open end
|
2023-05-12 13:01:12 +02:00
|
|
|
it 'is not finished by default' do expect(order).not_to be_finished end
|
|
|
|
it 'is not closed by default' do expect(order).not_to be_closed end
|
2013-07-15 00:17:07 +02:00
|
|
|
|
|
|
|
it 'has valid order articles' do
|
2021-03-01 15:27:26 +01:00
|
|
|
order.order_articles.each { |oa| expect(oa).to be_valid }
|
2013-07-15 00:17:07 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'can be finished' do
|
2023-05-12 13:01:12 +02:00
|
|
|
# TODO: randomise user
|
2020-08-01 02:49:15 +02:00
|
|
|
order.finish!(user)
|
2023-05-12 13:01:12 +02:00
|
|
|
expect(order).not_to be_open
|
2013-07-24 22:46:25 +02:00
|
|
|
expect(order).to be_finished
|
2023-05-12 13:01:12 +02:00
|
|
|
expect(order).not_to be_closed
|
2013-07-15 00:17:07 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'can be closed' do
|
2023-05-12 13:01:12 +02:00
|
|
|
# TODO: randomise user
|
2020-08-01 02:49:15 +02:00
|
|
|
order.finish!(user)
|
|
|
|
order.close!(user)
|
2023-05-12 13:01:12 +02:00
|
|
|
expect(order).not_to be_open
|
2013-07-24 22:46:25 +02:00
|
|
|
expect(order).to be_closed
|
2013-07-15 00:17:07 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-11-22 00:33:16 +01:00
|
|
|
describe 'with a default end date' do
|
2023-05-12 13:01:12 +02:00
|
|
|
let(:order) { create(:order) }
|
2022-02-20 16:15:22 +01:00
|
|
|
|
2014-11-22 00:33:16 +01:00
|
|
|
before do
|
2021-03-01 15:27:26 +01:00
|
|
|
FoodsoftConfig[:order_schedule] = { ends: { recurr: 'FREQ=WEEKLY;BYDAY=MO', time: '9:00' } }
|
2014-11-22 00:33:16 +01:00
|
|
|
order.init_dates
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'to have a correct date' do
|
|
|
|
expect(order.ends.to_date).to eq Date.today.next_week.at_beginning_of_week(:monday)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'to have a correct time' do
|
|
|
|
expect(order.ends.strftime('%H:%M')).to eq '09:00'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-07-12 10:26:36 +02:00
|
|
|
describe 'mapped to GroupOrders' do
|
2023-05-12 13:01:12 +02:00
|
|
|
let!(:user) { create(:user, groups: [create(:ordergroup)]) }
|
|
|
|
let!(:order) { create(:order) }
|
|
|
|
let!(:order2) { create(:order) }
|
|
|
|
let!(:go) { create(:group_order, order: order, ordergroup: user.ordergroup) }
|
2020-07-12 10:26:36 +02:00
|
|
|
|
|
|
|
it 'to map a user\'s GroupOrders to a list of Orders' do
|
|
|
|
orders = Order.ordergroup_group_orders_map(user.ordergroup)
|
|
|
|
|
|
|
|
expect(orders.length).to be 2
|
|
|
|
expect(orders[0][:order]).to have_attributes(id: order.id)
|
|
|
|
expect(orders[0][:group_order]).to have_attributes(id: go.id)
|
|
|
|
expect(orders[1][:order]).to have_attributes(id: order2.id)
|
|
|
|
expect(orders[1][:group_order]).to be_nil
|
|
|
|
end
|
|
|
|
end
|
2021-03-19 03:36:36 +01:00
|
|
|
|
|
|
|
describe 'balancing charges correct amounts' do
|
|
|
|
let!(:transport) { rand(0.1..26.0).round(2) }
|
2023-05-12 13:01:12 +02:00
|
|
|
let!(:order) { create(:order, article_count: 1) }
|
2021-03-19 03:36:36 +01:00
|
|
|
let!(:oa) { order.order_articles.first }
|
2023-05-12 13:01:12 +02:00
|
|
|
let!(:go) { create(:group_order, order: order, transport: transport) }
|
|
|
|
let!(:goa) { create(:group_order_article, group_order: go, order_article: oa, quantity: 1) }
|
2021-03-19 03:36:36 +01:00
|
|
|
|
|
|
|
before do
|
|
|
|
goa.update_quantities(1, 0)
|
|
|
|
go.reload
|
|
|
|
go.update_price!
|
|
|
|
user.ordergroup.update_stats!
|
|
|
|
oa.update_results!
|
|
|
|
|
|
|
|
order.finish!(user)
|
|
|
|
order.reload
|
|
|
|
order.close!(user, ftt)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates financial transaction with correct amount' do
|
|
|
|
expect(goa.result).to be > 0
|
|
|
|
expect(go.ordergroup.financial_transactions.last.amount).to eq(-go.total)
|
|
|
|
end
|
|
|
|
end
|
2013-07-15 00:17:07 +02:00
|
|
|
end
|