# CancelInventoryCount

## Permission Scope

inventoryControl

## Overview

cancelInventoryCount cancels an inventory count session that is in PLANNED or IN_PROGRESS status. Completed counts cannot be cancelled. Cancellation unfreezes any frozen stock movements for the counted locations and discards all count line data.

## Business Rules

- Inventory count must exist
- Inventory count must be in PLANNED or IN_PROGRESS status (not COMPLETED or already CANCELLED)
- Cancellation unfreezes stock movements if the count had frozen them
- All count line data is discarded upon cancellation

## Process Flow

```mermaid
flowchart TD
    A[Receive cancel request] --> B{Count exists?}
    B -->|No| C[Return error: COUNT_NOT_FOUND]
    B -->|Yes| D{Status is COMPLETED?}
    D -->|Yes| E[Return error: COUNT_ALREADY_COMPLETED]
    D -->|No| F{Status is CANCELLED?}
    F -->|Yes| G[Return error: COUNT_ALREADY_CANCELLED]
    F -->|No| H{Count had frozen stock?}
    H -->|Yes| I[Unfreeze stock movements for counted locations]
    I --> J[Update status to CANCELLED]
    H -->|No| J
    J --> K[Return cancelled count]
```

## External Dependencies

- None

## Error Scenarios

- **COUNT_NOT_FOUND**: Referenced inventory count does not exist
- **COUNT_ALREADY_COMPLETED**: Inventory count has already been completed and cannot be cancelled
- **COUNT_ALREADY_CANCELLED**: Inventory count has already been cancelled

## Test Cases

- returns error when not found
- returns error when COMPLETED
- returns error when already cancelled
- cancels PLANNED count
- cancels IN_PROGRESS count
- unfreezes stock movements when count had frozen them
- discards count line data
