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

83 lines
2.7 KiB
PHP

<?php
namespace KupShop\ShoppingListBundle\Admin;
use KupShop\KupShopBundle\Util\Compat\ServiceContainer;
use KupShop\ShoppingListBundle\Util\ShoppingListUtil;
use Query\Operator;
class ShoppingList extends \Window
{
protected $tableName = 'shopping_list';
/** @var ShoppingListUtil */
public $shoppingListUtil;
public function __construct()
{
$this->shoppingListUtil = ServiceContainer::getService(ShoppingListUtil::class);
}
public function get_vars()
{
$vars = parent::get_vars();
if ($ID = $this->getID()) {
$sl = $this->shoppingListUtil->getShoppingList($ID);
$vars['items'] = $this->shoppingListUtil->fetchItems($ID, true)->getProducts();
$vars['name'] = $sl->getName();
$vars['id_user'] = $sl->getIdUser();
$vars['note'] = $sl->getNote();
$vars['date'] = $sl->getDate() ?: date('Y-m-d');
}
return $vars;
}
public function handleUpdate()
{
$SQL = parent::handleUpdate(); // TODO: Change the autogenerated stub
$data = $this->getData();
$id = $this->getID();
$acn = $this->getAction();
if ($SQL && !empty($data)) {
if ($acn == 'add') {
$this->shoppingListUtil->updateHash($id, date('Y-m-d'));
$this->processShoppingListItems($id, $data['items'], true);
$this->returnOK('Nákupní seznam byl vytvořen');
} elseif ($acn == 'edit') {
$this->processShoppingListItems($id, $data['items']);
$this->returnOK('Nákupní seznam byl upraven');
}
}
return $SQL;
}
protected function processShoppingListItems($id_shopping_list, $items, $duplicate = false): void
{
if (is_iterable($items)) {
foreach ($items as $id => $item) {
if ($id == 0) {
continue;
}
if ($id > 0 && isset($item['delete'])) {
$this->shoppingListUtil->removeProductFromList($item['id']);
} elseif ($id > 0 && !$duplicate) {
sqlQueryBuilder()->update('shopping_list_products')
->set('pieces', $item['pieces'])
->where(Operator::equals(['id' => $item['id']]))->execute();
} elseif (!empty($item['id_product'])) {
$this->shoppingListUtil->addProductToShoppingList(
(int) $id_shopping_list,
(int) $item['id_product'],
(int) $item['id_variation'] ?: null,
floatval($item['pieces']));
}
}
}
}
}
return ShoppingList::class;