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

220 lines
7.1 KiB
PHP

<?php
$main_class = 'DeliveryTypesDelivery';
use KupShop\KupShopBundle\Context\ContextManager;
use KupShop\KupShopBundle\Context\CountryContext;
use KupShop\KupShopBundle\Context\CurrencyContext;
use KupShop\KupShopBundle\Util\Compat\ServiceContainer;
class DeliveryTypesDelivery extends Window
{
protected $tableName = 'delivery_type_delivery';
/** @var CurrencyContext */
private $currencyContext;
private $countryContext;
private $contextManager;
private $delivery;
protected $required = ['price' => true];
public function __construct()
{
$this->currencyContext = ServiceContainer::getService(CurrencyContext::class);
$this->countryContext = ServiceContainer::getService(CountryContext::class);
$this->contextManager = ServiceContainer::getService(ContextManager::class);
}
public function get_vars()
{
$vars = parent::get_vars();
$pageVars = getVal('body', $vars);
$vat = getAdminVat()['value'];
$dbcfg = Settings::getDefault();
$pageVars['data']['priceWithVat'] = $dbcfg['prod_prefer_price_vat'] == 'Y' || $dbcfg['prod_prefer_price_vat'] == 'F';
if (isset($pageVars['data']) && !empty($pageVars['data']['price']) && $pageVars['data']['priceWithVat']) {
$pageVars['data']['price'] = toDecimal($pageVars['data']['price'])->mul(toDecimal(1 + ($vat / 100)));
}
if (!empty($pageVars['data']['price_registered'])) {
$pageVars['data']['price_registered'] *= 1 + ($vat / 100);
}
if (!empty($pageVars['data']['countries'])) {
$pageVars['data']['countries'] = json_decode($pageVars['data']['countries']);
}
if (findModule('delivery_pricelist')) {
$pageVars['deliveryPricelists'] = $this->getDeliveryPricelists();
}
$pageVars['data']['currencies'] = $this->currencyContext->getAll();
if ($this->getAction() == 'add') {
$pageVars['data']['currency'] = $this->currencyContext->getDefaultId();
}
$pageVars['data']['template'] = $this->getDeliveryObject()->getAdminWindowTemplate();
$pageVars['data']['custom_data'] = $this->getCustomData();
$vars['body'] = $pageVars;
return $vars;
}
private function getDeliveryPricelists()
{
/** @var \Doctrine\ORM\EntityManager $em */
$em = \KupShop\KupShopBundle\Util\Compat\ServiceContainer::getService('doctrine.orm.entity_manager');
$repository = $em->getRepository(\KupShop\DeliveryPriceListBundle\Entity\PriceList::class);
return $repository->findAll();
}
protected function getObject()
{
$data = parent::getObject();
$data['photo_array'] = $this->getDeliveryObject()->getPhoto();
return $data;
}
public function getData()
{
global $cfg;
$data = parent::getData();
if (getVal('Submit')) {
$data['price'] = $this->prepareVatPrice($data['price']);
$data['price_registered'] = $this->prepareVatPrice($data['price_registered']);
if (!empty($data['price_buy'])) {
$data['price_buy'] = $this->prepareVatPrice($data['price_buy']);
}
if (!empty($data['countries']) && count(array_diff(array_keys($this->countryContext->getAll()), $data['countries'])) != 0) {
$data['countries'] = json_encode($data['countries']);
} else {
$data['countries'] = null;
}
if ($widgetOptions = $data['custom_data']['widget_options'] ?? false) {
if (is_array($widgetOptions)) {
$data['custom_data']['widget_options'] = $widgetOptions;
} else {
$data['custom_data']['widget_options'] = json_decode($widgetOptions, true);
if (json_last_error()) {
$this->returnError(sprintf(
'%s: %s',
translate('widgetOptions', 'deliveryDelivery', true, true),
json_last_error_msg(),
));
}
}
} else {
unset($data['custom_data']['widget_options']);
}
if (empty($data['custom_data']['users_groups'])) {
unset($data['custom_data']['users_groups_invert']);
}
}
$delivery = Delivery::getClass($data['class'] ?? null);
$custom_data = $delivery->processAdminWindowData($data['custom_data'] ?? []);
$oldCustomData = $this->getCustomData();
if (isset($oldCustomData['restrictions'])) {
$custom_data['restrictions'] = $oldCustomData['restrictions']; // preserve restrictions - already saved in DeliveryRestrictions tab
}
if (isset($oldCustomData['balikobot'])) {
$custom_data['balikobot'] = $oldCustomData['balikobot']; // preserve balikobot - already saved in BalikobotDeliveryTab
}
$this->setCustomData($custom_data);
return $data;
}
public function handleUpdate()
{
$data = $this->getData();
$SQL = parent::handleUpdate();
clearCache('freeDelivery_', true);
if (findModule('delivery_pricelist')) {
// Pricelist id update
if ($SQL) {
if (!empty($data['pricelist']['id_pricelist'])) {
$pricelistId = $data['pricelist']['id_pricelist'];
if ($pricelistId == 'null') {
$pricelistId = null;
}
$this->updateSQL($this->getTableName(), ['id_pricelist' => $pricelistId], ['id' => $this->getID()]);
}
}
}
// Add photo
$img = new Photos('delivery');
$img->newImage($this->getID());
if (!empty($_FILES['photo']['name'])) {
$img->uploadImage($_FILES['photo']);
if ($img->checkFileType()) {
$img->insertImageIntoDB();
} else {
$this->addError(getTextString('producers', 'errorBadPhoto'));
}
}
// Clear delivery thumbnail
$img->clearThumbnails([9]);
return $SQL;
}
public function handleDelete()
{
// Delete photo
$img = new Photos('delivery');
$img->newImage($this->getID());
$img->deletePhoto(false);
parent::handleDelete();
}
public function handleErasephoto()
{
// ########################################################################
$img = new Photos('delivery');
$img->newImage($this->getID());
$img->deletePhoto();
// ########################################################################
$this->returnOK();
}
public function getDeliveryObject()
{
if (!$this->delivery) {
$data = parent::getObject();
$this->delivery = Delivery::getClass($data['class'] ?? null);
if ($this->getID()) {
$this->delivery->createFromArray($data);
}
}
return $this->delivery;
}
}