diff --git a/app/helpers/admin/business_model_configuration_helper.rb b/app/helpers/admin/business_model_configuration_helper.rb new file mode 100644 index 0000000000..115fcf33ae --- /dev/null +++ b/app/helpers/admin/business_model_configuration_helper.rb @@ -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 diff --git a/config/locales/en.yml b/config/locales/en.yml index 373440ebfe..5a95fdb567 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -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 diff --git a/lib/spree/money_decorator.rb b/lib/spree/money_decorator.rb index fed92b8210..3479bbd9a2 100644 --- a/lib/spree/money_decorator.rb +++ b/lib/spree/money_decorator.rb @@ -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 diff --git a/spec/helpers/admin/business_model_configuration_helper_spec.rb b/spec/helpers/admin/business_model_configuration_helper_spec.rb new file mode 100644 index 0000000000..0c44d5c742 --- /dev/null +++ b/spec/helpers/admin/business_model_configuration_helper_spec.rb @@ -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