fix: auto-populate prescription content and lab content from pre-selected defaults

This commit is contained in:
SajjadMahmoody
2026-04-18 13:45:07 +03:30
parent bbb727cffe
commit 4abd856bf9
7 changed files with 147 additions and 22 deletions
+58 -19
View File
@@ -631,7 +631,7 @@ public static function table(Table $table): Table
if (! empty($arguments['delete'])) {
$existingAppointment = $record->surgeryAppointment;
SurgeryAppointment::where('patient_id', $record->id)->delete();
$existingAppointment?->delete();
if ($existingAppointment) {
\App\Filament\Pages\SmsLogsPage::cancelAppointmentMessages(
@@ -732,6 +732,7 @@ public static function table(Table $table): Table
$medicationIds = $prescription->medication_ids ?? [];
$labTestIds = $prescription->lab_test_ids ?? [];
$content = $prescription->content ?? '';
$labContent = $prescription->lab_content ?? '';
} else {
$defaultMeds = Medication::where('is_active', true)
->where('is_default', true)
@@ -743,22 +744,30 @@ public static function table(Table $table): Table
->orderBy('name')
->pluck('id')
->toArray();
$content = $defaultMeds->isNotEmpty()
? '<ul>' . $defaultMeds->map(fn ($m) =>
'<li>' . implode(' ', array_filter([
$m->dosage_form,
$m->name,
$m->strength,
$m->quantity ? '#' . $m->quantity : null,
$m->timing,
])) . '</li>'
)->implode('') . '</ul>'
: '';
$content = '';
$labContent = '';
}
$labContent = isset($prescription)
? ($prescription->lab_content ?? '')
: '';
if (empty($content) && ! empty($medicationIds)) {
$meds = Medication::whereIn('id', $medicationIds)->get()->keyBy('id');
$content = '<ul>' . collect($medicationIds)->map(fn ($id) => isset($meds[$id])
? '<li>' . implode(' ', array_filter([
$meds[$id]->dosage_form,
$meds[$id]->name,
$meds[$id]->strength,
$meds[$id]->quantity ? '#' . $meds[$id]->quantity : null,
$meds[$id]->timing,
])) . '</li>'
: ''
)->filter()->implode('') . '</ul>';
}
if (empty($labContent) && ! empty($labTestIds)) {
$tests = LabTest::whereIn('id', $labTestIds)->orderBy('name')->get();
$labContent = $tests->isNotEmpty()
? '<ul>' . $tests->map(fn ($t) => '<li>' . $t->name . '</li>')->implode('') . '</ul>'
: '';
}
return [
'medication_ids' => $medicationIds,
@@ -865,6 +874,13 @@ public static function table(Table $table): Table
)
->columns(2)
->live()
->afterStateHydrated(function (array $state, callable $set, callable $get) {
if (empty($state) || ! empty($get('lab_content'))) {
return;
}
$tests = LabTest::whereIn('id', $state)->orderBy('name')->get();
$set('lab_content', '<ul>' . $tests->map(fn ($t) => '<li>' . $t->name . '</li>')->implode('') . '</ul>');
})
->afterStateUpdated(function (array $state, callable $set) {
if (empty($state)) {
$set('lab_content', null);
@@ -906,16 +922,39 @@ public static function table(Table $table): Table
->icon('heroicon-o-beaker'),
])
->action(function (Patient $record, array $data, array $arguments, $livewire): void {
$medicationOrder = $data['medication_order'] ?? $data['medication_ids'] ?? [];
$content = $data['content'] ?? '';
if (empty($content) && ! empty($medicationOrder)) {
$medications = Medication::whereIn('id', $medicationOrder)->get()->keyBy('id');
$content = '<ul>' . collect($medicationOrder)->map(fn ($id) => isset($medications[$id])
? '<li>' . implode(' ', array_filter([
$medications[$id]->dosage_form,
$medications[$id]->name,
$medications[$id]->strength,
$medications[$id]->quantity ? '#' . $medications[$id]->quantity : null,
$medications[$id]->timing,
])) . '</li>'
: ''
)->filter()->implode('') . '</ul>';
}
$labTestIds = $data['lab_test_ids'] ?? [];
$labContent = $data['lab_content'] ?? '';
if (empty($labContent) && ! empty($labTestIds)) {
$tests = LabTest::whereIn('id', $labTestIds)->orderBy('name')->get();
$labContent = '<ul>' . $tests->map(fn ($t) => '<li>' . $t->name . '</li>')->implode('') . '</ul>';
}
$prescription = Prescription::updateOrCreate(
['patient' => $record->id],
[
'doctor' => auth()->user()?->name,
'issue_date' => $data['issue_date'] ?? '',
'issue_datetime' => now(),
'medication_ids' => $data['medication_order'] ?? $data['medication_ids'] ?? [],
'lab_test_ids' => $data['lab_test_ids'] ?? [],
'content' => $data['content'] ?? '',
'lab_content' => $data['lab_content'] ?? '',
'medication_ids' => $medicationOrder,
'lab_test_ids' => $labTestIds,
'content' => $content,
'lab_content' => $labContent,
]
);