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/MaintenanceFactory.php

118 lines
4.1 KiB
PHP

<?php
namespace Database\Factories;
use App\Models\Asset;
use App\Models\Maintenance;
use App\Models\MaintenanceType;
use App\Models\Supplier;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;
class MaintenanceFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = Maintenance::class;
/**
* Define the model's default state.
*
* @return array
*/
/**
* Short, realistic maintenance names so seeded / demo rows look
* plausible in the list, calendar, and other views instead of the
* Faker `sentence(3)` output ("Ipsum quos aut voluptatum.") that
* was there before.
*/
private const NAMES = [
'Battery replacement',
'Screen repair',
'Keyboard replacement',
'Fan replacement',
'Firmware update',
'OS reinstall',
'Cleaning',
'RAM upgrade',
'Hard drive replacement',
'Warranty repair',
'Annual service',
'Diagnostic check',
'Screen calibration',
'Cable replacement',
'Charging port repair',
];
/**
* Define the model's default state.
*
* Default state is a plain-active maintenance (no expected end,
* no completion) so existing feature tests that call
* ->factory()->create() and rely on that shape still pass. The
* ->realistic() state below is what the seeder uses for
* demo/dev data — anchored dates, expected end, and sometimes
* completed_at populated so the calendar and list views show a
* plausible mix.
*
* @return array
*/
public function definition()
{
$maintenanceType = MaintenanceType::factory()->create();
return [
// Set location_id to rtd_location_id on the generated asset so
// seeded maintenance rows point at assets with a real location,
// matching what snipeit:sync-asset-locations would have set.
'asset_id' => Asset::factory()->laptopZenbook()->afterMaking(function (Asset $asset) {
if ($asset->location_id === null) {
$asset->location_id = $asset->rtd_location_id;
}
}),
'supplier_id' => Supplier::factory(),
'maintenance_type_id' => $maintenanceType->id,
'asset_maintenance_type' => $maintenanceType->name,
'name' => $this->faker->randomElement(self::NAMES),
'start_date' => $this->faker->dateTime()->format('Y-m-d H:i:s'),
'is_warranty' => $this->faker->boolean(),
'notes' => $this->faker->paragraph(),
'url' => $this->faker->url(),
'cost' => $this->faker->randomFloat(2, 5, 500),
'created_by' => User::factory()->superuser(),
'image' => $this->faker->numberBetween(1, 11).'.png',
];
}
/**
* Seeder-friendly state that produces a maintenance with
* start_date anchored around today, an expected_completion_date
* a few days out, and a ~40% chance of already being completed
* (for past-start rows only). Produces a plausible mix on the
* calendar / list views so demo installs don't render as
* "everything from 1974".
*/
public function realistic(): static
{
return $this->state(function () {
$startDate = $this->faker->dateTimeBetween('-6 months', '+3 months');
$startCarbon = \Carbon\Carbon::instance($startDate);
$expectedEnd = (clone $startCarbon)->addDays($this->faker->numberBetween(1, 21));
$isCompletable = $startCarbon->isPast() && $this->faker->boolean(40);
$completedAt = $isCompletable
? $this->faker->dateTimeBetween($startCarbon, min($expectedEnd, \Carbon\Carbon::now()))
: null;
return [
'start_date' => $startCarbon->format('Y-m-d H:i:s'),
'expected_completion_date' => $expectedEnd->format('Y-m-d H:i:s'),
'completed_at' => $completedAt?->format('Y-m-d H:i:s'),
];
});
}
}