Files
kupshop/admin/lists/DeliveryDeliveryList.php
2025-08-02 16:30:27 +02:00

152 lines
5.3 KiB
PHP

<?php
use KupShop\AdminBundle\AdminList\BaseList;
use KupShop\AdminBundle\Query\Invert;
use KupShop\KupShopBundle\Query\JsonOperator;
use Query\Operator;
class DeliveryDeliveryList extends BaseList
{
use AdminListSortable;
protected $template = 'listSortable.tpl';
protected $showMassEdit = true;
protected $tableName = 'delivery_type_delivery';
protected ?string $tableAlias = 'dtd';
protected $tableDef = [
'id' => 'id',
'fields' => [
'Pořadí' => ['field' => 'position', 'render' => 'renderPosition', 'size' => 0.3, 'fieldType' => BaseList::TYPE_POSITION],
'Název' => ['field' => 'name_admin', 'fieldType' => BaseList::TYPE_STRING],
'Název v košíku' => ['field' => 'name', 'visible' => 'N', 'fieldType' => BaseList::TYPE_STRING],
'Termín doručení' => ['field' => 'time_days', 'render' => 'renderDelivery', 'class' => 'center'],
'Cena s DPH' => ['field' => 'price', 'render' => 'renderPriceVat', 'class' => 'right', 'fieldType' => BaseList::TYPE_PRICE_WITH_CURRENCY],
'Cena s DPH (reg.)' => ['field' => 'price_registered', 'visible' => 'N', 'render' => 'renderPriceVat', 'class' => 'right', 'fieldType' => BaseList::TYPE_PRICE_WITH_CURRENCY],
'Nezapočítat od' => ['field' => 'price_dont_countin_from', 'render' => 'renderPrice', 'class' => 'right', 'fieldType' => BaseList::TYPE_FLOAT_WITH_CURRENCY],
'Nezapočítat od (reg.)' => ['field' => 'price_dont_countin_from_reg', 'visible' => 'N', 'render' => 'renderPrice', 'class' => 'right', 'fieldType' => BaseList::TYPE_FLOAT_WITH_CURRENCY],
],
];
public function __construct()
{
if (findModule('deliveries')) {
$this->tableDef['fields']['Osobní odběr'] = ['field' => 'in_person', 'raw_field' => '(class = "OsobniOdber")', 'render' => 'renderInPerson', 'class' => 'center'];
$this->tableDef['fields']['Dopravce'] = ['field' => 'class', 'render' => 'renderDeliveryClass'];
}
if (findModule('delivery_pricelist')) {
$this->tableDef['fields']['Ceník dopravy'] = ['field' => 'id_pricelist', 'render' => 'renderPricelist'];
}
}
public function renderPriceList($values, $column)
{
$pricelistId = $values[$column['field']];
if ($pricelistId == null) {
return 'nepoužívat';
} else {
return $this->getPricelistName($pricelistId);
}
}
public function renderInPerson($values, $column)
{
if (isset($values['in_person'])) {
return $this->renderBoolean($values, $column);
}
$delivery = Delivery::getClass($values['class']);
$values['in_person'] = is_null($delivery) ? false : $delivery->isInPerson();
return $this->renderBoolean($values, $column);
}
public function renderDelivery($values, $column)
{
$value = $this->getListRowValue($values, $column['field']);
$hours = $this->getListRowValue($values, 'time_hours');
if ($value === '0') {
$value = 'ihned';
} elseif ($value > 0) {
$value .= ' dnů';
}
if ($hours) {
$value .= " do {$hours}";
}
return $value;
}
public function renderDeliveryClass($values, $column)
{
$value = $this->getListRowValue($values, $column['field']);
if (!$value) {
return $value;
}
$class = Delivery::getClass($value);
return $class::$className;
}
private function getPricelistName($id)
{
/** @var \Doctrine\ORM\EntityManager $em */
$em = \KupShop\KupShopBundle\Util\Compat\ServiceContainer::getService('doctrine.orm.entity_manager');
$repository = $em->getRepository(\KupShop\DeliveryPriceListBundle\Entity\PriceList::class);
$data = $repository->findOneBy(['id' => $id]);
return $data->getName();
}
public function handle()
{
parent::handle();
$item = getVal('moved_item');
if (!empty($item)) {
$this->saveList($item, 'delivery_type_delivery', 'position');
exit('{}');
}
}
public function getQuery()
{
$qb = sqlQueryBuilder()->select('dtd.*, COALESCE(dtd.name_admin, dtd.name) AS name_admin', 'dtd.id', 'dtd.name')
->from('delivery_type_delivery', 'dtd')
->orderBy('dtd.position');
$name = getVal('name');
if (isset($name) && $name) {
$qb->andWhere('dtd.name_admin LIKE :name_admin')->setParameter('name_admin', '%'.$name.'%');
}
$nameCart = getVal('nameCart');
if (isset($nameCart) && $nameCart) {
$qb->andWhere('dtd.name LIKE :name')->setParameter('name', '%'.$nameCart.'%');
}
$delivery = getVal('id_delivery');
if ($delivery) {
$qb->andWhere(Invert::checkInvert(Operator::inStringArray($delivery, 'dtd.class'), getVal('id_delivery_invert')));
}
$country = getVal('country');
if ($country) {
$operator = array_map(function ($value) {
return JsonOperator::search('dtd.data', $value, 'one', '$.restrictions.countries');
}, $country);
$qb->andWhere(Invert::checkInvert(Operator::orX($operator), getVal('country_invert')));
}
return $qb;
}
}