mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
Merge pull request #9611 from mkllnk/9605-tax-total
Index tax totals correctly for invoice display
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)),
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user