A free open source IT asset/license management system
Added fulfilled field to handle partial checkouts
snipe1e9f43c843cd
diff --git a/database/migrations/2026_08_21_010000_add_state_machine_to_checkout_requests.php b/database/migrations/2026_08_21_010000_add_state_machine_to_checkout_requests.phpnew file mode 100644index 0000000000..d7e3371b87--- /dev/null+++ b/database/migrations/2026_08_21_010000_add_state_machine_to_checkout_requests.php@@ -0,0 +1,128 @@+<?php++use App\Enums\CheckoutRequestState;+use Illuminate\Database\Migrations\Migration;+use Illuminate\Database\Schema\Blueprint;+use Illuminate\Support\Facades\DB;+use Illuminate\Support\Facades\Schema;++/**+ * Give CheckoutRequest a real state machine. Prior shape was a+ * two-state model (open vs. canceled_at) with a dangling+ * `fulfilled_at` column added in 2018 that nothing ever wrote. The+ * new state column drives every lifecycle decision (rendering, gate+ * checks, scheduler, admin queue) so there's a single source of+ * truth for "where is this request in its life."+ *+ * canceled_at + fulfilled_at stay in the schema. They now write in+ * lock-step with state transitions - the datetime says WHEN, the+ * state column says WHAT. Existing rows backfill in three passes:+ * 1. Default: state=pending (schema-side default).+ * 2. canceled_at set: state=canceled.+ * 3. fulfilled_at OR fulfilled_by set: state=fulfilled.+ * Fulfilled runs last so a row with both a canceled_at and evidence+ * of fulfillment (edge case, but real if someone was manually+ * poking data via tinker or an import) resolves to fulfilled - the+ * semantic being "actually received the item" trumps an earlier+ * cancel intent.+ *+ * checkout_actionlog_id is the audit link back to the checkout that+ * fulfilled the request. Without this column the item's history tab+ * would need a separate "request fulfilled" actionlog row per+ * fulfillment, doubling every checkout in the timeline for+ * requested items. See CheckoutRequestState + FulfillCheckoutRequestAction+ * for the wiring.+ */+return new class extends Migration+{+ public function up(): void+ {+ Schema::table('checkout_requests', function (Blueprint $table): void {+ if (! Schema::hasColumn('checkout_requests', 'state')) {+ $table->string('state', 20)+ ->default(CheckoutRequestState::Pending->value)+ ->after('notes');+ }+ if (! Schema::hasColumn('checkout_requests', 'fulfilled_by')) {+ $table->unsignedBigInteger('fulfilled_by')+ ->nullable()+ ->after('fulfilled_at');+ }+ if (! Schema::hasColumn('checkout_requests', 'checkout_actionlog_id')) {+ $table->unsignedBigInteger('checkout_actionlog_id')+ ->nullable()+ ->after('fulfilled_by');+ }+ });++ // Backfill BEFORE indexing so the composite index covers real+ // values instead of the default-string writes for every row.+ // Order matters: cancel first, then fulfilled - a row with+ // both set resolves to fulfilled (see class docblock).+ DB::table('checkout_requests')+ ->whereNotNull('canceled_at')+ ->where('state', CheckoutRequestState::Pending->value)+ ->update(['state' => CheckoutRequestState::Canceled->value]);++ DB::table('checkout_requests')+ ->where(function ($q) {+ $q->whereNotNull('fulfilled_at')+ ->orWhereNotNull('fulfilled_by');+ })+ ->update(['state' => CheckoutRequestState::Fulfilled->value]);++ Schema::table('checkout_requests', function (Blueprint $table): void {+ // Composite index for the "open request for this+ // requestable" lookup that every checkout-hook + admin-+ // queue query hits. Guard against re-add on partial+ // re-runs.+ $indexes = collect(Schema::getIndexes('checkout_requests'))+ ->pluck('name')+ ->all();++ if (! in_array('checkout_requests_requestable_state_index', $indexes, true)) {+ $table->index(+ ['requestable_type', 'requestable_id', 'state'],+ 'checkout_requests_requestable_state_index'+ );+ }++ if (! in_array('checkout_requests_fulfilled_by_index', $indexes, true)) {+ $table->index('fulfilled_by');+ }++ if (! in_array('checkout_requests_checkout_actionlog_id_index', $indexes, true)) {+ $table->index('checkout_actionlog_id');+ }+ });+ }++ public function down(): void+ {+ Schema::table('checkout_requests', function (Blueprint $table): void {+ $indexes = collect(Schema::getIndexes('checkout_requests'))+ ->pluck('name')+ ->all();++ if (in_array('checkout_requests_requestable_state_index', $indexes, true)) {+ $table->dropIndex('checkout_requests_requestable_state_index');+ }+ if (in_array('checkout_requests_fulfilled_by_index', $indexes, true)) {+ $table->dropIndex('checkout_requests_fulfilled_by_index');+ }+ if (in_array('checkout_requests_checkout_actionlog_id_index', $indexes, true)) {+ $table->dropIndex('checkout_requests_checkout_actionlog_id_index');+ }++ if (Schema::hasColumn('checkout_requests', 'checkout_actionlog_id')) {+ $table->dropColumn('checkout_actionlog_id');+ }+ if (Schema::hasColumn('checkout_requests', 'fulfilled_by')) {+ $table->dropColumn('fulfilled_by');+ }+ if (Schema::hasColumn('checkout_requests', 'state')) {+ $table->dropColumn('state');+ }+ });+ }+};diff --git a/database/migrations/2026_08_21_030000_add_fulfilled_quantity_to_checkout_requests.php b/database/migrations/2026_08_21_030000_add_fulfilled_quantity_to_checkout_requests.phpnew file mode 100644index 0000000000..f95828d8d8--- /dev/null+++ b/database/migrations/2026_08_21_030000_add_fulfilled_quantity_to_checkout_requests.php@@ -0,0 +1,54 @@+<?php++use Illuminate\Database\Migrations\Migration;+use Illuminate\Database\Schema\Blueprint;+use Illuminate\Support\Facades\DB;+use Illuminate\Support\Facades\Schema;++/**+ * Track partial fulfillment of qty-tracked CheckoutRequests. A+ * request for 5 pens that admin only has 3 of currently should+ * fulfill 3, stay pending, and let the admin come back to knock+ * out the remaining 2 later.+ *+ * Original `quantity` stays immutable so the audit trail preserves+ * what the requester actually asked for; the new+ * `fulfilled_quantity` is a running counter of how much has been+ * delivered so far. State stays `pending` until fulfilled_quantity+ * >= quantity, at which point FulfillCheckoutRequestAction flips+ * it to `fulfilled` and sets fulfilled_at + fulfilled_by.+ *+ * Backfill: existing fulfilled rows get fulfilled_quantity = quantity+ * (they were fully delivered, we just weren't tracking the counter).+ * Pending + canceled rows stay at 0 (nothing delivered yet).+ */+return new class extends Migration+{+ public function up(): void+ {+ Schema::table('checkout_requests', function (Blueprint $table): void {+ if (! Schema::hasColumn('checkout_requests', 'fulfilled_quantity')) {+ $table->unsignedInteger('fulfilled_quantity')+ ->default(0)+ ->after('quantity');+ }+ });++ // Reconcile existing rows: a row currently marked fulfilled+ // was delivered in full under the pre-partial-tracking+ // behavior, so backfill its counter to match the requested+ // quantity. Pending + canceled rows stay at the default 0.+ DB::table('checkout_requests')+ ->where('state', 'fulfilled')+ ->update(['fulfilled_quantity' => DB::raw('quantity')]);+ }++ public function down(): void+ {+ Schema::table('checkout_requests', function (Blueprint $table): void {+ if (Schema::hasColumn('checkout_requests', 'fulfilled_quantity')) {+ $table->dropColumn('fulfilled_quantity');+ }+ });+ }+};