fix: auto-populate prescription content and lab content from pre-selected defaults
This commit is contained in:
parent
bbb727cffe
commit
4abd856bf9
|
|
@ -631,7 +631,7 @@ public static function table(Table $table): Table
|
||||||
if (! empty($arguments['delete'])) {
|
if (! empty($arguments['delete'])) {
|
||||||
$existingAppointment = $record->surgeryAppointment;
|
$existingAppointment = $record->surgeryAppointment;
|
||||||
|
|
||||||
SurgeryAppointment::where('patient_id', $record->id)->delete();
|
$existingAppointment?->delete();
|
||||||
|
|
||||||
if ($existingAppointment) {
|
if ($existingAppointment) {
|
||||||
\App\Filament\Pages\SmsLogsPage::cancelAppointmentMessages(
|
\App\Filament\Pages\SmsLogsPage::cancelAppointmentMessages(
|
||||||
|
|
@ -732,6 +732,7 @@ public static function table(Table $table): Table
|
||||||
$medicationIds = $prescription->medication_ids ?? [];
|
$medicationIds = $prescription->medication_ids ?? [];
|
||||||
$labTestIds = $prescription->lab_test_ids ?? [];
|
$labTestIds = $prescription->lab_test_ids ?? [];
|
||||||
$content = $prescription->content ?? '';
|
$content = $prescription->content ?? '';
|
||||||
|
$labContent = $prescription->lab_content ?? '';
|
||||||
} else {
|
} else {
|
||||||
$defaultMeds = Medication::where('is_active', true)
|
$defaultMeds = Medication::where('is_active', true)
|
||||||
->where('is_default', true)
|
->where('is_default', true)
|
||||||
|
|
@ -743,22 +744,30 @@ public static function table(Table $table): Table
|
||||||
->orderBy('name')
|
->orderBy('name')
|
||||||
->pluck('id')
|
->pluck('id')
|
||||||
->toArray();
|
->toArray();
|
||||||
$content = $defaultMeds->isNotEmpty()
|
$content = '';
|
||||||
? '<ul>' . $defaultMeds->map(fn ($m) =>
|
$labContent = '';
|
||||||
'<li>' . implode(' ', array_filter([
|
|
||||||
$m->dosage_form,
|
|
||||||
$m->name,
|
|
||||||
$m->strength,
|
|
||||||
$m->quantity ? '#' . $m->quantity : null,
|
|
||||||
$m->timing,
|
|
||||||
])) . '</li>'
|
|
||||||
)->implode('') . '</ul>'
|
|
||||||
: '';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$labContent = isset($prescription)
|
if (empty($content) && ! empty($medicationIds)) {
|
||||||
? ($prescription->lab_content ?? '')
|
$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 [
|
return [
|
||||||
'medication_ids' => $medicationIds,
|
'medication_ids' => $medicationIds,
|
||||||
|
|
@ -865,6 +874,13 @@ public static function table(Table $table): Table
|
||||||
)
|
)
|
||||||
->columns(2)
|
->columns(2)
|
||||||
->live()
|
->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) {
|
->afterStateUpdated(function (array $state, callable $set) {
|
||||||
if (empty($state)) {
|
if (empty($state)) {
|
||||||
$set('lab_content', null);
|
$set('lab_content', null);
|
||||||
|
|
@ -906,16 +922,39 @@ public static function table(Table $table): Table
|
||||||
->icon('heroicon-o-beaker'),
|
->icon('heroicon-o-beaker'),
|
||||||
])
|
])
|
||||||
->action(function (Patient $record, array $data, array $arguments, $livewire): void {
|
->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(
|
$prescription = Prescription::updateOrCreate(
|
||||||
['patient' => $record->id],
|
['patient' => $record->id],
|
||||||
[
|
[
|
||||||
'doctor' => auth()->user()?->name,
|
'doctor' => auth()->user()?->name,
|
||||||
'issue_date' => $data['issue_date'] ?? '',
|
'issue_date' => $data['issue_date'] ?? '',
|
||||||
'issue_datetime' => now(),
|
'issue_datetime' => now(),
|
||||||
'medication_ids' => $data['medication_order'] ?? $data['medication_ids'] ?? [],
|
'medication_ids' => $medicationOrder,
|
||||||
'lab_test_ids' => $data['lab_test_ids'] ?? [],
|
'lab_test_ids' => $labTestIds,
|
||||||
'content' => $data['content'] ?? '',
|
'content' => $content,
|
||||||
'lab_content' => $data['lab_content'] ?? '',
|
'lab_content' => $labContent,
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -108,7 +108,7 @@ protected function getHeaderActions(): array
|
||||||
if (! empty($arguments['delete'])) {
|
if (! empty($arguments['delete'])) {
|
||||||
$existingAppointment = $record->surgeryAppointment;
|
$existingAppointment = $record->surgeryAppointment;
|
||||||
|
|
||||||
SurgeryAppointment::where('patient_id', $record->id)->delete();
|
$existingAppointment?->delete();
|
||||||
$record->unsetRelation('surgeryAppointment');
|
$record->unsetRelation('surgeryAppointment');
|
||||||
|
|
||||||
if ($existingAppointment) {
|
if ($existingAppointment) {
|
||||||
|
|
|
||||||
|
|
@ -182,6 +182,16 @@ public static function form(Schema $schema): Schema
|
||||||
)
|
)
|
||||||
->columns(3)
|
->columns(3)
|
||||||
->live()
|
->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 = '<ul>' . collect($order)->map(fn ($id) => isset($tests[$id]) ? '<li>' . $tests[$id]->name . '</li>' : '')->filter()->implode('') . '</ul>';
|
||||||
|
$set('lab_content', $html);
|
||||||
|
})
|
||||||
->afterStateUpdated(function (array $state, callable $set, callable $get) {
|
->afterStateUpdated(function (array $state, callable $set, callable $get) {
|
||||||
$currentOrder = array_map('intval', (array) ($get('lab_test_order') ?: []));
|
$currentOrder = array_map('intval', (array) ($get('lab_test_order') ?: []));
|
||||||
$state = array_map('intval', $state);
|
$state = array_map('intval', $state);
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,8 @@
|
||||||
namespace App\Filament\Resources\PrescriptionResource\Pages;
|
namespace App\Filament\Resources\PrescriptionResource\Pages;
|
||||||
|
|
||||||
use App\Filament\Resources\PrescriptionResource;
|
use App\Filament\Resources\PrescriptionResource;
|
||||||
|
use App\Models\LabTest;
|
||||||
|
use App\Models\Medication;
|
||||||
use Filament\Resources\Pages\CreateRecord;
|
use Filament\Resources\Pages\CreateRecord;
|
||||||
use Filament\Support\Enums\Width;
|
use Filament\Support\Enums\Width;
|
||||||
|
|
||||||
|
|
@ -20,6 +22,30 @@ protected function mutateFormDataBeforeCreate(array $data): array
|
||||||
{
|
{
|
||||||
$data['doctor'] = auth()->user()?->name;
|
$data['doctor'] = auth()->user()?->name;
|
||||||
$data['issue_datetime'] = now();
|
$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'] = '<ul>' . collect($data['medication_ids'])->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();
|
||||||
|
$data['lab_content'] = '<ul>' . $tests->map(fn ($t) => '<li>' . $t->name . '</li>')->implode('') . '</ul>';
|
||||||
|
}
|
||||||
|
|
||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,8 @@
|
||||||
namespace App\Filament\Resources\PrescriptionResource\Pages;
|
namespace App\Filament\Resources\PrescriptionResource\Pages;
|
||||||
|
|
||||||
use App\Filament\Resources\PrescriptionResource;
|
use App\Filament\Resources\PrescriptionResource;
|
||||||
|
use App\Models\LabTest;
|
||||||
|
use App\Models\Medication;
|
||||||
use Filament\Actions\Action;
|
use Filament\Actions\Action;
|
||||||
use Filament\Actions\DeleteAction;
|
use Filament\Actions\DeleteAction;
|
||||||
use Filament\Resources\Pages\EditRecord;
|
use Filament\Resources\Pages\EditRecord;
|
||||||
|
|
@ -21,6 +23,30 @@ protected function mutateFormDataBeforeFill(array $data): array
|
||||||
{
|
{
|
||||||
$data['medication_order'] = $data['medication_ids'] ?? [];
|
$data['medication_order'] = $data['medication_ids'] ?? [];
|
||||||
$data['lab_test_order'] = $data['lab_test_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'] = '<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>';
|
||||||
|
}
|
||||||
|
|
||||||
|
$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()
|
||||||
|
? '<ul>' . $tests->map(fn ($t) => '<li>' . $t->name . '</li>')->implode('') . '</ul>'
|
||||||
|
: '';
|
||||||
|
}
|
||||||
|
|
||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -30,6 +56,28 @@ protected function mutateFormDataBeforeSave(array $data): array
|
||||||
$data['issue_datetime'] = now();
|
$data['issue_datetime'] = now();
|
||||||
$data['medication_ids'] = $data['medication_order'] ?? $data['medication_ids'] ?? [];
|
$data['medication_ids'] = $data['medication_order'] ?? $data['medication_ids'] ?? [];
|
||||||
$data['lab_test_ids'] = $data['lab_test_order'] ?? $data['lab_test_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'] = '<ul>' . collect($data['medication_ids'])->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>';
|
||||||
|
}
|
||||||
|
|
||||||
|
$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'] = '<ul>' . $tests->map(fn ($t) => '<li>' . $t->name . '</li>')->implode('') . '</ul>';
|
||||||
|
}
|
||||||
|
|
||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,7 @@
|
||||||
use App\Models\SurgeryAppointment;
|
use App\Models\SurgeryAppointment;
|
||||||
use App\Models\SurgeryCenter;
|
use App\Models\SurgeryCenter;
|
||||||
use App\Models\Treatment;
|
use App\Models\Treatment;
|
||||||
|
use App\Models\LabTest;
|
||||||
use App\Models\Visit;
|
use App\Models\Visit;
|
||||||
use App\Observers\PatientIllnessObserver;
|
use App\Observers\PatientIllnessObserver;
|
||||||
use App\Observers\PatientObserver;
|
use App\Observers\PatientObserver;
|
||||||
|
|
@ -31,6 +32,7 @@ public function boot(): void
|
||||||
Patient::observe($observer);
|
Patient::observe($observer);
|
||||||
Patient::observe(PatientObserver::class);
|
Patient::observe(PatientObserver::class);
|
||||||
Doctor::observe($observer);
|
Doctor::observe($observer);
|
||||||
|
LabTest::observe($observer);
|
||||||
Treatment::observe($observer);
|
Treatment::observe($observer);
|
||||||
Medication::observe($observer);
|
Medication::observe($observer);
|
||||||
PaymentType::observe($observer);
|
PaymentType::observe($observer);
|
||||||
|
|
|
||||||
|
|
@ -402,7 +402,7 @@
|
||||||
{{-- Prescription section --}}
|
{{-- Prescription section --}}
|
||||||
<div class="print-section section-prescription">
|
<div class="print-section section-prescription">
|
||||||
<div class="section-toolbar">
|
<div class="section-toolbar">
|
||||||
<span class="section-label">📋 نسخه دارویی</span>
|
<span class="section-label">نسخه دارویی</span>
|
||||||
<div style="display:flex;gap:6px;">
|
<div style="display:flex;gap:6px;">
|
||||||
<button class="toolbar-btn btn-print" onclick="printSection('prescription')" style="font-size:12px;padding:5px 12px;">🖨 پرینت نسخه</button>
|
<button class="toolbar-btn btn-print" onclick="printSection('prescription')" style="font-size:12px;padding:5px 12px;">🖨 پرینت نسخه</button>
|
||||||
<button class="toolbar-btn btn-edit" onclick="openEditModal()" style="font-size:12px;padding:5px 12px;"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" style="width:14px;height:14px;flex-shrink:0;"><path d="M11 4H4a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2v-7"/><path d="M18.5 2.5a2.121 2.121 0 0 1 3 3L12 15l-4 1 1-4 9.5-9.5z"/></svg> ویرایش نسخه</button>
|
<button class="toolbar-btn btn-edit" onclick="openEditModal()" style="font-size:12px;padding:5px 12px;"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" style="width:14px;height:14px;flex-shrink:0;"><path d="M11 4H4a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2v-7"/><path d="M18.5 2.5a2.121 2.121 0 0 1 3 3L12 15l-4 1 1-4 9.5-9.5z"/></svg> ویرایش نسخه</button>
|
||||||
|
|
@ -447,7 +447,7 @@
|
||||||
{{-- Lab section --}}
|
{{-- Lab section --}}
|
||||||
<div class="print-section section-lab">
|
<div class="print-section section-lab">
|
||||||
<div class="section-toolbar">
|
<div class="section-toolbar">
|
||||||
<span class="section-label">🧪 درخواست آزمایش</span>
|
<span class="section-label">درخواست آزمایش</span>
|
||||||
<div style="display:flex;gap:6px;">
|
<div style="display:flex;gap:6px;">
|
||||||
<button class="toolbar-btn btn-lab" onclick="printSection('lab')" style="font-size:12px;padding:5px 12px;">🖨 پرینت آزمایش</button>
|
<button class="toolbar-btn btn-lab" onclick="printSection('lab')" style="font-size:12px;padding:5px 12px;">🖨 پرینت آزمایش</button>
|
||||||
<button class="toolbar-btn btn-edit" onclick="openEditModal()" style="font-size:12px;padding:5px 12px;"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" style="width:14px;height:14px;flex-shrink:0;"><path d="M11 4H4a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2v-7"/><path d="M18.5 2.5a2.121 2.121 0 0 1 3 3L12 15l-4 1 1-4 9.5-9.5z"/></svg> ویرایش آزمایش</button>
|
<button class="toolbar-btn btn-edit" onclick="openEditModal()" style="font-size:12px;padding:5px 12px;"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" style="width:14px;height:14px;flex-shrink:0;"><path d="M11 4H4a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2v-7"/><path d="M18.5 2.5a2.121 2.121 0 0 1 3 3L12 15l-4 1 1-4 9.5-9.5z"/></svg> ویرایش آزمایش</button>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue