# MED: Shared-Core Diff — Settings or Fork? (Technical Digest)

**Question answered:** Is `med.greennature.com.sa` "نفس البرنامج بإعدادات مختلفة" (same program, different settings) or a newer **fork** with real code divergence?

**Verdict: NEWER FORK with real code divergence** — yet heavily config-driven *within the new code*. The framework core is shared (`_controll.php` byte-identical), but MED adds ~70 new controllers and grows shared clinical controllers 2x–5x with code that does not exist in OBGY at all. Feature flags toggle these new paths; they cannot be "turned on by config" in OBGY because the code is absent.

## Quantified diff (`diff -qr obgy/core/controllers med/core/controllers`)

| Metric | Value |
|---|---|
| Controllers OBGY | 77 |
| Controllers MED | 134 |
| Shared filenames | 57 |
| Shared & **identical** | 21 |
| Shared but **differ** | 36 |
| **Removed** in MED | 21 |
| **Added** in MED | 78 |

Source: `/tmp/ctrl_diff.txt` produced by `diff -qr`.

## "Same program" evidence (identical / cosmetic)

- `core/controllers/_controll.php` — **IDENTICAL** (303 == 303 lines). Controller dispatch core unchanged.
- `examination.php` — diff `+2/-2`, **whitespace only** (blank-line shuffles at lines 139/142/202/227).
- `_header.php` — formatting/spacing only.
- `_menu.php` — diff `+6/-6`: commented out the `error.php?ac=relate` redirect+`exit()` on permission failure (2 sites), and re-enabled `R::findAll('awrolemenu',...)` + `R::trashAll`. Minor behavioral toggle, same logic.

## "Fork" evidence (structural divergence)

### Composer / Monolog — decisive
- OBGY: **no** `composer.json`, **no** `vendor/`.
- MED `_public/aw_config.php` (line ~8): `require_once __DIR__ . '/../vendor/autoload.php';` + `use Monolog\Handler\StreamHandler; use Monolog\Logger;`. `med/vendor/` contains `monolog/`, `psr/`, `composer/`. A Composer toolchain dependency cannot be a "setting."
- Other config deltas: timezone `Africa/Cairo` (was `America/Los_Angeles`); DB `medgreennatureco_med`; `//R::freeze(true)` toggled; OBGY-only `$hosturlApi`/`$redirectLink` (gt4it) removed.

### Shared controllers exploded in size

| File | OBGY lines | MED lines | diff +/− | functions OBGY→MED |
|---|---|---|---|---|
| `visits.php` | 1946 | 9660 | +8599 / -885 | 65 → 220 (+155 new) |
| `patients.php` | 2259 | 4157 | +2332 / -434 | 77 → 127 |
| `_member.php` | 356 | 1018 | (large) | + ERP doctor sync |
| `_role.php` | 598 | 991 | (large) | + separate user menus |
| `antenalvisit.php` | 1361 | 1756 | +395 | — |
| `_sidebar.php` | 160 | 361 | +241 / -40 | + `userMenuSidebar` |
| `programesetting.php` | 296 | 376 | +177 / -97 | +44 setting keys |
| `setup.php` | 225 | 354 | +149 | + ERP visa accounts, `tablesToClean` |

New `visits.php` methods (sample): `erpClient`, `erpSellbill*` (via `erpSellbillUpdate`/`erpSellbillDel` calls), `curlAddClient`, `addLabVisit`, `editlabvisit`, `completeLabPrint`, `createbarcode`, `createSampleBarCode`, `createQRcode`, `billPdf*`, `addAdvancePay`, `getLastAdvance*`, `branchSql`, `getDetectionsBy*`, `getOperationDates`, `arrange_visits`. (155 total new.)

`_member.php` new methods: `addAllDoctorsToERP`, `curlAddDoctor`, `curlupdateDoctor`, `curlDelDoctor`, `erpDoctor`, `updateerpsaveid`, `updateMenuUserSetting`, `displaymenush`, `setting`.

`setup.php` new methods: `geterpVisaAccounts`, `saveERPDBVisaAccount`, `tablesToClean`.

## Config-driven layer (real, but governs new code)

`programesetting.php` in MED is a feature-flag hub. **44 new columns** absent from OBGY (OBGY `programesetting.php` references `programesettingex`/`programesettinglab`/`programesetting2` = **0** times):
`system_type, system_lang, branches, regions, organizations, advance_payment, surgeon, rooms, triggering, ovum_tab, embro_tab, others_tab, tanks_view, whatsapp_sms, whatsapp_txt, voice_record, visit_phones, national_id_req, confirm_attend, birth_notify, case_summary, barcode_print, printserial, refer_doctor, fin_doc_dept, temp_patient, start_hour, end_hour, attend_today, past_search, diagnos_ant, diagnos_gyna, search_result_no, search_letters_no, presquantity, visittype, loginbar, rfc, personal, sexual, above2, buttom2, fontsizeen2, fontsizear2`.

