# UpdateGoodsReceipt

## Permission Scope

inventoryOperation

## Overview

updateGoodsReceipt updates a DRAFT goods receipt InventoryExecution via `headerPatch` and revises its inbound InventoryExecutionLine set via `addLines`, `updateLines`, and `removeLineIds`.

For purchase receipts, prefer `purchase.commands.updateGoodsReceipt` instead of calling `inventory.commands.updateGoodsReceipt` directly. The purchase command is the public purchase-facing entrypoint and keeps callers aligned with the purchase module boundary.

## Business Rules

- The target InventoryExecution must exist, have `executionType=GOODS_RECEIPT`, and be in DRAFT status
- Posted goods receipts cannot be updated
- effectiveDate can be updated independently
- sourceDocumentType and sourceDocumentId are immutable after creation
- `updateLines` and `removeLineIds` must target a line that exists on the execution
- `updateLines` patches the existing line in place, so its id and createdAt are preserved
- At least one line must remain after the changes
- Added and updated lines follow the same validation rules as createGoodsReceipt
- Added and updated lines must provide both source-unit quantity/unitId and converted primaryQuantity/unitConversionRate snapshots
- Added and updated line primaryUnitId must equal Item.unitId
- Added and updated line unitCost must be zero or greater when provided
- No stock or ledger changes occur during update

## Process Flow

```mermaid
flowchart TD
    A[Update goods receipt request] --> B[Lock InventoryExecution]
    B --> C{GOODS_RECEIPT and DRAFT?}
    C -->|No| D[Return error: INVALID_STATUS]
    C -->|Yes| E{Target lines exist?}
    E -->|No| F[Return error: RECEIPT_LINE_NOT_FOUND]
    E -->|Yes| H{At least one line remains?}
    H -->|No| I[Return error: EMPTY_RECEIPT_LINES]
    H -->|Yes| J[Validate added and updated lines]
    J --> K[Apply header updates and line changes]
    K --> M[Return execution and lines]
```

## External Dependencies

- [item-management::Item](../../../item-management/docs/model/Item.md) - Validates referenced items
- [inventory::InventoryExecution](../model/InventoryExecution.md) - Updates the goods receipt execution header and revises inbound execution lines

## Error Scenarios

- **INVALID_STATUS**: Target entity is not in a valid status for this operation
- **RECEIPT_LINE_NOT_FOUND**: Referenced goods receipt line does not exist on the target execution
- **EMPTY_RECEIPT_LINES**: No goods receipt lines were provided
- **INVALID_QUANTITY**: Quantity is zero or negative
- **PRIMARY_UNIT_MISMATCH**: primaryUnitId differs from Item.unitId
- **INVALID_UNIT_CONVERSION_RATE**: Unit conversion rate is zero or negative
- **INVALID_UNIT_COST**: Unit cost is negative
- **ITEM_NOT_FOUND**: Referenced item does not exist
- **STORAGE_LOCATION_NOT_FOUND**: Referenced storage location does not exist
- **LOT_NOT_FOUND**: Referenced lot does not exist
- **SERIAL_NOT_FOUND**: Referenced serial number does not exist

## Test Cases

- updates header metadata on a draft goods receipt
- updates a line in place so its id and createdAt survive the edit
- adds a goods receipt line via addLines
- removes a goods receipt line via removeLineIds
- returns error when goods receipt is not draft
- returns error when an update targets a missing line
- returns error when a removal would leave the goods receipt without lines
- returns error when an added line unit cost is negative
- returns error when an added line primary quantity is zero or negative
- returns error when an added line unit conversion rate is zero or negative
- returns error when an added line primary unit differs from item primary unit
