# پایگاه داده — تسک ۱۵: پرونده بیمار

## جدول: patient_records

| ستون | نوع | توضیح |
|------|-----|-------|
| id | INT UNSIGNED AUTO_INCREMENT PK | |
| uuid | CHAR(36) UNIQUE NOT NULL | |
| entity_type | VARCHAR(10) NOT NULL | `'doctor'` \| `'clinic'` |
| entity_id | INT NOT NULL | |
| user_id | INT NOT NULL FK→users.id ON DELETE RESTRICT | بیمار |
| created_by_type | VARCHAR(15) NOT NULL | `'doctor'` \| `'secretary'` \| `'system'` |
| created_by_id | INT NOT NULL | id ایجادکننده |
| created_at | INT NOT NULL | |

```sql
UNIQUE KEY uniq_patient_record (entity_type, entity_id, user_id)
INDEX idx_patient_records_entity (entity_type, entity_id)
```

## جدول: patient_sessions

| ستون | نوع | توضیح |
|------|-----|-------|
| id | INT UNSIGNED AUTO_INCREMENT PK | |
| uuid | CHAR(36) UNIQUE NOT NULL | |
| record_id | INT NOT NULL FK→patient_records.id ON DELETE CASCADE | |
| appointment_id | INT NULL FK→appointments.id ON DELETE SET NULL | |
| insurance_base_id | INT NULL FK→categories.id ON DELETE SET NULL | بیمه پایه |
| insurance_supplementary_id | INT NULL FK→categories.id ON DELETE SET NULL | بیمه مکمل |
| visit_price_rials | INT NOT NULL DEFAULT 0 | |
| base_insurance_discount_percent | DECIMAL(5,2) NOT NULL DEFAULT 0 | |
| supplementary_discount_percent | DECIMAL(5,2) NOT NULL DEFAULT 0 | |
| services_total_rials | INT NOT NULL DEFAULT 0 | کپی محاسبه‌شده |
| final_price_rials | INT NOT NULL DEFAULT 0 | |
| payment_method | VARCHAR(15) NOT NULL DEFAULT 'pending' | `'cash'`\|`'card'`\|`'insurance'`\|`'pending'` |
| notes | TEXT NULL | |
| created_at | INT NOT NULL | |
| updated_at | INT NOT NULL | |

```sql
INDEX idx_patient_sessions_record (record_id, created_at)
```

## جدول: session_services

| ستون | نوع | توضیح |
|------|-----|-------|
| id | INT UNSIGNED AUTO_INCREMENT PK | |
| uuid | CHAR(36) UNIQUE NOT NULL | |
| session_id | INT NOT NULL FK→patient_sessions.id ON DELETE CASCADE | |
| service_item_id | INT NOT NULL FK→service_items.id ON DELETE RESTRICT | |
| staff_id | INT NULL FK→clinic_staff.id ON DELETE SET NULL | |
| price_rials | INT NOT NULL | **کپی** قیمت در زمان ثبت |
| created_at | INT NOT NULL | |

## Migration نمونه

```sql
CREATE TABLE patient_records (
    id              INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    uuid            CHAR(36) NOT NULL UNIQUE,
    entity_type     VARCHAR(10) NOT NULL,
    entity_id       INT NOT NULL,
    user_id         INT NOT NULL,
    created_by_type VARCHAR(15) NOT NULL,
    created_by_id   INT NOT NULL,
    created_at      INT NOT NULL,
    UNIQUE KEY uniq_patient_record (entity_type, entity_id, user_id),
    INDEX idx_patient_records_entity (entity_type, entity_id),
    FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE patient_sessions (
    id                              INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    uuid                            CHAR(36) NOT NULL UNIQUE,
    record_id                       INT NOT NULL,
    appointment_id                  INT NULL,
    insurance_base_id               INT NULL,
    insurance_supplementary_id      INT NULL,
    visit_price_rials               INT NOT NULL DEFAULT 0,
    base_insurance_discount_percent DECIMAL(5,2) NOT NULL DEFAULT 0,
    supplementary_discount_percent  DECIMAL(5,2) NOT NULL DEFAULT 0,
    services_total_rials            INT NOT NULL DEFAULT 0,
    final_price_rials               INT NOT NULL DEFAULT 0,
    payment_method                  VARCHAR(15) NOT NULL DEFAULT 'pending',
    notes                           TEXT NULL,
    created_at                      INT NOT NULL,
    updated_at                      INT NOT NULL,
    FOREIGN KEY (record_id)        REFERENCES patient_records(id) ON DELETE CASCADE,
    FOREIGN KEY (appointment_id)   REFERENCES appointments(id) ON DELETE SET NULL,
    FOREIGN KEY (insurance_base_id)         REFERENCES categories(id) ON DELETE SET NULL,
    FOREIGN KEY (insurance_supplementary_id) REFERENCES categories(id) ON DELETE SET NULL,
    INDEX idx_patient_sessions_record (record_id, created_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE session_services (
    id              INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    uuid            CHAR(36) NOT NULL UNIQUE,
    session_id      INT NOT NULL,
    service_item_id INT NOT NULL,
    staff_id        INT NULL,
    price_rials     INT NOT NULL,
    created_at      INT NOT NULL,
    FOREIGN KEY (session_id)      REFERENCES patient_sessions(id) ON DELETE CASCADE,
    FOREIGN KEY (service_item_id) REFERENCES service_items(id) ON DELETE RESTRICT,
    FOREIGN KEY (staff_id)        REFERENCES clinic_staff(id) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
```

## نکات مهم

- **UNIQUE(entity_type, entity_id, user_id)** — یک بیمار یک پرونده در هر کلینیک/مطب دارد
- **price_rials در session_services کپی می‌شود** — تغییر قیمت سرویس در آینده روی سشن‌های قبلی اثر ندارد
- **services_total_rials و final_price_rials** در PatientSession کپی محاسبه‌شده هستند — برای گزارش‌گیری سریع
- **user_id ON DELETE RESTRICT** — نمی‌توان کاربری را که پرونده دارد حذف کرد
