diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..f4da20891502a3992abf2981d8f6c46e33868ab6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +tools/ +phpstan.neon +.phpunit.result.cache +phpunit.xml \ No newline at end of file diff --git a/.phive/phars.xml b/.phive/phars.xml new file mode 100644 index 0000000000000000000000000000000000000000..a1315a09b4adad780a9c5e52f74835c708c5c7d5 --- /dev/null +++ b/.phive/phars.xml @@ -0,0 +1,4 @@ +<?xml version="1.0" encoding="UTF-8"?> +<phive xmlns="https://phar.io/phive"> + <phar name="phpstan" version="^1.10.67" installed="1.10.67" location="./tools/phpstan" copy="false"/> +</phive> diff --git a/ajax/activate.php b/ajax/activate.php index f0d9443ea69e53cae2a5c33bb276741eacb690e7..b84ce1082bdcad248149960b65389499ec6b56e0 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 44fb53279a924cdeab5896a339e4b35b309e97a1..d014dcbc4ea1842a8a20272edc55e5c15b644862 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 dd763228ecbc5ffebc4a748674d7cf86c7324574..d82213b85cdb306278a4cba0f91fd7d5760cb585 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 c95c99f64a87e6cef6a6a142b88c09af14e8ad6a..1ab42737aa058a82e124c091363611a94b05ab6f 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 933bcbb89e1c04755945a8e19a2f2b97d6750af0..1b06068f259bae5d6f6ded74ace6e9f00ffd84dd 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 c6efba879f4379ba5eefd623da3483d423e66e3e..7a941df712e29a81218ee05b3f6379b480366092 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 c01ebe6796519bebcf4692c1f426e3a4b3561e2a..6a0665c7a933e5e40f84b4650473fe38f444ab47 100644 --- a/ajax/list.php +++ b/ajax/list.php @@ -20,7 +20,7 @@ function ($params) { $Locale = QUI::getLocale(); // search - $params = $Grid->parseDBParams(\json_decode($params, true)); + $params = $Grid->parseDBParams(json_decode($params, true)); $params['where'] = [ 'hidden' => 0 diff --git a/ajax/toggle.php b/ajax/toggle.php index da595f9bada0f6e63ae5265e392e8951053c7bad..2ff6bc5c79523e9761dabcdca9a65cfdd62fbba0 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 790782af429c5fef61ca11ff961c8f7efc39f44c..d957092b5af2fa5df48157551c45ebe7aa58d482 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/composer.json b/composer.json index d9f20c7b961b84641891186ac575d29f6264ab9d..fbc7f6afd4bca6a2b3be7f79a119f347566db92c 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "name": "quiqqer\/discount", + "name": "quiqqer/discount", "type": "quiqqer-plugin", "description": "Create discounts for user groups, specific users as well as time controlled.", "license": [ @@ -9,25 +9,25 @@ { "name": "Henning Leutz", "email": "leutz@pcsg.de", - "homepage": "http:\/\/www.pcsg.de", + "homepage": "https://www.pcsg.de", "role": "Developer" } ], "support": { "email": "support@pcsg.de", - "url": "http:\/\/www.pcsg.de" + "url": "https://www.pcsg.de" }, "require": { - "php": ">=5.3", - "quiqqer\/quiqqer": "*@dev", - "quiqqer\/areas": "*@dev", - "quiqqer\/products": "^1.6|*@dev", - "quiqqer\/tax": "*@dev" + "php": "^8.2", + "quiqqer/core": "^2", + "quiqqer/areas": "^2", + "quiqqer/products": "^2", + "quiqqer/tax": "^2" }, "autoload": { "psr-4": { - "QUI\\ERP\\Discount\\": "src\/QUI\/ERP\/Discount", - "QUITests\\ERP\\Discount\\": "tests\/QUITests\/ERP\/Discount" + "QUI\\ERP\\Discount\\": "src/QUI/ERP/Discount", + "QUITests\\ERP\\Discount\\": "tests/QUITests/ERP/Discount" } } } diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/phpstan.dist.neon b/phpstan.dist.neon new file mode 100644 index 0000000000000000000000000000000000000000..a545a041a3c5b0c758d99bf8c1c49b2a83252eaa --- /dev/null +++ b/phpstan.dist.neon @@ -0,0 +1,10 @@ +includes: + - phpstan-baseline.neon + +parameters: + level: 1 + paths: + - src + - ajax + bootstrapFiles: + - tests/phpstan-bootstrap.php \ No newline at end of file diff --git a/src/QUI/ERP/Discount/Discount.php b/src/QUI/ERP/Discount/Discount.php index 2715d73681b662bdb25545db46ae9eaf77858022..b9b2171d23d138b1907afdf56fd898e95a564c1f 100644 --- a/src/QUI/ERP/Discount/Discount.php +++ b/src/QUI/ERP/Discount/Discount.php @@ -7,12 +7,25 @@ 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 @@ -24,6 +37,7 @@ class Discount extends QUI\CRUD\Child * * @param int $id * @param Handler $Factory + * @throws Exception */ public function __construct($id, Handler $Factory) { @@ -61,7 +75,7 @@ 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 . ','; @@ -72,7 +86,7 @@ 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 . ','; @@ -83,7 +97,7 @@ 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 . ','; @@ -94,7 +108,7 @@ 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 . ','; @@ -203,29 +217,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 $value + * @return void */ - public function setAttribute($key, $value) + public function setAttribute(string $name, mixed $value): void { - if ($key === 'lastSumDiscount' && empty($value)) { + if ($name === 'lastSumDiscount' && empty($value)) { $value = null; } - if ($key === 'lastProductDiscount' && empty($value)) { + if ($name === 'lastProductDiscount' && empty($value)) { $value = null; } if ( - $key === 'scope' || - $key === 'discount_type' || - $key === 'usage_type' + $name === 'scope' || + $name === 'discount_type' || + $name === 'usage_type' ) { $value = (int)$value; } - parent::setAttribute($key, $value); + parent::setAttribute($name, $value); } /** @@ -234,7 +248,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(); @@ -251,9 +265,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'); } /** @@ -262,7 +276,7 @@ public function isActive() * @param Discount $Discount * @return bool */ - public function canCombinedWith(Discount $Discount) + public function canCombinedWith(Discount $Discount): bool { $combine = $this->getAttribute('combine'); @@ -270,16 +284,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; @@ -291,7 +299,7 @@ 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; @@ -300,13 +308,13 @@ public function canUsedBy(QUI\Interfaces\Users\User $User) // usage definitions / limits $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; } @@ -314,7 +322,7 @@ public function canUsedBy(QUI\Interfaces\Users\User $User) $userGroupValue = $this->getAttribute('user_groups'); $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; } @@ -336,6 +344,10 @@ public function canUsedBy(QUI\Interfaces\Users\User $User) if ($User->getId() == $uid) { return true; } + + if ($User->getUUID() == $uid) { + return true; + } } // group checking @@ -344,7 +356,11 @@ public function canUsedBy(QUI\Interfaces\Users\User $User) /* @var $Group QUI\Groups\Group */ foreach ($discountGroups as $gid) { foreach ($groupsOfUser as $Group) { - if ($Group->getId() == $gid) { + if ($Group->getUsers() == $gid) { + return true; + } + + if ($Group->getUUID() == $gid) { return true; } } @@ -359,26 +375,26 @@ 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; } // coupon - if ($Product->getId() === '-') { + if ($Product->getId() === -1) { return false; } $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); } @@ -388,9 +404,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; } } @@ -401,7 +417,7 @@ public function canUsedWith(QUI\ERP\Products\Interfaces\ProductInterface $Produc return true; } - if (!\is_array($categories)) { + if (!is_array($categories)) { return false; } @@ -410,7 +426,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; } } @@ -424,7 +440,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; @@ -436,7 +452,7 @@ public function canUsedInOrder(OrderInterface $Order) /* @var $Article QUI\ERP\Accounting\Article */ $id = $Article->getId(); - if (!\is_numeric($id)) { + if (!is_numeric($id)) { continue; } @@ -446,7 +462,7 @@ public function canUsedInOrder(OrderInterface $Order) if ($this->canUsedWith($Product)) { return true; } - } catch (QUI\Exception $Exception) { + } catch (QUI\Exception) { continue; } } @@ -460,7 +476,7 @@ 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([ @@ -480,7 +496,7 @@ 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([ @@ -488,7 +504,7 @@ public function verifyUser(User $User) 'exception.discount.user.cant.use.discount', [ 'id' => $this->getId(), - 'userId' => $User->getId() + 'userId' => $User->getUUID() ] ]); } @@ -497,13 +513,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; @@ -515,14 +533,10 @@ 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' @@ -542,7 +556,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; } @@ -562,7 +576,7 @@ public function toPriceFactor($Locale = null, $Customer = null) 'calculation' => $calculation, 'basis' => $basis, 'value' => $this->getAttribute('discount') * -1, - 'visible' => $hideDiscounts ? false : true, + 'visible' => !$hideDiscounts, 'vat' => $this->getAttribute('vat') ]); } @@ -577,7 +591,6 @@ public function toPriceFactor($Locale = null, $Customer = null) 'calculation' => $calculation, 'basis' => $basis, 'value' => $this->getAttribute('discount') * -1, - 'visible' => $hideDiscounts ? false : true ]); } @@ -586,7 +599,7 @@ 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'); @@ -595,18 +608,16 @@ public function update() $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 ebb506aa8c659a58160e1e8b3af0626430057cbe..c1de6ebd394ac430919641bce93713eadfcdec9a 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,16 +52,17 @@ 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 { - if (isset(self::$userDiscounts[$User->getId()])) { - return self::$userDiscounts[$User->getId()]; + if (isset(self::$userDiscounts[$User->getUUID()])) { + return self::$userDiscounts[$User->getUUID()]; } - self::$userDiscounts[$User->getId()] = Utils::getActiveUserDiscounts($User); + self::$userDiscounts[$User->getUUID()] = Utils::getActiveUserDiscounts($User); - return self::$userDiscounts[$User->getId()]; + return self::$userDiscounts[$User->getUUID()]; } /** @@ -65,10 +70,10 @@ 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'); $purchaseQuantityUntil = $Discount->getAttribute('purchase_quantity_until'); @@ -93,14 +98,14 @@ 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'); $purchaseValueUntil = $Discount->getAttribute('purchase_value_until'); @@ -136,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 @@ -158,18 +164,11 @@ public static function onQuiqqerProductsCalcListProduct( return (int)$Discount->getAttribute('scope') === Handler::DISCOUNT_SCOPE_EVERY_PRODUCT; }); - if (!\is_array($userDiscounts) || empty($userDiscounts)) { - return; - } - - try { - $attributes = $Product->getAttributes(); - } catch (QUI\Exception $Exception) { - QUI\System\Log::writeDebugException($Exception); - + if (empty($userDiscounts)) { return; } + $attributes = $Product->getAttributes(); $PriceFactors = $Product->getPriceFactors(); $productQuantity = $Product->getQuantity(); $productNettoSum = $attributes['calculated_nettoSum']; @@ -184,7 +183,7 @@ public static function onQuiqqerProductsCalcListProduct( continue; } - // check if Pricefactor is already in + // check if price factor is already in $factors = $PriceFactors->toArray(); $Factor = $Discount->toPriceFactor( $Calc->getUser()->getLocale(), @@ -210,20 +209,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 @@ -234,10 +230,6 @@ public static function onQuiqqerProductsCalcList( return (int)$Discount->getAttribute('scope') == Handler::DISCOUNT_SCOPE_TOTAL; }); - if (!\is_array($userDiscounts)) { - return; - } - $listQuantity = $List->getQuantity(); $products = $List->getProducts(); $PriceFactors = $List->getPriceFactors(); @@ -256,7 +248,7 @@ public static function onQuiqqerProductsCalcList( $productIds = $Discount->getAttribute('articles'); if ($productIds) { - $productIds = \explode(',', $productIds); + $productIds = explode(',', $productIds); // product id check $existProductIdInList = function ($products, $productIds) { @@ -281,7 +273,7 @@ public static function onQuiqqerProductsCalcList( $categories = $Discount->getAttribute('categories'); if ($categories) { - $categories = \explode(',', $categories); + $categories = explode(',', $categories); // product category check $existCategoryInList = function ($products, $categories) { diff --git a/src/QUI/ERP/Discount/Handler.php b/src/QUI/ERP/Discount/Handler.php index bde72a9ebd37cfaf313880c6a69b5d7ed851079b..a2f58611c513f7e73ea10695a18f379548be4fb3 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; /** @@ -49,7 +50,7 @@ class Handler extends QUI\CRUD\Factory const DISCOUNT_SCOPE_GRAND_TOTAL = 4; /** - * pricefactor discount type + * price factor discount type */ const DISCOUNT_PRICEFACTOR_TYPE = 'DISCOUNT_PRICE_FACTOR'; @@ -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 be19e27273f46d0dd669d241860de14e455d9343..c18cdb2bc519e1891f46b97ce27636445e3d91fe 100644 --- a/src/QUI/ERP/Discount/PriceFactor.php +++ b/src/QUI/ERP/Discount/PriceFactor.php @@ -10,22 +10,18 @@ /** * 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 */ protected string $type = Handler::DISCOUNT_PRICEFACTOR_TYPE; - /** - * @var string|null - */ - protected $vat = null; - /** * PriceFactor constructor. * @param array $params @@ -45,7 +41,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 +57,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 4e843db45cbcc4b29ce1b7c71e3db190b670312e..1861bc43fdf63ab5f291d780f1a60d31fcb0d2bd 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,7 +32,7 @@ class Utils public static function getUserDiscounts(UserInterface $User): array { $guString = UserGroups::getUserGroupStringFromUser($User); - $guString = ',' . \str_replace(',', ',|,', $guString) . ','; + $guString = ',' . str_replace(',', ',|,', $guString) . ','; $result = []; $Discounts = new Handler(); @@ -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; @@ -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,7 +115,7 @@ 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 = []; $Discounts = new Handler(); @@ -140,19 +144,19 @@ public static function getActiveUserDiscounts(UserInterface $User): array ] ]); - $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 diff --git a/tests/phpstan-bootstrap.php b/tests/phpstan-bootstrap.php new file mode 100644 index 0000000000000000000000000000000000000000..b61ff4b8300a965e0f46025ba64745a22b118e74 --- /dev/null +++ b/tests/phpstan-bootstrap.php @@ -0,0 +1,13 @@ +<?php + +if (!defined('QUIQQER_SYSTEM')) { + define('QUIQQER_SYSTEM', true); +} + +if (!defined('QUIQQER_AJAX')) { + define('QUIQQER_AJAX', true); +} + +putenv("QUIQQER_OTHER_AUTOLOADERS=KEEP"); + +require_once __DIR__ . '/../../../../bootstrap.php';