From c6c9cdca6557f19b2d4230d8b249001dded3b5b5 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Bellet Date: Mon, 10 Jul 2023 13:39:51 +0200 Subject: [PATCH] Add last completed order date --- config/locales/en.yml | 1 + lib/reporting/reports/customers/addresses.rb | 5 +++++ spec/lib/reports/customers_report_spec.rb | 21 +++++++++++--------- spec/system/admin/reports_spec.rb | 3 ++- 4 files changed, 20 insertions(+), 10 deletions(-) diff --git a/config/locales/en.yml b/config/locales/en.yml index e82f92235a..f676290ebe 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -3107,6 +3107,7 @@ See the %{link} to find out more about %{sitename}'s features and to start using report_header_total_taxable_admin: Total taxable admin adjustments (tax inclusive) report_line_cost_of_produce: Cost of produce report_line_line_items: line items + report_header_last_completed_order_date: Last completed order date report_xero_configuration: Xero Configuration initial_invoice_number: "Initial invoice number" invoice_date: "Invoice date" diff --git a/lib/reporting/reports/customers/addresses.rb b/lib/reporting/reports/customers/addresses.rb index cb3936ba94..a5540e40d3 100644 --- a/lib/reporting/reports/customers/addresses.rb +++ b/lib/reporting/reports/customers/addresses.rb @@ -19,6 +19,7 @@ module Reporting end # rubocop:disable Metrics/AbcSize + # rubocop:disable Metrics/CyclomaticComplexity def columns { first_name: proc { |orders| orders.first.billing_address.firstname }, @@ -31,9 +32,13 @@ module Reporting shipping_method: proc { |orders| orders.first.shipping_method&.name }, total_orders: proc { |orders| orders.count }, total_incl_tax: proc { |orders| orders.sum(&:total) }, + last_completed_order_date: proc { |orders| + orders.max_by(&:completed_at)&.completed_at&.to_date + }, } end # rubocop:enable Metrics/AbcSize + # rubocop:enable Metrics/CyclomaticComplexity def skip_duplicate_rows? true diff --git a/spec/lib/reports/customers_report_spec.rb b/spec/lib/reports/customers_report_spec.rb index f1a0187f03..529664b97c 100644 --- a/spec/lib/reports/customers_report_spec.rb +++ b/spec/lib/reports/customers_report_spec.rb @@ -65,7 +65,8 @@ module Reporting it "returns headers for addresses" do expect(subject.table_headers).to eq(["First Name", "Last Name", "Billing Address", "Email", "Phone", "Hub", "Hub Address", - "Shipping Method", "Total Number of Orders", "Total incl. tax ($)"]) + "Shipping Method", "Total Number of Orders", "Total incl. tax ($)", + "Last completed order date"]) end it "builds a table from a list of variants" do @@ -81,7 +82,7 @@ module Reporting o.email, a.phone, d.name, [d.address.address1, d.address.address2, d.address.city].join(" "), - o.shipping_method.name, 1, o.total + o.shipping_method.name, 1, o.total, "none" ]]) end @@ -100,12 +101,14 @@ module Reporting shipping_method: sm) } before do + o1.update(completed_at: Time.zone.yesterday) + o2.update(completed_at: Time.zone.today) [o1, o2].each do |order| order.update!(email: "test@test.com") end end - it "returns only one row per customer and count the number of orders" do + it "returns only one row per customer with the right data" do expect(subject.query_result).to match_array [[o1, o2]] expect(subject.table_rows.size).to eq(1) expect(subject.table_rows) @@ -114,7 +117,7 @@ module Reporting [a.address1, a.address2, a.city].join(" "), o1.email, a.phone, d.name, [d.address.address1, d.address.address2, d.address.city].join(" "), - o1.shipping_method.name, 2, o1.total + o2.total + o1.shipping_method.name, 2, o1.total + o2.total, o2.completed_at.strftime("%Y-%m-%d") ]]) end @@ -136,13 +139,13 @@ module Reporting [a.address1, a.address2, a.city].join(" "), o1.email, a.phone, d.name, [d.address.address1, d.address.address2, d.address.city].join(" "), - o1.shipping_method.name, 1, o1.total + o1.shipping_method.name, 1, o1.total, o1.completed_at.strftime("%Y-%m-%d") ], [ a.firstname, a.lastname, [a.address1, a.address2, a.city].join(" "), o2.email, a.phone, d2.name, [d2.address.address1, d2.address.address2, d2.address.city].join(" "), - o2.shipping_method.name, 1, o2.total + o2.shipping_method.name, 1, o2.total, o2.completed_at.strftime("%Y-%m-%d") ]]) end end @@ -161,7 +164,7 @@ module Reporting context "when the shipping method column is being included" do let(:fields_to_show) do [:first_name, :last_name, :billing_address, :email, :phone, :hub, :hub_address, - :shipping_method, :total_orders, :total_incl_tax] + :shipping_method, :total_orders, :total_incl_tax, :last_completed_order_date] end subject { Addresses.new(user, { fields_to_show: }) } @@ -178,7 +181,7 @@ module Reporting a.phone, d.name, [d.address.address1, d.address.address2, d.address.city].join(" "), - o1.shipping_method.name, 1, o1.total + o1.shipping_method.name, 1, o1.total, o1.completed_at.strftime("%Y-%m-%d") ], [ a.firstname, @@ -188,7 +191,7 @@ module Reporting a.phone, d.name, [d.address.address1, d.address.address2, d.address.city].join(" "), - sm2.name, 1, o2.total + sm2.name, 1, o2.total, o2.completed_at.strftime("%Y-%m-%d") ] ] ) diff --git a/spec/system/admin/reports_spec.rb b/spec/system/admin/reports_spec.rb index 5bc9f1d843..447e557e40 100644 --- a/spec/system/admin/reports_spec.rb +++ b/spec/system/admin/reports_spec.rb @@ -173,7 +173,8 @@ describe ' table = rows.map { |r| r.all("th").map { |c| c.text.strip } } expect(table.sort).to eq([ ["First Name", "Last Name", "Billing Address", "Email", "Phone", "Hub", "Hub Address", - "Shipping Method", "Total Number of Orders", "Total incl. tax ($)"].map(&:upcase) + "Shipping Method", "Total Number of Orders", "Total incl. tax ($)", + "Last completed order date"].map(&:upcase) ].sort) end end