Files
kupshop/bundles/KupShop/PreordersBundle/Admin/preordersCreateOrder.php
2025-08-02 16:30:27 +02:00

77 lines
2.1 KiB
PHP

<?php
namespace KupShop\PreordersBundle\Admin;
use KupShop\KupShopBundle\Util\Compat\ServiceContainer;
use KupShop\PreordersBundle\Entity\UserPreorder;
use KupShop\PreordersBundle\Service\DynamicContexts;
use KupShop\PreordersBundle\Service\OrderCreator;
class preordersCreateOrder extends \Window
{
protected $tableName = 'preorders_items';
private readonly DynamicContexts $contexts;
private ?UserPreorder $userPreorder = null;
public function __construct()
{
$this->contexts = ServiceContainer::getService(DynamicContexts::class);
}
private function getUserPreorder(): UserPreorder
{
if ($this->userPreorder === null) {
[$userId, $dateId] = explode('-', $this->getID());
$this->userPreorder = new UserPreorder((int) $userId, (int) $dateId);
}
return $this->userPreorder;
}
protected function getObject()
{
return $this->getUserPreorder()->getTemplateVars();
}
public function get_vars()
{
$vars = parent::get_vars();
$this->contexts->activateUserPreorder($this->getUserPreorder());
return $vars;
}
public function hasRights($name = null): bool
{
switch ($name) {
case \Window::RIGHT_DUPLICATE:
case \Window::RIGHT_DELETE:
return false;
default:
return true;
}
}
public function handleUpdate(): true
{
$vars = $this->get_vars();
$data = $vars['body']['data'];
$items = isset($data['items']) ? array_filter($data['items']) : [];
if (!empty($items)) {
/** @var OrderCreator $orderCreator */
$orderCreator = ServiceContainer::getService(OrderCreator::class);
$userPreorder = new UserPreorder((int) $data['id_user'], (int) $data['id_preorder_date']);
$order = $orderCreator->createOrder($userPreorder, $items);
$this->returnOK("Objednávka {$order->order_no} úspěšně vytvořena.");
}
return true;
}
}
return preordersCreateOrder::class;