374 lines
16 KiB
PHP
374 lines
16 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Filament\Resources;
|
|
use App\Filament\Resources\PrescriptionResource\Pages;
|
|
use App\Models\Medication;
|
|
use App\Models\Patient;
|
|
use App\Models\Prescription;
|
|
use App\Services\PdfService;
|
|
use Morilog\Jalali\Jalalian;
|
|
use Filament\Actions\Action;
|
|
use Filament\Actions\ActionGroup;
|
|
use Filament\Actions\BulkAction;
|
|
use Filament\Actions\BulkActionGroup;
|
|
use Filament\Actions\DeleteAction;
|
|
use Filament\Actions\DeleteBulkAction;
|
|
use Filament\Actions\EditAction;
|
|
use Filament\Forms\Components\CheckboxList;
|
|
use Filament\Forms\Components\DatePicker;
|
|
use Filament\Forms\Components\RichEditor;
|
|
use Filament\Forms\Components\Select;
|
|
use Filament\Resources\Resource;
|
|
use Filament\Schemas\Components\Grid;
|
|
use Filament\Schemas\Components\Section;
|
|
use Filament\Schemas\Schema;
|
|
use Filament\Tables\Columns\TextColumn;
|
|
use Filament\Tables\Table;
|
|
use Illuminate\Support\Collection;
|
|
use OpenSpout\Common\Entity\Row;
|
|
use OpenSpout\Writer\XLSX\Writer;
|
|
|
|
class PrescriptionResource extends Resource
|
|
{
|
|
protected static ?string $model = Prescription::class;
|
|
|
|
protected static ?string $recordTitleAttribute = 'doctor';
|
|
|
|
public static function getGloballySearchableAttributes(): array
|
|
{
|
|
return ['doctor', 'patientRecord.first_name', 'patientRecord.last_name'];
|
|
}
|
|
|
|
public static function getNavigationIcon(): \BackedEnum|string|null
|
|
{
|
|
return 'heroicon-o-document-text';
|
|
}
|
|
|
|
public static function getNavigationGroup(): ?string
|
|
{
|
|
return __('navigation.groups.patients');
|
|
}
|
|
|
|
public static function getNavigationSort(): ?int
|
|
{
|
|
return 2;
|
|
}
|
|
|
|
public static function getNavigationLabel(): string
|
|
{
|
|
return __('navigation.prescriptions.title');
|
|
}
|
|
|
|
public static function getModelLabel(): string
|
|
{
|
|
return __('navigation.prescriptions.singular');
|
|
}
|
|
|
|
public static function getPluralModelLabel(): string
|
|
{
|
|
return __('navigation.prescriptions.title');
|
|
}
|
|
|
|
public static function form(Schema $schema): Schema
|
|
{
|
|
return $schema
|
|
->components([
|
|
Grid::make()
|
|
->schema([
|
|
Section::make(__('prescriptions.sections.info'))
|
|
->schema([
|
|
Select::make('patient')
|
|
->label(__('prescriptions.fields.patient'))
|
|
->options(fn () => Patient::all()->mapWithKeys(
|
|
fn ($p) => [$p->id => $p->full_name]
|
|
)->toArray())
|
|
->searchable()
|
|
->nullable(),
|
|
|
|
DatePicker::make('issue_date')
|
|
->label(__('prescriptions.fields.issue_date'))
|
|
->jalali()
|
|
->default(now()),
|
|
])
|
|
->columns(2)
|
|
->columnSpanFull(),
|
|
|
|
Section::make(__('prescriptions.sections.medications'))
|
|
->schema([
|
|
CheckboxList::make('medication_ids')
|
|
->label(__('prescriptions.fields.medication_ids'))
|
|
->options(fn () => Medication::where('is_active', true)
|
|
->orderBy('name')
|
|
->get()
|
|
->mapWithKeys(fn ($m) => [
|
|
$m->id => implode(' ', array_filter([
|
|
$m->dosage_form,
|
|
$m->name,
|
|
$m->strength,
|
|
$m->quantity ? '#' . $m->quantity : null,
|
|
$m->timing,
|
|
])),
|
|
])
|
|
->toArray()
|
|
)
|
|
->columns(3)
|
|
->live()
|
|
->afterStateUpdated(function (array $state, callable $set) {
|
|
if (empty($state)) {
|
|
$set('content', null);
|
|
return;
|
|
}
|
|
$medications = Medication::whereIn('id', $state)
|
|
->orderBy('name')
|
|
->get();
|
|
$html = '<ul>' . $medications->map(fn ($m) =>
|
|
'<li>' . implode(' ', array_filter([
|
|
$m->dosage_form,
|
|
$m->name,
|
|
$m->strength,
|
|
$m->quantity ? '#' . $m->quantity : null,
|
|
$m->timing,
|
|
])) . '</li>'
|
|
)->implode('') . '</ul>';
|
|
$set('content', $html);
|
|
})
|
|
->columnSpanFull(),
|
|
])
|
|
->columnSpanFull(),
|
|
|
|
Section::make(__('prescriptions.sections.content'))
|
|
->schema([
|
|
RichEditor::make('content')
|
|
->label(__('prescriptions.fields.content'))
|
|
->columnSpanFull(),
|
|
])
|
|
->columnSpanFull()
|
|
->footerActions([
|
|
fn (string $operation) => Action::make('createAndPrint')
|
|
->label(__('prescriptions.actions.save_and_print'))
|
|
->color('warning')
|
|
->icon('heroicon-o-printer')
|
|
->action(function ($livewire) {
|
|
$livewire->printType = 'prescription';
|
|
$livewire->create();
|
|
})
|
|
->visible($operation === 'create'),
|
|
|
|
fn (string $operation) => Action::make('cancelCreate')
|
|
->label(__('filament-panels::resources/pages/create-record.form.actions.cancel.label'))
|
|
->url(static::getUrl('index'))
|
|
->color('gray')
|
|
->visible($operation === 'create'),
|
|
fn (string $operation) => Action::make('save')
|
|
->label(__('filament-panels::resources/pages/edit-record.form.actions.save.label'))
|
|
->submit('save')
|
|
->keyBindings(['mod+s'])
|
|
->visible($operation === 'edit'),
|
|
fn (string $operation) => Action::make('cancelEdit')
|
|
->label(__('filament-panels::resources/pages/edit-record.form.actions.cancel.label'))
|
|
->url(static::getUrl('index'))
|
|
->color('gray')
|
|
->visible($operation === 'edit'),
|
|
]),
|
|
])
|
|
->columnSpanFull(),
|
|
]);
|
|
}
|
|
|
|
public static function table(Table $table): Table
|
|
{
|
|
return $table
|
|
->modifyQueryUsing(fn ($query) => $query->with(['patientRecord', 'creator']))
|
|
->deferLoading()
|
|
->columns([
|
|
TextColumn::make('patientRecord.first_name')
|
|
->label(__('prescriptions.fields.patient'))
|
|
->formatStateUsing(fn ($record) => $record->patientRecord?->full_name ?? '-')
|
|
->searchable()
|
|
->sortable(),
|
|
|
|
TextColumn::make('doctor')
|
|
->label(__('prescriptions.fields.doctor'))
|
|
->searchable()
|
|
->sortable(),
|
|
|
|
TextColumn::make('issue_date')
|
|
->label(__('prescriptions.fields.issue_date'))
|
|
->sortable()
|
|
->formatStateUsing(function ($state) {
|
|
if (! $state) {
|
|
return '-';
|
|
}
|
|
try {
|
|
$date = \Carbon\Carbon::parse($state);
|
|
return Jalalian::fromCarbon($date)->format('Y/m/d - H:i');
|
|
} catch (\Exception $e) {
|
|
return $state;
|
|
}
|
|
})
|
|
->placeholder('-'),
|
|
|
|
TextColumn::make('content')
|
|
->label(__('prescriptions.fields.content'))
|
|
->formatStateUsing(fn ($state) => strip_tags($state ?? ''))
|
|
->limit(80)
|
|
->placeholder('-'),
|
|
|
|
TextColumn::make('creator.name')
|
|
->label(__('prescriptions.fields.created_by'))
|
|
->sortable(),
|
|
|
|
TextColumn::make('created_at')
|
|
->label(__('prescriptions.fields.created_at'))
|
|
->sortable()
|
|
->formatStateUsing(function ($state) {
|
|
if (! $state) {
|
|
return '-';
|
|
}
|
|
try {
|
|
$date = \Carbon\Carbon::parse($state);
|
|
return Jalalian::fromCarbon($date)->format('Y/m/d - H:i');
|
|
} catch (\Exception $e) {
|
|
return $state;
|
|
}
|
|
}),
|
|
])
|
|
->defaultSort('created_at', 'desc')
|
|
->recordActions([
|
|
Action::make('printPrescription')
|
|
->label(__('prescriptions.actions.print_prescription'))
|
|
->icon('heroicon-o-printer')
|
|
->color('info')
|
|
->url(fn ($record) => route('prescription.print', $record))
|
|
->openUrlInNewTab(),
|
|
|
|
Action::make('printAdmission')
|
|
->label(__('prescriptions.actions.print_admission'))
|
|
->icon('heroicon-o-printer')
|
|
->color('warning')
|
|
->url(fn ($record) => route('prescription.print.admission', $record))
|
|
->openUrlInNewTab(),
|
|
|
|
Action::make('printLab')
|
|
->label(__('prescriptions.actions.print_lab'))
|
|
->icon('heroicon-o-beaker')
|
|
->color('gray')
|
|
->url(fn ($record) => route('prescription.print', ['prescription' => $record, 'type' => 'lab']))
|
|
->openUrlInNewTab(),
|
|
|
|
EditAction::make(),
|
|
DeleteAction::make(),
|
|
])
|
|
->toolbarActions([
|
|
BulkActionGroup::make([
|
|
DeleteBulkAction::make(),
|
|
BulkAction::make('export_excel_selected')
|
|
->label(__('table.export_excel_selected'))
|
|
->icon('heroicon-o-table-cells')
|
|
->color('success')
|
|
->action(function (Collection $records) {
|
|
$tempPath = tempnam(sys_get_temp_dir(), 'export_') . '.xlsx';
|
|
$writer = new Writer();
|
|
$writer->openToFile($tempPath);
|
|
|
|
$writer->addRow(Row::fromValues([
|
|
'ID',
|
|
__('prescriptions.fields.patient'),
|
|
__('prescriptions.fields.doctor'),
|
|
__('prescriptions.fields.issue_date'),
|
|
__('prescriptions.fields.content'),
|
|
]));
|
|
|
|
foreach ($records as $record) {
|
|
$writer->addRow(Row::fromValues([
|
|
$record->id,
|
|
$record->patientRecord?->full_name ?? '',
|
|
$record->doctor,
|
|
$record->issue_date,
|
|
strip_tags($record->content ?? ''),
|
|
]));
|
|
}
|
|
|
|
$writer->close();
|
|
|
|
return response()->streamDownload(function () use ($tempPath) {
|
|
readfile($tempPath);
|
|
@unlink($tempPath);
|
|
}, 'prescriptions-' . now()->format('Y-m-d') . '.xlsx', [
|
|
'Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
|
]);
|
|
}),
|
|
]),
|
|
|
|
ActionGroup::make([
|
|
Action::make('export_excel')
|
|
->label(__('table.export_excel'))
|
|
->color('success')
|
|
->icon('heroicon-o-table-cells')
|
|
->action(function () {
|
|
$records = Prescription::with('patientRecord')->orderBy('created_at', 'desc')->get();
|
|
|
|
$tempPath = tempnam(sys_get_temp_dir(), 'export_') . '.xlsx';
|
|
$writer = new Writer();
|
|
$writer->openToFile($tempPath);
|
|
|
|
$writer->addRow(Row::fromValues([
|
|
'ID',
|
|
__('prescriptions.fields.patient'),
|
|
__('prescriptions.fields.doctor'),
|
|
__('prescriptions.fields.issue_date'),
|
|
__('prescriptions.fields.content'),
|
|
]));
|
|
|
|
foreach ($records as $record) {
|
|
$writer->addRow(Row::fromValues([
|
|
$record->id,
|
|
$record->patientRecord?->full_name ?? '',
|
|
$record->doctor,
|
|
$record->issue_date,
|
|
strip_tags($record->content ?? ''),
|
|
]));
|
|
}
|
|
|
|
$writer->close();
|
|
|
|
return response()->streamDownload(function () use ($tempPath) {
|
|
readfile($tempPath);
|
|
@unlink($tempPath);
|
|
}, 'prescriptions-' . now()->format('Y-m-d') . '.xlsx', [
|
|
'Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
|
]);
|
|
}),
|
|
|
|
Action::make('export_pdf')
|
|
->label(__('table.export_pdf'))
|
|
->color('danger')
|
|
->icon('heroicon-o-document-arrow-down')
|
|
->action(function () {
|
|
$records = Prescription::with('patientRecord')->orderBy('created_at', 'desc')->get();
|
|
|
|
return PdfService::download(
|
|
'exports.prescriptions-pdf',
|
|
['records' => $records],
|
|
'prescriptions-' . now()->format('Y-m-d') . '.pdf',
|
|
);
|
|
}),
|
|
])
|
|
->label(__('table.export'))
|
|
->color('orange')
|
|
->button(),
|
|
]);
|
|
}
|
|
|
|
public static function getPages(): array
|
|
{
|
|
return [
|
|
'index' => Pages\ListPrescriptions::route('/'),
|
|
'create' => Pages\CreatePrescription::route('/create'),
|
|
'edit' => Pages\EditPrescription::route('/{record}/edit'),
|
|
];
|
|
}
|
|
}
|