Skip to content

A free open source IT asset/license management system

Added more accessors and mutators

sniped7bf9b7f2eb3
diff --git a/app/Models/Asset.php b/app/Models/Asset.php
index e6eb590a48..f21454698a 100644
--- a/app/Models/Asset.php
+++ b/app/Models/Asset.php
@@ -227,7 +227,6 @@ class Asset extends Depreciable
}
-
public function customFieldValidationRules()
{
@@ -266,7 +265,6 @@ class Asset extends Depreciable
return parent::save($params);
}
-
public function getDisplayNameAttribute()
{
return $this->present()->name();
@@ -277,20 +275,79 @@ class Asset extends Depreciable
*
* @return \Carbon\Carbon|null
*/
- public function getWarrantyExpiresAttribute()
+
+
+ protected function warrantyExpires(): Attribute
{
- if (isset($this->attributes['warranty_months']) && isset($this->attributes['purchase_date'])) {
- if (is_string($this->attributes['purchase_date']) || is_string($this->attributes['purchase_date'])) {
- $purchase_date = \Carbon\Carbon::parse($this->attributes['purchase_date']);
- } else {
- $purchase_date = \Carbon\Carbon::instance($this->attributes['purchase_date']);
+ return Attribute:: make(
+ get: fn(mixed $value, array $attributes) => ($attributes['warranty_months'] && $attributes['purchase_date']) ? Carbon::parse($attributes['purchase_date'])->addMonths($attributes['warranty_months']) : null,
+ );
+ }
+
+ protected function warrantyExpiresFormattedDate(): Attribute
+ {
+
+ return Attribute:: make(
+ get: fn(mixed $value, array $attributes) => Helper::getFormattedDateObject($this->warrantyExpires, 'date', false)
+ );
+ }
+
+ protected function warrantyExpiresDiff(): Attribute
+ {
+ return Attribute:: make(
+ get: fn(mixed $value, array $attributes) => $this->warrantyExpires ? round((Carbon::now()->diffInDays($this->warrantyExpires))) : null,
+ );
+
+ }
+
+ protected function warrantyExpiresDiffForHumans(): Attribute
+ {
+ return Attribute:: make(
+ get: fn(mixed $value, array $attributes) => $this->warrantyExpires ? Carbon::parse($this->warrantyExpires)->diffForHumans() : null,
+ );
+
+ }
+
+ protected function eolDate(): Attribute
+ {
+
+ return Attribute:: make(
+ get: function(mixed $value, array $attributes) {
+ if ($attributes['asset_eol_date'] && $attributes['eol_explicit'] == '1') {
+ return Carbon::parse($attributes['asset_eol_date']);
+ } elseif ($attributes['purchase_date'] && $this->model && ((int) $this->model->eol > 0)) {
+ return Carbon::parse($attributes['purchase_date'])->addMonths((int) $this->model->eol);
+ } else {
+ return null;
+ }
}
- $purchase_date->setTime(0, 0, 0);
+ );
- return $purchase_date->addMonths((int) $this->attributes['warranty_months']);
- }
+ }
+
+
+ protected function eolFormattedDate(): Attribute
+ {
+ return Attribute:: make(
+ get: fn(mixed $value, array $attributes) => $this->eolDate ? Helper::getFormattedDateObject($this->eolDate, 'date', false) : null,
+ );
+ }
+
+ protected function eolDiff(): Attribute
+ {
+ return Attribute:: make(
+ get: fn(mixed $value, array $attributes) => $this->eolDate ? round((Carbon::now()->diffInDays(Carbon::parse($this->eolDate), false, 1))) : null,
+ );
+
+ }
+
+ protected function eolDiffForHumans(): Attribute
+ {
+
+ return Attribute:: make(
+ get: fn(mixed $value, array $attributes) => $this->eolDate ? Carbon::parse($this->eolDate)->diffForHumans() : null,
+ );
- return null;
}
diff --git a/app/Models/SnipeModel.php b/app/Models/SnipeModel.php
index ae65664474..b4db5f2c95 100644
--- a/app/Models/SnipeModel.php
+++ b/app/Models/SnipeModel.php
@@ -3,6 +3,7 @@
namespace App\Models;
use App\Helpers\Helper;
+use Carbon\Carbon;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model;
@@ -11,12 +12,43 @@ use Illuminate\Support\Facades\Request;
class SnipeModel extends Model
{
// Setters that are appropriate across multiple models.
- public function setPurchaseDateAttribute($value)
+
+
+ protected function purchaseDate(): Attribute
{
- if ($value == '') {
- $value = null;
- }
- $this->attributes['purchase_date'] = $value;
+ return Attribute:: make(
+ set: fn(mixed $value) => $value ?: null,
+ get: fn(mixed $value) => $value,
+ );
+ }
+
+ protected function purchaseDateFormatted(): Attribute
+ {
+ return Attribute:: make(
+ get: fn(mixed $value, array $attributes) => $attributes['purchase_date'] ? Helper::getFormattedDateObject(Carbon::parse($attributes['purchase_date']), 'date', false) : null,
+ );
+ }
+
+
+ protected function expiresDiff(): Attribute
+ {
+ return Attribute:: make(
+ get: fn(mixed $value, array $attributes) => $attributes['expiration_date'] ? round((Carbon::now()->diffInDays(Carbon::parse($attributes['expiration_date']), false, 1))) : null,
+ );
+ }
+
+ protected function expiresDiffForHumans(): Attribute
+ {
+ return Attribute:: make(
+ get: fn(mixed $value, array $attributes) => $attributes['expiration_date'] ? Carbon::parse($attributes['expiration_date'])->diffForHumans() : null,
+ );
+ }
+
+ protected function expiresFormattedDate(): Attribute
+ {
+ return Attribute:: make(
+ get: fn(mixed $value, array $attributes) => $attributes['expiration_date'] ? Helper::getFormattedDateObject($attributes['expiration_date'], 'date', false) : null,
+ );
}
/**
@@ -180,6 +212,7 @@ class SnipeModel extends Model
);
}
+
public function getEula()
{