Skip to content

A free open source IT asset/license management system

Updated builder to use new field

snipe76babf6d51aa
diff --git a/app/Models/Builders/MaintenanceQueryBuilder.php b/app/Models/Builders/MaintenanceQueryBuilder.php
index 7cac41bd1a..f7ae172752 100644
--- a/app/Models/Builders/MaintenanceQueryBuilder.php
+++ b/app/Models/Builders/MaintenanceQueryBuilder.php
@@ -52,32 +52,32 @@ class MaintenanceQueryBuilder extends Builder
public function orderByTag(string $order): static
{
- return $this->leftJoin('assets', 'maintenances.asset_id', '=', 'assets.id')
+ return $this->leftJoin('assets', 'maintenances.item_id', '=', 'assets.id')
->orderBy('assets.asset_tag', $order);
}
public function orderByAssetName(string $order): static
{
- return $this->leftJoin('assets', 'maintenances.asset_id', '=', 'assets.id')
+ return $this->leftJoin('assets', 'maintenances.item_id', '=', 'assets.id')
->orderBy('assets.name', $order);
}
public function orderByAssetSerial(string $order): static
{
- return $this->leftJoin('assets', 'maintenances.asset_id', '=', 'assets.id')
+ return $this->leftJoin('assets', 'maintenances.item_id', '=', 'assets.id')
->orderBy('assets.serial', $order);
}
public function orderStatusName(string $order): static
{
- return $this->join('assets as maintained_asset', 'maintenances.asset_id', '=', 'maintained_asset.id')
+ return $this->join('assets as maintained_asset', 'maintenances.item_id', '=', 'maintained_asset.id')
->leftJoin('status_labels as maintained_asset_status', 'maintained_asset_status.id', '=', 'maintained_asset.status_id')
->orderBy('maintained_asset_status.name', $order);
}
public function orderLocationName(string $order): static
{
- return $this->join('assets as maintained_asset', 'maintenances.asset_id', '=', 'maintained_asset.id')
+ return $this->join('assets as maintained_asset', 'maintenances.item_id', '=', 'maintained_asset.id')
->leftJoin('locations as maintained_asset_location', 'maintained_asset_location.id', '=', 'maintained_asset.location_id')
->orderBy('maintained_asset_location.name', $order);
}
@@ -92,14 +92,14 @@ class MaintenanceQueryBuilder extends Builder
public function orderByAssetModelName(string $order): static
{
- return $this->join('assets as maintained_asset', 'maintenances.asset_id', '=', 'maintained_asset.id')
+ return $this->join('assets as maintained_asset', 'maintenances.item_id', '=', 'maintained_asset.id')
->leftJoin('models as maintained_asset_model', 'maintained_asset_model.id', '=', 'maintained_asset.model_id')
->orderBy('maintained_asset_model.name', $order);
}
public function orderByAssetModelNumber(string $order): static
{
- return $this->join('assets as maintained_asset', 'maintenances.asset_id', '=', 'maintained_asset.id')
+ return $this->join('assets as maintained_asset', 'maintenances.item_id', '=', 'maintained_asset.id')
->leftJoin('models as maintained_asset_model', 'maintained_asset_model.id', '=', 'maintained_asset.model_id')
->orderBy('maintained_asset_model.model_number', $order);
}
diff --git a/app/Models/Maintenance.php b/app/Models/Maintenance.php
index 5e2fa86471..6a8a825127 100644
--- a/app/Models/Maintenance.php
+++ b/app/Models/Maintenance.php
@@ -5,6 +5,7 @@ namespace App\Models;
use App\Helpers\Helper;
use App\Models\Builders\MaintenanceQueryBuilder;
use App\Models\Traits\CompanyableChildTrait;
+use App\Models\Traits\HasCalendarEvents;
use App\Models\Traits\HasUploads;
use App\Models\Traits\Loggable;
use App\Models\Traits\Searchable;
@@ -20,10 +21,19 @@ use Watson\Validating\ValidatingTrait;
* Model for Asset Maintenances.
*
* @version v1.0
+ *
+ * @property-read \App\Models\Asset|null $asset
+ * @property-read \App\Models\Actionlog|null $assetlog
+ * @property-read \App\Models\Supplier|null $supplier
+ * @property-read \App\Models\MaintenanceType|null $maintenanceType
+ * @property-read \App\Models\User|null $adminuser
+ * @property-read \App\Models\User|null $responsibleParty
+ * @property-read \App\Models\User|null $completedByUser
*/
class Maintenance extends SnipeModel implements ICompanyableChild
{
use CompanyableChildTrait;
+ use HasCalendarEvents;
use HasFactory;
use HasUploads;
use Loggable, Presentable;
@@ -37,7 +47,11 @@ class Maintenance extends SnipeModel implements ICompanyableChild
protected $table = 'maintenances';
protected $rules = [
- 'asset_id' => 'required|integer',
+ // item_id is the polymorphic FK. Legacy asset_id keeps working
+ // via the accessor / mutator pair below; both mutators set
+ // item_id so the validator's item_id rule catches either.
+ 'item_id' => 'required|integer',
+ 'item_type' => 'required|string',
'supplier_id' => 'nullable|integer',
'maintenance_type_id' => 'required|integer|exists:maintenance_types,id',
'name' => 'required|max:100',
@@ -65,6 +79,13 @@ class Maintenance extends SnipeModel implements ICompanyableChild
*/
protected $fillable = [
'name',
+ // Polymorphic item FK. Legacy asset_id is kept fillable so
+ // Maintenance::create(['asset_id' => ...]) still works via the
+ // setAssetIdAttribute mutator that forwards to item_id +
+ // item_type=Asset. New callers should pass item_id + item_type
+ // directly.
+ 'item_id',
+ 'item_type',
'asset_id',
'supplier_id',
'asset_maintenance_type',
@@ -216,12 +237,59 @@ class Maintenance extends SnipeModel implements ICompanyableChild
*
* @version v1.0
*/
+ /**
+ * Polymorphic parent - an Asset (historically), and potentially
+ * an Accessory or other checkoutable model going forward. Every
+ * new caller should use ->item; ->asset() below stays as a
+ * backward-compat alias that returns the parent only when it IS
+ * an Asset (returns null for other item_types).
+ */
+ public function item()
+ {
+ return $this->morphTo()->withTrashed();
+ }
+
+ /**
+ * Legacy alias for ->item kept so existing blades, transformers,
+ * and controllers using $maintenance->asset or ->with('asset')
+ * keep working during the transition to polymorphic items.
+ * No item_type WHERE guard here because it would attach to the
+ * assets-table subquery during eager loading (Eloquent scopes the
+ * belongsTo child query at the parent side) and reference a
+ * non-existent maintenances.item_type column from the assets
+ * table context. Every existing maintenance has item_type = Asset
+ * per the rename migration's backfill, so the plain belongsTo
+ * still returns the right rows today. Once accessories actually
+ * carry maintenances, callers should switch to ->item.
+ */
public function asset()
{
- return $this->belongsTo(Asset::class, 'asset_id')
+ return $this->belongsTo(Asset::class, 'item_id')
->withTrashed();
}
+ /**
+ * Legacy accessor for the old asset_id column. Returns item_id
+ * only when the polymorphic parent is an Asset so it doesn't
+ * misrepresent a maintenance attached to an accessory as an
+ * asset-scoped one.
+ */
+ public function getAssetIdAttribute()
+ {
+ return $this->item_type === Asset::class ? $this->attributes['item_id'] ?? null : null;
+ }
+
+ /**
+ * Legacy mutator - Maintenance::create(['asset_id' => X]) still
+ * works. Sets item_id + pins item_type to Asset since callers
+ * using asset_id are, by definition, targeting an Asset.
+ */
+ public function setAssetIdAttribute($value): void
+ {
+ $this->attributes['item_id'] = $value;
+ $this->attributes['item_type'] = Asset::class;
+ }
+
/**
* Get the maintenance logs
*
@@ -267,6 +335,28 @@ class Maintenance extends SnipeModel implements ICompanyableChild
return $this->morphTo('checked_out_to');
}
+ /**
+ * Fields published to the calendar_events index table via the
+ * HasCalendarEvents trait. A single range event per maintenance
+ * anchored on start_date -> expected_completion_date. The
+ * completed_at column is intentionally NOT its own event today -
+ * "completed" is a state, not a scheduled item, and the read
+ * path can flip a completed marker via the source model. Add
+ * more entries here if a maintenance ever needs multiple event
+ * markers on the calendar.
+ */
+ public function calendarEventDefinitions(): array
+ {
+ return [
+ [
+ 'field' => 'start_date',
+ 'event_type' => 'maintenance.start',
+ 'end_field' => 'expected_completion_date',
+ 'all_day' => true,
+ ],
+ ];
+ }
+
public function journal()
{
return $this->assetlog()->where('action_type', '=', 'note added');
diff --git a/app/Models/MaintenanceType.php b/app/Models/MaintenanceType.php
index c0cd1287a7..02665e1efb 100644
--- a/app/Models/MaintenanceType.php
+++ b/app/Models/MaintenanceType.php
@@ -2,6 +2,7 @@
namespace App\Models;
+use App\Presenters\MaintenanceTypePresenter;
use App\Presenters\Presentable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\SoftDeletes;
@@ -15,6 +16,8 @@ class MaintenanceType extends SnipeModel
use SoftDeletes;
use ValidatingTrait;
+ protected $presenter = MaintenanceTypePresenter::class;
+
protected $table = 'maintenance_types';
protected $rules = [
@@ -23,12 +26,13 @@ class MaintenanceType extends SnipeModel
protected $injectUniqueIdentifier = true;
- protected $fillable = ['name'];
+ protected $fillable = ['name', 'tag_color'];
public function isDeletable(): bool
{
return Gate::allows('delete', $this)
- && ($this->deleted_at == '');
+ && ($this->deleted_at == '')
+ && $this->maintenances()->doesntExist();
}
public function maintenances()
@@ -36,6 +40,11 @@ class MaintenanceType extends SnipeModel
return $this->hasMany(Maintenance::class, 'maintenance_type_id');
}
+ public function adminuser()
+ {
+ return $this->belongsTo(User::class, 'created_by')->withTrashed();
+ }
+
public function getDisplayNameAttribute(): string
{
return $this->name;