> ## Documentation Index
> Fetch the complete documentation index at: https://docs.encoreos.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Recovery Housing Entity Relationship Diagram

> > Module: Recovery Housing > Prefix: rh_ > Tables: 51 > Last Updated: 2026-01-11

> **Module:** Recovery Housing\
> **Prefix:** `rh_`\
> **Tables:** 51\
> **Last Updated:** 2026-01-11

## Overview

The Recovery Housing module manages residential recovery programs including bed/room inventory, resident episodes, program phases, milestones, and billing integration.

## Sites & Beds ERD

```mermaid theme={null}
erDiagram
    pf_sites ||--o{ rh_buildings : contains
    rh_buildings ||--o{ rh_rooms : contains
    rh_rooms ||--o{ rh_beds : contains
    rh_beds ||--o{ rh_bed_assignments : assigned
    
    rh_buildings {
        uuid id PK
        uuid organization_id FK
        uuid site_id FK
        string name
        string address
        integer total_beds
        enum status
        jsonb amenities
        jsonb custom_fields
    }
    
    rh_rooms {
        uuid id PK
        uuid building_id FK
        string room_number
        integer bed_count
        enum room_type
        enum status
        decimal daily_rate
        jsonb amenities
    }
    
    rh_beds {
        uuid id PK
        uuid room_id FK
        string bed_label
        enum status
        boolean is_available
        jsonb custom_fields
    }
    
    rh_bed_assignments {
        uuid id PK
        uuid bed_id FK
        uuid resident_id FK
        uuid episode_id FK
        date start_date
        date end_date
        enum status
    }
```

## Residents & Episodes ERD

```mermaid theme={null}
erDiagram
    rh_residents ||--o{ rh_episodes : "has episodes"
    rh_residents ||--o{ rh_resident_contacts : "has contacts"
    rh_residents ||--o{ rh_resident_documents : "has documents"
    rh_episodes ||--o{ rh_episode_phases : "progresses through"
    rh_episodes ||--o{ rh_bed_assignments : has
    
    rh_residents {
        uuid id PK
        uuid organization_id FK
        string resident_number
        string first_name
        string last_name
        date date_of_birth
        string phone
        string email
        enum status
        jsonb demographics
        jsonb custom_fields
    }
    
    rh_episodes {
        uuid id PK
        uuid resident_id FK
        uuid organization_id FK
        uuid site_id FK
        uuid program_id FK
        date admission_date
        date discharge_date
        enum status
        enum discharge_reason
        uuid referred_by FK
        jsonb intake_data
    }
    
    rh_resident_contacts {
        uuid id PK
        uuid resident_id FK
        string contact_name
        string relationship
        string phone
        string email
        boolean is_emergency
        boolean can_release_to
    }
    
    rh_resident_documents {
        uuid id PK
        uuid resident_id FK
        string document_type
        string document_name
        string file_path
        date expiration_date
        enum status
    }
```

## Programs & Phases ERD

```mermaid theme={null}
erDiagram
    rh_programs ||--o{ rh_program_phases : "has phases"
    rh_programs ||--o{ rh_episodes : serves
    rh_program_phases ||--o{ rh_phase_milestones : requires
    rh_program_phases ||--o{ rh_phase_privileges : grants
    rh_episode_phases ||--o{ rh_milestone_completions : tracks
    
    rh_programs {
        uuid id PK
        uuid organization_id FK
        string name
        text description
        integer min_stay_days
        integer max_stay_days
        boolean is_active
        jsonb admission_criteria
        jsonb custom_fields
    }
    
    rh_program_phases {
        uuid id PK
        uuid program_id FK
        string name
        integer phase_order
        integer min_days
        integer max_days
        text description
        jsonb requirements
    }
    
    rh_phase_milestones {
        uuid id PK
        uuid phase_id FK
        string name
        text description
        boolean is_required
        integer display_order
        jsonb verification_criteria
    }
    
    rh_phase_privileges {
        uuid id PK
        uuid phase_id FK
        string privilege_name
        text description
        boolean is_default
    }
    
    rh_episode_phases {
        uuid id PK
        uuid episode_id FK
        uuid phase_id FK
        date start_date
        date end_date
        enum status
        uuid promoted_by FK
    }
    
    rh_milestone_completions {
        uuid id PK
        uuid episode_phase_id FK
        uuid milestone_id FK
        date completed_date
        uuid verified_by FK
        text notes
    }
```

