Skip to content

A free open source IT asset/license management system

Add migration to clean stale pending acceptances

Marcus Mooreec837c072237
diff --git a/database/factories/CheckoutAcceptanceFactory.php b/database/factories/CheckoutAcceptanceFactory.php
index 597faf4b46..d52ac73134 100644
--- a/database/factories/CheckoutAcceptanceFactory.php
+++ b/database/factories/CheckoutAcceptanceFactory.php
@@ -89,6 +89,14 @@ class CheckoutAcceptanceFactory extends Factory
]);
}
+ public function declined()
+ {
+ return $this->state([
+ 'accepted_at' => null,
+ 'declined_at' => now()->subDay(),
+ ]);
+ }
+
public function withoutAlerting()
{
return $this->state(function () {
diff --git a/database/migrations/2026_08_24_234955_clean_stale_pending_acceptances.php b/database/migrations/2026_08_24_234955_clean_stale_pending_acceptances.php
new file mode 100644
index 0000000000..d692f10b4b
--- /dev/null
+++ b/database/migrations/2026_08_24_234955_clean_stale_pending_acceptances.php
@@ -0,0 +1,178 @@
+<?php
+
+use App\Models\Accessory;
+use App\Models\Asset;
+use App\Models\Consumable;
+use App\Models\LicenseSeat;
+use App\Models\User;
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Query\Builder;
+use Illuminate\Support\Collection;
+use Illuminate\Support\Facades\DB;
+
+/**
+ * Soft-deletes pending acceptance requests that no longer correspond to
+ * anything the holder has, cleaning up after the checkin/transfer bugs fixed
+ * alongside this. Pendings for units still held are left alone.
+ */
+return new class extends Migration
+{
+ public function up(): void
+ {
+ $clearedAt = now();
+
+ // Assets and license seats are 1:1 with their acceptance rows, so a
+ // boolean "still holds it" test is enough. A trashed item does not
+ // count: a restored asset comes back unassigned and licenses have no
+ // restore path, so the pending could never become actionable.
+ $this->clean(Asset::class, $clearedAt, function (Builder $query) {
+ $query->from('assets')
+ ->whereColumn('assets.id', 'checkout_acceptances.checkoutable_id')
+ ->whereColumn('assets.assigned_to', 'checkout_acceptances.assigned_to_id')
+ ->where('assets.assigned_type', User::class)
+ ->whereNull('assets.deleted_at');
+ });
+
+ $this->clean(LicenseSeat::class, $clearedAt, function (Builder $query) {
+ $query->from('license_seats')
+ ->whereColumn('license_seats.id', 'checkout_acceptances.checkoutable_id')
+ ->whereColumn('license_seats.assigned_to', 'checkout_acceptances.assigned_to_id')
+ ->whereNull('license_seats.deleted_at');
+ });
+
+ // Accessories and consumables are not 1:1: their pivots hold one row
+ // per unit, so a held/not-held test cannot see "holds one unit, has
+ // pendings worth three".
+ $this->reconcile(Accessory::class, $clearedAt, 'accessories_checkout', 'accessory_id', pivotHasAssignedType: true);
+ $this->reconcile(Consumable::class, $clearedAt, 'consumables_users', 'consumable_id', pivotHasAssignedType: false);
+ }
+
+ public function down(): void
+ {
+ // No-op. These rows were already meaningless when this migration ran,
+ // and restoring them is not possible anyway: there is no way to tell
+ // them apart from acceptances soft-deleted by a normal checkin.
+ }
+
+ /**
+ * Soft-delete every pending acceptance of one 1:1 type whose holder no
+ * longer has the item. `$stillHolds` receives a subquery that should match
+ * a row proving the pair still holds it; anything unmatched gets cleared.
+ *
+ * The `deleted_at` filter is what makes re-running this a no-op.
+ */
+ private function clean(string $checkoutableType, DateTimeInterface $clearedAt, Closure $stillHolds): void
+ {
+ DB::table('checkout_acceptances')
+ ->select('id')
+ ->where('checkoutable_type', $checkoutableType)
+ ->whereNull('deleted_at')
+ ->whereNull('accepted_at')
+ ->whereNull('declined_at')
+ ->whereNotExists($stillHolds)
+ ->chunkById(500, function (Collection $acceptances) use ($clearedAt) {
+ DB::table('checkout_acceptances')
+ ->whereIn('id', $acceptances->pluck('id'))
+ ->update(['deleted_at' => $clearedAt]);
+ });
+ }
+
+ /**
+ * Bring each pair's total pending qty down to the units it actually holds.
+ *
+ * Excess is retired oldest row first: a row is soft-deleted when all of it
+ * is excess, decremented when only part is. A pair holding nothing loses
+ * every row, so this is a superset of the boolean pass, not a departure.
+ * Re-running computes zero excess and writes nothing.
+ */
+ private function reconcile(
+ string $checkoutableType,
+ DateTimeInterface $clearedAt,
+ string $pivotTable,
+ string $itemColumn,
+ bool $pivotHasAssignedType,
+ ): void {
+ foreach ($this->pairsWithPendingAcceptances($checkoutableType) as $pair) {
+ $acceptances = $this->pendingAcceptancesFor($checkoutableType, $pair);
+
+ $unitsHeld = DB::table($pivotTable)
+ ->where($itemColumn, $pair->checkoutable_id)
+ ->where('assigned_to', $pair->assigned_to_id)
+ ->when($pivotHasAssignedType, fn ($query) => $query->where('assigned_type', User::class))
+ ->count();
+
+ $excess = $this->totalQty($acceptances) - $unitsHeld;
+
+ foreach ($acceptances as $acceptance) {
+ if ($excess <= 0) {
+ break;
+ }
+
+ $qty = $acceptance->qty ?? 1;
+
+ if ($qty <= $excess) {
+ DB::table('checkout_acceptances')
+ ->where('id', $acceptance->id)
+ ->update(['deleted_at' => $clearedAt]);
+
+ $excess -= $qty;
+
+ continue;
+ }
+
+ DB::table('checkout_acceptances')
+ ->where('id', $acceptance->id)
+ ->update(['qty' => $qty - $excess]);
+
+ $excess = 0;
+ }
+ }
+ }
+
+ /**
+ * The distinct (item, holder) pairs with anything pending. Grouping keeps
+ * this to one row per pair rather than loading the whole table.
+ *
+ * @return Collection<int, stdClass>
+ */
+ private function pairsWithPendingAcceptances(string $checkoutableType): Collection
+ {
+ return DB::table('checkout_acceptances')
+ ->select('checkoutable_id', 'assigned_to_id')
+ ->where('checkoutable_type', $checkoutableType)
+ ->whereNull('deleted_at')
+ ->whereNull('accepted_at')
+ ->whereNull('declined_at')
+ ->groupBy('checkoutable_id', 'assigned_to_id')
+ ->get();
+ }
+
+ /**
+ * @return Collection<int, stdClass>
+ */
+ private function pendingAcceptancesFor(string $checkoutableType, stdClass $pair): Collection
+ {
+ return DB::table('checkout_acceptances')
+ ->select('id', 'qty')
+ ->where('checkoutable_type', $checkoutableType)
+ ->where('checkoutable_id', $pair->checkoutable_id)
+ ->where('assigned_to_id', $pair->assigned_to_id)
+ ->whereNull('deleted_at')
+ ->whereNull('accepted_at')
+ ->whereNull('declined_at')
+ ->orderBy('id')
+ ->get();
+ }
+
+ /**
+ * Null qty means one unit, as in AcceptanceController and LogListener.
+ * Rows predating the qty column, and every asset and seat acceptance,
+ * carry null.
+ *
+ * @param Collection<int, stdClass> $acceptances
+ */
+ private function totalQty(Collection $acceptances): int
+ {
+ return (int) $acceptances->sum(fn (stdClass $acceptance) => $acceptance->qty ?? 1);
+ }
+};
diff --git a/tests/Feature/Migrations/CleanStalePendingAcceptancesTest.php b/tests/Feature/Migrations/CleanStalePendingAcceptancesTest.php
new file mode 100644
index 0000000000..96c96f289c
--- /dev/null
+++ b/tests/Feature/Migrations/CleanStalePendingAcceptancesTest.php
@@ -0,0 +1,538 @@
+<?php
+
+namespace Tests\Feature\Migrations;
+
+use App\Models\Accessory;
+use App\Models\AccessoryCheckout;
+use App\Models\Asset;
+use App\Models\CheckoutAcceptance;
+use App\Models\Consumable;
+use App\Models\LicenseSeat;
+use App\Models\User;
+use Illuminate\Support\Facades\DB;
+use Tests\TestCase;
+
+class CleanStalePendingAcceptancesTest extends TestCase
+{
+ public function test_it_clears_a_pending_acceptance_for_an_asset_the_user_no_longer_holds(): void
+ {
+ $user = User::factory()->create();
+ $asset = Asset::factory()->create(['assigned_to' => null, 'assigned_type' => null]);
+
+ $acceptance = CheckoutAcceptance::factory()->withoutActionLog()->pending()->create([
+ 'checkoutable_type' => Asset::class,
+ 'checkoutable_id' => $asset->id,
+ 'assigned_to_id' => $user->id,
+ ]);
+
+ $this->runMigration();
+
+ $this->assertAcceptanceCleared($acceptance);
+ }
+
+ public function test_it_clears_a_pending_acceptance_for_a_license_seat_the_user_no_longer_holds(): void
+ {
+ $user = User::factory()->create();
+ $seat = LicenseSeat::factory()->create(['assigned_to' => null]);
+
+ $acceptance = CheckoutAcceptance::factory()->pending()->create([
+ 'checkoutable_type' => LicenseSeat::class,
+ 'checkoutable_id' => $seat->id,
+ 'assigned_to_id' => $user->id,
+ ]);
+
+ $this->runMigration();
+
+ $this->assertAcceptanceCleared($acceptance);
+ }
+
+ public function test_it_clears_a_pending_acceptance_for_an_accessory_the_user_no_longer_holds(): void
+ {
+ $user = User::factory()->create();
+ $accessory = Accessory::factory()->create();
+
+ $acceptance = CheckoutAcceptance::factory()->pending()->create([
+ 'checkoutable_type' => Accessory::class,
+ 'checkoutable_id' => $accessory->id,
+ 'assigned_to_id' => $user->id,
+ ]);
+
+ $this->assertDatabaseMissing('accessories_checkout', [
+ 'accessory_id' => $accessory->id,
+ 'assigned_to' => $user->id,
+ ]);
+
+ $this->runMigration();
+
+ $this->assertAcceptanceCleared($acceptance);
+ }
+
+ /**
+ * Consumables have no checkin route at all, so this state cannot be produced
+ * through the UI — it is constructed directly. The migration still covers the
+ * type because user-deletion paths do remove consumables_users rows.
+ */
+ public function test_it_clears_a_pending_acceptance_for_a_consumable_the_user_no_longer_holds(): void
+ {
+ $user = User::factory()->create();
+ $consumable = Consumable::factory()->create();
+
+ $acceptance = CheckoutAcceptance::factory()->pending()->create([
+ 'checkoutable_type' => Consumable::class,
+ 'checkoutable_id' => $consumable->id,
+ 'assigned_to_id' => $user->id,
+ ]);
+
+ $this->runMigration();
+
+ $this->assertAcceptanceCleared($acceptance);
+ }
+
+ public function test_it_leaves_a_pending_acceptance_alone_when_the_user_still_holds_the_asset(): void
+ {
+ $user = User::factory()->create();
+ $asset = Asset::factory()->assignedToUser($user)->create();
+
+ $acceptance = CheckoutAcceptance::factory()->withoutActionLog()->pending()->create([
+ 'checkoutable_type' => Asset::class,
+ 'checkoutable_id' => $asset->id,
+ 'assigned_to_id' => $user->id,
+ ]);
+
+ $this->assertSame($user->id, $asset->refresh()->assigned_to);
+
+ $this->runMigration();
+
+ $this->assertAcceptanceSurvived($acceptance);
+ }
+
+ public function test_it_leaves_a_pending_acceptance_alone_when_the_user_still_holds_the_license_seat(): void
+ {
+ $user = User::factory()->create();
+ $seat = LicenseSeat::factory()->assignedToUser($user)->create();
+
+ $acceptance = CheckoutAcceptance::factory()->pending()->create([
+ 'checkoutable_type' => LicenseSeat::class,
+ 'checkoutable_id' => $seat->id,
+ 'assigned_to_id' => $user->id,
+ ]);
+
+ $this->runMigration();
+
+ $this->assertAcceptanceSurvived($acceptance);
+ }
+
+ public function test_it_leaves_a_pending_acceptance_alone_when_the_user_still_holds_the_accessory(): void
+ {
+ $user = User::factory()->create();
+ $accessory = Accessory::factory()->checkedOutToUser($user)->create();
+
+ $acceptance = CheckoutAcceptance::factory()->pending()->create([
+ 'checkoutable_type' => Accessory::class,
+ 'checkoutable_id' => $accessory->id,
+ 'assigned_to_id' => $user->id,
+ ]);
+
+ $this->runMigration();
+
+ $this->assertAcceptanceSurvived($acceptance);
+ }
+
+ public function test_it_leaves_a_pending_acceptance_alone_when_the_user_still_holds_the_consumable(): void
+ {
+ $user = User::factory()->create();
+ $consumable = Consumable::factory()->create();
+ $consumable->users()->attach($consumable->id, [
+ 'consumable_id' => $consumable->id,
+ 'assigned_to' => $user->id,
+ 'created_by' => User::factory()->create()->id,
+ ]);
+
+ $acceptance = CheckoutAcceptance::factory()->pending()->create([
+ 'checkoutable_type' => Consumable::class,
+ 'checkoutable_id' => $consumable->id,
+ 'assigned_to_id' => $user->id,
+ ]);
+
+ $this->runMigration();
+
+ $this->assertAcceptanceSurvived($acceptance);
+ }
+
+ /**
+ * Restoring an asset hands it back unassigned, so this pending could never
+ * become actionable — it would sit on /account/accept with no buttons.
+ */
+ public function test_it_clears_a_pending_acceptance_when_the_asset_is_soft_deleted_but_still_assigned(): void
+ {
+ $user = User::factory()->create();
+ $asset = Asset::factory()->assignedToUser($user)->create();
+ $asset->delete();
+
+ $acceptance = CheckoutAcceptance::factory()->withoutActionLog()->pending()->create([
+ 'checkoutable_type' => Asset::class,
+ 'checkoutable_id' => $asset->id,
+ 'assigned_to_id' => $user->id,
+ ]);
+
+ $this->assertSoftDeleted($asset);
+ $this->assertSame($user->id, Asset::withTrashed()->find($asset->id)->assigned_to);
+
+ $this->runMigration();
+
+ $this->assertAcceptanceCleared($acceptance);
+ }
+
+ /**
+ * Bulk license deletion soft-deletes seats without nulling assigned_to, so
+ * this state is ordinary. There is no seat restore path anywhere in the
+ * app, so the pending is dead rather than dormant.
+ */
+ public function test_it_clears_a_pending_acceptance_when_the_license_seat_is_soft_deleted_but_still_assigned(): void
+ {
+ $user = User::factory()->create();
+ $seat = LicenseSeat::factory()->assignedToUser($user)->create();
+ $seat->delete();
+
+ $acceptance = CheckoutAcceptance::factory()->pending()->create([
+ 'checkoutable_type' => LicenseSeat::class,
+ 'checkoutable_id' => $seat->id,
+ 'assigned_to_id' => $user->id,
+ ]);
+
+ $this->assertSoftDeleted($seat);
+ $this->assertSame($user->id, LicenseSeat::withTrashed()->find($seat->id)->assigned_to);
+
+ $this->runMigration();
+
+ $this->assertAcceptanceCleared($acceptance);
+ }
+
+ /**
+ * The accessory pivot carries assigned_type, so a row assigned to an Asset
+ * rather than a User is not this user holding the item.
+ */
+ public function test_it_clears_when_the_only_accessory_pivot_row_belongs_to_another_holder_type(): void
+ {
+ $user = User::factory()->create();
+ $accessory = Accessory::factory()->create();
+
+ AccessoryCheckout::create([
+ 'accessory_id' => $accessory->id,
+ 'assigned_to' => $user->id,
+ 'assigned_type' => Asset::class,
+ 'created_by' => User::factory()->create()->id,
+ ]);
+
+ $acceptance = CheckoutAcceptance::factory()->pending()->create([
+ 'checkoutable_type' => Accessory::class,
+ 'checkoutable_id' => $accessory->id,
+ 'assigned_to_id' => $user->id,
+ ]);
+
+ $this->runMigration();
+
+ $this->assertAcceptanceCleared($acceptance);
+ }
+
+ public function test_it_leaves_already_accepted_rows_alone(): void
+ {
+ $user = User::factory()->create();
+ $seat = LicenseSeat::factory()->create(['assigned_to' => null]);
+
+ $acceptance = CheckoutAcceptance::factory()->accepted()->create([
+ 'checkoutable_type' => LicenseSeat::class,
+ 'checkoutable_id' => $seat->id,
+ 'assigned_to_id' => $user->id,
+ ]);
+
+ $this->runMigration();
+
+ $this->assertAcceptanceSurvived($acceptance);
+ }
+
+ public function test_it_leaves_already_declined_rows_alone(): void
+ {
+ $user = User::factory()->create();
+ $seat = LicenseSeat::factory()->create(['assigned_to' => null]);
+
+ $acceptance = CheckoutAcceptance::factory()->declined()->create([
+ 'checkoutable_type' => LicenseSeat::class,
+ 'checkoutable_id' => $seat->id,
+ 'assigned_to_id' => $user->id,
+ ]);
+
+ $this->runMigration();
+
+ $this->assertAcceptanceSurvived($acceptance);
+ }
+
+ public function test_it_soft_deletes_rather_than_hard_deletes(): void
+ {
+ $user = User::factory()->create();
+ $seat = LicenseSeat::factory()->create(['assigned_to' => null]);
+
+ $acceptance = CheckoutAcceptance::factory()->pending()->create([
+ 'checkoutable_type' => LicenseSeat::class,
+ 'checkoutable_id' => $seat->id,
+ 'assigned_to_id' => $user->id,
+ ]);
+
+ $this->runMigration();
+
+ $this->assertDatabaseHas('checkout_acceptances', ['id' => $acceptance->id]);
+ $this->assertNotNull(CheckoutAcceptance::withTrashed()->find($acceptance->id)->deleted_at);
+ $this->assertNull(CheckoutAcceptance::find($acceptance->id));
+ }
+
+ public function test_running_it_twice_leaves_the_same_result(): void
+ {
+ $user = User::factory()->create();
+
+ $staleSeat = LicenseSeat::factory()->create(['assigned_to' => null]);
+ $stale = CheckoutAcceptance::factory()->pending()->create([
+ 'checkoutable_type' => LicenseSeat::class,
+ 'checkoutable_id' => $staleSeat->id,
+ 'assigned_to_id' => $user->id,
+ ]);
+
+ $heldSeat = LicenseSeat::factory()->assignedToUser($user)->create();
+ $held = CheckoutAcceptance::factory()->pending()->create([
+ 'checkoutable_type' => LicenseSeat::class,
+ 'checkoutable_id' => $heldSeat->id,
+ 'assigned_to_id' => $user->id,
+ ]);
+
+ $this->runMigration();
+
+ $deletedAtAfterFirstRun = CheckoutAcceptance::withTrashed()->find($stale->id)->deleted_at;
+
+ $this->runMigration();
+
+ $this->assertEquals(
+ $deletedAtAfterFirstRun,
+ CheckoutAcceptance::withTrashed()->find($stale->id)->deleted_at,
+ 'The second run re-stamped an already soft-deleted row; up() is not idempotent.'
+ );
+ $this->assertAcceptanceSurvived($held);
+ }
+
+ public function test_it_does_not_touch_rows_belonging_to_a_different_user(): void
+ {
+ $holder = User::factory()->create();
+ $otherUser = User::factory()->create();
+ $seat = LicenseSeat::factory()->assignedToUser($holder)->create();
+
+ // The seat is held, but by someone else — so this pending row is stale.
+ $acceptance = CheckoutAcceptance::factory()->pending()->create([
+ 'checkoutable_type' => LicenseSeat::class,
+ 'checkoutable_id' => $seat->id,
+ 'assigned_to_id' => $otherUser->id,
+ ]);
+
+ $this->runMigration();
+
+ $this->assertAcceptanceCleared($acceptance);
+ }
+
+ // ─────────────────────────────────────────────────────────────────────
+ // Quantity reconcile — accessories and consumables are not 1:1, so a
+ // held/not-held test cannot see "holds one unit, has pendings worth three".
+ // ─────────────────────────────────────────────────────────────────────
+
+ public function test_it_decrements_an_accessory_pending_whose_qty_exceeds_the_units_held(): void
+ {
+ $user = User::factory()->create();
+ $accessory = Accessory::factory()->checkedOutToUser($user)->create();
+
+ $acceptance = CheckoutAcceptance::factory()->pending()->create([
+ 'checkoutable_type' => Accessory::class,
+ 'checkoutable_id' => $accessory->id,
+ 'assigned_to_id' => $user->id,
+ 'qty' => 3,
+ ]);
+
+ $this->runMigration();
+
+ // Deleting the row would leave the held unit with no record at all.
+ $this->assertAcceptanceSurvived($acceptance);
+ $this->assertSame(1, $this->qtyOf($acceptance));
+ }
+
+ public function test_it_spills_excess_units_across_accessory_acceptance_rows(): void
+ {
+ $user = User::factory()->create();
+ $accessory = Accessory::factory()->checkedOutToUser($user)->create();
+
+ $older = CheckoutAcceptance::factory()->pending()->create([
+ 'checkoutable_type' => Accessory::class,
+ 'checkoutable_id' => $accessory->id,
+ 'assigned_to_id' => $user->id,
+ 'qty' => 2,
+ ]);
+
+ $newer = CheckoutAcceptance::factory()->pending()->create([
+ 'checkoutable_type' => Accessory::class,
+ 'checkoutable_id' => $accessory->id,
+ 'assigned_to_id' => $user->id,
+ 'qty' => 2,
+ ]);
+
+ $this->runMigration();
+
+ // Four pending against one held: the older row is wholly excess and
+ // goes, the remainder bites into the newer one.
+ $this->assertAcceptanceCleared($older);
+ $this->assertAcceptanceSurvived($newer);
+ $this->assertSame(1, $this->qtyOf($newer));
+ }
+
+ public function test_it_clears_a_multi_unit_accessory_pending_when_the_user_holds_nothing(): void
+ {
+ $user = User::factory()->create();
+ $accessory = Accessory::factory()->create();
+
+ $acceptance = CheckoutAcceptance::factory()->pending()->create([
+ 'checkoutable_type' => Accessory::class,
+ 'checkoutable_id' => $accessory->id,
+ 'assigned_to_id' => $user->id,
+ 'qty' => 3,
+ ]);
+
+ $this->runMigration();
+
+ // Holding nothing makes the row wholly excess, so the reconcile is a
+ // superset of the boolean pass it replaced.
+ $this->assertAcceptanceCleared($acceptance);
+ }
+
+ public function test_it_leaves_an_accessory_pending_alone_when_its_qty_matches_the_units_held(): void
+ {
+ $user = User::factory()->create();
+ $accessory = Accessory::factory()->checkedOutToUsers([$user, $user, $user])->create();
+
+ $acceptance = CheckoutAcceptance::factory()->pending()->create([
+ 'checkoutable_type' => Accessory::class,
+ 'checkoutable_id' => $accessory->id,
+ 'assigned_to_id' => $user->id,
+ 'qty' => 3,
+ ]);
+
+ $this->runMigration();
+
+ $this->assertAcceptanceSurvived($acceptance);
+ $this->assertSame(3, $this->qtyOf($acceptance), 'A correct row must not be renumbered.');
+ }
+
+ public function test_it_decrements_a_consumable_pending_whose_qty_exceeds_the_units_held(): void
+ {
+ $user = User::factory()->create();
+ $consumable = Consumable::factory()->create();
+ $this->attachConsumableTo($consumable, $user);
+
+ $acceptance = CheckoutAcceptance::factory()->pending()->create([
+ 'checkoutable_type' => Consumable::class,
+ 'checkoutable_id' => $consumable->id,
+ 'assigned_to_id' => $user->id,
+ 'qty' => 4,
+ ]);
+
+ $this->runMigration();
+
+ $this->assertAcceptanceSurvived($acceptance);
+ $this->assertSame(1, $this->qtyOf($acceptance));
+ }
+
+ public function test_running_the_reconcile_twice_does_not_decrement_further(): void
+ {
+ $user = User::factory()->create();
+ $accessory = Accessory::factory()->checkedOutToUser($user)->create();
+
+ $acceptance = CheckoutAcceptance::factory()->pending()->create([
+ 'checkoutable_type' => Accessory::class,
+ 'checkoutable_id' => $accessory->id,
+ 'assigned_to_id' => $user->id,
+ 'qty' => 3,
+ ]);
+
+ $this->runMigration();
+ $this->runMigration();
+
+ $this->assertAcceptanceSurvived($acceptance);
+ $this->assertSame(1, $this->qtyOf($acceptance), 'The second run decremented again; the reconcile is not idempotent.');
+ }
+
+ /**
+ * Accepted rows are history and may cover units returned years ago, so
+ * netting them off could clear a pending for a unit still held. The
+ * reconcile compares against units held only, leaving an over-count here —
+ * the safe direction, which the live checkin path narrows from there.
+ */
+ public function test_it_does_not_net_accepted_history_off_the_pending_total(): void
+ {
+ $user = User::factory()->create();
+ $accessory = Accessory::factory()->checkedOutToUsers([$user, $user, $user])->create();
+
+ $accepted = CheckoutAcceptance::factory()->accepted()->create([
+ 'checkoutable_type' => Accessory::class,
+ 'checkoutable_id' => $accessory->id,
+ 'assigned_to_id' => $user->id,
+ 'qty' => 2,
+ ]);
+
+ $pending = CheckoutAcceptance::factory()->pending()->create([
+ 'checkoutable_type' => Accessory::class,
+ 'checkoutable_id' => $accessory->id,
+ 'assigned_to_id' => $user->id,
+ 'qty' => 3,
+ ]);
+
+ $this->runMigration();
+
+ $this->assertAcceptanceSurvived($pending);
+ $this->assertSame(3, $this->qtyOf($pending));
+ $this->assertAcceptanceSurvived($accepted);
+ $this->assertSame(2, $this->qtyOf($accepted), 'An accepted row must never be renumbered.');
+ }
+
+ /**
+ * LazilyRefreshDatabase has already run this migration once against an empty
+ * database, so each test builds fixtures and runs up() again — which only
+ * works because up() is idempotent.
+ */
+ private function runMigration(): void
+ {
+ $migration = require database_path('migrations/2026_08_24_234955_clean_stale_pending_acceptances.php');
+ $migration->up();
+ }
+
+ private function attachConsumableTo(Consumable $consumable, User $user): void
+ {
+ $consumable->users()->attach($consumable->id, [
+ 'consumable_id' => $consumable->id,
+ 'assigned_to' => $user->id,
+ 'created_by' => User::factory()->create()->id,
+ ]);
+ }
+
+ private function qtyOf(CheckoutAcceptance $acceptance): int
+ {
+ return (int) DB::table('checkout_acceptances')->where('id', $acceptance->id)->value('qty');
+ }
+
+ private function assertAcceptanceCleared(CheckoutAcceptance $acceptance): void
+ {
+ $row = DB::table('checkout_acceptances')->where('id', $acceptance->id)->first();
+
+ $this->assertNotNull($row, 'The row was hard-deleted; the migration must soft-delete.');
+ $this->assertNotNull($row->deleted_at, 'Expected the migration to soft-delete this stale pending acceptance.');
+ }
+
+ private function assertAcceptanceSurvived(CheckoutAcceptance $acceptance): void
+ {
+ $row = DB::table('checkout_acceptances')->where('id', $acceptance->id)->first();
+
+ $this->assertNotNull($row, 'The row was hard-deleted; the migration should not have touched it.');
+ $this->assertNull($row->deleted_at, 'The migration soft-deleted a row it should have left alone.');
+ }
+}