Files
Toiminnanohjaus/app/Modules/Sales/Domain/Entities/QuickSale.php
T

140 lines
3.8 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 App\Modules\Shared\Domain\ValueObjects\EmailAddress;
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,
private ?EmailAddress $receiptEmail = null,
) {
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, ?EmailAddress $receiptEmail = null): self
{
return new self(null, $sellerId, $customerId, $paymentMethod, $soldAt, $soldOn, $lines, null, $receiptEmail);
}
/**
* @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,
$original->receiptEmail,
);
}
/**
* @param list<QuickSaleLine> $lines
*/
public static function reconstitute(int $id, int $sellerId, ?int $customerId, PaymentMethod $paymentMethod, DateTimeImmutable $soldAt, DateTimeImmutable $soldOn, array $lines, ?int $creditsSaleId, ?EmailAddress $receiptEmail = null): self
{
return new self($id, $sellerId, $customerId, $paymentMethod, $soldAt, $soldOn, $lines, $creditsSaleId, $receiptEmail);
}
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;
}
public function receiptEmail(): ?EmailAddress
{
return $this->receiptEmail;
}
}