diff --git a/app/Http/Controllers/Accessories/AccessoriesController.php b/app/Http/Controllers/Accessories/AccessoriesController.php index eb004d559..3bebf895c 100755 --- a/app/Http/Controllers/Accessories/AccessoriesController.php +++ b/app/Http/Controllers/Accessories/AccessoriesController.php @@ -184,15 +184,15 @@ class AccessoriesController extends Controller */ public function destroy($accessoryId) : RedirectResponse { - if (is_null($accessory = Accessory::find($accessoryId))) { + if (is_null($accessory = Accessory::withCount('checkouts as checkouts_count')->find($accessoryId))) { return redirect()->route('accessories.index')->with('error', trans('admin/accessories/message.not_found')); } $this->authorize($accessory); - if ($accessory->hasUsers() > 0) { - return redirect()->route('accessories.index')->with('error', trans('admin/accessories/message.assoc_users', ['count'=> $accessory->hasUsers()])); + if ($accessory->checkouts_count > 0) { + return redirect()->route('accessories.index')->with('error', trans('admin/accessories/message.assoc_checkouts', ['count' => $accessory->checkouts_count])); } if ($accessory->image) { diff --git a/database/factories/AccessoryFactory.php b/database/factories/AccessoryFactory.php index bdcffd50e..f2c01dc8f 100644 --- a/database/factories/AccessoryFactory.php +++ b/database/factories/AccessoryFactory.php @@ -3,6 +3,7 @@ namespace Database\Factories; use App\Models\Accessory; +use App\Models\Asset; use App\Models\Category; use App\Models\Location; use App\Models\Manufacturer; @@ -170,4 +171,30 @@ class AccessoryFactory extends Factory } }); } + + public function checkedOutToAsset(Asset $asset = null) + { + return $this->afterCreating(function (Accessory $accessory) use ($asset) { + $accessory->checkouts()->create([ + 'accessory_id' => $accessory->id, + 'created_at' => Carbon::now(), + 'created_by' => 1, + 'assigned_to' => $asset->id ?? Asset::factory()->create()->id, + 'assigned_type' => Asset::class, + ]); + }); + } + + public function checkedOutToLocation(Location $location = null) + { + return $this->afterCreating(function (Accessory $accessory) use ($location) { + $accessory->checkouts()->create([ + 'accessory_id' => $accessory->id, + 'created_at' => Carbon::now(), + 'created_by' => 1, + 'assigned_to' => $location->id ?? Location::factory()->create()->id, + 'assigned_type' => Location::class, + ]); + }); + } } diff --git a/resources/lang/en-US/admin/accessories/message.php b/resources/lang/en-US/admin/accessories/message.php index f60d41957..693203c21 100644 --- a/resources/lang/en-US/admin/accessories/message.php +++ b/resources/lang/en-US/admin/accessories/message.php @@ -5,6 +5,7 @@ return array( 'does_not_exist' => 'The accessory [:id] does not exist.', 'not_found' => 'That accessory was not found.', 'assoc_users' => 'This accessory currently has :count items checked out to users. Please check in the accessories and and try again. ', + 'assoc_checkouts' => 'This accessory currently has :count items checked out. Please check in the accessories and and try again.', 'create' => array( 'error' => 'The accessory was not created, please try again.', diff --git a/tests/Feature/Accessories/Ui/DeleteAccessoryTest.php b/tests/Feature/Accessories/Ui/DeleteAccessoryTest.php index 80cf96091..45ebbf71f 100644 --- a/tests/Feature/Accessories/Ui/DeleteAccessoryTest.php +++ b/tests/Feature/Accessories/Ui/DeleteAccessoryTest.php @@ -5,6 +5,7 @@ namespace Tests\Feature\Accessories\Ui; use App\Models\Accessory; use App\Models\Company; use App\Models\User; +use PHPUnit\Framework\Attributes\DataProvider; use Tests\TestCase; class DeleteAccessoryTest extends TestCase @@ -29,9 +30,17 @@ class DeleteAccessoryTest extends TestCase $this->assertFalse($accessoryForCompanyA->refresh()->trashed(), 'Accessory should not be deleted'); } - public function testCannotDeleteAccessoryThatHasCheckouts() + public static function checkedOutAccessories() { - $accessory = Accessory::factory()->checkedOutToUser()->create(); + yield 'checked out to user' => [fn() => Accessory::factory()->checkedOutToUser()->create()]; + yield 'checked out to asset' => [fn() => Accessory::factory()->checkedOutToAsset()->create()]; + yield 'checked out to location' => [fn() => Accessory::factory()->checkedOutToLocation()->create()]; + } + + #[DataProvider('checkedOutAccessories')] + public function testCannotDeleteAccessoryThatHasCheckouts($data) + { + $accessory = $data(); $this->actingAs(User::factory()->deleteAccessories()->create()) ->delete(route('accessories.destroy', $accessory->id))