Add quick sales with cash days, credits and PDF receipts

Quick sales (pikamyynti) of active consumer products at the counter, in a
new Sales module:

- Product buttons add one to the sale's list per press; quantities can be
  lowered, raised or removed. Payment by cash or card, optional customer.
- Product durations are booked as the seller's hours on the yearly
  "Pikamyynnit" job (internal customer, not billed) through Jobs'
  ProvideQuickSaleTask and TimeTracking's AddTaskHours. The sale stands
  even when the hours cannot be booked, and the seller is told.
- Sales are never deleted: a credit records the lines negated on today's
  cash day, once per sale, by the seller or management.
- Closing a cash day stores its balance by payment method and VAT rate and
  stops sales on it; only management reopens a day.
- A printable A5 PDF receipt (barryvdh/laravel-dompdf) with the company's
  details, business ID, VAT number, lines and VAT breakdown.

Company details are kept in the settings and edited by the admin only on
the new Yritystiedot page. BusinessId moves from Customers to Shared so
both use the same check.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
2026-11-24 12:33:46 +02:00
co-authored by Claude Opus 5.5
parent c149c56388
commit 6de5d9bef1
101 changed files with 4768 additions and 29 deletions
@@ -0,0 +1,122 @@
<?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,
[],
$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;
}
}
@@ -17,6 +17,13 @@ interface CustomerJobRepository
*/
public function save(CustomerJob $job): CustomerJob;
/**
* The year's system job that quick sale hours are booked on.
*/
public function findQuickSaleJob(int $year): ?CustomerJob;
public function markAsQuickSaleJob(int $jobId, int $year): void;
/**
* @param list<FieldChange> $changes
*/
@@ -13,6 +13,11 @@ interface TaskRepository
*/
public function findForJob(int $jobId, int $taskId): ?Task;
/**
* The job's first task of the work type, if any.
*/
public function findByWorkType(int $jobId, int $workTypeId): ?Task;
public function save(Task $task): Task;
public function delete(Task $task): void;
@@ -101,6 +101,18 @@ final readonly class JobDetails
);
}
public function withServiceUnit(int $serviceUnitId): self
{
return new self(
$this->name, $this->description, $this->customerId, [...$this->serviceUnitIds, $serviceUnitId],
$this->startsOn, $this->dueOn, $this->plannedFrom, $this->plannedTo,
$this->kind, $this->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->quotedAmount,
);
}
/**
* Whether the user is a named member of the job in any role.
*/
@@ -43,6 +43,7 @@ use Illuminate\Database\Eloquent\Model;
* @property int $created_by
* @property int|null $quoted_amount_cents
* @property CarbonImmutable|null $completed_at
* @property int|null $quick_sale_year
* @property CarbonImmutable $created_at
*/
#[Table('customer_jobs')]
@@ -65,6 +66,7 @@ class CustomerJobModel extends Model
return [
'customer_id' => 'integer',
'quoted_amount_cents' => 'integer',
'quick_sale_year' => 'integer',
'completed_at' => 'immutable_datetime',
'starts_on' => 'immutable_date',
'due_on' => 'immutable_date',
@@ -70,6 +70,18 @@ class EloquentCustomerJobRepository implements CustomerJobRepository
return $this->toEntity($model->refresh());
}
public function findQuickSaleJob(int $year): ?CustomerJob
{
$model = CustomerJobModel::query()->where('quick_sale_year', $year)->first();
return $model === null ? null : $this->toEntity($model);
}
public function markAsQuickSaleJob(int $jobId, int $year): void
{
CustomerJobModel::query()->whereKey($jobId)->update(['quick_sale_year' => $year]);
}
public function recordChanges(int $jobId, int $userId, array $changes): void
{
$now = now();
@@ -19,6 +19,13 @@ class EloquentTaskRepository implements TaskRepository
return $model === null ? null : $this->toEntity($model);
}
public function findByWorkType(int $jobId, int $workTypeId): ?Task
{
$model = TaskModel::query()->where('customer_job_id', $jobId)->where('work_type_id', $workTypeId)->orderBy('id')->first();
return $model === null ? null : $this->toEntity($model);
}
public function save(Task $task): Task
{
$model = $task->id() === null ? new TaskModel : TaskModel::query()->findOrFail($task->id());