# AssignProfitCenterToHierarchyNode

## Permission Scope

accounting

## Overview

assignProfitCenterToHierarchyNode assigns a profit center to a specific node within a profit center hierarchy. A profit center can be assigned to exactly one node per hierarchy but can be assigned to nodes in multiple hierarchies simultaneously, enabling multi-dimensional reporting.

## Business Rules

- Profit center must exist
- Target hierarchy node must exist
- A profit center can be assigned to exactly one node within a given hierarchy
- A profit center can be assigned to nodes in multiple hierarchies simultaneously
- Assigning to a new node within the same hierarchy replaces the previous assignment
- Emits an audit event recording the hierarchy assignment and acting user

## Process Flow

```mermaid
flowchart TD
    A[Receive assign profit center to hierarchy node request] --> B{Profit center exists?}
    B -->|No| C[Return error: PROFIT_CENTER_NOT_FOUND]
    B -->|Yes| D{Target hierarchy node exists?}
    D -->|No| E[Return error: HIERARCHY_NODE_NOT_FOUND]
    D -->|Yes| F{Already assigned to a node in this hierarchy?}
    F -->|Yes| G[Replace existing assignment with new node]
    F -->|No| H[Create new assignment to target node]
    G --> I[Emit audit event]
    H --> I
    I --> J[Return updated assignment]
```

## External Dependencies

- None

## Error Scenarios

- **PROFIT_CENTER_NOT_FOUND**: Profit center with the specified ID does not exist
- **HIERARCHY_NODE_NOT_FOUND**: Referenced hierarchy node does not exist or does not belong to the same company

## Test Cases

- assigns a profit center to a hierarchy node (creates new assignment)
- replaces existing assignment when profit center is already assigned in the same hierarchy
- allows a profit center to be assigned to nodes in multiple hierarchies
- returns error when profit center does not exist
- returns error when target hierarchy node does not exist
