diff --git a/app/helpers/checkout_helper.rb b/app/helpers/checkout_helper.rb index 780c27c595..c4754e667e 100644 --- a/app/helpers/checkout_helper.rb +++ b/app/helpers/checkout_helper.rb @@ -70,10 +70,13 @@ module CheckoutHelper def display_checkout_taxes_hash(order) totals = OrderTaxAdjustmentsFetcher.new(order).totals - totals.each_with_object({}) do |(tax_rate, tax_amount), hash| - hash[number_to_percentage(tax_rate.amount * 100, precision: 1)] = - Spree::Money.new tax_amount, currency: order.currency - end + totals.map do |tax_rate, tax_amount| + { + amount: Spree::Money.new(tax_amount, currency: order.currency), + percentage: number_to_percentage(tax_rate.amount * 100, precision: 1), + rate_amount: tax_rate.amount, + } + end.sort_by { |tax| tax[:rate_amount] } end def display_line_item_tax_rates(line_item) diff --git a/app/views/spree/admin/orders/_invoice_table2.html.haml b/app/views/spree/admin/orders/_invoice_table2.html.haml index eb83714747..c9a19b7956 100644 --- a/app/views/spree/admin/orders/_invoice_table2.html.haml +++ b/app/views/spree/admin/orders/_invoice_table2.html.haml @@ -47,12 +47,12 @@ %strong= @order.has_taxes_included ? t(:total_incl_tax) : t(:total_excl_tax) %td{:align => "right", :colspan => "2"} %strong= @order.has_taxes_included ? @order.display_total : display_checkout_total_less_tax(@order) - - display_checkout_taxes_hash(@order).each do |tax_rate, tax_value| + - display_checkout_taxes_hash(@order).each do |tax| %tr %td{:align => "right", :colspan => "3"} - = t(:tax_total, rate: tax_rate) + = t(:tax_total, rate: tax[:percentage]) %td{:align => "right", :colspan => "2"} - = tax_value + = tax[:amount] %tr %td{:align => "right", :colspan => "3"} = @order.has_taxes_included ? t(:total_excl_tax) : t(:total_incl_tax) diff --git a/app/views/spree/admin/orders/ticket.html.haml b/app/views/spree/admin/orders/ticket.html.haml index 415fb97adb..d86ba0d1b6 100644 --- a/app/views/spree/admin/orders/ticket.html.haml +++ b/app/views/spree/admin/orders/ticket.html.haml @@ -58,10 +58,10 @@ j(@order.display_total.format(with_currency: false))]}", '\x1B' + '\x45' + '\x0A', // bold off '\x0A', - "#{display_checkout_taxes_hash(@order).map { |tax_rate, tax_value| + "#{display_checkout_taxes_hash(@order).map { |tax| '%31s%10s' % - [j(t(:tax_total, rate: tax_rate)), - j(tax_value.format(with_currency: false))] + + [j(t(:tax_total, rate: tax[:percentage])), + j(tax[:amount].format(with_currency: false))] + '" + \'\x0A\' + "'}.join }", "#{'%31s%10s' % [j(t(:total_excl_tax)), diff --git a/spec/helpers/checkout_helper_spec.rb b/spec/helpers/checkout_helper_spec.rb index 41bacae7bc..ef64fec69a 100644 --- a/spec/helpers/checkout_helper_spec.rb +++ b/spec/helpers/checkout_helper_spec.rb @@ -24,6 +24,98 @@ describe CheckoutHelper, type: :helper do end end + describe "#display_checkout_taxes_hash" do + let(:order) { build(:order_with_totals) } + let(:tax_rate10) { build(:tax_rate, amount: 0.1) } + let(:tax_rate20) { build(:tax_rate, amount: 0.2) } + let(:other_tax_rate20) { build(:tax_rate, amount: 0.2) } + let(:adjustment1) { + build(:adjustment, amount: 1, label: "10% tax", originator: tax_rate10) + } + let(:adjustment2) { + build(:adjustment, amount: 2, label: "20% tax", originator: tax_rate20) + } + let(:other_adjustment2) { + build(:adjustment, amount: 2, label: "20% tax", originator: other_tax_rate20) + } + + it "produces an empty array without taxes" do + expect(helper.display_checkout_taxes_hash(order)).to eq([]) + end + + it "shows a single tax adjustment" do + order.all_adjustments << adjustment1 + order.save! + + expect(helper.display_checkout_taxes_hash(order)).to eq [ + { + amount: Spree::Money.new(1, currency: order.currency), + percentage: "10.0%", + rate_amount: 0.1, + } + ] + end + + it "shows multiple tax adjustments" do + order.all_adjustments << adjustment1 + order.all_adjustments << adjustment2 + order.save! + + expect(helper.display_checkout_taxes_hash(order)).to eq [ + { + amount: Spree::Money.new(1, currency: order.currency), + percentage: "10.0%", + rate_amount: 0.1, + }, + { + amount: Spree::Money.new(2, currency: order.currency), + percentage: "20.0%", + rate_amount: 0.2, + }, + ] + end + + it "sorts adjustments by percentage" do + order.all_adjustments << adjustment2 + order.all_adjustments << adjustment1 + order.save! + + expect(helper.display_checkout_taxes_hash(order)).to eq [ + { + amount: Spree::Money.new(1, currency: order.currency), + percentage: "10.0%", + rate_amount: 0.1, + }, + { + amount: Spree::Money.new(2, currency: order.currency), + percentage: "20.0%", + rate_amount: 0.2, + }, + ] + end + + it "shows multiple tax adjustments with same percentage" do + order.all_adjustments << adjustment2 + order.all_adjustments << other_adjustment2 + order.save! + + expect(helper.display_checkout_taxes_hash(order)).to eq [ + { + amount: Spree::Money.new(2, currency: order.currency), + percentage: "20.0%", + rate_amount: 0.2, + }, + { + amount: Spree::Money.new(2, currency: order.currency), + percentage: "20.0%", + rate_amount: 0.2, + }, + ] + + expect(helper.display_checkout_taxes_hash(order).size).to eq 2 + end + end + it "knows if guests can checkout" do distributor = create(:distributor_enterprise) order = create(:order, distributor: distributor) diff --git a/spec/system/admin/order_print_ticket_spec.rb b/spec/system/admin/order_print_ticket_spec.rb index 18c2c2aede..0024a93478 100644 --- a/spec/system/admin/order_print_ticket_spec.rb +++ b/spec/system/admin/order_print_ticket_spec.rb @@ -92,10 +92,7 @@ describe ' end def taxes_in_print_data - display_checkout_taxes_hash(order).map { |tax_rate, tax_value| - [tax_rate, - tax_value.format(with_currency: false)] - } + [["10.0%", "$11.00"]] end end end