# ADR-0005 — Nightly precomputed customer/order statistics snapshot

> Produced by the Reversa **Detective** (phase: interpretation) · doc_level: `complete`
> Retroactive ADR reconstructed from code (`Order::summaryLogging`, `app/Console/Kernel.php`) and Git history: "Deploy | Added to enable Laravel scheduler via cron on dev" (`cbc04cd`), MR!6 "Customer | Updated Statistic & Filter" (`66c233c`).

- **Status:** Accepted (as-built) 🟢
- **Confidence:** 🟢 CONFIRMED (mechanism) / 🟡 (rationale inferred)

## Context

Per-customer statistics (order count, total spend, points, per-category breakdown incl. milk/medicine) are expensive to compute on the fly with a large order/`order_product` history, and are viewed occasionally (customer detail screen) rather than continuously.

## Decision

Precompute the rollup into `customer_order_summary` via `Order::summaryLogging` (a large raw-SQL nested aggregation), run **daily** by the Laravel scheduler (cron-driven, enabled per the deploy commit) and also runnable via artisan. `CustomerController::statis` reads only the snapshot.

## Consequences

- 🟢 The customer statistics screen is a cheap single-row read.
- 🟡 Statistics are **stale by up to a day** — same-day sales are not reflected until the next run. Acceptable for the trend/loyalty use case; not for real-time reporting.
- 🟢 Deployment depends on cron being configured (hence the explicit deploy commit); if the scheduler stops, stats silently freeze.
- 🟡 The aggregation is raw SQL and couples to the milk/medicine special-casing (see ADR-0006).
