# TraceLotBackward

## Overview

TraceLotBackward provides backward traceability — given a lot, it returns the originating goods receipt InventoryLedger entries and supplier information. This is used for root cause analysis to identify where a lot's items originated.

## Business Rules

- Requires `lotId`
- Returns all inbound InventoryLedger records where the lot was part of a goods receipt (sourceType=GOODS_RECEIPT)
- Each record includes:
  - Executed date (executedAt)
  - Source (supplier) location
  - Quantity
  - Reference to the originating document (via sourceType/sourceId)
- Returns error if the lot does not exist
- Returns an empty list if the lot exists but has no inbound receipt records

## Process Flow

```mermaid
flowchart TD
    A[Receive lotId] --> B{Lot exists?}
    B -->|No| C[Return LOT_NOT_FOUND error]
    B -->|Yes| D[SELECT from InventoryLedger where lotId matches AND sourceType is GOODS_RECEIPT]
    D --> E{Records found?}
    E -->|Yes| F[Return list of inbound InventoryLedger records]
    E -->|No| G[Return empty list]
```

## External Dependencies

- None

## Error Scenarios

- **LOT_NOT_FOUND**: Referenced lot does not exist

## Test Cases

- returns inbound movements for lot
- includes movement date and source reference
- returns error when lot not found
- returns empty list when no receipt found
