From d8024eb52626fe52486abaa2825b251f32627b2f Mon Sep 17 00:00:00 2001 From: Dung Bui Date: Sat, 9 Dec 2023 12:40:51 +0700 Subject: [PATCH] include voucher tax adjustment in revenues by hub report --- lib/reporting/reports/revenues_by_hub/base.rb | 54 ++++++++-- .../admin/reports/revenues_by_hub_spec.rb | 100 +++++++++++++++++- 2 files changed, 143 insertions(+), 11 deletions(-) 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 9d622ec355..1ca4fe378e 100644 --- a/spec/system/admin/reports/revenues_by_hub_spec.rb +++ b/spec/system/admin/reports/revenues_by_hub_spec.rb @@ -10,10 +10,37 @@ 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.0, + 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: true, + 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) } @@ -21,11 +48,29 @@ describe "Revenues By Hub Reports" do before do create(:line_item_with_shipment, order:, product:) + order_with_voucher_tax_included.create_tax_charge! + order_with_voucher_tax_included.update_shipping_fees! + order_with_voucher_tax_included.update_order! + + order_with_voucher_tax_excluded.create_tax_charge! + order_with_voucher_tax_excluded.update_shipping_fees! + order_with_voucher_tax_excluded.update_order! + + allow(VoucherAdjustmentsService).to receive(:new) do |order_arg| + if order_arg.id == order.id + next double(voucher_included_tax: 0.0, voucher_excluded_tax: 0.0) + elsif order_arg.id == order_with_voucher_tax_included.id + next double(voucher_included_tax: 0.5, voucher_excluded_tax: 0.0) + elsif order_arg.id == order_with_voucher_tax_excluded.id + next double(voucher_included_tax: 0.0, voucher_excluded_tax: -0.5) + end + end + 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 find("[type='submit']").click @@ -49,7 +94,8 @@ 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) + expect(lines[0]).to have_content([ "Hub 1", order.distributor.id, "none", @@ -68,6 +114,52 @@ describe "Revenues By Hub Reports" do order.total_tax, order.total ].compact.join(" ")) + + expect(lines[1]).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", + # 160.0$ - 10.5$ + 149.5, + # 10$ tax + 0.5$ voucher_included_tax + 10.5, + # 5 line_items at 10$ each + 1 line_item at 110$ + 160.0 + ].compact.join(" ")) + + expect(lines[2]).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", + # 160.0$ - 9.5$ + 150.5, + # 10$ tax - 0.5$ voucher_excluded_tax + 9.5, + # 5 line_items at 10$ each + 1 line_item at 110$ + 160.0 + ].compact.join(" ")) end end end