# Random

# 📘 Random Pool Selection Engine

This document defines the technical and functional logic of the **Random Pool Selection** engine, designed to ensure total transparency and mathematical determinism in audit processes, validated by the `RandomPoolServiceImpTest` suite.

---

## 1. Audit Transparency Structure

The engine generates an execution summary segmented into 6 key columns, each with a **D|A (Drug | Alcohol)** breakdown:

<table id="bkmrk-column-functional-de" style="width: 100%; border-collapse: collapse; margin-top: 10px;"><thead><tr style="background-color: #f1f5f9;"><th style="border: 1px solid #cbd5e1; padding: 10px; text-align: left;">Column</th><th style="border: 1px solid #cbd5e1; padding: 10px; text-align: left;">Functional Definition</th></tr></thead><tbody><tr><td style="border: 1px solid #cbd5e1; padding: 10px;">**ELIGIBLE (D|A)**</td><td style="border: 1px solid #cbd5e1; padding: 10px;">Active employees (DOT) in their primary organization, filtered by organization type (Solo Drug, Solo Alcohol, or ALL).</td></tr><tr><td style="border: 1px solid #cbd5e1; padding: 10px;">**INACTIVE (D|A)**</td><td style="border: 1px solid #cbd5e1; padding: 10px;">"Rescued" inactive employees when the `INCLUDE_INACTIVE_EMPLOYEE` parameter is active.</td></tr><tr><td style="border: 1px solid #cbd5e1; padding: 10px;">**2nd ORG (D|A)**</td><td style="border: 1px solid #cbd5e1; padding: 10px;">Employees included in the pool due to having this organization as a secondary association in their profile.</td></tr><tr><td style="border: 1px solid #cbd5e1; padding: 10px;">**IGNORED (D|A)**</td><td style="border: 1px solid #cbd5e1; padding: 10px;">Inactive employees existing in the database who were NOT rescued (used for accounting integrity).</td></tr><tr><td style="border: 1px solid #cbd5e1; padding: 10px;">**C/O (D|A)**</td><td style="border: 1px solid #cbd5e1; padding: 10px;">**Carry-Over:** Selection carried forward from the <u>most recent previous cycle</u>, regardless of its completion status (RUNNING or FINISHED).</td></tr><tr style="background-color: #f8fafc; font-weight: bold;"><td style="border: 1px solid #cbd5e1; padding: 10px;">**TARGET (D|A)**</td><td style="border: 1px solid #cbd5e1; padding: 10px;">Formula: (Eligible + Inactive + 2nd Org) \* % + Carry-Over. Rounding: `Math.ceil`.</td></tr></tbody></table>

---

## 2. Exclusion Logic &amp; Eligibility

The system applies exclusion filters to prevent duplicate testing based on organization settings:

- **Secondary Org Priority:** An employee is excluded from their primary organization <u>only</u> if they have an active pool record (NEW or RUNNING) in their secondary organization. If no active conflict exists, they remain in their primary pool.
- **Group Respect:** D|A quotas are assigned strictly according to the `TypeFormGroupRandom` of each organization. If an organization is "SOLO DRUG", its Alcohol columns will always report **0|0**.
- **Deterministic Selection:** The *Preview* and the *Real Execution* process share the same engine, ensuring that visualized results are identical to those processed.

---

## 3. Data Integrity &amp; Constraints

**Uniqueness (Double-Dip Protection):** When an employee is selected for both Drug and Alcohol (ALL mode), the system generates a single physical selection record (RandomPoolPerson) to prevent database violations, while maintaining traceability for both categories in summary counters (BOTH Category).

---

## 4. Security Validations

The process will abort with a controlled `RuntimeException` if:

1. The total eligible population (Active + Rescued Inactive + 2nd Org) is zero.
2. No **Stakeholders** (Service Providers) are assigned to perform "Random Pool" tests for the organizations.
3. The selection percentages exceed allowed limits (0-100%).