find($id); return $model === null ? null : $this->toEntity($model); } public function save(CustomerJob $job): CustomerJob { $details = $job->details(); $model = $job->id() === null ? new CustomerJobModel : CustomerJobModel::query()->findOrFail($job->id()); $model->fill([ 'number' => $job->number()->format(), 'name' => $details->name->value, 'description' => $details->description, 'customer_id' => $details->customerId, 'starts_on' => $details->startsOn?->format('Y-m-d'), 'due_on' => $details->dueOn?->format('Y-m-d'), 'planned_from' => $details->plannedFrom?->format('Y-m-d'), 'planned_to' => $details->plannedTo?->format('Y-m-d'), 'kind' => $details->kind, 'status' => $details->status, 'priority' => $details->priority, 'payment_basis' => $details->paymentBasis, 'estimated_hours' => $details->estimatedHours?->value, 'quoted_amount_cents' => $details->quotedAmount?->cents, 'completed_at' => $job->completedAt()?->format('Y-m-d H:i:s'), 'customer_reference' => $details->customerReference, 'instructions' => $details->instructions, 'responsible_user_id' => $details->responsibleUserId, 'contact_user_id' => $details->contactUserId, 'customer_contact_id' => $details->customerContactId, 'biller_user_id' => $details->billerUserId, 'billing_unit_id' => $details->billingUnitId, 'created_by' => $job->createdBy(), ])->save(); $this->syncPivot('customer_job_service_unit', 'service_unit_id', $model->id, $details->serviceUnitIds); $this->syncPivot('customer_job_members', 'user_id', $model->id, $details->memberUserIds); $this->syncPivot('customer_job_tag', 'tag_id', $model->id, $this->tagIds($details->tags)); DB::table('customer_job_links')->where('customer_job_id', $model->id)->delete(); DB::table('customer_job_links')->insert(array_map(fn (int $index, JobLink $link): array => [ 'customer_job_id' => $model->id, 'title' => $link->title, 'url' => $link->url, 'sort_order' => $index + 1, ], array_keys($details->links), $details->links)); return $this->toEntity($model->refresh()); } public function recordChanges(int $jobId, int $userId, array $changes): void { $now = now(); DB::table('customer_job_changes')->insert(array_map(fn ($change): array => [ 'customer_job_id' => $jobId, 'user_id' => $userId, 'field' => $change->field, 'old_value' => $change->oldValue, 'new_value' => $change->newValue, 'created_at' => $now, ], $changes)); } /** * @param list $ids */ private function syncPivot(string $table, string $column, int $jobId, array $ids): void { DB::table($table)->where('customer_job_id', $jobId)->whereNotIn($column, $ids)->delete(); DB::table($table)->insertOrIgnore(array_map(fn (int $id): array => ['customer_job_id' => $jobId, $column => $id], $ids)); } /** * Ids of the tags, adding names not seen before to the shared tag list. * * @param list $tags * @return list */ private function tagIds(array $tags): array { $ids = []; foreach ($tags as $tag) { $existing = DB::table('tags')->whereRaw('LOWER(name) = ?', [mb_strtolower($tag->value)])->value('id'); $ids[] = (int) ($existing ?? DB::table('tags')->insertGetId(['name' => $tag->value, 'created_at' => now(), 'updated_at' => now()])); } return $ids; } private function toEntity(CustomerJobModel $model): CustomerJob { $date = fn ($value): ?DateTimeImmutable => $value === null ? null : (DateTimeImmutable::createFromFormat('!Y-m-d', $value->format('Y-m-d')) ?: null); $details = new JobDetails( name: new JobName($model->name), description: $model->description, customerId: $model->customer_id, serviceUnitIds: DB::table('customer_job_service_unit')->where('customer_job_id', $model->id)->pluck('service_unit_id')->map(fn ($id): int => (int) $id)->all(), startsOn: $date($model->starts_on), dueOn: $date($model->due_on), plannedFrom: $date($model->planned_from), plannedTo: $date($model->planned_to), kind: $model->kind, status: $model->status, priority: $model->priority, paymentBasis: $model->payment_basis, estimatedHours: $model->estimated_hours === null ? null : new EstimatedHours((float) $model->estimated_hours), customerReference: $model->customer_reference, instructions: $model->instructions, responsibleUserId: $model->responsible_user_id, contactUserId: $model->contact_user_id, customerContactId: $model->customer_contact_id, billerUserId: $model->biller_user_id, billingUnitId: $model->billing_unit_id, memberUserIds: DB::table('customer_job_members')->where('customer_job_id', $model->id)->pluck('user_id')->map(fn ($id): int => (int) $id)->all(), tags: DB::table('customer_job_tag') ->join('tags', 'tags.id', '=', 'customer_job_tag.tag_id') ->where('customer_job_id', $model->id) ->orderBy('tags.name') ->pluck('tags.name') ->map(fn ($name): Tag => new Tag((string) $name)) ->all(), links: DB::table('customer_job_links') ->where('customer_job_id', $model->id) ->orderBy('sort_order') ->get() ->map(fn ($row): JobLink => new JobLink((string) $row->title, (string) $row->url)) ->all(), quotedAmount: $model->quoted_amount_cents === null ? null : Money::fromCents($model->quoted_amount_cents), ); return CustomerJob::reconstitute( $model->id, JobNumber::fromString($model->number), $details, $model->created_by, $model->completed_at === null ? null : DateTimeImmutable::createFromInterface($model->completed_at), ); } }