Files
Toiminnanohjaus/app/Modules/Sales/Application/DTOs/QuickSaleSummary.php
T

84 lines
2.4 KiB
PHP

<?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.
* $receiptEmail is the address the receipt was emailed to; it is shown
* only on the sales list, never in reports.
*/
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 ?string $receiptEmail = null,
public ?string $receiptSentAt = null,
public bool $receiptFailed = false,
) {}
/**
* The emailed receipt's state: null without an address, otherwise
* 'sent', 'failed' or 'pending' (still in the queue).
*/
public function receiptStatus(): ?string
{
return match (true) {
$this->receiptEmail === null => null,
$this->receiptFailed => 'failed',
$this->receiptSentAt !== null => 'sent',
default => 'pending',
};
}
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),
);
}
}