A free open source IT asset/license management system
Check for archived before bulk actions
snipe405b35c751a5
diff --git a/app/Http/Controllers/Accessories/AccessoriesController.php b/app/Http/Controllers/Accessories/AccessoriesController.phpindex 6a214c7d6d..1d90abd58f 100755--- a/app/Http/Controllers/Accessories/AccessoriesController.php+++ b/app/Http/Controllers/Accessories/AccessoriesController.php@@ -49,7 +49,8 @@ class AccessoriesController extends Controller$category_type = 'accessory';return view('accessories/edit')->with('category_type', $category_type)- ->with('item', new Accessory);+ ->with('item', new Accessory)+ ->with('pendingRequestCount', 0);}/**@@ -80,6 +81,7 @@ class AccessoriesController extends Controller$accessory->created_by = auth()->id();$accessory->notes = request('notes');$accessory->requestable = request('requestable', 0);+ $accessory->archived = request('archived', 0);// Seed the template supplier from the initial-acquisition// supplier on the create form so newly-created items already// have a "typical" supplier pre-populated. Editable afterwards.@@ -128,7 +130,10 @@ class AccessoriesController extends Controllersession()->put('url.intended', $safeReferer);}- return view('accessories.edit')->with('item', $accessory)->with('category_type', 'accessory');+ return view('accessories.edit')+ ->with('item', $accessory)+ ->with('category_type', 'accessory')+ ->with('pendingRequestCount', $accessory->openRequests()->count());}/**@@ -167,7 +172,8 @@ class AccessoriesController extends Controllerreturn view('accessories/edit')->with('cloned_model', $accessory_to_clone)- ->with('item', $cloned);+ ->with('item', $cloned)+ ->with('pendingRequestCount', 0);}@@ -208,6 +214,7 @@ class AccessoriesController extends Controller$accessory->default_supplier_id = request('default_supplier_id');$accessory->notes = request('notes');$accessory->requestable = request('requestable', 0);+ $accessory->archived = request('archived', 0);$accessory = $request->handleImages($accessory);@@ -252,6 +259,55 @@ class AccessoriesController extends Controllerreturn redirect()->route('accessories.index')->with('error', trans('admin/accessories/general.delete_disabled'));}+ /**+ * Restore a soft-deleted accessory. Mirrors ComponentsController::postRestore+ * (restoring is a delete-tier action across the project).+ */+ public function postRestore($id): RedirectResponse+ {+ if ($accessory = Accessory::withTrashed()->find($id)) {+ // Per-instance authorize so the policy's before() hook runs+ // its FMCS isCurrentUserHasAccess check against the actual+ // row, not just the class.+ $this->authorize('delete', $accessory);++ if ($accessory->deleted_at == '') {+ return redirect()->back()->with('error', trans('general.not_deleted', ['item_type' => trans('general.accessory')]));+ }++ if ($accessory->restore()) {+ return redirect()->route('accessories.show', $accessory)+ ->with('success', trans('admin/accessories/message.restore.success'));+ }++ return redirect()->back()->with('error', trans('general.could_not_restore', ['item_type' => trans('general.accessory'), 'error' => $accessory->getErrors()->first()]));+ }++ return redirect()->route('accessories.index')->with('error', trans('admin/accessories/message.does_not_exist'));+ }++ /**+ * Flip an accessory back to live inventory. Mirrors postRestore's+ * auth shape so unarchiving goes through the same per-instance+ * authorize path as restore from soft-delete.+ */+ public function postUnarchive(Accessory $accessory): RedirectResponse+ {+ $this->authorize('update', $accessory);++ if (! $accessory->archived) {+ return redirect()->back()->with('error', trans('admin/accessories/message.does_not_exist'));+ }++ $accessory->archived = false;++ if ($accessory->save()) {+ return redirect()->back()->with('success', trans('general.unarchive_success', ['item_type' => trans('general.accessory')]));+ }++ return redirect()->back()->withErrors($accessory->getErrors());+ }+/*** Returns a view that invokes the ajax table which contains* the content for the accessory detail view, which is generated in getDataView.diff --git a/app/Http/Controllers/Accessories/AccessoryCheckinController.php b/app/Http/Controllers/Accessories/AccessoryCheckinController.phpindex df8c899bc5..001e3e269a 100644--- a/app/Http/Controllers/Accessories/AccessoryCheckinController.php+++ b/app/Http/Controllers/Accessories/AccessoryCheckinController.php@@ -10,6 +10,9 @@ use App\Models\AccessoryCheckout;use Illuminate\Contracts\View\View;use Illuminate\Http\RedirectResponse;use Illuminate\Http\Request;+use Illuminate\Support\Carbon;+use Illuminate\Support\Facades\Gate;+use Illuminate\Support\Facades\URL;class AccessoryCheckinController extends Controller{@@ -90,4 +93,99 @@ class AccessoryCheckinController extends Controller// Redirect to the accessory management page with errorreturn redirect()->route('accessories.index')->with('error', trans('admin/accessories/message.checkin.error'));}++ /**+ * Render the bulk-checkin confirmation interstitial. The caller+ * ticks rows on the accessory show page's "Checked out" table and+ * posts their accessories_checkout ids here. The view lists the+ * selections and collects a single shared note that applies to+ * every row's checkin.+ */+ public function bulkCheckinShow(Request $request): View|RedirectResponse+ {+ $this->authorize('checkin', Accessory::class);++ $checkoutIds = array_filter((array) $request->input('ids', []));++ if (empty($checkoutIds)) {+ return redirect()->back()+ ->with('warning', trans('admin/accessories/message.bulk.checkin_selected.no_rows_selected'));+ }++ $checkouts = $this->scopedCheckoutsForBulkCheckin($checkoutIds);++ if ($checkouts->isEmpty()) {+ return redirect()->back()+ ->with('warning', trans('admin/accessories/message.bulk.checkin_selected.no_rows_selected'));+ }++ return view('accessories.bulk-checkin', [+ 'checkouts' => $checkouts,+ 'ids' => $checkouts->pluck('id')->all(),+ 'return_url' => Helper::sameOriginUrl(URL::previous()) ?? route('accessories.index'),+ ]);+ }++ /**+ * Apply the bulk checkin after the admin confirms on the+ * interstitial. The shared `note` from the form flows into every+ * CheckoutableCheckedIn event, so a single operator note lands on+ * each per-row action_log entry.+ */+ public function bulkCheckinStore(Request $request): RedirectResponse+ {+ $this->authorize('checkin', Accessory::class);++ $redirectTo = Helper::sameOriginUrl($request->input('return_url')) ?? route('accessories.index');++ $checkoutIds = array_filter((array) $request->input('ids', []));++ if (empty($checkoutIds)) {+ return redirect($redirectTo)+ ->with('warning', trans('admin/accessories/message.bulk.checkin_selected.no_rows_selected'));+ }++ $checkouts = $this->scopedCheckoutsForBulkCheckin($checkoutIds);+ $note = $request->input('note');++ $count = 0;+ foreach ($checkouts as $checkout) {+ $accessory = $checkout->accessory;+ if (! $accessory || ! Gate::allows('checkin', $accessory)) {+ continue;+ }++ if ($checkout->delete()) {+ event(new CheckoutableCheckedIn(+ $accessory,+ $checkout->assignedTo,+ auth()->user(),+ $note,+ Carbon::now(),+ ));+ $count++;+ }+ }++ return redirect($redirectTo)+ ->with('success', trans_choice('admin/accessories/message.bulk.checkin_selected.success', $count, ['count' => $count]));+ }++ /**+ * Defense in depth for FMCS / floater. AccessoryCheckout doesn't+ * carry CompanyableTrait, so a cross-company id POSTed by hand+ * would reach the loop and rely purely on the per-row Gate check.+ * Narrow the lookup to rows whose parent Accessory is reachable+ * via a subquery against Accessory's auto-applied CompanyableScope.+ *+ * @param array<int, mixed> $checkoutIds+ * @return \Illuminate\Database\Eloquent\Collection<int, AccessoryCheckout>+ */+ protected function scopedCheckoutsForBulkCheckin(array $checkoutIds)+ {+ return AccessoryCheckout::with('accessory')+ ->whereIn('id', $checkoutIds)+ ->whereIn('accessory_id', Accessory::query()->select('id'))+ ->get();+ }}diff --git a/app/Http/Controllers/Accessories/AccessoryCheckoutController.php b/app/Http/Controllers/Accessories/AccessoryCheckoutController.phpindex 5bad8d64d5..db5179c93f 100644--- a/app/Http/Controllers/Accessories/AccessoryCheckoutController.php+++ b/app/Http/Controllers/Accessories/AccessoryCheckoutController.php@@ -34,6 +34,11 @@ class AccessoryCheckoutController extends Controller$this->authorize('checkout', $accessory);+ if ($accessory->archived) {+ return redirect()->route('accessories.index')+ ->with('error', trans('general.cannot_checkout_archived', ['item_type' => trans('general.accessory')]));+ }+if ($accessory->category) {// Make sure there is at least one available to checkoutif ($accessory->numRemaining() <= 0) {@@ -78,6 +83,11 @@ class AccessoryCheckoutController extends Controller$this->authorize('checkout', $accessory);+ if ($accessory->archived) {+ return redirect()->route('accessories.index')+ ->with('error', trans('general.cannot_checkout_archived', ['item_type' => trans('general.accessory')]));+ }+$target = $this->determineCheckoutTarget();if (! $accessory->canCheckoutTo($target)) {diff --git a/app/Http/Controllers/Accessories/BulkAccessoriesController.php b/app/Http/Controllers/Accessories/BulkAccessoriesController.phpindex 17ebcdd954..596a7acdca 100644--- a/app/Http/Controllers/Accessories/BulkAccessoriesController.php+++ b/app/Http/Controllers/Accessories/BulkAccessoriesController.php@@ -4,11 +4,178 @@ namespace App\Http\Controllers\Accessories;use App\Http\Controllers\Controller;use App\Models\Accessory;+use App\Models\Company;+use Illuminate\Contracts\View\View;+use Illuminate\Http\RedirectResponse;use Illuminate\Http\Request;use Illuminate\Support\Facades\Gate;+use Illuminate\Validation\ValidationException;class BulkAccessoriesController extends Controller{+ /**+ * Fields admins may bulk-write across the selected rows. Mirrors+ * BulkComponentsController::EDITABLE_FIELDS. qty / supplier_id /+ * purchase_date / purchase_cost are quantity-adjustment territory+ * and stay out of bulk edit.+ *+ * @var array<int, string>+ */+ public const EDITABLE_FIELDS = [+ 'category_id',+ 'company_id',+ 'manufacturer_id',+ 'location_id',+ 'default_supplier_id',+ 'min_amt',+ 'requestable',+ 'archived',+ ];++ /**+ * Dispatcher. The index POSTs here with ids[] + bulk_actions, and+ * we fan out to the edit-form view, the delete-confirm view, or+ * the restore handler.+ */+ public function edit(Request $request): View|RedirectResponse+ {+ $this->authorize('view', Accessory::class);++ if (! $request->filled('ids')) {+ return redirect()->route('accessories.index')+ ->with('error', trans('admin/accessories/message.bulk.no_accessories_selected'));+ }++ // Restore acts on soft-deleted rows, so widen the lookup with+ // withTrashed for that branch only.+ $query = Accessory::whereIn('id', $request->input('ids'));+ if ($request->input('bulk_actions') === 'restore') {+ $query->withTrashed();+ }+ $accessories = $query->get();++ if ($accessories->isEmpty()) {+ return redirect()->route('accessories.index')+ ->with('error', trans('admin/accessories/message.bulk.no_accessories_selected'));+ }++ return match ($request->input('bulk_actions')) {+ 'edit' => $this->showEdit($accessories),+ 'delete' => $this->showDelete($accessories),+ 'restore' => $this->doRestore($accessories),+ default => redirect()->route('accessories.index')+ ->with('error', trans('admin/accessories/message.bulk.no_action_selected')),+ };+ }++ /**+ * Apply the bulk-edit form. Each editable field is written only+ * when the admin provided a value (empty fields are left alone),+ * so admins can tweak just the fields they care about without+ * flattening everything else to blank.+ */+ public function update(Request $request): RedirectResponse+ {+ $this->authorize('update', Accessory::class);++ if (! $request->filled('ids')) {+ return redirect()->route('accessories.index')+ ->with('error', trans('admin/accessories/message.bulk.no_accessories_selected'));+ }++ $accessories = Accessory::whereIn('id', $request->input('ids'))->get();++ if ($accessories->isEmpty()) {+ return redirect()->route('accessories.index')+ ->with('error', trans('admin/accessories/message.bulk.no_accessories_selected'));+ }++ $updateData = [];+ foreach (self::EDITABLE_FIELDS as $field) {+ if (! $request->has($field)) {+ continue;+ }++ $value = $request->input($field);++ // Treat blank selects / cleared text inputs as "don't touch".+ if ($value === '' || $value === null) {+ continue;+ }++ $updateData[$field] = $value;+ }++ // Route company_id through getIdForCurrentUser so a non-superuser+ // can't hand-craft a POST that writes a cross-tenant company_id+ // across the selection. Matches BulkComponentsController's shape.+ if (array_key_exists('company_id', $updateData)) {+ try {+ $updateData['company_id'] = Company::getIdForCurrentUser($updateData['company_id']);+ } catch (ValidationException $e) {+ return redirect()->route('accessories.index')+ ->withErrors($e->errors())+ ->with('error', trans('general.unauthorized'));+ }+ }++ // Opt-in null-outs via null_{field}=1 checkboxes.+ foreach (['category_id', 'company_id', 'manufacturer_id', 'location_id', 'default_supplier_id', 'min_amt'] as $field) {+ if ($request->boolean('null_'.$field)) {+ if ($field === 'category_id') {+ continue;+ }+ $updateData[$field] = null;+ }+ }++ if (empty($updateData)) {+ return redirect()->route('accessories.index')+ ->with('warning', trans('admin/accessories/message.bulk.no_fields_updated'));+ }++ $authorized = $accessories->filter(fn (Accessory $a) => Gate::allows('update', $a));+ $skipped = $accessories->count() - $authorized->count();++ $updated = 0;+ $failed_rows = [];+ foreach ($authorized as $accessory) {+ $accessory->fill($updateData);+ if ($accessory->save()) {+ $updated++;+ } else {+ $failed_rows[] = $accessory->name.': '.$accessory->getErrors()->first();+ }+ }++ $failed = count($failed_rows);+ $redirect = redirect()->route('accessories.index');++ if ($failed > 0) {+ $redirect->with('multi_error_messages', $failed_rows);+ }++ // All target rows rejected by validation. Don't dress a zero-row+ // update up as a success - the model errors tell the real story.+ if ($updated === 0 && $failed > 0) {+ return $redirect->with('error', trans_choice(+ 'admin/accessories/message.bulk.update.none_updated',+ $failed,+ ['count' => $failed]+ ));+ }++ $message = trans_choice('admin/accessories/message.bulk.update.success', $updated, ['count' => $updated]);++ if ($skipped > 0) {+ $message .= ' '.trans('admin/accessories/message.bulk.skipped_unauthorized', ['count' => $skipped]);+ }++ return $failed > 0+ ? $redirect->with('warning', $message)+ : $redirect->with('success', $message);+ }+public function destroy(Request $request){$this->authorize('delete', Accessory::class);@@ -18,9 +185,9 @@ class BulkAccessoriesController extends Controllerforeach ((array) $request->input('ids', []) as $id) {// Accessory uses CompanyableTrait, so Accessory::find() is- // filtered by CompanyableScope — cross-company rows return null- // and fall into the "does_not_exist" bucket without ever- // reaching authorization.+ // filtered by CompanyableScope. Cross-company rows return+ // null and fall into the "does_not_exist" bucket without+ // ever reaching authorization.$accessory = Accessory::find($id);if (is_null($accessory)) {$errors[] = trans('admin/accessories/message.does_not_exist', ['id' => $id]);@@ -70,4 +237,73 @@ class BulkAccessoriesController extends Controllerreturn redirect()->route('accessories.index')->with('success', trans_choice('admin/accessories/message.delete.bulk_success', $success_count, ['count' => $success_count]));}++ protected function showEdit($accessories): View+ {+ $this->authorize('update', Accessory::class);++ return view('accessories.bulk-edit', [+ 'accessories' => $accessories,+ 'ids' => $accessories->pluck('id')->all(),+ ]);+ }++ protected function showDelete($accessories): View+ {+ $this->authorize('delete', Accessory::class);++ // Load checkouts_count so the view's isDeletable() check can+ // distinguish "no active checkouts, safe to delete" from+ // "has checkouts, show X instead of a checkbox." Without this,+ // checkouts_count is null and isDeletable()'s === 0 check fails+ // for every row, so every row renders as undeletable.+ $accessories->loadCount('checkouts as checkouts_count');++ return view('accessories.bulk-delete', [+ 'accessories' => $accessories,+ 'ids' => $accessories->pluck('id')->all(),+ ]);+ }++ /**+ * Bulk-restore handler. Restore is non-destructive, so there's no+ * confirm view: iterate the selection, call ->restore(), redirect+ * back to the Deleted index with a summary. Per-row Gate::allows+ * `delete` keeps restore on the delete-tier auth bar (matches+ * postRestore).+ */+ protected function doRestore($accessories): RedirectResponse+ {+ $this->authorize('delete', Accessory::class);++ $restored = 0;+ $errors = [];++ foreach ($accessories as $accessory) {+ if (! Gate::allows('delete', $accessory)) {+ $errors[] = trans('general.unauthorized');++ continue;+ }++ if ($accessory->deleted_at === null) {+ // Already live. Skip silently.+ continue;+ }++ if ($accessory->restore()) {+ $restored++;+ }+ }++ $redirect = redirect()->route('accessories.index', ['status' => 'Deleted']);++ if (count($errors) > 0 && $restored === 0) {+ return $redirect->with('multi_error_messages', $errors);+ }++ return $redirect+ ->with('success', trans_choice('admin/accessories/message.bulk.restore.success', $restored, ['count' => $restored]))+ ->with('multi_error_messages', $errors ?: null);+ }}diff --git a/app/Http/Controllers/Components/BulkComponentsController.php b/app/Http/Controllers/Components/BulkComponentsController.phpindex 92147d6c36..56f625ee99 100644--- a/app/Http/Controllers/Components/BulkComponentsController.php+++ b/app/Http/Controllers/Components/BulkComponentsController.php@@ -148,21 +148,42 @@ class BulkComponentsController extends Controller$skipped = $components->count() - $authorized->count();$updated = 0;+ $failed_rows = [];foreach ($authorized as $component) {$component->fill($updateData);if ($component->save()) {$updated++;+ } else {+ $failed_rows[] = $component->name.': '.$component->getErrors()->first();}}+ $failed = count($failed_rows);+ $redirect = redirect()->route('components.index');++ if ($failed > 0) {+ $redirect->with('multi_error_messages', $failed_rows);+ }++ // All target rows rejected by validation. Don't dress a zero-row+ // update up as a success - the model errors tell the real story.+ if ($updated === 0 && $failed > 0) {+ return $redirect->with('error', trans_choice(+ 'admin/components/message.bulk.update.none_updated',+ $failed,+ ['count' => $failed]+ ));+ }+$message = trans_choice('admin/components/message.bulk.update.success', $updated, ['count' => $updated]);if ($skipped > 0) {- return redirect()->route('components.index')- ->with('warning', $message.' '.trans('admin/components/message.bulk.skipped_unauthorized', ['count' => $skipped]));+ $message .= ' '.trans('admin/components/message.bulk.skipped_unauthorized', ['count' => $skipped]);}- return redirect()->route('components.index')->with('success', $message);+ return $failed > 0+ ? $redirect->with('warning', $message)+ : $redirect->with('success', $message);}/**