# Addendum 73 — MED: Andrology Clinic & Radiology Department

**Scope:** Newer/extended "center/hospital" edition discovered at `/home/medgreennatureco/public_html/med` (live: med.greennature.com.sa). Compared against the original 312-table dump (`/home/amrtechogate/public_html/obgy/_db/obgy_12-7-2024.sql`). All schema knowledge below is extracted from controller code and Smarty views only — no live DB was queried.

**Controllers analyzed (full reads):**
- `core/controllers/and_visits.php` (1182 lines)
- `core/controllers/and_history.php` (456 lines)
- `core/controllers/and_examination.php` (197 lines)
- `core/controllers/addrays.php` (218 lines)
- `core/controllers/raysdept.php` (646 lines)
- `core/controllers/radiation.php` (698 lines)
- Helper: `core/controllers/invsdept_common.php` → `loadRays()` (line 3567)
- Views: `core/views/obgy/andrology/*` (examination.html, history.html, visits.html, printrows.html, newrowUS.html)

---

## 1. Dedicated Andrology Clinic (NEW domain)

The original system treated the male side as **semen/hormone lab rows inside the infertility sheet** (`semen`, `semen2`, `icsisemen`, `ssemen`, `hormonalprofile*` — see report section 07). The MED edition adds a **standalone andrology clinic** with its own visit, history, and exam screens, all keyed by `patientid`.

### `and_visits.php` → `andvisits`
- `index()` (line 71): loads `andvisits` filtered by `deleted=0 and patientid=…`, decodes CSV `diagnosisid`/`complaintid` against `anddiagnosis`/`andcomplaint` (lines 122-125). Merges legacy `diagnosistxt` into `complaint` then clears it (lines 99-104).
- Embeds five sub-workflows, each its own NEW table:
  - **Rays order** → `andvisitsrays` via `addray()` (lines 499-558); `forhusband` flag (1=husband/0=wife).
  - **Investigation order** → `andvisitsinvestigation` via `addinvestigation()` (lines 663-744); supports bundle offers `invoffer`/`invofferdetails` (lines 705-721, `offer_id`/`inv_id`).
  - **Prescription** → `andvisitsdrugs` via `loadPrescriptionDetails()`/`showprescription()` (lines 275-294, 849-926); `drugid` → `drugs`.
  - **Male ultrasound** → `andvisitsus` + follicles `andvisitsficils` via `loadusrow()`/`savefocil()` (lines 982-1101). Types `uscompleteAnd`/`ussimpleAnd`. Follicle row: `andvisitsusid`, `name`, `length`, `width`, `volume`, `type` (0=left/1=right), `sysdate`.
  - **Semen** → `andvisitssemen`; **Genetic testing** → `andvisitsgenetictesting`.
- `printselected()` (lines 1105-1172): decodes genetic `chromosome` (1=normal,2=abnormal) and `azf` (1=normal,2=deletion).
- Semen field names (inferred from `views/obgy/andrology/visits.html` colName attrs): `conc`, `nf` (motility), `ph`, `viscosity`, `aggl`, `vol`, `pus`.
- Generic `update()` does single-column inline edits; `del()` is soft delete (`deleted=1`).

### `and_history.php` → 5 sub-tables
- `index()` (line 71) calls `history_data`, `mProblems_data`, `erection_data`, `ejaculation_data`, `contraception_data`.
- `andhistory` (lines 371-395): `durationid`→`infertileduration`, `fertiletype`, `smoking`/`smokinglevel`, `inflammation`, `trauma`, `marriage`, `stopped`, `notes`. `abortionno` auto-computed by counting `phobstetric` rows with `obstermination='4'` (lines 383-389).
- `andmedicalproblems` → `medicaldisease` (**KNOWN** table reused) via `diseaseid`.
- `anderection` → `erectiondisease` (NEW); `andejaculation` → `ejaculationdisease` (NEW); `andcontraception` → `andcontratype` (NEW).
- Each lookup has an AJAX add endpoint: `addduration`, `adddmdisease`, `addderectiondisease`, `addejaculationdisease`, `addcontraception` (lines 168-286).

### `and_examination.php` → `andexamination`
- Bilateral exam, colName attrs from `views/obgy/andrology/examination.html`: `testesl`/`testesr`, `epidl`/`epidr`, `cordl`/`cordr`, `vasl`/`vasr`, `notes`, `date`.
- `newexam()` dispenses a row; inline `update()`; soft `del()`.

---

## 2. Radiology Department Workflow (`raysdept.php`) — NEW

Standalone department driven by shared `visits.for_department = 5`. Order → Perform → Report → Archive.

