Skip to content

Commit

Permalink
Group all orders by customer_id, email and distributor_id
Browse files Browse the repository at this point in the history
Therefore have one (and only) row per customer

Co-Authored-By: Maikel <maikel@email.org.au>
  • Loading branch information
jibees and mkllnk committed Sep 22, 2023
1 parent 933811c commit a7ec97f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
33 changes: 18 additions & 15 deletions lib/reporting/reports/customers/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,27 @@ 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

# 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) },
Expand Down Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions spec/lib/reports/customers_report_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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:)
}
let!(:o2) {
create(:order_with_totals_and_distribution, :completed, distributor: d,
bill_address: a,
shipping_method: sm)
shipping_method: sm,
customer:)
}
before do
o1.update(completed_at: "2023-01-01")
Expand Down

0 comments on commit a7ec97f

Please sign in to comment.