## Billing & Charges ERD

```mermaid theme={null}
erDiagram
    rh_episodes ||--o{ rh_charges : generates
    rh_charges ||--o{ rh_charge_payments : "paid by"
    rh_residents ||--o{ rh_payment_methods : "has payment methods"
    rh_charges }o--|| rh_charge_types : "has type"
    
    rh_charge_types {
        uuid id PK
        uuid organization_id FK
        string code
        string name
        decimal default_amount
        enum frequency
        uuid gl_account_id FK
        boolean is_active
    }
    
    rh_charges {
        uuid id PK
        uuid organization_id FK
        uuid episode_id FK
        uuid resident_id FK
        uuid charge_type_id FK
        date charge_date
        decimal amount
        string description
        enum status
        uuid invoice_id FK
    }
    
    rh_charge_payments {
        uuid id PK
        uuid charge_id FK
        uuid payment_id FK
        decimal amount_applied
        date applied_date
    }
    
    rh_payment_methods {
        uuid id PK
        uuid resident_id FK
        enum method_type
        string last_four
        boolean is_default
        boolean is_active
    }
```

## Referrals & Waitlist ERD

```mermaid theme={null}
erDiagram
    rh_referral_sources ||--o{ rh_referrals : sends
    rh_referrals ||--o{ rh_waitlist_entries : "added to"
    rh_waitlist_entries }o--|| rh_programs : "waiting for"
    
    rh_referral_sources {
        uuid id PK
        uuid organization_id FK
        string name
        enum source_type
        string contact_name
        string contact_email
        string contact_phone
        boolean is_active
    }
    
    rh_referrals {
        uuid id PK
        uuid organization_id FK
        uuid referral_source_id FK
        string first_name
        string last_name
        string phone
        string email
        date referral_date
        enum status
        text notes
        jsonb intake_responses
    }
    
    rh_waitlist_entries {
        uuid id PK
        uuid referral_id FK
        uuid program_id FK
        uuid site_id FK
        date added_date
        integer priority
        enum status
        date expected_availability
    }
```

## RLS Policy Coverage

| Table                | SELECT | INSERT | UPDATE | DELETE | WITH CHECK | Status   |
| -------------------- | ------ | ------ | ------ | ------ | ---------- | -------- |
| rh\_residents        | ✅      | ✅      | ✅      | ✅      | ✅          | Complete |
| rh\_episodes         | ✅      | ✅      | ✅      | ✅      | ✅          | Complete |
| rh\_buildings        | ✅      | ✅      | ✅      | ✅      | ✅          | Complete |
| rh\_rooms            | ✅      | ✅      | ✅      | ✅      | ✅          | Complete |
| rh\_beds             | ✅      | ✅      | ✅      | ✅      | ✅          | Complete |
| rh\_bed\_assignments | ✅      | ✅      | ✅      | ✅      | ✅          | Complete |
| rh\_programs         | ✅      | ✅      | ✅      | ✅      | ✅          | Complete |
| rh\_program\_phases  | ✅      | ✅      | ✅      | ✅      | ✅          | Complete |
| rh\_charges          | ✅      | ✅      | ✅      | ✅      | ✅          | Complete |
| rh\_referrals        | ✅      | ✅      | ✅      | ✅      | ✅          | Complete |

## Key Tables

### rh\_residents

Individual resident profiles with demographics and contact info.

### rh\_episodes

Admission episodes linking residents to programs and sites.

### rh\_beds

Individual bed inventory with availability tracking.

### rh\_programs

Recovery program definitions with phase structures.

### rh\_charges

Billing charges linked to episodes and GL accounts.

## Helper Functions

| Function                                  | Purpose                     |
| ----------------------------------------- | --------------------------- |
| `rh_has_org_access(org_id, user_id)`      | Check RH module access      |
| `rh_get_available_beds(site_id, date)`    | Get available beds for date |
| `rh_get_census(site_id, date)`            | Get census count for date   |
| `rh_calculate_length_of_stay(episode_id)` | Calculate LOS in days       |

## Cross-Module Dependencies

* **PF:** Organizations, sites, profiles
* **FA:** Charge posting, invoice creation
* **FW:** Intake forms, discharge workflows
* **HR:** Staff assignments (future)
