# CancelStockMovement

## Permission Scope

stockOperation

## Overview

cancelStockMovement cancels a stock movement that is in DRAFT or CONFIRMED status. Movements that have already been executed (DONE) cannot be cancelled. Cancellation does not affect any stock levels since stock is only modified during execution.

## Business Rules

- Stock movement must exist
- Stock movement must be in DRAFT or CONFIRMED status (not DONE or already CANCELLED)
- Cancellation does not affect stock levels

## Process Flow

```mermaid
flowchart TD
    A[Receive cancel request] --> B{Movement exists?}
    B -->|No| C[Return error: STOCK_MOVEMENT_NOT_FOUND]
    B -->|Yes| D{Status is DONE?}
    D -->|Yes| E[Return error: STOCK_MOVEMENT_ALREADY_DONE]
    D -->|No| F{Status is CANCELLED?}
    F -->|Yes| G[Return error: STOCK_MOVEMENT_ALREADY_CANCELLED]
    F -->|No| H[Update status to CANCELLED]
    H --> I[Return cancelled movement]
```

## External Dependencies

- None

## Error Scenarios

- **STOCK_MOVEMENT_NOT_FOUND**: Referenced stock movement does not exist
- **STOCK_MOVEMENT_ALREADY_DONE**: Stock movement has already been executed and cannot be cancelled
- **STOCK_MOVEMENT_ALREADY_CANCELLED**: Stock movement has already been cancelled

## Test Cases

- returns error when not found
- returns error when DONE
- returns error when already cancelled
- cancels DRAFT movement
- cancels CONFIRMED movement
- does not affect stock levels
