3
0
mirror of https://github.com/snipe/snipe-it.git synced 2026-08-18 11:15:42 +00:00
Files
snipe-it/database/factories/ComponentFactory.php
2026-08-05 22:21:18 +01:00

167 lines
5.7 KiB
PHP

<?php
namespace Database\Factories;
use App\Models\Asset;
use App\Models\Category;
use App\Models\Company;
use App\Models\Component;
use App\Models\Location;
use App\Models\Manufacturer;
use App\Models\Supplier;
use App\Models\User;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Carbon as SupportCarbon;
class ComponentFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = Component::class;
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
'category_id' => Category::factory(),
'company_id' => Company::factory(),
'created_by' => User::factory()->superuser(),
'location_id' => Location::factory(),
'min_amt' => $this->faker->numberBetween($min = 1, $max = 2),
'model_number' => $this->faker->numberBetween(1000000, 50000000),
'name' => $this->faker->text(20),
'default_purchase_cost' => $this->faker->randomFloat(2),
'legacy_purchase_date' => $this->faker->dateTime()->format('Y-m-d'),
'qty' => $this->faker->numberBetween(3, 10),
'serial' => $this->faker->uuid(),
'default_supplier_id' => Supplier::factory(),
];
}
public function ramCrucial4()
{
$manufacturer = Manufacturer::where('name', 'Crucial')->first() ?? Manufacturer::factory()->create(['name' => 'Crucial']);
return $this->state(function () use ($manufacturer) {
return [
'name' => 'Crucial 4GB DDR3L-1600 SODIMM',
'category_id' => function () {
return Category::where('name', 'RAM')->first() ?? Category::factory()->componentRamCategory();
},
'qty' => 10,
'min_amt' => 2,
'manufacturer_id' => $manufacturer->id,
'location_id' => Location::factory(),
];
});
}
public function ramCrucial8()
{
$manufacturer = Manufacturer::where('name', 'Crucial')->first() ?? Manufacturer::factory()->create(['name' => 'Crucial']);
return $this->state(function () use ($manufacturer) {
return [
'name' => 'Crucial 8GB DDR3L-1600 SODIMM Memory for Mac',
'category_id' => function () {
return Category::where('name', 'RAM')->first() ?? Category::factory()->componentRamCategory();
},
'qty' => 10,
'min_amt' => 2,
'manufacturer_id' => $manufacturer->id,
];
});
}
public function ssdCrucial120()
{
$manufacturer = Manufacturer::where('name', 'Crucial')->first() ?? Manufacturer::factory()->create(['name' => 'Crucial']);
return $this->state(function () use ($manufacturer) {
return [
'name' => 'Crucial BX300 120GB SATA Internal SSD',
'category_id' => function () {
return Category::where('name', 'HDD/SSD')->first() ?? Category::factory()->componentHddCategory();
},
'qty' => 10,
'min_amt' => 2,
'manufacturer_id' => $manufacturer->id,
];
});
}
public function ssdCrucial240()
{
$manufacturer = Manufacturer::where('name', 'Crucial')->first() ?? Manufacturer::factory()->create(['name' => 'Crucial']);
return $this->state(function () use ($manufacturer) {
return [
'name' => 'Crucial BX300 240GB SATA Internal SSD',
'category_id' => function () {
return Category::where('name', 'HDD/SSD')->first() ?? Category::factory()->componentHddCategory();
},
'qty' => 10,
'min_amt' => 2,
'manufacturer_id' => $manufacturer->id,
];
});
}
public function checkedOutToAsset(?Asset $asset = null)
{
return $this->afterCreating(function (Component $component) use ($asset) {
$component->assets()->attach($component->id, [
'component_id' => $component->id,
'created_at' => Carbon::now(),
'created_by' => 1,
'asset_id' => $asset->id ?? Asset::factory()->create()->id,
]);
});
}
/**
* Populate the observer-written initial Order + OrderItem with
* acquisition metadata (supplier / unit_cost / purchase_date) —
* simulating what the create form's `enrichInitialOrderFromRequest`
* writes when the user fills those fields at create time. Lets
* tests assert Order-level state without going through the HTTP
* controller.
*/
public function withInitialAcquisition(
?Supplier $supplier = null,
?float $unitCost = null,
?string $purchaseDate = null,
) {
return $this->afterCreating(function (Component $component) use ($supplier, $unitCost, $purchaseDate) {
$line = $component->orderItems()->latest('id')->first();
if (! $line) {
return;
}
if ($unitCost !== null) {
$line->price = $unitCost;
$line->save();
}
$order = $line->order;
if (! $order) {
return;
}
if ($supplier !== null) {
$order->supplier_id = $supplier->id;
}
if ($purchaseDate !== null) {
$order->purchase_date = SupportCarbon::parse($purchaseDate);
}
$order->save();
});
}
}