# 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 downstream fulfillment progress or downstream billing progress cannot be cancelled through this command.
- Draft, submitted, and confirmed 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 fulfillment and billing progress]
    C --> D{Downstream evidence exists?}
    D -->|Yes| E[Return CANCELLATION_BLOCKED]
    D -->|No| F[Set orderStatus to CANCELLED]
    F --> G[Return cancelled order id]
```

## 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**: Fulfillment progress or downstream billing progress already exists.

## Test Cases

- cancels a draft order
- cancels a confirmed order with no fulfillment or billing progress
- throws when fulfillment progress exists
- throws when billing progress exists
- throws when the order does not exist
