diff --git a/events.xml b/events.xml index 99b4da3cf98386f9cbdb2d8765b5cd7ef072ef21..109531952f70f522f92b51bb5d572c8aa6b301c9 100644 --- a/events.xml +++ b/events.xml @@ -15,6 +15,9 @@ <event on="onQuiqqerOrderOrderProcessCheckoutOutput" fire="\QUI\ERP\Shipping\EventHandler::onQuiqqerOrderOrderProcessCheckoutOutput" /> + <event on="onQuiqqerOrderOrderProcessCheckoutOutputBefore" + fire="\QUI\ERP\Shipping\EventHandler::onQuiqqerOrderOrderProcessCheckoutOutputBefore" + /> <event on="onUserSaveBegin" fire="\QUI\ERP\Shipping\EventHandler::onUserSaveBegin" /> diff --git a/src/QUI/ERP/Shipping/EventHandler.php b/src/QUI/ERP/Shipping/EventHandler.php index eefea5c195728e98f2ff4520a2b783e702c1aa0c..6f31d85d1fecd686dc0dfaa499deb900cb7ce345 100644 --- a/src/QUI/ERP/Shipping/EventHandler.php +++ b/src/QUI/ERP/Shipping/EventHandler.php @@ -7,10 +7,14 @@ namespace QUI\ERP\Shipping; use QUI; -use QUI\ERP\Products\Handler\Fields as ProductFields; use QUI\ERP\Order\Controls\OrderProcess\Checkout as OrderCheckoutStepControl; -use QUI\Template; -use \Quiqqer\Engine\Collector; +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 @@ -137,7 +141,7 @@ public static function onQuiqqerOrderBasketToOrderEnd( $Order->getArticles()->calc(); - if (\method_exists($Order, 'save')) { + if (method_exists($Order, 'save')) { $Order->save(); } } @@ -168,7 +172,7 @@ public static function onQuiqqerPaymentCanUsedInOrder( return; } - $payments = \explode(',', $payments); + $payments = explode(',', $payments); $Payments = QUI\ERP\Accounting\Payments\Payments::getInstance(); foreach ($payments as $paymentId) { @@ -211,6 +215,42 @@ public static function onOrderProcessCustomerDataEnd( $Collector->append($Control->create()); } + 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) { + } + } + } + /** * quiqqer/order: onQuiqqerOrderOrderProcessCheckoutOutput * @@ -240,13 +280,13 @@ public static function onQuiqqerOrderOrderProcessCheckoutOutput(OrderCheckoutSte if (!empty($deliveryAddressId)) { try { - $DeliveryAddress = $Customer->getAddress($deliveryAddressId); - $ErpDeliveryAddres = new QUI\ERP\Address( - \json_decode($DeliveryAddress->toJSON(), true), + $DeliveryAddress = $Customer->getAddress($deliveryAddressId); + $ErpDeliveryAddress = new QUI\ERP\Address( + json_decode($DeliveryAddress->toJSON(), true), $Order->getCustomer() ); - $Order->setDeliveryAddress($ErpDeliveryAddres); + $Order->setDeliveryAddress($ErpDeliveryAddress); $Order->save(QUI::getUsers()->getSystemUser()); } catch (\Exception $Exception) { QUI\System\Log::writeException($Exception); @@ -292,7 +332,7 @@ public static function onQuiqqerOrderCustomerDataSave( } $ErpAddress = new QUI\ERP\Address( - \array_merge($Address->getAttributes(), ['id' => $Address->getId()]) + array_merge($Address->getAttributes(), ['id' => $Address->getId()]) ); $Order->setDeliveryAddress($ErpAddress); @@ -345,11 +385,14 @@ public static function onUserSaveBegin(QUI\Users\User $User) 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); + } } /** @@ -445,7 +488,7 @@ public static function onQuiqqerProductsPriceEnd(Collector $Collector, QUI\ERP\P } $Engine = QUI::getTemplateManager()->getEngine(); - $html = $Engine->fetch(dirname(__FILE__).'/templates/shippingInformation.html'); + $html = $Engine->fetch(dirname(__FILE__) . '/templates/shippingInformation.html'); $Collector->append($html); } diff --git a/src/QUI/ERP/Shipping/Order/Shipping.php b/src/QUI/ERP/Shipping/Order/Shipping.php index 608ec2a66faaf836cef05a72f6bd5b80436b52ae..ea39bb55cbd969bcfaad1574180cd79ecd3dddcb 100644 --- a/src/QUI/ERP/Shipping/Order/Shipping.php +++ b/src/QUI/ERP/Shipping/Order/Shipping.php @@ -9,6 +9,8 @@ use QUI; use QUI\ERP\Shipping\Shipping as ShippingHandler; +use function count; + /** * Class Shipping * @@ -25,7 +27,7 @@ public function __construct($attributes = []) { parent::__construct($attributes); - $this->addCSSFile(\dirname(__FILE__).'/Shipping.css'); + $this->addCSSFile(\dirname(__FILE__) . '/Shipping.css'); } /** @@ -76,7 +78,7 @@ public function getBody() $DebugShippingEntry->setOrder($Order); QUI\ERP\Shipping\Debug::enable(); - QUI\ERP\Shipping\Debug::addLog('# '.$DebugShippingEntry->getTitle()); + QUI\ERP\Shipping\Debug::addLog('# ' . $DebugShippingEntry->getTitle()); if ($DebugShippingEntry->canUsedBy($User, $Order)) { $DebugShippingEntry->isValid(); @@ -92,7 +94,7 @@ public function getBody() QUI\ERP\Shipping\Debug::disable(); - $Logger->info("\n\n".\implode("\n", $debugStack)); + $Logger->info("\n\n" . \implode("\n", $debugStack)); $Engine->assign('debug', \implode("\n", $debugStack)); } @@ -128,7 +130,7 @@ public function getBody() 'shippingList' => $shippingList ]); - return $Engine->fetch(\dirname(__FILE__).'/Shipping.html'); + return $Engine->fetch(\dirname(__FILE__) . '/Shipping.html'); } /** @@ -157,7 +159,7 @@ public function validate() // if shipping are selectable and no shipping is selected $shippingList = $this->getValidShipping(); - if ($Shipping === null && \count($shippingList) === 1) { + if ($Shipping === null && count($shippingList) === 1) { try { $Order->setShipping($shippingList[0]); $Order->save(); diff --git a/src/QUI/ERP/Shipping/Types/ShippingEntry.php b/src/QUI/ERP/Shipping/Types/ShippingEntry.php index 3a047731b5a1822f090b4e24e2199cb315d2a1ca..82acb686f3da67b18a01c99f7ea50df3c3e94d98 100644 --- a/src/QUI/ERP/Shipping/Types/ShippingEntry.php +++ b/src/QUI/ERP/Shipping/Types/ShippingEntry.php @@ -8,13 +8,18 @@ use QUI; use QUI\CRUD\Factory; -use QUI\Translator; -use QUI\Permissions\Permission; - use QUI\ERP\Shipping\Api; use QUI\ERP\Shipping\Debug; use QUI\ERP\Shipping\Rules\Factory as RuleFactory; use QUI\ERP\Shipping\Rules\ShippingRule; +use QUI\Permissions\Permission; +use QUI\Translator; + +use function array_keys; +use function count; +use function json_encode; +use function key; +use function max; /** * Class ShippingEntry @@ -76,8 +81,8 @@ public function toArray() $lg = 'quiqqer/shipping'; $id = $this->getId(); - $attributes = $this->getAttributes(); - $Locale = QUI::getLocale(); + $attributes = $this->getAttributes(); + $Locale = QUI::getLocale(); $currentLang = $Locale->getCurrent(); $availableLanguages = QUI\Translator::getAvailableLanguages(); @@ -102,8 +107,8 @@ public function toArray() ); if ($language === $currentLang) { - $attributes['currentTitle'] = $attributes['title'][$language]; - $attributes['currentDescription'] = $attributes['description'][$language]; + $attributes['currentTitle'] = $attributes['title'][$language]; + $attributes['currentDescription'] = $attributes['description'][$language]; $attributes['currentWorkingTitle'] = $attributes['workingTitle'][$language]; } } @@ -118,11 +123,11 @@ public function toArray() } // icon - $attributes['icon'] = ''; + $attributes['icon'] = ''; $attributes['icon_path'] = ''; try { - $attributes['icon'] = $this->getIcon(); + $attributes['icon'] = $this->getIcon(); $attributes['icon_path'] = $this->getAttribute('icon'); } catch (QUI\Exception $Exception) { QUI\System\Log::writeDebugException($Exception); @@ -140,7 +145,7 @@ public function toArray() */ public function toJSON() { - return \json_encode($this->toArray()); + return json_encode($this->toArray()); } /** @@ -183,17 +188,17 @@ public function getPriceDisplay() { $PriceFactor = $this->toPriceFactor(); - $Order = $this->Order; + $Order = $this->Order; $isNetto = false; /* @var $Order QUI\ERP\Order\Order */ if ($Order) { $Customer = $Order->getCustomer(); - $isNetto = $Customer->isNetto(); + $isNetto = $Customer->isNetto(); } // display is incl vat - $vat = $PriceFactor->getVat(); + $vat = $PriceFactor->getVat(); $price = $this->getPrice(); if (!$isNetto && $vat) { @@ -222,7 +227,7 @@ public function getPrice() foreach ($rules as $Rule) { $discount = $Rule->getAttribute('discount'); - $type = $Rule->getDiscountType(); + $type = $Rule->getDiscountType(); if ($type === QUI\ERP\Shipping\Rules\Factory::DISCOUNT_TYPE_ABS) { $price = $price + $discount; @@ -232,15 +237,15 @@ public function getPrice() if ($type === QUI\ERP\Shipping\Rules\Factory::DISCOUNT_TYPE_PC_ORDER && $Order) { try { /* @var $Order QUI\ERP\Order\Order */ - $Order = $this->Order; + $Order = $this->Order; $Calculation = $Order->getPriceCalculation(); - $nettoSum = $Calculation->getNettoSum()->get(); + $nettoSum = $Calculation->getNettoSum()->get(); if (!$nettoSum) { continue; } - $pc = \round($nettoSum * ($discount / 100)); + $pc = \round($nettoSum * ($discount / 100)); $price = $price + $pc; continue; @@ -249,7 +254,7 @@ public function getPrice() } } - $pc = \round($price * ($discount / 100)); + $pc = \round($price * ($discount / 100)); $price = $price + $pc; } @@ -513,7 +518,7 @@ protected function setShippingLocale($var, $title) continue; } - $data[$language] = $title[$language]; + $data[$language] = $title[$language]; $data[$language . '_edit'] = $title[$language]; } @@ -556,7 +561,7 @@ public function addShippingRule(ShippingRule $Rule) $shippingRules[] = $Rule->getId(); } - $this->setAttribute('shipping_rules', \json_encode($shippingRules)); + $this->setAttribute('shipping_rules', json_encode($shippingRules)); } /** @@ -586,7 +591,7 @@ public function getShippingRules() return []; } - $debugging = QUI\ERP\Shipping\Shipping::getInstance()->debuggingEnabled(); + $debugging = QUI\ERP\Shipping\Shipping::getInstance()->debuggingEnabled(); $debuggingLog = []; // get rules @@ -777,26 +782,26 @@ public function toPriceFactor( /* @var $Article QUI\ERP\Accounting\Article */ $Articles = $Order->getArticles(); - $vats = []; + $vats = []; foreach ($Articles as $Article) { - $vat = $Article->getVat(); + $vat = $Article->getVat(); $price = $Article->getPrice()->getValue(); - if (!isset($vats[$vat])) { - $vats[$vat] = 0; + if (!isset($vats[(string)$vat])) { + $vats[(string)$vat] = 0; } - $vats[$vat] = $vats[$vat] + $price; + $vats[(string)$vat] = $vats[(string)$vat] + $price; } // look at vat, which vat should be used - if (\count($vats) === 1) { - $PriceFactor->setVat(\key($vats)); + if (count($vats) === 1) { + $PriceFactor->setVat(key($vats)); } else { // get max, use the max VAT if multiple exists // @todo implement VAT setting for shipping - $maxVat = \max(\array_keys($vats)); + $maxVat = max(array_keys($vats)); $PriceFactor->setVat($maxVat); }