snipe-it/database/factories/AccessoryFactory.php

160 lines
5.1 KiB
PHP
Raw Normal View History

<?php
2021-06-10 13:19:27 -07:00
2021-06-10 13:17:44 -07:00
namespace Database\Factories;
use App\Models\Accessory;
use App\Models\AccessoryCheckout;
use App\Models\Category;
use App\Models\Location;
use App\Models\Manufacturer;
use App\Models\Supplier;
2023-03-14 11:34:58 -07:00
use App\Models\User;
use Carbon\Carbon;
2021-06-10 13:19:27 -07:00
use Illuminate\Database\Eloquent\Factories\Factory;
2021-06-10 13:17:44 -07:00
class AccessoryFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = Accessory::class;
2021-06-10 13:17:44 -07:00
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
'name' => sprintf(
'%s %s',
$this->faker->randomElement(['Bluetooth', 'Wired']),
$this->faker->randomElement(['Keyboard', 'Wired'])
),
'user_id' => User::factory()->superuser(),
'category_id' => Category::factory()->forAccessories(),
2021-06-10 13:17:44 -07:00
'model_number' => $this->faker->numberBetween(1000000, 50000000),
'location_id' => Location::factory(),
'qty' => 1,
2021-06-10 13:17:44 -07:00
];
}
2021-06-10 13:17:44 -07:00
public function appleBtKeyboard()
{
return $this->state(function () {
return [
'name' => 'Bluetooth Keyboard',
'image' => 'bluetooth.jpg',
'category_id' => function () {
2023-03-20 12:01:39 -07:00
return Category::where('name', 'Keyboards')->first() ?? Category::factory()->accessoryKeyboardCategory();
},
'manufacturer_id' => function () {
return Manufacturer::where('name', 'Apple')->first() ?? Manufacturer::factory()->apple();
},
2021-06-10 13:17:44 -07:00
'qty' => 10,
'min_amt' => 2,
'supplier_id' => Supplier::factory(),
2021-06-10 13:17:44 -07:00
];
});
}
2021-06-10 13:17:44 -07:00
public function appleUsbKeyboard()
{
return $this->state(function () {
return [
'name' => 'USB Keyboard',
'image' => 'usb-keyboard.jpg',
'category_id' => function () {
2023-03-20 12:01:39 -07:00
return Category::where('name', 'Keyboards')->first() ?? Category::factory()->accessoryKeyboardCategory();
},
'manufacturer_id' => function () {
return Manufacturer::where('name', 'Apple')->first() ?? Manufacturer::factory()->apple();
},
2021-06-10 13:17:44 -07:00
'qty' => 15,
'min_amt' => 2,
'supplier_id' => Supplier::factory(),
2021-06-10 13:17:44 -07:00
];
});
}
2021-06-10 13:17:44 -07:00
public function appleMouse()
{
return $this->state(function () {
return [
'name' => 'Magic Mouse',
'image' => 'magic-mouse.jpg',
'category_id' => function () {
return Category::where('name', 'Mouse')->first() ?? Category::factory()->accessoryMouseCategory();
},
'manufacturer_id' => function () {
return Manufacturer::where('name', 'Apple')->first() ?? Manufacturer::factory()->apple();
},
2021-06-10 13:17:44 -07:00
'qty' => 13,
'min_amt' => 2,
'supplier_id' => Supplier::factory(),
2021-06-10 13:17:44 -07:00
];
});
}
2021-06-10 13:17:44 -07:00
public function microsoftMouse()
{
return $this->state(function () {
return [
'name' => 'Sculpt Comfort Mouse',
'image' => 'comfort-mouse.jpg',
'category_id' => function () {
return Category::where('name', 'Mouse')->first() ?? Category::factory()->accessoryMouseCategory();
},
'manufacturer_id' => function () {
return Manufacturer::where('name', 'Microsoft')->first() ?? Manufacturer::factory()->microsoft();
},
2021-06-10 13:17:44 -07:00
'qty' => 13,
'min_amt' => 2,
];
});
}
public function withoutItemsRemaining()
{
return $this->state(function () {
return [
'qty' => 1,
];
})->afterCreating(function ($accessory) {
$user = User::factory()->create();
$accessory->checkouts()->create([
'accessory_id' => $accessory->id,
'created_at' => Carbon::now(),
'user_id' => $user->id,
'assigned_to' => $user->id,
'assigned_type' => User::class,
'note' => '',
]);
});
}
2024-01-29 15:56:18 -08:00
public function requiringAcceptance()
{
return $this->afterCreating(function ($accessory) {
$accessory->category->update(['require_acceptance' => 1]);
});
}
2024-02-12 16:31:32 -08:00
public function checkedOutToUser(User $user = null)
{
return $this->afterCreating(function (Accessory $accessory) use ($user) {
$accessory->checkouts()->create([
'accessory_id' => $accessory->id,
'created_at' => Carbon::now(),
'user_id' => 1,
'assigned_to' => $user->id ?? User::factory()->create()->id,
'assigned_type' => User::class,
]);
});
}
2021-06-10 13:17:44 -07:00
}