A free open source IT asset/license management system
Only clear pending acceptances for users on checkin
Marcus Moore46a0d00c84de
diff --git a/app/Listeners/CheckoutableListener.php b/app/Listeners/CheckoutableListener.phpindex 8de7b40ea6..eb60bd0cc8 100644--- a/app/Listeners/CheckoutableListener.php+++ b/app/Listeners/CheckoutableListener.php@@ -173,7 +173,7 @@ class CheckoutableListener{Log::debug('onCheckedIn in the Checkoutable listener fired');- if ($event->checkedOutTo && $event->checkoutable) {+ if ($event->checkedOutTo instanceof User && $event->checkoutable) {$this->retirePendingAcceptances($event->checkoutable, $event->checkedOutTo);}@@ -270,8 +270,12 @@ class CheckoutableListener* are not: accessories_checkout holds one row per unit while an acceptance* row covers a whole checkout action and carries its qty, so checking one* unit in retires one unit rather than a row that may be worth three.+ *+ * Only ever called for a User holder. Acceptances are created for users+ * alone, so assigned_to_id holds a user id — matching a Location or Asset+ * id against it would clear a different holder's rows by collision.*/- private function retirePendingAcceptances(Model $checkoutable, $checkedOutTo): void+ private function retirePendingAcceptances(Model $checkoutable, User $checkedOutTo): void{$acceptances = CheckoutAcceptance::pending()->where('checkoutable_type', $checkoutable->getMorphClass())@@ -280,7 +284,7 @@ class CheckoutableListener->orderBy('id')->get();- if ($checkoutable instanceof Accessory && $checkedOutTo instanceof User) {+ if ($checkoutable instanceof Accessory) {$this->retireOneUnitOfPendingQty($acceptances);return;diff --git a/tests/Feature/CheckoutAcceptances/PendingAcceptanceQuantityOnCheckinTest.php b/tests/Feature/CheckoutAcceptances/PendingAcceptanceQuantityOnCheckinTest.phpindex b6edb2d827..18f5c93cd4 100644--- a/tests/Feature/CheckoutAcceptances/PendingAcceptanceQuantityOnCheckinTest.php+++ b/tests/Feature/CheckoutAcceptances/PendingAcceptanceQuantityOnCheckinTest.php@@ -3,13 +3,16 @@namespace Tests\Feature\CheckoutAcceptances;use App\Models\Accessory;+use App\Models\AccessoryCheckout;use App\Models\Asset;use App\Models\AssetModel;use App\Models\Category;use App\Models\CheckoutAcceptance;use App\Models\License;use App\Models\LicenseSeat;+use App\Models\Location;use App\Models\User;+use Illuminate\Support\Facades\DB;use Illuminate\Support\Facades\Mail;use PHPUnit\Framework\Attributes\Test;use Tests\TestCase;@@ -134,6 +137,31 @@ class PendingAcceptanceQuantityOnCheckinTest extends TestCase$this->assertPendingQtyMatchesUnacceptedUnitsHeld($accessory, $user);}+ #[Test]+ public function checking_in_a_location_held_unit_leaves_a_users_acceptance_alone(): void+ {+ [$accessory, $user] = $this->accessoryAndUser();+ $this->checkOut($accessory, $user, qty: 2);++ // Acceptances only ever belong to users, so assigned_to_id holds a user+ // id. An accessory can be held by a user and a location at once, so a+ // location id equal to a user id must not match that user's rows.+ $location = $this->locationWithIdCollidingWith($user);++ $locationCheckout = AccessoryCheckout::create([+ 'accessory_id' => $accessory->id,+ 'assigned_to' => $location->id,+ 'assigned_type' => Location::class,+ 'created_by' => User::factory()->create()->id,+ ]);++ $this->checkIn($locationCheckout->id);++ $this->assertSame(2, $this->unitsHeld($accessory, $user), 'The user\'s own units were not touched.');+ $this->assertSame(2, $this->pendingQtySum($accessory, $user),+ 'Checking in a location-held unit cleared the user\'s acceptance by id collision.');+ }+// ─────────────────────────────────────────────────────────────────────// MIXED accepted / pending — accepted rows are history and stay untouched// ─────────────────────────────────────────────────────────────────────@@ -394,6 +422,18 @@ class PendingAcceptanceQuantityOnCheckinTest extends TestCasereturn $seat->refresh();}+ /**+ * Ids are per-table, so a location sharing a user's id is ordinary in+ * production but has to be forced here.+ */+ private function locationWithIdCollidingWith(User $user): Location+ {+ $location = Location::factory()->create();+ DB::table('locations')->where('id', $location->id)->update(['id' => $user->id]);++ return Location::findOrFail($user->id);+ }+private function oneHeldCheckoutOf(Accessory $accessory, User $user): int{return $accessory->checkouts()