*/ class CustomerJobFactory extends Factory { protected $model = CustomerJobModel::class; private static int $sequence = 9000; /** * @return array */ public function definition(): array { $responsible = User::factory(); return [ 'number' => sprintf('2026-%04d', ++self::$sequence), 'name' => fake()->sentence(3), 'customer_id' => CustomerModel::factory(), 'kind' => JobKind::OneOff, 'status' => JobStatus::Tentative, 'priority' => Priority::Normal, 'responsible_user_id' => $responsible, 'created_by' => $responsible, ]; } /** * Every job has a service unit: the first active one unless given. */ public function configure(): static { return $this->afterCreating(function (CustomerJobModel $job): void { if (DB::table('customer_job_service_unit')->where('customer_job_id', $job->id)->exists()) { return; } $unitId = ServiceUnitModel::query()->where('is_shared', false)->orderBy('sort_order')->value('id') ?? ServiceUnitModel::factory()->create()->id; DB::table('customer_job_service_unit')->insert(['customer_job_id' => $job->id, 'service_unit_id' => $unitId]); }); } public function quoted(int $cents): static { return $this->state(fn (): array => ['quoted_amount_cents' => $cents]); } /** * @param list $userIds */ public function withMembers(array $userIds): static { return $this->afterCreating(function (CustomerJobModel $job) use ($userIds): void { DB::table('customer_job_members')->insert(array_map( fn (int $userId): array => ['customer_job_id' => $job->id, 'user_id' => $userId], $userIds, )); }); } }