44-- $1 timestamptz - inclusive reporting range start
55-- $2 timestamptz - exclusive reporting range end
66--
7- -- A paid event is a PAYMENT winning whose current payment status is PAID.
8- -- date_paid is authoritative when present; created_at supports migrated paid rows
9- -- that do not have a paid timestamp. TOPGEAR_PAYMENT is intentionally excluded
10- -- because it is a separate canonical accrual bucket and is not one of the four
11- -- dashboard categories.
7+ -- The report is a financial projection, so the latest non-cancelled payment
8+ -- record is counted in its creation month even when payout is still on hold or
9+ -- owed. TOPGEAR_PAYMENT is intentionally excluded because it is a separate
10+ -- canonical accrual bucket and is not one of the four dashboard categories.
1211WITH bounds AS (
1312 SELECT
1413 $1 ::timestamptz AT TIME ZONE ' UTC' AS start_at,
@@ -22,10 +21,17 @@ months AS (
2221 ) AS month_start
2322 FROM bounds b
2423),
25- paid_events AS MATERIALIZED (
24+ latest_payment_versions AS MATERIALIZED (
25+ SELECT
26+ p .winnings_id ,
27+ MAX (p .version ) AS max_version
28+ FROM finance .payment p
29+ GROUP BY p .winnings_id
30+ ),
31+ payment_events AS MATERIALIZED (
2632 SELECT
2733 NULLIF(TRIM (w .winner_id ), ' ' ) AS member_id,
28- COALESCE( p . date_paid , p . created_at ) AS paid_at ,
34+ p . created_at AS activity_at ,
2935 CASE
3036 WHEN w .category ::text = ' TAAS_PAYMENT' THEN ' taas'
3137 WHEN w .category ::text = ' ENGAGEMENT_PAYMENT' THEN ' engagement'
@@ -39,17 +45,20 @@ paid_events AS MATERIALIZED (
3945 ELSE ' challenge'
4046 END AS payment_type
4147 FROM finance .payment p
48+ JOIN latest_payment_versions lpv
49+ ON lpv .winnings_id = p .winnings_id
50+ AND lpv .max_version = p .version
4251 JOIN finance .winnings w
4352 ON w .winning_id = p .winnings_id
44- WHERE p .payment_status = ' PAID '
53+ WHERE p .payment_status IS DISTINCT FROM ' CANCELLED '
4554 AND w .type = ' PAYMENT'
46- AND COALESCE( p . date_paid , p . created_at ) IS NOT NULL
55+ AND p . created_at IS NOT NULL
4756 AND NULLIF(TRIM (w .winner_id ), ' ' ) IS NOT NULL
4857 AND w .category ::text IS DISTINCT FROM ' TOPGEAR_PAYMENT'
4958),
5059selected_months AS (
5160 SELECT
52- DATE_TRUNC(' month' , pe .paid_at ) AS month_start,
61+ DATE_TRUNC(' month' , pe .activity_at ) AS month_start,
5362 COUNT (DISTINCT pe .member_id ) FILTER (
5463 WHERE pe .payment_type = ' taas'
5564 ) AS taas,
@@ -62,18 +71,18 @@ selected_months AS (
6271 COUNT (DISTINCT pe .member_id ) FILTER (
6372 WHERE pe .payment_type = ' engagement'
6473 ) AS engagement
65- FROM paid_events pe
74+ FROM payment_events pe
6675 CROSS JOIN bounds b
67- WHERE pe .paid_at >= b .start_at
68- AND pe .paid_at < b .end_at
69- GROUP BY DATE_TRUNC(' month' , pe .paid_at )
76+ WHERE pe .activity_at >= b .start_at
77+ AND pe .activity_at < b .end_at
78+ GROUP BY DATE_TRUNC(' month' , pe .activity_at )
7079),
7180all_time_months AS (
7281 SELECT
73- DATE_TRUNC(' month' , pe .paid_at ) AS month_start,
82+ DATE_TRUNC(' month' , pe .activity_at ) AS month_start,
7483 COUNT (DISTINCT pe .member_id ) AS unique_members
75- FROM paid_events pe
76- GROUP BY DATE_TRUNC(' month' , pe .paid_at )
84+ FROM payment_events pe
85+ GROUP BY DATE_TRUNC(' month' , pe .activity_at )
7786),
7887peak_month AS (
7988 SELECT
@@ -98,7 +107,7 @@ all_time_summary AS (
98107 COUNT (DISTINCT pe .member_id ) FILTER (
99108 WHERE pe .payment_type = ' engagement'
100109 ) AS engagement_unique_members
101- FROM paid_events pe
110+ FROM payment_events pe
102111)
103112SELECT
104113 TO_CHAR(m .month_start , ' YYYY-MM-01' ) AS month,
0 commit comments