diff --git a/lib/reporting/reports/customers/base.rb b/lib/reporting/reports/customers/base.rb index 09b1cb2170a7..a1c2382e557b 100644 --- a/lib/reporting/reports/customers/base.rb +++ b/lib/reporting/reports/customers/base.rb @@ -11,13 +11,8 @@ def query_result .order(:id)) .group_by do |order| { - first_name: order.billing_address.firstname, - last_name: order.billing_address.lastname, - billing_address: order.billing_address.address_and_city, - email: order.email, - phone: order.billing_address.phone, + customer_id: order.customer_id || order.email, hub_id: order.distributor_id, - shipping_method_id: order.shipping_method&.id, } end.values end @@ -25,14 +20,18 @@ def query_result # rubocop:disable Metrics/AbcSize def columns { - first_name: proc { |orders| orders.first.billing_address.firstname }, - last_name: proc { |orders| orders.first.billing_address.lastname }, - billing_address: proc { |orders| orders.first.billing_address.address_and_city }, - email: proc { |orders| orders.first.email }, - phone: proc { |orders| orders.first.billing_address.phone }, - hub: proc { |orders| orders.first.distributor&.name }, - hub_address: proc { |orders| orders.first.distributor&.address&.address_and_city }, - shipping_method: proc { |orders| orders.first.shipping_method&.name }, + first_name: proc { |orders| last_completed_order(orders).billing_address.firstname }, + last_name: proc { |orders| last_completed_order(orders).billing_address.lastname }, + billing_address: proc { |orders| + last_completed_order(orders).billing_address.address_and_city + }, + email: proc { |orders| last_completed_order(orders).email }, + phone: proc { |orders| last_completed_order(orders).billing_address.phone }, + hub: proc { |orders| last_completed_order(orders).distributor&.name }, + hub_address: proc { |orders| + last_completed_order(orders).distributor&.address&.address_and_city + }, + shipping_method: proc { |orders| last_completed_order(orders).shipping_method&.name }, total_orders: proc { |orders| orders.count }, total_incl_tax: proc { |orders| orders.sum(&:total) }, last_completed_order_date: proc { |orders| last_completed_order_date(orders) }, @@ -75,8 +74,12 @@ def filter_to_order_cycle(orders) end end + def last_completed_order(orders) + orders.max_by(&:completed_at) + end + def last_completed_order_date(orders) - orders.max_by(&:completed_at)&.completed_at&.to_date + last_completed_order(orders).completed_at&.to_date end end end diff --git a/spec/lib/reports/customers_report_spec.rb b/spec/lib/reports/customers_report_spec.rb index 1b4237fee827..481ec09686b9 100644 --- a/spec/lib/reports/customers_report_spec.rb +++ b/spec/lib/reports/customers_report_spec.rb @@ -44,15 +44,18 @@ module Customers let!(:a) { create(:bill_address) } let!(:d){ create(:distributor_enterprise) } let!(:sm) { create(:shipping_method, distributors: [d]) } + let!(:customer) { create(:customer) } let!(:o1) { create(:order_with_totals_and_distribution, :completed, distributor: d, bill_address: a, - shipping_method: sm) + shipping_method: sm, + customer: customer) } let!(:o2) { create(:order_with_totals_and_distribution, :completed, distributor: d, bill_address: a, - shipping_method: sm) + shipping_method: sm, + customer: customer) } before do o1.update(completed_at: "2023-01-01")