124 lines
4 KiB
PHP
124 lines
4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Domain\Services;
|
|
|
|
use Domain\Enums\OrderStatus;
|
|
use Domain\Enums\ProductBrand;
|
|
use Domain\Models\Entities\Basket;
|
|
use Domain\Models\Entities\Order;
|
|
use Domain\Models\Entities\Shop;
|
|
use Domain\Models\Entities\Shopper;
|
|
use Domain\Models\ValueObjects\DeliveryAddress;
|
|
use Domain\Models\ValueObjects\Entities\OrderId;
|
|
use Domain\Models\ValueObjects\OrderEmployee;
|
|
use Domain\Models\ValueObjects\OrderLine;
|
|
use Domain\Models\ValueObjects\OrderLineProduct;
|
|
use Domain\Models\ValueObjects\OrderReference;
|
|
use Domain\Models\ValueObjects\PickupTimeSlot;
|
|
use Domain\Models\ValueObjects\ProductWeight;
|
|
use Domain\Ports\Repositories\OrderRepository;
|
|
use Domain\Ports\Services\UuidConverter;
|
|
use Ramsey\Uuid\Uuid;
|
|
|
|
class OrderService
|
|
{
|
|
public function __construct(
|
|
private readonly UuidConverter $uuidConverter,
|
|
private readonly OrderRepository $orderRepository,
|
|
) {
|
|
}
|
|
|
|
public function createOrderFromBasket(
|
|
Basket $basket,
|
|
PickupTimeSlot $pickupTimeSlot,
|
|
Shopper $shopper,
|
|
Shop $shop
|
|
): Order {
|
|
$lines = [];
|
|
foreach ($basket->items() as $item) {
|
|
$orderLineProduct = new OrderLineProduct(
|
|
incoId: $item->incoId ?? throw new \DomainException('incoId cannot be null for an order'),
|
|
originalId: $item->id,
|
|
name: $item->name,
|
|
weight: new ProductWeight($item->weightGrams, $item->weightVariable),
|
|
brand: $item->brand ?? ProductBrand::none,
|
|
orderUnit: $item->orderUnit,
|
|
price: clone $item->unitPriceHt,
|
|
discountedPrice: $item->unitPriceHt->subtract($item->unitDiscount),
|
|
priceKgTtc: clone $item->priceKgTtc,
|
|
taxRate: $item->taxRate,
|
|
picture: $item->picture
|
|
);
|
|
|
|
$lines[] = new OrderLine(
|
|
product: $orderLineProduct,
|
|
quantity: $item->qty,
|
|
unitPriceHt: clone $item->unitPriceHt,
|
|
unitDiscount: clone $item->unitDiscount,
|
|
unitTax: clone $item->taxRate
|
|
->applyTo($item->unitPriceHt
|
|
->subtract($item->unitDiscount))
|
|
->subtract($item->unitPriceHt
|
|
->subtract($item->unitDiscount)),
|
|
totalHt: clone $item->subtotalHt(),
|
|
totalDiscount: clone $item->discountTotal(),
|
|
totalTax: clone $item->taxTotal(),
|
|
totalTtc: clone $item->totalTtc()
|
|
);
|
|
}
|
|
|
|
return new Order(
|
|
id: $this->newOrderId(),
|
|
shopId: clone $shop->id(),
|
|
reference: $this->newReference(),
|
|
employee: new OrderEmployee(
|
|
matricule: clone $shopper->matricule,
|
|
firstname: $shopper->prenom,
|
|
lastname: $shopper->nom,
|
|
facility: $shopper->etablissement,
|
|
site: $shopper->site,
|
|
service: $shopper->service
|
|
),
|
|
deliveryAddress: new DeliveryAddress(
|
|
name: $shop->name,
|
|
address: $shop->address,
|
|
postcode: $shop->postcode,
|
|
city: $shop->city,
|
|
phone: $shop->phone
|
|
),
|
|
pickupTimeSlot: clone $pickupTimeSlot,
|
|
status: OrderStatus::VALIDATED,
|
|
total: clone $basket->totalTaxIncluded(),
|
|
lines: $lines
|
|
);
|
|
}
|
|
|
|
private function newOrderId(): OrderId
|
|
{
|
|
return new OrderId(Uuid::uuid7());
|
|
}
|
|
|
|
private function newReference(): OrderReference
|
|
{
|
|
return new OrderReference(
|
|
$this->uuidConverter->toBase58(
|
|
Uuid::uuid7()
|
|
)
|
|
);
|
|
}
|
|
|
|
public function cancelOrder(OrderId $id): void
|
|
{
|
|
$order = $this->orderRepository->find($id);
|
|
|
|
if (! $order) {
|
|
throw new \DomainException('Order not found');
|
|
}
|
|
|
|
$order->cancel();
|
|
|
|
$this->orderRepository->persist($order);
|
|
}
|
|
}
|