A free open source IT asset/license management system
Bulk maintenances controller
snipea7d3a628ebf5
diff --git a/app/Http/Controllers/BulkMaintenancesController.php b/app/Http/Controllers/BulkMaintenancesController.phpnew file mode 100644index 0000000000..e7f483862b--- /dev/null+++ b/app/Http/Controllers/BulkMaintenancesController.php@@ -0,0 +1,108 @@+<?php++namespace App\Http\Controllers;++use App\Enums\ActionType;+use App\Models\Actionlog;+use App\Models\Asset;+use App\Models\Maintenance;+use Illuminate\Http\RedirectResponse;+use Illuminate\Http\Request;++/**+ * Handles bulk actions from the maintenances index table. The single POST+ * endpoint branches on the bulk_actions form field so the same dropdown+ * can grow additional actions later without a new route per verb.+ */+class BulkMaintenancesController extends Controller+{+ public function store(Request $request): RedirectResponse+ {+ // Top-level gate mirrors MaintenancesController::complete()/destroy() —+ // maintenance CRUD keys on the asset edit permission (see+ // MaintenancePolicy). This blocks users without any assets.edit rights+ // from hitting the endpoint at all; per-row policy still runs below.+ // FMCS company scoping is applied automatically on the Maintenance+ // lookups further down via the model's CompanyableChildTrait global+ // scope, so a scoped user can't reach maintenance rows for assets+ // outside their company set even if they submit those ids directly.+ $this->authorize('update', Asset::class);++ $action = $request->input('bulk_actions');+ $ids = array_values(array_unique(array_filter(array_map('intval', (array) $request->input('ids', [])))));++ if (empty($ids)) {+ return redirect()->route('maintenances.index')+ ->with('warning', trans('general.bulk_checkin_delete.nothing_selected', ['object_type' => trans('general.maintenances')]));+ }++ return match ($action) {+ 'delete' => $this->bulkDelete($ids),+ 'complete' => $this->bulkComplete($ids),+ default => redirect()->route('maintenances.index')->with('error', trans('general.something_went_wrong')),+ };+ }++ /**+ * Soft-delete the selected maintenance records after per-row policy check.+ * Silently skips already-deleted rows and any the actor can't touch.+ */+ private function bulkDelete(array $ids): RedirectResponse+ {+ $success = 0;+ $skipped = 0;++ Maintenance::whereIn('id', $ids)->with('asset')->get()->each(function (Maintenance $maintenance) use (&$success, &$skipped) {+ if (! auth()->user()->can('delete', $maintenance)) {+ $skipped++;++ return;+ }+ $maintenance->delete();+ $success++;+ });++ return redirect()->route('maintenances.index')->with(+ $success > 0 ? 'success' : 'warning',+ trans_choice('admin/maintenances/message.bulk_delete', $success, ['count' => $success, 'skipped' => $skipped])+ );+ }++ /**+ * Mark the selected maintenance records complete. Skips rows that are+ * already completed (idempotent) and any the actor can't update.+ */+ private function bulkComplete(array $ids): RedirectResponse+ {+ $success = 0;+ $skipped = 0;++ Maintenance::whereIn('id', $ids)->whereNull('completed_at')->with('asset')->get()->each(function (Maintenance $maintenance) use (&$success, &$skipped) {+ if (! auth()->user()->can('update', $maintenance)) {+ $skipped++;++ return;+ }++ $maintenance->completed_at = now();+ $maintenance->completed_by = auth()->id();+ $maintenance->asset_maintenance_time = (int) $maintenance->created_at->diffInDays(now(), true);+ $maintenance->saveQuietly();++ $log = new Actionlog;+ $log->item_type = Maintenance::class;+ $log->item_id = $maintenance->id;+ $log->target_type = Asset::class;+ $log->target_id = $maintenance->asset_id;+ $log->created_by = auth()->id();+ $log->logaction(ActionType::MaintenanceComplete);++ $success++;+ });++ return redirect()->route('maintenances.index')->with(+ $success > 0 ? 'success' : 'warning',+ trans_choice('admin/maintenances/message.bulk_complete', $success, ['count' => $success, 'skipped' => $skipped])+ );+ }+}