A free open source IT asset/license management system
Link to the reservation if the type of event is a request fulfillment
snipe53625b784e8c
diff --git a/app/Models/CheckoutRequest.php b/app/Models/CheckoutRequest.phpindex 8809f79dbc..f5c9a4c3d1 100644--- a/app/Models/CheckoutRequest.php+++ b/app/Models/CheckoutRequest.php@@ -105,11 +105,43 @@ class CheckoutRequest extends Model* these methods when a source model isn't wired through the* Presenter/Presentable chain (which CheckoutRequest isn't - it* extends the plain Eloquent Model, not SnipeModel).+ *+ * Reservations link to the checkout confirmation screen with a+ * `request_id` query hint, so an admin clicking a today-widget+ * row lands on the pre-filled fulfill flow for that specific+ * request. Every checkout controller under Assets / Accessories+ * / Components / Consumables / Licenses honors the hint. Types+ * without a dedicated checkout screen (AssetModel) fall back to+ * the presenter's calendarUrl, which points at the item's show+ * page.*/public function calendarUrl(): ?string{$item = $this->itemRequested();- if ($item && method_exists($item, 'present')) {+ if (! $item) {+ return null;+ }++ $checkoutRoute = match (true) {+ $item instanceof Asset => ['hardware.checkout.create', ['asset' => $item->id]],+ $item instanceof Accessory => ['accessories.checkout.show', ['accessory' => $item->id]],+ $item instanceof Component => ['components.checkout.show', ['componentID' => $item->id]],+ $item instanceof Consumable => ['consumables.checkout.show', ['consumablesID' => $item->id]],+ $item instanceof License => ['licenses.checkout', ['license' => $item->id]],+ // AssetModel reservations point at the multi-fulfill+ // screen where an admin picks specific assets of that+ // model to fulfill the request against.+ $item instanceof AssetModel => ['models.fulfill-requests.create', ['model' => $item->id]],+ default => null,+ };++ if ($checkoutRoute !== null) {+ [$name, $params] = $checkoutRoute;++ return route($name, array_merge($params, ['request_id' => $this->id]));+ }++ if (method_exists($item, 'present')) {$presenter = $item->present();if (method_exists($presenter, 'calendarUrl')) {return $presenter->calendarUrl();diff --git a/tests/Unit/Models/CheckoutRequestTest.php b/tests/Unit/Models/CheckoutRequestTest.phpindex c95aafdfb0..96f8fb1dad 100644--- a/tests/Unit/Models/CheckoutRequestTest.php+++ b/tests/Unit/Models/CheckoutRequestTest.php@@ -2,7 +2,11 @@namespace Tests\Unit\Models;+use App\Models\Accessory;use App\Models\CheckoutRequest;+use App\Models\Component;+use App\Models\Consumable;+use App\Models\License;use Tests\TestCase;class CheckoutRequestTest extends TestCase@@ -93,4 +97,82 @@ class CheckoutRequestTest extends TestCase'Property access `->name` must return the accessor value, not throw when Eloquent misinterprets name() as a relation.');}++ public function test_calendar_url_points_at_asset_checkout_with_request_id()+ {+ $checkoutRequest = CheckoutRequest::factory()->forAsset()->create();+ $asset = $checkoutRequest->requestedItem;++ $url = $checkoutRequest->calendarUrl();++ $this->assertStringContainsString(route('hardware.checkout.create', ['asset' => $asset->id]), $url);+ $this->assertStringContainsString('request_id='.$checkoutRequest->id, $url);+ }++ public function test_calendar_url_points_at_asset_model_fulfill_screen_with_request_id()+ {+ $checkoutRequest = CheckoutRequest::factory()->forAssetModel()->create();+ $model = $checkoutRequest->requestedItem;++ $url = $checkoutRequest->calendarUrl();++ $this->assertStringContainsString(route('models.fulfill-requests.create', ['model' => $model->id]), $url);+ $this->assertStringContainsString('request_id='.$checkoutRequest->id, $url);+ }++ public function test_calendar_url_points_at_component_checkout_with_request_id()+ {+ $component = Component::factory()->create();+ $checkoutRequest = CheckoutRequest::factory()->create([+ 'requestable_type' => Component::class,+ 'requestable_id' => $component->id,+ ]);++ $url = $checkoutRequest->calendarUrl();++ $this->assertStringContainsString(route('components.checkout.show', ['componentID' => $component->id]), $url);+ $this->assertStringContainsString('request_id='.$checkoutRequest->id, $url);+ }++ public function test_calendar_url_points_at_accessory_checkout_with_request_id()+ {+ $accessory = Accessory::factory()->create();+ $checkoutRequest = CheckoutRequest::factory()->create([+ 'requestable_type' => Accessory::class,+ 'requestable_id' => $accessory->id,+ ]);++ $url = $checkoutRequest->calendarUrl();++ $this->assertStringContainsString(route('accessories.checkout.show', ['accessory' => $accessory->id]), $url);+ $this->assertStringContainsString('request_id='.$checkoutRequest->id, $url);+ }++ public function test_calendar_url_points_at_consumable_checkout_with_request_id()+ {+ $consumable = Consumable::factory()->create();+ $checkoutRequest = CheckoutRequest::factory()->create([+ 'requestable_type' => Consumable::class,+ 'requestable_id' => $consumable->id,+ ]);++ $url = $checkoutRequest->calendarUrl();++ $this->assertStringContainsString(route('consumables.checkout.show', ['consumablesID' => $consumable->id]), $url);+ $this->assertStringContainsString('request_id='.$checkoutRequest->id, $url);+ }++ public function test_calendar_url_points_at_license_checkout_with_request_id()+ {+ $license = License::factory()->create();+ $checkoutRequest = CheckoutRequest::factory()->create([+ 'requestable_type' => License::class,+ 'requestable_id' => $license->id,+ ]);++ $url = $checkoutRequest->calendarUrl();++ $this->assertStringContainsString(route('licenses.checkout', ['license' => $license->id]), $url);+ $this->assertStringContainsString('request_id='.$checkoutRequest->id, $url);+ }}