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;
}
}