Add customer jobs with numbering, members, tags, links and history
Phase 1, item 4. New Jobs module: - Customer jobs with the spec's fields except money (phase 2): yearly running numbers (2026-0001) from a locked counter row, service units, schedule, kind, status, priority, payment basis, estimated hours, PO reference, instructions, members (responsible, contact person, customer contact, biller, billing unit, other members), tags shared across jobs, and links. - Every change is recorded field by field and shown on the job page with names and Finnish dates. - References to other modules are checked through their query contracts: contact and billing unit must be the customer's own; service units and users must be in use unless already on the job. - Job list with search, filters and "my jobs" on by default; job form with an accessible customer search and quick add; job page. - Access: subcontractors see only jobs they are members of, read only; billing statuses for management and the job's biller; archiving for management. Related changes: - Customers: one internal customer (created by a migration) for internal work; contacts and billing units used by a job cannot be removed. - Users and Catalog: batch name lookups and active user options with roles for other modules. - <x-checkbox-group> component; spec and CLAUDE.md updated. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Jobs\Infrastructure\Persistence;
|
||||
|
||||
use App\Modules\Jobs\Domain\Contracts\JobNumberSequence;
|
||||
use App\Modules\Jobs\Domain\ValueObjects\JobNumber;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
/**
|
||||
* One counter row per year, locked while the next number is taken, so two
|
||||
* jobs saved at the same time never get the same number.
|
||||
*/
|
||||
class DatabaseJobNumberSequence implements JobNumberSequence
|
||||
{
|
||||
public function next(int $year): JobNumber
|
||||
{
|
||||
DB::table('job_number_sequences')->insertOrIgnore(['year' => $year, 'last_number' => 0]);
|
||||
|
||||
$last = (int) DB::table('job_number_sequences')->where('year', $year)->lockForUpdate()->value('last_number');
|
||||
|
||||
DB::table('job_number_sequences')->where('year', $year)->update(['last_number' => $last + 1]);
|
||||
|
||||
return new JobNumber($year, $last + 1);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Jobs\Infrastructure\Persistence\Eloquent;
|
||||
|
||||
use App\Modules\Jobs\Domain\Enums\JobKind;
|
||||
use App\Modules\Jobs\Domain\Enums\JobStatus;
|
||||
use App\Modules\Jobs\Domain\Enums\PaymentBasis;
|
||||
use App\Modules\Jobs\Domain\Enums\Priority;
|
||||
use Carbon\CarbonImmutable;
|
||||
use Database\Factories\CustomerJobFactory;
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Attributes\Table;
|
||||
use Illuminate\Database\Eloquent\Attributes\UseFactory;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
/**
|
||||
* Persistence model of a customer job, used only inside the Jobs
|
||||
* Infrastructure layer. Service units, members, tags and links are in
|
||||
* their own tables, read and written by the repository.
|
||||
*
|
||||
* @property int $id
|
||||
* @property string $number
|
||||
* @property string $name
|
||||
* @property string|null $description
|
||||
* @property int $customer_id
|
||||
* @property CarbonImmutable|null $starts_on
|
||||
* @property CarbonImmutable|null $due_on
|
||||
* @property CarbonImmutable|null $planned_from
|
||||
* @property CarbonImmutable|null $planned_to
|
||||
* @property JobKind $kind
|
||||
* @property JobStatus $status
|
||||
* @property Priority $priority
|
||||
* @property PaymentBasis|null $payment_basis
|
||||
* @property string|null $estimated_hours
|
||||
* @property string|null $customer_reference
|
||||
* @property string|null $instructions
|
||||
* @property int $responsible_user_id
|
||||
* @property int|null $contact_user_id
|
||||
* @property int|null $customer_contact_id
|
||||
* @property int|null $biller_user_id
|
||||
* @property int|null $billing_unit_id
|
||||
* @property int $created_by
|
||||
* @property CarbonImmutable $created_at
|
||||
*/
|
||||
#[Table('customer_jobs')]
|
||||
#[Fillable([
|
||||
'number', 'name', 'description', 'customer_id', 'starts_on', 'due_on', 'planned_from', 'planned_to',
|
||||
'kind', 'status', 'priority', 'payment_basis', 'estimated_hours', 'customer_reference', 'instructions',
|
||||
'responsible_user_id', 'contact_user_id', 'customer_contact_id', 'biller_user_id', 'billing_unit_id', 'created_by',
|
||||
])]
|
||||
#[UseFactory(CustomerJobFactory::class)]
|
||||
class CustomerJobModel extends Model
|
||||
{
|
||||
/** @use HasFactory<CustomerJobFactory> */
|
||||
use HasFactory;
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'customer_id' => 'integer',
|
||||
'starts_on' => 'immutable_date',
|
||||
'due_on' => 'immutable_date',
|
||||
'planned_from' => 'immutable_date',
|
||||
'planned_to' => 'immutable_date',
|
||||
'kind' => JobKind::class,
|
||||
'status' => JobStatus::class,
|
||||
'priority' => Priority::class,
|
||||
'payment_basis' => PaymentBasis::class,
|
||||
'responsible_user_id' => 'integer',
|
||||
'contact_user_id' => 'integer',
|
||||
'customer_contact_id' => 'integer',
|
||||
'biller_user_id' => 'integer',
|
||||
'billing_unit_id' => 'integer',
|
||||
'created_by' => 'integer',
|
||||
'created_at' => 'immutable_datetime',
|
||||
];
|
||||
}
|
||||
}
|
||||
+206
@@ -0,0 +1,206 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Jobs\Infrastructure\Persistence\Repositories;
|
||||
|
||||
use App\Modules\Catalog\Application\Contracts\CatalogQueries;
|
||||
use App\Modules\Customers\Application\Contracts\CustomerQueries;
|
||||
use App\Modules\Jobs\Application\Contracts\CustomerJobQueries;
|
||||
use App\Modules\Jobs\Application\DTOs\CustomerJobDetails;
|
||||
use App\Modules\Jobs\Application\DTOs\CustomerJobSummary;
|
||||
use App\Modules\Jobs\Application\DTOs\JobChangeEntry;
|
||||
use App\Modules\Jobs\Application\DTOs\JobListFilter;
|
||||
use App\Modules\Jobs\Application\DTOs\NamedId;
|
||||
use App\Modules\Jobs\Domain\Enums\JobStatus;
|
||||
use App\Modules\Jobs\Infrastructure\Persistence\Eloquent\CustomerJobModel;
|
||||
use App\Modules\Users\Application\Contracts\UserAccountQueries;
|
||||
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
/**
|
||||
* Names of customers, users, contacts and service units come from those
|
||||
* modules' query contracts, never from their tables.
|
||||
*/
|
||||
class EloquentCustomerJobQueries implements CustomerJobQueries
|
||||
{
|
||||
public function __construct(
|
||||
private CustomerQueries $customers,
|
||||
private UserAccountQueries $users,
|
||||
private CatalogQueries $catalog,
|
||||
) {}
|
||||
|
||||
public function paginate(JobListFilter $filter, int $perPage = 25): LengthAwarePaginator
|
||||
{
|
||||
$page = $this->filtered($filter)
|
||||
->orderByRaw('due_on IS NULL')
|
||||
->orderBy('due_on')
|
||||
->orderByDesc('id')
|
||||
->paginate($perPage);
|
||||
|
||||
$models = collect($page->items());
|
||||
$customerNames = $this->customers->namesByIds($models->pluck('customer_id')->unique()->values()->all());
|
||||
$userNames = $this->users->namesByIds($models->pluck('responsible_user_id')->unique()->values()->all());
|
||||
|
||||
return $page->through(fn (CustomerJobModel $model): CustomerJobSummary => new CustomerJobSummary(
|
||||
$model->id,
|
||||
$model->number,
|
||||
$model->name,
|
||||
$model->customer_id,
|
||||
$customerNames[$model->customer_id] ?? '',
|
||||
$model->status,
|
||||
$model->priority,
|
||||
$model->due_on?->format('Y-m-d'),
|
||||
$userNames[$model->responsible_user_id] ?? '',
|
||||
));
|
||||
}
|
||||
|
||||
public function findDetails(int $id): ?CustomerJobDetails
|
||||
{
|
||||
$model = CustomerJobModel::query()->find($id);
|
||||
|
||||
if ($model === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$memberIds = DB::table('customer_job_members')->where('customer_job_id', $id)->pluck('user_id')->map(fn ($id): int => (int) $id)->all();
|
||||
$userIds = array_values(array_filter([$model->responsible_user_id, $model->contact_user_id, $model->biller_user_id, $model->created_by, ...$memberIds]));
|
||||
$userNames = $this->users->namesByIds(array_values(array_unique($userIds)));
|
||||
$user = fn (?int $userId): ?NamedId => $userId === null ? null : new NamedId($userId, $userNames[$userId] ?? '');
|
||||
|
||||
$serviceUnitIds = DB::table('customer_job_service_unit')->where('customer_job_id', $id)->pluck('service_unit_id')->map(fn ($id): int => (int) $id)->all();
|
||||
$serviceUnits = [];
|
||||
foreach ($this->catalog->serviceUnitNames($serviceUnitIds) as $unitId => $name) {
|
||||
$serviceUnits[] = new NamedId((int) $unitId, $name);
|
||||
}
|
||||
|
||||
$contact = $model->customer_contact_id === null ? null : $this->customers->findContact($model->customer_id, $model->customer_contact_id);
|
||||
$billingUnit = $model->billing_unit_id === null ? null : $this->customers->findBillingUnit($model->customer_id, $model->billing_unit_id);
|
||||
|
||||
return new CustomerJobDetails(
|
||||
id: $model->id,
|
||||
number: $model->number,
|
||||
name: $model->name,
|
||||
description: $model->description,
|
||||
customer: new NamedId($model->customer_id, $this->customers->namesByIds([$model->customer_id])[$model->customer_id] ?? ''),
|
||||
serviceUnits: $serviceUnits,
|
||||
startsOn: $model->starts_on?->format('Y-m-d'),
|
||||
dueOn: $model->due_on?->format('Y-m-d'),
|
||||
plannedFrom: $model->planned_from?->format('Y-m-d'),
|
||||
plannedTo: $model->planned_to?->format('Y-m-d'),
|
||||
kind: $model->kind,
|
||||
status: $model->status,
|
||||
priority: $model->priority,
|
||||
paymentBasis: $model->payment_basis,
|
||||
estimatedHours: $model->estimated_hours === null ? null : rtrim(rtrim($model->estimated_hours, '0'), '.'),
|
||||
customerReference: $model->customer_reference,
|
||||
instructions: $model->instructions,
|
||||
responsible: $user($model->responsible_user_id) ?? new NamedId($model->responsible_user_id, ''),
|
||||
contactUser: $user($model->contact_user_id),
|
||||
customerContact: $contact === null ? null : new NamedId($contact->id, $contact->name),
|
||||
biller: $user($model->biller_user_id),
|
||||
billingUnit: $billingUnit === null ? null : new NamedId($billingUnit->id, $billingUnit->name),
|
||||
members: array_values(array_map(fn (int $memberId): NamedId => new NamedId($memberId, $userNames[$memberId] ?? ''), $memberIds)),
|
||||
tags: DB::table('customer_job_tag')
|
||||
->join('tags', 'tags.id', '=', 'customer_job_tag.tag_id')
|
||||
->where('customer_job_id', $id)
|
||||
->orderBy('tags.name')
|
||||
->pluck('tags.name')
|
||||
->map(fn ($name): string => (string) $name)
|
||||
->all(),
|
||||
links: DB::table('customer_job_links')
|
||||
->where('customer_job_id', $id)
|
||||
->orderBy('sort_order')
|
||||
->get()
|
||||
->map(fn ($row): array => ['title' => (string) $row->title, 'url' => (string) $row->url])
|
||||
->all(),
|
||||
createdAt: $model->created_at->format('Y-m-d H:i'),
|
||||
createdByName: $userNames[$model->created_by] ?? '',
|
||||
);
|
||||
}
|
||||
|
||||
public function changes(int $jobId): array
|
||||
{
|
||||
$rows = DB::table('customer_job_changes')->where('customer_job_id', $jobId)->orderByDesc('id')->get();
|
||||
$userNames = $this->users->namesByIds($rows->pluck('user_id')->map(fn ($id): int => (int) $id)->unique()->values()->all());
|
||||
|
||||
return $rows->map(fn ($row): JobChangeEntry => new JobChangeEntry(
|
||||
(string) $row->field,
|
||||
$row->old_value,
|
||||
$row->new_value,
|
||||
$userNames[(int) $row->user_id] ?? '',
|
||||
(string) $row->created_at,
|
||||
))->values()->all();
|
||||
}
|
||||
|
||||
public function tagSuggestions(string $term = '', int $limit = 20): array
|
||||
{
|
||||
$term = trim($term);
|
||||
|
||||
return DB::table('tags')
|
||||
->when($term !== '', fn ($query) => $query->where('name', 'like', '%'.addcslashes($term, '%_\\').'%'))
|
||||
->orderBy('name')
|
||||
->limit($limit)
|
||||
->pluck('name')
|
||||
->map(fn ($name): string => (string) $name)
|
||||
->all();
|
||||
}
|
||||
|
||||
public function isMember(int $jobId, int $userId): bool
|
||||
{
|
||||
return $this->involving(CustomerJobModel::query()->whereKey($jobId), $userId)->exists();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Builder<CustomerJobModel>
|
||||
*/
|
||||
private function filtered(JobListFilter $filter): Builder
|
||||
{
|
||||
$query = CustomerJobModel::query();
|
||||
$search = trim((string) $filter->search);
|
||||
|
||||
if ($search !== '') {
|
||||
$pattern = '%'.addcslashes($search, '%_\\').'%';
|
||||
$customerIds = array_map(fn ($customer): int => $customer->id, $this->customers->search($search, 50));
|
||||
|
||||
$query->where(fn (Builder $query) => $query
|
||||
->where('number', 'like', $pattern)
|
||||
->orWhere('name', 'like', $pattern)
|
||||
->orWhereIn('customer_id', $customerIds));
|
||||
}
|
||||
|
||||
if ($filter->status !== null) {
|
||||
$query->where('status', $filter->status);
|
||||
} elseif (! $filter->includeArchived) {
|
||||
$query->where('status', '!=', JobStatus::Archived->value);
|
||||
}
|
||||
|
||||
if ($filter->priority !== null) {
|
||||
$query->where('priority', $filter->priority);
|
||||
}
|
||||
|
||||
if ($filter->serviceUnitId !== null) {
|
||||
$query->whereIn('id', DB::table('customer_job_service_unit')->select('customer_job_id')->where('service_unit_id', $filter->serviceUnitId));
|
||||
}
|
||||
|
||||
foreach (array_filter([$filter->mineForUserId, $filter->onlyMemberUserId]) as $userId) {
|
||||
$this->involving($query, $userId);
|
||||
}
|
||||
|
||||
return $query;
|
||||
}
|
||||
|
||||
/**
|
||||
* Restricts to jobs where the user is responsible, contact, biller or member.
|
||||
*
|
||||
* @param Builder<CustomerJobModel> $query
|
||||
* @return Builder<CustomerJobModel>
|
||||
*/
|
||||
private function involving(Builder $query, int $userId): Builder
|
||||
{
|
||||
return $query->where(fn (Builder $query) => $query
|
||||
->where('responsible_user_id', $userId)
|
||||
->orWhere('contact_user_id', $userId)
|
||||
->orWhere('biller_user_id', $userId)
|
||||
->orWhereIn('id', DB::table('customer_job_members')->select('customer_job_id')->where('user_id', $userId)));
|
||||
}
|
||||
}
|
||||
+154
@@ -0,0 +1,154 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Jobs\Infrastructure\Persistence\Repositories;
|
||||
|
||||
use App\Modules\Jobs\Domain\Contracts\CustomerJobRepository;
|
||||
use App\Modules\Jobs\Domain\Entities\CustomerJob;
|
||||
use App\Modules\Jobs\Domain\ValueObjects\EstimatedHours;
|
||||
use App\Modules\Jobs\Domain\ValueObjects\JobDetails;
|
||||
use App\Modules\Jobs\Domain\ValueObjects\JobLink;
|
||||
use App\Modules\Jobs\Domain\ValueObjects\JobName;
|
||||
use App\Modules\Jobs\Domain\ValueObjects\JobNumber;
|
||||
use App\Modules\Jobs\Domain\ValueObjects\Tag;
|
||||
use App\Modules\Jobs\Infrastructure\Persistence\Eloquent\CustomerJobModel;
|
||||
use DateTimeImmutable;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class EloquentCustomerJobRepository implements CustomerJobRepository
|
||||
{
|
||||
public function findById(int $id): ?CustomerJob
|
||||
{
|
||||
$model = CustomerJobModel::query()->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,
|
||||
'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<int> $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<Tag> $tags
|
||||
* @return list<int>
|
||||
*/
|
||||
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(),
|
||||
);
|
||||
|
||||
return CustomerJob::reconstitute($model->id, JobNumber::fromString($model->number), $details, $model->created_by);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user