Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Fix previous period calculation for whole-month time ranges (#14774)

Damian Legawiec73a078b88b11
diff --git a/spree/core/lib/spree/reporting/adapters/live.rb b/spree/core/lib/spree/reporting/adapters/live.rb
index 9fcca66a87..c04bed5265 100644
--- a/spree/core/lib/spree/reporting/adapters/live.rb
+++ b/spree/core/lib/spree/reporting/adapters/live.rb
@@ -642,12 +642,12 @@ module Spree
end.to_h
end
- # Both periods span the same number of days, so their bucket counts
- # differ by at most one partial edge. Pairing runs from the start —
- # the nth bucket of this period with the nth of the previous — because
- # the ranges share a start offset: a range beginning mid-month pairs
- # its partial first bucket with the previous period's partial first
- # bucket. A bucket past the end of the previous list has no
+ # Both periods span the same number of days (or of whole months), so
+ # their bucket counts differ by at most one partial edge. Pairing runs
+ # from the start — the nth bucket of this period with the nth of the
+ # previous — because the ranges share a start offset: a range
+ # beginning mid-month pairs its partial first bucket with the previous
+ # period's partial first bucket. A bucket past the end of the previous list has no
# counterpart and pairs with nothing rather than wrapping around.
def bucket_pairs(grain)
current = expected_buckets(query.time_range, grain)
diff --git a/spree/core/lib/spree/reporting/query.rb b/spree/core/lib/spree/reporting/query.rb
index 8869ab46a5..49ad2d860d 100644
--- a/spree/core/lib/spree/reporting/query.rb
+++ b/spree/core/lib/spree/reporting/query.rb
@@ -126,12 +126,20 @@ module Spree
# year" means February to February, and an equal-day-count shift off a
# 28-day February would land in January. ActiveSupport maps February 29
# to February 28 in a non-leap year.
+ #
+ # A range of whole calendar months (a full quarter, `last_month`) shifts
+ # by the same number of months for the same reason: July – September is
+ # 92 days, and shifting it by days would start on March 31, putting a
+ # one-day March bucket at the front of the previous period.
def previous_time_range
first = time_range.first.in_time_zone(time_zone)
last = time_range.last.in_time_zone(time_zone)
if compare == 'previous_year'
(first - 1.year)..(last - 1.year)
+ elsif first == first.beginning_of_month && last == last.end_of_month
+ months = (last.year * 12 + last.month) - (first.year * 12 + first.month) + 1
+ (first - months.months)..(last - months.months).end_of_month
else
days = (last.to_date - first.to_date).to_i + 1
(first - days.days)..(last - days.days)
diff --git a/spree/core/spec/lib/spree/reporting/query_spec.rb b/spree/core/spec/lib/spree/reporting/query_spec.rb
index 7ce4d98135..5d39f47574 100644
--- a/spree/core/spec/lib/spree/reporting/query_spec.rb
+++ b/spree/core/spec/lib/spree/reporting/query_spec.rb
@@ -87,6 +87,18 @@ RSpec.describe Spree::Reporting::Query do
expect(previous.first.in_time_zone('Europe/Warsaw').strftime('%F %T')).to eq('2026-03-06 00:00:00')
expect(previous.last.in_time_zone('Europe/Warsaw').strftime('%F %T')).to eq('2026-03-19 23:59:59')
end
+
+ it 'shifts a range of whole calendar months by the same number of months' do
+ query = described_class.new(store: store, params: { metrics: %w[orders], time_range: { since: '2026-07-01', until: '2026-09-30' } })
+
+ previous = query.previous_time_range
+ expect(previous.first.strftime('%F %T')).to eq('2026-04-01 00:00:00')
+ expect(previous.last.strftime('%F %T')).to eq('2026-06-30 23:59:59')
+
+ query = described_class.new(store: store, params: { metrics: %w[orders], time_range: { since: '2026-03-01', until: '2026-03-31' } })
+ expect(query.previous_time_range.first.to_date.to_s).to eq('2026-02-01')
+ expect(query.previous_time_range.last.to_date.to_s).to eq('2026-02-28')
+ end
end
describe 'time presets' do
@@ -705,9 +717,12 @@ RSpec.describe Spree::Reporting::Query do
context 'with a quarter-to-date style range at month grain' do
# Range: the 1st of the month before last → today, spanning three
- # calendar months with the current one partial. Each month bucket must
- # compare with the month three back, never with a month inside the
- # current period.
+ # calendar months. Each month bucket must compare with the month three
+ # back, never with a neighbour. Frozen on the last day of a quarter,
+ # where the range is three whole months and a plain day shift would
+ # start the previous period on March 31.
+ around { |example| Timecop.freeze(Time.zone.parse('2026-09-30 12:00')) { example.run } }
+
let(:from) { 2.months.ago.beginning_of_month.to_date }
let!(:recent_order) { create(:completed_order_with_totals, store: store, completed_at: (from + 45).in_time_zone.change(hour: 12)) }
let!(:previous_order) { create(:completed_order_with_totals, store: store, completed_at: ((from + 45) << 3).in_time_zone.change(hour: 12)) }