# GetProfitCenterHierarchy

## Overview

GetProfitCenterHierarchy retrieves a profit center hierarchy by id, returning the full tree structure including all hierarchy nodes (root, intermediate grouping nodes, and leaf nodes) with their parent-child relationships. Each node includes the list of profit centers assigned to it. This query supports hierarchical roll-up profitability reporting, segment analysis, and profit center organization browsing.

## Business Rules

- Accepts a single lookup parameter: `{ id }`
- Returns the hierarchy record with all nodes assembled into a tree structure
- Each node includes its id, name, parent node reference, and list of assigned profit centers
- The root node has no parent reference
- Child nodes are nested under their parent nodes in the tree
- Assigned profit centers on each node include their full master data fields
- A profit center can appear in multiple hierarchies but only once within a given hierarchy
- Returns null if no matching hierarchy is found
- Lookup is an exact match on id

## Process Flow

```mermaid
flowchart TD
    A[Receive input with id] --> B[SELECT from ProfitCenterHierarchy where id = input.id]
    B --> C{Hierarchy found?}
    C -->|Yes| D[Load all hierarchy nodes]
    D --> E[Assemble nodes into tree structure using parent-child references]
    E --> F[Load assigned profit centers for each node]
    F --> G[Return hierarchy with complete tree and profit center assignments]
    C -->|No| H[Return null]
```

## External Dependencies

- None

## Error Scenarios

- **PROFIT_CENTER_HIERARCHY_NOT_FOUND**: No profit center hierarchy matches the given id — caller receives null

## Test Cases

- returns hierarchy with all nodes in tree structure when found by id
- returns hierarchy with root node and nested child nodes
- returns hierarchy nodes with assigned profit centers populated
- returns leaf nodes with no children
- returns null when no hierarchy matches the given id