New settings tables created on demand: `programesettingex` (dispensed with `revisioned=0`, columns filled from `programesetting2.colum_name`) and `programesettinglab` (`lab_style=1`).

Menus remain **DB-driven** via `awmenu` (`_sidebar.php` filters by `programesetting.simpleview` → `a.form in (0,1,2)`); MED adds a second `userMenuSidebar`. Framework menu structure unchanged.

## Removed sheet controllers → modular replacements

The architectural shift is single-page clinical **sheets → modular controllers**:

| Removed (OBGY) | Replacement (MED) |
|---|---|
| `ancsheet`, `ancsheet00` | expanded `antenalvisit.php` + `mainantenental*` flow |
| `gynasheet`, `gynasheet00` | `gyna.php` + `ultrasoundgyna.php` |
| `infertilitysheet`, `infertilitysheet00` | `infertility.php` |
| `ivfsheet`, `ivfsheet00` | `embryology.php`, `embryoslab.php`, `embryofreezing.php`, `spermfreezing.php`, `ovumpickupembryotrans.php`, `ivf_reports.php` |
| `iui`, `monitoring`, `edd`, `epc` | folded into visit/report flows |
| `Deliveries` | `birth_notify` flag + reports |
| `operations` | `operations_reserve.php`, `operations_calendar.php`, `operations_rooms.php`, `operations_reports.php`, `operation_types.php` |
| `mobileservices`, `sh`, `addpresenthistory`, `Completesreport`, `Ivfstatistics` | removed / replaced by center reports |

## ERP integration (live)

- `med/core/controllers/erp_common.php`: `orderFromErp()` curls `localhost/erp/controllers/api_web.php?do=...` with a **hardcoded JWT** in `$apiKey` (security note). Also `getPatientLink()` → `api.naslab.gt4it.com`.
- `visits.php` includes `erp_common.php` and calls `erpSellbill`, `erpSellbillUpdate`, `erpSellbillDel` on visit add/update/delete (lines ~1104, 1838, 2257, 2312–2393).
- `_member.php` syncs doctors to ERP; `setup.php` configures ERP visa/bank accounts.

## NEW tables/entities (vs original 312-table dump)

`setup.php → tablesToClean` enumerates ~171 entities; cross-check vs `/tmp/obgy_tables.txt` (312 names from `obgy_12-7-2024.sql`) yields **~91 NEW** entities. Domain-relevant new tables referenced by the shared-modified controllers: `programesettingex`, `programesettinglab`, `organizations`, `organizations_patient_no`, `organization_discount`, `advance_payment`, `archive_request`, `patients_childs`, `governorate`, `governorate_centers`, `regions`, `sub_regions`, `clinic_rooms`, `day_times`, `visit_hours`, `operations_main`, `operation_room`, `operations_rooms`, `operationotherth`, `residence_room`, `residence_rooms`, `rays`, `raysresults`, `raysresults_img`, `gynarays`, `mainantenentalrays`, `andvisitsrays`, `investigationresults` (+ culture/blood/urine/etc. lineage), `invoffer`, `invofferdetails`, `refer`, `visits_follows`, plus ERP bridge tables `sellbill`, `returnsellbill`, `client`, `clientdebtchange`, `bank`, `bankaccount`. All schema knowledge inferred from RedBeanPHP calls / raw SQL / `tablesToClean` — code only, no DB queried.

## ERP/HIS plan impacts

1. Use **MED as the migration baseline**, not OBGY — MED is the evolved center/hospital HIS.
2. Add full new modules: operations+rooms, residence/admission, endoscopy, radiology, advanced lab, pharmacy, andrology, IVF lab (6 controllers).
3. Multi-branch/region/organization model → ERP must be multi-tenant/branch aware.
4. Document the existing ERP bridge (`api_web.php`: sell-bills, clients, doctors); remediate hardcoded JWT in `erp_common.php`.
5. Migrate `programesetting` (+44 keys + 2 tables) as a feature-flag/facility-config layer.
6. Add ~91 new entities to the ERP/HIS data dictionary; verify against live schema later.

## File citations
- `/home/medgreennatureco/public_html/med/_public/aw_config.php`
- `/home/medgreennatureco/public_html/med/core/controllers/{programesetting,setup,_sidebar,_menu,visits,patients,_member,erp_common}.php`
- `/home/amrtechogate/public_html/obgy/core/controllers/*` (baseline)
- `/home/amrtechogate/public_html/obgy/_db/obgy_12-7-2024.sql` (312-table reference)
