From 4abd856bf9666956b1be19de16d171cfb1c3fe44 Mon Sep 17 00:00:00 2001
From: SajjadMahmoody <117562560+SajjadMahmoody@users.noreply.github.com>
Date: Sat, 18 Apr 2026 13:45:07 +0330
Subject: [PATCH] fix: auto-populate prescription content and lab content from
pre-selected defaults
---
app/Filament/Resources/PatientResource.php | 77 ++++++++++++++-----
.../PatientResource/Pages/EditPatient.php | 2 +-
.../Resources/PrescriptionResource.php | 10 +++
.../Pages/CreatePrescription.php | 26 +++++++
.../Pages/EditPrescription.php | 48 ++++++++++++
app/Providers/AppServiceProvider.php | 2 +
resources/views/prints/prescription.blade.php | 4 +-
7 files changed, 147 insertions(+), 22 deletions(-)
diff --git a/app/Filament/Resources/PatientResource.php b/app/Filament/Resources/PatientResource.php
index 22611ba..b8470a6 100644
--- a/app/Filament/Resources/PatientResource.php
+++ b/app/Filament/Resources/PatientResource.php
@@ -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()
- ? '
' . $defaultMeds->map(fn ($m) =>
- '- ' . implode(' ', array_filter([
- $m->dosage_form,
- $m->name,
- $m->strength,
- $m->quantity ? '#' . $m->quantity : null,
- $m->timing,
- ])) . '
'
- )->implode('') . '
'
- : '';
+ $content = '';
+ $labContent = '';
}
- $labContent = isset($prescription)
- ? ($prescription->lab_content ?? '')
- : '';
+ if (empty($content) && ! empty($medicationIds)) {
+ $meds = Medication::whereIn('id', $medicationIds)->get()->keyBy('id');
+ $content = '' . collect($medicationIds)->map(fn ($id) => isset($meds[$id])
+ ? '- ' . implode(' ', array_filter([
+ $meds[$id]->dosage_form,
+ $meds[$id]->name,
+ $meds[$id]->strength,
+ $meds[$id]->quantity ? '#' . $meds[$id]->quantity : null,
+ $meds[$id]->timing,
+ ])) . '
'
+ : ''
+ )->filter()->implode('') . '
';
+ }
+
+ if (empty($labContent) && ! empty($labTestIds)) {
+ $tests = LabTest::whereIn('id', $labTestIds)->orderBy('name')->get();
+ $labContent = $tests->isNotEmpty()
+ ? '' . $tests->map(fn ($t) => '- ' . $t->name . '
')->implode('') . '
'
+ : '';
+ }
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', '' . $tests->map(fn ($t) => '- ' . $t->name . '
')->implode('') . '
');
+ })
->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 = '' . collect($medicationOrder)->map(fn ($id) => isset($medications[$id])
+ ? '- ' . implode(' ', array_filter([
+ $medications[$id]->dosage_form,
+ $medications[$id]->name,
+ $medications[$id]->strength,
+ $medications[$id]->quantity ? '#' . $medications[$id]->quantity : null,
+ $medications[$id]->timing,
+ ])) . '
'
+ : ''
+ )->filter()->implode('') . '
';
+ }
+
+ $labTestIds = $data['lab_test_ids'] ?? [];
+ $labContent = $data['lab_content'] ?? '';
+ if (empty($labContent) && ! empty($labTestIds)) {
+ $tests = LabTest::whereIn('id', $labTestIds)->orderBy('name')->get();
+ $labContent = '' . $tests->map(fn ($t) => '- ' . $t->name . '
')->implode('') . '
';
+ }
+
$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,
]
);
diff --git a/app/Filament/Resources/PatientResource/Pages/EditPatient.php b/app/Filament/Resources/PatientResource/Pages/EditPatient.php
index 9c5db5d..8dae9ea 100644
--- a/app/Filament/Resources/PatientResource/Pages/EditPatient.php
+++ b/app/Filament/Resources/PatientResource/Pages/EditPatient.php
@@ -108,7 +108,7 @@ protected function getHeaderActions(): array
if (! empty($arguments['delete'])) {
$existingAppointment = $record->surgeryAppointment;
- SurgeryAppointment::where('patient_id', $record->id)->delete();
+ $existingAppointment?->delete();
$record->unsetRelation('surgeryAppointment');
if ($existingAppointment) {
diff --git a/app/Filament/Resources/PrescriptionResource.php b/app/Filament/Resources/PrescriptionResource.php
index fbe12b1..55f68be 100644
--- a/app/Filament/Resources/PrescriptionResource.php
+++ b/app/Filament/Resources/PrescriptionResource.php
@@ -182,6 +182,16 @@ public static function form(Schema $schema): Schema
)
->columns(3)
->live()
+ ->afterStateHydrated(function (array $state, callable $set, callable $get) {
+ if (empty($state) || ! empty($get('lab_content'))) {
+ return;
+ }
+ $order = array_map('intval', $state);
+ $set('lab_test_order', $order);
+ $tests = LabTest::whereIn('id', $order)->get()->keyBy('id');
+ $html = '' . collect($order)->map(fn ($id) => isset($tests[$id]) ? '- ' . $tests[$id]->name . '
' : '')->filter()->implode('') . '
';
+ $set('lab_content', $html);
+ })
->afterStateUpdated(function (array $state, callable $set, callable $get) {
$currentOrder = array_map('intval', (array) ($get('lab_test_order') ?: []));
$state = array_map('intval', $state);
diff --git a/app/Filament/Resources/PrescriptionResource/Pages/CreatePrescription.php b/app/Filament/Resources/PrescriptionResource/Pages/CreatePrescription.php
index a2bdc5a..57982fe 100644
--- a/app/Filament/Resources/PrescriptionResource/Pages/CreatePrescription.php
+++ b/app/Filament/Resources/PrescriptionResource/Pages/CreatePrescription.php
@@ -5,6 +5,8 @@
namespace App\Filament\Resources\PrescriptionResource\Pages;
use App\Filament\Resources\PrescriptionResource;
+use App\Models\LabTest;
+use App\Models\Medication;
use Filament\Resources\Pages\CreateRecord;
use Filament\Support\Enums\Width;
@@ -20,6 +22,30 @@ protected function mutateFormDataBeforeCreate(array $data): array
{
$data['doctor'] = auth()->user()?->name;
$data['issue_datetime'] = now();
+ $data['medication_ids'] = $data['medication_order'] ?? $data['medication_ids'] ?? [];
+
+ $content = $data['content'] ?? '';
+ if (empty($content) && ! empty($data['medication_ids'])) {
+ $medications = Medication::whereIn('id', $data['medication_ids'])->get()->keyBy('id');
+ $data['content'] = '' . collect($data['medication_ids'])->map(fn ($id) => isset($medications[$id])
+ ? '- ' . implode(' ', array_filter([
+ $medications[$id]->dosage_form,
+ $medications[$id]->name,
+ $medications[$id]->strength,
+ $medications[$id]->quantity ? '#' . $medications[$id]->quantity : null,
+ $medications[$id]->timing,
+ ])) . '
'
+ : ''
+ )->filter()->implode('') . '
';
+ }
+
+ $labTestIds = $data['lab_test_ids'] ?? [];
+ $labContent = $data['lab_content'] ?? '';
+ if (empty($labContent) && ! empty($labTestIds)) {
+ $tests = LabTest::whereIn('id', $labTestIds)->orderBy('name')->get();
+ $data['lab_content'] = '' . $tests->map(fn ($t) => '- ' . $t->name . '
')->implode('') . '
';
+ }
+
return $data;
}
diff --git a/app/Filament/Resources/PrescriptionResource/Pages/EditPrescription.php b/app/Filament/Resources/PrescriptionResource/Pages/EditPrescription.php
index f6cb368..25f8d5d 100644
--- a/app/Filament/Resources/PrescriptionResource/Pages/EditPrescription.php
+++ b/app/Filament/Resources/PrescriptionResource/Pages/EditPrescription.php
@@ -5,6 +5,8 @@
namespace App\Filament\Resources\PrescriptionResource\Pages;
use App\Filament\Resources\PrescriptionResource;
+use App\Models\LabTest;
+use App\Models\Medication;
use Filament\Actions\Action;
use Filament\Actions\DeleteAction;
use Filament\Resources\Pages\EditRecord;
@@ -21,6 +23,30 @@ protected function mutateFormDataBeforeFill(array $data): array
{
$data['medication_order'] = $data['medication_ids'] ?? [];
$data['lab_test_order'] = $data['lab_test_ids'] ?? [];
+
+ $medicationIds = $data['medication_ids'] ?? [];
+ if (empty($data['content']) && ! empty($medicationIds)) {
+ $meds = Medication::whereIn('id', $medicationIds)->get()->keyBy('id');
+ $data['content'] = '' . collect($medicationIds)->map(fn ($id) => isset($meds[$id])
+ ? '- ' . implode(' ', array_filter([
+ $meds[$id]->dosage_form,
+ $meds[$id]->name,
+ $meds[$id]->strength,
+ $meds[$id]->quantity ? '#' . $meds[$id]->quantity : null,
+ $meds[$id]->timing,
+ ])) . '
'
+ : ''
+ )->filter()->implode('') . '
';
+ }
+
+ $labTestIds = $data['lab_test_ids'] ?? [];
+ if (empty($data['lab_content']) && ! empty($labTestIds)) {
+ $tests = LabTest::whereIn('id', $labTestIds)->orderBy('name')->get();
+ $data['lab_content'] = $tests->isNotEmpty()
+ ? '' . $tests->map(fn ($t) => '- ' . $t->name . '
')->implode('') . '
'
+ : '';
+ }
+
return $data;
}
@@ -30,6 +56,28 @@ protected function mutateFormDataBeforeSave(array $data): array
$data['issue_datetime'] = now();
$data['medication_ids'] = $data['medication_order'] ?? $data['medication_ids'] ?? [];
$data['lab_test_ids'] = $data['lab_test_order'] ?? $data['lab_test_ids'] ?? [];
+
+ $content = $data['content'] ?? '';
+ if (empty($content) && ! empty($data['medication_ids'])) {
+ $medications = Medication::whereIn('id', $data['medication_ids'])->get()->keyBy('id');
+ $data['content'] = '' . collect($data['medication_ids'])->map(fn ($id) => isset($medications[$id])
+ ? '- ' . implode(' ', array_filter([
+ $medications[$id]->dosage_form,
+ $medications[$id]->name,
+ $medications[$id]->strength,
+ $medications[$id]->quantity ? '#' . $medications[$id]->quantity : null,
+ $medications[$id]->timing,
+ ])) . '
'
+ : ''
+ )->filter()->implode('') . '
';
+ }
+
+ $labContent = $data['lab_content'] ?? '';
+ if (empty($labContent) && ! empty($data['lab_test_ids'])) {
+ $tests = LabTest::whereIn('id', $data['lab_test_ids'])->orderBy('name')->get();
+ $data['lab_content'] = '' . $tests->map(fn ($t) => '- ' . $t->name . '
')->implode('') . '
';
+ }
+
return $data;
}
diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php
index c2e6b21..0132447 100644
--- a/app/Providers/AppServiceProvider.php
+++ b/app/Providers/AppServiceProvider.php
@@ -11,6 +11,7 @@
use App\Models\SurgeryAppointment;
use App\Models\SurgeryCenter;
use App\Models\Treatment;
+use App\Models\LabTest;
use App\Models\Visit;
use App\Observers\PatientIllnessObserver;
use App\Observers\PatientObserver;
@@ -31,6 +32,7 @@ public function boot(): void
Patient::observe($observer);
Patient::observe(PatientObserver::class);
Doctor::observe($observer);
+ LabTest::observe($observer);
Treatment::observe($observer);
Medication::observe($observer);
PaymentType::observe($observer);
diff --git a/resources/views/prints/prescription.blade.php b/resources/views/prints/prescription.blade.php
index d9e05ab..1f18ad2 100644
--- a/resources/views/prints/prescription.blade.php
+++ b/resources/views/prints/prescription.blade.php
@@ -402,7 +402,7 @@
{{-- Prescription section --}}