# CloseSalesOrder

## Permission Scope

salesOrder

## Overview

CloseSalesOrder marks the commercial commitment as complete after its fulfillment and billing obligations are complete.

## Business Rules

- Only a confirmed order may be closed.
- Every physical line must be fully fulfilled.
- Every line, including non-physical service lines, must be fully billed.
- Closing the order must preserve line-level fulfilled and billed history.

## Process Flow

```mermaid
flowchart TD
    A[Receive close request] --> B[Load sales order]
    B --> C[Calculate remaining open balance]
    C --> D{Open balance cleared?}
    D -->|No| E[Return ORDER_NOT_CLOSABLE]
    D -->|Yes| F[Set orderStatus to CLOSED]
    F --> G[Return closed 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
- **ORDER_NOT_CLOSABLE**: Remaining open balance still exists.

## Test Cases

- closes an order with no remaining open balance
- does not require shipment progress for a fully billed service line
- throws when remaining quantity is still open
- throws when order status does not allow closure
- throws when the order does not exist
