Merge pull request #9521 from jibees/create-new-report-revenues-by-hub

Create a new report that shows revenues (incl. or excl. taxes) by hub only available for super-admin
This commit is contained in:
Rachel Arnould
2022-09-08 16:10:14 +02:00
committed by GitHub
6 changed files with 111 additions and 3 deletions

View File

@@ -0,0 +1,2 @@
= render 'admin/reports/date_range_form', f: f

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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