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,22 @@
<?php
declare(strict_types=1);
namespace App\Modules\Sales\Application\Contracts;
use DateTimeImmutable;
/**
* Books the hours of a quick sale on the seller, on the year's quick sale
* job. Implemented with the Jobs and TimeTracking modules.
*/
interface QuickSaleHours
{
/**
* Adds the hours to the seller's day on the work type's task.
*
* @return bool False when the hours could not be booked, e.g. the
* day's hours are already approved or would pass 24 hours.
*/
public function book(int $sellerId, DateTimeImmutable $day, int $workTypeId, int $quarters, int $saleId): bool;
}
@@ -0,0 +1,19 @@
<?php
declare(strict_types=1);
namespace App\Modules\Sales\Application\Contracts;
use App\Modules\Sales\Application\DTOs\SellableProduct;
/**
* The products that can be sold at the counter: active consumer products
* of the price list. Implemented with the Catalog module's queries.
*/
interface QuickSaleProducts
{
/**
* @return list<SellableProduct> In price list order.
*/
public function sellable(): array;
}
@@ -0,0 +1,34 @@
<?php
declare(strict_types=1);
namespace App\Modules\Sales\Application\Contracts;
use App\Modules\Sales\Application\DTOs\CashDaySummary;
use App\Modules\Sales\Application\DTOs\QuickSaleSummary;
interface QuickSaleQueries
{
/**
* The day's sales and credits, latest first; only the seller's when given.
*
* @param string $day Y-m-d
* @return list<QuickSaleSummary>
*/
public function salesOn(string $day, ?int $sellerId = null): array;
public function findSale(int $id): ?QuickSaleSummary;
/**
* @param string $day Y-m-d
*/
public function cashDay(string $day): CashDaySummary;
/**
* Earlier days with sales that have not been closed, oldest first.
*
* @param string $day Y-m-d
* @return list<string> Y-m-d
*/
public function unclosedDaysBefore(string $day): array;
}
@@ -0,0 +1,25 @@
<?php
declare(strict_types=1);
namespace App\Modules\Sales\Application\DTOs;
use App\Modules\Sales\Domain\ValueObjects\CashDayBalance;
/**
* A cash day's state. The balance is the stored one of a closed day and
* the balance that closing would form on an open day. Times are
* "Y-m-d H:i" in the company's time zone.
*/
final readonly class CashDaySummary
{
public function __construct(
public string $day,
public bool $isClosed,
public CashDayBalance $balance,
public ?string $closedByName = null,
public ?string $closedAt = null,
public ?string $reopenedByName = null,
public ?string $reopenedAt = null,
) {}
}
@@ -0,0 +1,16 @@
<?php
declare(strict_types=1);
namespace App\Modules\Sales\Application\DTOs;
final readonly class QuickSaleLineSummary
{
public function __construct(
public string $productName,
public int $quantity,
public int $unitPriceCents,
public int $totalCents,
public int $vatBasisPoints = 0,
) {}
}
@@ -0,0 +1,64 @@
<?php
declare(strict_types=1);
namespace App\Modules\Sales\Application\DTOs;
use App\Modules\Sales\Domain\Enums\PaymentMethod;
use App\Modules\Sales\Domain\ValueObjects\VatTotal;
/**
* A quick sale as listed: amounts with VAT, negative on a credit.
* $soldAt is "Y-m-d H:i" in the company's time zone. $creditsSaleId is set
* on a credit, $creditedBySaleId on a sale that has been credited.
*/
final readonly class QuickSaleSummary
{
/**
* @param list<QuickSaleLineSummary> $lines
*/
public function __construct(
public int $id,
public string $soldAt,
public int $sellerId,
public string $sellerName,
public ?string $customerName,
public PaymentMethod $paymentMethod,
public int $totalCents,
public array $lines,
public ?int $creditsSaleId,
public ?int $creditedBySaleId,
) {}
public function isCredit(): bool
{
return $this->creditsSaleId !== null;
}
public function canBeCredited(): bool
{
return $this->creditsSaleId === null && $this->creditedBySaleId === null;
}
/**
* The sale's VAT by rate, highest rate first, as on its receipt.
*
* @return list<VatTotal>
*/
public function vatTotals(): array
{
$grossByRate = [];
foreach ($this->lines as $line) {
$grossByRate[$line->vatBasisPoints] = ($grossByRate[$line->vatBasisPoints] ?? 0) + $line->totalCents;
}
krsort($grossByRate);
return array_map(
fn (int $basisPoints, int $gross): VatTotal => VatTotal::ofGross($basisPoints, $gross),
array_keys($grossByRate),
array_values($grossByRate),
);
}
}
@@ -0,0 +1,21 @@
<?php
declare(strict_types=1);
namespace App\Modules\Sales\Application\DTOs;
/**
* Input for a quick sale: quantities by product id (zero or empty ones are
* left out by the caller), the payment method key and an optional customer.
*/
final readonly class SaleData
{
/**
* @param array<int, int> $quantities
*/
public function __construct(
public array $quantities,
public string $paymentMethod,
public ?int $customerId = null,
) {}
}
@@ -0,0 +1,19 @@
<?php
declare(strict_types=1);
namespace App\Modules\Sales\Application\DTOs;
/**
* A recorded quick sale: its id and total with VAT, and the hours booked
* on the seller or left unbooked (in quarters of an hour).
*/
final readonly class SaleResult
{
public function __construct(
public int $saleId,
public int $totalCents,
public int $bookedQuarters,
public int $unbookedQuarters,
) {}
}
@@ -0,0 +1,23 @@
<?php
declare(strict_types=1);
namespace App\Modules\Sales\Application\DTOs;
/**
* A consumer product as it is sold: its price with VAT and the hours its
* sale books on the seller ($durationQuarters on $workTypeId). $unit is
* the price list unit key, e.g. "piece".
*/
final readonly class SellableProduct
{
public function __construct(
public int $id,
public string $name,
public string $unit,
public int $grossCents,
public int $vatBasisPoints,
public int $durationQuarters,
public ?int $workTypeId,
) {}
}
@@ -0,0 +1,44 @@
<?php
declare(strict_types=1);
namespace App\Modules\Sales\Application\UseCases;
use App\Modules\Sales\Domain\Contracts\CashDayRepository;
use App\Modules\Sales\Domain\Contracts\QuickSaleRepository;
use App\Modules\Sales\Domain\Exceptions\CashDayClosed;
use App\Modules\Sales\Domain\Exceptions\CashDayInFuture;
use App\Modules\Shared\Application\Contracts\TransactionManager;
use App\Modules\Users\Application\Contracts\Clock;
use DateTimeImmutable;
/**
* Closes a cash day (today or a forgotten earlier one) and stores the
* balance of its quick sales.
*/
final class CloseCashDay
{
public function __construct(
private CashDayRepository $cashDays,
private QuickSaleRepository $sales,
private TransactionManager $transactions,
private Clock $clock,
) {}
/**
* @throws CashDayInFuture
* @throws CashDayClosed
*/
public function execute(DateTimeImmutable $day, int $actorUserId): void
{
if ($day->format('Y-m-d') > $this->clock->today()->format('Y-m-d')) {
throw new CashDayInFuture;
}
$this->transactions->run(function () use ($day, $actorUserId): void {
$cashDay = $this->cashDays->lockForDay($day);
$cashDay->close($this->sales->forDay($day), $actorUserId, $this->clock->now());
$this->cashDays->save($cashDay);
});
}
}
@@ -0,0 +1,57 @@
<?php
declare(strict_types=1);
namespace App\Modules\Sales\Application\UseCases;
use App\Modules\Sales\Domain\Contracts\CashDayRepository;
use App\Modules\Sales\Domain\Contracts\QuickSaleRepository;
use App\Modules\Sales\Domain\Entities\QuickSale;
use App\Modules\Sales\Domain\Exceptions\CashDayClosed;
use App\Modules\Sales\Domain\Exceptions\CreditCannotBeCredited;
use App\Modules\Sales\Domain\Exceptions\QuickSaleAlreadyCredited;
use App\Modules\Sales\Domain\Exceptions\QuickSaleNotFound;
use App\Modules\Shared\Application\Contracts\TransactionManager;
use App\Modules\Users\Application\Contracts\Clock;
/**
* Credits a quick sale (hyvitys): records the sale's lines negated on
* today's cash day, so a sale of a closed day can be credited too. Hours
* stay as they are: the work was done, and a wrong amount is corrected in
* the weekly view. Who may credit is checked by the caller.
*/
final class CreditQuickSale
{
public function __construct(
private QuickSaleRepository $sales,
private CashDayRepository $cashDays,
private TransactionManager $transactions,
private Clock $clock,
) {}
/**
* @return int The credit's id.
*
* @throws QuickSaleNotFound
* @throws QuickSaleAlreadyCredited
* @throws CreditCannotBeCredited
* @throws CashDayClosed
*/
public function execute(int $saleId, int $actorUserId): int
{
$today = $this->clock->today();
return $this->transactions->run(function () use ($saleId, $actorUserId, $today): int {
$original = $this->sales->findById($saleId) ?? throw new QuickSaleNotFound($saleId);
$this->cashDays->lockForDay($today)->ensureOpen();
if ($this->sales->isCredited($saleId)) {
throw new QuickSaleAlreadyCredited;
}
$credit = QuickSale::creditFor($original, $actorUserId, $this->clock->now(), $today);
return (int) $this->sales->add($credit)->id();
});
}
}
@@ -0,0 +1,36 @@
<?php
declare(strict_types=1);
namespace App\Modules\Sales\Application\UseCases;
use App\Modules\Sales\Domain\Contracts\CashDayRepository;
use App\Modules\Sales\Domain\Exceptions\CashDayNotClosed;
use App\Modules\Shared\Application\Contracts\TransactionManager;
use App\Modules\Users\Application\Contracts\Clock;
use DateTimeImmutable;
/**
* Reopens a closed cash day (management only, checked by the caller). The
* balance is formed again when the day is closed again.
*/
final class ReopenCashDay
{
public function __construct(
private CashDayRepository $cashDays,
private TransactionManager $transactions,
private Clock $clock,
) {}
/**
* @throws CashDayNotClosed
*/
public function execute(DateTimeImmutable $day, int $actorUserId): void
{
$this->transactions->run(function () use ($day, $actorUserId): void {
$cashDay = $this->cashDays->lockForDay($day);
$cashDay->reopen($actorUserId, $this->clock->now());
$this->cashDays->save($cashDay);
});
}
}
@@ -0,0 +1,94 @@
<?php
declare(strict_types=1);
namespace App\Modules\Sales\Application\UseCases;
use App\Modules\Customers\Application\Contracts\CustomerQueries;
use App\Modules\Sales\Application\Contracts\QuickSaleHours;
use App\Modules\Sales\Application\Contracts\QuickSaleProducts;
use App\Modules\Sales\Application\DTOs\SaleData;
use App\Modules\Sales\Application\DTOs\SaleResult;
use App\Modules\Sales\Domain\Contracts\CashDayRepository;
use App\Modules\Sales\Domain\Contracts\QuickSaleRepository;
use App\Modules\Sales\Domain\Entities\QuickSale;
use App\Modules\Sales\Domain\Enums\PaymentMethod;
use App\Modules\Sales\Domain\Exceptions\CashDayClosed;
use App\Modules\Sales\Domain\Exceptions\InvalidQuantity;
use App\Modules\Sales\Domain\Exceptions\ProductNotSellable;
use App\Modules\Sales\Domain\Exceptions\QuickSaleCustomerNotFound;
use App\Modules\Sales\Domain\Exceptions\QuickSaleEmpty;
use App\Modules\Sales\Domain\ValueObjects\QuickSaleLine;
use App\Modules\Shared\Application\Contracts\TransactionManager;
use App\Modules\Users\Application\Contracts\Clock;
/**
* Records a quick sale on today's cash day with the products' current
* prices, and books the products' durations as the seller's hours. The
* customer has paid, so the sale is kept even when the hours cannot be
* booked; the result tells how many were left out.
*/
final class SellQuickSale
{
public function __construct(
private QuickSaleRepository $sales,
private CashDayRepository $cashDays,
private QuickSaleProducts $products,
private QuickSaleHours $hours,
private CustomerQueries $customers,
private TransactionManager $transactions,
private Clock $clock,
) {}
/**
* @throws ProductNotSellable
* @throws InvalidQuantity
* @throws QuickSaleEmpty
* @throws QuickSaleCustomerNotFound
* @throws CashDayClosed
*/
public function execute(int $sellerId, SaleData $data): SaleResult
{
$sellable = [];
foreach ($this->products->sellable() as $product) {
$sellable[$product->id] = $product;
}
$lines = [];
$quartersByWorkType = [];
foreach ($data->quantities as $productId => $quantity) {
$product = $sellable[$productId] ?? throw new ProductNotSellable($productId);
$lines[] = QuickSaleLine::sold($product->id, $product->name, $quantity, $product->grossCents, $product->vatBasisPoints);
if ($product->durationQuarters > 0 && $product->workTypeId !== null) {
$quartersByWorkType[$product->workTypeId] = ($quartersByWorkType[$product->workTypeId] ?? 0) + $product->durationQuarters * $quantity;
}
}
if ($data->customerId !== null && $this->customers->findSummary($data->customerId) === null) {
throw new QuickSaleCustomerNotFound($data->customerId);
}
$today = $this->clock->today();
$sale = QuickSale::sell($sellerId, $data->customerId, PaymentMethod::from($data->paymentMethod), $lines, $this->clock->now(), $today);
return $this->transactions->run(function () use ($sale, $sellerId, $today, $quartersByWorkType): SaleResult {
$this->cashDays->lockForDay($today)->ensureOpen();
$saleId = (int) $this->sales->add($sale)->id();
$booked = 0;
$unbooked = 0;
foreach ($quartersByWorkType as $workTypeId => $quarters) {
if ($this->hours->book($sellerId, $today, $workTypeId, $quarters, $saleId)) {
$booked += $quarters;
} else {
$unbooked += $quarters;
}
}
return new SaleResult($saleId, $sale->totalCents(), $booked, $unbooked);
});
}
}