Files
2025-08-02 16:30:27 +02:00

178 lines
5.2 KiB
PHP

<?php
declare(strict_types=1);
namespace KupShop\SalesBundle\Feed\Wrapper;
use Doctrine\Common\Collections\ArrayCollection;
use KupShop\FeedsBundle\FeedProductCollection;
use KupShop\FeedsBundle\Wrapper\BaseWrapper;
use KupShop\FeedsBundle\Wrapper\CurrencyWrapper;
use KupShop\FeedsBundle\Wrapper\PriceVatAwareWrapper;
use KupShop\FeedsBundle\Wrapper\ProductWrapper;
use KupShop\KupShopBundle\Context\CurrencyContext;
use KupShop\KupShopBundle\Util\Contexts;
use KupShop\OrderingBundle\Feed\Wrapper\OrderDeliveryTypeWrapper;
use KupShop\OrderingBundle\Feed\Wrapper\TotalPriceWrapper;
use KupShop\SalesBundle\Entity\Sale;
use KupShop\SalesBundle\Entity\SaleItem;
use KupShop\SellerBundle\Feed\Wrapper\SellerWrapper;
use KupShop\SellerBundle\Utils\SellerUtil;
use KupShop\UserBundle\Feed\Wrapper\UserWrapper;
class SaleWrapper extends BaseWrapper
{
protected static $objectType = Sale::class;
/** @var Sale */
protected $object;
protected ?array $feedRow = null;
public function __construct(
protected readonly UserWrapper $userWrapper,
protected readonly CurrencyWrapper $currencyWrapper,
protected readonly PriceVatAwareWrapper $priceVatAwareWrapper,
protected readonly TotalPriceWrapper $totalPriceWrapper,
protected readonly OrderDeliveryTypeWrapper $orderDeliveryTypeWrapper,
protected readonly SellerWrapper $sellerWrapper,
protected readonly ProductWrapper $productWrapper,
protected readonly SellerUtil $sellerUtil,
) {
}
/**
* @private
*/
public function setFeedRow(?array $feedRow): void
{
$this->feedRow = $feedRow;
}
/** Interní ID prodejky */
public function field_id(): int
{
return $this->object->id;
}
/** Datum vytvoření.
*
* Použití: utils.formatDate(date)
*/
public function field_date_created(): \DateTimeInterface
{
// https://github.com/phpv8/v8js converts only PHP's \DateTime to js Date, \DateTimeImmutable would be passed as a regular object
return \DateTime::createFromInterface($this->object->dateCreated);
}
/** Měna */
public function field_currency(): CurrencyWrapper
{
return $this->currencyWrapper->setObject(
Contexts::get(CurrencyContext::class)->getAll()[$this->object->currency]
);
}
/** Jazyk */
public function field_language(): string
{
return $this->object->languageId;
}
public function field_number(): string
{
return $this->object->code;
}
/** Celková cena objednávky */
public function field_total_price(): TotalPriceWrapper
{
return (clone $this->totalPriceWrapper)->setObject(
$this->object->totalPrice
);
}
/** Poznámka */
public function field_note(): ?string
{
return $this->object->note;
}
/** Uživatel */
public function field_user(): ?UserWrapper
{
if (!($user = $this->object->getUser())) {
return null;
}
return $this->userWrapper->setObject($user);
}
public function field_deliveryType(): ?OrderDeliveryTypeWrapper
{
if (!($deliveryType = $this->object->getDeliveryType())) {
return null;
}
return $this->orderDeliveryTypeWrapper->setObject($deliveryType->getDelivery());
}
public function field_seller(): ?SellerWrapper
{
if (!$this->object->sellerId) {
return null;
}
if (!$this->sellerWrapper->sellers) {
$sellers = array_map(fn ($x) => (object) $x, $this->sellerUtil->getSellers());
$this->sellerWrapper->sellers = new ArrayCollection($sellers);
}
if ($this->sellerWrapper->sellers[$this->object->sellerId] ?? false) {
return $this->sellerWrapper->setObject($this->sellerWrapper->sellers[$this->object->sellerId]);
}
return null;
}
/** Dodatečná data */
public function field_data(): array
{
return $this->object->data;
}
public function field_items(): array
{
$items = [];
/** @var SaleItem $item */
foreach ($this->object->items as $item) {
$productWrapper = null;
if ($product = $item->getProduct()) {
$wrapper = clone $this->productWrapper;
$collection = new FeedProductCollection();
$collection->setEntityType(\FilterParams::ENTITY_VARIATION);
$key = implode('/', array_filter([$item->productId, $item->variationId]));
$collection->set($key, $product);
$wrapper->setProductCollection($collection);
$productWrapper = $wrapper->setObject($product);
}
$items[] = [
'id' => $item->id,
'id_product' => $item->productId,
'id_variation' => $item->variationId,
'pieces' => $item->pieces,
'name' => $item->name,
'piece_price' => $this->priceVatAwareWrapper->setObject($item->piecePrice),
'total_price' => $this->priceVatAwareWrapper->setObject($item->totalPrice),
'product' => $productWrapper,
];
}
return $items;
}
}