# CancelSalesOrder

## Permission Scope

salesOrder

## Overview

CancelSalesOrder voids a sales order that should no longer proceed. It preserves audit history while preventing further execution on the commercial document.

## Business Rules

- Orders with posted shipment or issued invoice evidence cannot be cancelled through this command.
- Draft, submitted, confirmed, and held orders may be cancelled when irreversible downstream execution has not occurred.
- Cancellation must preserve historical order and line references for audit purposes.

## Process Flow

```mermaid
flowchart TD
    A[Receive cancel request] --> B[Load sales order]
    B --> C[Check shipment and invoice progress]
    C --> D{Downstream evidence exists?}
    D -->|Yes| E[Return CANCELLATION_BLOCKED]
    D -->|No| F[Set status to CANCELLED]
    F --> G[Return cancelled order]
```

## External Dependencies

- None

## Error Scenarios

- **SALES_ORDER_NOT_FOUND**: Referenced sales order does not exist.
- **INVALID_ORDER_STATUS**: Sales order status does not allow this operation
- **CANCELLATION_BLOCKED**: Posted shipment or issued invoice evidence already exists.

## Test Cases

- cancels a draft order
- cancels a confirmed order with no shipment or invoice evidence
- throws when posted shipment evidence exists
- throws when issued invoice evidence exists
