Skip to content

A free open source IT asset/license management system

Save new requestable checkbox

snipe0f7295648351
diff --git a/app/Http/Controllers/Components/ComponentsController.php b/app/Http/Controllers/Components/ComponentsController.php
index 2bafa01851..480ec4340c 100644
--- a/app/Http/Controllers/Components/ComponentsController.php
+++ b/app/Http/Controllers/Components/ComponentsController.php
@@ -93,6 +93,10 @@ class ComponentsController extends Controller
$component->qty = $request->input('qty');
$component->created_by = auth()->id();
$component->notes = $request->input('notes');
+ // Unchecked checkboxes are omitted from the POST body; coerce
+ // absence to false so the flag really flips off. The setter
+ // normalizes the truthy branch.
+ $component->requestable = $request->input('requestable', 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'));
@@ -177,6 +181,9 @@ class ComponentsController extends Controller
// parent's "typical supplier" template.
$component->default_supplier_id = $request->input('default_supplier_id');
$component->notes = $request->input('notes');
+ // 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 = $request->handleImages($component);
diff --git a/app/Http/Controllers/Consumables/ConsumableCheckoutController.php b/app/Http/Controllers/Consumables/ConsumableCheckoutController.php
index f532f0dd05..26879ba0b4 100644
--- a/app/Http/Controllers/Consumables/ConsumableCheckoutController.php
+++ b/app/Http/Controllers/Consumables/ConsumableCheckoutController.php
@@ -7,6 +7,7 @@ use App\Events\CheckoutableCheckedOut;
use App\Helpers\Helper;
use App\Http\Controllers\Controller;
use App\Models\CheckoutAcceptance;
+use App\Models\CheckoutRequest;
use App\Models\Consumable;
use App\Models\User;
use Illuminate\Auth\Access\AuthorizationException;
@@ -27,7 +28,7 @@ class ConsumableCheckoutController extends Controller
*
* @param int $id
*/
- public function create($id): View|RedirectResponse
+ public function create(Request $request, $id): View|RedirectResponse
{
if ($consumable = Consumable::find($id)) {
@@ -43,8 +44,21 @@ class ConsumableCheckoutController extends Controller
->with('error', trans('admin/consumables/message.checkout.unavailable', ['requested' => 1, 'remaining' => $consumable->numRemaining()]));
}
- // Return the checkout view
- return view('consumables/checkout', compact('consumable'));
+ // Optional ?request_id hint. Present when the admin
+ // reached this screen from a /requests row. Drives
+ // the side-panel context box (who asked + waiting
+ // list). CheckoutRequest::contextForCheckout handles
+ // the URL-twiddle guards; a miss returns nulls /
+ // empty so the panel renders nothing.
+ $context = CheckoutRequest::contextForCheckout(
+ $request->integer('request_id') ?: null,
+ Consumable::class,
+ $consumable->id,
+ );
+
+ return view('consumables/checkout', compact('consumable'))
+ ->with('checkoutRequest', $context['checkoutRequest'])
+ ->with('otherPendingRequests', $context['otherPendingRequests']);
}
// Invalid category
@@ -193,4 +207,170 @@ class ConsumableCheckoutController extends Controller
return Helper::getRedirectOption($request, $consumable->id, 'Consumables')
->with('success', trans('admin/consumables/message.checkout.success'));
}
+
+ /**
+ * Bulk-fulfill screen. Mirrors AccessoryCheckoutController::
+ * bulkFulfillCreate - see there for the design rationale.
+ */
+ public function bulkFulfillCreate(Consumable $consumable)
+ {
+ $this->authorize('checkout', $consumable);
+
+ if ($consumable->numRemaining() <= 0) {
+ return redirect()->route('consumables.show', $consumable)
+ ->with('error', trans('admin/consumables/message.checkout.unavailable', [
+ 'requested' => 1,
+ 'remaining' => $consumable->numRemaining(),
+ ]));
+ }
+
+ $pendingRequests = CheckoutRequest::pending()
+ ->where('requestable_type', Consumable::class)
+ ->where('requestable_id', $consumable->id)
+ ->with('user')
+ ->orderBy('created_at')
+ ->orderBy('id')
+ ->get()
+ ->filter(fn (CheckoutRequest $r) => $r->user !== null)
+ ->values();
+
+ if ($pendingRequests->isEmpty()) {
+ return redirect()->route('consumables.show', $consumable)
+ ->with('info', trans('admin/hardware/message.requests.no_active'));
+ }
+
+ return view('checkouts/fulfill-multiple', [
+ 'item' => $consumable,
+ 'pendingRequests' => $pendingRequests,
+ 'formRoute' => route('consumables.fulfill-requests.store', $consumable),
+ 'remaining' => (int) $consumable->numRemaining(),
+ ]);
+ }
+
+ /**
+ * Iterate + fulfill. See AccessoryCheckoutController::
+ * bulkFulfillStore for the shared per-row/partial-success
+ * pattern.
+ */
+ public function bulkFulfillStore(Request $request, Consumable $consumable): RedirectResponse
+ {
+ $this->authorize('checkout', $consumable);
+
+ $enabledIds = collect((array) $request->input('enabled_requests', []))
+ ->map(fn ($id) => (int) $id)
+ ->filter()
+ ->unique()
+ ->values();
+
+ if ($enabledIds->isEmpty()) {
+ return redirect()->route('consumables.fulfill-requests.create', $consumable)
+ ->with('error', trans('admin/hardware/message.requests.no_selection'));
+ }
+
+ $qtyInputs = (array) $request->input('qty', []);
+ $userInputs = (array) $request->input('user_id', []);
+ $noteInputs = (array) $request->input('notes', []);
+
+ $requests = CheckoutRequest::pending()
+ ->where('requestable_type', Consumable::class)
+ ->where('requestable_id', $consumable->id)
+ ->whereIn('id', $enabledIds)
+ ->with('user')
+ ->get()
+ ->keyBy('id');
+
+ $adminUser = auth()->user();
+ $fulfilled = 0;
+ $errors = [];
+
+ foreach ($enabledIds as $requestId) {
+ /** @var CheckoutRequest|null $checkoutRequest */
+ $checkoutRequest = $requests->get($requestId);
+ if (! $checkoutRequest) {
+ $errors[] = trans('admin/hardware/message.requests.row_stale', ['id' => $requestId]);
+
+ continue;
+ }
+
+ $targetUserId = (int) ($userInputs[$requestId] ?? $checkoutRequest->user_id);
+ $qty = (int) ($qtyInputs[$requestId] ?? $checkoutRequest->quantity);
+ $note = $noteInputs[$requestId] ?? $checkoutRequest->notes;
+
+ if ($qty < 1) {
+ $errors[] = trans('admin/hardware/message.requests.row_qty_invalid', ['id' => $requestId]);
+
+ continue;
+ }
+
+ $targetUser = User::find($targetUserId);
+ if (! $targetUser) {
+ $errors[] = trans('admin/hardware/message.requests.row_user_missing', ['id' => $requestId]);
+
+ continue;
+ }
+
+ if (! $consumable->canCheckoutTo($targetUser)) {
+ $errors[] = trans('admin/hardware/message.requests.row_company_mismatch', [
+ 'id' => $requestId,
+ 'user' => $targetUser->display_name,
+ ]);
+
+ continue;
+ }
+
+ $overAllocated = false;
+
+ DB::transaction(function () use ($consumable, $targetUser, $qty, $note, $adminUser, &$overAllocated): void {
+ $locked = Consumable::whereKey($consumable->id)->lockForUpdate()->first();
+
+ if (! $locked || $locked->numRemaining() < $qty) {
+ $overAllocated = true;
+
+ return;
+ }
+
+ for ($i = 0; $i < $qty; $i++) {
+ $consumable->users()->attach($consumable->id, [
+ 'consumable_id' => $consumable->id,
+ 'created_by' => $adminUser->id,
+ 'assigned_to' => $targetUser->id,
+ 'note' => $note,
+ ]);
+ }
+ });
+
+ if ($overAllocated) {
+ $errors[] = trans('admin/hardware/message.requests.row_over_allocated', [
+ 'id' => $requestId,
+ ]);
+
+ continue;
+ }
+
+ try {
+ event(new CheckoutableCheckedOut(
+ $consumable,
+ $targetUser,
+ $adminUser,
+ $note,
+ [],
+ $qty,
+ false,
+ ));
+ } catch (\Throwable $e) {
+ \Illuminate\Support\Facades\Log::warning('Bulk-fulfill event dispatch failed for request '.$requestId.': '.$e->getMessage());
+ }
+
+ $fulfilled++;
+ }
+
+ $summary = trans('admin/hardware/message.requests.bulk_summary', [
+ 'fulfilled' => $fulfilled,
+ 'total' => $enabledIds->count(),
+ ]);
+
+ return redirect()->route('requests.index')
+ ->with($fulfilled > 0 ? 'success' : 'warning', $summary)
+ ->with('multi_error_messages', $errors);
+ }
}
diff --git a/app/Http/Controllers/Consumables/ConsumablesController.php b/app/Http/Controllers/Consumables/ConsumablesController.php
index ed43e655d7..28f0eb6096 100644
--- a/app/Http/Controllers/Consumables/ConsumablesController.php
+++ b/app/Http/Controllers/Consumables/ConsumablesController.php
@@ -94,6 +94,10 @@ class ConsumablesController extends Controller
$consumable->qty = $request->input('qty');
$consumable->created_by = auth()->id();
$consumable->notes = $request->input('notes');
+ // Unchecked checkboxes are omitted from the POST body; coerce
+ // absence to false so the flag really flips off. The setter
+ // normalizes the truthy branch.
+ $consumable->requestable = $request->input('requestable', false);
// Seed the template supplier from the initial-acquisition
// supplier on the create form; editable afterwards.
$consumable->default_supplier_id = $request->input('default_supplier_id', $request->input('supplier_id'));
@@ -187,6 +191,9 @@ class ConsumablesController extends Controller
// controller for the parent-as-template rationale.
$consumable->default_supplier_id = $request->input('default_supplier_id');
$consumable->notes = $request->input('notes');
+ // Unchecked checkbox is omitted from the POST body; coerce
+ // absence to false so unchecking really turns the flag off.
+ $consumable->requestable = $request->input('requestable', false);
$consumable = $request->handleImages($consumable);
diff --git a/app/Http/Controllers/Licenses/LicenseCheckoutController.php b/app/Http/Controllers/Licenses/LicenseCheckoutController.php
index 29abb0c26c..27793500a3 100644
--- a/app/Http/Controllers/Licenses/LicenseCheckoutController.php
+++ b/app/Http/Controllers/Licenses/LicenseCheckoutController.php
@@ -9,6 +9,7 @@ use App\Http\Controllers\Controller;
use App\Http\Requests\LicenseCheckoutRequest;
use App\Models\Asset;
use App\Models\CheckoutAcceptance;
+use App\Models\CheckoutRequest;
use App\Models\License;
use App\Models\LicenseSeat;
use App\Models\Setting;
@@ -17,6 +18,7 @@ use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Contracts\View\View;
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Http\RedirectResponse;
+use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
@@ -37,7 +39,7 @@ class LicenseCheckoutController extends Controller
*
* @throws AuthorizationException
*/
- public function create(License $license)
+ public function create(Request $request, License $license)
{
$this->authorize('checkout', $license);
@@ -58,8 +60,21 @@ class LicenseCheckoutController extends Controller
session()->put(['checkout_to_type' => 'user']);
}
- // Return the checkout view
- return view('licenses/checkout', compact('license'));
+ // Optional ?request_id hint. Present when the admin
+ // reached this screen from a /requests row. Drives the
+ // side-panel context box (who asked + waiting list).
+ // CheckoutRequest::contextForCheckout handles the URL-
+ // twiddle guards; a miss returns nulls / empty so the
+ // panel renders nothing.
+ $context = CheckoutRequest::contextForCheckout(
+ $request->integer('request_id') ?: null,
+ License::class,
+ $license->id,
+ );
+
+ return view('licenses/checkout', compact('license'))
+ ->with('checkoutRequest', $context['checkoutRequest'])
+ ->with('otherPendingRequests', $context['otherPendingRequests']);
}
// Invalid category
@@ -347,4 +362,187 @@ class LicenseCheckoutController extends Controller
return redirect()->back()->with('success', trans_choice('admin/licenses/general.bulk.checkout_all.success', 2, ['count' => $assigned_count]));
}
+
+ /**
+ * Bulk-fulfill screen for a license's pending-request queue.
+ * Mirrors AccessoryCheckoutController::bulkFulfillCreate - see
+ * there for the design rationale. Qty on a license request is
+ * the number of seats the requester asked for; the per-row
+ * fulfillment claims that many free seats via freeSeat(lock:true)
+ * in a loop.
+ */
+ public function bulkFulfillCreate(License $license)
+ {
+ $this->authorize('checkout', $license);
+
+ if ($license->availCount()->count() < 1) {
+ return redirect()->route('licenses.show', $license)
+ ->with('error', trans('admin/licenses/message.checkout.not_enough_seats'));
+ }
+
+ if ($license->isInactive()) {
+ return redirect()->route('licenses.show', $license)
+ ->with('error', trans('admin/licenses/message.checkout.license_is_inactive'));
+ }
+
+ $pendingRequests = CheckoutRequest::pending()
+ ->where('requestable_type', License::class)
+ ->where('requestable_id', $license->id)
+ ->with('user')
+ ->orderBy('created_at')
+ ->orderBy('id')
+ ->get()
+ ->filter(fn (CheckoutRequest $r) => $r->user !== null)
+ ->values();
+
+ if ($pendingRequests->isEmpty()) {
+ return redirect()->route('licenses.show', $license)
+ ->with('info', trans('admin/hardware/message.requests.no_active'));
+ }
+
+ return view('checkouts/fulfill-multiple', [
+ 'item' => $license,
+ 'pendingRequests' => $pendingRequests,
+ 'formRoute' => route('licenses.fulfill-requests.store', $license),
+ 'remaining' => (int) $license->availCount()->count(),
+ // License requests are one-seat-per-requester by
+ // convention (nobody realistically asks for 3 Photoshop
+ // seats for themselves), so hide the qty input on each
+ // row. The recipient-row component still emits a hidden
+ // qty=1 field so the controller's per-row shape stays
+ // uniform across every type.
+ 'hideQty' => true,
+ ]);
+ }
+
+ /**
+ * Iterate the ticked rows. For each, claim `qty` free seats
+ * via freeSeat(lock: true) in its own transaction (partial-
+ * success semantics matching the existing license bulk-
+ * checkout-to-all-users pattern above). Fires CheckoutableCheckedOut
+ * per seat so the state-machine listener closes the request.
+ */
+ public function bulkFulfillStore(Request $request, License $license): RedirectResponse
+ {
+ $this->authorize('checkout', $license);
+
+ $enabledIds = collect((array) $request->input('enabled_requests', []))
+ ->map(fn ($id) => (int) $id)
+ ->filter()
+ ->unique()
+ ->values();
+
+ if ($enabledIds->isEmpty()) {
+ return redirect()->route('licenses.fulfill-requests.create', $license)
+ ->with('error', trans('admin/hardware/message.requests.no_selection'));
+ }
+
+ $qtyInputs = (array) $request->input('qty', []);
+ $userInputs = (array) $request->input('user_id', []);
+ $noteInputs = (array) $request->input('notes', []);
+
+ $requests = CheckoutRequest::pending()
+ ->where('requestable_type', License::class)
+ ->where('requestable_id', $license->id)
+ ->whereIn('id', $enabledIds)
+ ->with('user')
+ ->get()
+ ->keyBy('id');
+
+ $adminUser = auth()->user();
+ $fulfilledSeats = 0;
+ $fulfilledRows = 0;
+ $errors = [];
+
+ foreach ($enabledIds as $requestId) {
+ /** @var CheckoutRequest|null $checkoutRequest */
+ $checkoutRequest = $requests->get($requestId);
+ if (! $checkoutRequest) {
+ $errors[] = trans('admin/hardware/message.requests.row_stale', ['id' => $requestId]);
+
+ continue;
+ }
+
+ $targetUserId = (int) ($userInputs[$requestId] ?? $checkoutRequest->user_id);
+ // License requests are one-seat-per-requester by
+ // convention (see bulkFulfillCreate for the rationale).
+ // Server-side pin to 1 regardless of what the form
+ // sends so a hand-crafted POST can't over-allocate.
+ $qty = 1;
+ $note = $noteInputs[$requestId] ?? $checkoutRequest->notes;
+
+ $targetUser = User::find($targetUserId);
+ if (! $targetUser) {
+ $errors[] = trans('admin/hardware/message.requests.row_user_missing', ['id' => $requestId]);
+
+ continue;
+ }
+
+ if (! $license->canCheckoutTo($targetUser)) {
+ $errors[] = trans('admin/hardware/message.requests.row_company_mismatch', [
+ 'id' => $requestId,
+ 'user' => $targetUser->display_name,
+ ]);
+
+ continue;
+ }
+
+ $claimed = 0;
+
+ for ($i = 0; $i < $qty; $i++) {
+ $seatClaimed = DB::transaction(function () use ($license, $targetUser, $note): ?LicenseSeat {
+ $seat = $license->freeSeat(lock: true);
+ if (! $seat) {
+ return null;
+ }
+ $seat->assigned_to = $targetUser->id;
+ $seat->created_by = auth()->id();
+ $seat->notes = $note;
+ if (! $seat->save()) {
+ return null;
+ }
+
+ return $seat;
+ });
+
+ if (! $seatClaimed) {
+ break;
+ }
+
+ try {
+ event(new CheckoutableCheckedOut(
+ $seatClaimed,
+ $targetUser,
+ $adminUser,
+ $note,
+ [],
+ 1,
+ false,
+ ));
+ } catch (\Throwable $e) {
+ Log::warning('Bulk-fulfill event dispatch failed for request '.$requestId.' seat '.$seatClaimed->id.': '.$e->getMessage());
+ }
+
+ $claimed++;
+ $fulfilledSeats++;
+ }
+
+ if ($claimed === 0) {
+ $errors[] = trans('admin/hardware/message.requests.row_over_allocated', ['id' => $requestId]);
+
+ continue;
+ }
+
+ $fulfilledRows++;
+ }
+
+ $summary = trans('admin/hardware/message.requests.bulk_summary', [
+ 'fulfilled' => $fulfilledRows,
+ 'total' => $enabledIds->count(),
+ ]);
+
+ return redirect()->route('requests.index')
+ ->with($fulfilledRows > 0 ? 'success' : 'warning', $summary)
+ ->with('multi_error_messages', $errors);
+ }
}
diff --git a/app/Http/Controllers/Licenses/LicensesController.php b/app/Http/Controllers/Licenses/LicensesController.php
index b33fb017dc..bf7bd5a9b7 100755
--- a/app/Http/Controllers/Licenses/LicensesController.php
+++ b/app/Http/Controllers/Licenses/LicensesController.php
@@ -112,6 +112,10 @@ class LicensesController extends Controller
$license->termination_date = $request->input('termination_date');
$license->created_by = auth()->id();
$license->min_amt = $request->input('min_amt');
+ // Unchecked checkboxes are omitted from the POST body; coerce
+ // absence to false so the flag really flips off. The setter
+ // normalizes the truthy branch.
+ $license->requestable = $request->input('requestable', false);
if ($request->input('redirect_option') === 'back') {
session()->put(['redirect_option' => 'index']);
@@ -198,6 +202,9 @@ class LicensesController extends Controller
$license->supplier_id = $request->input('supplier_id');
$license->category_id = $request->input('category_id');
$license->min_amt = $request->input('min_amt');
+ // Unchecked checkbox is omitted from the POST body; coerce
+ // absence to false so unchecking really turns the flag off.
+ $license->requestable = $request->input('requestable', false);
session()->put(['redirect_option' => $request->input('redirect_option')]);