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 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
10 changes: 7 additions & 3 deletions _ide_helper_models.php
Original file line number Diff line number Diff line change
Expand Up @@ -2133,14 +2133,15 @@ class IdeHelperInteractionType {}
/**
* AdvisingApp\InventoryManagement\Models\Asset
*
* @property string $purchase_age
* @property string $id
* @property string $serial_number
* @property string $name
* @property string $description
* @property string $type_id
* @property string $status_id
* @property string $location_id
* @property string $purchase_date
* @property \Illuminate\Support\Carbon $purchase_date
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
* @property \Illuminate\Support\Carbon|null $deleted_at
Expand Down Expand Up @@ -2320,8 +2321,6 @@ class IdeHelperAssetLocation {}
* @property-read int|null $assets_count
* @property-read \Illuminate\Database\Eloquent\Collection<int, \AdvisingApp\Audit\Models\Audit> $audits
* @property-read int|null $audits_count
* @method static \Illuminate\Database\Eloquent\Builder|AssetStatus available()
* @method static \Illuminate\Database\Eloquent\Builder|AssetStatus checkedOut()
* @method static \AdvisingApp\InventoryManagement\Database\Factories\AssetStatusFactory factory($count = null, $state = [])
* @method static \Illuminate\Database\Eloquent\Builder|AssetStatus newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|AssetStatus newQuery()
Expand Down Expand Up @@ -2710,6 +2709,7 @@ class IdeHelperEvent {}
* @property-read int|null $students_count
* @property-read \Illuminate\Database\Eloquent\Collection<int, \AdvisingApp\MeetingCenter\Models\EventRegistrationFormSubmission> $submissions
* @property-read int|null $submissions_count
* @method static \AdvisingApp\MeetingCenter\Database\Factories\EventAttendeeFactory factory($count = null, $state = [])
* @method static \Illuminate\Database\Eloquent\Builder|EventAttendee newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|EventAttendee newQuery()
* @method static \Illuminate\Database\Eloquent\Builder|EventAttendee query()
Expand Down Expand Up @@ -2748,6 +2748,7 @@ class IdeHelperEventAttendee {}
* @property-read int|null $steps_count
* @property-read \Illuminate\Database\Eloquent\Collection<int, \AdvisingApp\MeetingCenter\Models\EventRegistrationFormSubmission> $submissions
* @property-read int|null $submissions_count
* @method static \AdvisingApp\MeetingCenter\Database\Factories\EventRegistrationFormFactory factory($count = null, $state = [])
* @method static \Illuminate\Database\Eloquent\Builder|EventRegistrationForm newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|EventRegistrationForm newQuery()
* @method static \Illuminate\Database\Eloquent\Builder|EventRegistrationForm onlyTrashed()
Expand Down Expand Up @@ -2814,6 +2815,7 @@ class IdeHelperEventRegistrationFormAuthentication {}
* @property \Illuminate\Support\Carbon|null $updated_at
* @property-read \AdvisingApp\MeetingCenter\Models\EventRegistrationFormStep|null $step
* @property-read \AdvisingApp\MeetingCenter\Models\EventRegistrationForm $submissible
* @method static \AdvisingApp\MeetingCenter\Database\Factories\EventRegistrationFormFieldFactory factory($count = null, $state = [])
* @method static \Illuminate\Database\Eloquent\Builder|EventRegistrationFormField newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|EventRegistrationFormField newQuery()
* @method static \Illuminate\Database\Eloquent\Builder|EventRegistrationFormField query()
Expand Down Expand Up @@ -2846,6 +2848,7 @@ class IdeHelperEventRegistrationFormField {}
* @property-read \Illuminate\Database\Eloquent\Collection<int, \AdvisingApp\MeetingCenter\Models\EventRegistrationFormField> $fields
* @property-read int|null $fields_count
* @property-read \AdvisingApp\MeetingCenter\Models\EventRegistrationForm $submissible
* @method static \AdvisingApp\MeetingCenter\Database\Factories\EventRegistrationFormStepFactory factory($count = null, $state = [])
* @method static \Illuminate\Database\Eloquent\Builder|EventRegistrationFormStep newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|EventRegistrationFormStep newQuery()
* @method static \Illuminate\Database\Eloquent\Builder|EventRegistrationFormStep query()
Expand Down Expand Up @@ -2880,6 +2883,7 @@ class IdeHelperEventRegistrationFormStep {}
* @property-read int|null $fields_count
* @property-read \App\Models\User $requester
* @property-read \AdvisingApp\MeetingCenter\Models\EventRegistrationForm $submissible
* @method static \AdvisingApp\MeetingCenter\Database\Factories\EventRegistrationFormSubmissionFactory factory($count = null, $state = [])
* @method static \Illuminate\Database\Eloquent\Builder|Submission licensedToEducatable(string $relationship)
* @method static \Illuminate\Database\Eloquent\Builder|EventRegistrationFormSubmission newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|EventRegistrationFormSubmission newQuery()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
namespace AdvisingApp\InventoryManagement\Filament\Resources;

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 @@ -111,7 +112,17 @@ 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;
}

return (new Asset([
'purchase_date' => Carbon::parse($state),
]))->purchase_age;
}),
]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@
namespace AdvisingApp\InventoryManagement\Filament\Resources\AssetResource\Pages;

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

if ($record) {
return $record->purchase_age;
}

return (new Asset([
'purchase_date' => Carbon::parse($state),
]))->purchase_age;
}),
]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,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 @@ -64,9 +65,10 @@ public function table(Table $table): Table
TextColumn::make('type.name'),
TextColumn::make('status.name'),
TextColumn::make('location.name'),
TextColumn::make('purchase_date')
->searchable()
->sortable(),
TextColumn::make('purchase_age')
->label('Device Age')
->sortable(['purchase_date'])
->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 @@ -65,7 +65,9 @@ public function infolist(Infolist $infolist): Infolist
->label('Location'),
TextEntry::make('status.name')
->label('Status'),
TextEntry::make('purchase_date'),
TextEntry::make('purchase_age')
->label('Device Age')
->helperText(fn (Asset $record) => $record->purchase_date->format('M j, Y')),
]),
]);
}
Expand Down
29 changes: 29 additions & 0 deletions app-modules/inventory-management/src/Models/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,20 @@

namespace AdvisingApp\InventoryManagement\Models;

use App\Models\User;
use App\Models\BaseModel;
use OwenIt\Auditing\Contracts\Auditable;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use AdvisingApp\Audit\Models\Concerns\Auditable as AuditableTrait;
use AdvisingApp\InventoryManagement\Enums\SystemAssetStatusClassification;

/**
* @property-read string $purchase_age
*
* @mixin IdeHelperAsset
*/
class Asset extends BaseModel implements Auditable
Expand All @@ -63,6 +67,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 Expand Up @@ -121,4 +129,25 @@ public function isCheckedOut(): bool
return $this->status->classification === SystemAssetStatusClassification::CheckedOut
&& is_null($this->latestCheckOut?->asset_check_in_id);
}

protected function purchaseAge(): Attribute
{
return Attribute::get(function () {
if ($this->purchase_date->isFuture()) {
return '0 Years 0 Months';
}

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

$diff = $this
->purchase_date
->roundMonth()
->setTimezone($user?->timezone)
->diff();

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