Skip to content

A free open source IT asset/license management system

Updated controllers for interstitial

snipee3ae6c10cd6e
diff --git a/app/Http/Controllers/Api/AccessoriesController.php b/app/Http/Controllers/Api/AccessoriesController.php
index 9b01c48af7..441e108191 100644
--- a/app/Http/Controllers/Api/AccessoriesController.php
+++ b/app/Http/Controllers/Api/AccessoriesController.php
@@ -81,6 +81,19 @@ class AccessoriesController extends Controller
->with('category', 'company', 'manufacturer', 'checkouts', 'location', 'defaultSupplier', 'adminuser', 'orderItems.order.supplier')
->withCount('checkouts as checkouts_count');
+ // Archived / soft-deleted scope.
+ switch ($request->input('status')) {
+ case 'Archived':
+ $accessories->archived();
+ break;
+ case 'Deleted':
+ $accessories->onlyTrashed();
+ break;
+ default:
+ $accessories->accessoriesForShow();
+ break;
+ }
+
// This invokes the Searchable model trait scopeTextSearch and will handle input by search or by advanced search filter
if ($request->filled('filter') || $request->filled('search')) {
$accessories->TextSearch($request->input('filter') ? $request->input('filter') : $request->input('search'));
diff --git a/app/Http/Controllers/Components/ComponentCheckinController.php b/app/Http/Controllers/Components/ComponentCheckinController.php
index 23fefc28a2..e8a8f63980 100644
--- a/app/Http/Controllers/Components/ComponentCheckinController.php
+++ b/app/Http/Controllers/Components/ComponentCheckinController.php
@@ -16,6 +16,7 @@ use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Gate;
+use Illuminate\Support\Facades\URL;
use Illuminate\Support\Facades\Validator;
class ComponentCheckinController extends Controller
@@ -121,15 +122,14 @@ class ComponentCheckinController extends Controller
}
/**
- * Bulk check-in a list of component pivot rows. The caller
- * ticks rows on the component show page's "Assigned" table and
- * posts their components_assets ids here. Each pivot row is
- * fully checked back in (not partial-qty like the single-row
- * form).
+ * Render the bulk-checkin interstitial for component pivot rows.
+ * The caller ticks rows on the component show page's "Assigned"
+ * table and posts their components_assets ids here. The view lists
+ * the selections and collects a single shared note applied to
+ * every row's checkin.
*/
- public function bulkCheckinSelected(Request $request): RedirectResponse
+ public function bulkCheckinShow(Request $request): View|RedirectResponse
{
-
$this->authorize('checkin', Component::class);
$pivotIds = array_filter((array) $request->input('ids', []));
@@ -139,18 +139,50 @@ class ComponentCheckinController extends Controller
->with('warning', trans('admin/components/message.bulk.checkin_selected.no_rows_selected'));
}
- // Defense in depth for FMCS / floater. ComponentAssignment
- // doesn't carry CompanyableTrait, so a cross-company pivot id
- // POSTed by hand would reach the loop and rely purely on the
- // per-row Gate::allows('checkin') result. Narrow the lookup to
- // pivots whose parent Component is reachable by this caller,
- // via a subquery against the Component model's auto-applied
- // CompanyableScope. The eager-loaded `component` relation is
- // still used for the auth check and event payload below.
- $assignments = ComponentAssignment::with('component')
- ->whereIn('id', $pivotIds)
- ->whereIn('component_id', Component::query()->select('id'))
- ->get();
+ $assignments = $this->scopedAssignmentsForBulkCheckin($pivotIds);
+
+ if ($assignments->isEmpty()) {
+ return redirect()->back()
+ ->with('warning', trans('admin/components/message.bulk.checkin_selected.no_rows_selected'));
+ }
+
+ // ComponentAssignment's `assets()` is a hasMany that returns at
+ // most one row (asset_id → Asset.id). Resolve the single Asset
+ // up front so the view doesn't need an inline lookup.
+ $assetIds = $assignments->pluck('asset_id')->filter()->unique()->all();
+ $assetsById = Asset::withTrashed()->whereIn('id', $assetIds)->get()->keyBy('id');
+ foreach ($assignments as $assignment) {
+ $assignment->setRelation('resolvedAsset', $assetsById->get($assignment->asset_id));
+ }
+
+ return view('components.bulk-checkin', [
+ 'assignments' => $assignments,
+ 'ids' => $assignments->pluck('id')->all(),
+ 'return_url' => Helper::sameOriginUrl(URL::previous()) ?? route('components.index'),
+ ]);
+ }
+
+ /**
+ * Apply the bulk checkin after the admin confirms on the
+ * interstitial. Each pivot row is fully checked back in (not
+ * partial-qty like the single-row form). The shared `note` from
+ * the form rides every CheckoutableCheckedIn event.
+ */
+ public function bulkCheckinStore(Request $request): RedirectResponse
+ {
+ $this->authorize('checkin', Component::class);
+
+ $redirectTo = Helper::sameOriginUrl($request->input('return_url')) ?? route('components.index');
+
+ $pivotIds = array_filter((array) $request->input('ids', []));
+
+ if (empty($pivotIds)) {
+ return redirect($redirectTo)
+ ->with('warning', trans('admin/components/message.bulk.checkin_selected.no_rows_selected'));
+ }
+
+ $assignments = $this->scopedAssignmentsForBulkCheckin($pivotIds);
+ $note = $request->input('note');
// Count returned ROWS and total UNITS separately. Each pivot
// carries an assigned_qty, so "3 rows checked in" might really
@@ -161,7 +193,7 @@ class ComponentCheckinController extends Controller
$unitCount = 0;
foreach ($assignments as $assignment) {
$component = $assignment->component;
- if (!$component || !Gate::allows('checkin', $component)) {
+ if (! $component || ! Gate::allows('checkin', $component)) {
continue;
}
@@ -177,7 +209,7 @@ class ComponentCheckinController extends Controller
$component,
$asset,
auth()->user(),
- null,
+ $note,
Carbon::now(),
));
$rowCount++;
@@ -185,10 +217,29 @@ class ComponentCheckinController extends Controller
}
}
- return redirect()->back()
+ return redirect($redirectTo)
->with('success', trans_choice('admin/components/message.bulk.checkin_selected.success', $rowCount, [
'count' => $rowCount,
'unit_count' => $unitCount,
]));
}
+
+ /**
+ * Defense in depth for FMCS / floater. ComponentAssignment doesn't
+ * carry CompanyableTrait, so a cross-company pivot id POSTed by
+ * hand would reach the loop and rely purely on the per-row Gate
+ * check. Narrow the lookup to pivots whose parent Component is
+ * reachable via a subquery against Component's auto-applied
+ * CompanyableScope.
+ *
+ * @param array<int, mixed> $pivotIds
+ * @return \Illuminate\Database\Eloquent\Collection<int, ComponentAssignment>
+ */
+ protected function scopedAssignmentsForBulkCheckin(array $pivotIds)
+ {
+ return ComponentAssignment::with('component')
+ ->whereIn('id', $pivotIds)
+ ->whereIn('component_id', Component::query()->select('id'))
+ ->get();
+ }
}
diff --git a/app/Http/Controllers/Components/ComponentCheckoutController.php b/app/Http/Controllers/Components/ComponentCheckoutController.php
index dbed2a556d..6ac065f1dd 100644
--- a/app/Http/Controllers/Components/ComponentCheckoutController.php
+++ b/app/Http/Controllers/Components/ComponentCheckoutController.php
@@ -41,7 +41,7 @@ class ComponentCheckoutController extends Controller
if ($component->archived) {
return redirect()->route('components.index')
- ->with('error', trans('admin/components/message.checkout.archived'));
+ ->with('error', trans('general.cannot_checkout_archived', ['item_type' => trans('general.component')]));
}
// Make sure the category is valid
@@ -113,7 +113,7 @@ class ComponentCheckoutController extends Controller
if ($component->archived) {
return redirect()->route('components.index')
- ->with('error', trans('admin/components/message.checkout.archived'));
+ ->with('error', trans('general.cannot_checkout_archived', ['item_type' => trans('general.component')]));
}
$max_to_checkout = $component->numRemaining();
diff --git a/app/Http/Controllers/Licenses/LicenseCheckinController.php b/app/Http/Controllers/Licenses/LicenseCheckinController.php
index b123e40684..652e28a6c0 100644
--- a/app/Http/Controllers/Licenses/LicenseCheckinController.php
+++ b/app/Http/Controllers/Licenses/LicenseCheckinController.php
@@ -15,6 +15,7 @@ use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Log;
+use Illuminate\Support\Facades\URL;
use Illuminate\Support\Facades\Validator;
class LicenseCheckinController extends Controller
@@ -124,16 +125,11 @@ class LicenseCheckinController extends Controller
}
/**
- * Bulk checkin all license seats
- *
- * @author [A. Gianotto] [<[email protected]>]
- *
- * @see LicenseCheckinController::create() method that provides the form view
- * @since [v6.1.1]
+ * Render the bulk-checkin confirmation interstitial
*
* @throws AuthorizationException
*/
- public function bulkCheckinSelected(Request $request): RedirectResponse
+ public function bulkCheckinShow(Request $request): View|RedirectResponse
{
$this->authorize('checkin', License::class);
@@ -143,12 +139,45 @@ class LicenseCheckinController extends Controller
return redirect()->back()->with('warning', trans('admin/licenses/general.bulk.checkin_selected.no_seats_selected'));
}
- $seats = LicenseSeat::whereIn('id', $seatIds)
- ->where(function ($query) {
- $query->whereNotNull('assigned_to')->orWhereNotNull('asset_id');
- })
- ->with('license', 'user', 'asset')
- ->get();
+ $seats = $this->scopedSeatsForBulkCheckin($seatIds);
+
+ if ($seats->isEmpty()) {
+ return redirect()->back()->with('warning', trans('admin/licenses/general.bulk.checkin_selected.no_seats_selected'));
+ }
+
+ return view('licenses.bulk-checkin', [
+ 'seats' => $seats,
+ 'ids' => $seats->pluck('id')->all(),
+ 'return_url' => Helper::sameOriginUrl(URL::previous()) ?? route('licenses.index'),
+ ]);
+ }
+
+ /**
+ * Apply the bulk checkin after the admin confirms on the
+ * interstitial. The shared `note` from the form rides every
+ * CheckoutableCheckedIn event.
+ *
+ * @throws AuthorizationException
+ */
+ public function bulkCheckinStore(Request $request): RedirectResponse
+ {
+ $this->authorize('checkin', License::class);
+
+ $redirectTo = Helper::sameOriginUrl($request->input('return_url')) ?? route('licenses.index');
+
+ $seatIds = $request->input('ids', []);
+
+ if (empty($seatIds)) {
+ return redirect($redirectTo)->with('warning', trans('admin/licenses/general.bulk.checkin_selected.no_seats_selected'));
+ }
+
+ $seats = $this->scopedSeatsForBulkCheckin($seatIds);
+
+ if ($seats->isEmpty()) {
+ return redirect($redirectTo)->with('warning', trans('admin/licenses/general.bulk.checkin_selected.no_seats_selected'));
+ }
+
+ $note = $request->input('note');
$count = 0;
foreach ($seats as $seat) {
@@ -162,12 +191,31 @@ class LicenseCheckinController extends Controller
$seat->unreassignable_seat = true;
}
if ($seat->save()) {
- event(new CheckoutableCheckedIn($seat, $target, auth()->user(), null));
+ event(new CheckoutableCheckedIn($seat, $target, auth()->user(), $note));
$count++;
}
}
- return redirect()->back()->with('success', trans_choice('admin/licenses/general.bulk.checkin_selected.success', $count, ['count' => $count]));
+ return redirect($redirectTo)->with('success', trans_choice('admin/licenses/general.bulk.checkin_selected.success', $count, ['count' => $count]));
+ }
+
+ /**
+ * Narrow the lookup to seats actually assigned to someone or
+ * something. Unassigned seats posted via a stale selection just
+ * get skipped. The eager-loaded license + user + asset relations
+ * are reused for the auth check, event target, and interstitial.
+ *
+ * @param array<int, mixed> $seatIds
+ * @return \Illuminate\Database\Eloquent\Collection<int, LicenseSeat>
+ */
+ protected function scopedSeatsForBulkCheckin(array $seatIds)
+ {
+ return LicenseSeat::whereIn('id', $seatIds)
+ ->where(function ($query) {
+ $query->whereNotNull('assigned_to')->orWhereNotNull('asset_id');
+ })
+ ->with('license', 'user', 'asset')
+ ->get();
}
public function bulkCheckin(Request $request, $licenseId)