- 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.
314 lines
13 KiB
PHP
314 lines
13 KiB
PHP
<?php
|
|
|
|
use Empik\Marketplace\Adapter\ConfigurationAdapter;
|
|
use Empik\Marketplace\Adapter\LinkAdapter;
|
|
use Empik\Marketplace\API\EmpikClient;
|
|
use Empik\Marketplace\DataProvider\CarriersDataProvider;
|
|
use Empik\Marketplace\Factory\EmpikClientFactory;
|
|
use Empik\Marketplace\Manager\CarrierMapManager;
|
|
use Empik\Marketplace\Validator\Validator;
|
|
|
|
class AdminEmpikOrdersController extends ModuleAdminController
|
|
{
|
|
/** @var EmpikMarketplace */
|
|
public $module;
|
|
|
|
/** @var EmpikClientFactory */
|
|
private $empikClientFactory;
|
|
|
|
/** @var CarrierMapManager */
|
|
protected $carrierMapManager;
|
|
|
|
/** @var LinkAdapter */
|
|
protected $link;
|
|
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
|
|
$this->bootstrap = true;
|
|
|
|
$this->empikClientFactory = $this->module->getService('empik.marketplace.factory.empikClientFactory');
|
|
$this->carrierMapManager = $this->module->getService('empik.marketplace.manager.carrierMapManager');
|
|
$this->link = $this->module->getService('empik.marketplace.adapter.link');
|
|
}
|
|
|
|
public function init()
|
|
{
|
|
if (Tools::getIsset('submitOrderForm') || Tools::getIsset('submitOrderFormShipping')) {
|
|
$this->handleForm();
|
|
}
|
|
|
|
parent::init();
|
|
}
|
|
|
|
public function initContent()
|
|
{
|
|
$this->display = 'edit';
|
|
|
|
parent::initContent();
|
|
}
|
|
|
|
public function handleForm()
|
|
{
|
|
$result = true;
|
|
|
|
if (Tools::getIsset('submitOrderForm')) {
|
|
|
|
$importOrders = Tools::getValue('import_orders');
|
|
$autoAcceptOrders = Tools::getValue('auto_accept_orders');
|
|
$newOrderState = Tools::getValue('new_order_state');
|
|
$shippedOrderState = Tools::getValue('shipped_order_state');
|
|
$defaultVatRate = Tools::getValue('default_vat_rate');
|
|
|
|
if (!Validator::isBool($importOrders)) {
|
|
$this->errors[] = $this->l('Invalid Import orders field');
|
|
}
|
|
|
|
if (!Validator::isBool($autoAcceptOrders)) {
|
|
$this->errors[] = $this->l('Invalid Auto accept orders field');
|
|
}
|
|
|
|
if (!Validator::isInt($newOrderState)) {
|
|
$this->errors[] = $this->l('Invalid New order state field');
|
|
}
|
|
|
|
if (!Validator::isInt($shippedOrderState)) {
|
|
$this->errors[] = $this->l('Invalid Shipped order state field');
|
|
}
|
|
|
|
if (!Validator::isInt($defaultVatRate)) {
|
|
$this->errors[] = $this->l('Invalid Default VAT rate field');
|
|
}
|
|
|
|
if (empty($this->errors)) {
|
|
$result &= Configuration::updateValue(ConfigurationAdapter::CONF_IMPORT_ORDERS, (int)$importOrders);
|
|
$result &= Configuration::updateValue(ConfigurationAdapter::CONF_AUTO_ACCEPT_ORDERS, (int)$autoAcceptOrders);
|
|
$result &= Configuration::updateValue(ConfigurationAdapter::CONF_NEW_ORDER_STATE, (int)$newOrderState);
|
|
$result &= Configuration::updateValue(ConfigurationAdapter::CONF_SHIPPED_ORDER_STATE, (int)$shippedOrderState);
|
|
$result &= Configuration::updateValue(ConfigurationAdapter::CONF_DEFAULT_VAT_RATE, (int)$defaultVatRate);
|
|
}
|
|
}
|
|
|
|
if (Tools::getIsset('submitOrderFormShipping')) {
|
|
$result &= $this->carrierMapManager->store(Tools::getValue('carrier', []));
|
|
}
|
|
|
|
if ($result && empty($this->errors)) {
|
|
Tools::redirectAdmin(
|
|
$this->link->getAdminLink($this->controller_name, ['conf' => 4])
|
|
);
|
|
}
|
|
|
|
$this->errors[] = $this->l('Error saving form.');
|
|
}
|
|
|
|
public function renderForm()
|
|
{
|
|
if (!$this->module->getAuthStatus()) {
|
|
$this->errors[] = $this->l('Before starting, set up the connection');
|
|
return false;
|
|
}
|
|
|
|
return
|
|
$this->renderOrderForm().
|
|
$this->renderShippingMappingForm();
|
|
}
|
|
|
|
public function renderOrderForm()
|
|
{
|
|
$language = new Language((int)Configuration::get('PS_LANG_DEFAULT'));
|
|
$orderStates = OrderState::getOrderStates($language->id);
|
|
|
|
$formFields = [
|
|
'form' => [
|
|
'legend' => [
|
|
'title' => $this->l('Orders'),
|
|
'icon' => 'icon-cogs',
|
|
],
|
|
'input' => [
|
|
[
|
|
'type' => 'switch',
|
|
'label' => $this->l('Import orders from Empik Marketplace'),
|
|
'hint' => $this->l('Import orders from Empik Marketplace'),
|
|
'desc' => $this->l('When enabled, orders with status "Shipment in progress" will be imported via CRON.'),
|
|
'name' => 'import_orders',
|
|
'is_bool' => true,
|
|
'values' => [
|
|
[
|
|
'id' => 'import_orders_on',
|
|
'value' => 1,
|
|
'label' => $this->l('Yes'),
|
|
],
|
|
[
|
|
'id' => 'import_orders_off',
|
|
'value' => 0,
|
|
'label' => $this->l('No'),
|
|
],
|
|
],
|
|
],
|
|
[
|
|
'type' => 'switch',
|
|
'label' => $this->l('Auto accept orders in Empik Marketplace'),
|
|
'hint' => $this->l('Auto accept orders in Empik Marketplace'),
|
|
'desc' => $this->l('Auto-accept orders with status "Pending acceptance" if products have positive stock.'),
|
|
'name' => 'auto_accept_orders',
|
|
'is_bool' => true,
|
|
'values' => [
|
|
[
|
|
'id' => 'auto_accept_orders_on',
|
|
'value' => 1,
|
|
'label' => $this->l('Yes'),
|
|
],
|
|
[
|
|
'id' => 'auto_accept_orders_off',
|
|
'value' => 0,
|
|
'label' => $this->l('No'),
|
|
],
|
|
],
|
|
],
|
|
[
|
|
'type' => 'select',
|
|
'label' => $this->l('New order state'),
|
|
'hint' => $this->l('New order state'),
|
|
'desc' => $this->l('Status assigned to newly imported orders. Choose one that indicates awaiting processing, e.g. "Payment accepted".'),
|
|
'name' => 'new_order_state',
|
|
'options' => [
|
|
'query' => $orderStates,
|
|
'id' => 'id_order_state',
|
|
'name' => 'name',
|
|
],
|
|
'class' => 'fixed-width-xxl',
|
|
],
|
|
[
|
|
'type' => 'select',
|
|
'label' => $this->l('Shipped order state'),
|
|
'hint' => $this->l('Shipped order state'),
|
|
'desc' => $this->l('When order reaches this status, tracking number is sent and order marked as "Shipped" in EmpikPlace.'),
|
|
'name' => 'shipped_order_state',
|
|
'options' => [
|
|
'query' => $orderStates,
|
|
'id' => 'id_order_state',
|
|
'name' => 'name',
|
|
],
|
|
'class' => 'fixed-width-xxl',
|
|
],
|
|
[
|
|
'type' => 'select',
|
|
'label' => $this->l('Default VAT rate'),
|
|
'hint' => $this->l('Default VAT rate'),
|
|
'desc' => $this->l('Fallback VAT rate when product tax cannot be determined.'),
|
|
'name' => 'default_vat_rate',
|
|
'options' => [
|
|
'query' => [
|
|
['id' => '23', 'name' => '23%'],
|
|
['id' => '8', 'name' => '8%'],
|
|
['id' => '5', 'name' => '5%'],
|
|
['id' => '0', 'name' => '0%'],
|
|
],
|
|
'id' => 'id',
|
|
'name' => 'name'
|
|
],
|
|
'class' => 'fixed-width-xxl',
|
|
],
|
|
],
|
|
'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($this->controller_name);
|
|
$helper->token = Tools::getAdminTokenLite('AdminEmpikOrders');
|
|
$helper->tpl_vars = [
|
|
'languages' => $this->context->controller->getLanguages(),
|
|
'id_language' => $this->context->language->id,
|
|
];
|
|
|
|
$helper->tpl_vars['fields_value']['import_orders'] = (int)Tools::getValue('import_orders', Configuration::get(ConfigurationAdapter::CONF_IMPORT_ORDERS));
|
|
$helper->tpl_vars['fields_value']['auto_accept_orders'] = (int)Tools::getValue('auto_accept_orders', Configuration::get(ConfigurationAdapter::CONF_AUTO_ACCEPT_ORDERS));
|
|
$helper->tpl_vars['fields_value']['new_order_state'] = (int)Tools::getValue('new_order_state', Configuration::get(ConfigurationAdapter::CONF_NEW_ORDER_STATE));
|
|
$helper->tpl_vars['fields_value']['shipped_order_state'] = (int)Tools::getValue('shipped_order_state', Configuration::get(ConfigurationAdapter::CONF_SHIPPED_ORDER_STATE));
|
|
$helper->tpl_vars['fields_value']['default_vat_rate'] = (int)Tools::getValue('default_vat_rate', Configuration::get(ConfigurationAdapter::CONF_DEFAULT_VAT_RATE));
|
|
|
|
return $helper->generateForm([$formFields]);
|
|
}
|
|
|
|
public function renderShippingMappingForm()
|
|
{
|
|
$language = new Language((int)Configuration::get('PS_LANG_DEFAULT'));
|
|
|
|
/** @var EmpikClient $client */
|
|
$client = $this->empikClientFactory->createClient();
|
|
|
|
try {
|
|
$responseShippingTypes = $client->getShippingTypes();
|
|
$responseCarriers = $client->getCarriers();
|
|
} catch (Exception $e) {
|
|
$this->errors[] = $e->getMessage();
|
|
return false;
|
|
}
|
|
|
|
$shopCarriers = (new CarriersDataProvider())->getCarriers();
|
|
|
|
$this->context->smarty->assign(
|
|
[
|
|
'empik_carriers' => $responseCarriers['carriers'],
|
|
'empik_shipping_types' => $responseShippingTypes['shipping_types'],
|
|
'shop_carriers' => $shopCarriers,
|
|
'map' => $this->carrierMapManager->loadFromConfig(),
|
|
]
|
|
);
|
|
|
|
$formFields = [
|
|
'form' => [
|
|
'legend' => [
|
|
'title' => $this->l('Shipping'),
|
|
'icon' => 'icon-cogs',
|
|
],
|
|
'input' => [
|
|
[
|
|
'type' => 'html',
|
|
'label' => $this->l('Shipping mapping'),
|
|
'desc' => $this->l('Map EmpikPlace delivery methods to PrestaShop carriers. Only map methods you use.'),
|
|
'name' => 'html_data',
|
|
'html_content' => $this->context->smarty->fetch(
|
|
$this->module->getLocalPath().'/views/templates/admin/_partials/shipping_table.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 = 'submitOrderFormShipping';
|
|
$helper->currentIndex = $this->link->getAdminLink($this->controller_name);
|
|
$helper->token = Tools::getAdminTokenLite('AdminEmpikOrders');
|
|
$helper->tpl_vars = [
|
|
'languages' => $this->context->controller->getLanguages(),
|
|
'id_language' => $this->context->language->id,
|
|
];
|
|
|
|
return $helper->generateForm([$formFields]);
|
|
}
|
|
|
|
protected function l($string, $class = null, $addslashes = false, $htmlentities = true)
|
|
{
|
|
return Translate::getModuleTranslation($this->module, $string, get_class($this), null, $addslashes);
|
|
}
|
|
} |