# GetSerialNumberHistory

## Overview

GetSerialNumberHistory retrieves the full transaction history for a serial number — all state transitions, stock movements, and location changes. This provides a complete audit trail ordered chronologically.

## Business Rules

- Requires `serialNumberId`
- Returns an ordered list of InventoryLedger entries referencing this serial number, each containing:
  - `sourceType` — the type of inventory operation (GOODS_RECEIPT, GOODS_ISSUE, etc.)
  - `sourceId` — reference to the originating document (e.g., PurchaseOrder ID, StockAdjustment ID)
  - `executedAt` — when the movement occurred
  - `storageLocationId` — the storage location involved
  - `quantity` — the quantity moved
- Queries all InventoryLedger records where serialNumberId matches
- Results are ordered by executedAt ascending (oldest first)
- Returns error if the serial number does not exist
- Returns an empty history for a newly created serial with no movements

## Process Flow

```mermaid
flowchart TD
    A[Receive serialNumberId] --> B{Serial number exists?}
    B -->|No| C[Return SERIAL_NUMBER_NOT_FOUND error]
    B -->|Yes| D[SELECT from InventoryLedger where serialNumberId matches]
    D --> E[Order by executedAt ascending]
    E --> F{Records found?}
    F -->|Yes| G[Return ordered list of history entries]
    F -->|No| H[Return empty history]
```

## External Dependencies

- None

## Error Scenarios

- **SERIAL_NUMBER_NOT_FOUND**: The specified serial number does not exist

## Test Cases

- returns full history ordered by executedAt ascending
- includes sourceType and storageLocationId per entry
- includes storage location
- includes sourceType/sourceId for traceability
- returns error when serial not found
- returns empty history for newly created serial
