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:
+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)));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user