# customer-purchase-statistics Specification

## Purpose

Category-based purchase statistics shown on the customer's "Thống kê nhanh" tab (Sữa, Tiêu dùng, Thuốc boxes) plus the shared last-updated indicator for that batch-computed data.

## Requirements

### Requirement: Medicine category statistics box
The customer statistics tab SHALL display a "THỐNG KÊ - THUỐC" box below the "THỐNG KÊ - TIÊU DÙNG" box, listing quantity and total amount purchased for products in category_id `8`, using the same row shape (category name, quantity, amount) as the "THỐNG KÊ - TIÊU DÙNG" box.

#### Scenario: Customer has purchased medicine-category products
- **WHEN** the customer's pre-computed summary includes purchases in category_id `8`
- **THEN** the "THỐNG KÊ - THUỐC" box shows one row with the category name, summed quantity, and summed amount for that category

#### Scenario: Customer has no medicine-category purchases
- **WHEN** the customer's pre-computed summary has no entries for category_id `8`
- **THEN** the "THỐNG KÊ - THUỐC" box renders with no data rows, without error

### Requirement: Medicine category included in daily summary computation
The daily customer order summary batch job SHALL include category_id `8` in its per-category aggregation, computing quantity and amount totals per customer the same way it already does for the Sữa and Tiêu dùng category groups.

#### Scenario: Daily batch job runs
- **WHEN** the scheduled (or manually triggered) customer order summary job runs
- **THEN** the resulting per-customer summary data includes a category_id `8` group with summed quantity and amount, alongside the existing Sữa and Tiêu dùng groups

### Requirement: Shared last-updated indicator for statistics column
The customer statistics tab SHALL display a single "last updated" timestamp, positioned above the "THỐNG KÊ - SỮA" title at the top of the statistics column, reflecting when the underlying batch job last computed the customer's summary data. This single timestamp applies to all boxes in that column (Sữa, Tiêu dùng, Thuốc).

#### Scenario: Summary has been computed at least once
- **WHEN** the customer's summary record has a non-null last-updated timestamp
- **THEN** the statistics column shows that timestamp once, above "THỐNG KÊ - SỮA", and no per-box timestamps are shown

#### Scenario: Summary has never been computed for this customer
- **WHEN** the customer's summary record has a null last-updated timestamp (e.g., no batch run has processed this customer yet)
- **THEN** the statistics column does not display a misleading or blank timestamp; it indicates the data has not been computed yet

### Requirement: Daily summary job records computation timestamp
The daily customer order summary batch job SHALL record the time of each computation run on the customer's summary record (both on first creation and on every subsequent update), so that record's timestamp reflects when its data was last computed.

#### Scenario: First-time computation for a customer
- **WHEN** the batch job computes a summary for a customer with no existing summary record
- **THEN** the new summary record is created with both created and last-updated timestamps set to the run time

#### Scenario: Re-computation for an existing customer
- **WHEN** the batch job re-computes a summary for a customer with an existing summary record
- **THEN** the existing record's last-updated timestamp is set to the current run time
