Sales Tax Totals by order, fix tax amount when voucher applied

Apply the tax discount to the tax collumn when a voucher is added
to the order, and update total excluding tax accordingly.
This commit is contained in:
Gaetan Craig-Riou
2023-09-12 14:36:28 +02:00
parent a7bceb0b28
commit 9925399292
3 changed files with 25 additions and 8 deletions

View File

@@ -70,6 +70,7 @@ module Spree
scope :credit, -> { where('amount < 0') }
scope :return_authorization, -> { where(originator_type: "Spree::ReturnAuthorization") }
scope :voucher, -> { where(originator_type: "Voucher") }
scope :voucher_tax, -> { where(originator_type: "Voucher").where("label LIKE 'Tax%'") }
scope :non_voucher, -> { where.not(originator_type: "Voucher") }
scope :inclusive, -> { where(included: true) }
scope :additional, -> { where(included: false) }

View File

@@ -92,8 +92,8 @@ module Reporting
summary_row: proc do |_key, items, _rows|
order = items.first.second
{
total_excl_tax: order.total - order.total_tax,
tax: order.total_tax,
total_excl_tax: order.total - (order.total_tax + voucher_tax_adjustment(order)),
tax: order.total_tax + voucher_tax_adjustment(order),
total_incl_tax: order.total,
first_name: order.customer&.first_name,
last_name: order.customer&.last_name,
@@ -130,20 +130,36 @@ module Reporting
end
def total_excl_tax(query_result_row)
order(query_result_row).total - order(query_result_row).total_tax
order = order(query_result_row)
total_excl_tax = order.total - order.total_tax
# Tax adjusment is a negative value, so we need to substract to add it to the total
total_excl_tax - voucher_tax_adjustment(order)
end
def tax_rate_total(query_result_row)
total_tax = non_adjusted_tax_rate_total(query_result_row)
# Tax adjustment is already a negative value
order = order(query_result_row)
total_tax + voucher_tax_adjustment(order)
end
def total_incl_tax(query_result_row)
order(query_result_row).total -
order(query_result_row).total_tax +
non_adjusted_tax_rate_total(query_result_row)
end
def non_adjusted_tax_rate_total(query_result_row)
order(query_result_row).all_adjustments
.tax
.where(originator_id: tax_rate_id(query_result_row))
.pick('sum(amount)') || 0
end
def total_incl_tax(query_result_row)
order(query_result_row).total -
order(query_result_row).total_tax +
tax_rate_total(query_result_row)
def voucher_tax_adjustment(order)
order.all_adjustments.voucher_tax.first&.amount || 0
end
def first_name(query_result_row)

View File

@@ -174,7 +174,7 @@ describe "Sales Tax Totals By order" do
OrderWorkflow.new(order).complete!
end
pending "generates the report with the correct tax amount" do
it "generates the report with the correct tax amount" do
visit_sales_tax_totals_by_order
expect(page).to have_button("Go")