BillablePeriods use global config to calculate bills

This commit is contained in:
Rob Harrington
2015-10-28 12:15:53 +11:00
parent c1d04af5cc
commit 85f61364f8
2 changed files with 109 additions and 14 deletions

View File

@@ -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

View File

@@ -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