Files
interblue.pl/modules/empikmarketplace/controllers/admin/AdminEmpikOffersController.php
Jacek Pyziak 3bd8164d3d Add PHPStan configuration for Symfony4 tests
- Created a new phpstan.neon file in the Symfony4 tests directory.
- Configured paths and excluded Symfony3 directory.
- Added bootstrap files for autoloading.
- Set dynamic constant names and adjusted reporting settings.
- Established PHPStan level to 6 for stricter analysis.
2026-02-09 23:14:09 +01:00

352 lines
15 KiB
PHP

<?php
use Empik\Marketplace\Adapter\ConfigurationAdapter;
use Empik\Marketplace\Adapter\LinkAdapter;
use Empik\Marketplace\Processor\ExportOfferProcessor;
use Empik\Marketplace\Validator\Validator;
class AdminEmpikOffersController extends ModuleAdminController
{
const CONF_EXPORT_SUCCESS = 1;
/** @var ExportOfferProcessor */
protected $exportOfferProcessor;
/** @var LinkAdapter */
protected $link;
public function __construct()
{
parent::__construct();
$this->bootstrap = true;
$this->_conf[self::CONF_EXPORT_SUCCESS] = $this->l('Export successful.');
$this->exportOfferProcessor = $this->module->getService('empik.marketplace.processor.exportOfferProcessor');
$this->link = $this->module->getService('empik.marketplace.adapter.link');
}
public function init()
{
if (Tools::getIsset('submitOrderForm')) {
$this->handleForm();
}
parent::init();
}
public function ajaxProcessExportOffers()
{
$page = (int)Tools::getValue('page');
if ($page < 0) {
$page = 0;
}
$this->exportOfferProcessor->setPage($page);
$this->exportOfferProcessor->process();
$continue = !$this->exportOfferProcessor->isLastPage();
$this->ajaxRender(json_encode([
'success' => !$this->exportOfferProcessor->hasErrors(),
'errors' => $this->exportOfferProcessor->getErrors(),
'continue' => $continue,
'redirect' => $this->link->getAdminLink($this->controller_name, ['conf' => self::CONF_EXPORT_SUCCESS]),
]));
die();
}
public function initContent()
{
$this->display = 'edit';
parent::initContent();
}
public function setMedia($isNewTheme = false)
{
parent::setMedia($isNewTheme);
$this->addJS(_MODULE_DIR_ . $this->module->name . '/views/js/admin/empik.js');
Media::addJsDef([
'empikAjaxUrl' => $this->link->getAdminLink($this->controller_name, ['ajax' => 1]),
]);
}
public function renderForm()
{
return $this->renderOrderForm();
}
public function renderOrderForm()
{
$controller = $this->controller_name;
$language = new Language((int)Configuration::get('PS_LANG_DEFAULT'));
$this->context->smarty->assign([
'url_export_offers' => $this->link->getAdminLink($controller, ['action' => 'exportOffers']),
'url_manage_price' => $this->link->getAdminLink('AdminEmpikProductsPrice'),
]);
$leadTimeToShip = [];
$leadTimeToShip[] = [
'id' => -1,
'name' => $this->l('- No selection -'),
];
for ($x = 0; $x <= 44; $x++) {
$leadTimeToShip[] = [
'id' => $x,
'name' => $x,
];
}
$formFields = [
'form' => [
'legend' => [
'title' => $this->l('Offers'),
'icon' => 'icon-cogs',
],
'input' => [
[
'type' => 'switch',
'label' => $this->l('Sync Empik Marketplace offers automatically'),
'hint' => $this->l('Sync Empik Marketplace offers automatically'),
'desc' => $this->l('When enabled, prices and stock will be automatically synchronized with EmpikPlace via CRON.'),
'name' => 'sync_offers',
'is_bool' => true,
'values' => [
[
'id' => 'sync_offers_on',
'value' => 1,
'label' => $this->l('Yes'),
],
[
'id' => 'sync_offers_off',
'value' => 0,
'label' => $this->l('No'),
],
],
],
[
'type' => 'switch',
'label' => $this->l('Sync logistic class'),
'hint' => $this->l('Enabling this option will update logistic class for all offers'),
'desc' => $this->l('When enabled, logistic class will be included in offer export.'),
'name' => 'sync_logistic_class',
'is_bool' => true,
'values' => [
[
'id' => 'sync_logistic_class_on',
'value' => 1,
'label' => $this->l('Yes'),
],
[
'id' => 'sync_logistic_class_off',
'value' => 0,
'label' => $this->l('No'),
],
],
],
[
'type' => 'select',
'label' => $this->l('Offer identifier'),
'hint' => $this->l('Offer identifier'),
'desc' => $this->l('Product identifier sent to EmpikPlace. If products have EAN codes, we recommend using EAN.'),
'name' => 'offer_identifier',
'options' => [
'query' => [
[
'id' => 'EAN',
'name' => $this->l('EAN'),
],
[
'id' => 'SHOP_SKU',
'name' => $this->l('SKU'),
],
],
'id' => 'id',
'name' => 'name',
],
],
[
'type' => 'select',
'label' => $this->l('SKU type'),
'hint' => $this->l('SKU type'),
'desc' => $this->l('Field visible as offer SKU in EmpikPlace. Must match existing offers or delete old offers first.'),
'name' => 'sku_type',
'options' => [
'query' => [
[
'id' => 'REFERENCE',
'name' => $this->l('Reference'),
],
[
'id' => 'EAN',
'name' => $this->l('EAN'),
],
[
'id' => 'ID',
'name' => $this->l('ID'),
],
],
'id' => 'id',
'name' => 'name',
],
],
[
'type' => 'select',
'label' => $this->l('Leadtime to ship'),
'hint' => $this->l('Leadtime to ship'),
'desc' => $this->l('Business days to prepare and ship the order. Leave unset to use EmpikPlace default.'),
'name' => 'lead_time_to_ship',
'options' => [
'query' => $leadTimeToShip,
'id' => 'id',
'name' => 'name',
],
],
[
'type' => 'text',
'label' => $this->l('Price ratio'),
'hint' => $this->l('Price ratio'),
'desc' => $this->l('Multiplier for offer prices. Value 1.0 = original price, 1.1 = +10%, 0.9 = -10%.'),
'name' => 'price_ratio',
],
[
'type' => 'text',
'label' => $this->l('Add to price'),
'hint' => $this->l('Add to price'),
'desc' => $this->l('Fixed amount added to all offer prices.'),
'name' => 'add_to_price',
'suffix' => Currency::getDefaultCurrency()->sign,
],
[
'type' => 'text',
'label' => $this->l('Reduce stock'),
'hint' => $this->l('Reduce stock'),
'desc' => $this->l('Safety stock buffer. E.g. value 3 means products with stock ≤3 will be sent as 0.'),
'name' => 'reduce_stock',
],
[
'label' => '',
'type' => 'html',
'name' => 'html_data',
'html_content' => $this->context->smarty->fetch(
$this->module->getLocalPath().'/views/templates/admin/_partials/export_offers_button.tpl'
),
],
[
'label' => '',
'type' => 'html',
'name' => 'html_data',
'html_content' => $this->context->smarty->fetch(
$this->module->getLocalPath().'/views/templates/admin/_partials/manage_price_button.tpl'
),
],
],
'buttons' => [],
'submit' => [
'title' => $this->l('Save'),
],
],
];
$helper = new HelperForm();
$helper->show_toolbar = false;
$helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') ? Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') : 0;
$helper->default_form_language = $language->id;
$helper->submit_action = 'submitOrderForm';
$helper->currentIndex = $this->link->getAdminLink($controller);
$helper->token = Tools::getAdminTokenLite('AdminEmpikOffers');
$helper->tpl_vars = [
'languages' => $this->context->controller->getLanguages(),
'id_language' => $this->context->language->id,
];
$helper->tpl_vars['fields_value']['sync_offers'] = Tools::getValue('sync_offers', (int)Configuration::get(ConfigurationAdapter::CONF_SYNC_OFFERS));
$helper->tpl_vars['fields_value']['sync_logistic_class'] = Tools::getValue('sync_logistic_class', (int)Configuration::get(ConfigurationAdapter::CONF_SYNC_LOGISTIC_CLASS));
$helper->tpl_vars['fields_value']['offer_identifier'] = Tools::getValue('offer_identifier', Configuration::get(ConfigurationAdapter::CONF_OFFER_IDENTIFIER));
$helper->tpl_vars['fields_value']['sku_type'] = Tools::getValue('sku_type', Configuration::get(ConfigurationAdapter::CONF_SKU_TYPE));
$helper->tpl_vars['fields_value']['lead_time_to_ship'] = Tools::getValue('lead_time_to_ship', (float)Configuration::get(ConfigurationAdapter::CONF_LEAD_TIME_TO_SHIP));
$helper->tpl_vars['fields_value']['price_ratio'] = Tools::getValue('price_ratio', (float)Configuration::get(ConfigurationAdapter::CONF_PRICE_RATIO));
$helper->tpl_vars['fields_value']['add_to_price'] = Tools::getValue('add_to_price', (float)Configuration::get(ConfigurationAdapter::CONF_ADD_TO_PRICE));
$helper->tpl_vars['fields_value']['reduce_stock'] = Tools::getValue('reduce_stock', (int)Configuration::get(ConfigurationAdapter::CONF_REDUCE_STOCK));
return $helper->generateForm([$formFields]);
}
public function handleForm()
{
$syncOffers = Tools::getValue('sync_offers');
$syncLogisticClass = Tools::getValue('sync_logistic_class');
$offerIdentifier = Tools::getValue('offer_identifier');
$skuType = Tools::getValue('sku_type');
$leadTimeToShip = Tools::getValue('lead_time_to_ship');
$priceRatio = Tools::getValue('price_ratio');
$addToPrice = Tools::getValue('add_to_price');
$reduceStock = Tools::getValue('reduce_stock');
if (!is_numeric($syncOffers)) {
$this->errors[] = $this->l('Invalid Sync offers field');
}
if (!is_numeric($syncLogisticClass)) {
$this->errors[] = $this->l('Invalid Sync logistic class field');
}
if (!Validator::isOfferIdentifier($offerIdentifier)) {
$this->errors[] = $this->l('Invalid Offer identifier field');
}
if (!Validator::isSkuType($skuType)) {
$this->errors[] = $this->l('Invalid SKU type field');
}
if (!Validator::isInt($leadTimeToShip) || $leadTimeToShip < -1 || $leadTimeToShip > 44) {
$this->errors[] = $this->l('Invalid Lead time to ship field');
}
if (!Validator::isFloat($priceRatio) || $priceRatio < 0) {
$this->errors[] = $this->l('Invalid Price ratio field');
}
if (!Validator::isFloat($addToPrice) || $addToPrice < 0) {
$this->errors[] = $this->l('Invalid Add to price field');
}
if (!Validator::isInt($reduceStock) || $reduceStock < 0) {
$this->errors[] = $this->l('Invalid Reduce stock field');
}
if (empty($this->errors)) {
$result = true;
$result &= Configuration::updateValue(ConfigurationAdapter::CONF_SYNC_OFFERS, (int)$syncOffers);
$result &= Configuration::updateValue(ConfigurationAdapter::CONF_SYNC_LOGISTIC_CLASS, (int)$syncLogisticClass);
$result &= Configuration::updateValue(ConfigurationAdapter::CONF_OFFER_IDENTIFIER, $offerIdentifier);
$result &= Configuration::updateValue(ConfigurationAdapter::CONF_SKU_TYPE, $skuType);
$result &= Configuration::updateValue(ConfigurationAdapter::CONF_LEAD_TIME_TO_SHIP, $leadTimeToShip);
$result &= Configuration::updateValue(ConfigurationAdapter::CONF_PRICE_RATIO, (float)$priceRatio);
$result &= Configuration::updateValue(ConfigurationAdapter::CONF_ADD_TO_PRICE, (float)$addToPrice);
$result &= Configuration::updateValue(ConfigurationAdapter::CONF_REDUCE_STOCK, (float)$reduceStock);
if ($result) {
Tools::redirectAdmin(
$this->link->getAdminLink($this->controller_name, ['conf' => 4])
);
}
}
$this->errors[] = $this->l('Error saving form.');
}
protected function l($string, $class = null, $addslashes = false, $htmlentities = true)
{
return Translate::getModuleTranslation($this->module, $string, get_class($this), null, $addslashes);
}
}