From d8024eb52626fe52486abaa2825b251f32627b2f Mon Sep 17 00:00:00 2001 From: Dung Bui Date: Sat, 9 Dec 2023 12:40:51 +0700 Subject: [PATCH 1/3] 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 From 1867d7ef8e774c13fcee963c348aca5fc1c6cc5f Mon Sep 17 00:00:00 2001 From: Dung Bui Date: Sun, 17 Dec 2023 10:47:08 +0700 Subject: [PATCH 2/3] creat real records instead of mocking --- .../admin/reports/revenues_by_hub_spec.rb | 52 ++++++++----------- 1 file changed, 22 insertions(+), 30 deletions(-) diff --git a/spec/system/admin/reports/revenues_by_hub_spec.rb b/spec/system/admin/reports/revenues_by_hub_spec.rb index e8ffbf4137..2bad48930b 100644 --- a/spec/system/admin/reports/revenues_by_hub_spec.rb +++ b/spec/system/admin/reports/revenues_by_hub_spec.rb @@ -19,7 +19,7 @@ describe "Revenues By Hub Reports" do completed_at: 2.days.ago, order_cycle:, distributor: distributor2, - product_price: 110.0, + product_price: 110, tax_rate_amount: 0.1, included_in_price: true, tax_rate_name: "Tax 1" @@ -33,7 +33,7 @@ describe "Revenues By Hub Reports" do distributor: distributor3, product_price: 110.0, tax_rate_amount: 0.1, - included_in_price: true, + included_in_price: false, tax_rate_name: "Tax 1" ) end @@ -44,27 +44,14 @@ describe "Revenues By Hub Reports" do 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:) - 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 + 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') @@ -130,11 +117,8 @@ describe "Revenues By Hub Reports" do "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$ + 150.63, + 9.37, 160.0 ].compact.join(" ")) @@ -153,13 +137,21 @@ describe "Revenues By Hub Reports" do "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 + 160.64, + 10.36, + 171.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 + end end From 248b9fb18602d109fcac68fede907a30ba127533 Mon Sep 17 00:00:00 2001 From: Dung Bui Date: Mon, 18 Dec 2023 19:25:46 +0700 Subject: [PATCH 3/3] update order state --- .../admin/reports/revenues_by_hub_spec.rb | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/spec/system/admin/reports/revenues_by_hub_spec.rb b/spec/system/admin/reports/revenues_by_hub_spec.rb index 2bad48930b..8d950901cb 100644 --- a/spec/system/admin/reports/revenues_by_hub_spec.rb +++ b/spec/system/admin/reports/revenues_by_hub_spec.rb @@ -82,7 +82,8 @@ describe "Revenues By Hub Reports" do ].join(" ")) lines = page.all('table.report__table tbody tr').map(&:text) - expect(lines[0]).to have_content([ + first_line = lines.detect { |line| line.include?('Hub 1') } + expect(first_line).to have_content([ "Hub 1", order.distributor.id, "none", @@ -102,7 +103,8 @@ describe "Revenues By Hub Reports" do order.total ].compact.join(" ")) - expect(lines[1]).to have_content([ + 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", @@ -117,12 +119,13 @@ describe "Revenues By Hub Reports" do "20170", "Victoria", "1", - 150.63, + 140.63, 9.37, - 160.0 + 150.0 ].compact.join(" ")) - expect(lines[2]).to have_content([ + 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", @@ -137,9 +140,9 @@ describe "Revenues By Hub Reports" do "20170", "Victoria", "1", - 160.64, + 150.64, 10.36, - 171.0 + 161.0 ].compact.join(" ")) end end @@ -153,5 +156,7 @@ describe "Revenues By Hub Reports" do order.update_order! VoucherAdjustmentsService.new(order).update + + order.update_totals_and_states end end