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

139 lines
5.0 KiB
PHP

<?php
namespace KupShop\POSBundle\Admin;
use KupShop\KupShopBundle\Context\CountryContext;
use KupShop\KupShopBundle\Context\CurrencyContext;
use KupShop\KupShopBundle\Context\DomainContext;
use KupShop\KupShopBundle\Context\LanguageContext;
use KupShop\KupShopBundle\Context\PriceLevelContext;
use KupShop\KupShopBundle\Util\Contexts;
use KupShop\KupShopBundle\Util\Functional\Mapping;
use KupShop\PricelistBundle\Context\PricelistContext;
use Query\Operator;
class PosSettings extends \Window
{
private DomainContext $domainContext;
private CountryContext $countryContext;
private LanguageContext $languageContext;
protected $tableName = 'pos';
protected $nameField = 'id';
protected $template = 'window/posSettings.tpl';
public function __construct()
{
$this->domainContext = Contexts::get(DomainContext::class);
$this->countryContext = Contexts::get(CountryContext::class);
$this->languageContext = Contexts::get(LanguageContext::class);
}
public function getData()
{
$data = parent::getData();
if (getVal('Submit')) {
$data['date_created'] = $this->prepareDateTime(new \DateTime());
$this->serializeCustomData($data);
}
return $data;
}
public function handleUpdate()
{
$data = $this->getData();
if (empty($data['name'])) {
$this->returnError('Není zadán název pokladny');
return false;
}
if (empty($data['card_delivery_type']) && empty($data['cash_delivery_type']) && empty($data['invoice_delivery_type']) && empty($data['custom_delivery_type'])) {
$this->returnError('Není vybrán žádný způsob doručení');
return false;
}
return parent::handleUpdate();
}
public function get_vars()
{
$vars = parent::get_vars();
$deliveryTypes = $this->loadDeliveryTypes();
// Filters all available delivery types for payment methods
$paymentTypes = Mapping::mapKeys(\Payment::listClasses(), function ($class) {
$payment = \Payment::getClass($class);
return [$class, $payment->getPayMethod()];
});
foreach ($deliveryTypes as $type) {
switch ($paymentTypes[$type['class_payment']]) {
case \Payment::METHOD_CARD:
$vars['body']['card_delivery_type'][] = $type;
break;
case \Payment::METHOD_INVOICE:
case \Payment::METHOD_TRANSFER:
$vars['body']['invoice_delivery_type'][] = $type;
break;
case \Payment::METHOD_CASH:
$vars['body']['cash_delivery_type'][] = $type;
break;
default:
// Skip
}
}
$vars['body']['custom_delivery_type'] = $deliveryTypes;
$vars['body']['domain_contexts'] = $this->domainContext->getSupported();
if (method_exists($this->countryContext, 'getAllActive')) {
$vars['body']['country_contexts'] = $this->countryContext->getAllActive();
} else {
$vars['body']['country_contexts'] = $this->countryContext->getSupported();
}
$vars['body']['language_contexts'] = $this->languageContext->getSupported();
$vars['body']['pricelist_contexts'] = [];
$vars['body']['currency_contexts'] = [];
$vars['body']['pricelevel_contexts'] = [];
if (findModule(\Modules::PRICELISTS)) {
$priceListContext = Contexts::get(PricelistContext::class);
$vars['body']['pricelist_contexts'] = $priceListContext->getSupported();
}
if (findModule(\Modules::CURRENCIES)) {
$currencyContext = Contexts::get(CurrencyContext::class);
$vars['body']['currency_contexts'] = $currencyContext->getSupported();
}
if (findModule(\Modules::PRICE_LEVELS)) {
$priceLevelContext = Contexts::get(PriceLevelContext::class);
$vars['body']['pricelevel_contexts'] = $priceLevelContext->getSupported() ?? [];
}
return $vars;
}
private function loadDeliveryTypes(): array
{
return sqlQueryBuilder()
->select('dt.id, dtd.id as id_delivery, dtp.id as id_payment, dtd.class as class_delivery, dtp.class as class_payment, CONCAT(COALESCE(dtp.name_admin, dtp.name), " - ", COALESCE(dtd.name_admin, dtd.name)) as name')
->from('delivery_type', 'dt')
->leftJoin('dt', 'delivery_type_delivery', 'dtd', 'dtd.id=dt.id_delivery')
->leftJoin('dt', 'delivery_type_payment', 'dtp', 'dtp.id=dt.id_payment')
->andWhere(Operator::inStringArray(array_filter(array_keys(\Delivery::listClasses()), function ($class) {
return \Delivery::getClass($class)->getType() === \Delivery::TYPE_IN_PERSON;
}), 'dtd.class'))
->execute()
->fetchAllAssociative();
}
}
return PosSettings::class;