38960-vm/print_prescription.php
2026-03-05 08:11:47 +00:00

156 lines
5.8 KiB
PHP

<?php
require 'db/config.php';
require 'helpers.php';
// Enable error reporting for debugging (remove in production if sensitive)
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
try {
$db = db();
$visit_id = $_GET['visit_id'] ?? 0;
if (!$visit_id) {
throw new Exception("Invalid Visit ID");
}
// Fetch visit details
// Ensure all joined columns exist or use COALESCE/Check schema
// We select specific columns to avoid 'ambiguous column' errors or fetching too much
$stmt = $db->prepare("
SELECT
v.*,
p.name as patient_name,
p.dob,
p.gender,
d.name_en as doctor_name_en,
d.name_ar as doctor_name_ar,
d.specialization_en,
d.specialization_ar
FROM visits v
JOIN patients p ON v.patient_id = p.id
JOIN doctors d ON v.doctor_id = d.id
WHERE v.id = ?
");
$stmt->execute([$visit_id]);
$visit = $stmt->fetch();
if (!$visit) {
throw new Exception("Visit not found");
}
// Fetch prescriptions
// Check if table exists implicitly by try-catch
$stmt = $db->prepare("SELECT * FROM visit_prescriptions WHERE visit_id = ?");
$stmt->execute([$visit_id]);
$prescriptions = $stmt->fetchAll();
$lang = $_SESSION['lang'] ?? 'en';
} catch (Exception $e) {
die("Error: " . $e->getMessage());
}
?>
<!DOCTYPE html>
<html lang="<?php echo $lang; ?>" dir="<?php echo $lang == 'ar' ? 'rtl' : 'ltr'; ?>">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Prescription #<?php echo $visit_id; ?></title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
body { font-family: 'Times New Roman', Times, serif; }
.prescription-header { border-bottom: 2px solid #000; padding-bottom: 20px; margin-bottom: 30px; }
.prescription-footer { border-top: 2px solid #000; padding-top: 20px; margin-top: 50px; }
.rx-symbol { font-size: 40px; font-weight: bold; font-family: cursive; margin-bottom: 10px; }
.drug-item { margin-bottom: 15px; border-bottom: 1px dashed #ccc; padding-bottom: 10px; }
.drug-name { font-weight: bold; font-size: 18px; }
.drug-dose { font-style: italic; }
@media print {
.no-print { display: none; }
body { padding: 20px; }
}
</style>
</head>
<body onload="window.print()">
<div class="container">
<div class="no-print mb-3 text-end">
<button onclick="window.print()" class="btn btn-primary">Print</button>
<button onclick="window.close()" class="btn btn-secondary">Close</button>
</div>
<div class="prescription-header text-center">
<h2>Hospital Management System</h2>
<p>123 Medical Center Street, City, Country</p>
<p>Phone: +123 456 7890 | Email: info@hospital.com</p>
</div>
<div class="row mb-4">
<div class="col-6">
<h5 class="fw-bold">Doctor:</h5>
<p class="mb-0"><?php echo $visit['doctor_name_' . $lang] ?? $visit['doctor_name_en']; ?></p>
<p class="text-muted small"><?php echo $visit['specialization_' . $lang] ?? $visit['specialization_en']; ?></p>
</div>
<div class="col-6 text-end">
<h5 class="fw-bold">Date:</h5>
<p><?php echo date('d M Y', strtotime($visit['visit_date'] ?? 'now')); ?></p>
</div>
</div>
<div class="row mb-5 border p-3 rounded">
<div class="col-md-6">
<strong>Patient Name:</strong> <?php echo htmlspecialchars($visit['patient_name']); ?>
</div>
<div class="col-md-3">
<strong>Age:</strong> <?php echo calculate_age($visit['dob']); ?>
</div>
<div class="col-md-3">
<strong>Gender:</strong> <?php echo $visit['gender']; ?>
</div>
</div>
<div class="rx-symbol">Rx</div>
<div class="prescription-body mb-5">
<?php if (empty($prescriptions)): ?>
<p class="text-muted text-center py-5">No medications prescribed.</p>
<?php else: ?>
<div class="list-group list-group-flush">
<?php foreach ($prescriptions as $index => $p): ?>
<div class="drug-item">
<div class="row">
<div class="col-1 text-muted fw-bold"><?php echo $index + 1; ?>.</div>
<div class="col-11">
<div class="drug-name"><?php echo htmlspecialchars($p['drug_name']); ?></div>
<div class="row mt-1">
<div class="col-md-6">
<span class="text-muted">Dose:</span> <span class="drug-dose"><?php echo htmlspecialchars($p['dosage']); ?></span>
</div>
<div class="col-md-6">
<span class="text-muted">Instructions:</span> <span><?php echo htmlspecialchars($p['instructions']); ?></span>
</div>
</div>
</div>
</div>
</div>
<?php endforeach; ?>
</div>
<?php endif; ?>
</div>
<div class="prescription-footer">
<div class="row">
<div class="col-8">
<p class="small text-muted">This prescription is valid for 30 days from the date of issue.</p>
</div>
<div class="col-4 text-center">
<div class="border-bottom border-dark mb-2" style="height: 50px;"></div>
<p class="fw-bold">Doctor's Signature</p>
</div>
</div>
</div>
</div>
</body>
</html>