diff --git a/app/Modules/Jobs/Application/DTOs/CustomerJobData.php b/app/Modules/Jobs/Application/DTOs/CustomerJobData.php
index 23ec770..5043ce9 100644
--- a/app/Modules/Jobs/Application/DTOs/CustomerJobData.php
+++ b/app/Modules/Jobs/Application/DTOs/CustomerJobData.php
@@ -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,
) {}
}
diff --git a/app/Modules/Jobs/Application/DTOs/CustomerJobDetails.php b/app/Modules/Jobs/Application/DTOs/CustomerJobDetails.php
index 7ad0ffc..668c0c3 100644
--- a/app/Modules/Jobs/Application/DTOs/CustomerJobDetails.php
+++ b/app/Modules/Jobs/Application/DTOs/CustomerJobDetails.php
@@ -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,
) {}
/**
diff --git a/app/Modules/Jobs/Application/Services/JobDetailsFactory.php b/app/Modules/Jobs/Application/Services/JobDetailsFactory.php
index 18a29ad..ebc7410 100644
--- a/app/Modules/Jobs/Application/Services/JobDetailsFactory.php
+++ b/app/Modules/Jobs/Application/Services/JobDetailsFactory.php
@@ -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,
);
}
diff --git a/app/Modules/Jobs/Application/UseCases/ChangeJobStatus.php b/app/Modules/Jobs/Application/UseCases/ChangeJobStatus.php
index dd83575..bf6daf9 100644
--- a/app/Modules/Jobs/Application/UseCases/ChangeJobStatus.php
+++ b/app/Modules/Jobs/Application/UseCases/ChangeJobStatus.php
@@ -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;
diff --git a/app/Modules/Jobs/Application/UseCases/CreateCustomerJob.php b/app/Modules/Jobs/Application/UseCases/CreateCustomerJob.php
index d4f1ceb..e7cf27e 100644
--- a/app/Modules/Jobs/Application/UseCases/CreateCustomerJob.php
+++ b/app/Modules/Jobs/Application/UseCases/CreateCustomerJob.php
@@ -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())]);
diff --git a/app/Modules/Jobs/Application/UseCases/UpdateCustomerJob.php b/app/Modules/Jobs/Application/UseCases/UpdateCustomerJob.php
index 35c6453..1f5147c 100644
--- a/app/Modules/Jobs/Application/UseCases/UpdateCustomerJob.php
+++ b/app/Modules/Jobs/Application/UseCases/UpdateCustomerJob.php
@@ -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;
diff --git a/app/Modules/Jobs/Domain/Entities/CustomerJob.php b/app/Modules/Jobs/Domain/Entities/CustomerJob.php
index f63c860..6bb0067 100644
--- a/app/Modules/Jobs/Domain/Entities/CustomerJob.php
+++ b/app/Modules/Jobs/Domain/Entities/CustomerJob.php
@@ -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;
}
/**
diff --git a/app/Modules/Jobs/Domain/ValueObjects/JobDetails.php b/app/Modules/Jobs/Domain/ValueObjects/JobDetails.php
index 503f722..ca16d86 100644
--- a/app/Modules/Jobs/Domain/ValueObjects/JobDetails.php
+++ b/app/Modules/Jobs/Domain/ValueObjects/JobDetails.php
@@ -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,
diff --git a/app/Modules/Jobs/Infrastructure/Persistence/Eloquent/CustomerJobModel.php b/app/Modules/Jobs/Infrastructure/Persistence/Eloquent/CustomerJobModel.php
index 75da550..c8d8569 100644
--- a/app/Modules/Jobs/Infrastructure/Persistence/Eloquent/CustomerJobModel.php
+++ b/app/Modules/Jobs/Infrastructure/Persistence/Eloquent/CustomerJobModel.php
@@ -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',
diff --git a/app/Modules/Jobs/Infrastructure/Persistence/Repositories/EloquentCustomerJobQueries.php b/app/Modules/Jobs/Infrastructure/Persistence/Repositories/EloquentCustomerJobQueries.php
index f107e62..8979a10 100644
--- a/app/Modules/Jobs/Infrastructure/Persistence/Repositories/EloquentCustomerJobQueries.php
+++ b/app/Modules/Jobs/Infrastructure/Persistence/Repositories/EloquentCustomerJobQueries.php
@@ -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'),
);
}
diff --git a/app/Modules/Jobs/Infrastructure/Persistence/Repositories/EloquentCustomerJobRepository.php b/app/Modules/Jobs/Infrastructure/Persistence/Repositories/EloquentCustomerJobRepository.php
index eccb6d5..9ee9bd1 100644
--- a/app/Modules/Jobs/Infrastructure/Persistence/Repositories/EloquentCustomerJobRepository.php
+++ b/app/Modules/Jobs/Infrastructure/Persistence/Repositories/EloquentCustomerJobRepository.php
@@ -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),
+ );
}
}
diff --git a/app/Modules/Jobs/Presentation/Livewire/Forms/JobForm.php b/app/Modules/Jobs/Presentation/Livewire/Forms/JobForm.php
index 1378b9c..aed4abe 100644
--- a/app/Modules/Jobs/Presentation/Livewire/Forms/JobForm.php
+++ b/app/Modules/Jobs/Presentation/Livewire/Forms/JobForm.php
@@ -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'),
diff --git a/app/Modules/Jobs/Presentation/Livewire/⚡form.blade.php b/app/Modules/Jobs/Presentation/Livewire/⚡form.blade.php
index d5ac277..3d33c49 100644
--- a/app/Modules/Jobs/Presentation/Livewire/⚡form.blade.php
+++ b/app/Modules/Jobs/Presentation/Livewire/⚡form.blade.php
@@ -358,6 +358,16 @@ new class extends Component
:label="__('jobs.fields.estimated_hours')"
:hint="__('jobs.fields.estimated_hours_description')"
/>
+
+ @can('money.quotes')
+