Files
Toiminnanohjaus/app/Modules/Jobs/Application/UseCases/ProvideQuickSaleTask.php
petriandClaude Opus 5.5 18598cc08e Give tasks one assignee and members, and schedule template tasks to due dates
- A task now has one optional assignee (tasks.assignee_user_id), whose
  calendar row it is on and whose days its estimate books, and members
  (task_members) who see it among their own jobs and in the weekly sheet
  but get no bookings. A migration moves existing assignees over: the one
  with most booked time becomes the assignee, the others members.
- Adding tasks from a workflow template asks for each step's assignee and,
  by default, schedules tasks with a due date to end on it, booking the
  estimate backwards from the due date (not before today); what does not
  fit runs past the due date and is reported as late.
- The task calendar leaves quick sale tasks out of the unscheduled list,
  shows a bar's job details in a popover on hover and focus, and shows the
  day load on its own grid line.
- The template table stacks its cells on narrow screens to reflow at 320 px.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-24 19:01:11 +02:00

124 lines
4.8 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Modules\Jobs\Application\UseCases;
use App\Modules\Catalog\Application\Contracts\CatalogQueries;
use App\Modules\Customers\Application\Contracts\CustomerQueries;
use App\Modules\Jobs\Domain\Contracts\CustomerJobRepository;
use App\Modules\Jobs\Domain\Contracts\JobNumberSequence;
use App\Modules\Jobs\Domain\Contracts\TaskRepository;
use App\Modules\Jobs\Domain\Entities\CustomerJob;
use App\Modules\Jobs\Domain\Entities\Task;
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 App\Modules\Jobs\Domain\Enums\TaskStatus;
use App\Modules\Jobs\Domain\Exceptions\InvalidTaskReference;
use App\Modules\Jobs\Domain\ValueObjects\FieldChange;
use App\Modules\Jobs\Domain\ValueObjects\JobDetails;
use App\Modules\Jobs\Domain\ValueObjects\JobName;
use App\Modules\Jobs\Domain\ValueObjects\TaskTitle;
use App\Modules\Shared\Application\Contracts\TransactionManager;
use LogicException;
/**
* The task that quick sale hours of a work type are booked on. Hours of
* quick sales go on one system job per year (on the internal customer, not
* billed), which is opened on the year's first sale; each work type gets
* its own task, and the job gets the work type's service unit.
*/
final class ProvideQuickSaleTask
{
public function __construct(
private CustomerJobRepository $jobs,
private JobNumberSequence $numbers,
private TaskRepository $tasks,
private CatalogQueries $catalog,
private CustomerQueries $customers,
private TransactionManager $transactions,
) {}
/**
* @param string $jobName The name for a new job, e.g. "Pikamyynnit 2026".
* @return int The task's id.
*
* @throws InvalidTaskReference When the work type does not exist.
*/
public function execute(int $year, int $workTypeId, int $actorUserId, string $jobName): int
{
$workType = $this->catalog->findWorkType($workTypeId) ?? throw new InvalidTaskReference('work_type');
$unit = $this->catalog->findServiceUnit($workType->serviceUnitId);
$unitId = $unit !== null && ! $unit->isShared ? $unit->id : null;
return $this->transactions->run(function () use ($year, $workTypeId, $workType, $unitId, $actorUserId, $jobName): int {
$job = $this->jobs->findQuickSaleJob($year) ?? $this->openJob($year, $unitId, $actorUserId, $jobName);
$jobId = (int) $job->id();
if ($unitId !== null && ! in_array($unitId, $job->details()->serviceUnitIds, true)) {
$changes = $job->changeDetails($job->details()->withServiceUnit($unitId));
$this->jobs->save($job);
$this->jobs->recordChanges($jobId, $actorUserId, $changes);
}
$task = $this->tasks->findByWorkType($jobId, $workTypeId) ?? $this->tasks->save(Task::create(
$jobId,
$job->details()->status,
$workTypeId,
new TaskTitle($workType->name),
null,
TaskStatus::InProgress,
null,
null,
null,
[],
$actorUserId,
));
return (int) $task->id();
});
}
private function openJob(int $year, ?int $unitId, int $actorUserId, string $jobName): CustomerJob
{
$customerId = $this->customers->internalCustomerId() ?? throw new LogicException('The internal customer is missing.');
$unitId ??= $this->catalog->activeServiceUnits()[0]->id ?? throw new LogicException('There are no service units.');
$details = new JobDetails(
name: new JobName($jobName),
description: null,
customerId: $customerId,
serviceUnitIds: [$unitId],
startsOn: null,
dueOn: null,
plannedFrom: null,
plannedTo: null,
kind: JobKind::Ongoing,
status: JobStatus::InProduction,
priority: Priority::Normal,
paymentBasis: PaymentBasis::NotBillable,
estimatedHours: null,
customerReference: null,
instructions: null,
responsibleUserId: $actorUserId,
contactUserId: null,
customerContactId: null,
billerUserId: null,
billingUnitId: null,
memberUserIds: [],
tags: [],
links: [],
);
$job = $this->jobs->save(CustomerJob::open($this->numbers->next($year), $details, $actorUserId));
$jobId = (int) $job->id();
$this->jobs->markAsQuickSaleJob($jobId, $year);
$this->jobs->recordChanges($jobId, $actorUserId, [new FieldChange('created', null, $job->number()->format())]);
return $job;
}
}