Newer
Older
<?php
/**
* This File contains \QUI\ERP\Shipping\EventHandler
*/
namespace QUI\ERP\Shipping;
use QUI;
use QUI\ERP\Products\Handler\Fields as ProductFields;
/**
* Class EventHandler
*
* @package QUI\ERP\Shipping
*/
class EventHandler
{
/**
* event for on package setup
*
* @throws QUI\Exception
*/
public static function onPackageSetup()
{
$languages = QUI\Translator::getAvailableLanguages();
// 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());
}
// Product fields
self::createProductFields();
/**
* 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;
}
$PriceFactors = $Products->getPriceFactors();
$PriceFactors->addToEnd($Shipping->toPriceFactor());
try {
$Products->recalculation();
} catch (QUI\Exception $Exception) {
QUI\System\Log::writeDebugException($Exception);
}
$Order->getArticles()->calc();
if (\method_exists($Order, 'save')) {
$Order->save();
}
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/**
* @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());
}
/**
* @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) {
return;
}
$ErpAddress = new QUI\ERP\Address(
\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');
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);
}
}
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/**
* 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');
}
}