Skip to content

A free open source IT asset/license management system

Added bulk checkin

snipec9ce6808c1d7
diff --git a/app/Http/Controllers/Components/ComponentCheckinController.php b/app/Http/Controllers/Components/ComponentCheckinController.php
index bc6f400149..5160b449dc 100644
--- a/app/Http/Controllers/Components/ComponentCheckinController.php
+++ b/app/Http/Controllers/Components/ComponentCheckinController.php
@@ -15,6 +15,7 @@ use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
+use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Validator;
class ComponentCheckinController extends Controller
@@ -118,4 +119,62 @@ class ComponentCheckinController extends Controller
return redirect()->route('components.index')->with('error', trans('admin/components/message.does_not_exist'));
}
+
+ /**
+ * 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).
+ */
+ public function bulkCheckinSelected(Request $request): RedirectResponse
+ {
+ $pivotIds = array_filter((array) $request->input('ids', []));
+
+ if (empty($pivotIds)) {
+ return redirect()->back()
+ ->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();
+
+ $count = 0;
+ foreach ($assignments as $assignment) {
+ $component = $assignment->component;
+ if (!$component || !Gate::allows('checkin', $component)) {
+ continue;
+ }
+
+ // ComponentAssignment has no belongsTo asset relation
+ // (only a quirky `assets()` hasMany), so fetch the
+ // Asset directly by its pivot column. withTrashed so a
+ // soft-deleted asset doesn't break the checkin event.
+ $asset = Asset::withTrashed()->find($assignment->asset_id);
+
+ if ($assignment->delete()) {
+ event(new CheckoutableCheckedIn(
+ $component,
+ $asset,
+ auth()->user(),
+ null,
+ Carbon::now(),
+ ));
+ $count++;
+ }
+ }
+
+ return redirect()->back()
+ ->with('success', trans_choice('admin/components/message.bulk.checkin_selected.success', $count, ['count' => $count]));
+ }
}
diff --git a/app/Http/Controllers/Components/ComponentCheckoutController.php b/app/Http/Controllers/Components/ComponentCheckoutController.php
index 478d06944a..2723b63c75 100644
--- a/app/Http/Controllers/Components/ComponentCheckoutController.php
+++ b/app/Http/Controllers/Components/ComponentCheckoutController.php
@@ -40,6 +40,11 @@ class ComponentCheckoutController extends Controller
$this->authorize('checkout', $component);
+ if ($component->archived) {
+ return redirect()->route('components.index')
+ ->with('error', trans('admin/components/message.checkout.archived'));
+ }
+
// Make sure the category is valid
if ($component->category) {
@@ -107,6 +112,11 @@ class ComponentCheckoutController extends Controller
$this->authorize('checkout', $component);
+ if ($component->archived) {
+ return redirect()->route('components.index')
+ ->with('error', trans('admin/components/message.checkout.archived'));
+ }
+
$max_to_checkout = $component->numRemaining();
// Make sure there are at least the requested number of components available to checkout
diff --git a/app/Http/Controllers/Components/ComponentsController.php b/app/Http/Controllers/Components/ComponentsController.php
index cd33e5e6c7..c11e57fb75 100644
--- a/app/Http/Controllers/Components/ComponentsController.php
+++ b/app/Http/Controllers/Components/ComponentsController.php
@@ -97,6 +97,7 @@ class ComponentsController extends Controller
// absence to false so the flag really flips off. The setter
// normalizes the truthy branch.
$component->requestable = $request->input('requestable', false);
+ $component->archived = $request->input('archived', false);
// Seed the template supplier from the initial-acquisition
// supplier on the create form; editable afterwards.
$component->default_supplier_id = $request->input('default_supplier_id', $request->input('supplier_id'));
@@ -182,6 +183,7 @@ class ComponentsController extends Controller
// Unchecked checkbox is omitted from the POST body; coerce
// absence to false so unchecking really turns the flag off.
$component->requestable = $request->input('requestable', false);
+ $component->archived = $request->input('archived', false);
$component = $request->handleImages($component);