# pacs_downloadiso Module — Integration Analysis

## Overview

The `pacs_downloadiso` module (under `/data/newsas-git/hisv2/his2_dev_raw/module/pacs_downloadiso/`) is the HIS frontend that drives the mkiso scripts. It runs inside the HIS web app and its JavaScript opens new browser windows pointing at scripts on the **PACS host**.

---

## Two-Server Architecture

```
┌────────── HIS Server ──────────┐          ┌────── PACS Server ──────┐
│                                │          │                         │
│  pacs_downloadiso/             │  HTTP    │  mkiso.php              │
│    index.php (UI)              │ ──────→  │  mkiso_multiple.php     │
│    pacs_downloadiso.php (API)  │          │  send_rimage_multiple.* │
│                                │          │                         │
│  hiscnf/config.php             │          │  ABPACS:11112           │
│    $_PACS_host (PACS server IP)│          │  CDRECORD:10104        │
│    $_PACS                      │          │                         │
│    $_CDPUBLISHER               │          │  /usr/local/dcm4che/    │
│                                │          │  /usr/bin/genisoimage   │
│  DB: rsabt201107 (192.168.2.7) │          │  /var/www/html/microdicom│
│    trxdepartemen               │          │                         │
│    pacs_result_series          │          │  pacsdb_his:3306        │
│    regpas / medrec             │          │  (192.168.2.7)          │
└────────────────────────────────┘          └─────────────────────────┘
```

- **HIS DB** (`rsabt201107`) — transaction data, patient registration, PACS result tracking
- **PACS DB** (`pacsdb_his`) — `mkiso_multiple.php` queries it directly for patient name lookup via `AccessionNumber`
- **Config key**: `$_PACS_host` in `hiscnf/config.php` tells the HIS frontend where the PACS web server is

---

## Endpoints Called by HIS Frontend

| HIS JS Function | HTTP Call | PACS Script | Parameters |
|-----------------|-----------|-------------|------------|
| `dowloadiso()` | `window.open` | `/mkiso.php` | `?accession_number=X` |
| `printiso()` | `window.open` | `/send_rimage_multiple.php` | `?accession_number=X` |
| `fn_downloadisomultiple()` | `window.open` | `/mkiso_multiple.php` | `?accession_number=X,Y,Z` (JS array .toString()) |
| `fn_printisomultiple()` | `window.open` | `/send_rimage_multiple.php` | `?accession_number=X,Y,Z` (JS array .toString()) |

### Single vs Multi accession decision

The HIS UI determines which to call based on **how many AccessionNumbers a TrxDepartemenID has**:

- **1 AccessionNumber** → shows "Download ISO" button → calls `dowloadiso()` → `/mkiso.php?accession_number=X`
- **>1 AccessionNumber** → shows "Download" link → opens multi dialog → calls `fn_downloadisomultiple()` → `/mkiso_multiple.php?accession_number=X,Y,Z`

The decision is made in `ambildatapreview()` (line ~163 in `pacs_downloadiso.php`):
```php
if($JumAccessionNo>1){
    $Action = "<a href='#' onclick=\"fn_bukadivdownloadmultiple('$row[TrxDepartemenID]')\">...";
} else {
    $Action = "<a href='#' onclick=\"fn_bukadivdownload('$pr[AccessionNumber]')\">...";
}
```

---

## mkiso2.php — NOT Called by HIS

**The `mkiso2.php` script is absent from all HIS integration points.** It was found in the local `/data/notes/my-task/mkiso/` directory but:

- Not referenced in `pacs_downloadiso/index.php` (JS)
- Not referenced in `pacs_downloadiso/pacs_downloadiso.php` (PHP)
- Not mentioned in any config

`mkiso2.php` is a **DICOM relay** that fetches from `ABPACS@localhost:11112` and forwards to `172.16.0.120:104` using `dcmsend`. It appears to be a standalone utility, possibly run manually or by cron.

**Action:** Mark mkiso2.php as lower priority in the migration todo.

---

## send_rimage_multiple.php — Out of Scope

Not present in local mkiso project. This is a **CD Publisher print script** on the PACS server. It's conditionally shown only when `$_CDPUBLISHER=='Y'`. Not part of the Java→dcmtk migration.

---

## AJAX API Flow (HIS Backend → PACS Host Resolution)

### 1. Single download path (`dowloadiso()`)

```
User clicks "Download ISO"
  → AJAX: pacs_downloadiso.php?exe=cek_data_accessionno&AccessionNumber=X
  → PHP: checks pacs_result_series table for Published='Y'
  → Response: { OK: "OK", PACS_HOST: "192.168.x.x" }
  → JS: window.open("http://{PACS_HOST}/mkiso.php?accession_number=X")
```

