prepare( "SELECT pv.*, p.patient_name, p.address, p.phone_number, u.username as doctor_name FROM patient_visits pv JOIN patients p ON pv.patient_id = p.id JOIN users u ON pv.doctor_id = u.id WHERE pv.visit_id = ?" ); $stmt->execute([$visit_id]); $visit = $stmt->fetch(PDO::FETCH_ASSOC); // Fetch ordered tests for the visit $tests_stmt = $pdo->prepare( "SELECT ot.test_type, CASE WHEN ot.test_type = 'lab' THEN lt.test_name WHEN ot.test_type = 'imaging' THEN it.test_name END as test_name, CASE WHEN ot.test_type = 'lab' THEN lt.cost WHEN ot.test_type = 'imaging' THEN it.cost END as cost FROM ordered_tests ot LEFT JOIN lab_tests lt ON ot.test_type = 'lab' AND ot.test_id = lt.test_id LEFT JOIN imaging_tests it ON ot.test_type = 'imaging' AND ot.test_id = it.test_id WHERE ot.visit_id = ?" ); $tests_stmt->execute([$visit_id]); $ordered_tests = $tests_stmt->fetchAll(PDO::FETCH_ASSOC); } catch (PDOException $e) { $visit = null; $ordered_tests = []; // In a real app, you would log this error } if (!$visit) { die("No completed visit found for this ID or an error occurred."); } // Calculate total bill $total_bill = $visit['cost']; foreach ($ordered_tests as $test) { $total_bill += $test['cost']; } ?>
Invoice #: INV-
Date:
Status:
Consulting Doctor: Dr.
| Item | Cost |
|---|---|
| (Consultation) | $ |
| ( Test) | $ |
| Total: | $ |