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..a2c00ea082 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 @@ -24,9 +24,9 @@ module Reporting # [tax_rate, supplier_id, distributor_id and order_cycle_id] report_line_items.list .flat_map do |line_item| - line_item.tax_rates.map do |tax_rate| + line_item.adjustments.eligible.tax.map do |tax_rate| { - tax_rate_id: tax_rate.id, + tax_rate_id: tax_rate.originator_id, line_item: } end 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..b1d319b887 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 } @@ -69,9 +70,7 @@ describe "Sales Tax Totals By Producer" do ship_address_id: ship_address.id }) - while !order.completed? - break unless order.next! - end + OrderWorkflow.new(order).complete! end it "generates the report" do @@ -119,6 +118,54 @@ 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 + }) + + OrderWorkflow.new(order).complete! + 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", + "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(" ")) + + # Even though 2 tax rates exist (but only one has been applied), we should get only 2 lines: + # one line item + total + expect(page.all("table.report__table tbody tr").count).to eq(2) + end + end + context 'included tax' do before do state_tax_rate.update!({ included_in_price: true }) @@ -130,9 +177,7 @@ describe "Sales Tax Totals By Producer" do ship_address_id: ship_address.id }) - while !order.completed? - break unless order.next! - end + OrderWorkflow.new(order).complete! end it "generates the report" do login_as admin @@ -308,9 +353,7 @@ describe "Sales Tax Totals By Producer" do ship_address_id: customer1.bill_address_id, customer_id: customer1.id }) - while !order.completed? - break unless order.next! - end + OrderWorkflow.new(order).complete! order2.line_items.create({ variant:, quantity: 1, price: 200 }) order2.update!({ @@ -318,9 +361,7 @@ describe "Sales Tax Totals By Producer" do ship_address_id: customer2.bill_address_id, customer_id: customer2.id }) - while !order2.completed? - break unless order2.next! - end + OrderWorkflow.new(order2).complete! login_as admin visit admin_reports_path click_on 'Sales Tax Totals By Producer'