From 53978de632298219117894179a002d92dd29f2e8 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Bellet Date: Thu, 4 Aug 2022 14:32:10 +0200 Subject: [PATCH] Create a new report that show revenues (incl. or excl. taxes) by hub + Create spec --- .../reports/filters/_revenues_by_hub.haml | 2 + config/locales/en.yml | 7 +++ lib/reporting/reports/list.rb | 3 +- lib/reporting/reports/revenues_by_hub/base.rb | 47 +++++++++++++++++ spec/models/spree/ability_spec.rb | 4 +- .../admin/reports/revenues_by_hub_spec.rb | 51 +++++++++++++++++++ 6 files changed, 111 insertions(+), 3 deletions(-) create mode 100644 app/views/admin/reports/filters/_revenues_by_hub.haml create mode 100644 lib/reporting/reports/revenues_by_hub/base.rb create mode 100644 spec/system/admin/reports/revenues_by_hub_spec.rb diff --git a/app/views/admin/reports/filters/_revenues_by_hub.haml b/app/views/admin/reports/filters/_revenues_by_hub.haml new file mode 100644 index 0000000000..56d2b75c7a --- /dev/null +++ b/app/views/admin/reports/filters/_revenues_by_hub.haml @@ -0,0 +1,2 @@ += render 'admin/reports/date_range_form', f: f + diff --git a/config/locales/en.yml b/config/locales/en.yml index 24b37f5962..7d8a6f473f 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -1290,6 +1290,9 @@ en: pack_by_customer: Pack By Customer pack_by_supplier: Pack By Supplier pack_by_product: Pack By Product + revenues_by_hub: + name: Revenues By Hub + description: Revenues by hub orders_and_distributors: name: Orders And Distributors description: Orders with distributor details @@ -2673,6 +2676,8 @@ See the %{link} to find out more about %{sitename}'s features and to start using report_header_hub_address: Hub Address report_header_to_hub: To Hub report_header_hub_code: Hub Code + report_header_hub_id: Hub ID + report_header_hub_owner_email: Hub Owner Email report_header_code: Code report_header_paid: Paid? report_header_delivery: Delivery? @@ -2705,6 +2710,8 @@ See the %{link} to find out more about %{sitename}'s features and to start using report_header_tax_on_delivery: "Tax on Delivery (%{currency_symbol})" report_header_tax_on_fees: "Tax on Fees (%{currency_symbol})" report_header_total_tax: "Total Tax (%{currency_symbol})" + report_header_total_excl_tax: "Total excl. tax (%{currency_symbol})" + report_header_total_incl_tax: "Total incl. tax (%{currency_symbol})" report_header_enterprise: Enterprise report_header_customer: Customer report_header_customer_code: Customer Code diff --git a/lib/reporting/reports/list.rb b/lib/reporting/reports/list.rb index f43e61debd..1fca951b3a 100644 --- a/lib/reporting/reports/list.rb +++ b/lib/reporting/reports/list.rb @@ -20,7 +20,8 @@ module Reporting order_cycle_management: order_cycle_management_report_types, sales_tax: sales_tax_report_types, xero_invoices: xero_report_types, - packing: packing_report_types + packing: packing_report_types, + revenues_by_hub: [], } end diff --git a/lib/reporting/reports/revenues_by_hub/base.rb b/lib/reporting/reports/revenues_by_hub/base.rb new file mode 100644 index 0000000000..e4192600c8 --- /dev/null +++ b/lib/reporting/reports/revenues_by_hub/base.rb @@ -0,0 +1,47 @@ +# frozen_string_literal: true + +module Reporting + module Reports + module RevenuesByHub + class Base < ReportTemplate + def search + permissions = ::Permissions::Order.new(user) + sold_states = %w(complete resumed) + permissions.editable_orders.where(state: sold_states).ransack(ransack_params) + end + + def default_params + { + q: { + completed_at_gt: 1.month.ago.beginning_of_day, + completed_at_lt: 1.day.from_now.beginning_of_day + } + } + end + + def columns + { + hub: proc { |orders| distributor(orders).name }, + hub_id: proc { |orders| distributor(orders).id }, + hub_owner_email: proc { |orders| distributor(orders).owner.email }, + 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) } + } + end + + def query_result + search.result.group_by(&:distributor).values + end + + private + + def distributor(orders) + orders.first.distributor + end + end + end + end +end diff --git a/spec/models/spree/ability_spec.rb b/spec/models/spree/ability_spec.rb index c4f754f6d9..be904d6bce 100644 --- a/spec/models/spree/ability_spec.rb +++ b/spec/models/spree/ability_spec.rb @@ -452,7 +452,7 @@ describe Spree::Ability do it "should not be able to read other reports" do is_expected.not_to have_ability( [:group_buys, :payments, :orders_and_distributors, :users_and_enterprises, - :xero_invoices], for: :report + :xero_invoices, :revenues_by_hub], for: :report ) end @@ -684,7 +684,7 @@ describe Spree::Ability do include_examples "allows access to Enterprise Fee Summary" it "should not be able to read other reports" do - is_expected.not_to have_ability([:users_and_enterprises], + is_expected.not_to have_ability([:users_and_enterprises, :revenues_by_hub], for: :report) end diff --git a/spec/system/admin/reports/revenues_by_hub_spec.rb b/spec/system/admin/reports/revenues_by_hub_spec.rb new file mode 100644 index 0000000000..13a1b96e4e --- /dev/null +++ b/spec/system/admin/reports/revenues_by_hub_spec.rb @@ -0,0 +1,51 @@ +# frozen_string_literal: true + +require 'system_helper' + +describe "Revenues By Hub Reports" do + include AuthenticationHelper + + let(:order) do + create( + :completed_order_with_totals, + completed_at: 2.days.ago, + order_cycle: order_cycle, + distributor: create(:enterprise, name: "Hub 1", + owner: create(:user, email: "email@email.com")), + ) + end + let(:order_cycle) { create(:simple_order_cycle) } + let(:product) { create(:product, supplier: supplier) } + let(:supplier) { create(:supplier_enterprise) } + + before do + create(:line_item_with_shipment, order: order, product: product) + + login_as_admin + visit main_app.admin_report_path(report_type: 'revenues_by_hub') + end + + context "testing report" do + it "show the right values" do + find("[type='submit']").click + + expect(page.find("table.report__table thead tr").text).to have_content([ + "HUB", + "HUB ID", + "HUB OWNER EMAIL", + "TOTAL EXCL. TAX ($)", + "TOTAL TAX ($)", + "TOTAL INCL. TAX ($)" + ].join(" ")) + + expect(page.find("table.report__table tbody tr").text).to have_content([ + "Hub 1", + order.distributor.id, + "email@email.com", + order.total - order.total_tax, + order.total_tax, + order.total + ].compact.join(" ")) + end + end +end