Updated the OrderService to utilize the new fromBasket method for creating orders, enhancing code clarity and maintainability. Removed the ProductAvailabilityService as its functionality is now integrated into the ProductRepository.
69 lines
2.2 KiB
PHP
69 lines
2.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Domain\Models\ValueObjects;
|
|
|
|
use Money\Money;
|
|
|
|
final readonly class OrderLine
|
|
{
|
|
public Money $unitTtc;
|
|
|
|
public function __construct(
|
|
public OrderLineProduct $product,
|
|
public int $quantity,
|
|
public Money $unitPriceHt,
|
|
public Money $unitDiscount,
|
|
public Money $unitTax,
|
|
public Money $totalHt,
|
|
public Money $totalDiscount,
|
|
public Money $totalTax,
|
|
public Money $totalTtc,
|
|
) {
|
|
$this->unitTtc = $unitPriceHt->subtract($unitDiscount)->add($unitTax);
|
|
|
|
if ($quantity <= 0) {
|
|
throw new \DomainException('Quantity must be greater than 0');
|
|
}
|
|
if ($unitPriceHt->isNegative()) {
|
|
throw new \DomainException('Unit price must be non-negative');
|
|
}
|
|
if ($unitDiscount->isNegative()) {
|
|
throw new \DomainException('Unit discount must be non-negative');
|
|
}
|
|
if ($unitTax->isNegative()) {
|
|
throw new \DomainException('Unit tax must be non-negative');
|
|
}
|
|
if ($this->unitTtc->isNegative()) {
|
|
throw new \DomainException('Unit TTC must be non-negative');
|
|
}
|
|
if ($totalHt->isNegative()) {
|
|
throw new \DomainException('Total HT must be non-negative');
|
|
}
|
|
if ($totalDiscount->isNegative()) {
|
|
throw new \DomainException('Total discount must be non-negative');
|
|
}
|
|
if ($totalTax->isNegative()) {
|
|
throw new \DomainException('Total tax must be non-negative');
|
|
}
|
|
if ($totalTtc->isNegative()) {
|
|
throw new \DomainException('Total TTC must be non-negative');
|
|
}
|
|
}
|
|
|
|
public static function fromBasketProduct(BasketProduct $item): self
|
|
{
|
|
return new self(
|
|
product: OrderLineProduct::fromBasketProduct($item),
|
|
quantity: $item->qty,
|
|
unitPriceHt: clone $item->unitPriceHt,
|
|
unitDiscount: clone $item->unitDiscount,
|
|
unitTax: clone $item->unitTax(),
|
|
totalHt: clone $item->subtotalHt(),
|
|
totalDiscount: clone $item->discountTotal(),
|
|
totalTax: clone $item->taxTotal(),
|
|
totalTtc: clone $item->totalTtc()
|
|
);
|
|
}
|
|
}
|