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]>
132 lines
3.5 KiB
PHP
132 lines
3.5 KiB
PHP
<?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;
|
|
}
|
|
}
|