From 326b0af35ff3955f308abf70ea301646255eecf6 Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Thu, 1 Sep 2022 11:19:02 +1000 Subject: [PATCH] Sort tax total by percentage on invoice --- app/helpers/checkout_helper.rb | 3 ++- spec/helpers/checkout_helper_spec.rb | 28 ++++++++++++++++++++++++++-- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/app/helpers/checkout_helper.rb b/app/helpers/checkout_helper.rb index 51f58bf52e..c4754e667e 100644 --- a/app/helpers/checkout_helper.rb +++ b/app/helpers/checkout_helper.rb @@ -74,8 +74,9 @@ module CheckoutHelper { 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 + end.sort_by { |tax| tax[:rate_amount] } end def display_line_item_tax_rates(line_item) diff --git a/spec/helpers/checkout_helper_spec.rb b/spec/helpers/checkout_helper_spec.rb index 2bc56b83e6..ef64fec69a 100644 --- a/spec/helpers/checkout_helper_spec.rb +++ b/spec/helpers/checkout_helper_spec.rb @@ -51,6 +51,7 @@ describe CheckoutHelper, type: :helper do { amount: Spree::Money.new(1, currency: order.currency), percentage: "10.0%", + rate_amount: 0.1, } ] end @@ -60,14 +61,35 @@ describe CheckoutHelper, type: :helper do order.all_adjustments << adjustment2 order.save! - expect(helper.display_checkout_taxes_hash(order)).to match_array [ + 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 @@ -77,14 +99,16 @@ describe CheckoutHelper, type: :helper do order.all_adjustments << other_adjustment2 order.save! - expect(helper.display_checkout_taxes_hash(order)).to match_array [ + 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, }, ]