243 lines
11 KiB
PHP
243 lines
11 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Filament\Resources\PatientResource\RelationManagers;
|
|
|
|
use App\Filament\Resources\PatientResource\Pages\CreatePatient;
|
|
use App\Filament\Resources\PatientResource\Pages\EditPatient;
|
|
use App\Filament\Resources\PatientResource\Pages\ViewPatient;
|
|
use App\Models\Doctor;
|
|
use App\Models\PaymentType;
|
|
use App\Models\Treatment;
|
|
use Filament\Actions\Action;
|
|
use Filament\Actions\CreateAction;
|
|
use Filament\Actions\DeleteAction;
|
|
use Filament\Actions\EditAction;
|
|
use Filament\Forms\Components\DatePicker;
|
|
use Filament\Forms\Components\Select;
|
|
use Filament\Forms\Components\Textarea;
|
|
use Filament\Forms\Components\TextInput;
|
|
use Filament\Forms\Components\TimePicker;
|
|
use Filament\Resources\RelationManagers\RelationManager;
|
|
use Filament\Schemas\Components\Grid;
|
|
use Filament\Schemas\Components\Section;
|
|
use Filament\Schemas\Schema;
|
|
use Filament\Tables\Columns\Summarizers\Sum;
|
|
use Filament\Tables\Columns\TextColumn;
|
|
use Filament\Tables\Table;
|
|
|
|
class VisitsRelationManager extends RelationManager
|
|
{
|
|
protected static string $relationship = 'visits';
|
|
|
|
public static function canViewForRecord(\Illuminate\Database\Eloquent\Model $ownerRecord, string $pageClass): bool
|
|
{
|
|
return in_array($pageClass, [CreatePatient::class, EditPatient::class, ViewPatient::class]);
|
|
}
|
|
|
|
public static function getTitle(\Illuminate\Database\Eloquent\Model $ownerRecord, string $pageClass): string
|
|
{
|
|
return __('navigation.visits.title');
|
|
}
|
|
|
|
public function form(Schema $schema): Schema
|
|
{
|
|
return $schema
|
|
->components([
|
|
Grid::make()
|
|
->schema([
|
|
Section::make(__('visits.sections.visit_info'))
|
|
->schema([
|
|
DatePicker::make('visit_date')
|
|
->label(__('visits.fields.visit_date'))
|
|
->jalali()
|
|
->default(now())
|
|
->required(),
|
|
|
|
TimePicker::make('visit_time')
|
|
->label(__('visits.fields.visit_time'))
|
|
->native(false)
|
|
->seconds(false)
|
|
->afterStateHydrated(function ($component, $state) {
|
|
if (blank($state)) {
|
|
$component->state(now()->format('Y-m-d H:i:s'));
|
|
}
|
|
}),
|
|
|
|
Select::make('doctor')
|
|
->label(__('visits.fields.doctor'))
|
|
->options(fn () => Doctor::all()->mapWithKeys(
|
|
fn ($d) => [$d->id => $d->full_name]
|
|
)->toArray())
|
|
->default(fn () => Doctor::where('is_default', true)->first()?->id)
|
|
->nullable(),
|
|
|
|
Select::make('treatment')
|
|
->label(__('visits.fields.treatment'))
|
|
->options(fn () => Treatment::all()->mapWithKeys(
|
|
fn ($t) => [$t->id => ($t->descriptions ? '★ ' : '') . ($t->treatment_name ?: $t->treatment_type)]
|
|
)->toArray())
|
|
->searchable()
|
|
->required()
|
|
->live()
|
|
->afterStateUpdated(function ($state, callable $set) {
|
|
if ($state) {
|
|
$treatment = Treatment::find($state);
|
|
if ($treatment) {
|
|
if ($treatment->treatment_cost > 0) {
|
|
$set('treatment_cost', $treatment->treatment_cost);
|
|
$set('paid_amount', $treatment->treatment_cost);
|
|
}
|
|
if ($treatment->descriptions) {
|
|
$set('treatment_description', $treatment->descriptions);
|
|
}
|
|
}
|
|
}
|
|
}),
|
|
])
|
|
->columns(2)
|
|
->columnSpanFull(),
|
|
|
|
Section::make(__('visits.sections.financial_info'))
|
|
->schema([
|
|
TextInput::make('treatment_cost')
|
|
->label(__('visits.fields.treatment_cost'))
|
|
->numeric()
|
|
->nullable()
|
|
->suffix(__('visits.fields.currency')),
|
|
|
|
TextInput::make('paid_amount')
|
|
->label(__('visits.fields.paid_amount'))
|
|
->numeric()
|
|
->nullable()
|
|
->suffix(__('visits.fields.currency')),
|
|
|
|
Select::make('payment_type')
|
|
->label(__('visits.fields.payment_type'))
|
|
->options(fn () => PaymentType::all()->mapWithKeys(
|
|
fn ($p) => [$p->id => $p->payment_type]
|
|
)->toArray())
|
|
->searchable()
|
|
->required(),
|
|
])
|
|
->columns(3)
|
|
->columnSpanFull(),
|
|
|
|
Section::make(__('visits.sections.treatment_details'))
|
|
->schema([
|
|
Textarea::make('treatment_description')
|
|
->label(__('visits.fields.treatment_description'))
|
|
->rows(3)
|
|
->columnSpanFull(),
|
|
])
|
|
->columnSpanFull(),
|
|
])
|
|
->columnSpanFull(),
|
|
]);
|
|
}
|
|
|
|
private static function jalaliDate(?string $date): string
|
|
{
|
|
if (! $date) {
|
|
return '-';
|
|
}
|
|
try {
|
|
return \Morilog\Jalali\Jalalian::fromCarbon(\Carbon\Carbon::parse($date))->format('Y/m/d');
|
|
} catch (\Exception $e) {
|
|
return $date;
|
|
}
|
|
}
|
|
|
|
public function table(Table $table): Table
|
|
{
|
|
return $table
|
|
->recordTitleAttribute('visit_date')
|
|
->modifyQueryUsing(fn ($query) => $query->with(['doctorRecord', 'treatmentRecord', 'paymentTypeRecord']))
|
|
->deferLoading()
|
|
->columns([
|
|
TextColumn::make('visit_date')
|
|
->label(__('visits.fields.visit_date'))
|
|
->sortable()
|
|
->wrap(false)
|
|
->formatStateUsing(function ($state) {
|
|
if (! $state) {
|
|
return '-';
|
|
}
|
|
try {
|
|
$date = \Carbon\Carbon::parse($state);
|
|
return \Morilog\Jalali\Jalalian::fromCarbon($date)->format('Y/m/d');
|
|
} catch (\Exception $e) {
|
|
return $state;
|
|
}
|
|
})
|
|
->placeholder('-'),
|
|
|
|
TextColumn::make('doctorRecord.first_name')
|
|
->label(__('visits.fields.doctor'))
|
|
->formatStateUsing(fn ($record) => $record->doctorRecord?->full_name ?? '-')
|
|
->wrap(false)
|
|
->placeholder('-'),
|
|
|
|
TextColumn::make('treatmentRecord.treatment_name')
|
|
->label(__('visits.fields.treatment'))
|
|
->formatStateUsing(fn ($record) => $record->treatmentRecord
|
|
? ($record->treatmentRecord->treatment_name ?: $record->treatmentRecord->treatment_type)
|
|
: '-')
|
|
->wrap(false)
|
|
->placeholder('-'),
|
|
|
|
TextColumn::make('treatment_cost')
|
|
->label(__('visits.fields.treatment_cost'))
|
|
->formatStateUsing(fn ($state) => $state !== null ? number_format((int) $state) : '-')
|
|
->wrap(false)
|
|
->placeholder('-')
|
|
->summarize(
|
|
Sum::make()
|
|
->label(__('visits.fields.treatment_cost'))
|
|
->formatStateUsing(fn ($state) => number_format((int) ($state ?? 0)))
|
|
),
|
|
|
|
TextColumn::make('paid_amount')
|
|
->label(__('visits.fields.paid_amount'))
|
|
->formatStateUsing(fn ($state) => $state !== null ? number_format((int) $state) : '-')
|
|
->wrap(false)
|
|
->placeholder('-')
|
|
->summarize(
|
|
Sum::make()
|
|
->label(__('visits.fields.paid_amount'))
|
|
->formatStateUsing(fn ($state) => number_format((int) ($state ?? 0)))
|
|
),
|
|
|
|
TextColumn::make('remained_amount')
|
|
->label(__('visits.fields.remained_amount'))
|
|
->state(fn ($record) => $record->remained_amount)
|
|
->formatStateUsing(fn ($state) => number_format((int) $state))
|
|
->color(fn ($record) => $record->remained_amount > 0 ? 'danger' : 'success')
|
|
->wrap(false),
|
|
|
|
TextColumn::make('paymentTypeRecord.payment_type')
|
|
->label(__('visits.fields.payment_type'))
|
|
->wrap(false)
|
|
->placeholder('-'),
|
|
])
|
|
->defaultSort('visit_date', 'desc')
|
|
->headerActions([
|
|
CreateAction::make()
|
|
->label(__('visits.actions.create'))
|
|
->icon('heroicon-o-plus')
|
|
->slideOver()
|
|
->modalWidth('4xl'),
|
|
])
|
|
->recordActions([
|
|
EditAction::make()
|
|
->slideOver()
|
|
->modalWidth('4xl')
|
|
->modalHeading(fn () => __('filament-actions::edit.single.modal.heading', ['label' => $this->getOwnerRecord()->full_name])),
|
|
DeleteAction::make()
|
|
->modalHeading(fn () => __('filament-actions::delete.single.modal.heading', ['label' => $this->getOwnerRecord()->full_name])),
|
|
])
|
|
->toolbarActions([]);
|
|
}
|
|
}
|