prepare("INSERT INTO patients (name, phone, dob, gender, blood_group, address, insurance_company_id, policy_number) VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
$stmt->execute([$name, $phone, $dob, $gender, $blood_group, $address, $insurance_company_id, $policy_number]);
$message = __('add_patient') . ' ' . __('successfully');
}
} elseif ($_POST['action'] === 'add_insurance') {
$name_en = $_POST['name_en'] ?? '';
$name_ar = $_POST['name_ar'] ?? '';
$email = $_POST['email'] ?? '';
$phone = $_POST['phone'] ?? '';
$contact = $_POST['contact_info'] ?? '';
if ($name_en && $name_ar) {
$stmt = $db->prepare("INSERT INTO insurance_companies (name_en, name_ar, email, phone, contact_info) VALUES (?, ?, ?, ?, ?)");
$stmt->execute([$name_en, $name_ar, $email, $phone, $contact]);
$message = __('insurance_company') . ' ' . __('successfully');
}
} elseif ($_POST['action'] === 'book_appointment') {
$patient_id = $_POST['patient_id'] ?? '';
$doctor_id = $_POST['doctor_id'] ?? '';
$date = $_POST['date'] ?? '';
$reason = $_POST['reason'] ?? '';
if ($patient_id && $doctor_id && $date) {
$stmt = $db->prepare("INSERT INTO appointments (patient_id, doctor_id, appointment_date, reason) VALUES (?, ?, ?, ?)");
$stmt->execute([$patient_id, $doctor_id, $date, $reason]);
$message = __('book_appointment') . ' ' . __('successfully');
}
} elseif ($_POST['action'] === 'record_visit') {
$patient_id = $_POST['patient_id'] ?? '';
$doctor_id = $_POST['doctor_id'] ?? '';
$appointment_id = $_POST['appointment_id'] ?: null;
$weight = $_POST['weight'] ?? '';
$bp = $_POST['blood_pressure'] ?? '';
$hr = $_POST['heart_rate'] ?? '';
$temp = $_POST['temperature'] ?? '';
$symptoms = $_POST['symptoms'] ?? '';
$diagnosis = $_POST['diagnosis'] ?? '';
$treatment = $_POST['treatment_plan'] ?? '';
if ($patient_id && $doctor_id) {
$stmt = $db->prepare("INSERT INTO visits (patient_id, doctor_id, appointment_id, weight, blood_pressure, heart_rate, temperature, symptoms, diagnosis, treatment_plan) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
$stmt->execute([$patient_id, $doctor_id, $appointment_id, $weight, $bp, $hr, $temp, $symptoms, $diagnosis, $treatment]);
if ($appointment_id) {
$db->prepare("UPDATE appointments SET status = 'Completed' WHERE id = ?")->execute([$appointment_id]);
}
$message = __('visit_recorded');
}
} elseif ($_POST['action'] === 'create_report') {
$visit_id = $_POST['visit_id'] ?? '';
$type = $_POST['report_type'] ?? '';
$findings = $_POST['findings'] ?? '';
$recommendations = $_POST['recommendations'] ?? '';
if ($visit_id && $type) {
$stmt = $db->prepare("INSERT INTO provisional_reports (visit_id, report_type, findings, recommendations) VALUES (?, ?, ?, ?)");
$stmt->execute([$visit_id, $type, $findings, $recommendations]);
$message = __('report_created');
}
} elseif ($_POST['action'] === 'create_bill') {
$patient_id = $_POST['patient_id'] ?? '';
$visit_id = $_POST['visit_id'] ?: null;
$items = $_POST['items'] ?? [];
$amounts = $_POST['amounts'] ?? [];
if ($patient_id && !empty($items)) {
$total = array_sum($amounts);
// Check if patient has insurance
$patient = $db->prepare("SELECT insurance_company_id FROM patients WHERE id = ?");
$patient->execute([$patient_id]);
$p_data = $patient->fetch();
$insurance_covered = 0;
if ($p_data && $p_data['insurance_company_id']) {
// Simple logic: insurance covers 80% if they have insurance
$insurance_covered = $total * 0.8;
}
$patient_payable = $total - $insurance_covered;
$stmt = $db->prepare("INSERT INTO bills (patient_id, visit_id, total_amount, insurance_covered, patient_payable, status) VALUES (?, ?, ?, ?, ?, 'Pending')");
$stmt->execute([$patient_id, $visit_id, $total, $insurance_covered, $patient_payable]);
$bill_id = $db->lastInsertId();
$item_stmt = $db->prepare("INSERT INTO bill_items (bill_id, description, amount) VALUES (?, ?, ?)");
foreach ($items as $index => $desc) {
if ($desc && isset($amounts[$index])) {
$item_stmt->execute([$bill_id, $desc, $amounts[$index]]);
}
}
$message = __('bill_created');
}
} elseif ($_POST['action'] === 'mark_paid') {
$bill_id = $_POST['bill_id'] ?? '';
if ($bill_id) {
$db->prepare("UPDATE bills SET status = 'Paid' WHERE id = ?")->execute([$bill_id]);
$message = __('bill_paid');
}
}
}
}
// Fetch Stats
$total_patients = $db->query("SELECT COUNT(*) FROM patients")->fetchColumn();
$today_appointments = $db->query("SELECT COUNT(*) FROM appointments WHERE DATE(appointment_date) = CURDATE()")->fetchColumn();
$total_visits = $db->query("SELECT COUNT(*) FROM visits")->fetchColumn();
$total_revenue = $db->query("SELECT SUM(total_amount) FROM bills WHERE status = 'Paid'")->fetchColumn() ?: 0;
$pending_revenue = $db->query("SELECT SUM(total_amount) FROM bills WHERE status = 'Pending'")->fetchColumn() ?: 0;
// Fetch Data based on section
if ($section === 'dashboard') {
$patients_sql = "
SELECT p.*, ic.name_$lang as insurance_name
FROM patients p
LEFT JOIN insurance_companies ic ON p.insurance_company_id = ic.id
ORDER BY p.id DESC LIMIT 5";
$patients = $db->query($patients_sql)->fetchAll();
$appointments_sql = "
SELECT a.*, 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
ORDER BY a.appointment_date DESC
LIMIT 5";
$appointments = $db->query($appointments_sql)->fetchAll();
} elseif ($section === 'patients') {
$patients_sql = "
SELECT p.*, ic.name_$lang as insurance_name
FROM patients p
LEFT JOIN insurance_companies ic ON p.insurance_company_id = ic.id
ORDER BY p.id DESC";
$patients = $db->query($patients_sql)->fetchAll();
} elseif ($section === 'visits') {
$visits_sql = "
SELECT v.*, p.name as patient_name, d.name_$lang as doctor_name
FROM visits v
JOIN patients p ON v.patient_id = p.id
JOIN doctors d ON v.doctor_id = d.id
ORDER BY v.visit_date DESC";
$visits = $db->query($visits_sql)->fetchAll();
} elseif ($section === 'billing') {
$bills_sql = "
SELECT b.*, p.name as patient_name
FROM bills b
JOIN patients p ON b.patient_id = p.id
ORDER BY b.created_at DESC";
$bills = $db->query($bills_sql)->fetchAll();
} elseif ($section === 'insurance') {
$insurance_companies = $db->query("SELECT * FROM insurance_companies ORDER BY id DESC")->fetchAll();
}
// Common data for selects
$all_doctors = $db->query("SELECT id, name_$lang as name FROM doctors")->fetchAll();
$all_patients = $db->query("SELECT id, name FROM patients")->fetchAll();
$all_insurance = $db->query("SELECT id, name_$lang as name FROM insurance_companies")->fetchAll();
$scheduled_appointments = $db->query("
SELECT a.id, p.name as patient_name, a.appointment_date, 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();
?>
| ID |
|
|
|
|
|
|
|
| # |
|
|
$ |
$ |
$ |
|
|
| No bills found. |
| ID |
(EN) |
(AR) |
|
|
|
| # |
|
|
|
|
|
| No insurance companies found. |