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:
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Sales\Domain\Contracts;
|
||||
|
||||
use App\Modules\Sales\Domain\Entities\CashDay;
|
||||
use DateTimeImmutable;
|
||||
|
||||
interface CashDayRepository
|
||||
{
|
||||
/**
|
||||
* The stored cash day, or an open one when the day has not been closed yet.
|
||||
*/
|
||||
public function forDay(DateTimeImmutable $day): CashDay;
|
||||
|
||||
/**
|
||||
* Stores the day's state and balance.
|
||||
*/
|
||||
public function save(CashDay $cashDay): CashDay;
|
||||
|
||||
/**
|
||||
* The day, stored if it was not, with its row locked until the
|
||||
* transaction ends, so a sale and the closing of the same day cannot
|
||||
* interleave.
|
||||
*/
|
||||
public function lockForDay(DateTimeImmutable $day): CashDay;
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Sales\Domain\Contracts;
|
||||
|
||||
use App\Modules\Sales\Domain\Entities\QuickSale;
|
||||
use DateTimeImmutable;
|
||||
|
||||
interface QuickSaleRepository
|
||||
{
|
||||
public function findById(int $id): ?QuickSale;
|
||||
|
||||
/**
|
||||
* Inserts a new sale with its lines and returns the stored sale.
|
||||
*/
|
||||
public function add(QuickSale $sale): QuickSale;
|
||||
|
||||
public function isCredited(int $saleId): bool;
|
||||
|
||||
/**
|
||||
* The cash day's sales, credits included.
|
||||
*
|
||||
* @return list<QuickSale>
|
||||
*/
|
||||
public function forDay(DateTimeImmutable $day): array;
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Sales\Domain\Enums;
|
||||
|
||||
/**
|
||||
* How a quick sale was paid at the counter.
|
||||
*/
|
||||
enum PaymentMethod: string
|
||||
{
|
||||
case Cash = 'cash';
|
||||
case Card = 'card';
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Sales\Domain\Exceptions;
|
||||
|
||||
use App\Modules\Shared\Domain\Exceptions\DomainException;
|
||||
|
||||
final class CashDayClosed extends DomainException
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct('The cash day is closed.');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Sales\Domain\Exceptions;
|
||||
|
||||
use App\Modules\Shared\Domain\Exceptions\DomainException;
|
||||
|
||||
final class CashDayInFuture extends DomainException
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct('A cash day in the future cannot be closed.');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Sales\Domain\Exceptions;
|
||||
|
||||
use App\Modules\Shared\Domain\Exceptions\DomainException;
|
||||
|
||||
final class CashDayNotClosed extends DomainException
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct('The cash day is not closed.');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Sales\Domain\Exceptions;
|
||||
|
||||
use App\Modules\Shared\Domain\Exceptions\DomainException;
|
||||
|
||||
final class CreditCannotBeCredited extends DomainException
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct('A credit cannot be credited.');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Sales\Domain\Exceptions;
|
||||
|
||||
use App\Modules\Shared\Domain\Exceptions\DomainException;
|
||||
|
||||
final class InvalidQuantity extends DomainException
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct('A quantity must be a whole number from 1 to 999.');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Sales\Domain\Exceptions;
|
||||
|
||||
use App\Modules\Shared\Domain\Exceptions\DomainException;
|
||||
|
||||
final class ProductNotSellable extends DomainException
|
||||
{
|
||||
public function __construct(public readonly int $id)
|
||||
{
|
||||
parent::__construct("Product {$id} is not an active consumer product.");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Sales\Domain\Exceptions;
|
||||
|
||||
use App\Modules\Shared\Domain\Exceptions\DomainException;
|
||||
|
||||
final class QuickSaleAlreadyCredited extends DomainException
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct('The quick sale has already been credited.');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Sales\Domain\Exceptions;
|
||||
|
||||
use App\Modules\Shared\Domain\Exceptions\DomainException;
|
||||
|
||||
final class QuickSaleCustomerNotFound extends DomainException
|
||||
{
|
||||
public function __construct(public readonly int $id)
|
||||
{
|
||||
parent::__construct("Customer {$id} was not found.");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Sales\Domain\Exceptions;
|
||||
|
||||
use App\Modules\Shared\Domain\Exceptions\DomainException;
|
||||
|
||||
final class QuickSaleEmpty extends DomainException
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct('A quick sale needs at least one product.');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Sales\Domain\Exceptions;
|
||||
|
||||
use App\Modules\Shared\Domain\Exceptions\DomainException;
|
||||
|
||||
final class QuickSaleNotFound extends DomainException
|
||||
{
|
||||
public function __construct(public readonly int $id)
|
||||
{
|
||||
parent::__construct("Quick sale {$id} was not found.");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Sales\Domain\ValueObjects;
|
||||
|
||||
use App\Modules\Sales\Domain\Entities\QuickSale;
|
||||
use App\Modules\Sales\Domain\Enums\PaymentMethod;
|
||||
|
||||
/**
|
||||
* The balance of a cash day's quick sales: how many, how much by payment
|
||||
* method and by VAT rate (highest rate first). Credits are included as
|
||||
* negative amounts.
|
||||
*/
|
||||
final readonly class CashDayBalance
|
||||
{
|
||||
/**
|
||||
* @param list<VatTotal> $vatTotals
|
||||
*/
|
||||
public function __construct(
|
||||
public int $saleCount,
|
||||
public int $cashCents,
|
||||
public int $cardCents,
|
||||
public array $vatTotals,
|
||||
) {}
|
||||
|
||||
public static function empty(): self
|
||||
{
|
||||
return new self(0, 0, 0, []);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<QuickSale> $sales
|
||||
*/
|
||||
public static function of(array $sales): self
|
||||
{
|
||||
$cash = 0;
|
||||
$card = 0;
|
||||
$grossByRate = [];
|
||||
|
||||
foreach ($sales as $sale) {
|
||||
if ($sale->paymentMethod() === PaymentMethod::Cash) {
|
||||
$cash += $sale->totalCents();
|
||||
} else {
|
||||
$card += $sale->totalCents();
|
||||
}
|
||||
|
||||
foreach ($sale->lines() as $line) {
|
||||
$grossByRate[$line->vatBasisPoints] = ($grossByRate[$line->vatBasisPoints] ?? 0) + $line->totalCents();
|
||||
}
|
||||
}
|
||||
|
||||
krsort($grossByRate);
|
||||
|
||||
$vatTotals = [];
|
||||
foreach ($grossByRate as $basisPoints => $gross) {
|
||||
$vatTotals[] = VatTotal::ofGross($basisPoints, $gross);
|
||||
}
|
||||
|
||||
return new self(count($sales), $cash, $card, $vatTotals);
|
||||
}
|
||||
|
||||
public function totalCents(): int
|
||||
{
|
||||
return $this->cashCents + $this->cardCents;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Sales\Domain\ValueObjects;
|
||||
|
||||
use App\Modules\Sales\Domain\Exceptions\InvalidQuantity;
|
||||
|
||||
/**
|
||||
* One product on a quick sale. The name, price (with VAT) and VAT rate are
|
||||
* copied from the price list when sold, so later price changes do not
|
||||
* change the sale. A credit line has a negative quantity.
|
||||
*/
|
||||
final readonly class QuickSaleLine
|
||||
{
|
||||
public const int MAX_QUANTITY = 999;
|
||||
|
||||
public function __construct(
|
||||
public int $productId,
|
||||
public string $productName,
|
||||
public int $quantity,
|
||||
public int $unitPriceCents,
|
||||
public int $vatBasisPoints,
|
||||
) {
|
||||
if ($quantity === 0 || abs($quantity) > self::MAX_QUANTITY) {
|
||||
throw new InvalidQuantity;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A sold line: the quantity must be positive.
|
||||
*
|
||||
* @throws InvalidQuantity
|
||||
*/
|
||||
public static function sold(int $productId, string $productName, int $quantity, int $unitPriceCents, int $vatBasisPoints): self
|
||||
{
|
||||
if ($quantity < 1) {
|
||||
throw new InvalidQuantity;
|
||||
}
|
||||
|
||||
return new self($productId, $productName, $quantity, $unitPriceCents, $vatBasisPoints);
|
||||
}
|
||||
|
||||
public function negated(): self
|
||||
{
|
||||
return new self($this->productId, $this->productName, -$this->quantity, $this->unitPriceCents, $this->vatBasisPoints);
|
||||
}
|
||||
|
||||
public function totalCents(): int
|
||||
{
|
||||
return $this->quantity * $this->unitPriceCents;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Sales\Domain\ValueObjects;
|
||||
|
||||
use App\Modules\Shared\Domain\ValueObjects\Money;
|
||||
use App\Modules\Shared\Domain\ValueObjects\VatPercentage;
|
||||
|
||||
/**
|
||||
* A cash day's sales of one VAT rate: with VAT, without VAT and the VAT.
|
||||
* The VAT is taken out of the day's total of the rate, not line by line,
|
||||
* as on a till report. Credits make amounts negative.
|
||||
*/
|
||||
final readonly class VatTotal
|
||||
{
|
||||
public function __construct(
|
||||
public int $vatBasisPoints,
|
||||
public int $grossCents,
|
||||
public int $netCents,
|
||||
public int $vatCents,
|
||||
) {}
|
||||
|
||||
public static function ofGross(int $vatBasisPoints, int $grossCents): self
|
||||
{
|
||||
$net = Money::fromCents(abs($grossCents))->removeVat(new VatPercentage($vatBasisPoints))->cents;
|
||||
$net = $grossCents < 0 ? -$net : $net;
|
||||
|
||||
return new self($vatBasisPoints, $grossCents, $net, $grossCents - $net);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user