Spaces:
Sleeping
Sleeping
File size: 19,601 Bytes
6931883 4d27410 6931883 4d27410 6931883 94b3580 4d27410 94b3580 6931883 94b3580 6931883 f01496a 6931883 4d27410 6931883 94b3580 4d27410 43d9b90 94b3580 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 | CREATE DATABASE IF NOT EXISTS care_people
CHARACTER SET utf8mb4
COLLATE utf8mb4_unicode_ci;
USE care_people;
CREATE TABLE IF NOT EXISTS doctors (
id VARCHAR(10) PRIMARY KEY,
name VARCHAR(100) NOT NULL,
department VARCHAR(50) NOT NULL,
designation VARCHAR(100) NOT NULL,
degrees VARCHAR(200) NOT NULL,
profile_image_url LONGTEXT NULL,
room_number VARCHAR(10) NOT NULL,
consultation_fee DECIMAL(10, 2) NOT NULL,
consultation_days JSON NOT NULL,
consultation_times VARCHAR(100) NOT NULL,
password_hash VARCHAR(255) NOT NULL,
is_active TINYINT(1) NOT NULL DEFAULT 1,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS patients (
phone_number VARCHAR(15) PRIMARY KEY,
email VARCHAR(255) NULL,
name VARCHAR(100) NOT NULL,
profile_image_url LONGTEXT NULL,
date_of_birth DATE NOT NULL,
address TEXT NOT NULL,
gender ENUM('male', 'female', 'other') NOT NULL DEFAULT 'male',
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME NULL DEFAULT NULL,
UNIQUE KEY uniq_patients_email (email)
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS administrators (
id VARCHAR(10) PRIMARY KEY,
name VARCHAR(100) NOT NULL,
email VARCHAR(255) NOT NULL UNIQUE,
profile_image_url LONGTEXT NULL,
password_hash VARCHAR(255) NOT NULL,
is_active TINYINT(1) NOT NULL DEFAULT 1,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS sessions (
id CHAR(36) PRIMARY KEY,
user_type ENUM('patient', 'doctor', 'administrator') NOT NULL,
user_identifier VARCHAR(32) NOT NULL,
refresh_token_hash CHAR(64) NOT NULL,
login_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
expires_at DATETIME NOT NULL,
revoked_at DATETIME NULL DEFAULT NULL,
metadata_json JSON NULL,
INDEX idx_sessions_user (user_type, user_identifier),
INDEX idx_sessions_expires (expires_at)
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS appointments (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
doctor_id VARCHAR(10) NOT NULL,
patient_phone VARCHAR(15) NOT NULL,
appointment_date DATE NOT NULL,
time_slot VARCHAR(20) NOT NULL,
serial_number INT NOT NULL,
status ENUM('confirmed', 'cancelled', 'completed') NOT NULL DEFAULT 'confirmed',
notes TEXT NULL,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
UNIQUE KEY uniq_appointment_slot (doctor_id, appointment_date, time_slot),
INDEX idx_appointments_doctor_date (doctor_id, appointment_date),
INDEX idx_appointments_patient_date (patient_phone, appointment_date),
CONSTRAINT fk_appointments_doctor
FOREIGN KEY (doctor_id) REFERENCES doctors(id)
ON DELETE RESTRICT
ON UPDATE CASCADE,
CONSTRAINT fk_appointments_patient
FOREIGN KEY (patient_phone) REFERENCES patients(phone_number)
ON DELETE RESTRICT
ON UPDATE CASCADE
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS reschedule_requests (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
appointment_id BIGINT UNSIGNED NOT NULL,
requested_date DATE NOT NULL,
requested_time_slot VARCHAR(20) NOT NULL,
reason TEXT NULL,
status ENUM('pending', 'approved', 'rejected') NOT NULL DEFAULT 'pending',
responded_at DATETIME NULL DEFAULT NULL,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
INDEX idx_reschedule_requests_appt_status (appointment_id, status),
CONSTRAINT fk_reschedule_requests_appointment
FOREIGN KEY (appointment_id) REFERENCES appointments(id)
ON DELETE CASCADE
ON UPDATE CASCADE
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS prescriptions (
id VARCHAR(40) PRIMARY KEY,
doctor_id VARCHAR(10) NOT NULL,
patient_phone VARCHAR(15) NOT NULL,
appointment_id BIGINT UNSIGNED NULL,
appointment_date DATE NOT NULL,
issued_at DATETIME NOT NULL,
additional_notes TEXT NULL,
pdf_path VARCHAR(500) NULL,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
INDEX idx_prescriptions_doctor_issue (doctor_id, issued_at),
INDEX idx_prescriptions_patient_issue (patient_phone, issued_at),
CONSTRAINT fk_prescriptions_doctor
FOREIGN KEY (doctor_id) REFERENCES doctors(id)
ON DELETE RESTRICT
ON UPDATE CASCADE,
CONSTRAINT fk_prescriptions_patient
FOREIGN KEY (patient_phone) REFERENCES patients(phone_number)
ON DELETE RESTRICT
ON UPDATE CASCADE,
CONSTRAINT fk_prescriptions_appointment
FOREIGN KEY (appointment_id) REFERENCES appointments(id)
ON DELETE SET NULL
ON UPDATE CASCADE
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS prescription_diagnoses (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
prescription_id VARCHAR(40) NOT NULL,
diagnosis_text TEXT NOT NULL,
sort_order INT NOT NULL DEFAULT 0,
INDEX idx_prescription_diagnoses_rx (prescription_id, sort_order),
CONSTRAINT fk_prescription_diagnoses_prescription
FOREIGN KEY (prescription_id) REFERENCES prescriptions(id)
ON DELETE CASCADE
ON UPDATE CASCADE
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS prescribed_medicines (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
prescription_id VARCHAR(40) NOT NULL,
name VARCHAR(200) NOT NULL,
dosage VARCHAR(100) NOT NULL,
frequency VARCHAR(100) NOT NULL,
duration VARCHAR(100) NOT NULL,
times_per_day INT NULL,
time_slots_json JSON NULL,
duration_days INT NULL,
meal_relation ENUM('before_meal', 'after_meal', 'any') NULL,
notes TEXT NULL,
INDEX idx_prescribed_medicines_rx (prescription_id),
CONSTRAINT fk_prescribed_medicines_prescription
FOREIGN KEY (prescription_id) REFERENCES prescriptions(id)
ON DELETE CASCADE
ON UPDATE CASCADE
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS test_catalog (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
code VARCHAR(40) NOT NULL UNIQUE,
name VARCHAR(180) NOT NULL,
department VARCHAR(120) NOT NULL,
room_number VARCHAR(40) NOT NULL,
required_specimen VARCHAR(180) NULL,
preparation_instructions TEXT NULL,
estimated_price DECIMAL(12, 2) NOT NULL DEFAULT 0,
expected_turnaround_hours INT NOT NULL DEFAULT 24,
is_active TINYINT(1) NOT NULL DEFAULT 1,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
INDEX idx_test_catalog_name (name),
INDEX idx_test_catalog_department (department)
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS doctor_prescription_templates (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
doctor_id VARCHAR(10) NOT NULL,
title VARCHAR(160) NOT NULL,
diagnosis_json JSON NOT NULL,
medicines_json JSON NOT NULL,
tests_json JSON NULL,
additional_notes TEXT NULL,
is_default TINYINT(1) NOT NULL DEFAULT 0,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
INDEX idx_templates_doctor (doctor_id, created_at),
CONSTRAINT fk_templates_doctor
FOREIGN KEY (doctor_id) REFERENCES doctors(id)
ON DELETE CASCADE
ON UPDATE CASCADE
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS prescription_tests (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
prescription_id VARCHAR(40) NOT NULL,
doctor_id VARCHAR(10) NOT NULL,
patient_phone VARCHAR(15) NOT NULL,
catalog_test_id BIGINT UNSIGNED NULL,
test_name VARCHAR(180) NOT NULL,
department VARCHAR(120) NOT NULL,
room_number VARCHAR(40) NOT NULL,
required_specimen VARCHAR(180) NULL,
preparation_instructions TEXT NULL,
estimated_price DECIMAL(12, 2) NOT NULL DEFAULT 0,
status ENUM('pending', 'ordered', 'cancelled', 'completed') NOT NULL DEFAULT 'pending',
report_summary TEXT NULL,
report_file_url VARCHAR(500) NULL,
report_updated_at DATETIME NULL,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
INDEX idx_prescription_tests_rx (prescription_id),
INDEX idx_prescription_tests_patient_status (patient_phone, status),
INDEX idx_prescription_tests_doctor_status (doctor_id, status),
CONSTRAINT fk_prescription_tests_prescription
FOREIGN KEY (prescription_id) REFERENCES prescriptions(id)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT fk_prescription_tests_doctor
FOREIGN KEY (doctor_id) REFERENCES doctors(id)
ON DELETE RESTRICT
ON UPDATE CASCADE,
CONSTRAINT fk_prescription_tests_patient
FOREIGN KEY (patient_phone) REFERENCES patients(phone_number)
ON DELETE RESTRICT
ON UPDATE CASCADE,
CONSTRAINT fk_prescription_tests_catalog
FOREIGN KEY (catalog_test_id) REFERENCES test_catalog(id)
ON DELETE SET NULL
ON UPDATE CASCADE
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS lab_test_orders (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
prescription_test_id BIGINT UNSIGNED NOT NULL,
prescription_id VARCHAR(40) NOT NULL,
doctor_id VARCHAR(10) NOT NULL,
patient_phone VARCHAR(15) NOT NULL,
status ENUM('ordered', 'cancelled', 'sample_collected', 'processing', 'reported') NOT NULL DEFAULT 'ordered',
instruction_text TEXT NULL,
scheduled_at DATETIME NULL,
specimen_room VARCHAR(40) NULL,
report_summary TEXT NULL,
report_file_url VARCHAR(500) NULL,
ordered_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
INDEX idx_lab_orders_patient_status (patient_phone, status),
INDEX idx_lab_orders_doctor_status (doctor_id, status),
INDEX idx_lab_orders_test (prescription_test_id),
CONSTRAINT fk_lab_orders_prescription_test
FOREIGN KEY (prescription_test_id) REFERENCES prescription_tests(id)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT fk_lab_orders_prescription
FOREIGN KEY (prescription_id) REFERENCES prescriptions(id)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT fk_lab_orders_doctor
FOREIGN KEY (doctor_id) REFERENCES doctors(id)
ON DELETE RESTRICT
ON UPDATE CASCADE,
CONSTRAINT fk_lab_orders_patient
FOREIGN KEY (patient_phone) REFERENCES patients(phone_number)
ON DELETE RESTRICT
ON UPDATE CASCADE
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS medication_reminders (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
prescription_id VARCHAR(40) NOT NULL,
patient_phone VARCHAR(15) NOT NULL,
medicine_name VARCHAR(200) NOT NULL,
dosage VARCHAR(120) NULL,
intake_instruction VARCHAR(120) NULL,
times_per_day INT NOT NULL,
time_slots_json JSON NOT NULL,
start_date DATE NOT NULL,
duration_days INT NOT NULL,
status ENUM('active', 'completed', 'cancelled') NOT NULL DEFAULT 'active',
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
INDEX idx_medication_reminders_patient (patient_phone, start_date),
INDEX idx_medication_reminders_rx (prescription_id),
CONSTRAINT fk_medication_reminders_prescription
FOREIGN KEY (prescription_id) REFERENCES prescriptions(id)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT fk_medication_reminders_patient
FOREIGN KEY (patient_phone) REFERENCES patients(phone_number)
ON DELETE CASCADE
ON UPDATE CASCADE
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS medication_reminder_occurrences (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
reminder_id BIGINT UNSIGNED NOT NULL,
patient_phone VARCHAR(15) NOT NULL,
scheduled_for DATETIME NOT NULL,
status ENUM('scheduled', 'notified', 'dismissed', 'taken', 'skipped') NOT NULL DEFAULT 'scheduled',
notification_title VARCHAR(180) NULL,
notification_body TEXT NULL,
notified_at DATETIME NULL,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
INDEX idx_reminder_occurrences_patient_time (patient_phone, scheduled_for),
INDEX idx_reminder_occurrences_status_time (status, scheduled_for),
CONSTRAINT fk_reminder_occurrences_reminder
FOREIGN KEY (reminder_id) REFERENCES medication_reminders(id)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT fk_reminder_occurrences_patient
FOREIGN KEY (patient_phone) REFERENCES patients(phone_number)
ON DELETE CASCADE
ON UPDATE CASCADE
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS in_app_notifications (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
user_type ENUM('patient', 'doctor', 'administrator') NOT NULL,
user_identifier VARCHAR(32) NOT NULL,
category VARCHAR(80) NOT NULL,
title VARCHAR(180) NOT NULL,
body TEXT NOT NULL,
metadata_json JSON NULL,
read_at DATETIME NULL,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
INDEX idx_in_app_notifications_user (user_type, user_identifier, created_at),
INDEX idx_in_app_notifications_unread (user_type, user_identifier, read_at)
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS push_device_tokens (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
user_type ENUM('patient', 'doctor', 'administrator') NOT NULL,
user_identifier VARCHAR(32) NOT NULL,
platform ENUM('android', 'ios', 'web') NOT NULL DEFAULT 'android',
device_id VARCHAR(128) NOT NULL,
token VARCHAR(255) NOT NULL,
app_version VARCHAR(30) NULL,
device_model VARCHAR(120) NULL,
locale VARCHAR(20) NULL,
timezone VARCHAR(60) NULL,
is_active TINYINT(1) NOT NULL DEFAULT 1,
last_seen_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
UNIQUE KEY uniq_push_device_token (token),
UNIQUE KEY uniq_push_user_device (user_type, user_identifier, platform, device_id),
INDEX idx_push_tokens_user_active (user_type, user_identifier, is_active),
INDEX idx_push_tokens_last_seen (last_seen_at)
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS admissions (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
patient_phone VARCHAR(15) NOT NULL,
admitting_doctor_id VARCHAR(10) NOT NULL,
ward VARCHAR(60) NOT NULL,
bed_number VARCHAR(30) NULL,
admission_reason TEXT NOT NULL,
status ENUM('admitted', 'discharged') NOT NULL DEFAULT 'admitted',
admitted_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
expected_discharge_at DATETIME NULL,
discharged_at DATETIME NULL,
discharge_summary TEXT NULL,
created_by_type ENUM('doctor', 'administrator', 'system') NOT NULL DEFAULT 'system',
created_by_identifier VARCHAR(32) NULL,
updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
INDEX idx_admissions_patient_status (patient_phone, status),
INDEX idx_admissions_doctor_status (admitting_doctor_id, status),
INDEX idx_admissions_admitted_at (admitted_at),
CONSTRAINT fk_admissions_patient
FOREIGN KEY (patient_phone) REFERENCES patients(phone_number)
ON DELETE RESTRICT
ON UPDATE CASCADE,
CONSTRAINT fk_admissions_doctor
FOREIGN KEY (admitting_doctor_id) REFERENCES doctors(id)
ON DELETE RESTRICT
ON UPDATE CASCADE
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS bills (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
bill_number VARCHAR(40) NOT NULL UNIQUE,
patient_phone VARCHAR(15) NOT NULL,
admission_id BIGINT UNSIGNED NULL,
status ENUM('issued', 'partially_paid', 'paid', 'cancelled') NOT NULL DEFAULT 'issued',
subtotal DECIMAL(12, 2) NOT NULL DEFAULT 0,
discount_amount DECIMAL(12, 2) NOT NULL DEFAULT 0,
tax_amount DECIMAL(12, 2) NOT NULL DEFAULT 0,
total_amount DECIMAL(12, 2) NOT NULL DEFAULT 0,
paid_amount DECIMAL(12, 2) NOT NULL DEFAULT 0,
currency_code VARCHAR(8) NOT NULL DEFAULT 'BDT',
notes TEXT NULL,
issued_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
due_at DATETIME NULL,
paid_at DATETIME NULL,
created_by_type ENUM('doctor', 'administrator', 'system') NOT NULL DEFAULT 'system',
created_by_identifier VARCHAR(32) NULL,
updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
INDEX idx_bills_patient_status (patient_phone, status),
INDEX idx_bills_issued_at (issued_at),
CONSTRAINT fk_bills_patient
FOREIGN KEY (patient_phone) REFERENCES patients(phone_number)
ON DELETE RESTRICT
ON UPDATE CASCADE,
CONSTRAINT fk_bills_admission
FOREIGN KEY (admission_id) REFERENCES admissions(id)
ON DELETE SET NULL
ON UPDATE CASCADE
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS bill_line_items (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
bill_id BIGINT UNSIGNED NOT NULL,
description VARCHAR(255) NOT NULL,
quantity DECIMAL(10, 2) NOT NULL,
unit_price DECIMAL(12, 2) NOT NULL,
line_total DECIMAL(12, 2) NOT NULL,
INDEX idx_bill_line_items_bill (bill_id),
CONSTRAINT fk_bill_line_items_bill
FOREIGN KEY (bill_id) REFERENCES bills(id)
ON DELETE CASCADE
ON UPDATE CASCADE
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS inventory_items (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
sku VARCHAR(60) NOT NULL UNIQUE,
name VARCHAR(150) NOT NULL,
category VARCHAR(80) NULL,
unit_of_measure VARCHAR(20) NOT NULL DEFAULT 'unit',
quantity_on_hand DECIMAL(12, 2) NOT NULL DEFAULT 0,
reorder_level DECIMAL(12, 2) NOT NULL DEFAULT 0,
unit_cost DECIMAL(12, 2) NOT NULL DEFAULT 0,
is_active TINYINT(1) NOT NULL DEFAULT 1,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
INDEX idx_inventory_items_name (name),
INDEX idx_inventory_items_reorder (quantity_on_hand, reorder_level)
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS inventory_transactions (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
item_id BIGINT UNSIGNED NOT NULL,
transaction_type ENUM('in', 'out', 'adjust') NOT NULL,
quantity DECIMAL(12, 2) NOT NULL,
quantity_before DECIMAL(12, 2) NOT NULL,
quantity_after DECIMAL(12, 2) NOT NULL,
unit_cost DECIMAL(12, 2) NULL,
reference_type VARCHAR(50) NULL,
reference_id VARCHAR(64) NULL,
notes TEXT NULL,
performed_by_type ENUM('doctor', 'administrator', 'system') NOT NULL DEFAULT 'system',
performed_by_identifier VARCHAR(32) NULL,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
INDEX idx_inventory_txn_item_date (item_id, created_at),
INDEX idx_inventory_txn_type_date (transaction_type, created_at),
CONSTRAINT fk_inventory_transactions_item
FOREIGN KEY (item_id) REFERENCES inventory_items(id)
ON DELETE RESTRICT
ON UPDATE CASCADE
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS audit_logs (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
event_type VARCHAR(80) NOT NULL,
action VARCHAR(80) NOT NULL,
actor_type ENUM('patient', 'doctor', 'administrator', 'system') NOT NULL,
actor_identifier VARCHAR(32) NULL,
entity_type VARCHAR(80) NOT NULL,
entity_id VARCHAR(64) NULL,
metadata_json JSON NULL,
ip_address VARCHAR(64) NULL,
user_agent VARCHAR(255) NULL,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
INDEX idx_audit_logs_created (created_at),
INDEX idx_audit_logs_event (event_type, created_at),
INDEX idx_audit_logs_actor (actor_type, actor_identifier, created_at),
INDEX idx_audit_logs_entity (entity_type, entity_id, created_at)
) ENGINE=InnoDB;
|