Add the quoted amount and completion time to customer jobs

- Jobs get a budget / quoted amount (cents, VAT 0). Only users who may
  see quotes see and edit it on the job form and page, and only they see
  its changes in the history. Others' saves keep the stored amount: the
  server decides whether the amount is set, not the browser.
- Jobs record when they were done: set when a job first reaches a status
  that locks its hours, kept through the billing statuses, cleared when
  management opens the lock. The billing export will use it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
2026-11-24 11:46:36 +02:00
co-authored by Claude Opus 5.5
parent d5cbf9d6eb
commit c149c56388
24 changed files with 284 additions and 15 deletions
@@ -40,5 +40,7 @@ final readonly class CustomerJobData
public array $memberUserIds = [],
public array $tags = [],
public array $links = [],
public ?string $quotedAmount = null,
public bool $setsQuotedAmount = false,
) {}
}
@@ -48,6 +48,8 @@ final readonly class CustomerJobDetails
public array $links,
public string $createdAt,
public string $createdByName,
public ?int $quotedAmountCents = null,
public ?string $completedAt = null,
) {}
/**
@@ -17,6 +17,8 @@ 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\Tag;
use App\Modules\Shared\Domain\Exceptions\InvalidMoneyAmount;
use App\Modules\Shared\Domain\ValueObjects\Money;
use App\Modules\Users\Application\Contracts\UserAccountQueries;
use App\Modules\Users\Domain\Enums\Role;
use DateTimeImmutable;
@@ -36,7 +38,11 @@ final class JobDetailsFactory
) {}
/**
* The quoted amount changes only when the caller may set it
* ($data->setsQuotedAmount); otherwise the job keeps its amount.
*
* @throws InvalidJobReference
* @throws InvalidMoneyAmount
*/
public function build(CustomerJobData $data, ?JobDetails $current = null): JobDetails
{
@@ -66,6 +72,7 @@ final class JobDetailsFactory
memberUserIds: $data->memberUserIds,
tags: array_map(fn (string $tag): Tag => new Tag($tag), $data->tags),
links: array_map(fn (array $link): JobLink => new JobLink($link['title'], $link['url']), $data->links),
quotedAmount: $data->setsQuotedAmount ? Money::parse((string) $data->quotedAmount) : $current?->quotedAmount,
);
}
@@ -8,6 +8,7 @@ use App\Modules\Jobs\Domain\Contracts\CustomerJobRepository;
use App\Modules\Jobs\Domain\Enums\JobStatus;
use App\Modules\Jobs\Domain\Exceptions\CustomerJobNotFound;
use App\Modules\Shared\Application\Contracts\TransactionManager;
use App\Modules\Users\Application\Contracts\Clock;
/**
* Moves a job to another status (e.g. on the Kanban board) and records it.
@@ -17,6 +18,7 @@ final class ChangeJobStatus
public function __construct(
private CustomerJobRepository $jobs,
private TransactionManager $transactions,
private Clock $clock,
) {}
/**
@@ -27,6 +29,7 @@ final class ChangeJobStatus
$this->transactions->run(function () use ($jobId, $status, $actorUserId): void {
$job = $this->jobs->findById($jobId) ?? throw new CustomerJobNotFound($jobId);
$changes = $job->changeStatus($status);
$job->recordCompletion($this->clock->now());
if ($changes === []) {
return;
@@ -40,6 +40,7 @@ final class CreateCustomerJob
return $this->transactions->run(function () use ($details, $year, $actorUserId): int {
$job = CustomerJob::open($this->numbers->next($year), $details, $actorUserId);
$job->recordCompletion($this->clock->now());
$jobId = (int) $this->jobs->save($job)->id();
$this->jobs->recordChanges($jobId, $actorUserId, [new FieldChange('created', null, $job->number()->format())]);
@@ -10,6 +10,7 @@ use App\Modules\Jobs\Domain\Contracts\CustomerJobRepository;
use App\Modules\Jobs\Domain\Exceptions\CustomerJobNotFound;
use App\Modules\Jobs\Domain\Exceptions\InvalidJobReference;
use App\Modules\Shared\Application\Contracts\TransactionManager;
use App\Modules\Users\Application\Contracts\Clock;
/**
* Changes a customer job and records each changed field in its history.
@@ -21,6 +22,7 @@ final class UpdateCustomerJob
private CustomerJobRepository $jobs,
private JobDetailsFactory $detailsFactory,
private TransactionManager $transactions,
private Clock $clock,
) {}
/**
@@ -33,6 +35,7 @@ final class UpdateCustomerJob
$job = $this->jobs->findById($jobId) ?? throw new CustomerJobNotFound($jobId);
$changes = $job->changeDetails($this->detailsFactory->build($data, $job->details()));
$job->recordCompletion($this->clock->now());
if ($changes === []) {
return;
@@ -8,10 +8,13 @@ use App\Modules\Jobs\Domain\Enums\JobStatus;
use App\Modules\Jobs\Domain\ValueObjects\FieldChange;
use App\Modules\Jobs\Domain\ValueObjects\JobDetails;
use App\Modules\Jobs\Domain\ValueObjects\JobNumber;
use DateTimeImmutable;
/**
* A customer job (asiakastyö): the work done for a customer, which tasks,
* time entries and invoices are attached to.
* time entries and invoices are attached to. It knows when it was done:
* reaching a status that locks hours sets the time, and opening the lock
* clears it.
*/
final class CustomerJob
{
@@ -20,6 +23,7 @@ final class CustomerJob
private JobNumber $number,
private JobDetails $details,
private int $createdBy,
private ?DateTimeImmutable $completedAt = null,
) {}
public static function open(JobNumber $number, JobDetails $details, int $createdBy): self
@@ -27,9 +31,30 @@ final class CustomerJob
return new self(null, $number, $details, $createdBy);
}
public static function reconstitute(int $id, JobNumber $number, JobDetails $details, int $createdBy): self
public static function reconstitute(int $id, JobNumber $number, JobDetails $details, int $createdBy, ?DateTimeImmutable $completedAt = null): self
{
return new self($id, $number, $details, $createdBy);
return new self($id, $number, $details, $createdBy, $completedAt);
}
/**
* Keeps the completion time in step with the status: set when the job
* first reaches a locking status, kept while it stays locked, cleared
* when the lock is opened.
*/
public function recordCompletion(DateTimeImmutable $now): void
{
if (! $this->details->status->locksTime()) {
$this->completedAt = null;
return;
}
$this->completedAt ??= $now;
}
public function completedAt(): ?DateTimeImmutable
{
return $this->completedAt;
}
/**
@@ -11,6 +11,7 @@ use App\Modules\Jobs\Domain\Enums\Priority;
use App\Modules\Jobs\Domain\Exceptions\DueBeforeStart;
use App\Modules\Jobs\Domain\Exceptions\PlanEndsBeforeStart;
use App\Modules\Jobs\Domain\Exceptions\ServiceUnitRequired;
use App\Modules\Shared\Domain\ValueObjects\Money;
use DateTimeImmutable;
/**
@@ -58,6 +59,7 @@ final readonly class JobDetails
array $memberUserIds,
array $tags,
public array $links,
public ?Money $quotedAmount = null,
) {
$serviceUnitIds = array_values(array_unique($serviceUnitIds));
@@ -95,7 +97,7 @@ final readonly class JobDetails
$this->kind, $status, $this->priority, $this->paymentBasis, $this->estimatedHours,
$this->customerReference, $this->instructions, $this->responsibleUserId, $this->contactUserId,
$this->customerContactId, $this->billerUserId, $this->billingUnitId, $this->memberUserIds,
$this->tags, $this->links,
$this->tags, $this->links, $this->quotedAmount,
);
}
@@ -137,6 +139,7 @@ final readonly class JobDetails
'priority' => $this->priority->value,
'payment_basis' => $this->paymentBasis?->value,
'estimated_hours' => $this->estimatedHours?->format(),
'quoted_amount' => $this->quotedAmount === null ? null : (string) $this->quotedAmount->cents,
'customer_reference' => $this->customerReference,
'instructions' => $this->instructions,
'responsible_user' => (string) $this->responsibleUserId,
@@ -41,13 +41,15 @@ use Illuminate\Database\Eloquent\Model;
* @property int|null $biller_user_id
* @property int|null $billing_unit_id
* @property int $created_by
* @property int|null $quoted_amount_cents
* @property CarbonImmutable|null $completed_at
* @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',
'kind', 'status', 'priority', 'payment_basis', 'estimated_hours', 'quoted_amount_cents', 'customer_reference', 'instructions',
'responsible_user_id', 'contact_user_id', 'customer_contact_id', 'biller_user_id', 'billing_unit_id', 'created_by', 'completed_at',
])]
#[UseFactory(CustomerJobFactory::class)]
class CustomerJobModel extends Model
@@ -62,6 +64,8 @@ class CustomerJobModel extends Model
{
return [
'customer_id' => 'integer',
'quoted_amount_cents' => 'integer',
'completed_at' => 'immutable_datetime',
'starts_on' => 'immutable_date',
'due_on' => 'immutable_date',
'planned_from' => 'immutable_date',
@@ -179,6 +179,8 @@ class EloquentCustomerJobQueries implements CustomerJobQueries
->all(),
createdAt: $model->created_at->format('Y-m-d H:i'),
createdByName: $userNames[$model->created_by] ?? '',
quotedAmountCents: $model->quoted_amount_cents,
completedAt: $model->completed_at?->setTimezone((string) config('app.business_timezone'))->format('Y-m-d H:i'),
);
}
@@ -11,6 +11,7 @@ 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 App\Modules\Shared\Domain\ValueObjects\Money;
use DateTimeImmutable;
use Illuminate\Support\Facades\DB;
@@ -42,6 +43,8 @@ class EloquentCustomerJobRepository implements CustomerJobRepository
'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,
@@ -147,8 +150,15 @@ class EloquentCustomerJobRepository implements CustomerJobRepository
->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);
return CustomerJob::reconstitute(
$model->id,
JobNumber::fromString($model->number),
$details,
$model->created_by,
$model->completed_at === null ? null : DateTimeImmutable::createFromInterface($model->completed_at),
);
}
}
@@ -18,6 +18,9 @@ use App\Modules\Jobs\Domain\Exceptions\InvalidTag;
use App\Modules\Jobs\Domain\Exceptions\PlanEndsBeforeStart;
use App\Modules\Jobs\Domain\Exceptions\ServiceUnitRequired;
use App\Modules\Shared\Domain\Exceptions\DomainException;
use App\Modules\Shared\Domain\Exceptions\InvalidMoneyAmount;
use App\Modules\Shared\Domain\ValueObjects\Money;
use Illuminate\Support\Facades\Gate;
use Illuminate\Validation\Rule;
use Livewire\Form;
@@ -54,6 +57,11 @@ class JobForm extends Form
public string $estimated_hours = '';
/**
* Only for users who may see quoted amounts; filled and sent only for them.
*/
public string $quoted_amount = '';
public string $customer_reference = '';
public string $instructions = '';
@@ -92,6 +100,7 @@ class JobForm extends Form
$this->priority = $job->priority->value;
$this->payment_basis = $job->paymentBasis->value ?? '';
$this->estimated_hours = $job->estimatedHours ?? '';
$this->quoted_amount = $job->quotedAmountCents === null || ! Gate::allows('money.quotes') ? '' : Money::fromCents($job->quotedAmountCents)->inputValue();
$this->customer_reference = $job->customerReference ?? '';
$this->instructions = $job->instructions ?? '';
$this->responsible_user_id = (string) $job->responsible->id;
@@ -135,6 +144,8 @@ class JobForm extends Form
memberUserIds: array_map('intval', $this->member_user_ids),
tags: $this->tags,
links: array_values(array_filter($this->links, fn (array $link): bool => trim($link['title']) !== '' || trim($link['url']) !== '')),
quotedAmount: $text($this->quoted_amount),
setsQuotedAmount: Gate::allows('money.quotes'),
);
}
@@ -149,6 +160,7 @@ class JobForm extends Form
$exception instanceof DueBeforeStart => ['due_on', 'jobs.errors.due_before_start'],
$exception instanceof PlanEndsBeforeStart => ['planned_to', 'jobs.errors.plan_ends_before_start'],
$exception instanceof InvalidEstimatedHours => ['estimated_hours', 'jobs.errors.invalid_hours'],
$exception instanceof InvalidMoneyAmount => ['quoted_amount', 'jobs.errors.invalid_amount'],
$exception instanceof InvalidTag => ['tags', 'jobs.errors.invalid_tag'],
$exception instanceof InvalidJobLink => ['links', 'jobs.errors.invalid_link'],
$exception instanceof InvalidJobReference => [self::referenceField($exception->field), 'jobs.errors.invalid_reference'],
@@ -188,6 +200,7 @@ class JobForm extends Form
'priority' => ['required', Rule::enum(Priority::class)],
'payment_basis' => ['nullable', Rule::enum(PaymentBasis::class)],
'estimated_hours' => ['nullable', 'numeric', 'min:0', 'max:99999'],
'quoted_amount' => ['nullable', 'string', 'max:20'],
'customer_reference' => ['nullable', 'string', 'max:255'],
'instructions' => ['nullable', 'string', 'max:10000'],
'responsible_user_id' => ['required', 'integer'],
@@ -226,6 +239,7 @@ class JobForm extends Form
'estimated_hours.numeric' => __('jobs.errors.invalid_hours'),
'estimated_hours.min' => __('jobs.errors.invalid_hours'),
'estimated_hours.max' => __('jobs.errors.invalid_hours'),
'quoted_amount.max' => __('jobs.errors.invalid_amount'),
'customer_reference.max' => __('jobs.errors.too_long', ['max' => 255]),
'instructions.max' => __('jobs.errors.too_long', ['max' => 10000]),
'responsible_user_id.required' => __('jobs.errors.responsible_required'),
@@ -358,6 +358,16 @@ new class extends Component
:label="__('jobs.fields.estimated_hours')"
:hint="__('jobs.fields.estimated_hours_description')"
/>
@can('money.quotes')
<x-input
wire:model="form.quoted_amount"
inputmode="decimal"
autocomplete="off"
:label="__('jobs.fields.quoted_amount_input')"
:hint="__('jobs.fields.quoted_amount_description')"
/>
@endcan
</div>
</fieldset>
@@ -15,6 +15,7 @@ use App\Modules\Jobs\Domain\Exceptions\TaskNotFound;
use App\Modules\Users\Application\Contracts\UserAccountQueries;
use Carbon\CarbonImmutable;
use Illuminate\Support\Facades\Gate;
use App\Modules\Shared\Domain\ValueObjects\Money;
use Livewire\Attributes\Computed;
use Livewire\Attributes\Locked;
use Livewire\Component;
@@ -61,7 +62,12 @@ new class extends Component
#[Computed]
public function changes(): array
{
return app(CustomerJobQueries::class)->changes($this->jobId);
$changes = app(CustomerJobQueries::class)->changes($this->jobId);
// Changes of the quoted amount are shown only to those who may see quotes (spec 5.7).
return Gate::allows('money.quotes')
? $changes
: array_values(array_filter($changes, fn (JobChangeEntry $change): bool => $change->field !== 'quoted_amount'));
}
/**
@@ -165,6 +171,7 @@ new class extends Component
return match ($field) {
'starts_on', 'due_on', 'planned_from', 'planned_to' => $this->formatDate($value),
'quoted_amount' => Money::fromCents((int) $value)->format(),
'kind' => __('jobs.kinds.'.$value),
'status' => __('jobs.statuses.'.$value),
'priority' => __('jobs.priorities.'.$value),
@@ -235,12 +242,22 @@ new class extends Component
<dt class="font-medium">{{ __('jobs.fields.estimated_hours') }}</dt>
<dd>{{ $job->estimatedHours === null ? __('jobs.not_set') : str_replace('.', ',', $job->estimatedHours).' h' }}</dd>
@can('money.quotes')
<dt class="font-medium">{{ __('jobs.fields.quoted_amount') }}</dt>
<dd>{{ $job->quotedAmountCents === null ? __('jobs.not_set') : Money::fromCents($job->quotedAmountCents)->format().' '.__('jobs.vat_excluded') }}</dd>
@endcan
<dt class="font-medium">{{ __('jobs.fields.customer_reference') }}</dt>
<dd class="break-words">{{ $job->customerReference ?? __('jobs.not_set') }}</dd>
<dt class="font-medium">{{ __('jobs.fields.tags') }}</dt>
<dd>{{ $job->tags === [] ? __('jobs.tags.none') : implode(', ', $job->tags) }}</dd>
@if ($job->completedAt !== null)
<dt class="font-medium">{{ __('jobs.fields.completed_at') }}</dt>
<dd>{{ CarbonImmutable::parse($job->completedAt)->format('j.n.Y H.i') }}</dd>
@endif
<dt class="font-medium">{{ __('jobs.fields.created_at') }}</dt>
<dd>{{ CarbonImmutable::parse($job->createdAt)->format('j.n.Y H.i') }}, {{ $job->createdByName }}</dd>
</dl>