Adding helper for describing monthly billing charges

This commit is contained in:
Rob Harrington
2015-10-28 11:39:01 +11:00
parent da325780b1
commit 5b72f53738
4 changed files with 124 additions and 1 deletions

View File

@@ -0,0 +1,44 @@
module Admin
module BusinessModelConfigurationHelper
def monthly_bill_description
plus = monthly_bill_includes_fixed? && monthly_bill_includes_rate? ? " + " : ""
if fixed_description.empty? && rate_description.empty?
t(:free).upcase
elsif monthly_bill_includes_cap? && monthly_bill_includes_rate? # only care about cap if there is a rate too
"#{fixed_description}#{plus}#{rate_description}{joiner}#{cap_description} #{t(:per_month).upcase}"
else
"#{fixed_description}#{plus}#{rate_description} #{t(:per_month).upcase}"
end
end
private
def fixed_description
fixed_amount = Spree::Money.new(Spree::Config[:account_invoices_monthly_fixed], {currency: Spree::Config[:currency]} ).rounded
monthly_bill_includes_fixed? ? "#{fixed_amount}" : ""
end
def rate_description
percentage = (Spree::Config[:account_invoices_monthly_rate]*100).round(2)
monthly_bill_includes_rate? ? t(:percentage_of_sales, percentage: "#{percentage}%").upcase : ""
end
def cap_description
cap_amount = Spree::Money.new(Spree::Config[:account_invoices_monthly_cap], { currency: Spree::Config[:currency] }).rounded
monthly_bill_includes_cap? ? "#{t(:capped_at_cap, cap: cap_amount).upcase}" : ""
end
def monthly_bill_includes_fixed?
Spree::Config[:account_invoices_monthly_fixed] > 0
end
def monthly_bill_includes_rate?
Spree::Config[:account_invoices_monthly_rate] > 0
end
def monthly_bill_includes_cap?
Spree::Config[:account_invoices_monthly_cap] > 0
end
end
end

View File

@@ -30,6 +30,12 @@ en:
confirm_send_invoice: "An invoice for this order will be sent to the customer. Are you sure you want to continue?"
confirm_resend_order_confirmation: "Are you sure you want to resend the order confirmation email?"
must_have_valid_business_number: "%{enterprise_name} must have a valid ABN before invoices can be sent."
invoice: "Invoice"
percentage_of_sales: "%{percentage} of sales"
capped_at: "capped at"
capped_at_cap: "capped at %{cap}"
per_month: "per month"
free: "free"
sort_order_cycles_on_shopfront_by: "Sort Order Cycles On Shopfront By"
@@ -55,7 +61,6 @@ en:
footer_links_md: "Links"
footer_about_url: "About URL"
footer_tos_url: "Terms of Service URL"
invoice: "Invoice"
name: Name
first_name: First Name

View File

@@ -4,4 +4,9 @@ Spree::Money.class_eval do
def self.currency_symbol
Money.new(0, Spree::Config[:currency]).symbol
end
def rounded
@options[:no_cents] = true if @money.amount % 1 == 0
to_s
end
end

View File

@@ -0,0 +1,69 @@
require 'spec_helper'
describe Admin::BusinessModelConfigurationHelper do
describe "describing monthly bills for enterprises" do
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.05) }
context "when the bill is capped" do
before { Spree::Config.set(:account_invoices_monthly_cap, 20) }
it { expect(helper.monthly_bill_description).to eq "$10 + 5.0% OF SALES{joiner}CAPPED AT $20 PER MONTH" }
end
context "when the bill is not capped" do
before { Spree::Config.set(:account_invoices_monthly_cap, 0) }
it { expect(helper.monthly_bill_description).to eq "$10 + 5.0% OF SALES PER MONTH" }
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(helper.monthly_bill_description).to eq "$10 PER MONTH" }
end
context "when the bill is not capped" do
before { Spree::Config.set(:account_invoices_monthly_cap, 0) }
it { expect(helper.monthly_bill_description).to eq "$10 PER MONTH" }
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.05) }
context "when the bill is capped" do
before { Spree::Config.set(:account_invoices_monthly_cap, 20) }
it { expect(helper.monthly_bill_description).to eq "5.0% OF SALES{joiner}CAPPED AT $20 PER MONTH" }
end
context "when the bill is not capped" do
before { Spree::Config.set(:account_invoices_monthly_cap, 0) }
it { expect(helper.monthly_bill_description).to eq "5.0% OF SALES PER MONTH" }
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(helper.monthly_bill_description).to eq "FREE" }
end
context "when the bill is not capped" do
before { Spree::Config.set(:account_invoices_monthly_cap, 0) }
it { expect(helper.monthly_bill_description).to eq "FREE" }
end
end
end
end
end