# UpdateSalesOrder

## Permission Scope

salesOrder

## Overview

UpdateSalesOrder revises mutable sales-order fields while the order is still editable. It supports draft correction of addresses, payment terms, commercial notes, and line composition before the order is submitted or revised through a future explicit workflow.

## Business Rules

- Only DRAFT orders may be updated directly by this command.
- Updated order lines must still satisfy item, quantity, and pricing validity rules.
- The order must retain at least one line after the update completes.
- Commercial snapshots frozen at confirmation cannot be changed through this command.

## Process Flow

```mermaid
flowchart TD
    A[Receive update request] --> B[Load sales order]
    B --> C{Status is DRAFT?}
    C -->|No| D[Return ORDER_NOT_EDITABLE]
    C -->|Yes| E[Validate updated fields and lines]
    E --> F[Persist order and line changes]
    F --> G[Return updated sales order]
```

## External Dependencies

- [business-partner::GetPartner](../../../business-partner/docs/queries/GetPartner.md) - Revalidate customer changes when customer context is edited.
- [item-management::GetItem](../../../item-management/docs/queries/GetItem.md) - Revalidate changed item references on lines.
- [getSalesPaymentTerm](../queries/GetSalesPaymentTerm.md) - Revalidate changed payment-term assignment.

## Error Scenarios

- **SALES_ORDER_NOT_FOUND**: Referenced sales order does not exist.
- **ORDER_NOT_EDITABLE**: Only DRAFT orders may be updated directly.
- **INVALID_ORDER_LINE**: Order line is missing quantity, price, or item context
- **EMPTY_ORDER_NOT_ALLOWED**: Sales order has no lines

## Test Cases

- updates mutable draft order header fields
- updates draft order lines and preserves line integrity
- returns error when the order does not exist
- returns error when updating a non-draft order
- returns error when the update would remove all lines
