Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ADVAPP-206]: Display device age as a calculated column in years and months in assets #434

Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@

namespace AdvisingApp\InventoryManagement\Filament\Resources;

use App\Models\User;
use Filament\Forms\Form;
use Illuminate\Support\Carbon;
use Filament\Resources\Resource;
use Filament\Resources\Pages\Page;
use Filament\Forms\Components\Select;
Expand Down Expand Up @@ -68,8 +70,6 @@ class AssetResource extends Resource

protected static ?string $breadcrumb = 'Asset Management';

protected static bool $shouldRegisterNavigation = false;

public function getTitle(): string | Htmlable
{
return 'Manage Assets';
Expand Down Expand Up @@ -112,7 +112,30 @@ public static function form(Form $form): Form
->required()
->exists((new AssetLocation())->getTable(), 'id'),
DatePicker::make('purchase_date')
->required(),
->required()
->live()
->helperText(function (?string $state) {
if (blank($state)) {
return null;
}

$date = Carbon::parse($state);

if ($date->isFuture()) {
return '0 Years 0 Months';
}

/** @var User $user */
$user = auth()->user();

$diff = $date
->roundMonth()
->setTimezone($user->timezone)
->diff();

return $diff->y . ' ' . ($diff->y === 1 ? 'Year' : 'Years') . ' ' .
$diff->m . ' ' . ($diff->m === 1 ? 'Month' : 'Months');
}),
]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@

namespace AdvisingApp\InventoryManagement\Filament\Resources\AssetResource\Pages;

use App\Models\User;
use Filament\Forms\Form;
use Illuminate\Support\Carbon;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
Expand Down Expand Up @@ -80,7 +82,30 @@ public function form(Form $form): Form
->required()
->exists((new AssetLocation())->getTable(), 'id'),
DatePicker::make('purchase_date')
->required(),
->required()
->live()
->helperText(function (?string $state) {
if (blank($state)) {
return null;
}

$date = Carbon::parse($state);

if ($date->isFuture()) {
return '0 Years 0 Months';
}

/** @var User $user */
$user = auth()->user();

$diff = $date
->roundMonth()
->setTimezone($user->timezone)
->diff();

return $diff->y . ' ' . ($diff->y === 1 ? 'Year' : 'Years') . ' ' .
$diff->m . ' ' . ($diff->m === 1 ? 'Month' : 'Months');
}),
]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@

namespace AdvisingApp\InventoryManagement\Filament\Resources\AssetResource\Pages;

use App\Models\User;
use Filament\Tables\Table;
use Illuminate\Support\Carbon;
use Filament\Actions\CreateAction;
use Filament\Tables\Actions\EditAction;
use Filament\Resources\Pages\ListRecords;
Expand All @@ -45,6 +47,7 @@
use Filament\Tables\Actions\BulkActionGroup;
use Filament\Tables\Actions\DeleteBulkAction;
use App\Filament\Columns\OpenSearch\TextColumn;
use AdvisingApp\InventoryManagement\Models\Asset;
use AdvisingApp\InventoryManagement\Filament\Resources\AssetResource;

class ListAssets extends ListRecords
Expand All @@ -65,8 +68,27 @@ public function table(Table $table): Table
TextColumn::make('status.name'),
TextColumn::make('location.name'),
TextColumn::make('purchase_date')
->searchable()
->sortable(),
->label('Device Age')
->sortable()
->formatStateUsing(function (string $state) {
$date = Carbon::parse($state);

if ($date->isFuture()) {
return '0 Years 0 Months';
}

/** @var User $user */
$user = auth()->user();

$diff = $date
->roundMonth()
->setTimezone($user->timezone)
->diff();

return $diff->y . ' ' . ($diff->y === 1 ? 'Year' : 'Years') . ' ' .
$diff->m . ' ' . ($diff->m === 1 ? 'Month' : 'Months');
Orrison marked this conversation as resolved.
Show resolved Hide resolved
})
->tooltip(fn (Asset $record) => $record->purchase_date->format('M j, Y')),
])
->filters([
SelectFilter::make('type')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@

namespace AdvisingApp\InventoryManagement\Filament\Resources\AssetResource\Pages;

use App\Models\User;
use Illuminate\Support\Carbon;
use Filament\Actions\EditAction;
use Filament\Infolists\Infolist;
use Filament\Resources\Pages\ViewRecord;
Expand Down Expand Up @@ -65,7 +67,27 @@ public function infolist(Infolist $infolist): Infolist
->label('Location'),
TextEntry::make('status.name')
->label('Status'),
TextEntry::make('purchase_date'),
TextEntry::make('purchase_date') //Strict
->label('Device Age')
->formatStateUsing(function (string $state) {
$date = Carbon::parse($state);

if ($date->isFuture()) {
return '0 Years 0 Months';
}

/** @var User $user */
$user = auth()->user();

$diff = $date
->roundMonth()
->setTimezone($user->timezone)
->diff();

return $diff->y . ' ' . ($diff->y === 1 ? 'Year' : 'Years') . ' ' .
$diff->m . ' ' . ($diff->m === 1 ? 'Month' : 'Months');
})
->helperText(fn (Asset $record) => $record->purchase_date->format('M j, Y')),
]),
]);
}
Expand Down
4 changes: 4 additions & 0 deletions app-modules/inventory-management/src/Models/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ class Asset extends BaseModel implements Auditable
'type_id',
];

protected $casts = [
'purchase_date' => 'datetime',
];

public function type(): BelongsTo
{
return $this->belongsTo(AssetType::class, 'type_id');
Expand Down