diff --git a/app/models/billable_period.rb b/app/models/billable_period.rb index 79f4b3eaba..635a0b9a28 100644 --- a/app/models/billable_period.rb +++ b/app/models/billable_period.rb @@ -15,14 +15,16 @@ class BillablePeriod < ActiveRecord::Base end def bill - # Will make this more sophisicated in the future in that it will use global config variables to calculate + fixed = Spree::Config[:account_invoices_monthly_fixed] + rate = Spree::Config[:account_invoices_monthly_rate] + cap = Spree::Config[:account_invoices_monthly_cap] + return 0 if trial? - if ['own', 'any'].include? sells - bill = (turnover * 0.02).round(2) - bill > 50 ? 50 : bill - else - 0 - end + return 0 unless ['own', 'any'].include?(sells) + + bill = fixed + (turnover * rate).round(2) + return bill unless cap > 0 + [bill, cap].min end def label diff --git a/spec/models/billable_period_spec.rb b/spec/models/billable_period_spec.rb index 1ad974d804..b53a0bc36b 100644 --- a/spec/models/billable_period_spec.rb +++ b/spec/models/billable_period_spec.rb @@ -1,15 +1,18 @@ require 'spec_helper' -describe Customer, type: :model do +describe BillablePeriod, type: :model do + + require 'spec_helper' + describe 'ensure_correct_adjustment' do let!(:start_of_july) { Time.zone.now.beginning_of_year + 6.months } let!(:user) { create(:user) } let!(:invoice) { create(:order, user: user) } - let!(:billable_period) { create(:billable_period, owner: user, begins_at: start_of_july, ends_at: start_of_july + 12.days) } + let!(:subject) { create(:billable_period, owner: user, begins_at: start_of_july, ends_at: start_of_july + 12.days) } before do - allow(billable_period).to receive(:bill) { 99 } - allow(billable_period).to receive(:adjustment_label) { "Label for adjustment" } + allow(subject).to receive(:bill) { 99 } + allow(subject).to receive(:adjustment_label) { "Label for adjustment" } Spree::Config.set({ account_bill_inc_tax: true }) Spree::Config.set({ account_bill_tax_rate: 0.1 }) end @@ -17,11 +20,101 @@ describe Customer, type: :model do context "when no adjustment currently exists" do it "creates an adjustment on the given order" do expect(invoice.total_tax).to eq 0.0 - expect(billable_period.adjustment).to be nil - billable_period.ensure_correct_adjustment_for(invoice) - expect(billable_period.adjustment).to be_a Spree::Adjustment + expect(subject.adjustment).to be nil + subject.ensure_correct_adjustment_for(invoice) + expect(subject.adjustment).to be_a Spree::Adjustment expect(invoice.total_tax).to eq 9.0 end end end + + + describe "calculating monthly bills for enterprises" do + let!(:subject) { create(:billable_period, turnover: 100) } + + context "when a fixed cost is included" do + before { Spree::Config.set(:account_invoices_monthly_fixed, 10) } + + context "when a percentage of turnover is included" do + before { Spree::Config.set(:account_invoices_monthly_rate, 0.5) } + + context "when the bill is capped" do + context "at a level higher than the fixed charge plus the product of the rate and turnover" do + before { Spree::Config.set(:account_invoices_monthly_cap, 65) } + it { expect(subject.bill).to eq 60 } + end + + context "at a level lower than the fixed charge plus the product of the rate and turnover" do + before { Spree::Config.set(:account_invoices_monthly_cap, 55) } + it { expect(subject.bill).to eq 55 } + end + end + + context "when the bill is not capped" do + before { Spree::Config.set(:account_invoices_monthly_cap, 0) } + it { expect(subject.bill).to eq 60 } + end + end + + context "when a percentage of turnover is not included" do + before { Spree::Config.set(:account_invoices_monthly_rate, 0) } + + context "when the bill is capped" do + context "at a level higher than the fixed charge" do + before { Spree::Config.set(:account_invoices_monthly_cap, 15) } + it { expect(subject.bill).to eq 10 } + end + + context "at a level lower than the fixed charge" do + before { Spree::Config.set(:account_invoices_monthly_cap, 5) } + it { expect(subject.bill).to eq 5 } + end + end + + context "when the bill is not capped" do + before { Spree::Config.set(:account_invoices_monthly_cap, 0) } + it { expect(subject.bill).to eq 10 } + end + end + end + + context "when a fixed cost is not included" do + before { Spree::Config.set(:account_invoices_monthly_fixed, 0) } + + context "when a percentage of turnover is included" do + before { Spree::Config.set(:account_invoices_monthly_rate, 0.5) } + + context "when the bill is capped" do + context "at a level higher than the product of the rate and turnover" do + before { Spree::Config.set(:account_invoices_monthly_cap, 55) } + it { expect(subject.bill).to eq 50 } + end + + context "at a level lower than the product of the rate and turnover" do + before { Spree::Config.set(:account_invoices_monthly_cap, 45) } + it { expect(subject.bill).to eq 45 } + end + end + + context "when the bill is not capped" do + before { Spree::Config.set(:account_invoices_monthly_cap, 0) } + it { expect(subject.bill).to eq 50 } + end + end + + context "when a percentage of turnover is not included" do + before { Spree::Config.set(:account_invoices_monthly_rate, 0) } + + context "when the bill is capped" do + before { Spree::Config.set(:account_invoices_monthly_cap, 20) } + it { expect(subject.bill).to eq 0 } + end + + context "when the bill is not capped" do + before { Spree::Config.set(:account_invoices_monthly_cap, 0) } + it { expect(subject.bill).to eq 0 } + end + end + end + end end