# UpdateSalesCreditNote

## Permission Scope

salesBilling

## Overview

UpdateSalesCreditNote revises draft credit-note content before issue. It supports correction of quantities, amounts, references, and reason codes while the document remains draft.

## Business Rules

- Only DRAFT credit notes may be updated directly.
- Updated correction lines must still reference valid issued invoice lines.
- Each `updateLines` or `removeLineIds` entry must target an existing credit-note line.
- In-place line updates preserve the line's id and createdAt.

## Process Flow

```mermaid
flowchart TD
    A[Receive update request] --> B[Load credit note]
    B --> C{Status is DRAFT?}
    C -->|No| D[Return CREDIT_NOTE_NOT_EDITABLE]
    C -->|Yes| E[Validate updated correction content]
    E --> F[Persist updates]
    F --> G[Return updated credit note]
```

## External Dependencies

- [getSalesCreditNote](../query/GetSalesCreditNote.md) - Load current credit-note state before applying changes.

## Error Scenarios

- **SALES_CREDIT_NOTE_NOT_FOUND**: Referenced sales credit note does not exist.
- **CREDIT_NOTE_NOT_EDITABLE**: Only DRAFT credit notes may be updated.
- **INVALID_CREDIT_NOTE_LINE**: Credit note correction line or amount is invalid
- **LINE_NOT_FOUND**: Referenced line does not exist on the target document
- **EMPTY_SALES_CREDIT_NOTE_LINES**: No sales-credit-note lines were provided

## Test Cases

- updates draft credit-note metadata
- updates a line in place so its id and createdAt survive the edit
- adds a credit-note line via addLines
- removes a credit-note line via removeLineIds
- returns error when credit note does not exist
- returns error when updating an issued credit note
- returns error when an update targets a missing line
- returns error when a removal would leave the credit note without lines
