From f454abed017172a04a308dafdbd8da163cd45683 Mon Sep 17 00:00:00 2001 From: Obfuscoder Date: Sat, 3 Sep 2022 12:13:11 +0200 Subject: [PATCH] Enhance reservation/event models to allow fee based on item count --- app/models/reservation.rb | 3 ++- db/schema.rb | 3 ++- spec/features/admin/events_spec.rb | 2 +- spec/models/event_spec.rb | 26 ++++++++++++++++++++++++-- spec/models/reservation_spec.rb | 28 ++++++++++++++++++++++++---- 5 files changed, 53 insertions(+), 9 deletions(-) diff --git a/app/models/reservation.rb b/app/models/reservation.rb index 75428655..f9e31fb8 100644 --- a/app/models/reservation.rb +++ b/app/models/reservation.rb @@ -49,7 +49,8 @@ def commission_rate end def fee - self[:fee] || event.reservation_fee + result = self[:fee] || event.reservation_fee + event.reservation_fee_based_on_item_count? ? result * items.count : result end def max_items diff --git a/db/schema.rb b/db/schema.rb index 92bf72d7..b5d7bb8d 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20220803083507) do +ActiveRecord::Schema.define(version: 20220903092144) do create_table "bills", force: :cascade do |t| t.integer "event_id" @@ -128,6 +128,7 @@ t.boolean "precise_bill_amounts" t.boolean "gates" t.decimal "price_factor", precision: 3, scale: 2 + t.boolean "reservation_fee_based_on_item_count" t.index ["client_id"], name: "index_events_on_client_id" t.index ["number", "client_id"], name: "index_events_on_number_and_client_id", unique: true t.index ["token"], name: "index_events_on_token", unique: true diff --git a/spec/features/admin/events_spec.rb b/spec/features/admin/events_spec.rb index 41218db3..3b7d2fed 100644 --- a/spec/features/admin/events_spec.rb +++ b/spec/features/admin/events_spec.rb @@ -31,7 +31,7 @@ expect(find_field('Basis für Preisangaben').value).to eq '0.1' end - it 'seller fee prefilled with brand setting' do + it 'reservation fee prefilled with brand setting' do click_on 'Neuer Termin' expect(find_field('event_reservation_fee').value).to eq '2.0' end diff --git a/spec/models/event_spec.rb b/spec/models/event_spec.rb index 10e12193..5c6e3039 100644 --- a/spec/models/event_spec.rb +++ b/spec/models/event_spec.rb @@ -241,9 +241,31 @@ describe '#reservation_fees_sum' do subject { event.reservation_fees_sum } - let!(:reservation2) { create :reservation, event: event, fee: 10 } + it { is_expected.to eq 2 } + + context 'with 2nd reservation having special fee' do + let!(:reservation2) { create :reservation, event: event, fee: 3 } + + it { is_expected.to eq 5 } + end + + context 'when reservation fee is based on item count' do + let(:event) { build :event_with_ongoing_reservation, reservation_fee_based_on_item_count: true } - it { is_expected.to eq 12 } + it { is_expected.to eq 10 } + + context 'with 2nd reservation having special fee' do + let!(:reservation2) { create :reservation, event: event, fee: 3 } + + it { is_expected.to eq 10 } + + context 'with items' do + let!(:items2) { create_list :item, 3, reservation: reservation2 } + + it { is_expected.to eq 19 } + end + end + end end describe '#revenue' do diff --git a/spec/models/reservation_spec.rb b/spec/models/reservation_spec.rb index 0953140f..45766296 100644 --- a/spec/models/reservation_spec.rb +++ b/spec/models/reservation_spec.rb @@ -154,16 +154,36 @@ describe '#fee' do subject(:action) { reservation.fee } - let(:fee) { 1.5 } + let(:event) { create :event_with_ongoing_reservation } + let(:reservation) { create :reservation, event: event, fee: fee } + let(:fee) { nil } + + it { is_expected.to eq reservation.event.reservation_fee } context 'when reservation fee is set' do - before { reservation.fee = fee } + let(:fee) { 1.5 } it { is_expected.to eq fee } end - context 'when reservation fee is not set' do - it { is_expected.to eq reservation.event.reservation_fee } + context 'when event reservation fee is based on item count' do + let(:event) { create :event_with_ongoing_reservation, reservation_fee_based_on_item_count: true } + + it { is_expected.to eq 0 } + + context 'with items' do + let(:items) { create_list :item, 5, reservation: reservation } + + before { items } + + it { is_expected.to eq 10 } + + context 'when reservation fee is set' do + let(:fee) { 1.5 } + + it { is_expected.to eq 7.5 } + end + end end end