Lokasi:
/
home
/
u847703118
/
domains
/
alphamed.cloud
/
public_html
/
Ubah: barcode_reader.php
<?php require_once 'includes/auth_session.php'; require_once 'includes/header.php'; requireModuleAccess('system_barcode_reader', []); function name_token_full(string $first, string $last): string { $full = trim($first . ' ' . $last); $tok = strtoupper(preg_replace('/[^A-Za-z0-9 ]/', '', $full) ?? ''); $tok = preg_replace('/\s+/', ' ', $tok) ?? $tok; $tok = str_replace(' ', '', trim($tok)); return substr($tok, 0, 28); } function compact_barcode_key(string $barcode): string { return strtoupper(preg_replace('/[^A-Za-z0-9]/', '', $barcode) ?? ''); } function compact_name_token(string $first, string $last): string { $first = trim($first); $last = trim($last); $fi = $first !== '' ? strtoupper(substr($first, 0, 1)) : ''; $ln = strtoupper(preg_replace('/[^A-Za-z0-9]/', '', $last) ?? ''); $tok = strtoupper(preg_replace('/[^A-Za-z0-9]/', '', $fi . $ln) ?? ''); if ($tok === '') $tok = 'NA'; return substr($tok, 0, 14); } function full_payload(string $barcodeVal, string $nameTok): string { $base = $barcodeVal . '|' . $nameTok; $chk = substr(sha1($base), 0, 8); return $base . '|' . $chk; } function compact_payload(string $barcodeKey, string $nameTok): string { $base = $barcodeKey . '|' . $nameTok; $chk = strtoupper(substr(sha1($base), 0, 4)); return $barcodeKey . '-' . $nameTok . '-' . $chk; } function spinray_sample_key(string $accession): string { $acc = trim((string)$accession); if ($acc === '') return ''; $digits = preg_replace('/\D+/', '', $acc) ?? ''; if ($digits !== '') { $digits = ltrim($digits, '0'); if ($digits === '') $digits = '0'; return str_pad($digits, 10, '0', STR_PAD_LEFT); } return strtoupper(preg_replace('/[^A-Za-z0-9]/', '', $acc) ?? ''); } function decode_scanned(string $raw): array { $raw = trim($raw); if ($raw === '') return ['barcode' => '', 'name' => '', 'checksum' => '', 'valid' => null, 'format' => '']; if (strpos($raw, '|') !== false) { $parts = array_map('trim', explode('|', $raw)); $barcode = $parts[0] ?? ''; $name = $parts[1] ?? ''; $name = strtoupper(preg_replace('/[^A-Za-z0-9 ]/', '', $name) ?? ''); $name = preg_replace('/\s+/', ' ', $name) ?? $name; $name = str_replace(' ', '', trim($name)); $chk = $parts[2] ?? ''; $valid = null; if ($chk !== '' && preg_match('/^[0-9a-fA-F]{8}$/', $chk)) { $expect = substr(sha1($barcode . '|' . $name), 0, 8); $valid = (strtolower($expect) === strtolower($chk)); } return ['barcode' => $barcode, 'name' => $name, 'checksum' => $chk, 'valid' => $valid, 'format' => 'pipe']; } if (strpos($raw, '-') !== false) { $parts = array_map('trim', explode('-', $raw)); $barcode = $parts[0] ?? ''; $name = $parts[1] ?? ''; $name = strtoupper(preg_replace('/[^A-Za-z0-9 ]/', '', $name) ?? ''); $name = preg_replace('/\s+/', ' ', $name) ?? $name; $name = str_replace(' ', '', trim($name)); $chk = $parts[2] ?? ''; $valid = null; if ($chk !== '' && preg_match('/^[0-9a-fA-F]{4,8}$/', $chk)) { $expect = substr(sha1($barcode . '|' . $name), 0, strlen($chk)); $valid = (strtolower($expect) === strtolower($chk)); } return ['barcode' => $barcode, 'name' => $name, 'checksum' => $chk, 'valid' => $valid, 'format' => 'dash']; } return ['barcode' => $raw, 'name' => '', 'checksum' => '', 'valid' => null, 'format' => 'raw']; } function parse_print_url(string $url): array { $url = trim($url); if ($url === '') return ['type' => '', 'id' => 0, 'error' => '']; $p = @parse_url($url); if (!is_array($p)) return ['type' => '', 'id' => 0, 'error' => 'Invalid URL.']; $path = (string)($p['path'] ?? ''); if ($path === '' || stripos($path, 'print_barcode.php') === false) return ['type' => '', 'id' => 0, 'error' => 'URL must point to print_barcode.php']; $qs = (string)($p['query'] ?? ''); $q = []; parse_str($qs, $q); $type = strtoupper(trim((string)($q['type'] ?? ''))); $id = (int)($q['id'] ?? 0); if (!in_array($type, ['INV', 'TR'], true)) return ['type' => '', 'id' => 0, 'error' => 'Type must be INV or TR.']; if ($id <= 0) return ['type' => '', 'id' => 0, 'error' => 'Invalid id.']; return ['type' => $type, 'id' => $id, 'error' => '']; } $inputUrl = trim((string)($_POST['url'] ?? '')); $scanRaw = trim((string)($_POST['scan'] ?? '')); $type = strtoupper(trim((string)($_POST['type'] ?? ''))); $id = (int)($_POST['id'] ?? 0); $fromUrl = null; $compute = null; $computeErr = ''; if ($inputUrl !== '') { $fromUrl = parse_print_url($inputUrl); if (($fromUrl['error'] ?? '') === '') { $type = (string)$fromUrl['type']; $id = (int)$fromUrl['id']; } else { $computeErr = (string)$fromUrl['error']; } } if ($computeErr === '' && $type !== '' && $id > 0) { if (!in_array($type, ['INV', 'TR'], true)) { $computeErr = 'Type must be INV or TR.'; } else { $barcodeVal = $type . '-' . str_pad((string)$id, 6, '0', STR_PAD_LEFT); $first = ''; $last = ''; $label = ''; $accessionNo = ''; if ($type === 'INV') { $st = $pdo->prepare("SELECT p.first_name, p.last_name, (SELECT tr.accession_no FROM test_results tr WHERE tr.invoice_id = i.id AND tr.accession_no IS NOT NULL AND tr.accession_no != '' ORDER BY tr.id ASC LIMIT 1) AS accession_no FROM invoices i JOIN patients p ON i.patient_id = p.id WHERE i.id = ? LIMIT 1"); $st->execute([$id]); $row = $st->fetch(PDO::FETCH_ASSOC) ?: null; if (!$row) { $computeErr = 'Invoice not found.'; } else { $first = (string)($row['first_name'] ?? ''); $last = (string)($row['last_name'] ?? ''); $accessionNo = (string)($row['accession_no'] ?? ''); $label = substr($first, 0, 1) . ". " . $last . " - Invoice #" . str_pad((string)$id, 6, '0', STR_PAD_LEFT); } } else { $st = $pdo->prepare("SELECT t.test_name, p.first_name, p.last_name, tr.accession_no FROM test_results tr JOIN lab_tests t ON tr.test_id = t.id JOIN patients p ON tr.patient_id = p.id WHERE tr.id = ? LIMIT 1"); $st->execute([$id]); $row = $st->fetch(PDO::FETCH_ASSOC) ?: null; if (!$row) { $computeErr = 'Test result not found.'; } else { $first = (string)($row['first_name'] ?? ''); $last = (string)($row['last_name'] ?? ''); $tn = (string)($row['test_name'] ?? ''); $accessionNo = (string)($row['accession_no'] ?? ''); $label = substr($first, 0, 1) . ". " . $last . " - " . $tn; } } if ($computeErr === '') { $fullTok = name_token_full($first, $last); $barcodeKey = compact_barcode_key($barcodeVal); $nameTok = compact_name_token($first, $last); $compute = [ 'type' => $type, 'id' => $id, 'label' => $label, 'barcode_val' => $barcodeVal, 'barcode_key' => $barcodeKey, 'name_full_token' => $fullTok, 'name_compact_token' => $nameTok, 'payload_full' => full_payload($barcodeVal, $fullTok), 'payload_compact_1d' => compact_payload($barcodeKey, $nameTok), 'payload_machine_key' => $barcodeKey, 'payload_spinray' => spinray_sample_key($accessionNo), ]; } } } $decoded = $scanRaw !== '' ? decode_scanned($scanRaw) : null; ?> <div class="container-fluid py-3"> <div class="d-flex align-items-center justify-content-between mb-3"> <h4 class="mb-0">Barcode Reader / Generator (Debug)</h4> <a href="dashboard.php" class="btn btn-outline-secondary btn-sm">Back</a> </div> <div class="card shadow-sm mb-3"> <div class="card-body"> <form method="POST" class="row g-3"> <div class="col-12"> <label class="form-label">Paste print_barcode.php URL</label> <input class="form-control" name="url" value="<?php echo htmlspecialchars($inputUrl); ?>" placeholder="https://alphamed.cloud/print_barcode.php?type=INV&id=37"> </div> <div class="col-md-2"> <label class="form-label">Type</label> <select class="form-select" name="type"> <option value="">—</option> <option value="INV" <?php echo ($type === 'INV') ? 'selected' : ''; ?>>INV</option> <option value="TR" <?php echo ($type === 'TR') ? 'selected' : ''; ?>>TR</option> </select> </div> <div class="col-md-4"> <label class="form-label">ID</label> <input class="form-control" name="id" value="<?php echo htmlspecialchars((string)($id ?: '')); ?>" placeholder="37"> </div> <div class="col-md-6"> <label class="form-label">Scan/Raw String (optional)</label> <input class="form-control" name="scan" value="<?php echo htmlspecialchars($scanRaw); ?>" placeholder="INV000037-JDELACRUZ-1A2B"> </div> <div class="col-12"> <button class="btn btn-primary">Generate</button> </div> </form> </div> </div> <?php if ($computeErr !== ''): ?> <div class="alert alert-danger"><?php echo htmlspecialchars($computeErr); ?></div> <?php endif; ?> <?php if (is_array($compute)): ?> <div class="card shadow-sm mb-3"> <div class="card-body"> <div class="row g-3"> <div class="col-md-6"> <div class="fw-bold mb-1">Label</div> <div class="text-muted"><?php echo htmlspecialchars($compute['label']); ?></div> </div> <div class="col-md-6"> <div class="fw-bold mb-1">Print Link</div> <div class="text-muted"><?php echo htmlspecialchars("print_barcode.php?type=" . $compute['type'] . "&id=" . $compute['id']); ?></div> </div> <div class="col-md-6"> <div class="fw-bold mb-1">Machine Sample ID (Option A / HL7 OBR-3)</div> <input class="form-control" value="<?php echo htmlspecialchars($compute['payload_machine_key']); ?>" readonly onclick="this.select()"> <div class="form-text">Enter/scan this into the analyzer so HL7 OBR-3 equals this value.</div> </div> <div class="col-md-6"> <div class="fw-bold mb-1">Print Link (Machine Mode)</div> <div class="text-muted"><?php echo htmlspecialchars("print_barcode.php?type=" . $compute['type'] . "&id=" . $compute['id'] . "&mode=machine"); ?></div> </div> <div class="col-md-6"> <div class="fw-bold mb-1">Spinray Sample Key (OBR-3)</div> <input class="form-control" value="<?php echo htmlspecialchars((string)($compute['payload_spinray'] ?? '')); ?>" readonly onclick="this.select()"> <div class="form-text">Use this when Spinray expects numeric accession style key.</div> </div> <div class="col-md-6"> <div class="fw-bold mb-1">Print Link (Spinray Mode)</div> <div class="text-muted"><?php echo htmlspecialchars("print_barcode.php?type=" . $compute['type'] . "&id=" . $compute['id'] . "&mode=spinray"); ?></div> </div> <div class="col-md-6"> <div class="fw-bold mb-1">Printed 1D Payload (Current)</div> <input class="form-control" value="<?php echo htmlspecialchars($compute['payload_compact_1d']); ?>" readonly onclick="this.select()"> <div class="form-text">This is the string your 1D scanner should output.</div> </div> <div class="col-md-6"> <div class="fw-bold mb-1">Legacy Full Payload (Pipe)</div> <input class="form-control" value="<?php echo htmlspecialchars($compute['payload_full']); ?>" readonly onclick="this.select()"> <div class="form-text">Longer format. May not scan reliably on 1D with small labels.</div> </div> <div class="col-md-4"> <div class="fw-bold mb-1">Barcode Val</div> <input class="form-control" value="<?php echo htmlspecialchars($compute['barcode_val']); ?>" readonly onclick="this.select()"> </div> <div class="col-md-4"> <div class="fw-bold mb-1">Barcode Key</div> <input class="form-control" value="<?php echo htmlspecialchars($compute['barcode_key']); ?>" readonly onclick="this.select()"> </div> <div class="col-md-4"> <div class="fw-bold mb-1">Name Token (Compact)</div> <input class="form-control" value="<?php echo htmlspecialchars($compute['name_compact_token']); ?>" readonly onclick="this.select()"> </div> </div> </div> </div> <div class="card shadow-sm mb-3"> <div class="card-body"> <div class="fw-bold mb-2">1D Barcodes (Scan on Screen)</div> <div class="row g-3"> <div class="col-lg-6"> <div class="text-muted small mb-1">Machine Sample ID</div> <div class="border rounded p-2 bg-white"> <svg class="js-barcode" data-value="<?php echo htmlspecialchars($compute['payload_machine_key']); ?>"></svg> </div> </div> <div class="col-lg-6"> <div class="text-muted small mb-1">Spinray Sample Key</div> <div class="border rounded p-2 bg-white"> <svg class="js-barcode" data-value="<?php echo htmlspecialchars((string)($compute['payload_spinray'] ?? '')); ?>"></svg> </div> </div> <div class="col-lg-12"> <div class="text-muted small mb-1">Printed 1D Payload (Current)</div> <div class="border rounded p-2 bg-white"> <svg class="js-barcode" data-value="<?php echo htmlspecialchars($compute['payload_compact_1d']); ?>"></svg> </div> </div> </div> </div> </div> <?php endif; ?> <?php if (is_array($decoded)): ?> <div class="card shadow-sm mb-3"> <div class="card-body"> <div class="fw-bold mb-2">Decode Result</div> <div class="row g-3"> <div class="col-md-3"> <div class="text-muted small">Format</div> <div><?php echo htmlspecialchars((string)$decoded['format']); ?></div> </div> <div class="col-md-3"> <div class="text-muted small">Barcode</div> <div><?php echo htmlspecialchars((string)$decoded['barcode']); ?></div> </div> <div class="col-md-3"> <div class="text-muted small">Name</div> <div><?php echo htmlspecialchars((string)$decoded['name']); ?></div> </div> <div class="col-md-3"> <div class="text-muted small">Checksum</div> <div><?php echo htmlspecialchars((string)$decoded['checksum']); ?></div> </div> <div class="col-12"> <div class="text-muted small">Checksum Valid</div> <div> <?php $v = $decoded['valid']; if ($v === null) echo '<span class="text-muted">n/a</span>'; elseif ($v) echo '<span class="text-success">valid</span>'; else echo '<span class="text-danger">invalid</span>'; ?> </div> </div> <div class="col-12"> <div class="text-muted small mb-1">Scanned Value Barcode</div> <div class="border rounded p-2 bg-white"> <svg class="js-barcode" data-value="<?php echo htmlspecialchars($scanRaw); ?>"></svg> </div> </div> </div> </div> </div> <?php endif; ?> </div> <script src="https://cdn.jsdelivr.net/npm/jsbarcode@3.11.0/dist/JsBarcode.all.min.js"></script> <script> (function() { function renderAll() { if (typeof JsBarcode === 'undefined') return; document.querySelectorAll('svg.js-barcode').forEach(function(el) { var val = String(el.getAttribute('data-value') || '').trim(); if (!val) return; try { JsBarcode(el, val, { format: "CODE128", width: 2, height: 64, displayValue: true, fontSize: 14, textMargin: 2, margin: 0 }); } catch (e) {} }); } if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', renderAll); } else { renderAll(); } })(); </script> <?php require_once 'includes/footer.php'; ?>
Batal