Files

108 lines
4.3 KiB
PHP

<?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\Contracts\ReceiptMailer;
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\Shared\Domain\Exceptions\InvalidEmailAddress;
use App\Modules\Shared\Domain\ValueObjects\EmailAddress;
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. When an address is
* given, the PDF receipt is emailed to it after the sale is stored.
*/
final class SellQuickSale
{
public function __construct(
private QuickSaleRepository $sales,
private CashDayRepository $cashDays,
private QuickSaleProducts $products,
private QuickSaleHours $hours,
private ReceiptMailer $mailer,
private CustomerQueries $customers,
private TransactionManager $transactions,
private Clock $clock,
) {}
/**
* @throws ProductNotSellable
* @throws InvalidQuantity
* @throws QuickSaleEmpty
* @throws QuickSaleCustomerNotFound
* @throws CashDayClosed
* @throws InvalidEmailAddress
*/
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);
}
$receiptEmail = $data->receiptEmail === null || trim($data->receiptEmail) === '' ? null : new EmailAddress($data->receiptEmail);
$today = $this->clock->today();
$sale = QuickSale::sell($sellerId, $data->customerId, PaymentMethod::from($data->paymentMethod), $lines, $this->clock->now(), $today, $receiptEmail);
$result = $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, $sale->receiptEmail()?->value);
});
if ($result->receiptEmail !== null) {
$this->mailer->send($result->saleId);
}
return $result;
}
}