Newer
Older
<?php
/**
* This File contains \QUI\ERP\Shipping\EventHandler
*/
namespace QUI\ERP\Shipping;
use QUI;
use QUI\ERP\Order\Controls\OrderProcess\Checkout as OrderCheckoutStepControl;
use QUI\ERP\Products\Handler\Fields as ProductFields;
use Quiqqer\Engine\Collector;
use function array_merge;
use function explode;
use function json_decode;
use function method_exists;
/**
* Class EventHandler
*
* @package QUI\ERP\Shipping
*/
class EventHandler
{
/**
* event for on package setup
*
* @throws QUI\Exception
*/

Patrick Müller
committed
public static function onPackageSetup(QUI\Package\Package $Package)

Patrick Müller
committed
if ($Package->getName() !== 'quiqqer/shipping') {
return;
}
$languages = QUI\Translator::getAvailableLanguages();
$StatusFactory = QUI\ERP\Shipping\ShippingStatus\Factory::getInstance();
// create locale
$var = 'message.no.rule.found.order.continue';
$params = [
'datatype' => 'php,js',
'package' => 'quiqqer/shipping'
];
foreach ($languages as $language) {
$params[$language] = QUI::getLocale()->getByLang(
$language,
'quiqqer/shipping',
$var
);
}
try {
QUI\Translator::addUserVar('quiqqer/shipping', $var, $params);
} catch (QUI\Exception $Exception) {
QUI\System\Log::addNotice($Exception->getMessage());
}
// create locale
$params = [
'datatype' => 'php,js',
'package' => 'quiqqer/shipping'
];
foreach ($languages as $language) {
$params[$language] = QUI::getLocale()->getByLang(
$language,
'quiqqer/shipping',
$var
);
}
try {
QUI\Translator::addUserVar('quiqqer/shipping', $var, $params);
} catch (QUI\Exception $Exception) {
QUI\System\Log::addNotice($Exception->getMessage());
}
// create shipping order status
$getLocaleTranslations = function ($key) use ($languages) {
$result = [];
foreach ($languages as $language) {
$result[$language] = QUI::getLocale()->getByLang($language, 'quiqqer/order', $key);
}
return $result;
};
$Handler = QUI\ERP\Shipping\ShippingStatus\Handler::getInstance();

Patrick Müller
committed
$list = $Handler->getList();

Patrick Müller
committed
if (empty($list)) {
$StatusFactory->createShippingStatus(1, '#dbb50c', $getLocaleTranslations('processing.status.default.1'));
$StatusFactory->createShippingStatus(2, '#418e73', $getLocaleTranslations('processing.status.default.2'));
$StatusFactory->createShippingStatus(3, '#4fd500', $getLocaleTranslations('processing.status.default.3'));
}
// Product fields
self::createProductFields();
/**
* event : on admin load footer
*/
public static function onAdminLoadFooter()
{
echo '<script src="' . URL_OPT_DIR . 'quiqqer/shipping/bin/backend/load.js"></script>';
}
/**
* event - on price factor init
*
* @param $Basket
* @param QUI\ERP\Order\AbstractOrder $Order
* @param QUI\ERP\Products\Product\ProductList $Products
*/
public static function onQuiqqerOrderBasketToOrderEnd(
$Basket,
QUI\ERP\Order\AbstractOrder $Order,
QUI\ERP\Products\Product\ProductList $Products
) {
if (Shipping::getInstance()->shippingDisabled()) {
return;
}
$Shipping = $Order->getShipping();
if (!$Shipping) {
return;
}
if (!$Shipping->getPrice()) {
return;
}
$PriceFactors = $Products->getPriceFactors();

Henning Leutz
committed
$PriceFactors->addToEnd($Shipping->toPriceFactor(null, $Order));
try {
$Products->recalculation();
} catch (QUI\Exception $Exception) {
QUI\System\Log::writeDebugException($Exception);
}
if (method_exists($Order, 'save')) {
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/**
* @param QUI\ERP\Accounting\Payments\Types\Payment $Payment
* @param QUI\ERP\Order\OrderInterface $Order
*
* @throws QUI\ERP\Accounting\Payments\Exceptions\PaymentCanNotBeUsed
*/
public static function onQuiqqerPaymentCanUsedInOrder(
QUI\ERP\Accounting\Payments\Types\Payment $Payment,
QUI\ERP\Order\OrderInterface $Order
) {
if (Shipping::getInstance()->shippingDisabled()) {
return;
}
$Shipping = $Order->getShipping();
if (!$Shipping) {
return;
}
$payments = $Shipping->getAttribute('payments');
if (empty($payments)) {
return;
}
$payments = explode(',', $payments);
$Payments = QUI\ERP\Accounting\Payments\Payments::getInstance();
foreach ($payments as $paymentId) {
try {
$ShippingPayment = $Payments->getPayment($paymentId);
if ($ShippingPayment->getId() === $Payment->getId()) {
return;
}
} catch (QUI\Exception $Exception) {
QUI\System\Log::writeDebugException($Exception);
}
}
throw new QUI\ERP\Accounting\Payments\Exceptions\PaymentCanNotBeUsed(
QUI::getLocale()->get('This Payment can not be used, because of the shipping rules')
);
}
* @param $User
* @param $Order
*/
public static function onOrderProcessCustomerDataEnd(
$User,
$Address,
$Order
) {
if (Shipping::getInstance()->shippingDisabled()) {
return;
}
$Control = new QUI\ERP\Shipping\Order\ShippingAddress([
'User' => $User,
'Order' => $Order
]);
$Collector->append($Control->create());
}
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
public static function onQuiqqerOrderOrderProcessCheckoutOutputBefore(
OrderCheckoutStepControl $Checkout
) {
if (Shipping::getInstance()->shippingDisabled()) {
return;
}
$Order = $Checkout->getOrder();
if (!$Order) {
return;
}
if ($Order->hasDeliveryAddress()) {
return;
}
$SessionUser = QUI::getUserBySession();
$Customer = $Order->getCustomer();
if ($SessionUser->getId() !== $Customer->getId()) {
return;
}
$addressId = $SessionUser->getAttribute('quiqqer.delivery.address');
if ($addressId) {
try {
$DeliveryAddress = $Customer->getAddress($addressId);
$Order->setDeliveryAddress($DeliveryAddress);
$Order->save(QUI::getUsers()->getSystemUser());
} catch (\Exception $Exception) {
}
}
}
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/**
* quiqqer/order: onQuiqqerOrderOrderProcessCheckoutOutput
*
* @param OrderCheckoutStepControl $Checkout
* @param string $text
* @return void
*/
public static function onQuiqqerOrderOrderProcessCheckoutOutput(OrderCheckoutStepControl $Checkout, string $text)
{
if (Shipping::getInstance()->shippingDisabled()) {
return;
}
$Order = $Checkout->getOrder();
if (!$Order) {
return;
}
$DeliveryAddress = $Order->getDeliveryAddress();
if ($DeliveryAddress->getId() === 0) {
$customerId = $Order->getCustomer()->getId();
$Customer = QUI::getUsers()->get($customerId);
$deliveryAddressId = $Customer->getAttribute('quiqqer.delivery.address');
if (!empty($deliveryAddressId)) {
try {
$DeliveryAddress = $Customer->getAddress($deliveryAddressId);
$ErpDeliveryAddress = new QUI\ERP\Address(
json_decode($DeliveryAddress->toJSON(), true),
$Order->getCustomer()
);
$Order->setDeliveryAddress($ErpDeliveryAddress);
$Order->save(QUI::getUsers()->getSystemUser());
} catch (\Exception $Exception) {
QUI\System\Log::writeException($Exception);
}
}
}
}
/**
* @param QUI\ERP\Order\Controls\OrderProcess\CustomerData $CustomerData
*
* @throws QUI\Exception
* @throws QUI\Permissions\Exception
*/
public static function onQuiqqerOrderCustomerDataSave(
QUI\ERP\Order\Controls\OrderProcess\CustomerData $CustomerData
) {
if (Shipping::getInstance()->shippingDisabled()) {
return;
}
if (!isset($_REQUEST['shipping-address'])) {
return;
}
// save shipping address
$Order = $CustomerData->getOrder();
$Customer = $Order->getCustomer();
try {
$User = QUI::getUsers()->get($Customer->getId());
} catch (QUI\Exception $Exception) {
$User = QUI::getUserBySession();
}
try {
$Address = $User->getAddress($_REQUEST['shipping-address']);
} catch (QUI\Exception $Exception) {
$Order->clearAddressDelivery();
$Order->save();

Henning Leutz
committed
array_merge($Address->getAttributes(), ['id' => $Address->getId()])
);
$Order->setDeliveryAddress($ErpAddress);
/**
* @param Collector $Collector
* @param QUI\Users\User $User
*/
public static function onFrontendUsersAddressTop(
Collector $Collector,
QUI\Users\User $User
) {
if (Shipping::getInstance()->shippingDisabled()) {
return;
}
$ShippingAddress = new QUI\ERP\Shipping\FrontendUsers\ShippingAddressSelect([
'User' => $User
]);
$Collector->append($ShippingAddress->create());
}
/**
* @param QUI\Users\User $User
*/
public static function onUserSaveBegin(QUI\Users\User $User)
{
if (Shipping::getInstance()->shippingDisabled()) {
return;
}
$Request = QUI::getRequest()->request;
$submit = $Request->get('submit-shipping');
$address = (int)$Request->get('shipping-address');

Henning Leutz
committed
if (isset($_REQUEST['step'])
&& $_REQUEST['step'] === 'Customer'
&& !empty($_REQUEST['shipping-address'])
) {
$address = (int)$_REQUEST['shipping-address'];
}
if ($submit === false || !$address) {
return;
}
try {
$Address = $User->getAddress($address);
$User->setAttribute('quiqqer.delivery.address', $Address->getId());
} catch (QUI\Exception $Exception) {
QUI\System\Log::writeDebugException($Exception);
}
if (isset($Address)) {
QUI\ERP\Utils\User::setUserCurrentAddress($User, $Address);
}
/**
* event: onTemplateGetHeader
* sets the uer current address
*/
public static function onTemplateGetHeader()
{
$User = QUI::getUserBySession();
$addressId = $User->getAttribute('quiqqer.delivery.address');
if (!$addressId) {
return;
}
try {
QUI\ERP\Utils\User::setUserCurrentAddress(
$User,
$User->getAddress($addressId)
);
} catch (QUI\Exception $Exception) {
}
}
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
/**
* Create all fixed product fields that quiqqer/shipping provides
*
* @return void
* @throws QUI\Exception
*/
protected static function createProductFields()
{
$fields = [
Shipping::PRODUCT_FIELD_SHIPPING_TIME => [
'title' => [
'de' => 'Lieferzeit',
'en' => 'Delivery time'
],
'type' => Shipping::PRODUCT_FIELD_TYPE_SHIPPING_TIME,
'public' => true,
'standard' => true
]
];
$fieldsCreated = false;
foreach ($fields as $fieldId => $field) {
try {
ProductFields::getField($fieldId);
continue;
} catch (\Exception $Exception) {
// Field does not exist -> create it
}
try {
ProductFields::createField([
'id' => $fieldId,
'type' => $field['type'],
'titles' => $field['title'],
'workingtitles' => $field['title'],
'systemField' => 0,
'standardField' => !empty($field['standard']) ? 1 : 0,
'publicField' => !empty($field['public']) ? 1 : 0,
'options' => !empty($field['options']) ? $field['options'] : null
]);
} catch (\Exception $Exception) {
QUI\System\Log::writeException($Exception);
continue;
}
$fieldsCreated = true;
}
if ($fieldsCreated) {
QUI\Translator::publish('quiqqer/products');
}
}
/**
* Create a shipping information button
*
* @param Collector $Collector
* @param QUI\ERP\Products\Controls\Price $Price
* @throws QUI\Exception
*/
public static function onQuiqqerProductsPriceEnd(Collector $Collector, QUI\ERP\Products\Controls\Price $Price)
{
$Config = QUI::getPackage('quiqqer/shipping')->getConfig();
$enableShippingInfo = !!$Config->getValue('shipping', 'showShippingInfoAfterPrice');
if (!$enableShippingInfo || !$Price->getAttribute('withVatText')) {
return;
}
$Engine = QUI::getTemplateManager()->getEngine();
$html = $Engine->fetch(dirname(__FILE__) . '/templates/shippingInformation.html');
$Collector->append($html);
}
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
/**
* @param \QUI\ERP\Order\AbstractOrder $Order
* @return void
*
* @throws \QUI\Exception
*/
public static function onQuiqqerOrderShippingOnEmpty(QUI\ERP\Order\AbstractOrder $Order)
{
if (!QUI::isBackend()) {
return;
}
$Config = QUI::getPackage('quiqqer/shipping')->getConfig();
$default = $Config->getValue('shipping', 'defaultShipping');
$add = $Config->getValue('shipping', 'addDefaultShipping');
if (empty($add)) {
return;
}
try {
$Shipping = Shipping::getInstance()->getShippingEntry($default);
$Order->setShipping($Shipping);
} catch (QUI\Exception $Exception) {
}
}