diff --git a/add_prescription.php b/add_prescription.php new file mode 100644 index 0000000..106e476 --- /dev/null +++ b/add_prescription.php @@ -0,0 +1,43 @@ + false, 'message' => 'An unknown error occurred.']; + +if ($_SERVER['REQUEST_METHOD'] === 'POST') { + $visit_id = $_POST['visit_id'] ?? null; + $patient_id = $_POST['patient_id'] ?? null; + $medication = $_POST['medication'] ?? null; + $dosage = $_POST['dosage'] ?? null; + $frequency = $_POST['frequency'] ?? null; + $notes = $_POST['notes'] ?? ''; + $doctor_id = $_SESSION['user_id'] ?? null; + + if ($visit_id && $patient_id && $doctor_id && $medication && $dosage && $frequency) { + try { + $pdo = db(); + $stmt = $pdo->prepare( + "INSERT INTO prescriptions (visit_id, patient_id, doctor_id, medication, dosage, frequency, notes) + VALUES (?, ?, ?, ?, ?, ?, ?)" + ); + $stmt->execute([$visit_id, $patient_id, $doctor_id, $medication, $dosage, $frequency, $notes]); + + $response['success'] = true; + $response['message'] = 'Prescription saved successfully.'; + + } catch (PDOException $e) { + // In a real app, log this error instead of echoing it. + $response['message'] = 'Database error: ' . $e->getMessage(); + } + } else { + $response['message'] = 'Invalid or missing data provided.'; + } +} else { + $response['message'] = 'Invalid request method.'; +} + +echo json_encode($response); +?> \ No newline at end of file diff --git a/alter_visits_table.php b/alter_visits_table.php new file mode 100644 index 0000000..dd3d163 --- /dev/null +++ b/alter_visits_table.php @@ -0,0 +1,12 @@ +exec($sql); + echo "Table 'patient_visits' altered successfully. Column 'id' renamed to 'visit_id'.\n"; +} catch (PDOException $e) { + die("Database error: " . $e->getMessage()); +} +?> \ No newline at end of file diff --git a/billing.php b/billing.php index fea00f0..315cf3d 100644 --- a/billing.php +++ b/billing.php @@ -5,13 +5,12 @@ require_once 'db/config.php'; // Handle status update if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'mark_paid') { - $patient_id_to_update = $_POST['patient_id'] ?? null; - if ($patient_id_to_update) { + $visit_id_to_update = $_POST['visit_id'] ?? null; + if ($visit_id_to_update) { try { $pdo = db(); - $stmt = $pdo->prepare("UPDATE patients SET payment_status = 'paid' WHERE id = ?"); - $stmt->execute([$patient_id_to_update]); - // Redirect to avoid form resubmission + $stmt = $pdo->prepare("UPDATE patient_visits SET payment_status = 'paid' WHERE visit_id = ?"); + $stmt->execute([$visit_id_to_update]); header("Location: billing.php"); exit; } catch (PDOException $e) { @@ -20,15 +19,31 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST[' } } -// Fetch unpaid patients +// Fetch unpaid visits and calculate total cost try { $pdo = db(); - $stmt = $pdo->prepare("SELECT p.*, u.username as doctor_name FROM patients p JOIN users u ON p.doctor_id = u.id WHERE p.status = 'Completed' AND p.payment_status = 'unpaid' ORDER BY p.updated_at DESC"); + $stmt = $pdo->prepare( + "SELECT + pv.visit_id, + pv.cost as consultation_fee, + p.patient_name, + p.id as patient_id, + u.username as doctor_name, + pv.service_rendered, + COALESCE((SELECT SUM(lt.cost) FROM ordered_tests ot JOIN lab_tests lt ON ot.test_id = lt.test_id WHERE ot.visit_id = pv.visit_id AND ot.test_type = 'lab'), 0) as lab_tests_cost, + COALESCE((SELECT SUM(it.cost) FROM ordered_tests ot JOIN imaging_tests it ON ot.test_id = it.test_id WHERE ot.visit_id = pv.visit_id AND ot.test_type = 'imaging'), 0) as imaging_tests_cost + FROM patient_visits pv + JOIN patients p ON pv.patient_id = p.id + JOIN users u ON pv.doctor_id = u.id + WHERE pv.status = 'Completed' AND pv.payment_status = 'unpaid' + ORDER BY pv.visit_time DESC" + ); $stmt->execute(); - $unpaid_patients = $stmt->fetchAll(PDO::FETCH_ASSOC); + $unpaid_visits = $stmt->fetchAll(PDO::FETCH_ASSOC); } catch (PDOException $e) { - $unpaid_patients = []; - // Log error + $unpaid_visits = []; + // You should log the error in a real application + // error_log($e->getMessage()); } ?> @@ -101,26 +116,27 @@ try {