Skip to content

A free open source IT asset/license management system

check that item requested is not null in checkoutrequest, add delete and restore hooks to asset observer for calendar reservations

Godfrey M7287c59af91e
diff --git a/app/Models/CheckoutRequest.php b/app/Models/CheckoutRequest.php
index f5c9a4c3d1..38be45ac58 100644
--- a/app/Models/CheckoutRequest.php
+++ b/app/Models/CheckoutRequest.php
@@ -270,11 +270,13 @@ class CheckoutRequest extends Model
public function name()
{
- if ($this->itemType() == 'asset') {
- return $this->itemRequested()->display_name;
+ $item = $this->itemRequested();
+
+ if (!$item) {
+ return '';
}
- return $this->itemRequested()->name;
+ return $this->itemType() === 'asset' ? $item->display_name : $item->name;
}
/**
diff --git a/app/Observers/AssetObserver.php b/app/Observers/AssetObserver.php
index 5a7b18b500..50dca65e90 100644
--- a/app/Observers/AssetObserver.php
+++ b/app/Observers/AssetObserver.php
@@ -9,6 +9,8 @@ use App\Models\OrderItem;
use App\Models\Setting;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
+use App\Models\CalendarEvent;
+use App\Models\CheckoutRequest;
class AssetObserver
{
@@ -276,6 +278,10 @@ class AssetObserver
$logAction->created_by = auth()->id();
$logAction->logaction('delete');
+ $this->reservationCalendarEventsFor($asset)
+ ->whereNull('deleted_at')
+ ->delete();
+
// Sync-adapter identity rows would otherwise become orphans
// that violate the unique(source, external_id) constraint on
// the next sync run when the vendor keeps reporting the same
@@ -298,6 +304,10 @@ class AssetObserver
$logAction->created_at = date('Y-m-d H:i:s');
$logAction->created_by = auth()->id();
$logAction->logaction('restore');
+
+ $this->reservationCalendarEventsFor($asset)
+ ->whereNotNull('deleted_at')
+ ->restore();
}
/**
@@ -337,4 +347,17 @@ class AssetObserver
$asset->eol_explicit = true;
}
}
+
+ private function reservationCalendarEventsFor(Asset $asset)
+ {
+ $requestIds = CheckoutRequest::withTrashed()
+ ->where('requestable_type', Asset::class)
+ ->where('requestable_id', $asset->id)
+ ->pluck('id');
+
+ return CalendarEvent::withTrashed()
+ ->where('source_type', CheckoutRequest::class)
+ ->whereIn('source_id', $requestIds)
+ ->where('event_type', 'request.reservation');
+ }
}