diff --git a/bin/controls/DiscountEdit.js b/bin/controls/DiscountEdit.js index d50aafc056c764bc726fa2706d96d08a9dcc02b6..5e8722175ecc5aa621faf4bdb4cb0091fc450aae 100644 --- a/bin/controls/DiscountEdit.js +++ b/bin/controls/DiscountEdit.js @@ -129,8 +129,6 @@ define('package/quiqqer/discount/bin/controls/DiscountEdit', [ data.discount_type = Discounts.DISCOUNT_TYPE_PERCENT; } - console.log(data); - QUIFormUtils.setDataToForm(data, Form); self.$Translate = new Translation({ diff --git a/locale.xml b/locale.xml index 7009f027cbb11807a6eef8731ba0eb4313c54ca0..dde5c14b0147f44afff2b397912d8909c4ff449e 100644 --- a/locale.xml +++ b/locale.xml @@ -74,7 +74,7 @@ <en><![CDATA[Purchase value]]></en> </locale> <locale name="discount.grid.articles"> - <de><![CDATA[Zugewiesene Artikel]]></de> + <de><![CDATA[Zugewiesene Produkte]]></de> <en><![CDATA[Assigned products]]></en> </locale> <locale name="discount.grid.categories"> diff --git a/src/QUI/ERP/Discount/Discount.php b/src/QUI/ERP/Discount/Discount.php index a2a39aa3efead941325cae7f94c7afed04c13955..f9673a38f713157cedb2f92108b55c3a3c4d4a0d 100644 --- a/src/QUI/ERP/Discount/Discount.php +++ b/src/QUI/ERP/Discount/Discount.php @@ -9,6 +9,7 @@ use QUI\Users\User; use QUI\Permissions\Permission; use QUI\Utils\Security\Orthos; +use QUI\ERP\Areas\Utils as AreaUtils; /** * Class Discount @@ -114,6 +115,27 @@ public function __construct($id, Handler $Factory) )); } + + // cleanup user group save + $cleanup = QUI\Utils\ArrayHelper::cleanup($this->getAttribute('user_groups')); + $cleanup = implode(',', $cleanup); + + if (!empty($cleanup)) { + $cleanup = ',' . $cleanup . ','; + } + + $this->setAttribute('user_groups', $cleanup); + + + // cleanup product(s) + $cleanup = QUI\Utils\ArrayHelper::cleanup($this->getAttribute('articles')); + $cleanup = implode(',', $cleanup); + + if (!empty($cleanup)) { + $cleanup = ',' . $cleanup . ','; + } + + $this->setAttribute('articles', $cleanup); }); } @@ -170,15 +192,28 @@ public function canCombinedWith(Discount $Discount) /** * is the user allowed to use the discount * - * @param User $User + * @param QUI\Interfaces\Users\User $User * @return boolean */ - public function canUsedBy(User $User) + public function canUsedBy(QUI\Interfaces\Users\User $User) { if ($this->isActive() === false) { return false; } + $userGroupValue = $this->getAttribute('user_groups'); + $areasValue = $this->getAttribute('areas'); + + // if groups and areas are empty, everbody is allowed + if (empty($userGroupValue) && empty($areasValue)) { + return true; + } + + // not in area + if (!empty($areasValue) && !AreaUtils::isUserInAreas($User, $areasValue)) { + return false; + } + $userGroups = QUI\Utils\UserGroups::parseUsersGroupsString( $this->getAttribute('user_groups') ); diff --git a/src/QUI/ERP/Discount/Utils.php b/src/QUI/ERP/Discount/Utils.php new file mode 100644 index 0000000000000000000000000000000000000000..f39328006c1b7fbb425d9851bb95287312d6e288 --- /dev/null +++ b/src/QUI/ERP/Discount/Utils.php @@ -0,0 +1,161 @@ +<?php + +/** + * This file contains QUI\ERP\Discount\Utils + */ +namespace QUI\ERP\Discount; + +use QUI\ERP\Products\Product\Product; +use QUI\Utils\UserGroups; +use QUI\Interfaces\Users\User as UserInterface; + +/** + * Class Utils + * + * @package QUI\ERP\Discount + */ +class Utils +{ + /** + * Return all discounts which are usable by the user + * + * @param \QUI\Interfaces\Users\User $User + * @return array + */ + public static function getUserDiscounts(UserInterface $User) + { + $guString = UserGroups::getUserGroupStringFromUser($User); + $guString = ',' . str_replace(',', ',|,', $guString) . ','; + + $result = array(); + $Discounts = new Handler(); + + $personalDiscounts = $Discounts->getChildren(array( + 'where' => array( + 'user_groups' => array( + 'type' => 'REGEXP', + 'value' => $guString + ) + ) + )); + + $discounts = $Discounts->getChildren(array( + 'where' => array( + 'user_groups' => '' + ) + )); + + + if (!empty($personalDiscounts)) { + $result = array_merge($personalDiscounts, $result); + } + + if (!empty($discounts)) { + $result = array_merge($discounts, $result); + } + + return $result; + } + + /** + * Return all discounts which are usable with the product + * + * @param Product $Product + * @return array + */ + public static function getProductDiscounts(Product $Product) + { + $result = array(); + $Discounts = new Handler(); + + $productDiscounts = $Discounts->getChildren(array( + 'where' => array( + 'user_groups' => array( + 'type' => 'REGEXP', + 'value' => ',' . $Product->getId() . ',' + ) + ) + )); + + $discounts = $Discounts->getChildren(array( + 'where' => array( + 'user_groups' => '' + ) + )); + + + if (!empty($productDiscounts)) { + $result = array_merge($productDiscounts, $result); + } + + if (!empty($discounts)) { + $result = array_merge($discounts, $result); + } + + return $result; + } + + /** + * Return all active discounts which are usable by the user + * + * @param \QUI\Interfaces\Users\User $User + * @return array + */ + public static function getActiveUserDiscounts(UserInterface $User) + { + $guString = UserGroups::getUserGroupStringFromUser($User); + $guString = ',' . str_replace(',', ',|,', $guString) . ','; + + $result = array(); + $Discounts = new Handler(); + + $personalDiscounts = $Discounts->getChildren(array( + 'where' => array( + 'active' => 1, + 'user_groups' => array( + 'type' => 'REGEXP', + 'value' => $guString + ) + ) + )); + + $discounts = $Discounts->getChildren(array( + 'where' => array( + 'active' => 1, + 'user_groups' => '' + ) + )); + + + if (!empty($personalDiscounts)) { + $result = array_merge($personalDiscounts, $result); + } + + if (!empty($discounts)) { + $result = array_merge($discounts, $result); + } + + return $result; + } + + /** + * Return all active and usable discounts which are usable by the user + * + * @param \QUI\Interfaces\Users\User $User + * @return array + */ + public static function getUsableUserDiscounts(UserInterface $User) + { + $discounts = self::getActiveUserDiscounts($User); + $result = array(); + + /* @var $Discount Discount */ + foreach ($discounts as $Discount) { + if ($Discount->canUsedBy($User)) { + $result[] = $Discount; + } + } + + return $result; + } +}