# TraceLotBackward

## Overview

TraceLotBackward provides backward traceability — given a lot, it returns the originating goods receipt 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 StockMoveLine records where the lot was part of a goods receipt
- Each record includes:
  - Movement date
  - Source (supplier) location
  - Quantity
  - Reference (e.g., purchase order, production order)
- Joins StockMoveLine with StockMovement to obtain date and reference information
- 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 StockMoveLine where lotId matches AND movement type is goods receipt]
    D --> E[JOIN with StockMovement for date and reference]
    E --> F{Records found?}
    F -->|Yes| G[Return list of inbound StockMoveLine records]
    F -->|No| H[Return empty list]
```

## External Dependencies

- None

## Error Scenarios

- **LOT_NOT_FOUND**: The specified 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
