From 609783bf1c4b2a8c5918a963f9aee60d43c2e14a Mon Sep 17 00:00:00 2001 From: Henning Leutz <leutz@pcsg.de> Date: Thu, 18 Apr 2024 10:51:49 +0200 Subject: [PATCH] feat: quiqqer v2 compatibility --- ajax/activate.php | 7 +- ajax/create.php | 2 +- ajax/deactivate.php | 7 +- ajax/deleteChild.php | 2 +- ajax/deleteChildren.php | 4 +- ajax/get.php | 9 +- ajax/list.php | 14 +- ajax/search.php | 12 +- ajax/toggle.php | 7 +- ajax/update.php | 4 +- src/QUI/ERP/Discount/Discount.php | 227 +++++++++++++------------ src/QUI/ERP/Discount/EventHandling.php | 71 ++++---- src/QUI/ERP/Discount/Handler.php | 16 +- src/QUI/ERP/Discount/PriceFactor.php | 9 +- src/QUI/ERP/Discount/Utils.php | 55 +++--- 15 files changed, 239 insertions(+), 207 deletions(-) diff --git a/ajax/activate.php b/ajax/activate.php index f0d9443..b84ce10 100644 --- a/ajax/activate.php +++ b/ajax/activate.php @@ -9,13 +9,16 @@ * * @param integer $discountId - Discount-ID */ + +use QUI\ERP\Discount\Discount; + QUI::$Ajax->registerFunction( 'package_quiqqer_discount_ajax_activate', function ($discountId) { - $Handler = new QUI\ERP\Discount\Handler(); + $Handler = new QUI\ERP\Discount\Handler(); $Discount = $Handler->getChild($discountId); - /* @var $Discount \QUI\ERP\Discount\Discount */ + /* @var $Discount Discount */ $Discount->setAttribute('active', 1); $Discount->update(); diff --git a/ajax/create.php b/ajax/create.php index 44fb532..d014dcb 100644 --- a/ajax/create.php +++ b/ajax/create.php @@ -12,7 +12,7 @@ QUI::$Ajax->registerFunction( 'package_quiqqer_discount_ajax_create', function ($params) { - $params = json_decode($params, true); + $params = json_decode($params, true); $Discounts = new QUI\ERP\Discount\Handler(); if (!isset($params['active'])) { diff --git a/ajax/deactivate.php b/ajax/deactivate.php index dd76322..d82213b 100644 --- a/ajax/deactivate.php +++ b/ajax/deactivate.php @@ -9,13 +9,16 @@ * * @param integer $discountId - Discount-ID */ + +use QUI\ERP\Discount\Discount; + QUI::$Ajax->registerFunction( 'package_quiqqer_discount_ajax_deactivate', function ($discountId) { - $Handler = new QUI\ERP\Discount\Handler(); + $Handler = new QUI\ERP\Discount\Handler(); $Discount = $Handler->getChild($discountId); - /* @var $Discount \QUI\ERP\Discount\Discount */ + /* @var $Discount Discount */ $Discount->setAttribute('active', 0); $Discount->update(); diff --git a/ajax/deleteChild.php b/ajax/deleteChild.php index c95c99f..1ab4273 100644 --- a/ajax/deleteChild.php +++ b/ajax/deleteChild.php @@ -15,7 +15,7 @@ 'package_quiqqer_discount_ajax_deleteChild', function ($discountId) { $Discounts = new QUI\ERP\Discount\Handler(); - $Discount = $Discounts->getChild($discountId); + $Discount = $Discounts->getChild($discountId); $Discount->delete(); }, ['discountId'], diff --git a/ajax/deleteChildren.php b/ajax/deleteChildren.php index 933bcbb..1b06068 100644 --- a/ajax/deleteChildren.php +++ b/ajax/deleteChildren.php @@ -14,8 +14,8 @@ QUI::$Ajax->registerFunction( 'package_quiqqer_discount_ajax_deleteChildren', function ($discountIds) { - $discountIds = json_decode($discountIds, true); - $Discounts = new QUI\ERP\Discount\Handler(); + $discountIds = json_decode($discountIds, true); + $Discounts = new QUI\ERP\Discount\Handler(); $ExceptionStack = new QUI\ExceptionStack(); foreach ($discountIds as $discountId) { diff --git a/ajax/get.php b/ajax/get.php index c6efba8..7a941df 100644 --- a/ajax/get.php +++ b/ajax/get.php @@ -11,14 +11,17 @@ * * @return array */ + +use QUI\ERP\Discount\Discount; + QUI::$Ajax->registerFunction( 'package_quiqqer_discount_ajax_get', function ($id) { - $Discounts = new QUI\ERP\Discount\Handler(); - $Discount = $Discounts->getChild($id); + $Discounts = new QUI\ERP\Discount\Handler(); + $Discount = $Discounts->getChild($id); $attributes = $Discount->getAttributes(); - /* @var $Discount \QUI\ERP\Discount\Discount */ + /* @var $Discount Discount */ $attributes['title'] = $Discount->getTitle(); return $attributes; diff --git a/ajax/list.php b/ajax/list.php index ccab3b3..6a0665c 100644 --- a/ajax/list.php +++ b/ajax/list.php @@ -14,13 +14,13 @@ QUI::$Ajax->registerFunction( 'package_quiqqer_discount_ajax_list', function ($params) { - $Grid = new QUI\Utils\Grid(); + $Grid = new QUI\Utils\Grid(); $Discounts = new QUI\ERP\Discount\Handler(); - $result = []; - $Locale = QUI::getLocale(); + $result = []; + $Locale = QUI::getLocale(); // search - $params = $Grid->parseDBParams(\json_decode($params, true)); + $params = $Grid->parseDBParams(json_decode($params, true)); $params['where'] = [ 'hidden' => 0 @@ -31,15 +31,15 @@ function ($params) { foreach ($data as $entry) { $entry['title'] = [ 'quiqqer/discount', - 'discount.'.$entry['id'].'.title' + 'discount.' . $entry['id'] . '.title' ]; $entry['text'] = $Locale->get( 'quiqqer/discount', - 'discount.'.$entry['id'].'.title' + 'discount.' . $entry['id'] . '.title' ); - $type = (int)$entry['discount_type']; + $type = (int)$entry['discount_type']; $usageType = (int)$entry['usage_type']; // attributes diff --git a/ajax/search.php b/ajax/search.php index ed5bed6..0dfdbc1 100644 --- a/ajax/search.php +++ b/ajax/search.php @@ -15,12 +15,12 @@ 'package_quiqqer_discount_ajax_search', function ($fields, $params) { $Discounts = new QUI\ERP\Discount\Handler(); - $result = []; - $Locale = QUI::getLocale(); + $result = []; + $Locale = QUI::getLocale(); $allowedFields = $Discounts->getChildAttributes(); - $query = []; + $query = []; $params = json_decode($params, true); $fields = json_decode($fields, true); @@ -44,7 +44,7 @@ function ($fields, $params) { } $query['where_or'][$field] = [ - 'type' => '%LIKE%', + 'type' => '%LIKE%', 'value' => $value ]; } @@ -55,12 +55,12 @@ function ($fields, $params) { foreach ($data as $entry) { $entry['title'] = [ 'quiqqer/discount', - 'discount.'.$entry['id'].'.title' + 'discount.' . $entry['id'] . '.title' ]; $entry['text'] = $Locale->get( 'quiqqer/discount', - 'discount.'.$entry['id'].'.title' + 'discount.' . $entry['id'] . '.title' ); $result[] = $entry; diff --git a/ajax/toggle.php b/ajax/toggle.php index da595f9..2ff6bc5 100644 --- a/ajax/toggle.php +++ b/ajax/toggle.php @@ -9,13 +9,16 @@ * * @param integer $discountId - Discount-ID */ + +use QUI\ERP\Discount\Discount; + QUI::$Ajax->registerFunction( 'package_quiqqer_discount_ajax_toggle', function ($discountId) { - $Handler = new QUI\ERP\Discount\Handler(); + $Handler = new QUI\ERP\Discount\Handler(); $Discount = $Handler->getChild($discountId); - /* @var $Discount \QUI\ERP\Discount\Discount */ + /* @var $Discount Discount */ if ($Discount->isActive()) { $Discount->setAttribute('active', 0); } else { diff --git a/ajax/update.php b/ajax/update.php index 790782a..d957092 100644 --- a/ajax/update.php +++ b/ajax/update.php @@ -16,8 +16,8 @@ 'package_quiqqer_discount_ajax_update', function ($discountId, $params) { $Discounts = new QUI\ERP\Discount\Handler(); - $Discount = $Discounts->getChild($discountId); - $params = json_decode($params, true); + $Discount = $Discounts->getChild($discountId); + $params = json_decode($params, true); $Discount->setAttributes($params); $Discount->update(); diff --git a/src/QUI/ERP/Discount/Discount.php b/src/QUI/ERP/Discount/Discount.php index 0c158d8..b155d59 100644 --- a/src/QUI/ERP/Discount/Discount.php +++ b/src/QUI/ERP/Discount/Discount.php @@ -7,13 +7,26 @@ namespace QUI\ERP\Discount; use QUI; +use QUI\Database\Exception; use QUI\ERP\Order\OrderInterface; +use QUI\ERP\Products\Interfaces\PriceFactorInterface; +use QUI\ERP\Products\Interfaces\PriceFactorWithVatInterface; +use QUI\ERP\Products\Utils\PriceFactor; use QUI\Users\User; use QUI\Permissions\Permission; use QUI\Utils\Security\Orthos; use QUI\ERP\Areas\Utils as AreaUtils; +use function array_key_exists; +use function explode; +use function implode; +use function is_array; +use function is_numeric; +use function is_string; +use function strtotime; +use function time; + /** * Class Discount * @package QUI\ERP\Discount @@ -25,6 +38,7 @@ class Discount extends QUI\CRUD\Child * * @param int $id * @param Handler $Factory + * @throws Exception */ public function __construct($id, Handler $Factory) { @@ -62,10 +76,10 @@ public function __construct($id, Handler $Factory) // cleanup user group save $cleanup = QUI\Utils\ArrayHelper::cleanup($this->getAttribute('user_groups')); - $cleanup = \implode(',', $cleanup); + $cleanup = implode(',', $cleanup); if (!empty($cleanup)) { - $cleanup = ','.$cleanup.','; + $cleanup = ',' . $cleanup . ','; } $this->setAttribute('user_groups', $cleanup); @@ -73,10 +87,10 @@ public function __construct($id, Handler $Factory) // cleanup product(s) $cleanup = QUI\Utils\ArrayHelper::cleanup($this->getAttribute('articles')); - $cleanup = \implode(',', $cleanup); + $cleanup = implode(',', $cleanup); if (!empty($cleanup)) { - $cleanup = ','.$cleanup.','; + $cleanup = ',' . $cleanup . ','; } $this->setAttribute('articles', $cleanup); @@ -84,10 +98,10 @@ public function __construct($id, Handler $Factory) // cleanup user group save $cleanup = QUI\Utils\ArrayHelper::cleanup($this->getAttribute('user_groups')); - $cleanup = \implode(',', $cleanup); + $cleanup = implode(',', $cleanup); if (!empty($cleanup)) { - $cleanup = ','.$cleanup.','; + $cleanup = ',' . $cleanup . ','; } $this->setAttribute('user_groups', $cleanup); @@ -95,10 +109,10 @@ public function __construct($id, Handler $Factory) // cleanup product(s) $cleanup = QUI\Utils\ArrayHelper::cleanup($this->getAttribute('articles')); - $cleanup = \implode(',', $cleanup); + $cleanup = implode(',', $cleanup); if (!empty($cleanup)) { - $cleanup = ','.$cleanup.','; + $cleanup = ',' . $cleanup . ','; } $this->setAttribute('articles', $cleanup); @@ -112,14 +126,15 @@ public function __construct($id, Handler $Factory) $this->Events->addEvent('onDeleteEnd', function () { QUI\Translator::delete( 'quiqqer/discount', - 'discount.'.$this->getId().'.title' + 'discount.' . $this->getId() . '.title' ); }); $this->Events->addEvent('onSaveBegin', function () { Permission::checkPermission('quiqqer.areas.area.edit'); - if ($this->getAttribute('date_from') + if ( + $this->getAttribute('date_from') && !Orthos::checkMySqlDatetimeSyntax($this->getAttribute('date_from')) ) { throw new QUI\ERP\Discount\Exception([ @@ -128,7 +143,8 @@ public function __construct($id, Handler $Factory) ]); } - if ($this->getAttribute('date_until') + if ( + $this->getAttribute('date_until') && !Orthos::checkMySqlDatetimeSyntax($this->getAttribute('date_until')) ) { throw new QUI\ERP\Discount\Exception([ @@ -137,10 +153,10 @@ public function __construct($id, Handler $Factory) ]); } - $purchaseQuantityFrom = $this->getAttribute('purchase_quantity_from'); + $purchaseQuantityFrom = $this->getAttribute('purchase_quantity_from'); $purchaseQuantityUntil = $this->getAttribute('purchase_quantity_until'); - $purchaseValueFrom = $this->getAttribute('purchase_value_from'); - $purchaseValueUntil = $this->getAttribute('purchase_value_until'); + $purchaseValueFrom = $this->getAttribute('purchase_value_from'); + $purchaseValueUntil = $this->getAttribute('purchase_value_until'); if ($purchaseQuantityFrom !== '' && ($purchaseQuantityFrom === false || $purchaseQuantityFrom < 0)) { throw new QUI\ERP\Discount\Exception([ @@ -202,28 +218,29 @@ public function __construct($id, Handler $Factory) } /** - * @param string $key - * @param array|bool|object|string $value - * @return QUI\QDOM|void + * @param string $name + * @param mixed $val + * @return void */ - public function setAttribute($key, $value) + public function setAttribute(string $name, mixed $val): void { - if ($key === 'lastSumDiscount' && empty($value)) { - $value = null; + if ($name === 'lastSumDiscount' && empty($val)) { + $val = null; } - if ($key === 'lastProductDiscount' && empty($value)) { - $value = null; + if ($name === 'lastProductDiscount' && empty($val)) { + $val = null; } - if ($key === 'scope' || - $key === 'discount_type' || - $key === 'usage_type' + if ( + $name === 'scope' || + $name === 'discount_type' || + $name === 'usage_type' ) { - $value = (int)$value; + $val = (int)$val; } - parent::setAttribute($key, $value); + parent::setAttribute($name, $val); } /** @@ -232,7 +249,7 @@ public function setAttribute($key, $value) * @param null|QUI\Locale $Locale - optional, locale object * @return string */ - public function getTitle($Locale = null) + public function getTitle(QUI\Locale $Locale = null): string { if (!$Locale) { $Locale = QUI::getLocale(); @@ -240,7 +257,7 @@ public function getTitle($Locale = null) return $Locale->get( 'quiqqer/discount', - 'discount.'.$this->getId().'.title' + 'discount.' . $this->getId() . '.title' ); } @@ -249,9 +266,9 @@ public function getTitle($Locale = null) * * @return boolean */ - public function isActive() + public function isActive(): bool { - return $this->getAttribute('active') ? true : false; + return (bool)$this->getAttribute('active'); } /** @@ -260,7 +277,7 @@ public function isActive() * @param Discount $Discount * @return bool */ - public function canCombinedWith(Discount $Discount) + public function canCombinedWith(Discount $Discount): bool { $combine = $this->getAttribute('combine'); @@ -268,16 +285,10 @@ public function canCombinedWith(Discount $Discount) return false; } - $combine = \implode($combine, ','); + $combine = implode($combine, ','); - if (!\is_array($combine)) { - return false; - } - - foreach ($combine as $combineId) { - if ($Discount->getId() == $combineId) { - return true; - } + if (in_array($Discount->getId(), (array)$combine)) { + return true; } return false; @@ -289,30 +300,30 @@ public function canCombinedWith(Discount $Discount) * @param QUI\Interfaces\Users\User $User * @return boolean */ - public function canUsedBy(QUI\Interfaces\Users\User $User) + public function canUsedBy(QUI\Interfaces\Users\User $User): bool { if ($this->isActive() === false) { return false; } // usage definitions / limits - $dateFrom = $this->getAttribute('date_from'); + $dateFrom = $this->getAttribute('date_from'); $dateUntil = $this->getAttribute('date_until'); - $now = \time(); + $now = time(); - if ($dateFrom && \strtotime($dateFrom) > $now) { + if ($dateFrom && strtotime($dateFrom) > $now) { return false; } - if ($dateUntil && \strtotime($dateUntil) < $now) { + if ($dateUntil && strtotime($dateUntil) < $now) { return false; } // assignment $userGroupValue = $this->getAttribute('user_groups'); - $areasValue = $this->getAttribute('areas'); + $areasValue = $this->getAttribute('areas'); - // if groups and areas are empty, everbody is allowed + // if groups and areas are empty, everybody is allowed if (empty($userGroupValue) && empty($areasValue)) { return true; } @@ -326,7 +337,7 @@ public function canUsedBy(QUI\Interfaces\Users\User $User) $this->getAttribute('user_groups') ); - $discountUsers = $userGroups['users']; + $discountUsers = $userGroups['users']; $discountGroups = $userGroups['groups']; // user checking @@ -334,6 +345,10 @@ public function canUsedBy(QUI\Interfaces\Users\User $User) if ($User->getId() == $uid) { return true; } + + if ($User->getUUID() == $uid) { + return true; + } } // group checking @@ -345,6 +360,10 @@ public function canUsedBy(QUI\Interfaces\Users\User $User) if ($Group->getId() == $gid) { return true; } + + if ($Group->getUUID() == $gid) { + return true; + } } } @@ -357,7 +376,7 @@ public function canUsedBy(QUI\Interfaces\Users\User $User) * @param QUI\ERP\Products\Interfaces\ProductInterface $Product * @return boolean */ - public function canUsedWith(QUI\ERP\Products\Interfaces\ProductInterface $Product) + public function canUsedWith(QUI\ERP\Products\Interfaces\ProductInterface $Product): bool { if ($this->isActive() === false) { return false; @@ -368,15 +387,15 @@ public function canUsedWith(QUI\ERP\Products\Interfaces\ProductInterface $Produc return false; } - $articles = $this->getAttribute('articles'); + $articles = $this->getAttribute('articles'); $categories = $this->getAttribute('categories'); - if (\is_string($articles)) { - $articles = \explode(',', $articles); + if (is_string($articles)) { + $articles = explode(',', $articles); } - if (\is_string($categories)) { - $categories = \explode(',', $categories); + if (is_string($categories)) { + $categories = explode(',', $categories); } @@ -386,9 +405,9 @@ public function canUsedWith(QUI\ERP\Products\Interfaces\ProductInterface $Produc } // article / product check - if (\is_array($articles)) { + if (is_array($articles)) { foreach ($articles as $articleId) { - if ((int)$Product->getId() === (int)$articleId) { + if ($Product->getId() === (int)$articleId) { return true; } } @@ -399,7 +418,7 @@ public function canUsedWith(QUI\ERP\Products\Interfaces\ProductInterface $Produc return true; } - if (!\is_array($categories)) { + if (!is_array($categories)) { return false; } @@ -408,7 +427,7 @@ public function canUsedWith(QUI\ERP\Products\Interfaces\ProductInterface $Produc foreach ($productCategories as $Category) { /* @var $Category QUI\ERP\Products\Category\Category */ - if ((int)$Category->getId() === (int)$category) { + if ($Category->getId() === (int)$category) { return true; } } @@ -422,7 +441,7 @@ public function canUsedWith(QUI\ERP\Products\Interfaces\ProductInterface $Produc * @param OrderInterface $Order * @return bool */ - public function canUsedInOrder(OrderInterface $Order) + public function canUsedInOrder(OrderInterface $Order): bool { if ($this->isActive() === false) { return false; @@ -434,7 +453,7 @@ public function canUsedInOrder(OrderInterface $Order) /* @var $Article QUI\ERP\Accounting\Article */ $id = $Article->getId(); - if (!\is_numeric($id)) { + if (!is_numeric($id)) { continue; } @@ -444,7 +463,7 @@ public function canUsedInOrder(OrderInterface $Order) if ($this->canUsedWith($Product)) { return true; } - } catch (QUI\Exception $Exception) { + } catch (QUI\Exception) { continue; } } @@ -458,14 +477,14 @@ public function canUsedInOrder(OrderInterface $Order) * @param Discount $Discount * @throws QUI\ERP\Discount\Exception */ - public function verifyCombinationWith(Discount $Discount) + public function verifyCombinationWith(Discount $Discount): void { if ($this->canCombinedWith($Discount) === false) { throw new QUI\ERP\Discount\Exception([ 'quiqqer/discount', 'exception.discount.not.combinable', [ - 'id' => $this->getId(), + 'id' => $this->getId(), 'discountId' => $Discount->getId() ] ]); @@ -478,15 +497,15 @@ public function verifyCombinationWith(Discount $Discount) * @param User $User * @throws QUI\ERP\Discount\Exception */ - public function verifyUser(User $User) + public function verifyUser(User $User): void { if ($this->canUsedBy($User) === false) { throw new QUI\ERP\Discount\Exception([ 'quiqqer/discount', 'exception.discount.user.cant.use.discount', [ - 'id' => $this->getId(), - 'userId' => $User->getId() + 'id' => $this->getId(), + 'userId' => $User->getUUID() ] ]); } @@ -495,13 +514,15 @@ public function verifyUser(User $User) /** * Parse the discount to a price factor * - * @param null|QUI\Locale $Locale - optional, locale object - * @param null|QUI\Interfaces\Users\User $Customer - optional, + * @param null $Locale - optional, locale object + * @param null $Customer - optional, * - * @return QUI\ERP\Products\Interfaces\PriceFactorWithVatInterface|QUI\ERP\Products\Interfaces\PriceFactorInterface + * @return PriceFactorInterface|PriceFactorWithVatInterface|PriceFactor */ - public function toPriceFactor($Locale = null, $Customer = null) - { + public function toPriceFactor( + $Locale = null, + $Customer = null + ): QUI\ERP\Products\Interfaces\PriceFactorInterface|QUI\ERP\Products\Interfaces\PriceFactorWithVatInterface|QUI\ERP\Products\Utils\PriceFactor { switch ($this->getAttribute('discount_type')) { case QUI\ERP\Accounting\Calc::CALCULATION_PERCENTAGE: $calculation = QUI\ERP\Accounting\Calc::CALCULATION_PERCENTAGE; @@ -513,19 +534,15 @@ public function toPriceFactor($Locale = null, $Customer = null) break; } - switch ($this->getAttribute('price_calculation_basis')) { - case QUI\ERP\Accounting\Calc::CALCULATION_BASIS_NETTO: - $basis = QUI\ERP\Accounting\Calc::CALCULATION_BASIS_NETTO; - break; - - default: - $basis = QUI\ERP\Accounting\Calc::CALCULATION_BASIS_CURRENTPRICE; - } + $basis = match ($this->getAttribute('price_calculation_basis')) { + QUI\ERP\Accounting\Calc::CALCULATION_BASIS_NETTO => QUI\ERP\Accounting\Calc::CALCULATION_BASIS_NETTO, + default => QUI\ERP\Accounting\Calc::CALCULATION_BASIS_CURRENTPRICE, + }; // check calculation basis VAT $useAuto = $this->getAttribute('consider_vat') === 'auto' - && $Customer - && QUI\ERP\Utils\User::isNettoUser($Customer) === false; + && $Customer + && QUI\ERP\Utils\User::isNettoUser($Customer) === false; if ($useAuto || $this->getAttribute('consider_vat') === 'brutto') { $basis = QUI\ERP\Accounting\Calc::CALCULATION_BASIS_VAT_BRUTTO; @@ -540,7 +557,7 @@ public function toPriceFactor($Locale = null, $Customer = null) $Config = $Plugin->getConfig(); $hideDiscounts = (int)$Config->getValue('products', 'hideDiscounts'); - } catch (QUI\Exception $Exception) { + } catch (QUI\Exception) { $hideDiscounts = false; } @@ -552,30 +569,30 @@ public function toPriceFactor($Locale = null, $Customer = null) } return new PriceFactor([ - 'identifier' => 'discount-'.$this->getId(), - 'title' => $this->getTitle($Locale), - 'valueText' => $valueText, + 'identifier' => 'discount-' . $this->getId(), + 'title' => $this->getTitle($Locale), + 'valueText' => $valueText, 'description' => '', - 'priority' => (int)$this->getAttribute('priority'), + 'priority' => (int)$this->getAttribute('priority'), 'calculation' => $calculation, - 'basis' => $basis, - 'value' => $this->getAttribute('discount') * -1, - 'visible' => $hideDiscounts ? false : true, - 'vat' => $this->getAttribute('vat') + 'basis' => $basis, + 'value' => $this->getAttribute('discount') * -1, + 'visible' => !$hideDiscounts, + 'vat' => $this->getAttribute('vat') ]); } // to product return new QUI\ERP\Products\Utils\PriceFactor([ - 'identifier' => 'discount-'.$this->getId(), - 'title' => $this->getTitle($Locale), + 'identifier' => 'discount-' . $this->getId(), + 'title' => $this->getTitle($Locale), // 'valueText' => $this->getTitle($Locale), 'description' => '', - 'priority' => (int)$this->getAttribute('priority'), + 'priority' => (int)$this->getAttribute('priority'), 'calculation' => $calculation, - 'basis' => $basis, - 'value' => $this->getAttribute('discount') * -1, - 'visible' => $hideDiscounts ? false : true + 'basis' => $basis, + 'value' => $this->getAttribute('discount') * -1, + 'visible' => !$hideDiscounts ]); } @@ -584,27 +601,25 @@ public function toPriceFactor($Locale = null, $Customer = null) * * @throws QUI\ExceptionStack|QUI\Exception */ - public function update() + public function update(): void { $this->Events->fireEvent('saveBegin'); $this->Events->fireEvent('updateBegin'); - $needles = $this->Factory->getChildAttributes(); + $needles = $this->Factory->getChildAttributes(); $savedData = []; foreach ($needles as $needle) { - if (!\array_key_exists($needle, $this->attributes)) { + if (!array_key_exists($needle, $this->attributes)) { continue; } $value = $this->getAttribute($needle); - switch ($needle) { - case 'user_groups': - if (!empty($value)) { - $value = ','.$value.','; - } - break; + if ($needle == 'user_groups') { + if (!empty($value)) { + $value = ',' . $value . ','; + } } $savedData[$needle] = $value; diff --git a/src/QUI/ERP/Discount/EventHandling.php b/src/QUI/ERP/Discount/EventHandling.php index 4b452c6..2adb56d 100644 --- a/src/QUI/ERP/Discount/EventHandling.php +++ b/src/QUI/ERP/Discount/EventHandling.php @@ -7,11 +7,15 @@ namespace QUI\ERP\Discount; use QUI; +use QUI\Database\Exception; use QUI\ERP\Accounting\Calc as ErpCalc; use QUI\ERP\Products\Product\UniqueProduct; use QUI\ERP\Products\Utils\Calc; use QUI\ERP\Products\Product\ProductList; +use function array_filter; +use function explode; + /** * Class EventHandling * @@ -20,19 +24,19 @@ class EventHandling { /** - * @var null + * @var Handler|null */ - protected static $Handler = null; + protected static ?Handler $Handler = null; /** * @var array */ - protected static $userDiscounts = []; + protected static array $userDiscounts = []; /** * Return the global * - * @return Handler + * @return Handler|null */ protected static function getHandler(): ?Handler { @@ -48,6 +52,7 @@ protected static function getHandler(): ?Handler * * @param QUI\Interfaces\Users\User $User * @return array + * @throws Exception */ protected static function getUserDiscounts(QUI\Interfaces\Users\User $User): array { @@ -65,12 +70,12 @@ protected static function getUserDiscounts(QUI\Interfaces\Users\User $User): arr * - Einkaufsmengeprüfung * * @param Discount $Discount - * @param integer|double $quantity + * @param double|integer $quantity * @return bool */ - public static function isDiscountUsableWithQuantity(Discount $Discount, $quantity): bool + public static function isDiscountUsableWithQuantity(Discount $Discount, float|int $quantity): bool { - $purchaseQuantityFrom = $Discount->getAttribute('purchase_quantity_from'); + $purchaseQuantityFrom = $Discount->getAttribute('purchase_quantity_from'); $purchaseQuantityUntil = $Discount->getAttribute('purchase_quantity_until'); if ($quantity === 0) { @@ -93,22 +98,24 @@ public static function isDiscountUsableWithQuantity(Discount $Discount, $quantit } /** - * Discount pirchase value check for usage + * Discount purchase value check for usage * - Einkaufswertprüfung * * @param Discount $Discount - * @param integer|double $value + * @param float|integer $value * @return bool */ - public static function isDiscountUsableWithPurchaseValue(Discount $Discount, $value): bool + public static function isDiscountUsableWithPurchaseValue(Discount $Discount, float|int $value): bool { - $purchaseValueFrom = $Discount->getAttribute('purchase_value_from'); + $purchaseValueFrom = $Discount->getAttribute('purchase_value_from'); $purchaseValueUntil = $Discount->getAttribute('purchase_value_until'); // wenn complement, z.B. 10€, muss geprüft werden // ob meine value grösser ist als der gegebene value - if ((int)$Discount->getAttribute('discount_type') === ErpCalc::CALCULATION_COMPLEMENT - && $Discount->getAttribute('discount') > $value) { + if ( + (int)$Discount->getAttribute('discount_type') === ErpCalc::CALCULATION_COMPLEMENT + && $Discount->getAttribute('discount') > $value + ) { return false; } @@ -134,18 +141,19 @@ public static function isDiscountUsableWithPurchaseValue(Discount $Discount, $va * * @param QUI\ERP\Products\Utils\Calc $Calc * @param UniqueProduct $Product + * @throws Exception */ public static function onQuiqqerProductsCalcListProduct( Calc $Calc, UniqueProduct $Product - ) { + ): void { $userDiscounts = self::getUserDiscounts($Calc->getUser()); - if (!\is_array($userDiscounts) || empty($userDiscounts)) { + if (empty($userDiscounts)) { return; } - $userDiscounts = \array_filter($userDiscounts, function ($Discount) { + $userDiscounts = array_filter($userDiscounts, function ($Discount) { /* @var $Discount Discount */ // don't use manuel usage type @@ -156,7 +164,7 @@ public static function onQuiqqerProductsCalcListProduct( return (int)$Discount->getAttribute('scope') === Handler::DISCOUNT_SCOPE_EVERY_PRODUCT; }); - if (!\is_array($userDiscounts) || empty($userDiscounts)) { + if (empty($userDiscounts)) { return; } @@ -168,7 +176,7 @@ public static function onQuiqqerProductsCalcListProduct( return; } - $PriceFactors = $Product->getPriceFactors(); + $PriceFactors = $Product->getPriceFactors(); $productQuantity = $Product->getQuantity(); $productNettoSum = $attributes['calculated_nettoSum']; @@ -184,7 +192,7 @@ public static function onQuiqqerProductsCalcListProduct( // check if Pricefactor is already in $factors = $PriceFactors->toArray(); - $Factor = $Discount->toPriceFactor( + $Factor = $Discount->toPriceFactor( $Calc->getUser()->getLocale(), $Calc->getUser() ); @@ -208,20 +216,17 @@ public static function onQuiqqerProductsCalcListProduct( * * @param Calc $Calc * @param ProductList $List - * @param integer|double $nettoSum + * @param float|integer $nettoSum + * @throws Exception */ public static function onQuiqqerProductsCalcList( Calc $Calc, ProductList $List, - $nettoSum - ) { + float|int $nettoSum + ): void { $userDiscounts = self::getUserDiscounts($Calc->getUser()); - if (!\is_array($userDiscounts)) { - return; - } - - $userDiscounts = \array_filter($userDiscounts, function ($Discount) { + $userDiscounts = array_filter($userDiscounts, function ($Discount) { /* @var $Discount Discount */ // don't use manuel usage type @@ -232,12 +237,8 @@ public static function onQuiqqerProductsCalcList( return (int)$Discount->getAttribute('scope') == Handler::DISCOUNT_SCOPE_TOTAL; }); - if (!\is_array($userDiscounts)) { - return; - } - $listQuantity = $List->getQuantity(); - $products = $List->getProducts(); + $products = $List->getProducts(); $PriceFactors = $List->getPriceFactors(); /* @var $Discount Discount */ @@ -254,7 +255,7 @@ public static function onQuiqqerProductsCalcList( $productIds = $Discount->getAttribute('articles'); if ($productIds) { - $productIds = \explode(',', $productIds); + $productIds = explode(',', $productIds); // product id check $existProductIdInList = function ($products, $productIds) { @@ -279,7 +280,7 @@ public static function onQuiqqerProductsCalcList( $categories = $Discount->getAttribute('categories'); if ($categories) { - $categories = \explode(',', $categories); + $categories = explode(',', $categories); // product category check $existCategoryInList = function ($products, $categories) { @@ -307,7 +308,7 @@ public static function onQuiqqerProductsCalcList( // check if Pricefactor is already in $factors = $PriceFactors->toArray(); - $Factor = $Discount->toPriceFactor( + $Factor = $Discount->toPriceFactor( $Calc->getUser()->getLocale(), $Calc->getUser() ); diff --git a/src/QUI/ERP/Discount/Handler.php b/src/QUI/ERP/Discount/Handler.php index b706918..293106b 100644 --- a/src/QUI/ERP/Discount/Handler.php +++ b/src/QUI/ERP/Discount/Handler.php @@ -7,6 +7,7 @@ namespace QUI\ERP\Discount; use QUI; +use QUI\Database\Exception; use QUI\Permissions\Permission; /** @@ -130,7 +131,7 @@ public function __construct() // create new translation var for the discount $this->Events->addEvent('onCreateEnd', function ($New, $data) { /* @var $New QUI\ERP\Discount\Discount */ - $newVar = 'discount.'.$New->getId().'.title'; + $newVar = 'discount.' . $New->getId() . '.title'; $current = QUI::getLocale()->getCurrent(); $title = $New->getAttribute('title'); @@ -146,9 +147,9 @@ public function __construct() try { QUI\Translator::addUserVar('quiqqer/discount', $newVar, [ - $current => $title, + $current => $title, 'datatype' => 'php,js', - 'package' => 'quiqqer/discount' + 'package' => 'quiqqer/discount' ]); } catch (QUI\Exception $Exception) { QUI::getMessagesHandler()->addAttention( @@ -163,7 +164,7 @@ public function __construct() * * @return string */ - public function getDataBaseTableName() + public function getDataBaseTableName(): string { return QUI::getDBTableName('discounts'); } @@ -173,7 +174,7 @@ public function getDataBaseTableName() * * @return string */ - public function getChildClass() + public function getChildClass(): string { return 'QUI\ERP\Discount\Discount'; } @@ -183,7 +184,7 @@ public function getChildClass() * * @return array */ - public function getChildAttributes() + public function getChildAttributes(): array { return [ 'active', @@ -217,8 +218,9 @@ public function getChildAttributes() * * @param array $queryParams * @return array - [Child, Child, Child] + * @throws Exception */ - public function getChildrenData($queryParams = []) + public function getChildrenData(array $queryParams = []): array { if (!isset($queryParams['order'])) { $queryParams['order'] = 'priority ASC'; diff --git a/src/QUI/ERP/Discount/PriceFactor.php b/src/QUI/ERP/Discount/PriceFactor.php index be19e27..b2481db 100644 --- a/src/QUI/ERP/Discount/PriceFactor.php +++ b/src/QUI/ERP/Discount/PriceFactor.php @@ -10,11 +10,12 @@ /** * Class PriceFactor - * This pricefactors is used by Handler::DISCOUNT_SCOPE_TOTAL + * This price factor is used by Handler::DISCOUNT_SCOPE_TOTAL * * @package QUI\ERP\Discount */ -class PriceFactor extends QUI\ERP\Products\Utils\PriceFactor implements QUI\ERP\Products\Interfaces\PriceFactorWithVatInterface +class PriceFactor extends QUI\ERP\Products\Utils\PriceFactor implements + QUI\ERP\Products\Interfaces\PriceFactorWithVatInterface { /** * @var string @@ -45,7 +46,7 @@ public function __construct(array $params) * @return QUI\ERP\Tax\TaxType * @throws QUI\Exception */ - public function getVatType() + public function getVatType(): QUI\ERP\Tax\TaxType { if (!$this->vat) { return QUI\ERP\Tax\Utils::getShopTaxType(); @@ -61,7 +62,7 @@ public function getVatType() $Handler = new QUI\ERP\Tax\Handler(); return $Handler->getTaxType($standardTax[1]); - } catch (QUI\Exception $Exception) { + } catch (QUI\Exception) { } return QUI\ERP\Tax\Utils::getShopTaxType(); diff --git a/src/QUI/ERP/Discount/Utils.php b/src/QUI/ERP/Discount/Utils.php index 7cd5fd1..1861bc4 100644 --- a/src/QUI/ERP/Discount/Utils.php +++ b/src/QUI/ERP/Discount/Utils.php @@ -10,6 +10,10 @@ use QUI\Utils\UserGroups; use QUI\Interfaces\Users\User as UserInterface; +use function array_filter; +use function array_merge; +use function str_replace; + /** * Class Utils * @@ -20,7 +24,7 @@ class Utils /** * Return all discounts which are usable by the user * - * @param \QUI\Interfaces\Users\User $User + * @param UserInterface $User * @return array * * @throws \QUI\Database\Exception @@ -28,15 +32,15 @@ class Utils public static function getUserDiscounts(UserInterface $User): array { $guString = UserGroups::getUserGroupStringFromUser($User); - $guString = ','.\str_replace(',', ',|,', $guString).','; + $guString = ',' . str_replace(',', ',|,', $guString) . ','; - $result = []; + $result = []; $Discounts = new Handler(); $personalDiscounts = $Discounts->getChildren([ 'where' => [ 'user_groups' => [ - 'type' => 'REGEXP', + 'type' => 'REGEXP', 'value' => $guString ] ] @@ -50,11 +54,11 @@ public static function getUserDiscounts(UserInterface $User): array if (!empty($personalDiscounts)) { - $result = \array_merge($personalDiscounts, $result); + $result = array_merge($personalDiscounts, $result); } if (!empty($discounts)) { - $result = \array_merge($discounts, $result); + $result = array_merge($discounts, $result); } return $result; @@ -70,14 +74,14 @@ public static function getUserDiscounts(UserInterface $User): array */ public static function getProductDiscounts(Product $Product): array { - $result = []; + $result = []; $Discounts = new Handler(); $productDiscounts = $Discounts->getChildren([ 'where' => [ 'user_groups' => [ - 'type' => 'REGEXP', - 'value' => ','.$Product->getId().',' + 'type' => 'REGEXP', + 'value' => ',' . $Product->getId() . ',' ] ] ]); @@ -90,11 +94,11 @@ public static function getProductDiscounts(Product $Product): array if (!empty($productDiscounts)) { - $result = \array_merge($productDiscounts, $result); + $result = array_merge($productDiscounts, $result); } if (!empty($discounts)) { - $result = \array_merge($discounts, $result); + $result = array_merge($discounts, $result); } return $result; @@ -103,7 +107,7 @@ public static function getProductDiscounts(Product $Product): array /** * Return all active discounts which are usable by the user * - * @param \QUI\Interfaces\Users\User $User + * @param UserInterface $User * @return array * * @throws \QUI\Database\Exception @@ -111,16 +115,16 @@ public static function getProductDiscounts(Product $Product): array public static function getActiveUserDiscounts(UserInterface $User): array { $guString = UserGroups::getUserGroupStringFromUser($User); - $guString = ','.\str_replace(',', ',|,', $guString).','; + $guString = ',' . str_replace(',', ',|,', $guString) . ','; - $result = []; + $result = []; $Discounts = new Handler(); $personalDiscounts = $Discounts->getChildren([ 'where' => [ - 'active' => 1, + 'active' => 1, 'user_groups' => [ - 'type' => 'REGEXP', + 'type' => 'REGEXP', 'value' => $guString ] ] @@ -128,31 +132,31 @@ public static function getActiveUserDiscounts(UserInterface $User): array $discounts = $Discounts->getChildren([ 'where' => [ - 'active' => 1, + 'active' => 1, 'user_groups' => '' ] ]); $discountsNULL = $Discounts->getChildren([ 'where' => [ - 'active' => 1, + 'active' => 1, 'user_groups' => null ] ]); - $discounts = \array_merge($discounts, $discountsNULL); + $discounts = array_merge($discounts, $discountsNULL); if (!empty($personalDiscounts)) { - $result = \array_merge($personalDiscounts, $result); + $result = array_merge($personalDiscounts, $result); } if (!empty($discounts)) { - $result = \array_merge($discounts, $result); + $result = array_merge($discounts, $result); } $alreadyAttached = []; - $result = \array_filter($result, function ($Discount) use (&$alreadyAttached) { + return array_filter($result, function ($Discount) use (&$alreadyAttached) { /* @var $Discount Discount */ $id = $Discount->getId(); @@ -164,15 +168,12 @@ public static function getActiveUserDiscounts(UserInterface $User): array return true; }); - - - return $result; } /** * Return all active and usable discounts which are usable by the user * - * @param \QUI\Interfaces\Users\User $User + * @param UserInterface $User * @return array * * @throws \QUI\Database\Exception @@ -180,7 +181,7 @@ public static function getActiveUserDiscounts(UserInterface $User): array public static function getUsableUserDiscounts(UserInterface $User): array { $discounts = self::getActiveUserDiscounts($User); - $result = []; + $result = []; /* @var $Discount Discount */ foreach ($discounts as $Discount) { -- GitLab