The `PACS_HOST` comes from `hiscnf/config.php` (`$_PACS_host`). The HIS module never constructs a URL with hardcoded PACS IP — it always resolves via AJAX or config.

### 2. Multi download path (`fn_downloadisomultiple()`)

```
User clicks "Download ISO" (multi dialog)
  → JS: get_config_pacs() — synchronous AJAX to pacs_downloadiso.php?exe=ambil_config_pacs
  → Response: { PACS_HOST: "192.168.x.x" }
  → JS: collects checked AccessionNumbers from flexigrid
  → JS: window.open("http://{PACS_HOST}/mkiso_multiple.php?accession_number=X,Y,Z")
```

### 3. Print ISO paths (`printiso()` / `fn_printisomultiple()`)

Same pattern but calling `/send_rimage_multiple.php` instead.

---

## Config Dependencies

The `pacs_downloadiso.php` backend uses these config variables (from `hiscnf/config.php`):

| Config Variable | Purpose | Used In |
|----------------|---------|---------|
| `$_PACS` | Must be `'1'` to enable the module | `cek_setting_pacs()`, `cek_data_accessionno()`, `ambil_config_pacs()` |
| `$_PACS_host` | PACS server IP/hostname (where mkiso scripts live) | `cek_data_accessionno()`, `ambil_config_pacs()` |
| `$_CDPUBLISHER` | If `'Y'`, show "Print ISO" button | `index.php` (inline PHP) |
| `$_RAD_Workorder` | If `1`, filter by WorkOrder only | `ambildatapreview()` |
| `$_trxdepartemen_verif` | If `'Y'`, exclude verification field from query | `ambildatapreview()` |

Also includes `hiscnf/pacsdownloadiso.config.php` — only used for `$_CDPUBLISHER`.

---

## Database Tables Involved

### HIS DB (`rsabt201107`)

| Table | Role |
|-------|------|
| `trxdepartemen` | Transaction header — RegID, TrxID, Tanggal, DepartemenID, WorkOrder |
| `trxlayanan` | Links TrxLayananID → TrxDepartemenID |
| `regpas` | Patient registration — Nama, MEDRECID |
| `medrec` | Medical records — Nama (patient name) |
| `departemen` | Department — Nama, ModulExternal, NamaModulExternal |
| `dokter` | Doctor — Nama |
| `masterlayanan` | Service catalog — Nama, ModalityCode |
| `pacs_result_series` | PACS result tracking — AccessionNumber, Published, TrxDepartemenID |
| `pacs_order_mwl` | PACS worklist — AccessionNumber, TrxDepartemenID |

### PACS DB (`pacsdb_his` on 192.168.2.7)

| Table | Use by mkiso_multiple.php |
|-------|---------------------------|
| `pacs_result_series` | Lookup `MEDRECID`, `RegID` from `AccessionNumber` |
| `medrec` | Lookup `Nama` (patient name) from `MEDRECID` |

---

## Key Observations for dcmtk Migration

### 1. `PACS_HOST` is a config variable — must update on the PACS server only

The HIS module points to whatever `$_PACS_host` is. **No code changes needed in the HIS module** for the mkiso migration. All changes are on the PACS server where `mkiso.php`, `mkiso_multiple.php`, and `mkiso2.php` live.

### 2. Concurrent requests are possible

Multiple HIS users might click "Download ISO" at once. The current Java-based approach binds to **port 10104** (hardcoded). With dcmtk `storescp`, you need a unique port per request to avoid conflicts.

### 3. mkiso2.php is standalone — can be batched

Not called by HIS. Can be migrated independently or de-prioritized.

### 4. send_rimage_multiple.php out of scope

Not part of the mkiso scripts. Leave as-is.

### 5. No changes to HIS module needed

The AJAX calls pass `AccessionNumber` as a query parameter to scripts on the PACS host. The mkiso scripts' **external interface** (GET parameter `accession_number`) does not change. Only the internal DICOM fetching mechanism changes.

### 6. Multi-accession parameter format

`mkiso_multiple.php` receives `?accession_number=X,Y,Z` (comma-separated). The `mkiso_multiple.php` script already handles this format via `explode(",", ...)`. No change needed.

---

## Verification Checklist (Post-Migration)

- [ ] HIS preview still shows PACS studies with correct Status and Download buttons
- [ ] Single accession download: `window.open` to `/mkiso.php?accession_number=X` works
- [ ] Multi accession download: `window.open` to `/mkiso_multiple.php?accession_number=X,Y,Z` works
- [ ] Print ISO (CD Publisher) still works via `/send_rimage_multiple.php`
- [ ] Config check: `cek_setting_pacs()` returns OK
- [ ] AJAX: `cek_data_accessionno()` returns correct PACS_HOST and OK status
- [ ] AJAX: `ambil_config_pacs()` returns correct PACS_HOST
- [ ] No PHP errors in pacs_downloadiso.php after migration
