From 91eaa522cf098c3fa76afd4acad53d8578b278a0 Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Wed, 31 Aug 2022 16:46:52 +1000 Subject: [PATCH] Simplify tax totals helper --- app/helpers/checkout_helper.rb | 11 ++++---- .../admin/orders/_invoice_table2.html.haml | 2 +- app/views/spree/admin/orders/ticket.html.haml | 2 +- spec/helpers/checkout_helper_spec.rb | 26 +++++++++---------- 4 files changed, 20 insertions(+), 21 deletions(-) diff --git a/app/helpers/checkout_helper.rb b/app/helpers/checkout_helper.rb index a4fd0b13f1..51f58bf52e 100644 --- a/app/helpers/checkout_helper.rb +++ b/app/helpers/checkout_helper.rb @@ -70,12 +70,11 @@ module CheckoutHelper def display_checkout_taxes_hash(order) totals = OrderTaxAdjustmentsFetcher.new(order).totals - totals.each_with_object({}) do |(tax_rate, tax_amount), hash| - hash[tax_rate] = - { - amount: Spree::Money.new(tax_amount, currency: order.currency), - percentage: number_to_percentage(tax_rate.amount * 100, precision: 1), - } + 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), + } end end diff --git a/app/views/spree/admin/orders/_invoice_table2.html.haml b/app/views/spree/admin/orders/_invoice_table2.html.haml index e94485afc7..c9a19b7956 100644 --- a/app/views/spree/admin/orders/_invoice_table2.html.haml +++ b/app/views/spree/admin/orders/_invoice_table2.html.haml @@ -47,7 +47,7 @@ %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| + - display_checkout_taxes_hash(@order).each do |tax| %tr %td{:align => "right", :colspan => "3"} = t(:tax_total, rate: tax[:percentage]) diff --git a/app/views/spree/admin/orders/ticket.html.haml b/app/views/spree/admin/orders/ticket.html.haml index 9cf318a409..d86ba0d1b6 100644 --- a/app/views/spree/admin/orders/ticket.html.haml +++ b/app/views/spree/admin/orders/ticket.html.haml @@ -58,7 +58,7 @@ j(@order.display_total.format(with_currency: false))]}", '\x1B' + '\x45' + '\x0A', // bold off '\x0A', - "#{display_checkout_taxes_hash(@order).map { |tax_rate, tax| + "#{display_checkout_taxes_hash(@order).map { |tax| '%31s%10s' % [j(t(:tax_total, rate: tax[:percentage])), j(tax[:amount].format(with_currency: false))] + diff --git a/spec/helpers/checkout_helper_spec.rb b/spec/helpers/checkout_helper_spec.rb index c1c5eada0a..2bc56b83e6 100644 --- a/spec/helpers/checkout_helper_spec.rb +++ b/spec/helpers/checkout_helper_spec.rb @@ -39,20 +39,20 @@ describe CheckoutHelper, type: :helper do build(:adjustment, amount: 2, label: "20% tax", originator: other_tax_rate20) } - it "produces an empty hash without taxes" do - expect(helper.display_checkout_taxes_hash(order)).to eq({}) + 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( - tax_rate10 => { + expect(helper.display_checkout_taxes_hash(order)).to eq [ + { amount: Spree::Money.new(1, currency: order.currency), percentage: "10.0%", } - ) + ] end it "shows multiple tax adjustments" do @@ -60,16 +60,16 @@ describe CheckoutHelper, type: :helper do order.all_adjustments << adjustment2 order.save! - expect(helper.display_checkout_taxes_hash(order)).to eq( - tax_rate10 => { + expect(helper.display_checkout_taxes_hash(order)).to match_array [ + { amount: Spree::Money.new(1, currency: order.currency), percentage: "10.0%", }, - tax_rate20 => { + { amount: Spree::Money.new(2, currency: order.currency), percentage: "20.0%", }, - ) + ] end it "shows multiple tax adjustments with same percentage" do @@ -77,16 +77,16 @@ describe CheckoutHelper, type: :helper do order.all_adjustments << other_adjustment2 order.save! - expect(helper.display_checkout_taxes_hash(order)).to eq( - tax_rate20 => { + expect(helper.display_checkout_taxes_hash(order)).to match_array [ + { amount: Spree::Money.new(2, currency: order.currency), percentage: "20.0%", }, - other_tax_rate20 => { + { amount: Spree::Money.new(2, currency: order.currency), percentage: "20.0%", }, - ) + ] expect(helper.display_checkout_taxes_hash(order).size).to eq 2 end