| | 1 | = UseCase13 - Create / Update Billing Record = |
| | 2 | |
| | 3 | == Initiating Actor - `System Administrator / Billing Admin ` == |
| | 4 | |
| | 5 | == Description == |
| | 6 | The system automatically creates and updates billing records when lab tests or procedures are requested. Billing admins can view pending records, inspect itemized costs, and update payment status when payment is received. |
| | 7 | |
| | 8 | == Scenario == |
| | 9 | |
| | 10 | 1. Doctor requests a lab test and the system calculates the total cost for tests requested on that date. |
| | 11 | |
| | 12 | {{{ |
| | 13 | #!sql |
| | 14 | SELECT SUM(lt.cost) AS total_cost |
| | 15 | FROM performed_lab_tests plt |
| | 16 | JOIN lab_tests lt ON plt.test_id = lt.test_id |
| | 17 | WHERE plt.patient_id = ( |
| | 18 | SELECT patient_id |
| | 19 | FROM users |
| | 20 | WHERE username = 'maja.veljanova') |
| | 21 | AND plt.test_date = '2026-06-05'; |
| | 22 | }}} |
| | 23 | |
| | 24 | 2. System creates a billing record for the patient on that date. |
| | 25 | |
| | 26 | {{{ |
| | 27 | #!sql |
| | 28 | INSERT INTO billing (total_cost, payment_status, record_id, admin_id) |
| | 29 | VALUES ( |
| | 30 | 75.00, |
| | 31 | 'PENDING', |
| | 32 | (SELECT record_id FROM medical_records WHERE patient_id = ( |
| | 33 | SELECT patient_id |
| | 34 | FROM users |
| | 35 | WHERE username = 'maja.veljanova')), |
| | 36 | ( |
| | 37 | SELECT admin_id |
| | 38 | FROM admin LIMIT 1) |
| | 39 | ) |
| | 40 | RETURNING bill_id; |
| | 41 | }}} |
| | 42 | |
| | 43 | 3. System links the performed lab test to the billing record. |
| | 44 | |
| | 45 | {{{ |
| | 46 | #!sql |
| | 47 | INSERT INTO billing_lab_tests (bill_id, test_id) |
| | 48 | VALUES ( |
| | 49 | (SELECT MAX(bill_id) FROM billing), |
| | 50 | (SELECT test_id FROM lab_tests WHERE test_name = 'Complete Blood Count') |
| | 51 | ); |
| | 52 | }}} |
| | 53 | |
| | 54 | 4. System links the performed procedure to the billing record. |
| | 55 | |
| | 56 | {{{ |
| | 57 | #!sql |
| | 58 | INSERT INTO billing_procedures (bill_id, procedure_id) |
| | 59 | VALUES ( |
| | 60 | (SELECT MAX(bill_id) FROM billing), |
| | 61 | (SELECT procedure_id FROM procedures WHERE procedure_type = 'ECG') |
| | 62 | ); |
| | 63 | }}} |
| | 64 | |
| | 65 | 5. Billing admin logs in and retrieves all pending billing records. |
| | 66 | |
| | 67 | {{{ |
| | 68 | #!sql |
| | 69 | SELECT |
| | 70 | b.bill_id, |
| | 71 | u_p.first_name AS patient_first_name, |
| | 72 | u_p.last_name AS patient_last_name, |
| | 73 | b.total_cost, |
| | 74 | b.payment_status |
| | 75 | FROM billing b |
| | 76 | JOIN medical_records mr ON b.record_id = mr.record_id |
| | 77 | JOIN patients p ON mr.patient_id = p.patient_id |
| | 78 | JOIN users u_p ON p.patient_id = u_p.patient_id |
| | 79 | WHERE b.payment_status = 'PENDING' |
| | 80 | ORDER BY b.bill_id; |
| | 81 | }}} |
| | 82 | |
| | 83 | 6. Admin views the detailed billing record with itemized costs. |
| | 84 | |
| | 85 | {{{ |
| | 86 | #!sql |
| | 87 | SELECT |
| | 88 | b.bill_id, |
| | 89 | b.total_cost, |
| | 90 | b.payment_status, |
| | 91 | u_p.first_name AS patient_first_name, |
| | 92 | u_p.last_name AS patient_last_name, |
| | 93 | p.embg, |
| | 94 | lt.test_name, |
| | 95 | lt.cost AS test_cost, |
| | 96 | pr.procedure_type, |
| | 97 | pr.cost AS procedure_cost |
| | 98 | FROM billing b |
| | 99 | JOIN medical_records mr ON b.record_id = mr.record_id |
| | 100 | JOIN patients p ON mr.patient_id = p.patient_id |
| | 101 | JOIN users u_p ON p.patient_id = u_p.patient_id |
| | 102 | LEFT JOIN billing_lab_tests blt ON b.bill_id = blt.bill_id |
| | 103 | LEFT JOIN lab_tests lt ON blt.test_id = lt.test_id |
| | 104 | LEFT JOIN billing_procedures bp ON b.bill_id = bp.bill_id |
| | 105 | LEFT JOIN procedures pr ON bp.procedure_id = pr.procedure_id |
| | 106 | WHERE b.bill_id = (SELECT MAX(bill_id) FROM billing); |
| | 107 | }}} |
| | 108 | |
| | 109 | 7. Admin marks the billing record as paid once payment is received. |
| | 110 | |
| | 111 | {{{ |
| | 112 | #!sql |
| | 113 | UPDATE billing |
| | 114 | SET payment_status = 'PAID', |
| | 115 | payment_date = '2026-06-09' |
| | 116 | WHERE bill_id = (SELECT MAX(bill_id) FROM billing WHERE payment_status = 'PENDING'); |
| | 117 | }}} |