# StockAdjustment

## Description

StockAdjustment represents an inventory adjustment request that follows a submit-then-confirm workflow. Adjustments cover four types: CORRECTION (fix quantity errors), SCRAP (write off damaged/expired stock), BLOCK (quarantine stock), and UNBLOCK (release quarantined stock). Each adjustment requires a reasonCode explaining why the change is needed.

A StockAdjustment progresses through a simple lifecycle: it is created as a DRAFT, submitted for review (SUBMITTED), then either confirmed or rejected. Confirmation executes the actual stock changes — creating InventoryLedger entries and updating StockLevel. A draft adjustment can be cancelled, and a rejected adjustment can be updated and resubmitted.

## Domain Model Definitions

### Model type

Stateful

#### State Transitions

```mermaid
stateDiagram-v2
    [*] --> Draft: createStockAdjustment
    Draft --> Submitted: submitStockAdjustment
    Draft --> Draft: updateStockAdjustment
    Draft --> Cancelled: cancelStockAdjustment
    Submitted --> Confirmed: confirmStockAdjustment
    Submitted --> Rejected: rejectStockAdjustment
    Rejected --> Draft: updateStockAdjustment (resubmit)
```

| Operation | From | To | Command |
|-----------|------|----|---------|
| submit | DRAFT | SUBMITTED | [submitStockAdjustment](../commands/SubmitStockAdjustment.md) |
| cancel | DRAFT | CANCELLED | [cancelStockAdjustment](../commands/CancelStockAdjustment.md) |
| confirm | SUBMITTED | CONFIRMED | [confirmStockAdjustment](../commands/ConfirmStockAdjustment.md) |
| reject | SUBMITTED | REJECTED | [rejectStockAdjustment](../commands/RejectStockAdjustment.md) |
| resubmit | REJECTED | DRAFT | [updateStockAdjustment](../commands/UpdateStockAdjustment.md) |

### Command Definitions

- [createStockAdjustment](../commands/CreateStockAdjustment.md) - Create a new stock adjustment in DRAFT status with adjustment type, reason code, and lines
- [updateStockAdjustment](../commands/UpdateStockAdjustment.md) - Update a DRAFT or REJECTED adjustment (modify lines, reason code); REJECTED transitions back to DRAFT
- [submitStockAdjustment](../commands/SubmitStockAdjustment.md) - Submit a draft adjustment for review, transitioning to SUBMITTED
- [confirmStockAdjustment](../commands/ConfirmStockAdjustment.md) - Approve and execute a submitted adjustment, creating InventoryLedger entries and updating StockLevel
- [rejectStockAdjustment](../commands/RejectStockAdjustment.md) - Reject a submitted adjustment, transitioning to REJECTED
- [cancelStockAdjustment](../commands/CancelStockAdjustment.md) - Cancel a draft adjustment, transitioning to CANCELLED

### Query Definitions

- [getStockAdjustment](../queries/GetStockAdjustment.md) - Retrieve a stock adjustment by id
- [listStockAdjustments](../queries/ListStockAdjustments.md) - List stock adjustments with optional filters by status, adjustmentType, date range

### Models

- StockAdjustment

### Invariants

- adjustmentType must be one of: CORRECTION, SCRAP, BLOCK, UNBLOCK
- reasonCode is required for all adjustments
- Only DRAFT adjustments can be submitted
- Only SUBMITTED adjustments can be confirmed or rejected
- Only DRAFT adjustments can be cancelled
- A rejected adjustment returns to DRAFT via updateStockAdjustment and can be resubmitted
- Confirmation creates InventoryLedger entries for each line and updates StockLevel
- Must have at least one StockAdjustmentLine before submission

### Relationships

- **Has many StockAdjustmentLine**: Each adjustment contains one or more lines specifying item-location quantity changes
- **Creates InventoryLedger**: On confirmation, each line generates an InventoryLedger entry with the corresponding sourceType
