From 8cce8b29f29ea5ac17b09717e08d3660781288e5 Mon Sep 17 00:00:00 2001 From: Alex Popa Date: Thu, 12 Dec 2024 12:01:16 +0200 Subject: [PATCH] Birthdate format --- app/Concerns/HasBirthdate.php | 24 +++++++++++++++++++ .../Pages/EditBeneficiaryIdentity.php | 8 +++---- .../Pages/ViewBeneficiary.php | 2 +- .../Pages/ViewBeneficiaryIdentity.php | 5 ++-- .../Pages/CreateMonitoring.php | 5 ++++ .../MonitoringResource/Pages/EditChildren.php | 4 ++-- .../Pages/ViewMonitoring.php | 3 ++- app/Forms/Components/DateInput.php | 2 +- app/Models/Beneficiary.php | 4 ++-- app/Models/Children.php | 9 ++----- app/Models/MonitoringChild.php | 3 ++- database/factories/ChildrenFactory.php | 1 - 12 files changed, 47 insertions(+), 23 deletions(-) create mode 100644 app/Concerns/HasBirthdate.php diff --git a/app/Concerns/HasBirthdate.php b/app/Concerns/HasBirthdate.php new file mode 100644 index 00000000..5bfeab24 --- /dev/null +++ b/app/Concerns/HasBirthdate.php @@ -0,0 +1,24 @@ +casts['birthdate'] = 'date'; + + $this->fillable[] = 'birthdate'; + } + + public function setBirthdateAttribute(?string $value = null): void + { + $date = $value ? Carbon::createFromFormat('d-m-Y', $value) : null; + + $this->attributes['birthdate'] = $date?->format('Y-m-d'); + } +} diff --git a/app/Filament/Organizations/Resources/BeneficiaryResource/Pages/EditBeneficiaryIdentity.php b/app/Filament/Organizations/Resources/BeneficiaryResource/Pages/EditBeneficiaryIdentity.php index 09a74ca0..dd97178b 100644 --- a/app/Filament/Organizations/Resources/BeneficiaryResource/Pages/EditBeneficiaryIdentity.php +++ b/app/Filament/Organizations/Resources/BeneficiaryResource/Pages/EditBeneficiaryIdentity.php @@ -13,7 +13,7 @@ use App\Enums\Gender; use App\Enums\IDType; use App\Filament\Organizations\Resources\BeneficiaryResource; -use App\Forms\Components\DatePicker; +use App\Forms\Components\DateInput; use App\Forms\Components\Location; use App\Forms\Components\Select; use App\Forms\Components\Spacer; @@ -136,7 +136,7 @@ public static function getBeneficiaryIdentityFormSchema(?Beneficiary $parentBene return; } - if (filled($birthdate = (new Cnp($state))->getBirthDateFromCNP())) { + if (filled($birthdate = (new Cnp($state))->getBirthDateFromCNP('d-m-Y'))) { $set('birthdate', $birthdate); } }); @@ -154,10 +154,8 @@ public static function getBeneficiaryIdentityFormSchema(?Beneficiary $parentBene ->options(Gender::options()) ->enum(Gender::class), - DatePicker::make('birthdate') + DateInput::make('birthdate') ->label(__('field.birthdate')) - ->maxDate(today()->endOfDay()) - ->nullable() ->live(), TextInput::make('birthplace') diff --git a/app/Filament/Organizations/Resources/BeneficiaryResource/Pages/ViewBeneficiary.php b/app/Filament/Organizations/Resources/BeneficiaryResource/Pages/ViewBeneficiary.php index 6df13644..c604fe0b 100644 --- a/app/Filament/Organizations/Resources/BeneficiaryResource/Pages/ViewBeneficiary.php +++ b/app/Filament/Organizations/Resources/BeneficiaryResource/Pages/ViewBeneficiary.php @@ -179,7 +179,7 @@ protected function identitySectionSection(): Section TextEntry::make('birthdate') ->label(__('field.birthdate')) - ->formatStateUsing(fn ($state) => $state === '-' ? $state : $state->toDateString()), + ->formatStateUsing(fn ($state) => $state !== '-' ? $state->format('m-d-Y') : $state), EnumEntry::make('gender') ->label(__('field.gender')), diff --git a/app/Filament/Organizations/Resources/BeneficiaryResource/Pages/ViewBeneficiaryIdentity.php b/app/Filament/Organizations/Resources/BeneficiaryResource/Pages/ViewBeneficiaryIdentity.php index 8c5e1247..7865991c 100644 --- a/app/Filament/Organizations/Resources/BeneficiaryResource/Pages/ViewBeneficiaryIdentity.php +++ b/app/Filament/Organizations/Resources/BeneficiaryResource/Pages/ViewBeneficiaryIdentity.php @@ -124,7 +124,7 @@ protected static function identitySchema(): array TextEntry::make('birthdate') ->label(__('field.birthdate')) - ->formatStateUsing(fn ($state) => $state !== '-' ? $state->format('Y-m-d') : $state), + ->formatStateUsing(fn ($state) => $state !== '-' ? $state->format('d-m-Y') : $state), TextEntry::make('birthplace') ->label(__('field.birthplace')) @@ -278,7 +278,8 @@ public static function childrenSchema(): array ->label(__('field.gender')), TextEntry::make('birthdate') - ->label(__('field.birthdate')), + ->label(__('field.birthdate')) + ->formatStateUsing(fn ($state) => $state !== '-' ? $state->format('d-m-Y') : $state), TextEntry::make('current_address') ->label(__('field.current_address')), diff --git a/app/Filament/Organizations/Resources/MonitoringResource/Pages/CreateMonitoring.php b/app/Filament/Organizations/Resources/MonitoringResource/Pages/CreateMonitoring.php index c3c6ca3e..082f7829 100644 --- a/app/Filament/Organizations/Resources/MonitoringResource/Pages/CreateMonitoring.php +++ b/app/Filament/Organizations/Resources/MonitoringResource/Pages/CreateMonitoring.php @@ -10,6 +10,7 @@ use App\Models\Monitoring; use App\Models\Specialist; use App\Services\Breadcrumb\BeneficiaryBreadcrumb; +use Carbon\Carbon; use Filament\Actions\Action; use Filament\Forms\Components\Wizard; use Filament\Forms\Set; @@ -70,6 +71,10 @@ protected function afterFill(): void ->first() ?->load(['children', 'specialistsTeam']); $this->children = $this->getChildren(); + foreach ($this->children as &$child) { + $child['birthdate'] = $child['birthdate'] ? Carbon::parse($child['birthdate'])->format('d-m-Y') : null; + } + $this->specialistTeam = $this->getSpecialists(); $data = [ diff --git a/app/Filament/Organizations/Resources/MonitoringResource/Pages/EditChildren.php b/app/Filament/Organizations/Resources/MonitoringResource/Pages/EditChildren.php index dac3678a..352699f9 100644 --- a/app/Filament/Organizations/Resources/MonitoringResource/Pages/EditChildren.php +++ b/app/Filament/Organizations/Resources/MonitoringResource/Pages/EditChildren.php @@ -9,7 +9,7 @@ use App\Enums\ChildAggressorRelationship; use App\Enums\MaintenanceSources; use App\Filament\Organizations\Resources\MonitoringResource; -use App\Forms\Components\DatePicker; +use App\Forms\Components\DateInput; use App\Forms\Components\Repeater; use App\Forms\Components\Select; use App\Services\Breadcrumb\BeneficiaryBreadcrumb; @@ -83,7 +83,7 @@ public static function getSchema(): array ->maxLength(2) ->mask('99'), - DatePicker::make('birthdate') + DateInput::make('birthdate') ->label(__('monitoring.labels.birthdate')), Select::make('aggressor_relationship') diff --git a/app/Filament/Organizations/Resources/MonitoringResource/Pages/ViewMonitoring.php b/app/Filament/Organizations/Resources/MonitoringResource/Pages/ViewMonitoring.php index cb12b291..dc6a3e63 100644 --- a/app/Filament/Organizations/Resources/MonitoringResource/Pages/ViewMonitoring.php +++ b/app/Filament/Organizations/Resources/MonitoringResource/Pages/ViewMonitoring.php @@ -128,7 +128,8 @@ public function infolist(Infolist $infolist): Infolist ->label(__('monitoring.labels.age')), TextEntry::make('birthdate') - ->label(__('monitoring.labels.birthdate')), + ->label(__('monitoring.labels.birthdate')) + ->formatStateUsing(fn ($state) => $state !== '-' ? $state->format('d-m-Y') : $state), TextEntry::make('aggressor_relationship') ->label(__('monitoring.labels.aggressor_relationship')), diff --git a/app/Forms/Components/DateInput.php b/app/Forms/Components/DateInput.php index f0366090..fc736fac 100644 --- a/app/Forms/Components/DateInput.php +++ b/app/Forms/Components/DateInput.php @@ -16,7 +16,7 @@ protected function setUp(): void $this->placeholder('dd-mm-yyyy'); $this->mask('99-99-9999'); - $this->formatStateUsing(fn (string $state) => $state ? Carbon::parse($state)->format('d-m-Y') : null); + $this->formatStateUsing(fn (?string $state) => $state ? Carbon::parse($state)->format('d-m-Y') : null); $this->rules([ 'date_format:d-m-Y', fn (): Closure => function (string $attribute, $value, Closure $fail) { diff --git a/app/Models/Beneficiary.php b/app/Models/Beneficiary.php index 2d8ac9cc..fdba5122 100644 --- a/app/Models/Beneficiary.php +++ b/app/Models/Beneficiary.php @@ -5,6 +5,7 @@ namespace App\Models; use App\Concerns\BelongsToOrganization; +use App\Concerns\HasBirthdate; use App\Concerns\HasCaseStatus; use App\Concerns\HasCitizenship; use App\Concerns\HasEffectiveAddress; @@ -36,6 +37,7 @@ class Beneficiary extends Model use LogsActivity; use LogsActivityOptions; use HasSpecialistsTeam; + use HasBirthdate; protected $fillable = [ 'initial_id', @@ -47,7 +49,6 @@ class Beneficiary extends Model 'cnp', 'gender', - 'birthdate', 'birthplace', 'ethnicity', @@ -80,7 +81,6 @@ class Beneficiary extends Model protected $casts = [ 'id_type' => IDType::class, - 'birthdate' => 'date', 'children_18_care_count' => 'integer', 'children_accompanying_count' => 'integer', 'children_care_count' => 'integer', diff --git a/app/Models/Children.php b/app/Models/Children.php index 58b93b3a..5170da35 100644 --- a/app/Models/Children.php +++ b/app/Models/Children.php @@ -5,6 +5,7 @@ namespace App\Models; use App\Concerns\BelongsToBeneficiary; +use App\Concerns\HasBirthdate; use App\Enums\GenderShortValues; use Carbon\Carbon; use Illuminate\Database\Eloquent\Factories\HasFactory; @@ -14,10 +15,10 @@ class Children extends Model { use HasFactory; use BelongsToBeneficiary; + use HasBirthdate; protected $fillable = [ 'name', - 'birthdate', 'current_address', 'status', 'gender', @@ -34,10 +35,4 @@ public function getAgeAttribute(): int | string | null return $age === 0 ? '<1' : $age; } - - public function setBirthdateAttribute(?string $value = null): void - { - $date = Carbon::createFromFormat('d-m-Y', $value); - $this->attributes['birthdate'] = $date->format('Y-m-d'); - } } diff --git a/app/Models/MonitoringChild.php b/app/Models/MonitoringChild.php index 372e0ea4..8df8debe 100644 --- a/app/Models/MonitoringChild.php +++ b/app/Models/MonitoringChild.php @@ -4,6 +4,7 @@ namespace App\Models; +use App\Concerns\HasBirthdate; use App\Enums\ChildAggressorRelationship; use App\Enums\MaintenanceSources; use Illuminate\Database\Eloquent\Factories\HasFactory; @@ -13,13 +14,13 @@ class MonitoringChild extends Model { use HasFactory; + use HasBirthdate; protected $fillable = [ 'monitoring_id', 'name', 'status', 'age', - 'birthdate', 'aggressor_relationship', 'maintenance_sources', 'location', diff --git a/database/factories/ChildrenFactory.php b/database/factories/ChildrenFactory.php index 019ff0d2..4ec2bae3 100644 --- a/database/factories/ChildrenFactory.php +++ b/database/factories/ChildrenFactory.php @@ -20,7 +20,6 @@ public function definition(): array { return [ 'name' => fake()->name(), - 'age' => fake()->boolean() ? fake()->numberBetween(0, 20) : null, 'birthdate' => fake()->date(), 'current_address' => fake()->boolean() ? fake()->address() : null, 'status' => fake()->boolean() ? fake()->words(asText: true) : null,