diff --git a/lib/reporting/reports/revenues_by_hub/base.rb b/lib/reporting/reports/revenues_by_hub/base.rb index 5fb5de67c3..e539c77fed 100644 --- a/lib/reporting/reports/revenues_by_hub/base.rb +++ b/lib/reporting/reports/revenues_by_hub/base.rb @@ -19,7 +19,7 @@ module Reporting } end - def columns # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity + def columns # rubocop:disable Metrics/AbcSize { hub: proc { |orders| distributor(orders).name }, hub_id: proc { |orders| distributor(orders).id }, @@ -35,16 +35,16 @@ module Reporting hub_address_zipcode: proc { |orders| distributor(orders).address&.zipcode }, hub_address_state_name: proc { |orders| distributor(orders).address&.state_name }, total_orders: proc { |orders| orders.count }, - total_excl_tax: proc { |orders| - orders.sum { |order| order.total - order.total_tax } - }, - total_tax: proc { |orders| orders.sum(&:total_tax) }, - total_incl_tax: proc { |orders| orders.sum(&:total) } + total_excl_tax: :total_excl_tax, + total_tax: :total_tax, + total_incl_tax: :total_incl_tax } end def query_result - search.result.group_by(&:distributor).values + result = search.result.group_by(&:distributor).values + build_tax_data(result) + result end private @@ -52,6 +52,46 @@ module Reporting def distributor(orders) orders.first.distributor end + + def build_tax_data(grouped_orders) + @tax_data = {} + + grouped_orders.each do |orders| + voucher_adjustments = calculate_voucher_adjustments(orders) + + total_incl_tax = orders.sum(&:total) + total_tax = orders.sum(&:total_tax) + voucher_adjustments + total_excl_tax = total_incl_tax - total_tax + + @tax_data[distributor(orders).id] = { + total_incl_tax:, total_tax:, total_excl_tax: + } + end + end + + def calculate_voucher_adjustments(orders) + result = 0.0 + + orders.each do |order| + adjustment_service = VoucherAdjustmentsService.new(order) + result += adjustment_service.voucher_included_tax + + adjustment_service.voucher_excluded_tax + end + + result + end + + def total_incl_tax(orders) + @tax_data[distributor(orders).id][:total_incl_tax] + end + + def total_tax(orders) + @tax_data[distributor(orders).id][:total_tax] + end + + def total_excl_tax(orders) + @tax_data[distributor(orders).id][:total_excl_tax] + end end end end diff --git a/spec/system/admin/reports/revenues_by_hub_spec.rb b/spec/system/admin/reports/revenues_by_hub_spec.rb index 1daabb5359..8d950901cb 100644 --- a/spec/system/admin/reports/revenues_by_hub_spec.rb +++ b/spec/system/admin/reports/revenues_by_hub_spec.rb @@ -10,22 +10,54 @@ describe "Revenues By Hub Reports" do :completed_order_with_totals, completed_at: 2.days.ago, order_cycle:, - distributor: create(:enterprise, name: "Hub 1", - owner: create(:user, email: "email@email.com")), + distributor: distributor1 ) end + let(:order_with_voucher_tax_included) do + create( + :order_with_taxes, + completed_at: 2.days.ago, + order_cycle:, + distributor: distributor2, + product_price: 110, + tax_rate_amount: 0.1, + included_in_price: true, + tax_rate_name: "Tax 1" + ) + end + let(:order_with_voucher_tax_excluded) do + create( + :order_with_taxes, + completed_at: 2.days.ago, + order_cycle:, + distributor: distributor3, + product_price: 110.0, + tax_rate_amount: 0.1, + included_in_price: false, + tax_rate_name: "Tax 1" + ) + end + let(:distributor1) { create(:enterprise, name: "Hub 1", owner:) } + let(:distributor2) { create(:enterprise, name: "Hub 2", owner:) } + let(:distributor3) { create(:enterprise, name: "Hub 3", owner:) } + let(:owner) { create(:user, email: 'email@email.com') } let(:order_cycle) { create(:simple_order_cycle) } let(:product) { create(:product, supplier:) } let(:supplier) { create(:supplier_enterprise) } + let(:voucher2) { create(:voucher_flat_rate, code: 'code', enterprise: distributor2, amount: 10) } + let(:voucher3) { create(:voucher_flat_rate, code: 'code', enterprise: distributor3, amount: 10) } before do create(:line_item_with_shipment, order:, product:) + apply_voucher(order_with_voucher_tax_included, voucher2) + apply_voucher(order_with_voucher_tax_excluded, voucher3) + login_as_admin visit main_app.admin_report_path(report_type: 'revenues_by_hub') end - context "testing report" do + context "testing report", aggregate_failures: true do it "show the right values" do run_report @@ -49,7 +81,9 @@ describe "Revenues By Hub Reports" do "TOTAL INCL. TAX ($)" ].join(" ")) - expect(page.find("table.report__table tbody tr").text).to have_content([ + lines = page.all('table.report__table tbody tr').map(&:text) + first_line = lines.detect { |line| line.include?('Hub 1') } + expect(first_line).to have_content([ "Hub 1", order.distributor.id, "none", @@ -68,6 +102,61 @@ describe "Revenues By Hub Reports" do order.total_tax, order.total ].compact.join(" ")) + + second_line = lines.detect { |line| line.include?('Hub 2') } + expect(second_line).to have_content([ + "Hub 2", + order_with_voucher_tax_included.distributor.id, + "none", + "none", + "none", + "none", + "email@email.com", + "none", + "10 Lovely Street", + nil, + "Northwest Herndon", + "20170", + "Victoria", + "1", + 140.63, + 9.37, + 150.0 + ].compact.join(" ")) + + third_line = lines.detect { |line| line.include?('Hub 3') } + expect(third_line).to have_content([ + "Hub 3", + order_with_voucher_tax_excluded.distributor.id, + "none", + "none", + "none", + "none", + "email@email.com", + "none", + "10 Lovely Street", + nil, + "Northwest Herndon", + "20170", + "Victoria", + "1", + 150.64, + 10.36, + 161.0 + ].compact.join(" ")) end end + + def apply_voucher(order, voucher) + voucher.create_adjustment(voucher.code, order) + + # Update taxes + order.create_tax_charge! + order.update_shipping_fees! + order.update_order! + + VoucherAdjustmentsService.new(order).update + + order.update_totals_and_states + end end