# CreateSalesOrder

## Permission Scope

salesOrder

## Overview

CreateSalesOrder establishes a new sales order in DRAFT status with one or more order lines. It captures the initial customer, company, address, payment-term, pricing, and optional channel context needed to begin the commercial review flow.

## Business Rules

- Order is always created in DRAFT status.
- Customer must reference an ACTIVE business partner with the CUSTOMER role.
- At least one order line is required at creation time.
- Each order line must reference an ACTIVE item, a positive quantity, and a non-negative unit price.
- All customer, item, site, and channel references must belong to the same company scope.

## Process Flow

```mermaid
flowchart TD
    A[Receive create request] --> B[Validate company and customer]
    B --> C[Validate order lines and references]
    C --> D[Resolve optional pricing and payment defaults]
    D --> E[Create SalesOrder and SalesOrderLine records]
    E --> F[Set status to DRAFT]
    F --> G[Return created sales order]
```

## External Dependencies

- [business-partner::GetPartner](../../../business-partner/docs/query/GetPartner.md) - Validate the referenced customer exists and is active.
- [item-management::GetItem](../../../item-management/docs/query/GetItem.md) - Validate each referenced sellable item.
- [organization::GetSite](../../../organization/docs/query/GetSite.md) - Validate ship-from or billing-site references when provided.
- [getApplicableSalesPrice](../query/GetApplicableSalesPrice.md) - Optionally resolve default draft pricing context.
- [getSalesPaymentTerm](../query/GetSalesPaymentTerm.md) - Optionally resolve default payment-term context.

## Error Scenarios

- **CUSTOMER_NOT_FOUND**: Referenced customer does not exist.
- **CUSTOMER_NOT_ACTIVE**: Referenced customer is not in ACTIVE status
- **CUSTOMER_ROLE_REQUIRED**: Referenced partner does not carry the CUSTOMER role.
- **ITEM_NOT_FOUND**: One or more referenced items do not exist
- **INVALID_ORDER_LINE**: Order line is missing quantity, price, or item context
- **CROSS_COMPANY_REFERENCE**: One or more referenced records belong to a different company

## Test Cases

- creates a draft sales order with valid customer and lines
- throws when customer does not exist
- throws when customer is inactive or lacks CUSTOMER role
- throws when an order line references an inactive item
- throws when line quantity is zero or unit price is negative
- throws when references cross company boundaries
