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,118 @@
<?php
declare(strict_types=1);
namespace App\Modules\Sales\Domain\Entities;
use App\Modules\Sales\Domain\Exceptions\CashDayClosed;
use App\Modules\Sales\Domain\Exceptions\CashDayNotClosed;
use App\Modules\Sales\Domain\ValueObjects\CashDayBalance;
use DateTimeImmutable;
/**
* A day of quick sales at the counter (kassapäivä). Closing it forms the
* day's balance and stops further sales on it; management may reopen it,
* which empties the balance until it is closed again.
*/
final class CashDay
{
private function __construct(
private ?int $id,
private DateTimeImmutable $day,
private ?CashDayBalance $balance,
private ?int $closedBy,
private ?DateTimeImmutable $closedAt,
private ?int $reopenedBy,
private ?DateTimeImmutable $reopenedAt,
) {}
public static function open(DateTimeImmutable $day): self
{
return new self(null, $day, null, null, null, null, null);
}
public static function reconstitute(int $id, DateTimeImmutable $day, ?CashDayBalance $balance, ?int $closedBy, ?DateTimeImmutable $closedAt, ?int $reopenedBy, ?DateTimeImmutable $reopenedAt): self
{
return new self($id, $day, $balance, $closedBy, $closedAt, $reopenedBy, $reopenedAt);
}
/**
* @param list<QuickSale> $sales The day's sales, credits included.
*
* @throws CashDayClosed
*/
public function close(array $sales, int $closedBy, DateTimeImmutable $now): void
{
$this->ensureOpen();
$this->balance = CashDayBalance::of($sales);
$this->closedBy = $closedBy;
$this->closedAt = $now;
}
/**
* @throws CashDayNotClosed
*/
public function reopen(int $reopenedBy, DateTimeImmutable $now): void
{
if (! $this->isClosed()) {
throw new CashDayNotClosed;
}
$this->balance = null;
$this->closedBy = null;
$this->closedAt = null;
$this->reopenedBy = $reopenedBy;
$this->reopenedAt = $now;
}
/**
* @throws CashDayClosed
*/
public function ensureOpen(): void
{
if ($this->isClosed()) {
throw new CashDayClosed;
}
}
public function isClosed(): bool
{
return $this->closedAt !== null;
}
public function id(): ?int
{
return $this->id;
}
public function day(): DateTimeImmutable
{
return $this->day;
}
public function balance(): ?CashDayBalance
{
return $this->balance;
}
public function closedBy(): ?int
{
return $this->closedBy;
}
public function closedAt(): ?DateTimeImmutable
{
return $this->closedAt;
}
public function reopenedBy(): ?int
{
return $this->reopenedBy;
}
public function reopenedAt(): ?DateTimeImmutable
{
return $this->reopenedAt;
}
}
@@ -0,0 +1,131 @@
<?php
declare(strict_types=1);
namespace App\Modules\Sales\Domain\Entities;
use App\Modules\Sales\Domain\Enums\PaymentMethod;
use App\Modules\Sales\Domain\Exceptions\CreditCannotBeCredited;
use App\Modules\Sales\Domain\Exceptions\QuickSaleEmpty;
use App\Modules\Sales\Domain\ValueObjects\QuickSaleLine;
use DateTimeImmutable;
/**
* A cash or card sale of consumer products at the counter (pikamyynti),
* possibly to a named customer. A sale is never deleted: a mistake is
* corrected with a credit, a sale of its own with the original's lines
* negated, paid back the same way and booked on the day it is made.
*/
final class QuickSale
{
/**
* @param list<QuickSaleLine> $lines
*/
private function __construct(
private ?int $id,
private int $sellerId,
private ?int $customerId,
private PaymentMethod $paymentMethod,
private DateTimeImmutable $soldAt,
private DateTimeImmutable $soldOn,
private array $lines,
private ?int $creditsSaleId,
) {
if ($lines === []) {
throw new QuickSaleEmpty;
}
}
/**
* @param list<QuickSaleLine> $lines
* @param DateTimeImmutable $soldOn The cash day, in the company's time zone.
*
* @throws QuickSaleEmpty
*/
public static function sell(int $sellerId, ?int $customerId, PaymentMethod $paymentMethod, array $lines, DateTimeImmutable $soldAt, DateTimeImmutable $soldOn): self
{
return new self(null, $sellerId, $customerId, $paymentMethod, $soldAt, $soldOn, $lines, null);
}
/**
* @throws CreditCannotBeCredited
*/
public static function creditFor(self $original, int $actorUserId, DateTimeImmutable $soldAt, DateTimeImmutable $soldOn): self
{
if ($original->isCredit()) {
throw new CreditCannotBeCredited;
}
return new self(
null,
$actorUserId,
$original->customerId,
$original->paymentMethod,
$soldAt,
$soldOn,
array_map(fn (QuickSaleLine $line): QuickSaleLine => $line->negated(), $original->lines),
$original->id,
);
}
/**
* @param list<QuickSaleLine> $lines
*/
public static function reconstitute(int $id, int $sellerId, ?int $customerId, PaymentMethod $paymentMethod, DateTimeImmutable $soldAt, DateTimeImmutable $soldOn, array $lines, ?int $creditsSaleId): self
{
return new self($id, $sellerId, $customerId, $paymentMethod, $soldAt, $soldOn, $lines, $creditsSaleId);
}
public function totalCents(): int
{
return array_sum(array_map(fn (QuickSaleLine $line): int => $line->totalCents(), $this->lines));
}
public function isCredit(): bool
{
return $this->creditsSaleId !== null;
}
public function id(): ?int
{
return $this->id;
}
public function sellerId(): int
{
return $this->sellerId;
}
public function customerId(): ?int
{
return $this->customerId;
}
public function paymentMethod(): PaymentMethod
{
return $this->paymentMethod;
}
public function soldAt(): DateTimeImmutable
{
return $this->soldAt;
}
public function soldOn(): DateTimeImmutable
{
return $this->soldOn;
}
/**
* @return list<QuickSaleLine>
*/
public function lines(): array
{
return $this->lines;
}
public function creditsSaleId(): ?int
{
return $this->creditsSaleId;
}
}