diff --git a/api/appointments.php b/api/appointments.php new file mode 100644 index 0000000..671f763 --- /dev/null +++ b/api/appointments.php @@ -0,0 +1,184 @@ +prepare("SELECT * FROM appointments WHERE id = ?"); + $stmt->execute([$id]); + $appointment = $stmt->fetch(PDO::FETCH_ASSOC); + echo json_encode($appointment); + exit; + } + + $startStr = $_GET['start'] ?? null; + $endStr = $_GET['end'] ?? null; + $doctor_id = $_GET['doctor_id'] ?? null; + + $events = []; + $businessHours = []; + + // Fetch Appointments + $query = " + SELECT + a.id, a.start_time as start, a.end_time as end, a.reason as title, a.status, + a.patient_id, a.doctor_id, + p.name as patient_name, + d.name_$lang as doctor_name + FROM appointments a + JOIN patients p ON a.patient_id = p.id + JOIN doctors d ON a.doctor_id = d.id + WHERE 1=1"; + + $params = []; + if ($startStr) { $query .= " AND a.start_time >= ?"; $params[] = $startStr; } + if ($endStr) { $query .= " AND a.start_time <= ?"; $params[] = $endStr; } + if ($doctor_id) { $query .= " AND a.doctor_id = ?"; $params[] = $doctor_id; } + + $stmt = $db->prepare($query); + $stmt->execute($params); + $appointments = $stmt->fetchAll(PDO::FETCH_ASSOC); + + foreach ($appointments as $a) { + $color = '#0d6efd'; // blue + if ($a['status'] === 'Completed') $color = '#198754'; // green + if ($a['status'] === 'Cancelled') $color = '#dc3545'; // red + + $events[] = [ + 'id' => $a['id'], + 'title' => $a['patient_name'] . ' (' . $a['doctor_name'] . ')', + 'start' => $a['start'], + 'end' => $a['end'], + 'color' => $color, + 'extendedProps' => [ + 'type' => 'appointment', + 'patient_id' => $a['patient_id'], + 'doctor_id' => $a['doctor_id'], + 'patient_name' => $a['patient_name'], + 'doctor_name' => $a['doctor_name'], + 'status' => $a['status'], + 'reason' => $a['reason'] + ] + ]; + } + + // Fetch Holidays + $holidayQuery = "SELECT holiday_date as start, name_$lang as title FROM holidays WHERE 1=1"; + $holidayParams = []; + if ($startStr) { $holidayQuery .= " AND holiday_date >= ?"; $holidayParams[] = date('Y-m-d', strtotime($startStr)); } + if ($endStr) { $holidayQuery .= " AND holiday_date <= ?"; $holidayParams[] = date('Y-m-d', strtotime($endStr)); } + + $stmt = $db->prepare($holidayQuery); + $stmt->execute($holidayParams); + $holidays = $stmt->fetchAll(PDO::FETCH_ASSOC); + + foreach ($holidays as $h) { + $events[] = [ + 'id' => 'hol_' . $h['start'], + 'title' => 'Holiday: ' . $h['title'], + 'start' => $h['start'], + 'allDay' => true, + 'color' => '#ffc107', // yellow + 'textColor' => '#000', + 'display' => 'background', + 'extendedProps' => ['type' => 'holiday'] + ]; + + $events[] = [ + 'title' => $h['title'], + 'start' => $h['start'], + 'allDay' => true, + 'color' => '#ffc107', + 'textColor' => '#000', + 'extendedProps' => ['type' => 'holiday'] + ]; + } + + // Fetch Doctor Business Hours + if ($doctor_id) { + $scheduleStmt = $db->prepare("SELECT day_of_week as day, start_time as start, end_time as end FROM doctor_schedules WHERE doctor_id = ?"); + $scheduleStmt->execute([$doctor_id]); + $schedules = $scheduleStmt->fetchAll(PDO::FETCH_ASSOC); + + $bhMap = []; + foreach ($schedules as $s) { + $key = $s['start'] . '-' . $s['end']; + if (!isset($bhMap[$key])) { + $bhMap[$key] = [ + 'daysOfWeek' => [], + 'startTime' => $s['start'], + 'endTime' => $s['end'] + ]; + } + $bhMap[$key]['daysOfWeek'][] = (int)$s['day']; + } + $businessHours = array_values($bhMap); + } else { + $businessHours = [ + [ + 'daysOfWeek' => [0, 1, 2, 3, 4, 5, 6], + 'startTime' => '08:00', + 'endTime' => '17:00' + ] + ]; + } + + echo json_encode([ + 'events' => $events, + 'businessHours' => $businessHours + ]); + exit; +} + +if ($method === 'POST') { + $input = json_decode(file_get_contents('php://input'), true) ?? $_POST; + $action = $input['action'] ?? ''; + + if ($action === 'create') { + $patient_id = $input['patient_id'] ?? ''; + $doctor_id = $input['doctor_id'] ?? ''; + $start_time = $input['start_time'] ?? ''; + $reason = $input['reason'] ?? ''; + + if ($patient_id && $doctor_id && $start_time) { + $stmt = $db->prepare("INSERT INTO appointments (patient_id, doctor_id, start_time, end_time, reason) VALUES (?, ?, ?, DATE_ADD(?, INTERVAL 30 MINUTE), ?)"); + $stmt->execute([$patient_id, $doctor_id, $start_time, $start_time, $reason]); + echo json_encode(['success' => true, 'id' => $db->lastInsertId()]); + } else { + echo json_encode(['success' => false, 'error' => 'Missing fields']); + } + } elseif ($action === 'update') { + $id = $input['id'] ?? ''; + $patient_id = $input['patient_id'] ?? ''; + $doctor_id = $input['doctor_id'] ?? ''; + $start_time = $input['start_time'] ?? ''; + $status = $input['status'] ?? 'Scheduled'; + $reason = $input['reason'] ?? ''; + + if ($id && $patient_id && $doctor_id && $start_time) { + $stmt = $db->prepare("UPDATE appointments SET patient_id = ?, doctor_id = ?, start_time = ?, end_time = DATE_ADD(?, INTERVAL 30 MINUTE), status = ?, reason = ? WHERE id = ?"); + $stmt->execute([$patient_id, $doctor_id, $start_time, $start_time, $status, $reason, $id]); + echo json_encode(['success' => true]); + } else { + echo json_encode(['success' => false, 'error' => 'Missing fields']); + } + } elseif ($action === 'delete') { + $id = $input['id'] ?? ''; + if ($id) { + $stmt = $db->prepare("DELETE FROM appointments WHERE id = ?"); + $stmt->execute([$id]); + echo json_encode(['success' => true]); + } else { + echo json_encode(['success' => false, 'error' => 'Missing ID']); + } + } + exit; +} diff --git a/appointments.php b/appointments.php new file mode 100644 index 0000000..2da6959 --- /dev/null +++ b/appointments.php @@ -0,0 +1,12 @@ +prepare("INSERT INTO appointments (patient_id, doctor_id, appointment_date, reason) VALUES (?, ?, ?, ?)"); - $stmt->execute([$patient_id, $doctor_id, $date, $reason]); + $stmt = $db->prepare("INSERT INTO appointments (patient_id, doctor_id, start_time, end_time, reason) VALUES (?, ?, ?, DATE_ADD(?, INTERVAL 30 MINUTE), ?)"); + $stmt->execute([$patient_id, $doctor_id, $date, $date, $reason]); $_SESSION['flash_message'] = __('book_appointment') . ' ' . __('successfully'); $redirect = true; } @@ -401,7 +401,6 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') { $patient_name = $_POST['patient_name'] ?? ''; $test_ids = $_POST['test_ids'] ?? []; $results = $_POST['results'] ?? []; - $ranges = $_POST['normal_ranges'] ?? []; $source = $_POST['source'] ?? 'Internal'; $date = $_POST['inquiry_date'] ?: date('Y-m-d H:i'); $status = $_POST['status'] ?? 'Pending'; @@ -414,10 +413,10 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') { $inquiry_id = $db->lastInsertId(); if (!empty($test_ids)) { - $testStmt = $db->prepare("INSERT INTO inquiry_tests (inquiry_id, test_id, result, normal_range) VALUES (?, ?, ?, ?)"); + $testStmt = $db->prepare("INSERT INTO inquiry_tests (inquiry_id, test_id, result) VALUES (?, ?, ?)"); foreach ($test_ids as $index => $tid) { if ($tid) { - $testStmt->execute([$inquiry_id, $tid, $results[$index] ?? '', $ranges[$index] ?? '']); + $testStmt->execute([$inquiry_id, $tid, $results[$index] ?? '']); } } } @@ -430,7 +429,6 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') { $patient_name = $_POST['patient_name'] ?? ''; $test_ids = $_POST['test_ids'] ?? []; $results = $_POST['results'] ?? []; - $ranges = $_POST['normal_ranges'] ?? []; $source = $_POST['source'] ?? 'Internal'; $date = $_POST['inquiry_date'] ?: date('Y-m-d H:i'); $status = $_POST['status'] ?? 'Pending'; @@ -446,10 +444,10 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') { $stmt->execute([$id]); if (!empty($test_ids)) { - $testStmt = $db->prepare("INSERT INTO inquiry_tests (inquiry_id, test_id, result, normal_range) VALUES (?, ?, ?, ?)"); + $testStmt = $db->prepare("INSERT INTO inquiry_tests (inquiry_id, test_id, result) VALUES (?, ?, ?)"); foreach ($test_ids as $index => $tid) { if ($tid) { - $testStmt->execute([$id, $tid, $results[$index] ?? '', $ranges[$index] ?? '']); + $testStmt->execute([$id, $tid, $results[$index] ?? '']); } } } @@ -465,6 +463,124 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') { $_SESSION['flash_message'] = __('delete') . ' ' . __('successfully'); $redirect = true; } + } elseif ($_POST['action'] === 'add_xray_group') { + $name_en = $_POST['name_en'] ?? ''; + $name_ar = $_POST['name_ar'] ?? ''; + if ($name_en && $name_ar) { + $stmt = $db->prepare("INSERT INTO xray_groups (name_en, name_ar) VALUES (?, ?)"); + $stmt->execute([$name_en, $name_ar]); + $_SESSION['flash_message'] = __('add_xray_group') . ' ' . __('successfully'); + $redirect = true; + } + } elseif ($_POST['action'] === 'edit_xray_group') { + $id = $_POST['id'] ?? ''; + $name_en = $_POST['name_en'] ?? ''; + $name_ar = $_POST['name_ar'] ?? ''; + if ($id && $name_en && $name_ar) { + $stmt = $db->prepare("UPDATE xray_groups SET name_en = ?, name_ar = ? WHERE id = ?"); + $stmt->execute([$name_en, $name_ar, $id]); + $_SESSION['flash_message'] = __('edit_xray_group') . ' ' . __('successfully'); + $redirect = true; + } + } elseif ($_POST['action'] === 'delete_xray_group') { + $id = $_POST['id'] ?? ''; + if ($id) { + $stmt = $db->prepare("DELETE FROM xray_groups WHERE id = ?"); + $stmt->execute([$id]); + $_SESSION['flash_message'] = __('delete') . ' ' . __('successfully'); + $redirect = true; + } + } elseif ($_POST['action'] === 'add_xray_test') { + $group_id = $_POST['group_id'] ?: null; + $name_en = $_POST['name_en'] ?? ''; + $name_ar = $_POST['name_ar'] ?? ''; + $price = $_POST['price'] ?? 0; + if ($name_en && $name_ar) { + $stmt = $db->prepare("INSERT INTO xray_tests (group_id, name_en, name_ar, price) VALUES (?, ?, ?, ?)"); + $stmt->execute([$group_id, $name_en, $name_ar, $price]); + $_SESSION['flash_message'] = __('add_xray_test') . ' ' . __('successfully'); + $redirect = true; + } + } elseif ($_POST['action'] === 'edit_xray_test') { + $id = $_POST['id'] ?? ''; + $group_id = $_POST['group_id'] ?: null; + $name_en = $_POST['name_en'] ?? ''; + $name_ar = $_POST['name_ar'] ?? ''; + $price = $_POST['price'] ?? 0; + if ($id && $name_en && $name_ar) { + $stmt = $db->prepare("UPDATE xray_tests SET group_id = ?, name_en = ?, name_ar = ?, price = ? WHERE id = ?"); + $stmt->execute([$group_id, $name_en, $name_ar, $price, $id]); + $_SESSION['flash_message'] = __('edit_xray_test') . ' ' . __('successfully'); + $redirect = true; + } + } elseif ($_POST['action'] === 'delete_xray_test') { + $id = $_POST['id'] ?? ''; + if ($id) { + $stmt = $db->prepare("DELETE FROM xray_tests WHERE id = ?"); + $stmt->execute([$id]); + $_SESSION['flash_message'] = __('delete') . ' ' . __('successfully'); + $redirect = true; + } + } elseif ($_POST['action'] === 'add_xray_inquiry') { + $patient_name = $_POST['patient_name'] ?? ''; + $xray_ids = $_POST['xray_ids'] ?? []; + $results = $_POST['results'] ?? []; + $source = $_POST['source'] ?? 'Internal'; + $date = $_POST['inquiry_date'] ?: date('Y-m-d H:i'); + $status = $_POST['status'] ?? 'Pending'; + $notes = $_POST['notes'] ?? ''; + if ($patient_name) { + $db->beginTransaction(); + $stmt = $db->prepare("INSERT INTO xray_inquiries (patient_name, source, inquiry_date, status, notes) VALUES (?, ?, ?, ?, ?)"); + $stmt->execute([$patient_name, $source, $date, $status, $notes]); + $inquiry_id = $db->lastInsertId(); + if (!empty($xray_ids)) { + $testStmt = $db->prepare("INSERT INTO xray_inquiry_items (inquiry_id, xray_id, result) VALUES (?, ?, ?)"); + foreach ($xray_ids as $index => $tid) { + if ($tid) { + $testStmt->execute([$inquiry_id, $tid, $results[$index] ?? '']); + } + } + } + $db->commit(); + $_SESSION['flash_message'] = __('add_xray_inquiry') . ' ' . __('successfully'); + $redirect = true; + } + } elseif ($_POST['action'] === 'edit_xray_inquiry') { + $id = $_POST['id'] ?? ''; + $patient_name = $_POST['patient_name'] ?? ''; + $xray_ids = $_POST['xray_ids'] ?? []; + $results = $_POST['results'] ?? []; + $source = $_POST['source'] ?? 'Internal'; + $date = $_POST['inquiry_date'] ?: date('Y-m-d H:i'); + $status = $_POST['status'] ?? 'Pending'; + $notes = $_POST['notes'] ?? ''; + if ($id && $patient_name) { + $db->beginTransaction(); + $stmt = $db->prepare("UPDATE xray_inquiries SET patient_name = ?, source = ?, inquiry_date = ?, status = ?, notes = ? WHERE id = ?"); + $stmt->execute([$patient_name, $source, $date, $status, $notes, $id]); + $stmt = $db->prepare("DELETE FROM xray_inquiry_items WHERE inquiry_id = ?"); + $stmt->execute([$id]); + if (!empty($xray_ids)) { + $testStmt = $db->prepare("INSERT INTO xray_inquiry_items (inquiry_id, xray_id, result) VALUES (?, ?, ?)"); + foreach ($xray_ids as $index => $tid) { + if ($tid) { + $testStmt->execute([$id, $tid, $results[$index] ?? '']); + } + } + } + $db->commit(); + $_SESSION['flash_message'] = __('edit_xray_inquiry') . ' ' . __('successfully'); + $redirect = true; + } + } elseif ($_POST['action'] === 'delete_xray_inquiry') { + $id = $_POST['id'] ?? ''; + if ($id) { + $stmt = $db->prepare("DELETE FROM xray_inquiries WHERE id = ?"); + $stmt->execute([$id]); + $_SESSION['flash_message'] = __('delete') . ' ' . __('successfully'); + $redirect = true; + } } } @@ -472,4 +588,4 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') { header("Location: " . $_SERVER['REQUEST_URI']); exit; } -} +} \ No newline at end of file diff --git a/includes/common_data.php b/includes/common_data.php index 28ab8f8..6cc20d5 100644 --- a/includes/common_data.php +++ b/includes/common_data.php @@ -8,11 +8,15 @@ $all_employees = $db->query("SELECT id, name_$lang as name FROM employees")->fet $all_poisons = $db->query("SELECT id, name_$lang as name FROM poisons")->fetchAll(); $all_insurance = $db->query("SELECT id, name_$lang as name FROM insurance_companies")->fetchAll(); $all_test_groups = $db->query("SELECT id, name_$lang as name FROM test_groups")->fetchAll(); -$all_tests = $db->query("SELECT id, name_$lang as name, price FROM laboratory_tests")->fetchAll(); +$all_tests = $db->query("SELECT id, name_$lang as name, price, normal_range FROM laboratory_tests")->fetchAll(); + +// X-Ray Data +$all_xray_groups = $db->query("SELECT id, name_$lang as name FROM xray_groups")->fetchAll(); +$all_xrays = $db->query("SELECT id, name_$lang as name, price FROM xray_tests")->fetchAll(); $scheduled_appointments = $db->query(" - SELECT a.id, p.name as patient_name, a.appointment_date, a.patient_id, a.doctor_id + SELECT a.id, p.name as patient_name, a.start_time, a.patient_id, a.doctor_id FROM appointments a JOIN patients p ON a.patient_id = p.id WHERE a.status = 'Scheduled' - ORDER BY a.appointment_date ASC")->fetchAll(); \ No newline at end of file + ORDER BY a.start_time ASC")->fetchAll(); \ No newline at end of file diff --git a/includes/layout/footer.php b/includes/layout/footer.php index 365fa78..1f7271f 100644 --- a/includes/layout/footer.php +++ b/includes/layout/footer.php @@ -1236,7 +1236,7 @@ @@ -1455,12 +1455,352 @@ + + +
+ + + + + + + + + + + + + + + + + + + + + + ++
+
| # | ++ | + | + |
|---|---|---|---|
| + + No groups found. + | +|||
| + | + | + |
+
+
+ |
+
| # | ++ | + | + | + | + |
|---|---|---|---|---|---|
| + + No inquiries found. + | +|||||
| + | + | + | + + + | ++ + + + : + + + | +
+
+
+ |
+
| # | ++ | + | + | + |
|---|---|---|---|---|
| + + + | +||||
| + |
+
+
+
+
+
+
+
+
+
+ |
+ + + + + | ++ |
+
+
+ |
+