= UseCase06 - Doctor Requests Lab Test = == Initiating Actor - `Doctor` == == Description == A doctor requests a laboratory test for a patient. The system creates a performed lab test record linking the doctor, patient, and the specific test. == Scenario == 1. Doctor opens a patient's medical record. {{{ #!sql SELECT mr.record_id, mr.patient_id FROM medical_records mr WHERE mr.patient_id = ( SELECT patient_id FROM users WHERE username = 'maja.veljanova' ); }}} 2. System displays all available lab tests. {{{ #!sql SELECT lt.test_id, lt.test_name, lt.description, lt.cost FROM lab_tests lt ORDER BY lt.test_name; }}} 3. Doctor selects a lab test, specifies the test date and notes, and submits the request. {{{ #!sql INSERT INTO performed_lab_tests (patient_id, doctor_id, test_id, test_date, notes) VALUES ( (SELECT patient_id FROM users WHERE username = 'maja.veljanova'), (SELECT doctor_id FROM users WHERE username = 'elena.kirova'), (SELECT test_id FROM lab_tests WHERE test_name = 'Complete Blood Count'), '2026-06-05', 'Blood work for hypertension monitoring' ) RETURNING performed_test_id; }}} 4. System calculates the total cost of lab tests performed on that date for this patient. {{{ #!sql SELECT SUM(lt.cost) AS total_cost FROM performed_lab_tests plt JOIN lab_tests lt ON plt.test_id = lt.test_id WHERE plt.patient_id = ( SELECT patient_id FROM users WHERE username = 'maja.veljanova') AND plt.test_date = '2026-06-05'; }}} 5. System creates a billing record for the patient. {{{ #!sql INSERT INTO billing (total_cost, payment_status, record_id, admin_id) VALUES ( 75.00, 'PENDING', (SELECT record_id FROM medical_records WHERE patient_id = (SELECT patient_id FROM users WHERE username = 'maja.veljanova')), (SELECT admin_id FROM admin LIMIT 1) ) RETURNING bill_id; }}} 6. System links the billing record to the performed lab test. {{{ #!sql INSERT INTO billing_lab_tests (bill_id, test_id) VALUES ( (SELECT MAX(bill_id) FROM billing), (SELECT test_id FROM lab_tests WHERE test_name = 'Complete Blood Count') ); }}} 7. System confirms the request and displays all pending lab tests for the patient. {{{ #!sql SELECT plt.performed_test_id, lt.test_name, plt.test_date, plt.notes FROM performed_lab_tests plt JOIN lab_tests lt ON plt.test_id = lt.test_id WHERE plt.patient_id = ( SELECT patient_id FROM users WHERE username = 'maja.veljanova'); }}}