# CreateSalesInvoice

## Permission Scope

salesBilling

## Overview

CreateSalesInvoice creates a draft customer invoice that references one sales order and one or more invoice lines. It captures the draft billing document before issue-time validation and snapshotting occur.

## Business Rules

- Referenced sales order must exist and belong to the same company as the invoice.
- At least one invoice line is required.
- Each invoice line must reference a valid sales-order line and a billed quantity greater than zero.

## Process Flow

```mermaid
flowchart TD
    A[Receive create request] --> B[Load sales order]
    B --> C[Validate invoice lines and customer context]
    C --> D[Insert draft invoice and invoice lines]
    D --> E[Return draft invoice]
```

## External Dependencies

- [getSalesOrder](../queries/GetSalesOrder.md) - Validate the source order and customer context.
- [getSalesPaymentTerm](../queries/GetSalesPaymentTerm.md) - Resolve the assigned payment term for due schedule generation.

## Error Scenarios

- **SALES_ORDER_NOT_FOUND**: Referenced sales order does not exist.
- **INVALID_INVOICE_LINE**: Invoice line content is incomplete or invalid
- **CROSS_COMPANY_REFERENCE**: One or more referenced records belong to a different company

## Test Cases

- creates a draft invoice for a valid order
- throws when no invoice lines are provided
- throws when invoice line references an unknown sales-order line
- throws when order belongs to another company
