diff --git a/lib/reporting/reports/sales_tax/sales_tax_totals_by_producer.rb b/lib/reporting/reports/sales_tax/sales_tax_totals_by_producer.rb index 0271b38d59..864ba54956 100644 --- a/lib/reporting/reports/sales_tax/sales_tax_totals_by_producer.rb +++ b/lib/reporting/reports/sales_tax/sales_tax_totals_by_producer.rb @@ -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) diff --git a/spec/system/admin/reports/sales_tax/sales_tax_totals_by_producer_spec.rb b/spec/system/admin/reports/sales_tax/sales_tax_totals_by_producer_spec.rb index 964537a037..c602bb8981 100644 --- a/spec/system/admin/reports/sales_tax/sales_tax_totals_by_producer_spec.rb +++ b/spec/system/admin/reports/sales_tax/sales_tax_totals_by_producer_spec.rb @@ -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 })