- **Order:** `newvisit()` (line 232) sets `fordepartment=5`, lists `rayscats`/`rays`; `savenewvisit()` (line 312) inserts `visits` then calls `sendToDept()` (line 358) which dispenses `raysresults` per selected ray: `rayid`, `raytype` (invType), `visitid`, `patientid`, `doctorid=0`, `deleted=0`, `forhusband`, `date`. Internal requests (`invType==1`) set `internalrequestid`/`internaltable` (lines 375-378). For `for_department==12` it writes `investigationresults` instead (line 369).
- **Worklist:** `index()` (line 73) raw SQL joins `raysresults`⋈`visits`⋈`patients`⋈`patients_childs`, filtered `raysresults.status=0` (today). Subject resolution by `visits.for_husband` (1=husband / 0=wife / 2=child via `patients_childs.child_name`/`gender`).
- **Perform/Report:** `showdetails()` (line 105) lists `raysresults` for visit + `raysresults_img`. `update()` (line 199): writing `rayresult` stamps `date` and `doctorid`; if `raytype==1` propagates result to the internal source table (`R::load($obj->internaltable, $obj->internalrequestid)`).
- **Images:** `upload()` → single `raysresults.rayimg`; `uploadMulti()` (line 469) → multiple `raysresults_img` rows (`raysresults_id`, `rayimg`, `deleted`) via `R::xdispense`. Files in `upload/rayimg`.
- **Finish/Archive:** `finishRequest()` (line 589) sets `status=1`; `archive()` (line 391) lists `status=1`; `printresults()` (line 523) prints report, resolves referring doctor via `patients.refer_doctor` or last visit `for_doctor`.
- **`loadRays()`** in `invsdept_common.php` (line 3567): per-patient rays history across `visits` where `for_department=5`, attaching `raysresults` and `raysresults_img`.

### `addrays.php` — Ray catalog admin
- `rayscats` (`name`, `displayorder`, `deleted`) and `rays` (`name`, `rayscatid`, `price`, `range`, `favorite`, `deleted`). `displayorder` groups display into 5 buckets (mirrored in `and_visits.loadRaysDetails()` lines 167-213 and `raysdept.newvisit()` lines 255-300).

---

## 3. `radiation.php` — NOT radiation therapy

Internal controllername is `instruction`; operates on the **KNOWN** `instruction` table for patient instructions + hospital transfer letters. Uses NEW `operationinstructions` (`insttext`) and KNOWN `hospitalnames`. Transfer letter aggregates obstetric/medical/surgical/gyn history (`phobstetric`, `phpastmedical`, `phpastsurgical`, `phpastgynecological`). Included only to clarify it is unrelated to diagnostic radiology.

---

## NEW vs KNOWN cross-check

Verified against `grep CREATE TABLE` of the original dump. The original contains semen-lab tables only: `semen`, `semen2`, `icsisemen`, `ssemen`, `semeninfertility`, `semenplace`, `sementype`, `semen2result*`, `semen2place*` — but **no** andrology-clinic tables.

**NEW:** `andvisits`, `andhistory`, `andexamination`, `andmedicalproblems`, `anderection`, `andejaculation`, `andcontraception`, `andcontratype`, `anddiagnosis`, `andcomplaint`, `infertileduration`, `erectiondisease`, `ejaculationdisease`, `andvisitssemen`, `andvisitsgenetictesting`, `andvisitsus`, `andvisitsficils`, `andvisitsdrugs`, `andvisitsrays`, `andvisitsinvestigation`, `rays`, `rayscats`, `raysresults`, `raysresults_img`, `investigationresults`, `patients_childs`, `operationinstructions`, `invoffer`, `invofferdetails`.

**KNOWN (reused):** `medicaldisease`, `instruction`, `hospitalnames`, `visits`, `invests`, `investcats`, `detections`, `patients`, `awusers`, `drugs`, `programesetting`, `wifetypes`, `husbandtypes`, `phobstetric`.

---

## ERP/HIS plan impacts

1. Add an **independent Andrology Clinic domain** (17+ `and*` tables) to the migration blueprint — not an appendix of the infertility sheet.
2. Model a **generic Orders/Results service-department** pattern (`raysresults`+`investigationresults` over `visits.for_department`) instead of sheet-bound results.
3. Introduce a **Subject/beneficiary** model: one order can target husband/wife/child (`forhusband`, `visits.for_husband`, `patients_childs`).
4. Model the **polymorphic internal-request link** (`raytype`/`internaltable`/`internalrequestid`) as a flexible Order↔SourceRef relation.
5. Wire **priced catalogs + bundles** (`rays`/`rayscats`, `invoffer`/`invofferdetails`) to the billing module.
6. Centralize **user-extensible lookup management** (many AJAX-addable reference tables).
7. Exclude `radiation.php` from radiology scope (it is transfer-letter/instructions).
8. Plan an **attachment/PACS-lite store** (or DICOM integration) for `raysresults_img` / `upload/rayimg` (inference).
