Fix incorrect results for multiple tax rates in report

- total_excl_tax would sum amounts and taxes without
    knowledge if taxes rates were really applied or not
  - spec shows use case described in issue 12066
This commit is contained in:
cyrillefr
2024-02-27 10:45:57 +01:00
committed by Rachel Arnould
parent 07a43fecdf
commit 7fdf1a110d
2 changed files with 70 additions and 3 deletions

View File

@@ -125,8 +125,14 @@ module Reporting
end
def total_excl_tax(query_result_row)
line_items(query_result_row).sum(&:amount) -
line_items(query_result_row).sum(&:included_tax)
line_items(query_result_row)&.map do |line_item|
if line_item.adjustments.eligible.tax
.where(originator_id: tax_rate_id(query_result_row)).empty?
0
else
(line_item.amount - line_item.included_tax)
end
end&.sum
end
def tax(query_result_row)

View File

@@ -3,7 +3,7 @@
require 'system_helper'
describe "Sales Tax Totals By Producer" do
# Scenarion 1: added tax
# Scenario 1: added tax
# 1 producer
# 1 distributor
# 1 product that costs 100$
@@ -30,6 +30,7 @@ describe "Sales Tax Totals By Producer" do
let!(:state_tax_rate){ create(:tax_rate, zone: state_zone, tax_category:) }
let!(:country_tax_rate){ create(:tax_rate, zone: country_zone, tax_category:) }
let!(:ship_address){ create(:ship_address) }
let(:another_state){ create(:state, name: 'Another state', country: ship_address.country) }
let!(:variant){ create(:variant) }
let!(:product){ variant.product }
@@ -119,6 +120,66 @@ describe "Sales Tax Totals By Producer" do
end
end
context 'Order not to be shipped in a state affected by state tax rate' do
# Therefore, do not apply both tax rates here, only country one
before do
ship_address.update!({ state_id: another_state.id })
order.line_items.create({ variant:, quantity: 1, price: 100 })
order.update!({
order_cycle_id: order_cycle.id,
ship_address_id: ship_address.id
})
while !order.completed?
break unless order.next!
end
end
it 'generates the report' do
login_as admin
visit admin_reports_path
click_on 'Sales Tax Totals By Producer'
run_report
expect(page.find("table.report__table thead tr").text).to have_content(table_header)
expect(page.find("table.report__table tbody").text).to have_content([
"Distributor",
"Yes",
"Supplier",
"Yes",
"oc1",
"tax_category",
"State",
"1.5 %",
"0",
"0",
"0"
].join(" "))
expect(page.find("table.report__table tbody").text).to have_content([
"Distributor",
"Yes",
"Supplier",
"Yes",
"oc1",
"tax_category",
"Country",
"2.5 %",
"100.0",
"2.5",
"102.5"
].join(" "))
expect(page.find("table.report__table tbody").text).to have_content([
"TOTAL",
"100.0",
"2.5",
"102.5"
].join(" "))
end
end
context 'included tax' do
before do
state_tax_rate.update!({ included_in_price: true })