diff --git a/database/factories/AccessoryFactory.php b/database/factories/AccessoryFactory.php index 84edcb705c..8ce34303b3 100644 --- a/database/factories/AccessoryFactory.php +++ b/database/factories/AccessoryFactory.php @@ -2,17 +2,14 @@ namespace Database\Factories; +use App\Models\Accessory; +use App\Models\Category; +use App\Models\Location; +use App\Models\Manufacturer; +use App\Models\Supplier; +use App\Models\User; use Illuminate\Database\Eloquent\Factories\Factory; -/* -|-------------------------------------------------------------------------- -| Asset Model Factories -|-------------------------------------------------------------------------- -| -| Factories related exclusively to creating models .. -| -*/ - class AccessoryFactory extends Factory { /** @@ -20,7 +17,7 @@ class AccessoryFactory extends Factory * * @var string */ - protected $model = \App\Models\Accessory::class; + protected $model = Accessory::class; /** * Define the model's default state. @@ -30,9 +27,16 @@ class AccessoryFactory extends Factory public function definition() { return [ - 'user_id' => 1, + 'name' => sprintf( + '%s %s', + $this->faker->randomElement(['Bluetooth', 'Wired']), + $this->faker->randomElement(['Keyboard', 'Wired']) + ), + 'user_id' => User::factory()->superuser(), + 'category_id' => Category::factory(), 'model_number' => $this->faker->numberBetween(1000000, 50000000), - 'location_id' => rand(1, 5), + 'location_id' => Location::factory(), + 'qty' => 1, ]; } @@ -42,11 +46,15 @@ class AccessoryFactory extends Factory return [ 'name' => 'Bluetooth Keyboard', 'image' => 'bluetooth.jpg', - 'category_id' => 8, - 'manufacturer_id' => 1, + 'category_id' => function () { + return Category::where('name', 'Keyboards')->first() ?? Category::factory()->accessoryKeyboardCategory(); + }, + 'manufacturer_id' => function () { + return Manufacturer::where('name', 'Apple')->first() ?? Manufacturer::factory()->apple(); + }, 'qty' => 10, 'min_amt' => 2, - 'supplier_id' => rand(1, 5), + 'supplier_id' => Supplier::factory(), ]; }); } @@ -57,11 +65,15 @@ class AccessoryFactory extends Factory return [ 'name' => 'USB Keyboard', 'image' => 'usb-keyboard.jpg', - 'category_id' => 8, - 'manufacturer_id' => 1, + 'category_id' => function () { + return Category::where('name', 'Keyboards')->first() ?? Category::factory()->accessoryKeyboardCategory(); + }, + 'manufacturer_id' => function () { + return Manufacturer::where('name', 'Apple')->first() ?? Manufacturer::factory()->apple(); + }, 'qty' => 15, 'min_amt' => 2, - 'supplier_id' => rand(1, 5), + 'supplier_id' => Supplier::factory(), ]; }); } @@ -72,11 +84,15 @@ class AccessoryFactory extends Factory return [ 'name' => 'Magic Mouse', 'image' => 'magic-mouse.jpg', - 'category_id' => 9, - 'manufacturer_id' => 1, + 'category_id' => function () { + return Category::where('name', 'Mouse')->first() ?? Category::factory()->accessoryMouseCategory(); + }, + 'manufacturer_id' => function () { + return Manufacturer::where('name', 'Apple')->first() ?? Manufacturer::factory()->apple(); + }, 'qty' => 13, 'min_amt' => 2, - 'supplier_id' => rand(1, 5), + 'supplier_id' => Supplier::factory(), ]; }); } @@ -87,8 +103,12 @@ class AccessoryFactory extends Factory return [ 'name' => 'Sculpt Comfort Mouse', 'image' => 'comfort-mouse.jpg', - 'category_id' => 9, - 'manufacturer_id' => 2, + 'category_id' => function () { + return Category::where('name', 'Mouse')->first() ?? Category::factory()->accessoryMouseCategory(); + }, + 'manufacturer_id' => function () { + return Manufacturer::where('name', 'Microsoft')->first() ?? Manufacturer::factory()->microsoft(); + }, 'qty' => 13, 'min_amt' => 2, ]; diff --git a/database/factories/ActionlogFactory.php b/database/factories/ActionlogFactory.php index 382a6412c2..c25fdcc70a 100644 --- a/database/factories/ActionlogFactory.php +++ b/database/factories/ActionlogFactory.php @@ -4,20 +4,12 @@ namespace Database\Factories; use App\Models\Actionlog; use App\Models\Asset; -use App\Models\Company; +use App\Models\License; +use App\Models\LicenseSeat; use App\Models\Location; use App\Models\User; use Illuminate\Database\Eloquent\Factories\Factory; -/* -|-------------------------------------------------------------------------- -| Action Log Factories -|-------------------------------------------------------------------------- -| -| This simulates checkin/checkout/etc activities -| -*/ - class ActionlogFactory extends Factory { /** @@ -25,7 +17,7 @@ class ActionlogFactory extends Factory * * @var string */ - protected $model = \App\Models\Actionlog::class; + protected $model = Actionlog::class; /** * Define the model's default state. @@ -34,71 +26,83 @@ class ActionlogFactory extends Factory */ public function definition() { - $asset = \App\Models\Asset::factory()->create(); return [ - 'item_type' => get_class($asset), - 'item_id' => 1, - 'user_id' => 1, + 'item_id' => Asset::factory(), + 'item_type' => Asset::class, + 'user_id' => User::factory()->superuser(), 'action_type' => 'uploaded', ]; } - public function assetCheckoutToUser() { return $this->state(function () { - $target = \App\Models\User::inRandomOrder()->first(); - $item = \App\Models\Asset::RTD()->inRandomOrder()->first(); - $user_id = rand(1, 2); // keep it simple - make it one of the two superadmins - $asset = Asset::where('id', $item->id) - ->update( + $target = User::inRandomOrder()->first(); + $asset = Asset::RTD()->inRandomOrder()->first(); + + $asset->update( [ 'assigned_to' => $target->id, - 'assigned_type' => \App\Models\User::class, + 'assigned_type' => User::class, 'location_id' => $target->location_id, ] ); return [ 'created_at' => $this->faker->dateTimeBetween('-1 years', 'now', date_default_timezone_get()), - 'user_id' => $user_id, 'action_type' => 'checkout', - 'item_id' => $item->id, - 'item_type' => \App\Models\Asset::class, + 'item_id' => $asset->id, + 'item_type' => Asset::class, 'target_id' => $target->id, - 'target_type' => get_class($target), - + 'target_type' => User::class, ]; }); } - public function assetCheckoutToLocation() { return $this->state(function () { - $target = \App\Models\Location::inRandomOrder()->first(); - $item = \App\Models\Asset::inRandomOrder()->RTD()->first(); - $user_id = rand(1, 2); // keep it simple - make it one of the two superadmins - $asset = \App\Models\Asset::where('id', $item->id) - ->update( + $target = Location::inRandomOrder()->first(); + $asset = Asset::inRandomOrder()->RTD()->first(); + + $asset->update( [ 'assigned_to' => $target->id, - 'assigned_type' => \App\Models\Location::class, + 'assigned_type' => Location::class, 'location_id' => $target->id, ] ); return [ 'created_at' => $this->faker->dateTimeBetween('-1 years', 'now', date_default_timezone_get()), - 'user_id' => $user_id, 'action_type' => 'checkout', - 'item_id' => $item->id, - 'item_type' => \App\Models\Asset::class, + 'item_id' => $asset->id, + 'item_type' => Asset::class, 'target_id' => $target->id, - 'target_type' => get_class($target), + 'target_type' => Location::class, ]; }); } + public function licenseCheckoutToUser() + { + return $this->state(function () { + $target = User::inRandomOrder()->first(); + $licenseSeat = LicenseSeat::whereNull('assigned_to')->inRandomOrder()->first(); + $licenseSeat->update([ + 'assigned_to' => $target->id, + 'user_id' => 1, // not ideal but works + ]); + + return [ + 'created_at' => $this->faker->dateTimeBetween('-1 years', 'now', date_default_timezone_get()), + 'action_type' => 'checkout', + 'item_id' => $licenseSeat->license->id, + 'item_type' => License::class, + 'target_id' => $target->id, + 'target_type' => User::class, + ]; + }); + } } diff --git a/database/factories/AssetFactory.php b/database/factories/AssetFactory.php index 6b77b8b078..cd3e0f8391 100644 --- a/database/factories/AssetFactory.php +++ b/database/factories/AssetFactory.php @@ -4,22 +4,12 @@ namespace Database\Factories; use App\Models\Asset; use App\Models\AssetModel; -use App\Models\Category; use App\Models\Location; +use App\Models\Statuslabel; use App\Models\Supplier; +use App\Models\User; use Illuminate\Database\Eloquent\Factories\Factory; -/* -|-------------------------------------------------------------------------- -| Model Factories -|-------------------------------------------------------------------------- -| -| Factories related exclusively to modelling assets. -| -*/ - -// These are just for unit tests, not to generate data - class AssetFactory extends Factory { /** @@ -38,10 +28,13 @@ class AssetFactory extends Factory { return [ 'name' => null, + 'model_id' => AssetModel::factory(), 'rtd_location_id' => Location::factory(), 'serial' => $this->faker->uuid(), - 'status_id' => 1, - 'user_id' => 1, + 'status_id' => function () { + return Statuslabel::where('name', 'Ready to Deploy')->first() ?? Statuslabel::factory()->rtd()->create(['name' => 'Ready to Deploy']); + }, + 'user_id' => User::factory()->superuser(), 'asset_tag' => $this->faker->unixTime('now'), 'notes' => 'Created by DB seeder', 'purchase_date' => $this->faker->dateTimeBetween('-1 years', 'now', date_default_timezone_get())->format('Y-m-d'), @@ -60,7 +53,9 @@ class AssetFactory extends Factory { return $this->state(function () { return [ - 'model_id' => 1, + 'model_id' => function () { + return AssetModel::where('name', 'Macbook Pro 13"')->first() ?? AssetModel::factory()->mbp13Model(); + }, ]; }); } @@ -69,8 +64,12 @@ class AssetFactory extends Factory { return $this->state(function () { return [ - 'model_id' => 1, - 'status_id' => 2, + 'model_id' => function () { + return AssetModel::where('name', 'Macbook Pro 13"')->first() ?? AssetModel::factory()->mbp13Model(); + }, + 'status_id' => function () { + return Statuslabel::where('name', 'Pending')->first() ?? Statuslabel::factory()->pending()->make(['name' => 'Pending']); + }, ]; }); } @@ -79,8 +78,12 @@ class AssetFactory extends Factory { return $this->state(function () { return [ - 'model_id' => 1, - 'status_id' => 3, + 'model_id' => function () { + return AssetModel::where('name', 'Macbook Pro 13"')->first() ?? AssetModel::factory()->mbp13Model(); + }, + 'status_id' => function () { + return Statuslabel::where('name', 'Archived')->first() ?? Statuslabel::factory()->archived()->make(['name' => 'Archived']); + }, ]; }); } @@ -89,7 +92,9 @@ class AssetFactory extends Factory { return $this->state(function () { return [ - 'model_id' => 2, + 'model_id' => function () { + return AssetModel::where('name', 'Macbook Air')->first() ?? AssetModel::factory()->mbpAirModel(); + }, ]; }); } @@ -98,7 +103,9 @@ class AssetFactory extends Factory { return $this->state(function () { return [ - 'model_id' => 3, + 'model_id' => function () { + return AssetModel::where('name', 'Surface')->first() ?? AssetModel::factory()->surfaceModel(); + }, ]; }); } @@ -107,7 +114,9 @@ class AssetFactory extends Factory { return $this->state(function () { return [ - 'model_id' => 4, + 'model_id' => function () { + return AssetModel::where('name', 'XPS 13')->first() ?? AssetModel::factory()->xps13Model(); + }, ]; }); } @@ -116,7 +125,9 @@ class AssetFactory extends Factory { return $this->state(function () { return [ - 'model_id' => 5, + 'model_id' => function () { + return AssetModel::where('name', 'Spectre')->first() ?? AssetModel::factory()->spectreModel(); + }, ]; }); } @@ -125,7 +136,9 @@ class AssetFactory extends Factory { return $this->state(function () { return [ - 'model_id' => 6, + 'model_id' => function () { + return AssetModel::where('name', 'ZenBook UX310')->first() ?? AssetModel::factory()->zenbookModel(); + }, ]; }); } @@ -134,7 +147,9 @@ class AssetFactory extends Factory { return $this->state(function () { return [ - 'model_id' => 7, + 'model_id' => function () { + return AssetModel::where('name', 'Yoga 910')->first() ?? AssetModel::factory()->yogaModel(); + }, ]; }); } @@ -143,7 +158,9 @@ class AssetFactory extends Factory { return $this->state(function () { return [ - 'model_id' => 8, + 'model_id' => function () { + return AssetModel::where('name', 'iMac Pro')->first() ?? AssetModel::factory()->macproModel(); + }, ]; }); } @@ -152,7 +169,9 @@ class AssetFactory extends Factory { return $this->state(function () { return [ - 'model_id' => 9, + 'model_id' => function () { + return AssetModel::where('name', 'Lenovo Intel Core i5')->first() ?? AssetModel::factory()->lenovoI5Model(); + }, ]; }); } @@ -161,7 +180,9 @@ class AssetFactory extends Factory { return $this->state(function () { return [ - 'model_id' => 10, + 'model_id' => function () { + return AssetModel::where('name', 'OptiPlex')->first() ?? AssetModel::factory()->optiplexModel(); + }, ]; }); } @@ -170,7 +191,9 @@ class AssetFactory extends Factory { return $this->state(function () { return [ - 'model_id' => 11, + 'model_id' => function () { + return AssetModel::where('name', 'SoundStation 2')->first() ?? AssetModel::factory()->polycomModel(); + }, ]; }); } @@ -179,7 +202,9 @@ class AssetFactory extends Factory { return $this->state(function () { return [ - 'model_id' => 12, + 'model_id' => function () { + return AssetModel::where('name', 'Polycom CX3000 IP Conference Phone')->first() ?? AssetModel::factory()->polycomcxModel(); + }, ]; }); } @@ -188,7 +213,9 @@ class AssetFactory extends Factory { return $this->state(function () { return [ - 'model_id' => 13, + 'model_id' => function () { + return AssetModel::where('name', 'iPad Pro')->first() ?? AssetModel::factory()->ipadModel(); + }, ]; }); } @@ -197,7 +224,9 @@ class AssetFactory extends Factory { return $this->state(function () { return [ - 'model_id' => 14, + 'model_id' => function () { + return AssetModel::where('name', 'Tab3')->first() ?? AssetModel::factory()->tab3Model(); + }, ]; }); } @@ -206,7 +235,9 @@ class AssetFactory extends Factory { return $this->state(function () { return [ - 'model_id' => 15, + 'model_id' => function () { + return AssetModel::where('name', 'iPhone 11')->first() ?? AssetModel::factory()->iphone11Model(); + }, ]; }); } @@ -215,7 +246,9 @@ class AssetFactory extends Factory { return $this->state(function () { return [ - 'model_id' => 16, + 'model_id' => function () { + return AssetModel::where('name', 'iPhone 12')->first() ?? AssetModel::factory()->iphone12Model(); + }, ]; }); } @@ -224,7 +257,9 @@ class AssetFactory extends Factory { return $this->state(function () { return [ - 'model_id' => 17, + 'model_id' => function () { + return AssetModel::where('name', 'Ultrafine 4k')->first() ?? AssetModel::factory()->ultrafine(); + }, ]; }); } @@ -233,7 +268,9 @@ class AssetFactory extends Factory { return $this->state(function () { return [ - 'model_id' => 18, + 'model_id' => function () { + return AssetModel::where('name', 'Ultrasharp U2415')->first() ?? AssetModel::factory()->ultrasharp(); + }, ]; }); } @@ -242,9 +279,8 @@ class AssetFactory extends Factory { return $this->state(function () { return [ - 'model_id' => 1, - 'assigned_to' => \App\Models\User::factory()->create()->id, - 'assigned_type' => \App\Models\User::class, + 'assigned_to' => User::factory(), + 'assigned_type' => User::class, ]; }); } @@ -253,9 +289,8 @@ class AssetFactory extends Factory { return $this->state(function () { return [ - 'model_id' => 1, - 'assigned_to' => \App\Models\Location::factory()->create()->id, - 'assigned_type' => \App\Models\Location::class, + 'assigned_to' => Location::factory(), + 'assigned_type' => Location::class, ]; }); } @@ -265,8 +300,8 @@ class AssetFactory extends Factory return $this->state(function () { return [ 'model_id' => 1, - 'assigned_to' => \App\Models\Asset::factory()->create()->id, - 'assigned_type' => \App\Models\Asset::class, + 'assigned_to' => Asset::factory(), + 'assigned_type' => Asset::class, ]; }); } @@ -275,7 +310,9 @@ class AssetFactory extends Factory { return $this->state(function () { return [ - 'model_id' => 1, + 'model_id' => function () { + return AssetModel::where('name', 'Macbook Pro 13')->first() ?? AssetModel::factory()->mbp13Model(); + }, ]; }); } @@ -284,7 +321,9 @@ class AssetFactory extends Factory { return $this->state(function () { return [ - 'model_id' => 1, + 'model_id' => function () { + return AssetModel::where('name', 'Macbook Pro 13')->first() ?? AssetModel::factory()->mbp13Model(); + }, 'deleted_at' => $this->faker->dateTime(), ]; }); diff --git a/database/factories/AssetMaintenanceFactory.php b/database/factories/AssetMaintenanceFactory.php index f9351f5188..ada73f7ed8 100644 --- a/database/factories/AssetMaintenanceFactory.php +++ b/database/factories/AssetMaintenanceFactory.php @@ -3,19 +3,10 @@ namespace Database\Factories; use App\Models\Asset; -use App\Models\AssetModel; -use App\Models\Category; +use App\Models\AssetMaintenance; +use App\Models\Supplier; use Illuminate\Database\Eloquent\Factories\Factory; -/* -|-------------------------------------------------------------------------- -| Model Factories -|-------------------------------------------------------------------------- -| -| Factories related exclusively to modelling assets. -| -*/ - class AssetMaintenanceFactory extends Factory { /** @@ -23,7 +14,7 @@ class AssetMaintenanceFactory extends Factory * * @var string */ - protected $model = \App\Models\AssetMaintenance::class; + protected $model = AssetMaintenance::class; /** * Define the model's default state. @@ -33,14 +24,10 @@ class AssetMaintenanceFactory extends Factory public function definition() { return [ - 'asset_id' => function () { - return \App\Models\Asset::factory()->create()->id; - }, - 'supplier_id' => function () { - return \App\Models\Supplier::factory()->create()->id; - }, + 'asset_id' => Asset::factory(), + 'supplier_id' => Supplier::factory(), 'asset_maintenance_type' => $this->faker->randomElement(['maintenance', 'repair', 'upgrade']), - 'title' => $this->faker->sentence, + 'title' => $this->faker->sentence(), 'start_date' => $this->faker->date(), 'is_warranty' => $this->faker->boolean(), 'notes' => $this->faker->paragraph(), diff --git a/database/factories/AssetModelFactory.php b/database/factories/AssetModelFactory.php index 187bc539fc..4881d6560b 100644 --- a/database/factories/AssetModelFactory.php +++ b/database/factories/AssetModelFactory.php @@ -2,68 +2,14 @@ namespace Database\Factories; +use App\Models\AssetModel; +use App\Models\CustomFieldset; +use App\Models\Depreciation; +use App\Models\Manufacturer; +use App\Models\User; use Illuminate\Database\Eloquent\Factories\Factory; use App\Models\Category; -/* -|-------------------------------------------------------------------------- -| Asset Model Factories -|-------------------------------------------------------------------------- -| -| Factories related exclusively to creating models .. -| -*/ - -/* -|-------------------------------------------------------------------------- -| Laptops -|-------------------------------------------------------------------------- -*/ - -// 1 - -// 2 - -// 3 - -// 4 - -// 5 - -// 6 - -// 7 - -/* -|-------------------------------------------------------------------------- -| Desktops -|-------------------------------------------------------------------------- -*/ - -/* -|-------------------------------------------------------------------------- -| Conference Phones -|-------------------------------------------------------------------------- -*/ - -/* -|-------------------------------------------------------------------------- -| Tablets -|-------------------------------------------------------------------------- -*/ - -/* -|-------------------------------------------------------------------------- -| Mobile Phones -|-------------------------------------------------------------------------- -*/ - -/* -|-------------------------------------------------------------------------- -| Displays -|-------------------------------------------------------------------------- -*/ - class AssetModelFactory extends Factory { /** @@ -71,7 +17,7 @@ class AssetModelFactory extends Factory * * @var string */ - protected $model = \App\Models\AssetModel::class; + protected $model = AssetModel::class; /** * Define the model's default state. @@ -81,8 +27,9 @@ class AssetModelFactory extends Factory public function definition() { return [ - 'user_id' => 1, + 'user_id' => User::factory()->superuser(), 'name' => $this->faker->catchPhrase(), + 'category_id' => Category::factory(), 'model_number' => $this->faker->creditCardNumber(), 'notes' => 'Created by demo seeder', @@ -94,11 +41,17 @@ class AssetModelFactory extends Factory return $this->state(function () { return [ 'name' => 'Macbook Pro 13"', - 'category_id' => 1, + 'category_id' => function () { + return Category::where('name', 'Laptops')->first() ?? Category::factory()->assetLaptopCategory(); + }, 'eol' => '36', - 'depreciation_id' => 1, + 'depreciation_id' => function () { + return Depreciation::where('name', 'Computer Depreciation')->first() ?? Depreciation::factory()->computer(); + }, 'image' => 'mbp.jpg', - 'fieldset_id' => 2, + 'fieldset_id' => function () { + return CustomFieldset::where('name', 'Laptops and Desktops')->first() ?? CustomFieldset::factory()->computer(); + }, ]; }); } @@ -108,12 +61,20 @@ class AssetModelFactory extends Factory return $this->state(function () { return [ 'name' => 'Macbook Air', - 'category_id' => 1, - 'manufacturer_id' => 1, + 'category_id' => function () { + return Category::where('name', 'Laptops')->first() ?? Category::factory()->assetLaptopCategory(); + }, + 'manufacturer_id' => function () { + return Manufacturer::where('name', 'Apple')->first() ?? Manufacturer::factory()->apple(); + }, 'eol' => '36', - 'depreciation_id' => 1, + 'depreciation_id' => function () { + return Depreciation::where('name', 'Computer Depreciation')->first() ?? Depreciation::factory()->computer(); + }, 'image' => 'macbookair.jpg', - 'fieldset_id' => 2, + 'fieldset_id' => function () { + return CustomFieldset::where('name', 'Laptops and Desktops')->first() ?? CustomFieldset::factory()->computer(); + }, ]; }); } @@ -123,12 +84,20 @@ class AssetModelFactory extends Factory return $this->state(function () { return [ 'name' => 'Surface', - 'category_id' => 1, - 'manufacturer_id' => 2, + 'category_id' => function () { + return Category::where('name', 'Laptops')->first() ?? Category::factory()->assetLaptopCategory(); + }, + 'manufacturer_id' => function () { + return Manufacturer::where('name', 'Microsoft')->first() ?? Manufacturer::factory()->microsoft(); + }, 'eol' => '36', - 'depreciation_id' => 1, + 'depreciation_id' => function () { + return Depreciation::where('name', 'Computer Depreciation')->first() ?? Depreciation::factory()->computer(); + }, 'image' => 'surface.jpg', - 'fieldset_id' => 2, + 'fieldset_id' => function () { + return CustomFieldset::where('name', 'Laptops and Desktops')->first() ?? CustomFieldset::factory()->computer(); + }, ]; }); } @@ -138,12 +107,20 @@ class AssetModelFactory extends Factory return $this->state(function () { return [ 'name' => 'XPS 13', - 'category_id' => 1, - 'manufacturer_id' => 3, + 'category_id' => function () { + return Category::where('name', 'Laptops')->first() ?? Category::factory()->assetLaptopCategory(); + }, + 'manufacturer_id' => function () { + return Manufacturer::where('name', 'Dell')->first() ?? Manufacturer::factory()->dell(); + }, 'eol' => '36', - 'depreciation_id' => 1, + 'depreciation_id' => function () { + return Depreciation::where('name', 'Computer Depreciation')->first() ?? Depreciation::factory()->computer(); + }, 'image' => 'xps.jpg', - 'fieldset_id' => 2, + 'fieldset_id' => function () { + return CustomFieldset::where('name', 'Laptops and Desktops')->first() ?? CustomFieldset::factory()->computer(); + }, ]; }); } @@ -153,12 +130,20 @@ class AssetModelFactory extends Factory return $this->state(function () { return [ 'name' => 'ZenBook UX310', - 'category_id' => 1, - 'manufacturer_id' => 4, + 'category_id' => function () { + return Category::where('name', 'Laptops')->first() ?? Category::factory()->assetLaptopCategory(); + }, + 'manufacturer_id' => function () { + return Manufacturer::where('name', 'Asus')->first() ?? Manufacturer::factory()->asus(); + }, 'eol' => '36', - 'depreciation_id' => 1, + 'depreciation_id' => function () { + return Depreciation::where('name', 'Computer Depreciation')->first() ?? Depreciation::factory()->computer(); + }, 'image' => 'zenbook.jpg', - 'fieldset_id' => 2, + 'fieldset_id' => function () { + return CustomFieldset::where('name', 'Laptops and Desktops')->first() ?? CustomFieldset::factory()->computer(); + }, ]; }); } @@ -168,12 +153,20 @@ class AssetModelFactory extends Factory return $this->state(function () { return [ 'name' => 'Spectre', - 'category_id' => 1, - 'manufacturer_id' => 5, + 'category_id' => function () { + return Category::where('name', 'Laptops')->first() ?? Category::factory()->assetLaptopCategory(); + }, + 'manufacturer_id' => function () { + return Manufacturer::where('name', 'HP')->first() ?? Manufacturer::factory()->hp(); + }, 'eol' => '36', - 'depreciation_id' => 1, + 'depreciation_id' => function () { + return Depreciation::where('name', 'Computer Depreciation')->first() ?? Depreciation::factory()->computer(); + }, 'image' => 'spectre.jpg', - 'fieldset_id' => 2, + 'fieldset_id' => function () { + return CustomFieldset::where('name', 'Laptops and Desktops')->first() ?? CustomFieldset::factory()->computer(); + }, ]; }); } @@ -183,12 +176,20 @@ class AssetModelFactory extends Factory return $this->state(function () { return [ 'name' => 'Yoga 910', - 'category_id' => 1, - 'manufacturer_id' => 6, + 'category_id' => function () { + return Category::where('name', 'Laptops')->first() ?? Category::factory()->assetLaptopCategory(); + }, + 'manufacturer_id' => function () { + return Manufacturer::where('name', 'Lenovo')->first() ?? Manufacturer::factory()->lenovo(); + }, 'eol' => '36', - 'depreciation_id' => 1, + 'depreciation_id' => function () { + return Depreciation::where('name', 'Computer Depreciation')->first() ?? Depreciation::factory()->computer(); + }, 'image' => 'yoga.png', - 'fieldset_id' => 2, + 'fieldset_id' => function () { + return CustomFieldset::where('name', 'Laptops and Desktops')->first() ?? CustomFieldset::factory()->computer(); + }, ]; }); } @@ -198,12 +199,20 @@ class AssetModelFactory extends Factory return $this->state(function () { return [ 'name' => 'iMac Pro', - 'category_id' => 2, - 'manufacturer_id' => 1, + 'category_id' => function (){ + return Category::where('name', 'Desktops')->first() ?? Category::factory()->assetDesktopCategory(); + }, + 'manufacturer_id' => function () { + return Manufacturer::where('name', 'Apple')->first() ?? Manufacturer::factory()->apple(); + }, 'eol' => '24', - 'depreciation_id' => 1, + 'depreciation_id' => function () { + return Depreciation::where('name', 'Computer Depreciation')->first() ?? Depreciation::factory()->computer(); + }, 'image' => 'imacpro.jpg', - 'fieldset_id' => 2, + 'fieldset_id' => function () { + return CustomFieldset::where('name', 'Laptops and Desktops')->first() ?? CustomFieldset::factory()->computer(); + }, ]; }); } @@ -213,12 +222,20 @@ class AssetModelFactory extends Factory return $this->state(function () { return [ 'name' => 'Lenovo Intel Core i5', - 'category_id' => 2, - 'manufacturer_id' => 6, + 'category_id' => function () { + return Category::where('name', 'Desktops')->first() ?? Category::factory()->assetDesktopCategory(); + }, + 'manufacturer_id' => function () { + return Manufacturer::where('name', 'Lenovo')->first() ?? Manufacturer::factory()->lenovo(); + }, 'eol' => '24', - 'depreciation_id' => 1, + 'depreciation_id' => function () { + return Depreciation::where('name', 'Computer Depreciation')->first() ?? Depreciation::factory()->computer(); + }, 'image' => 'lenovoi5.png', - 'fieldset_id' => 2, + 'fieldset_id' => function () { + return CustomFieldset::where('name', 'Laptops and Desktops')->first() ?? CustomFieldset::factory()->computer(); + }, ]; }); } @@ -228,13 +245,21 @@ class AssetModelFactory extends Factory return $this->state(function () { return [ 'name' => 'OptiPlex', - 'category_id' => 2, - 'manufacturer_id' => 3, + 'category_id' => function (){ + return Category::where('name', 'Desktops')->first() ?? Category::factory()->assetDesktopCategory(); + }, + 'manufacturer_id' => function () { + return Manufacturer::where('name', 'Dell')->first() ?? Manufacturer::factory()->dell(); + }, 'model_number' => '5040 (MRR81)', 'eol' => '24', - 'depreciation_id' => 1, + 'depreciation_id' => function () { + return Depreciation::where('name', 'Computer Depreciation')->first() ?? Depreciation::factory()->computer(); + }, 'image' => 'optiplex.jpg', - 'fieldset_id' => 2, + 'fieldset_id' => function () { + return CustomFieldset::where('name', 'Laptops and Desktops')->first() ?? CustomFieldset::factory()->computer(); + }, ]; }); } @@ -244,10 +269,16 @@ class AssetModelFactory extends Factory return $this->state(function () { return [ 'name' => 'SoundStation 2', - 'category_id' => 6, - 'manufacturer_id' => 8, + 'category_id' => function () { + return Category::where('name', 'VOIP Phones')->first() ?? Category::factory()->assetVoipCategory(); + }, + 'manufacturer_id' => function () { + return Manufacturer::where('name', 'Polycom')->first() ?? Manufacturer::factory()->polycom(); + }, 'eol' => '12', - 'depreciation_id' => 1, + 'depreciation_id' => function () { + return Depreciation::where('name', 'Computer Depreciation')->first() ?? Depreciation::factory()->computer(); + }, 'image' => 'soundstation.jpg', ]; }); @@ -258,10 +289,16 @@ class AssetModelFactory extends Factory return $this->state(function () { return [ 'name' => 'Polycom CX3000 IP Conference Phone', - 'category_id' => 6, - 'manufacturer_id' => 8, + 'category_id' => function () { + return Category::where('name', 'VOIP Phones')->first() ?? Category::factory()->assetVoipCategory(); + }, + 'manufacturer_id' => function () { + return Manufacturer::where('name', 'Polycom')->first() ?? Manufacturer::factory()->polycom(); + }, 'eol' => '12', - 'depreciation_id' => 1, + 'depreciation_id' => function () { + return Depreciation::where('name', 'Computer Depreciation')->first() ?? Depreciation::factory()->computer(); + }, 'image' => 'cx3000.png', ]; }); @@ -272,10 +309,16 @@ class AssetModelFactory extends Factory return $this->state(function () { return [ 'name' => 'iPad Pro', - 'category_id' => 3, - 'manufacturer_id' => 1, + 'category_id' => function () { + return Category::where('name', 'Tablets')->first() ?? Category::factory()->assetTabletCategory(); + }, + 'manufacturer_id' => function () { + return Manufacturer::where('name', 'Apple')->first() ?? Manufacturer::factory()->apple(); + }, 'eol' => '12', - 'depreciation_id' => 1, + 'depreciation_id' => function () { + return Depreciation::where('name', 'Computer Depreciation')->first() ?? Depreciation::factory()->computer(); + }, 'image' => 'ipad.jpg', ]; }); @@ -286,10 +329,16 @@ class AssetModelFactory extends Factory return $this->state(function () { return [ 'name' => 'Tab3', - 'category_id' => 3, - 'manufacturer_id' => 6, + 'category_id' => function () { + return Category::where('name', 'Tablets')->first() ?? Category::factory()->assetTabletCategory(); + }, + 'manufacturer_id' => function () { + return Manufacturer::where('name', 'Lenovo')->first() ?? Manufacturer::factory()->lenovo(); + }, 'eol' => '12', - 'depreciation_id' => 1, + 'depreciation_id' => function () { + return Depreciation::where('name', 'Computer Depreciation')->first() ?? Depreciation::factory()->computer(); + }, 'image' => 'tab3.png', ]; }); @@ -300,12 +349,20 @@ class AssetModelFactory extends Factory return $this->state(function () { return [ 'name' => 'iPhone 11', - 'category_id' => 4, - 'manufacturer_id' => 1, + 'category_id' => function () { + return Category::where('name', 'Mobile Phones')->first() ?? Category::factory()->assetMobileCategory(); + }, + 'manufacturer_id' => function () { + return Manufacturer::where('name', 'Apple')->first() ?? Manufacturer::factory()->apple(); + }, 'eol' => '12', - 'depreciation_id' => 3, + 'depreciation_id' => function () { + return Depreciation::where('name', 'Mobile Phone Depreciation')->first() ?? Depreciation::factory()->mobilePhones(); + }, 'image' => 'iphone11.jpeg', - 'fieldset_id' => 1, + 'fieldset_id' => function () { + return CustomFieldset::where('name', 'Mobile Devices')->first() ?? CustomFieldset::factory()->mobile(); + }, ]; }); } @@ -315,12 +372,20 @@ class AssetModelFactory extends Factory return $this->state(function () { return [ 'name' => 'iPhone 12', - 'category_id' => 4, - 'manufacturer_id' => 1, + 'category_id' => function () { + return Category::where('name', 'Mobile Phones')->first() ?? Category::factory()->assetMobileCategory(); + }, + 'manufacturer_id' => function () { + return Manufacturer::where('name', 'Apple')->first() ?? Manufacturer::factory()->apple(); + }, 'eol' => '12', - 'depreciation_id' => 1, + 'depreciation_id' => function () { + return Depreciation::where('name', 'Computer Depreciation')->first() ?? Depreciation::factory()->computer(); + }, 'image' => 'iphone12.jpeg', - 'fieldset_id' => 1, + 'fieldset_id' => function () { + return CustomFieldset::where('name', 'Mobile Devices')->first() ?? CustomFieldset::factory()->mobile(); + }, ]; }); } @@ -330,10 +395,16 @@ class AssetModelFactory extends Factory return $this->state(function () { return [ 'name' => 'Ultrafine 4k', - 'category_id' => 5, - 'manufacturer_id' => 7, + 'category_id' => function () { + return Category::where('name', 'Displays')->first() ?? Category::factory()->assetDisplayCategory(); + }, + 'manufacturer_id' => function () { + return Manufacturer::where('name', 'LG')->first() ?? Manufacturer::factory()->lg(); + }, 'eol' => '12', - 'depreciation_id' => 2, + 'depreciation_id' => function () { + return Depreciation::where('name', 'Display Depreciation')->first() ?? Depreciation::factory()->display(); + }, 'image' => 'ultrafine.jpg', ]; }); @@ -344,10 +415,16 @@ class AssetModelFactory extends Factory return $this->state(function () { return [ 'name' => 'Ultrasharp U2415', - 'category_id' => 5, - 'manufacturer_id' => 3, + 'category_id' => function () { + return Category::where('name', 'Displays')->first() ?? Category::factory()->assetDisplayCategory(); + }, + 'manufacturer_id' => function () { + return Manufacturer::where('name', 'Dell')->first() ?? Manufacturer::factory()->dell(); + }, 'eol' => '12', - 'depreciation_id' => 2, + 'depreciation_id' => function () { + return Depreciation::where('name', 'Display Depreciation')->first() ?? Depreciation::factory()->display(); + }, 'image' => 'ultrasharp.jpg', ]; }); diff --git a/database/factories/CategoryFactory.php b/database/factories/CategoryFactory.php index 8f9dd90e3f..94a9626da4 100644 --- a/database/factories/CategoryFactory.php +++ b/database/factories/CategoryFactory.php @@ -2,18 +2,10 @@ namespace Database\Factories; +use App\Models\User; use Illuminate\Database\Eloquent\Factories\Factory; use App\Models\Category; -/* -|-------------------------------------------------------------------------- -| Category Factories -|-------------------------------------------------------------------------- -| -| Factories related exclusively to creating categories and the various states.. -| -*/ - class CategoryFactory extends Factory { /** @@ -21,7 +13,7 @@ class CategoryFactory extends Factory * * @var string */ - protected $model = \App\Models\Category::class; + protected $model = Category::class; /** * Define the model's default state. @@ -32,18 +24,19 @@ class CategoryFactory extends Factory { return [ 'name' => $this->faker->catchPhrase(), + 'category_type' => 'asset', 'checkin_email' => $this->faker->boolean(), 'eula_text' => $this->faker->paragraph(), 'require_acceptance' => false, 'use_default_eula' => $this->faker->boolean(), - 'user_id' => 1, + 'user_id' => User::factory()->superuser(), ]; } // usage: Category::factory()->assetLaptopCategory(); public function assetLaptopCategory() { - return Category::factory()->create([ + return $this->state([ 'name' => 'Laptops', 'category_type' => 'asset', 'require_acceptance' => true, @@ -53,7 +46,7 @@ class CategoryFactory extends Factory // usage: Category::factory()->assetDesktopCategory(); public function assetDesktopCategory() { - return Category::factory()->create([ + return $this->state([ 'name' => 'Desktops', 'category_type' => 'asset', 'require_acceptance' => true, @@ -63,7 +56,7 @@ class CategoryFactory extends Factory // usage: Category::factory()->assetDisplayCategory(); public function assetDisplayCategory() { - return Category::factory()->create([ + return $this->state([ 'name' => 'Displays', 'category_type' => 'asset', ]); @@ -72,7 +65,7 @@ class CategoryFactory extends Factory // usage: Category::factory()->assetTabletCategory(); public function assetTabletCategory() { - return Category::factory()->create([ + return $this->state([ 'name' => 'Tablets', 'category_type' => 'asset', ]); @@ -81,7 +74,7 @@ class CategoryFactory extends Factory // usage: Category::factory()->assetMobileCategory(); public function assetMobileCategory() { - return Category::factory()->create([ + return $this->state([ 'name' => 'Mobile Phones', 'category_type' => 'asset', ]); @@ -90,7 +83,7 @@ class CategoryFactory extends Factory // usage: Category::factory()->assetConferenceCategory(); public function assetConferenceCategory() { - return Category::factory()->create([ + return $this->state([ 'name' => 'Conference Phones', 'category_type' => 'asset', ]); @@ -100,7 +93,7 @@ class CategoryFactory extends Factory // usage: Category::factory()->assetVoipCategory(); public function assetVoipCategory() { - return Category::factory()->create([ + return $this->state([ 'name' => 'VOIP Phones', 'category_type' => 'asset', ]); @@ -109,8 +102,8 @@ class CategoryFactory extends Factory // usage: Category::factory()->accessoryKeyboardCategory(); public function accessoryKeyboardCategory() { - return Category::factory()->create([ - 'name' => 'Keyboardss', + return $this->state([ + 'name' => 'Keyboards', 'category_type' => 'accessory', ]); } @@ -119,7 +112,7 @@ class CategoryFactory extends Factory // usage: Category::factory()->accessoryMouseCategory(); public function accessoryMouseCategory() { - return Category::factory()->create([ + return $this->state([ 'name' => 'Mouse', 'category_type' => 'accessory', ]); @@ -128,7 +121,7 @@ class CategoryFactory extends Factory // usage: Category::factory()->componentHddCategory(); public function componentHddCategory() { - return Category::factory()->create([ + return $this->state([ 'name' => 'HDD/SSD', 'category_type' => 'component', ]); @@ -137,7 +130,7 @@ class CategoryFactory extends Factory // usage: Category::factory()->componentRamCategory(); public function componentRamCategory() { - return Category::factory()->create([ + return $this->state([ 'name' => 'RAM', 'category_type' => 'component', ]); @@ -146,7 +139,7 @@ class CategoryFactory extends Factory // usage: Category::factory()->consumablePaperCategory(); public function consumablePaperCategory() { - return Category::factory()->create([ + return $this->state([ 'name' => 'Printer Paper', 'category_type' => 'consumable', ]); @@ -155,7 +148,7 @@ class CategoryFactory extends Factory // usage: Category::factory()->consumableInkCategory(); public function consumableInkCategory() { - return Category::factory()->create([ + return $this->state([ 'name' => 'Printer Ink', 'category_type' => 'consumable', ]); @@ -164,7 +157,7 @@ class CategoryFactory extends Factory // usage: Category::factory()->licenseGraphicsCategory(); public function licenseGraphicsCategory() { - return Category::factory()->create([ + return $this->state([ 'name' => 'Graphics Software', 'category_type' => 'license', ]); @@ -173,7 +166,7 @@ class CategoryFactory extends Factory // usage: Category::factory()->licenseGraphicsCategory(); public function licenseOfficeCategory() { - return Category::factory()->create([ + return $this->state([ 'name' => 'Office Software', 'category_type' => 'license', ]); diff --git a/database/factories/CompanyFactory.php b/database/factories/CompanyFactory.php index 695a2dbdb5..bf4b7ce242 100644 --- a/database/factories/CompanyFactory.php +++ b/database/factories/CompanyFactory.php @@ -1,25 +1,8 @@ $this->faker->company, + 'name' => $this->faker->company(), ]; } } diff --git a/database/factories/ComponentFactory.php b/database/factories/ComponentFactory.php index 33adfc993a..6db9ef24fa 100644 --- a/database/factories/ComponentFactory.php +++ b/database/factories/ComponentFactory.php @@ -2,17 +2,12 @@ namespace Database\Factories; +use App\Models\Category; +use App\Models\Company; +use App\Models\Component; +use App\Models\Location; use Illuminate\Database\Eloquent\Factories\Factory; -/* -|-------------------------------------------------------------------------- -| Components Factories -|-------------------------------------------------------------------------- -| -| Factories related exclusively to creating components .. -| -*/ - class ComponentFactory extends Factory { /** @@ -20,7 +15,7 @@ class ComponentFactory extends Factory * * @var string */ - protected $model = \App\Models\Component::class; + protected $model = Component::class; /** * Define the model's default state. @@ -31,19 +26,15 @@ class ComponentFactory extends Factory { return [ 'name' => $this->faker->text(20), - 'category_id' => function () { - return \App\Models\Category::factory()->create()->id; - }, - 'location_id' => 1, - 'serial' => $this->faker->uuid, + 'category_id' => Category::factory(), + 'location_id' => Location::factory(), + 'serial' => $this->faker->uuid(), 'qty' => $this->faker->numberBetween(3, 10), 'order_number' => $this->faker->numberBetween(1000000, 50000000), 'purchase_date' => $this->faker->dateTime()->format('Y-m-d'), 'purchase_cost' => $this->faker->randomFloat(2), 'min_amt' => $this->faker->numberBetween($min = 1, $max = 2), - 'company_id' => function () { - return \App\Models\Company::factory()->create()->id; - }, + 'company_id' => Company::factory(), ]; } @@ -52,11 +43,12 @@ class ComponentFactory extends Factory return $this->state(function () { return [ 'name' => 'Crucial 4GB DDR3L-1600 SODIMM', - 'category_id' => 13, + 'category_id' => function () { + return Category::where('name', 'RAM')->first() ?? Category::factory()->componentRamCategory(); + }, 'qty' => 10, 'min_amt' => 2, - 'location_id' => 3, - 'company_id' => 2, + 'location_id' => Location::factory(), ]; }); } @@ -66,7 +58,9 @@ class ComponentFactory extends Factory return $this->state(function () { return [ 'name' => 'Crucial 8GB DDR3L-1600 SODIMM Memory for Mac', - 'category_id' => 13, + 'category_id' => function () { + return Category::where('name', 'RAM')->first() ?? Category::factory()->componentRamCategory(); + }, 'qty' => 10, 'min_amt' => 2, ]; @@ -78,7 +72,9 @@ class ComponentFactory extends Factory return $this->state(function () { return [ 'name' => 'Crucial BX300 120GB SATA Internal SSD', - 'category_id' => 12, + 'category_id' => function () { + return Category::where('name', 'HDD/SSD')->first() ?? Category::factory()->componentHddCategory(); + }, 'qty' => 10, 'min_amt' => 2, ]; @@ -90,7 +86,9 @@ class ComponentFactory extends Factory return $this->state(function () { return [ 'name' => 'Crucial BX300 240GB SATA Internal SSD', - 'category_id' => 12, + 'category_id' => function () { + return Category::where('name', 'HDD/SSD')->first() ?? Category::factory()->componentHddCategory(); + }, 'qty' => 10, 'min_amt' => 2, ]; diff --git a/database/factories/ConsumableFactory.php b/database/factories/ConsumableFactory.php index 89acc144b7..8dd56229e0 100644 --- a/database/factories/ConsumableFactory.php +++ b/database/factories/ConsumableFactory.php @@ -2,17 +2,13 @@ namespace Database\Factories; +use App\Models\Category; +use App\Models\Company; +use App\Models\Consumable; +use App\Models\Manufacturer; +use App\Models\User; use Illuminate\Database\Eloquent\Factories\Factory; -/* -|-------------------------------------------------------------------------- -| Consumables Factories -|-------------------------------------------------------------------------- -| -| Factories related exclusively to creating consumables .. -| -*/ - class ConsumableFactory extends Factory { /** @@ -20,7 +16,7 @@ class ConsumableFactory extends Factory * * @var string */ - protected $model = \App\Models\Consumable::class; + protected $model = Consumable::class; /** * Define the model's default state. @@ -30,13 +26,16 @@ class ConsumableFactory extends Factory public function definition() { return [ - 'user_id' => 1, + 'name' => $this->faker->word(), + 'category_id' => Category::factory(), + 'user_id' => User::factory()->superuser(), 'item_no' => $this->faker->numberBetween(1000000, 50000000), 'order_number' => $this->faker->numberBetween(1000000, 50000000), 'purchase_date' => $this->faker->dateTimeBetween('-1 years', 'now', date_default_timezone_get())->format('Y-m-d'), 'purchase_cost' => $this->faker->randomFloat(2, 1, 50), 'qty' => $this->faker->numberBetween(5, 10), 'min_amt' => $this->faker->numberBetween($min = 1, $max = 2), + 'company_id' => Company::factory(), ]; } @@ -45,11 +44,14 @@ class ConsumableFactory extends Factory return $this->state(function () { return [ 'name' => 'Cardstock (White)', - 'category_id' => 10, - 'manufacturer_id' => 10, + 'category_id' => function () { + return Category::where('name', 'Printer Paper')->first() ?? Category::factory()->consumablePaperCategory(); + }, + 'manufacturer_id' => function () { + return Manufacturer::where('name', 'Avery')->first() ?? Manufacturer::factory()->avery(); + }, 'qty' => 10, 'min_amt' => 2, - 'company_id' => 3, ]; }); } @@ -59,8 +61,12 @@ class ConsumableFactory extends Factory return $this->state(function () { return [ 'name' => 'Laserjet Paper (Ream)', - 'category_id' => 10, - 'manufacturer_id' => 10, + 'category_id' => function () { + return Category::where('name', 'Printer Paper')->first() ?? Category::factory()->consumablePaperCategory(); + }, + 'manufacturer_id' => function () { + return Manufacturer::where('name', 'Avery')->first() ?? Manufacturer::factory()->avery(); + }, 'qty' => 20, 'min_amt' => 2, ]; @@ -72,8 +78,12 @@ class ConsumableFactory extends Factory return $this->state(function () { return [ 'name' => 'Laserjet Toner (black)', - 'category_id' => 11, - 'manufacturer_id' => 5, + 'category_id' => function () { + return Category::where('name', 'Printer Ink')->first() ?? Category::factory()->consumableInkCategory(); + }, + 'manufacturer_id' => function () { + return Manufacturer::where('name', 'HP')->first() ?? Manufacturer::factory()->hp(); + }, 'qty' => 20, 'min_amt' => 2, ]; diff --git a/database/factories/CustomFieldFactory.php b/database/factories/CustomFieldFactory.php index c964ea44d1..bfa41b4d8d 100644 --- a/database/factories/CustomFieldFactory.php +++ b/database/factories/CustomFieldFactory.php @@ -2,6 +2,7 @@ namespace Database\Factories; +use App\Models\CustomField; use Illuminate\Database\Eloquent\Factories\Factory; class CustomFieldFactory extends Factory @@ -11,7 +12,7 @@ class CustomFieldFactory extends Factory * * @var string */ - protected $model = \App\Models\CustomField::class; + protected $model = CustomField::class; /** * Define the model's default state. @@ -21,7 +22,7 @@ class CustomFieldFactory extends Factory public function definition() { return [ - 'name' => $this->faker->catchPhrase, + 'name' => $this->faker->catchPhrase(), 'format' => '', 'element' => 'text', ]; diff --git a/database/factories/CustomFieldsetFactory.php b/database/factories/CustomFieldsetFactory.php index 1dd834d052..f369121c8f 100644 --- a/database/factories/CustomFieldsetFactory.php +++ b/database/factories/CustomFieldsetFactory.php @@ -2,6 +2,7 @@ namespace Database\Factories; +use App\Models\CustomFieldset; use Illuminate\Database\Eloquent\Factories\Factory; class CustomFieldsetFactory extends Factory @@ -11,7 +12,7 @@ class CustomFieldsetFactory extends Factory * * @var string */ - protected $model = \App\Models\CustomFieldset::class; + protected $model = CustomFieldset::class; /** * Define the model's default state. @@ -21,7 +22,7 @@ class CustomFieldsetFactory extends Factory public function definition() { return [ - 'name' => $this->faker->catchPhrase, + 'name' => $this->faker->catchPhrase(), ]; } diff --git a/database/factories/DepartmentFactory.php b/database/factories/DepartmentFactory.php index ad80d32841..cd8c86a875 100644 --- a/database/factories/DepartmentFactory.php +++ b/database/factories/DepartmentFactory.php @@ -2,17 +2,11 @@ namespace Database\Factories; +use App\Models\Department; +use App\Models\Location; +use App\Models\User; use Illuminate\Database\Eloquent\Factories\Factory; -/* -|-------------------------------------------------------------------------- -| Asset Model Factories -|-------------------------------------------------------------------------- -| -| Factories related exclusively to creating models .. -| -*/ - class DepartmentFactory extends Factory { /** @@ -20,7 +14,7 @@ class DepartmentFactory extends Factory * * @var string */ - protected $model = \App\Models\Department::class; + protected $model = Department::class; /** * Define the model's default state. @@ -30,8 +24,9 @@ class DepartmentFactory extends Factory public function definition() { return [ - 'user_id' => 1, - 'location_id' => rand(1, 5), + 'name' => $this->faker->word() . ' Department', + 'user_id' => User::factory()->superuser(), + 'location_id' => Location::factory(), ]; } diff --git a/database/factories/DepreciationFactory.php b/database/factories/DepreciationFactory.php index 17940ac515..6a648d7ade 100644 --- a/database/factories/DepreciationFactory.php +++ b/database/factories/DepreciationFactory.php @@ -2,17 +2,10 @@ namespace Database\Factories; +use App\Models\Depreciation; +use App\Models\User; use Illuminate\Database\Eloquent\Factories\Factory; -/* -|-------------------------------------------------------------------------- -| Asset Model Factories -|-------------------------------------------------------------------------- -| -| Factories related exclusively to creating models .. -| -*/ - class DepreciationFactory extends Factory { /** @@ -20,7 +13,7 @@ class DepreciationFactory extends Factory * * @var string */ - protected $model = \App\Models\Depreciation::class; + protected $model = Depreciation::class; /** * Define the model's default state. @@ -31,7 +24,7 @@ class DepreciationFactory extends Factory { return [ 'name' => $this->faker->catchPhrase(), - 'user_id' => 1, + 'user_id' => User::factory()->superuser(), 'months' => 36, ]; } diff --git a/database/factories/GroupFactory.php b/database/factories/GroupFactory.php index 17f3493c2b..9dee88a965 100644 --- a/database/factories/GroupFactory.php +++ b/database/factories/GroupFactory.php @@ -1,25 +1,8 @@ $this->faker->name, + 'name' => $this->faker->name(), ]; } } diff --git a/database/factories/LicenseFactory.php b/database/factories/LicenseFactory.php index 399b468581..6360735c5f 100644 --- a/database/factories/LicenseFactory.php +++ b/database/factories/LicenseFactory.php @@ -2,25 +2,12 @@ namespace Database\Factories; use App\Models\Category; +use App\Models\License; +use App\Models\Manufacturer; +use App\Models\Supplier; +use App\Models\User; use Illuminate\Database\Eloquent\Factories\Factory; -/* -|-------------------------------------------------------------------------- -| Asset Model Factories -|-------------------------------------------------------------------------- -| -| Factories related exclusively to creating models .. -| -*/ - -// 1 - -// 2 - -// 3 - -// 4 - class LicenseFactory extends Factory { /** @@ -28,7 +15,7 @@ class LicenseFactory extends Factory * * @var string */ - protected $model = \App\Models\License::class; + protected $model = License::class; /** * Define the model's default state. @@ -37,13 +24,11 @@ class LicenseFactory extends Factory */ public function definition() { - - return [ - 'user_id' => 1, - 'name' => $this->faker->name, - 'license_email' => $this->faker->safeEmail, - 'serial' => $this->faker->uuid, + 'user_id' => User::factory()->superuser(), + 'name' => $this->faker->name(), + 'license_email' => $this->faker->safeEmail(), + 'serial' => $this->faker->uuid(), 'notes' => 'Created by DB seeder', 'seats' => $this->faker->numberBetween(1, 10), 'purchase_date' => $this->faker->dateTimeBetween('-1 years', 'now', date_default_timezone_get())->format('Y-m-d'), @@ -51,8 +36,8 @@ class LicenseFactory extends Factory 'expiration_date' => $this->faker->dateTimeBetween('now', '+3 years', date_default_timezone_get())->format('Y-m-d H:i:s'), 'reassignable' => $this->faker->boolean(), 'termination_date' => $this->faker->dateTimeBetween('-1 years', 'now', date_default_timezone_get())->format('Y-m-d H:i:s'), - 'supplier_id' => $this->faker->numberBetween(1, 5), - 'category_id' => Category::where('category_type', '=', 'license')->inRandomOrder()->first()->id + 'supplier_id' => Supplier::factory(), + 'category_id' => Category::factory(), ]; } @@ -61,12 +46,16 @@ class LicenseFactory extends Factory return $this->state(function () { $data = [ 'name' => 'Photoshop', - 'manufacturer_id' => 9, + 'manufacturer_id' => function () { + return Manufacturer::where('name', 'Adobe')->first() ?? Manufacturer::factory()->adobe(); + }, 'purchase_cost' => '299.99', 'seats' => 10, 'purchase_order' => '13503Q', 'maintained' => true, - 'category_id' => 14, + 'category_id' => function () { + return Category::where('name', 'Graphics Software')->first() ?? Category::factory()->licenseGraphicsCategory(); + }, ]; return $data; @@ -78,10 +67,14 @@ class LicenseFactory extends Factory return $this->state(function () { $data = [ 'name' => 'Acrobat', - 'manufacturer_id' => 9, + 'manufacturer_id' => function () { + return Manufacturer::where('name', 'Adobe')->first() ?? Manufacturer::factory()->adobe(); + }, 'purchase_cost' => '29.99', 'seats' => 10, - 'category_id' => 14, + 'category_id' => function () { + return Category::where('name', 'Graphics Software')->first() ?? Category::factory()->licenseGraphicsCategory(); + }, ]; return $data; @@ -93,10 +86,14 @@ class LicenseFactory extends Factory return $this->state(function () { $data = [ 'name' => 'InDesign', - 'manufacturer_id' => 9, + 'manufacturer_id' => function () { + return Manufacturer::where('name', 'Adobe')->first() ?? Manufacturer::factory()->adobe(); + }, 'purchase_cost' => '199.99', 'seats' => 10, - 'category_id' => 14, + 'category_id' => function () { + return Category::where('name', 'Graphics Software')->first() ?? Category::factory()->licenseGraphicsCategory(); + }, ]; @@ -109,10 +106,14 @@ class LicenseFactory extends Factory return $this->state(function () { $data = [ 'name' => 'Office', - 'manufacturer_id' => 2, + 'manufacturer_id' => function () { + return Manufacturer::where('name', 'Microsoft')->first() ?? Manufacturer::factory()->microsoft(); + }, 'purchase_cost' => '49.99', 'seats' => 20, - 'category_id' => 15, + 'category_id' => function () { + return Category::where('name', 'Office Software')->first() ?? Category::factory()->licenseOfficeCategory(); + }, ]; return $data; diff --git a/database/factories/LocationFactory.php b/database/factories/LocationFactory.php index f6a4bb973b..6db268e8c1 100644 --- a/database/factories/LocationFactory.php +++ b/database/factories/LocationFactory.php @@ -23,8 +23,6 @@ class LocationFactory extends Factory 'currency' => $this->faker->currencyCode(), 'zip' => $this->faker->postcode(), 'image' => rand(1, 9).'.jpg', - ]; } - } diff --git a/database/factories/ManufacturerFactory.php b/database/factories/ManufacturerFactory.php index 427cd0b6db..ab22262a78 100644 --- a/database/factories/ManufacturerFactory.php +++ b/database/factories/ManufacturerFactory.php @@ -2,39 +2,10 @@ namespace Database\Factories; +use App\Models\Manufacturer; +use App\Models\User; use Illuminate\Database\Eloquent\Factories\Factory; -/* -|-------------------------------------------------------------------------- -| Asset Model Factories -|-------------------------------------------------------------------------- -| -| Factories related exclusively to creating models .. -| -*/ - -// 1 - -// 2 - -// 3 - -// 4 - -// 5 - -// 6 - -// 7 - -// 8 - -// 9 - -// 10 - -// 11 - class ManufacturerFactory extends Factory { /** @@ -42,7 +13,7 @@ class ManufacturerFactory extends Factory * * @var string */ - protected $model = \App\Models\Manufacturer::class; + protected $model = Manufacturer::class; /** * Define the model's default state. @@ -52,7 +23,8 @@ class ManufacturerFactory extends Factory public function definition() { return [ - 'user_id' => 1, + 'name' => $this->faker->company(), + 'user_id' => User::factory()->superuser(), 'support_phone' => $this->faker->phoneNumber(), 'url' => $this->faker->url(), 'support_email' => $this->faker->safeEmail(), diff --git a/database/factories/SettingFactory.php b/database/factories/SettingFactory.php index cd88cdaf2f..970d00cd68 100644 --- a/database/factories/SettingFactory.php +++ b/database/factories/SettingFactory.php @@ -1,25 +1,8 @@ 1, 'per_page' => 20, - 'site_name' => $this->faker->sentence, + 'site_name' => $this->faker->sentence(), 'auto_increment_assets' => false, 'alert_email' => $this->faker->safeEmail(), 'alerts_enabled' => true, 'brand' => 1, - 'default_currency' => $this->faker->currencyCode, + 'default_currency' => $this->faker->currencyCode(), 'locale' => 'en', 'pwd_secure_min' => 10, // Match web setup 'email_domain' => 'test.com', diff --git a/database/factories/StatuslabelFactory.php b/database/factories/StatuslabelFactory.php index 051e8131ee..0b8359dd5b 100644 --- a/database/factories/StatuslabelFactory.php +++ b/database/factories/StatuslabelFactory.php @@ -3,6 +3,7 @@ namespace Database\Factories; use App\Models\Statuslabel; +use App\Models\User; use Illuminate\Database\Eloquent\Factories\Factory; class StatuslabelFactory extends Factory @@ -22,10 +23,10 @@ class StatuslabelFactory extends Factory public function definition() { return [ - 'name' => $this->faker->sentence, + 'name' => $this->faker->sentence(), 'created_at' => $this->faker->dateTime(), 'updated_at' => $this->faker->dateTime(), - 'user_id' => 1, + 'user_id' => User::factory()->superuser(), 'deleted_at' => null, 'deployable' => 0, 'pending' => 0, @@ -38,7 +39,7 @@ class StatuslabelFactory extends Factory { return $this->state(function () { return [ - 'notes' => $this->faker->sentence, + 'notes' => $this->faker->sentence(), 'deployable' => 1, 'default_label' => 1, ]; @@ -49,7 +50,7 @@ class StatuslabelFactory extends Factory { return $this->state(function () { return [ - 'notes' => $this->faker->sentence, + 'notes' => $this->faker->sentence(), 'pending' => 1, 'default_label' => 1, ]; diff --git a/database/factories/SupplierFactory.php b/database/factories/SupplierFactory.php index 47ec2ceee8..971cd28990 100644 --- a/database/factories/SupplierFactory.php +++ b/database/factories/SupplierFactory.php @@ -1,24 +1,7 @@ 1, 'address' => $this->faker->address(), 'city' => $this->faker->city(), - 'company_id' => rand(1, 4), + 'company_id' => Company::factory(), 'country' => $this->faker->country(), - 'department_id' => rand(1, 6), - 'email' => $this->faker->safeEmail, + 'email' => $this->faker->safeEmail(), 'employee_num' => $this->faker->numberBetween(3500, 35050), 'first_name' => $this->faker->firstName(), 'jobtitle' => $this->faker->jobTitle(), @@ -30,10 +31,10 @@ class UserFactory extends Factory 'notes' => 'Created by DB seeder', 'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password 'permissions' => '{"user":"0"}', - 'phone' => $this->faker->phoneNumber, - 'state' => $this->faker->stateAbbr, - 'username' => $this->faker->username, - 'zip' => $this->faker->postcode, + 'phone' => $this->faker->phoneNumber(), + 'state' => $this->faker->stateAbbr(), + 'username' => $this->faker->username(), + 'zip' => $this->faker->postcode(), ]; } @@ -78,7 +79,9 @@ class UserFactory extends Factory return $this->state(function () { return [ 'permissions' => '{"admin":"1"}', - 'manager_id' => rand(1, 2), + 'manager_id' => function () { + return User::where('permissions->superuser', '1')->first() ?? User::factory()->firstAdmin(); + }, ]; }); } diff --git a/database/seeders/AccessorySeeder.php b/database/seeders/AccessorySeeder.php index 6c4123c08b..31f0c478d4 100644 --- a/database/seeders/AccessorySeeder.php +++ b/database/seeders/AccessorySeeder.php @@ -3,6 +3,9 @@ namespace Database\Seeders; use App\Models\Accessory; +use App\Models\Location; +use App\Models\Supplier; +use App\Models\User; use Illuminate\Database\Seeder; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Log; @@ -14,10 +17,45 @@ class AccessorySeeder extends Seeder { Accessory::truncate(); DB::table('accessories_users')->truncate(); - Accessory::factory()->count(1)->appleUsbKeyboard()->create(); - Accessory::factory()->count(1)->appleBtKeyboard()->create(); - Accessory::factory()->count(1)->appleMouse()->create(); - Accessory::factory()->count(1)->microsoftMouse()->create(); + + if (! Location::count()) { + $this->call(LocationSeeder::class); + } + + $locationIds = Location::all()->pluck('id'); + + if (! Supplier::count()) { + $this->call(SupplierSeeder::class); + } + + $supplierIds = Supplier::all()->pluck('id'); + + $admin = User::where('permissions->superuser', '1')->first() ?? User::factory()->firstAdmin()->create(); + + Accessory::factory()->appleUsbKeyboard()->create([ + 'location_id' => $locationIds->random(), + 'supplier_id' => $supplierIds->random(), + 'user_id' => $admin->id, + ]); + + Accessory::factory()->appleBtKeyboard()->create([ + 'location_id' => $locationIds->random(), + 'supplier_id' => $supplierIds->random(), + 'user_id' => $admin->id, + ]); + + Accessory::factory()->appleMouse()->create([ + 'location_id' => $locationIds->random(), + 'supplier_id' => $supplierIds->random(), + 'user_id' => $admin->id, + ]); + + Accessory::factory()->microsoftMouse()->create([ + 'location_id' => $locationIds->random(), + 'supplier_id' => $supplierIds->random(), + 'user_id' => $admin->id, + ]); + $src = public_path('/img/demo/accessories/'); $dst = 'accessories'.'/'; diff --git a/database/seeders/ActionlogSeeder.php b/database/seeders/ActionlogSeeder.php index 06eb27bdeb..28191d53b0 100644 --- a/database/seeders/ActionlogSeeder.php +++ b/database/seeders/ActionlogSeeder.php @@ -3,6 +3,9 @@ namespace Database\Seeders; use App\Models\Actionlog; +use App\Models\Asset; +use App\Models\Location; +use App\Models\User; use Illuminate\Database\Seeder; class ActionlogSeeder extends Seeder @@ -10,9 +13,30 @@ class ActionlogSeeder extends Seeder public function run() { Actionlog::truncate(); - Actionlog::factory()->count(300)->assetCheckoutToUser()->create(); - Actionlog::factory()->count(100)->assetCheckoutToLocation()->create(); - + if (! Asset::count()) { + $this->call(AssetSeeder::class); + } + + if (! Location::count()) { + $this->call(LocationSeeder::class); + } + + $admin = User::where('permissions->superuser', '1')->first() ?? User::factory()->firstAdmin()->create(); + + Actionlog::factory() + ->count(300) + ->assetCheckoutToUser() + ->create(['user_id' => $admin->id]); + + Actionlog::factory() + ->count(100) + ->assetCheckoutToLocation() + ->create(['user_id' => $admin->id]); + + Actionlog::factory() + ->count(20) + ->licenseCheckoutToUser() + ->create(['user_id' => $admin->id]); } } diff --git a/database/seeders/AssetModelSeeder.php b/database/seeders/AssetModelSeeder.php index 0652fbfb84..1fc0b28cd3 100755 --- a/database/seeders/AssetModelSeeder.php +++ b/database/seeders/AssetModelSeeder.php @@ -3,6 +3,7 @@ namespace Database\Seeders; use App\Models\AssetModel; +use App\Models\User; use Illuminate\Database\Seeder; use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Storage; @@ -13,35 +14,37 @@ class AssetModelSeeder extends Seeder { AssetModel::truncate(); + $admin = User::where('permissions->superuser', '1')->first() ?? User::factory()->firstAdmin()->create(); + // Laptops - AssetModel::factory()->count(1)->mbp13Model()->create(); // 1 - AssetModel::factory()->count(1)->mbpAirModel()->create(); // 2 - AssetModel::factory()->count(1)->surfaceModel()->create(); // 3 - AssetModel::factory()->count(1)->xps13Model()->create(); // 4 - AssetModel::factory()->count(1)->spectreModel()->create(); // 5 - AssetModel::factory()->count(1)->zenbookModel()->create(); // 6 - AssetModel::factory()->count(1)->yogaModel()->create(); // 7 + AssetModel::factory()->count(1)->mbp13Model()->create(['user_id' => $admin->id]); + AssetModel::factory()->count(1)->mbpAirModel()->create(['user_id' => $admin->id]); + AssetModel::factory()->count(1)->surfaceModel()->create(['user_id' => $admin->id]); + AssetModel::factory()->count(1)->xps13Model()->create(['user_id' => $admin->id]); + AssetModel::factory()->count(1)->spectreModel()->create(['user_id' => $admin->id]); + AssetModel::factory()->count(1)->zenbookModel()->create(['user_id' => $admin->id]); + AssetModel::factory()->count(1)->yogaModel()->create(['user_id' => $admin->id]); // Desktops - AssetModel::factory()->count(1)->macproModel()->create(); // 8 - AssetModel::factory()->count(1)->lenovoI5Model()->create(); // 9 - AssetModel::factory()->count(1)->optiplexModel()->create(); // 10 + AssetModel::factory()->count(1)->macproModel()->create(['user_id' => $admin->id]); + AssetModel::factory()->count(1)->lenovoI5Model()->create(['user_id' => $admin->id]); + AssetModel::factory()->count(1)->optiplexModel()->create(['user_id' => $admin->id]); // Conference Phones - AssetModel::factory()->count(1)->polycomModel()->create(); // 11 - AssetModel::factory()->count(1)->polycomcxModel()->create(); // 12 + AssetModel::factory()->count(1)->polycomModel()->create(['user_id' => $admin->id]); + AssetModel::factory()->count(1)->polycomcxModel()->create(['user_id' => $admin->id]); // Tablets - AssetModel::factory()->count(1)->ipadModel()->create(); // 13 - AssetModel::factory()->count(1)->tab3Model()->create(); // 14 + AssetModel::factory()->count(1)->ipadModel()->create(['user_id' => $admin->id]); + AssetModel::factory()->count(1)->tab3Model()->create(['user_id' => $admin->id]); // Phones - AssetModel::factory()->count(1)->iphone11Model()->create(); // 15 - AssetModel::factory()->count(1)->iphone12Model()->create(); // 16 + AssetModel::factory()->count(1)->iphone11Model()->create(['user_id' => $admin->id]); + AssetModel::factory()->count(1)->iphone12Model()->create(['user_id' => $admin->id]); // Displays - AssetModel::factory()->count(1)->ultrafine()->create(); // 17 - AssetModel::factory()->count(1)->ultrasharp()->create(); // 18 + AssetModel::factory()->count(1)->ultrafine()->create(['user_id' => $admin->id]); + AssetModel::factory()->count(1)->ultrasharp()->create(['user_id' => $admin->id]); $src = public_path('/img/demo/models/'); $dst = 'models'.'/'; diff --git a/database/seeders/AssetSeeder.php b/database/seeders/AssetSeeder.php index c5ea479a34..53af19758b 100644 --- a/database/seeders/AssetSeeder.php +++ b/database/seeders/AssetSeeder.php @@ -3,6 +3,10 @@ namespace Database\Seeders; use App\Models\Asset; +use App\Models\Location; +use App\Models\Supplier; +use App\Models\User; +use Illuminate\Database\Eloquent\Factories\Sequence; use Illuminate\Database\Seeder; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Log; @@ -10,40 +14,47 @@ use Illuminate\Support\Facades\Storage; class AssetSeeder extends Seeder { + private $admin; + private $locationIds; + private $supplierIds; + public function run() { Asset::truncate(); - Asset::factory()->count(1000)->laptopMbp()->create(); - Asset::factory()->count(50)->laptopMbpPending()->create(); - Asset::factory()->count(50)->laptopMbpArchived()->create(); - Asset::factory()->count(50)->laptopAir()->create(); - Asset::factory()->count(5)->laptopSurface()->create(); - Asset::factory()->count(5)->laptopXps()->create(); - Asset::factory()->count(5)->laptopSpectre()->create(); - Asset::factory()->count(5)->laptopZenbook()->create(); - Asset::factory()->count(3)->laptopYoga()->create(); - Asset::factory()->count(30)->desktopMacpro()->create(); - Asset::factory()->count(30)->desktopLenovoI5()->create(); - Asset::factory()->count(30)->desktopOptiplex()->create(); + $this->ensureLocationsSeeded(); + $this->ensureSuppliersSeeded(); - Asset::factory()->count(5)->confPolycom()->create(); - Asset::factory()->count(2)->confPolycomcx()->create(); + $this->admin = User::where('permissions->superuser', '1')->first() ?? User::factory()->firstAdmin()->create(); + $this->locationIds = Location::all()->pluck('id'); + $this->supplierIds = Supplier::all()->pluck('id'); - Asset::factory()->count(12)->tabletIpad()->create(); - Asset::factory()->count(4)->tabletTab3()->create(); - - Asset::factory()->count(27)->phoneIphone11()->create(); - Asset::factory()->count(40)->phoneIphone12()->create(); - - Asset::factory()->count(10)->ultrafine()->create(); - Asset::factory()->count(10)->ultrasharp()->create(); + Asset::factory()->count(1000)->laptopMbp()->state(new Sequence($this->getState()))->create(); + Asset::factory()->count(50)->laptopMbpPending()->state(new Sequence($this->getState()))->create(); + Asset::factory()->count(50)->laptopMbpArchived()->state(new Sequence($this->getState()))->create(); + Asset::factory()->count(50)->laptopAir()->state(new Sequence($this->getState()))->create(); + Asset::factory()->count(5)->laptopSurface()->state(new Sequence($this->getState()))->create(); + Asset::factory()->count(5)->laptopXps()->state(new Sequence($this->getState()))->create(); + Asset::factory()->count(5)->laptopSpectre()->state(new Sequence($this->getState()))->create(); + Asset::factory()->count(5)->laptopZenbook()->state(new Sequence($this->getState()))->create(); + Asset::factory()->count(3)->laptopYoga()->state(new Sequence($this->getState()))->create(); + Asset::factory()->count(30)->desktopMacpro()->state(new Sequence($this->getState()))->create(); + Asset::factory()->count(30)->desktopLenovoI5()->state(new Sequence($this->getState()))->create(); + Asset::factory()->count(30)->desktopOptiplex()->state(new Sequence($this->getState()))->create(); + Asset::factory()->count(5)->confPolycom()->state(new Sequence($this->getState()))->create(); + Asset::factory()->count(2)->confPolycomcx()->state(new Sequence($this->getState()))->create(); + Asset::factory()->count(12)->tabletIpad()->state(new Sequence($this->getState()))->create(); + Asset::factory()->count(4)->tabletTab3()->state(new Sequence($this->getState()))->create(); + Asset::factory()->count(27)->phoneIphone11()->state(new Sequence($this->getState()))->create(); + Asset::factory()->count(40)->phoneIphone12()->state(new Sequence($this->getState()))->create(); + Asset::factory()->count(10)->ultrafine()->state(new Sequence($this->getState()))->create(); + Asset::factory()->count(10)->ultrasharp()->state(new Sequence($this->getState()))->create(); $del_files = Storage::files('assets'); foreach ($del_files as $del_file) { // iterate files - Log::debug('Deleting: '.$del_files); + Log::debug('Deleting: ' . $del_files); try { - Storage::disk('public')->delete('assets'.'/'.$del_files); + Storage::disk('public')->delete('assets' . '/' . $del_files); } catch (\Exception $e) { Log::debug($e); } @@ -51,4 +62,27 @@ class AssetSeeder extends Seeder DB::table('checkout_requests')->truncate(); } + + private function ensureLocationsSeeded() + { + if (! Location::count()) { + $this->call(LocationSeeder::class); + } + } + + private function ensureSuppliersSeeded() + { + if (! Supplier::count()) { + $this->call(SupplierSeeder::class); + } + } + + private function getState() + { + return fn($sequence) => [ + 'rtd_location_id' => $this->locationIds->random(), + 'supplier_id' => $this->supplierIds->random(), + 'user_id' => $this->admin->id, + ]; + } } diff --git a/database/seeders/CategorySeeder.php b/database/seeders/CategorySeeder.php index 6a554fc825..da542cff9e 100755 --- a/database/seeders/CategorySeeder.php +++ b/database/seeders/CategorySeeder.php @@ -3,6 +3,7 @@ namespace Database\Seeders; use App\Models\Category; +use App\Models\User; use Illuminate\Database\Seeder; class CategorySeeder extends Seeder @@ -11,20 +12,22 @@ class CategorySeeder extends Seeder { Category::truncate(); - Category::factory()->count(1)->assetLaptopCategory()->create(); // 1 - Category::factory()->count(1)->assetDesktopCategory()->create(); // 2 - Category::factory()->count(1)->assetTabletCategory()->create(); // 3 - Category::factory()->count(1)->assetMobileCategory()->create(); // 4 - Category::factory()->count(1)->assetDisplayCategory()->create(); // 5 - Category::factory()->count(1)->assetVoipCategory()->create(); // 6 - Category::factory()->count(1)->assetConferenceCategory()->create(); // 7 - Category::factory()->count(1)->accessoryKeyboardCategory()->create(); // 8 - Category::factory()->count(1)->accessoryMouseCategory()->create(); // 9 - Category::factory()->count(1)->consumablePaperCategory()->create(); // 10 - Category::factory()->count(1)->consumableInkCategory()->create(); // 11 - Category::factory()->count(1)->componentHddCategory()->create(); // 12 - Category::factory()->count(1)->componentRamCategory()->create(); // 13 - Category::factory()->count(1)->licenseGraphicsCategory()->create(); // 14 - Category::factory()->count(1)->licenseOfficeCategory()->create(); // 15 + $admin = User::where('permissions->superuser', '1')->first() ?? User::factory()->firstAdmin()->create(); + + Category::factory()->count(1)->assetLaptopCategory()->create(['user_id' => $admin->id]); + Category::factory()->count(1)->assetDesktopCategory()->create(['user_id' => $admin->id]); + Category::factory()->count(1)->assetTabletCategory()->create(['user_id' => $admin->id]); + Category::factory()->count(1)->assetMobileCategory()->create(['user_id' => $admin->id]); + Category::factory()->count(1)->assetDisplayCategory()->create(['user_id' => $admin->id]); + Category::factory()->count(1)->assetVoipCategory()->create(['user_id' => $admin->id]); + Category::factory()->count(1)->assetConferenceCategory()->create(['user_id' => $admin->id]); + Category::factory()->count(1)->accessoryKeyboardCategory()->create(['user_id' => $admin->id]); + Category::factory()->count(1)->accessoryMouseCategory()->create(['user_id' => $admin->id]); + Category::factory()->count(1)->consumablePaperCategory()->create(['user_id' => $admin->id]); + Category::factory()->count(1)->consumableInkCategory()->create(['user_id' => $admin->id]); + Category::factory()->count(1)->componentHddCategory()->create(['user_id' => $admin->id]); + Category::factory()->count(1)->componentRamCategory()->create(['user_id' => $admin->id]); + Category::factory()->count(1)->licenseGraphicsCategory()->create(['user_id' => $admin->id]); + Category::factory()->count(1)->licenseOfficeCategory()->create(['user_id' => $admin->id]); } } diff --git a/database/seeders/ComponentSeeder.php b/database/seeders/ComponentSeeder.php index 46380e1682..5a80243466 100644 --- a/database/seeders/ComponentSeeder.php +++ b/database/seeders/ComponentSeeder.php @@ -2,7 +2,9 @@ namespace Database\Seeders; +use App\Models\Company; use App\Models\Component; +use App\Models\Location; use Illuminate\Database\Seeder; use Illuminate\Support\Facades\DB; @@ -12,9 +14,34 @@ class ComponentSeeder extends Seeder { Component::truncate(); DB::table('components_assets')->truncate(); - Component::factory()->count(1)->ramCrucial4()->create(); // 1 - Component::factory()->count(1)->ramCrucial8()->create(); // 1 - Component::factory()->count(1)->ssdCrucial120()->create(); // 1 - Component::factory()->count(1)->ssdCrucial240()->create(); // 1 + + if (! Company::count()) { + $this->call(CompanySeeder::class); + } + + $companyIds = Company::all()->pluck('id'); + + if (! Location::count()) { + $this->call(LocationSeeder::class); + } + + $locationIds = Location::all()->pluck('id'); + + Component::factory()->ramCrucial4()->create([ + 'company_id' => $companyIds->random(), + 'location_id' => $locationIds->random(), + ]); + Component::factory()->ramCrucial8()->create([ + 'company_id' => $companyIds->random(), + 'location_id' => $locationIds->random(), + ]); + Component::factory()->ssdCrucial120()->create([ + 'company_id' => $companyIds->random(), + 'location_id' => $locationIds->random(), + ]); + Component::factory()->ssdCrucial240()->create([ + 'company_id' => $companyIds->random(), + 'location_id' => $locationIds->random(), + ]); } } diff --git a/database/seeders/ConsumableSeeder.php b/database/seeders/ConsumableSeeder.php index 240741fe6d..42527e1df8 100644 --- a/database/seeders/ConsumableSeeder.php +++ b/database/seeders/ConsumableSeeder.php @@ -3,6 +3,7 @@ namespace Database\Seeders; use App\Models\Consumable; +use App\Models\User; use Illuminate\Database\Seeder; use Illuminate\Support\Facades\DB; @@ -12,8 +13,11 @@ class ConsumableSeeder extends Seeder { Consumable::truncate(); DB::table('consumables_users')->truncate(); - Consumable::factory()->count(1)->cardstock()->create(); // 1 - Consumable::factory()->count(1)->paper()->create(); // 2 - Consumable::factory()->count(1)->ink()->create(); // 3 + + $admin = User::where('permissions->superuser', '1')->first() ?? User::factory()->firstAdmin()->create(); + + Consumable::factory()->count(1)->cardstock()->create(['user_id' => $admin->id]); + Consumable::factory()->count(1)->paper()->create(['user_id' => $admin->id]); + Consumable::factory()->count(1)->ink()->create(['user_id' => $admin->id]); } } diff --git a/database/seeders/DatabaseSeeder.php b/database/seeders/DatabaseSeeder.php index 54fb1e088b..1429604139 100644 --- a/database/seeders/DatabaseSeeder.php +++ b/database/seeders/DatabaseSeeder.php @@ -3,15 +3,6 @@ namespace Database\Seeders; use App\Models\Setting; -use Database\Seeders\AccessorySeeder; -use Database\Seeders\ActionlogSeeder; -use Database\Seeders\AssetModelSeeder; -use Database\Seeders\AssetSeeder; -use Database\Seeders\CategorySeeder; -use Database\Seeders\CompanySeeder; -use Database\Seeders\ComponentSeeder; -use Database\Seeders\ConsumableSeeder; -use Database\Seeders\CustomFieldSeeder; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Seeder; use Illuminate\Support\Facades\Artisan; @@ -38,9 +29,9 @@ class DatabaseSeeder extends Seeder $this->call(CompanySeeder::class); $this->call(CategorySeeder::class); $this->call(LocationSeeder::class); + $this->call(DepartmentSeeder::class); $this->call(UserSeeder::class); $this->call(DepreciationSeeder::class); - $this->call(DepartmentSeeder::class); $this->call(ManufacturerSeeder::class); $this->call(SupplierSeeder::class); $this->call(AssetModelSeeder::class); diff --git a/database/seeders/DepartmentSeeder.php b/database/seeders/DepartmentSeeder.php index f82e355edd..7406b97afb 100644 --- a/database/seeders/DepartmentSeeder.php +++ b/database/seeders/DepartmentSeeder.php @@ -3,6 +3,8 @@ namespace Database\Seeders; use App\Models\Department; +use App\Models\Location; +use App\Models\User; use Illuminate\Database\Seeder; class DepartmentSeeder extends Seeder @@ -10,11 +12,43 @@ class DepartmentSeeder extends Seeder public function run() { Department::truncate(); - Department::factory()->count(1)->hr()->create(); // 1 - Department::factory()->count(1)->engineering()->create(); // 2 - Department::factory()->count(1)->marketing()->create(); // 3 - Department::factory()->count(1)->client()->create(); // 4 - Department::factory()->count(1)->product()->create(); // 5 - Department::factory()->count(1)->silly()->create(); // 6 + + if (! Location::count()) { + $this->call(LocationSeeder::class); + } + + $locationIds = Location::all()->pluck('id'); + + $admin = User::where('permissions->superuser', '1')->first() ?? User::factory()->firstAdmin()->create(); + + Department::factory()->count(1)->hr()->create([ + 'location_id' => $locationIds->random(), + 'user_id' => $admin->id, + ]); + + Department::factory()->count(1)->engineering()->create([ + 'location_id' => $locationIds->random(), + 'user_id' => $admin->id, + ]); + + Department::factory()->count(1)->marketing()->create([ + 'location_id' => $locationIds->random(), + 'user_id' => $admin->id, + ]); + + Department::factory()->count(1)->client()->create([ + 'location_id' => $locationIds->random(), + 'user_id' => $admin->id, + ]); + + Department::factory()->count(1)->product()->create([ + 'location_id' => $locationIds->random(), + 'user_id' => $admin->id, + ]); + + Department::factory()->count(1)->silly()->create([ + 'location_id' => $locationIds->random(), + 'user_id' => $admin->id, + ]); } } diff --git a/database/seeders/DepreciationSeeder.php b/database/seeders/DepreciationSeeder.php index 1dfebd3062..349d8aff53 100644 --- a/database/seeders/DepreciationSeeder.php +++ b/database/seeders/DepreciationSeeder.php @@ -3,6 +3,7 @@ namespace Database\Seeders; use App\Models\Depreciation; +use App\Models\User; use Illuminate\Database\Seeder; class DepreciationSeeder extends Seeder @@ -10,8 +11,11 @@ class DepreciationSeeder extends Seeder public function run() { Depreciation::truncate(); - Depreciation::factory()->count(1)->computer()->create(); // 1 - Depreciation::factory()->count(1)->display()->create(); // 2 - Depreciation::factory()->count(1)->mobilePhones()->create(); // 3 + + $admin = User::where('permissions->superuser', '1')->first() ?? User::factory()->firstAdmin()->create(); + + Depreciation::factory()->count(1)->computer()->create(['user_id' => $admin->id]); + Depreciation::factory()->count(1)->display()->create(['user_id' => $admin->id]); + Depreciation::factory()->count(1)->mobilePhones()->create(['user_id' => $admin->id]); } } diff --git a/database/seeders/LicenseSeeder.php b/database/seeders/LicenseSeeder.php index 843eeb42f1..4868dd41e1 100644 --- a/database/seeders/LicenseSeeder.php +++ b/database/seeders/LicenseSeeder.php @@ -2,8 +2,11 @@ namespace Database\Seeders; +use App\Models\Category; use App\Models\License; use App\Models\LicenseSeat; +use App\Models\Supplier; +use App\Models\User; use Illuminate\Database\Seeder; class LicenseSeeder extends Seeder @@ -12,9 +15,43 @@ class LicenseSeeder extends Seeder { License::truncate(); LicenseSeat::truncate(); - License::factory()->count(1)->photoshop()->create(); - License::factory()->count(1)->acrobat()->create(); - License::factory()->count(1)->indesign()->create(); - License::factory()->count(1)->office()->create(); + + if (! Category::count()) { + $this->call(CategorySeeder::class); + } + + $categoryIds = Category::all()->pluck('id'); + + if (! Supplier::count()) { + $this->call(SupplierSeeder::class); + } + + $supplierIds = Supplier::all()->pluck('id'); + + $admin = User::where('permissions->superuser', '1')->first() ?? User::factory()->firstAdmin()->create(); + + License::factory()->count(1)->photoshop()->create([ + 'category_id' => $categoryIds->random(), + 'supplier_id' => $supplierIds->random(), + 'user_id' => $admin->id, + ]); + + License::factory()->count(1)->acrobat()->create([ + 'category_id' => $categoryIds->random(), + 'supplier_id' => $supplierIds->random(), + 'user_id' => $admin->id, + ]); + + License::factory()->count(1)->indesign()->create([ + 'category_id' => $categoryIds->random(), + 'supplier_id' => $supplierIds->random(), + 'user_id' => $admin->id, + ]); + + License::factory()->count(1)->office()->create([ + 'category_id' => $categoryIds->random(), + 'supplier_id' => $supplierIds->random(), + 'user_id' => $admin->id, + ]); } } diff --git a/database/seeders/ManufacturerSeeder.php b/database/seeders/ManufacturerSeeder.php index fc6a3c34e9..cbd70f4c3d 100644 --- a/database/seeders/ManufacturerSeeder.php +++ b/database/seeders/ManufacturerSeeder.php @@ -3,6 +3,7 @@ namespace Database\Seeders; use App\Models\Manufacturer; +use App\Models\User; use Illuminate\Database\Seeder; use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Storage; @@ -12,17 +13,20 @@ class ManufacturerSeeder extends Seeder public function run() { Manufacturer::truncate(); - Manufacturer::factory()->count(1)->apple()->create(); // 1 - Manufacturer::factory()->count(1)->microsoft()->create(); // 2 - Manufacturer::factory()->count(1)->dell()->create(); // 3 - Manufacturer::factory()->count(1)->asus()->create(); // 4 - Manufacturer::factory()->count(1)->hp()->create(); // 5 - Manufacturer::factory()->count(1)->lenovo()->create(); // 6 - Manufacturer::factory()->count(1)->lg()->create(); // 7 - Manufacturer::factory()->count(1)->polycom()->create(); // 8 - Manufacturer::factory()->count(1)->adobe()->create(); // 9 - Manufacturer::factory()->count(1)->avery()->create(); // 10 - Manufacturer::factory()->count(1)->crucial()->create(); // 10 + + $admin = User::where('permissions->superuser', '1')->first() ?? User::factory()->firstAdmin()->create(); + + Manufacturer::factory()->count(1)->apple()->create(['user_id' => $admin->id]); + Manufacturer::factory()->count(1)->microsoft()->create(['user_id' => $admin->id]); + Manufacturer::factory()->count(1)->dell()->create(['user_id' => $admin->id]); + Manufacturer::factory()->count(1)->asus()->create(['user_id' => $admin->id]); + Manufacturer::factory()->count(1)->hp()->create(['user_id' => $admin->id]); + Manufacturer::factory()->count(1)->lenovo()->create(['user_id' => $admin->id]); + Manufacturer::factory()->count(1)->lg()->create(['user_id' => $admin->id]); + Manufacturer::factory()->count(1)->polycom()->create(['user_id' => $admin->id]); + Manufacturer::factory()->count(1)->adobe()->create(['user_id' => $admin->id]); + Manufacturer::factory()->count(1)->avery()->create(['user_id' => $admin->id]); + Manufacturer::factory()->count(1)->crucial()->create(['user_id' => $admin->id]); $src = public_path('/img/demo/manufacturers/'); $dst = 'manufacturers'.'/'; diff --git a/database/seeders/StatuslabelSeeder.php b/database/seeders/StatuslabelSeeder.php index 94cd2140ae..fbc6a9fb66 100755 --- a/database/seeders/StatuslabelSeeder.php +++ b/database/seeders/StatuslabelSeeder.php @@ -3,6 +3,7 @@ namespace Database\Seeders; use App\Models\Statuslabel; +use App\Models\User; use Illuminate\Database\Seeder; class StatuslabelSeeder extends Seeder @@ -10,12 +11,27 @@ class StatuslabelSeeder extends Seeder public function run() { Statuslabel::truncate(); - Statuslabel::factory()->rtd()->create(['name' => 'Ready to Deploy']); - Statuslabel::factory()->pending()->create(['name' => 'Pending']); - Statuslabel::factory()->archived()->create(['name' => 'Archived']); - Statuslabel::factory()->outForDiagnostics()->create(); - Statuslabel::factory()->outForRepair()->create(); - Statuslabel::factory()->broken()->create(); - Statuslabel::factory()->lost()->create(); + + $admin = User::where('permissions->superuser', '1')->first() ?? User::factory()->firstAdmin()->create(); + + Statuslabel::factory()->rtd()->create([ + 'name' => 'Ready to Deploy', + 'user_id' => $admin->id, + ]); + + Statuslabel::factory()->pending()->create([ + 'name' => 'Pending', + 'user_id' => $admin->id, + ]); + + Statuslabel::factory()->archived()->create([ + 'name' => 'Archived', + 'user_id' => $admin->id, + ]); + + Statuslabel::factory()->outForDiagnostics()->create(['user_id' => $admin->id]); + Statuslabel::factory()->outForRepair()->create(['user_id' => $admin->id]); + Statuslabel::factory()->broken()->create(['user_id' => $admin->id]); + Statuslabel::factory()->lost()->create(['user_id' => $admin->id]); } } diff --git a/database/seeders/UserSeeder.php b/database/seeders/UserSeeder.php index 04f46e29b4..2eba6f3721 100644 --- a/database/seeders/UserSeeder.php +++ b/database/seeders/UserSeeder.php @@ -2,7 +2,10 @@ namespace Database\Seeders; +use App\Models\Company; +use App\Models\Department; use App\Models\User; +use Illuminate\Database\Eloquent\Factories\Sequence; use Illuminate\Database\Seeder; use Illuminate\Support\Facades\Storage; use Illuminate\Support\Facades\Log; @@ -17,11 +20,53 @@ class UserSeeder extends Seeder public function run() { User::truncate(); - User::factory()->count(1)->firstAdmin()->create(); - User::factory()->count(1)->snipeAdmin()->create(); - User::factory()->count(3)->superuser()->create(); - User::factory()->count(3)->admin()->create(); - User::factory()->count(50)->viewAssets()->create(); + + if (! Company::count()) { + $this->call(CompanySeeder::class); + } + + $companyIds = Company::all()->pluck('id'); + + if (! Department::count()) { + $this->call(DepartmentSeeder::class); + } + + $departmentIds = Department::all()->pluck('id'); + + User::factory()->count(1)->firstAdmin() + ->state(new Sequence(fn($sequence) => [ + 'company_id' => $companyIds->random(), + 'department_id' => $departmentIds->random(), + ])) + ->create(); + + User::factory()->count(1)->snipeAdmin() + ->state(new Sequence(fn($sequence) => [ + 'company_id' => $companyIds->random(), + 'department_id' => $departmentIds->random(), + ])) + ->create(); + + User::factory()->count(3)->superuser() + ->state(new Sequence(fn($sequence) => [ + 'company_id' => $companyIds->random(), + 'department_id' => $departmentIds->random(), + ])) + ->create(); + + User::factory()->count(3)->admin() + ->state(new Sequence(fn($sequence) => [ + 'company_id' => $companyIds->random(), + 'department_id' => $departmentIds->random(), + ])) + ->create(); + + User::factory()->count(50)->viewAssets() + ->state(new Sequence(fn($sequence) => [ + 'company_id' => $companyIds->random(), + 'department_id' => $departmentIds->random(), + ])) + ->create(); $src = public_path('/img/demo/avatars/'); $dst = 'avatars'.'/'; diff --git a/tests/Feature/Api/Assets/AssetIndexTest.php b/tests/Feature/Api/Assets/AssetIndexTest.php new file mode 100644 index 0000000000..2190c791f6 --- /dev/null +++ b/tests/Feature/Api/Assets/AssetIndexTest.php @@ -0,0 +1,35 @@ +create(); + + Asset::factory()->count(3)->create(); + + Passport::actingAs(User::factory()->superuser()->create()); + $this->getJson( + route('api.assets.index', [ + 'sort' => 'name', + 'order' => 'asc', + 'offset' => '0', + 'limit' => '20', + ])) + ->assertOk() + ->assertJsonStructure([ + 'total', + 'rows', + ]) + ->assertJson(fn(AssertableJson $json) => $json->has('rows', 3)->etc()); + } +} diff --git a/tests/Feature/Api/Users/UsersForSelectListTest.php b/tests/Feature/Api/Users/UsersForSelectListTest.php index b4ede4b117..8b2c01bcf3 100644 --- a/tests/Feature/Api/Users/UsersForSelectListTest.php +++ b/tests/Feature/Api/Users/UsersForSelectListTest.php @@ -15,9 +15,9 @@ class UsersForSelectListTest extends TestCase { Setting::factory()->create(); - User::factory()->count(3)->create(); + $users = User::factory()->superuser()->count(3)->create(); - Passport::actingAs(User::factory()->firstAdmin()->create()); + Passport::actingAs($users->first()); $this->getJson(route('api.users.selectlist')) ->assertOk() ->assertJsonStructure([ @@ -27,7 +27,7 @@ class UsersForSelectListTest extends TestCase 'page', 'page_count', ]) - ->assertJson(fn(AssertableJson $json) => $json->has('results', 4)->etc()); + ->assertJson(fn(AssertableJson $json) => $json->has('results', 3)->etc()); } public function testUsersScopedToCompanyWhenMultipleFullCompanySupportEnabled() diff --git a/tests/Unit/AssetTest.php b/tests/Unit/AssetTest.php index f0a9b11f19..e4dc8a803f 100644 --- a/tests/Unit/AssetTest.php +++ b/tests/Unit/AssetTest.php @@ -36,7 +36,7 @@ class AssetTest extends TestCase 'model_id' => AssetModel::factory() ->create( [ - 'category_id' => Category::factory()->assetLaptopCategory()->id + 'category_id' => Category::factory()->assetLaptopCategory()->create()->id ] )->id, 'warranty_months' => 24, diff --git a/tests/Unit/CategoryTest.php b/tests/Unit/CategoryTest.php index 8d749a3b22..387ed946e0 100644 --- a/tests/Unit/CategoryTest.php +++ b/tests/Unit/CategoryTest.php @@ -26,7 +26,7 @@ class CategoryTest extends TestCase public function testACategoryCanHaveAssets() { - $category = Category::factory()->assetDesktopCategory(); + $category = Category::factory()->assetDesktopCategory()->create(); // Generate 5 models via factory $models = AssetModel::factory() diff --git a/tests/Unit/DepreciationTest.php b/tests/Unit/DepreciationTest.php index 900ae73671..4dd8422276 100644 --- a/tests/Unit/DepreciationTest.php +++ b/tests/Unit/DepreciationTest.php @@ -18,7 +18,7 @@ class DepreciationTest extends TestCase ->count(5) ->create( [ - 'category_id' => Category::factory()->assetLaptopCategory(), + 'category_id' => Category::factory()->assetLaptopCategory()->create(), 'depreciation_id' => $depreciation->id ]); @@ -35,7 +35,7 @@ class DepreciationTest extends TestCase ->photoshop() ->create( [ - 'category_id' => Category::factory()->licenseGraphicsCategory(), + 'category_id' => Category::factory()->licenseGraphicsCategory()->create(), 'depreciation_id' => $depreciation->id ]); diff --git a/tests/Unit/NotificationTest.php b/tests/Unit/NotificationTest.php index 33687ef639..8005759a1e 100644 --- a/tests/Unit/NotificationTest.php +++ b/tests/Unit/NotificationTest.php @@ -22,7 +22,7 @@ class NotificationTest extends TestCase 'model_id' => AssetModel::factory() ->create( [ - 'category_id' => Category::factory()->assetLaptopCategory()->id + 'category_id' => Category::factory()->assetLaptopCategory()->create()->id ] )->id, 'warranty_months' => 24,