Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/Filament/Shelter/Resources/BeneficiaryResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public static function table(Table $table): Table
->formatStateUsing(fn (Stay $state) => \sprintf(
'%s – %s',
$state->start_date->toFormattedDate(),
$state->end_date->toFormattedDate(),
$state->end_date?->toFormattedDate() ?? __('app.stay.indefinite')
)),
])
->filters([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

namespace App\Filament\Shelter\Resources\BeneficiaryResource\Actions;

use App\Filament\Shelter\Resources\BeneficiaryResource\Schemas\StayForm;
use App\Models\Stay;
use Filament\Actions\Action;
use Filament\Forms\Components\DatePicker;
use Filament\Forms\Components\Hidden;

class ExtendStayAction extends Action
{
Expand All @@ -29,22 +30,25 @@ protected function setUp(): void

$this->modalHeading(
fn (Stay $record) => __('app.stay.actions.extend.confirm.title', [
'stay' => $record->id,
'start_date' => $record->start_date->toFormattedDate(),
'end_date' => $record->end_date->toFormattedDate(),
'title' => $record->title,
])
);

$this->modalDescription(__('app.stay.actions.extend.confirm.description'));

$this->modalWidth('md');

$this->fillForm(fn (Stay $record) => [
'start_date' => $record->start_date,
'end_date' => $record->end_date,
'is_indefinite' => $record->is_indefinite,
]);

$this->form([
DatePicker::make('end_date')
->label(__('app.field.end_date'))
->after(fn (Stay $record) => $record->end_date->toDateString())
->default(fn (Stay $record) => $record->end_date->toDateString())
->required(),
Hidden::make('start_date')
->label(__('app.field.start_date')),

StayForm::getEndDateGroup(),
]);

$this->action(function (Stay $record, array $data) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Get;
use Filament\Forms\Set;
use Illuminate\Support\Str;

class StayForm
Expand All @@ -32,10 +33,7 @@ public static function getSchema(?int $beneficiary_id = null): array
->label(__('app.field.start_date'))
->required(),

DatePicker::make('end_date')
->label(__('app.field.end_date'))
->afterOrEqual('start_date')
->required(),
static::getEndDateGroup(),

Checkbox::make('has_children')
->label(__('app.field.has_children'))
Expand Down Expand Up @@ -87,4 +85,25 @@ public static function getSchema(?int $beneficiary_id = null): array
]),
];
}

public static function getEndDateGroup(): Group
{
return Group::make()
->schema([
DatePicker::make('end_date')
->label(__('app.field.end_date'))
->afterOrEqual('start_date')
->required(fn (Get $get) => ! $get('is_indefinite'))
->disabled(fn (Get $get) => $get('is_indefinite')),

Checkbox::make('is_indefinite')
->label(__('app.field.is_indefinite'))
->live()
->afterStateUpdated(function (bool $state, Set $set) {
if ($state) {
$set('end_date', null);
}
}),
]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@

use App\Filament\Shelter\Resources\RequestResource;
use App\Models\Stay;
use Carbon\Carbon;
use Filament\Infolists\Components\Group;
use Filament\Infolists\Components\Section;
use Filament\Infolists\Components\TextEntry;
use Filament\Infolists\Infolist;

class StayInfolist
{
Expand All @@ -34,9 +36,7 @@ public static function getSchema(): array
->label(__('app.field.start_date'))
->date(),

TextEntry::make('end_date')
->label(__('app.field.end_date'))
->date(),
static::getEndDateTextEntry(),

TextEntry::make('has_children')
->label(__('app.field.has_children'))
Expand Down Expand Up @@ -65,4 +65,20 @@ public static function getSchema(): array
]),
];
}

