# 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](../commands/IssueSalesInvoice.md) |
| send | ISSUED | SENT | [sendSalesInvoice](../commands/SendSalesInvoice.md) |
| cancel | DRAFT, ISSUED | CANCELLED | [cancelSalesInvoice](../commands/CancelSalesInvoice.md) |

### Command Definitions

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

### Query Definitions

- [getSalesInvoice](../queries/GetSalesInvoice.md) - Retrieve one invoice with lines, due schedule, and status metadata.
- [listSalesInvoicesBySalesOrder](../queries/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.
