Skip to content
Code-Schnipsel Gruppen Projekte
Utils.php 4,42 KiB
Newer Older
  • Learn to ignore specific revisions
  • Henning Leutz's avatar
    Henning Leutz committed
    <?php
    
    /**
     * This file contains QUI\ERP\Discount\Utils
     */
    
    Henning Leutz's avatar
    Henning Leutz committed
    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    = [];
    
    Henning Leutz's avatar
    Henning Leutz committed
            $Discounts = new Handler();
    
    
            $personalDiscounts = $Discounts->getChildren([
                'where' => [
                    'user_groups' => [
    
    Henning Leutz's avatar
    Henning Leutz committed
                        'type'  => 'REGEXP',
                        'value' => $guString
    
            $discounts = $Discounts->getChildren([
                'where' => [
    
    Henning Leutz's avatar
    Henning Leutz committed
                    'user_groups' => ''
    
    Henning Leutz's avatar
    Henning Leutz committed
    
    
            if (!empty($personalDiscounts)) {
    
                $result = \array_merge($personalDiscounts, $result);
    
    Henning Leutz's avatar
    Henning Leutz committed
            }
    
            if (!empty($discounts)) {
    
                $result = \array_merge($discounts, $result);
    
    Henning Leutz's avatar
    Henning Leutz committed
            }
    
            return $result;
        }
    
        /**
         * Return all discounts which are usable with the product
         *
         * @param Product $Product
         * @return array
         */
        public static function getProductDiscounts(Product $Product)
        {
    
            $result    = [];
    
    Henning Leutz's avatar
    Henning Leutz committed
            $Discounts = new Handler();
    
    
            $productDiscounts = $Discounts->getChildren([
                'where' => [
                    'user_groups' => [
    
    Henning Leutz's avatar
    Henning Leutz committed
                        'type'  => 'REGEXP',
    
                        'value' => ','.$Product->getId().','
                    ]
                ]
            ]);
    
            $discounts = $Discounts->getChildren([
                'where' => [
    
    Henning Leutz's avatar
    Henning Leutz committed
                    'user_groups' => ''
    
    Henning Leutz's avatar
    Henning Leutz committed
    
    
            if (!empty($productDiscounts)) {
    
                $result = \array_merge($productDiscounts, $result);
    
    Henning Leutz's avatar
    Henning Leutz committed
            }
    
            if (!empty($discounts)) {
    
                $result = \array_merge($discounts, $result);
    
    Henning Leutz's avatar
    Henning Leutz committed
            }
    
            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    = [];
    
    Henning Leutz's avatar
    Henning Leutz committed
            $Discounts = new Handler();
    
    
            $personalDiscounts = $Discounts->getChildren([
                'where' => [
    
    Henning Leutz's avatar
    Henning Leutz committed
                    'active'      => 1,
    
                    'user_groups' => [
    
    Henning Leutz's avatar
    Henning Leutz committed
                        'type'  => 'REGEXP',
                        'value' => $guString
    
            $discounts = $Discounts->getChildren([
                'where' => [
    
    Henning Leutz's avatar
    Henning Leutz committed
                    'active'      => 1,
                    'user_groups' => ''
    
            $discountsNULL = $Discounts->getChildren([
                'where' => [
                    'active'      => 1,
                    'user_groups' => null
                ]
            ]);
    
            $discounts = \array_merge($discounts, $discountsNULL);
    
    Henning Leutz's avatar
    Henning Leutz committed
    
            if (!empty($personalDiscounts)) {
    
                $result = \array_merge($personalDiscounts, $result);
    
    Henning Leutz's avatar
    Henning Leutz committed
            }
    
            if (!empty($discounts)) {
    
                $result = \array_merge($discounts, $result);
    
            $alreadyAttached = [];
    
            $result = \array_filter($result, function ($Discount) use (&$alreadyAttached) {
                /* @var $Discount Discount */
                $id = $Discount->getId();
    
                if (isset($alreadyAttached[$id])) {
                    return false;
                }
    
                $alreadyAttached[$id] = true;
    
                return true;
            });
    
    
    
    Henning Leutz's avatar
    Henning Leutz committed
            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    = [];
    
    Henning Leutz's avatar
    Henning Leutz committed
    
            /* @var $Discount Discount */
            foreach ($discounts as $Discount) {
                if ($Discount->canUsedBy($User)) {
                    $result[] = $Discount;
                }
            }
    
            return $result;
        }
    }