149 lines
3.9 KiB
PHP
149 lines
3.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace KupShop\PreordersBundle\Dto;
|
|
|
|
use KupShop\CatalogBundle\ProductList\ProductCollection;
|
|
use KupShop\KupShopBundle\Arrayable;
|
|
use KupShop\KupShopBundle\Util\Price\TotalPrice;
|
|
use KupShop\PreordersBundle\Entity\Preorder;
|
|
use KupShop\PreordersBundle\Service\PreorderUtil;
|
|
use KupShop\PreordersBundle\Util\ProductsResult;
|
|
|
|
class PreorderDateInfo implements DateInfo, Arrayable, \ArrayAccess
|
|
{
|
|
private array $asArray;
|
|
private array $validDates;
|
|
private array $validPreorders;
|
|
|
|
public function __construct(
|
|
private readonly Preorder $preorder,
|
|
private readonly \User $user,
|
|
private readonly ProductsResult $productsAndPrices,
|
|
) {
|
|
}
|
|
|
|
public function getPreorder(): Preorder
|
|
{
|
|
return $this->preorder;
|
|
}
|
|
|
|
public function getUser(): \User
|
|
{
|
|
return $this->user;
|
|
}
|
|
|
|
public function getValidDates(): array
|
|
{
|
|
return $this->validDates ??= PreorderUtil::getOpenDates($this->user, [$this->preorder->id]);
|
|
}
|
|
|
|
/**
|
|
* @return Preorder[]
|
|
*/
|
|
public function getValidPreorders(): array
|
|
{
|
|
if (!isset($this->validPreorders)) {
|
|
$this->validPreorders = [];
|
|
|
|
$validPreorderIds = array_unique(array_column($this->getValidDates(), 'id_preorder'));
|
|
// TODO: multifetch Preorders?
|
|
foreach ($validPreorderIds as $id) {
|
|
$this->validPreorders[] = new Preorder($id);
|
|
}
|
|
}
|
|
|
|
return $this->validPreorders;
|
|
}
|
|
|
|
public function getProducts(): ProductCollection
|
|
{
|
|
return $this->productsAndPrices->products;
|
|
}
|
|
|
|
public function getTotalPrice(): TotalPrice
|
|
{
|
|
return $this->productsAndPrices->totalPrice;
|
|
}
|
|
|
|
public function getTotalBasePrice(): TotalPrice
|
|
{
|
|
return $this->productsAndPrices->totalBasePrice;
|
|
}
|
|
|
|
public function getPartialBasePrice(): ?TotalPrice
|
|
{
|
|
return $this->productsAndPrices->partialTotals?->basePrice;
|
|
}
|
|
|
|
public function getPartialTotalPrice(): ?TotalPrice
|
|
{
|
|
return $this->productsAndPrices->partialTotals?->totalPrice;
|
|
}
|
|
|
|
public function isExportAvailable(): bool
|
|
{
|
|
return count($this->preorder->getItemsByUser((int) $this->user->id)) > 0;
|
|
}
|
|
|
|
public function getDiscountLevels(): array
|
|
{
|
|
return $this->preorder->getSettings()['dynamic_prices'] ?? [];
|
|
}
|
|
|
|
public function toArray(): array
|
|
{
|
|
return $this->asArray ??= array_merge($this->preorder->toArray(), [
|
|
'id_preorder' => $this->getPreorder()->id,
|
|
|
|
'dates' => $this->getValidDates(), // only permitted dates for a single preorder - replace those from $preorder->toArray()
|
|
'preorders' => $this->getValidPreorders(),
|
|
|
|
'products' => $this->getProducts(),
|
|
'price_total' => $this->getTotalPrice(),
|
|
'base_price_total' => $this->getTotalBasePrice(),
|
|
|
|
'partial_price_base' => $this->getPartialBasePrice(),
|
|
'partial_price_total' => $this->getPartialTotalPrice(),
|
|
|
|
'export_available' => $this->isExportAvailable(),
|
|
'discount_levels' => $this->getDiscountLevels(),
|
|
]);
|
|
}
|
|
|
|
public function offsetExists(mixed $offset): bool
|
|
{
|
|
return isset($this->toArray()[$offset]);
|
|
}
|
|
|
|
public function offsetGet(mixed $offset): mixed
|
|
{
|
|
return $this->toArray()[$offset];
|
|
}
|
|
|
|
public function offsetSet(mixed $offset, mixed $value): void
|
|
{
|
|
throw new \LogicException('Not allowed');
|
|
}
|
|
|
|
public function offsetUnset(mixed $offset): void
|
|
{
|
|
throw new \LogicException('Not allowed');
|
|
}
|
|
|
|
public function getId(): int
|
|
{
|
|
return $this->getPreorder()->id;
|
|
}
|
|
|
|
public function getPiecesSum(): int
|
|
{
|
|
return array_reduce(
|
|
$this->preorder->getItemsByUser($this->user),
|
|
static fn (int $acc, array $item) => $acc + $item['pieces'],
|
|
0,
|
|
);
|
|
}
|
|
}
|