# SalesInvoice

## Description

SalesInvoice is the customer-facing receivable document issued from the sales domain. It snapshots invoice identity, billing policy outcome, price and tax context, payment-term output, and customer references so downstream accounts receivable can consume a stable commercial document.

## Domain Model Definitions

### Model type

Stateful

#### State Transitions

```mermaid
stateDiagram-v2
    [*] --> Draft: createSalesInvoice
    Draft --> Issued: issueSalesInvoice
    Issued --> Sent: sendSalesInvoice
    Draft --> Cancelled: cancelSalesInvoice
    Issued --> Cancelled: cancelSalesInvoice
```

| Operation | From | To | Command |
|-----------|------|----|---------|
| issue | DRAFT | ISSUED | [issueSalesInvoice](../command/IssueSalesInvoice.md) |
| send | ISSUED | SENT | [sendSalesInvoice](../command/SendSalesInvoice.md) |
| cancel | DRAFT, ISSUED | CANCELLED | [cancelSalesInvoice](../command/CancelSalesInvoice.md) |

### Command Definitions

- [createSalesInvoice](../command/CreateSalesInvoice.md) - Create a draft invoice with references to the customer, order, and invoice lines.
- [updateSalesInvoice](../command/UpdateSalesInvoice.md) - Correct mutable invoice fields while the document remains draft.
- [issueSalesInvoice](../command/IssueSalesInvoice.md) - Validate billing policy, snapshot receivable metadata, and issue the invoice.
- [sendSalesInvoice](../command/SendSalesInvoice.md) - Record customer-facing delivery metadata after issuance.
- [cancelSalesInvoice](../command/CancelSalesInvoice.md) - Cancel a draft or issued invoice before downstream AR handoff acceptance.

### Query Definitions

- [getSalesInvoice](../query/GetSalesInvoice.md) - Retrieve one invoice with lines, due schedule, and status metadata.
- [listSalesInvoicesBySalesOrder](../query/ListSalesInvoicesBySalesOrder.md) - List all invoices issued against one sales order.

### Models

- SalesInvoice
- SalesInvoiceLine

### Invariants

- Every invoice belongs to exactly one company and one customer.
- An issued invoice preserves external invoice number, invoice date, payment-term snapshot, customer tax identity, taxable amount, and tax amount.
- Invoice quantities and prices must validate against the originating sales order and, when required, posted shipment evidence.
- A cancelled invoice preserves historical references and may not delete or silently rewrite issued billing evidence.

### Relationships

- **Owns SalesInvoiceLine**: Invoice lines hold the detailed billed quantities and prices.
- **References SalesOrder**: `salesOrderId` links the invoice to the commercial commitment being billed.
- **References Shipment**: Shipment-gated billing may cite posted shipment evidence.
- **References SalesPaymentTerm**: The invoice snapshots the assigned term and derived due schedule.
- **Referenced By SalesCreditNote**: Credit notes correct previously issued invoice evidence without mutating the original invoice.