public static function getEndDateTextEntry(): TextEntry
{
return TextEntry::make('end_date')
->label(__('app.field.end_date'))
->date()
->formatStateUsing(function (TextEntry $component, $state) {
if (blank($state) || $component->getDefaultState() === $state) {
return __('app.stay.indefinite');
}

return Carbon::parse($state)
->setTimezone($component->getTimezone())
->translatedFormat($component->evaluate(Infolist::$defaultDateDisplayFormat));
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use App\Filament\Shelter\Resources\RequestResource;
use App\Models\Beneficiary;
use App\Models\Stay;
use Carbon\Carbon;
use Filament\Tables\Actions\CreateAction;
use Filament\Tables\Actions\ViewAction;
use Filament\Tables\Columns\TextColumn;
Expand Down Expand Up @@ -40,7 +41,16 @@ public function table(Table $table): Table

TextColumn::make('end_date')
->label(__('app.field.end_date'))
->date()
->default(__('app.stay.indefinite'))
->formatStateUsing(function (TextColumn $column, $state) {
if (blank($state) || $state === __('app.stay.indefinite')) {
return $state;
}

return Carbon::parse($state)
->setTimezone($column->getTimezone())
->translatedFormat($column->evaluate(Table::$defaultDateDisplayFormat));
})
->sortable()
->shrink(),

Expand Down
2 changes: 1 addition & 1 deletion app/Models/Beneficiary.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function stays(): HasMany

public function latestStay(): HasOne
{
return $this->hasOne(Stay::class)->latestOfMany('end_date');
return $this->hasOne(Stay::class)->latestOfMany();
}

public function documents(): HasMany
Expand Down
16 changes: 13 additions & 3 deletions app/Models/Stay.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,24 @@ public function scopeWhereCurrent(Builder $query): Builder
{
return $query
->whereDate('start_date', '<=', today())
->whereDate('end_date', '>=', today());
->where(function (Builder $query) {
$query->whereDate('end_date', '>=', today())
->orWhereNull('end_date');
});
}

public function scopeWhereInShelter(Builder $query, Shelter $shelter): Builder
{
return $query->where('shelter_id', $shelter->id);
}

protected function isIndefinite(): Attribute
{
return Attribute::make(
get: fn (mixed $value, array $attributes) => blank($attributes['end_date']),
);
}

public function hasChildren(): Attribute
{
return Attribute::make(
Expand All @@ -80,10 +90,10 @@ public function title(): Attribute
{
return Attribute::make(
fn () => \sprintf(
'#%s %s%s',
'#%s %s - %s',
$this->id,
$this->start_date->toFormattedDate(),
$this->end_date->toFormattedDate()
$this->end_date?->toFormattedDate() ?? __('app.stay.indefinite')
)
);
}
Expand Down
7 changes: 7 additions & 0 deletions database/factories/ShelterFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ public function configure(): static
->for($shelter)
->create();

Stay::factory()
->count($beneficiaries->count())
->sequence(...$beneficiaries)
->for($shelter)
->indefinite()
->create();

Request::factory()
->count(10)
->for($shelter)
Expand Down
7 changes: 7 additions & 0 deletions database/factories/StayFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,11 @@ public function definition(): array
'children_notes' => $children ? fake()->optional()->sentence() : null,
];
}

public function indefinite(): static
{
return $this->state([
'end_date' => null,
]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('stays', function (Blueprint $table) {
$table->date('end_date')->nullable()->change();
});
}
};
4 changes: 3 additions & 1 deletion lang/en/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
'id_type' => 'ID type',
'id' => 'ID',
'identifier' => 'Identifier',
'is_indefinite' => 'Indefinite end date',
'label' => 'Label',
'latest_stay' => 'Latest stay',
'legal_documents' => 'Legal documents',
Expand Down Expand Up @@ -130,13 +131,14 @@
'singular' => 'stay',
'plural' => 'stays',
],
'indefinite' => 'Indefinite',
'details' => 'Stay details',
'actions' => [
'extend' => [
'button' => 'Extend stay',
'confirm' => [
'description' => 'Extend a beneficiary\'s stay by updating the end date.',
'title' => 'Extend stay #:stay :start_date—:end_date',
'title' => 'Extend stay :title',
'success' => 'Stay extended successfully.',
],
],
Expand Down
2 changes: 1 addition & 1 deletion lang/es/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@
'button' => 'Prolongar estancia',
'confirm' => [
'description' => 'Amplíe la estancia de un beneficiario actualizando la fecha de finalización.',
'title' => 'Prolongar estancia #:stay:start_date—:end_date',
'title' => 'Prolongar estancia :title',
'success' => 'Manténgase extendido con éxito.',
],
],
Expand Down
Loading