Skip to content
Code-Schnipsel Gruppen Projekte
Handler.php 13,5 KiB
Newer Older
Patrick Müller's avatar
Patrick Müller committed
<?php

namespace QUI\ERP\Coupons;

use QUI;
use QUI\Utils\Grid;
use QUI\Utils\Security\Orthos;
use QUI\ERP\Discount\Handler as DiscountHandler;
Patrick Müller's avatar
Patrick Müller committed

/**
 * Class Handler
 *
 * Main CouponCode Code handler
 */
class Handler
{
    /**
     * Permissions
     */
    const PERMISSION_VIEW = 'quiqqer.couponcode.view';
Patrick Müller's avatar
Patrick Müller committed
    const PERMISSION_CREATE = 'quiqqer.couponcode.create';
    const PERMISSION_EDIT = 'quiqqer.couponcode.edit';
Patrick Müller's avatar
Patrick Müller committed
    const PERMISSION_DELETE = 'quiqqer.couponcode.delete';
Patrick Müller's avatar
Patrick Müller committed

    /**
Patrick Müller's avatar
Patrick Müller committed
     * CouponCode runtime cache
Patrick Müller's avatar
Patrick Müller committed
     *
Patrick Müller's avatar
Patrick Müller committed
     * @var CouponCode[]
Patrick Müller's avatar
Patrick Müller committed
     */
Patrick Müller's avatar
Patrick Müller committed
    protected static $couponCodes = [];
Patrick Müller's avatar
Patrick Müller committed

    /**
Patrick Müller's avatar
Patrick Müller committed
     * Get CouponCode
Patrick Müller's avatar
Patrick Müller committed
     *
     * @param int $id
Patrick Müller's avatar
Patrick Müller committed
     * @return CouponCode
     * @throws CouponCodeException
Patrick Müller's avatar
Patrick Müller committed
     */
Patrick Müller's avatar
Patrick Müller committed
    public static function getCouponCode($id)
Patrick Müller's avatar
Patrick Müller committed
    {
Patrick Müller's avatar
Patrick Müller committed
        if (isset(self::$couponCodes[$id])) {
            return self::$couponCodes[$id];
Patrick Müller's avatar
Patrick Müller committed
        }

Patrick Müller's avatar
Patrick Müller committed
        self::$couponCodes[$id] = new CouponCode($id);
Patrick Müller's avatar
Patrick Müller committed

Patrick Müller's avatar
Patrick Müller committed
        return self::$couponCodes[$id];
Patrick Müller's avatar
Patrick Müller committed
    }

    /**
Patrick Müller's avatar
Patrick Müller committed
     * Get CouponCode by its actual code
Patrick Müller's avatar
Patrick Müller committed
     *
     * @param string $code
Patrick Müller's avatar
Patrick Müller committed
     * @return CouponCode
Patrick Müller's avatar
Patrick Müller committed
     *
Patrick Müller's avatar
Patrick Müller committed
     * @throws CouponCodeException
Patrick Müller's avatar
Patrick Müller committed
     */
Patrick Müller's avatar
Patrick Müller committed
    public static function getCouponCodeByCode($code)
Patrick Müller's avatar
Patrick Müller committed
    {
Patrick Müller's avatar
Patrick Müller committed
        $result = QUI::getDataBase()->fetch([
            'select' => [
Patrick Müller's avatar
Patrick Müller committed
                'id'
Patrick Müller's avatar
Patrick Müller committed
            ],
Patrick Müller's avatar
Patrick Müller committed
            'from'   => self::getTable(),
Patrick Müller's avatar
Patrick Müller committed
            'where'  => [
Patrick Müller's avatar
Patrick Müller committed
                'code' => $code
Patrick Müller's avatar
Patrick Müller committed
            ],
Patrick Müller's avatar
Patrick Müller committed
            'limit'  => 1
Patrick Müller's avatar
Patrick Müller committed
        ]);
Patrick Müller's avatar
Patrick Müller committed

        if (empty($result)) {
Patrick Müller's avatar
Patrick Müller committed
            throw new CouponCodeException([
                'quiqqer/coupons',
Patrick Müller's avatar
Patrick Müller committed
                'exception.Handler.code_not_found',
Patrick Müller's avatar
Patrick Müller committed
                [
Patrick Müller's avatar
Patrick Müller committed
                    'code' => $code
Patrick Müller's avatar
Patrick Müller committed
                ]
            ], 404);
Patrick Müller's avatar
Patrick Müller committed
        }

Patrick Müller's avatar
Patrick Müller committed
        return self::getCouponCode($result[0]['id']);
Patrick Müller's avatar
Patrick Müller committed
    }

    /**
Patrick Müller's avatar
Patrick Müller committed
     * Create new CouponCode
Patrick Müller's avatar
Patrick Müller committed
     *
     * @param array $discountIds - IDs of the discounts that are linked to this CouponCode
     * @param array $settings (optional) - If omitted a random default CouponCode is generated
Patrick Müller's avatar
Patrick Müller committed
     * @return CouponCode
Patrick Müller's avatar
Patrick Müller committed
     *
     * @throws \Exception
     */
    public static function createCouponCode($discountIds, $settings = [])
Patrick Müller's avatar
Patrick Müller committed
    {
        $DiscountHandler = DiscountHandler::getInstance();

        if (empty($discountIds)) {
            throw new CouponCodeException([
                'quiqqer/coupons',
                'exception.Handler.no_discounts_linked'
            ]);
        }

        // check if all given discounts exist
        foreach ($discountIds as $discountId) {
            try {
                $DiscountHandler->getChild($discountId);
            } catch (\Exception $Exception) {
                throw new CouponCodeException([
                    'quiqqer/coupons',
                    'exception.Handler.discount_error',
                    [
                        'discountId' => $discountId,
                        'error'      => $Exception->getMessage()
                    ]
                ]);
            }
        }

Patrick Müller's avatar
Patrick Müller committed
        $Now = new \DateTime();

        if (!empty($settings['code'])) {
            if (self::existsCode($settings['code'])) {
Patrick Müller's avatar
Patrick Müller committed
                throw new CouponCodeException([
                    'quiqqer/coupons',
                    'exception.Handler.code_already_exists',
                    [
                        'code' => $settings['code']
            $code = $settings['code'];
Patrick Müller's avatar
Patrick Müller committed
        } else {
            $code = CodeGenerator::generate();
        }

        $couponCode = [
            'title'       => empty($settings['title']) ? null : $settings['title'],
            'createDate'  => $Now->format('Y-m-d H:i:s'),
            'code'        => $code,
            'isReusable'  => empty($settings['reusable']) ? 0 : 1,
            'discountIds' => json_encode($discountIds)
Patrick Müller's avatar
Patrick Müller committed
        ];
Patrick Müller's avatar
Patrick Müller committed

        if (!empty($settings['validUntilDate'])) {
            $ValidUntil                   = new \DateTime($settings['validUntilDate']);
Patrick Müller's avatar
Patrick Müller committed
            $couponCode['validUntilDate'] = $ValidUntil->format('Y-m-d H:i:s');
Patrick Müller's avatar
Patrick Müller committed
        }

        if (!empty($settings['userIds'])) {
            $couponCode['userIds'] = json_encode($settings['userIds']);
Patrick Müller's avatar
Patrick Müller committed
        }
Patrick Müller's avatar
Patrick Müller committed

        if (!empty($settings['groupIds'])) {
            $couponCode['groupIds'] = json_encode($settings['groupIds']);
Patrick Müller's avatar
Patrick Müller committed
        }

        QUI::getDataBase()->insert(
            self::getTable(),
Patrick Müller's avatar
Patrick Müller committed
            $couponCode
Patrick Müller's avatar
Patrick Müller committed
        );

Patrick Müller's avatar
Patrick Müller committed
        return self::getCouponCode(QUI::getPDO()->lastInsertId());
Patrick Müller's avatar
Patrick Müller committed
    }

    /**
     * Edit a CouponCode
     *
     * @param int $id - CouponCode ID
     * @param array $discountIds - IDs of the discounts that are linked to this CouponCode
     * @param array $settings (optional) - If omitted a random default CouponCode is generated
     * @return CouponCode
     *
     * @throws \Exception
     */
    public static function editCouponCode($id, $discountIds, $settings = [])
    {
        QUI\Permissions\Permission::checkPermission(self::PERMISSION_EDIT);

        // check if CouponCode exists
        $CouponCode = self::getCouponCode($id);

        // Check settings
        $DiscountHandler = DiscountHandler::getInstance();

        if (empty($discountIds)) {
            throw new CouponCodeException([
                'quiqqer/coupons',
                'exception.Handler.no_discounts_linked'
            ]);
        }

        // check if all given discounts exist
        foreach ($discountIds as $discountId) {
            try {
                $DiscountHandler->getChild($discountId);
            } catch (\Exception $Exception) {
                throw new CouponCodeException([
                    'quiqqer/coupons',
                    'exception.Handler.discount_error',
                    [
                        'discountId' => $discountId,
                        'error'      => $Exception->getMessage()
                    ]
                ]);
            }
        }

        $Now = new \DateTime();

        if (!empty($settings['code'])) {
            if ($CouponCode->getCode() !== $settings['code']
                && self::existsCode($settings['code'])) {
                throw new CouponCodeException([
                    'quiqqer/coupons',
                    'exception.Handler.code_already_exists',
                    [
                        'code' => $settings['code']
                    ]
                ]);
            }

            $code = $settings['code'];
        } else {
            $code = CodeGenerator::generate();
        }

        $couponCode = [
            'title'       => empty($settings['title']) ? null : $settings['title'],
            'createDate'  => $Now->format('Y-m-d H:i:s'),
            'code'        => $code,
            'isReusable'  => !empty($settings['reusable']),
            'discountIds' => json_encode($discountIds)
        ];

        if (!empty($settings['validUntilDate'])) {
            $ValidUntil                   = new \DateTime($settings['validUntilDate']);
            $couponCode['validUntilDate'] = $ValidUntil->format('Y-m-d H:i:s');
        } else {
            $couponCode['validUntilDate'] = null;
        }

        if (!empty($settings['userIds'])) {
            $couponCode['userIds'] = json_encode($settings['userIds']);
        } else {
            $couponCode['userIds'] = null;
        }

        if (!empty($settings['groupIds'])) {
            $couponCode['groupIds'] = json_encode($settings['groupIds']);
        } else {
            $couponCode['groupIds'] = null;
        }

        QUI::getDataBase()->update(
            self::getTable(),
            $couponCode,
            [
                'id' => $id
            ]
        );

        return self::getCouponCode($id);
    }

Patrick Müller's avatar
Patrick Müller committed
    /**
Patrick Müller's avatar
Patrick Müller committed
     * Search CouponCodes
Patrick Müller's avatar
Patrick Müller committed
     *
     * @param array $searchParams
     * @param bool $countOnly (optional) - get result count only [default: false]
Patrick Müller's avatar
Patrick Müller committed
     * @return CouponCode[]|int
Patrick Müller's avatar
Patrick Müller committed
     * @throws CouponCodeException
Patrick Müller's avatar
Patrick Müller committed
     */
    public static function search($searchParams, $countOnly = false)
    {
Patrick Müller's avatar
Patrick Müller committed
        $couponCodes = [];
Patrick Müller's avatar
Patrick Müller committed
        $Grid        = new Grid($searchParams);
        $gridParams  = $Grid->parseDBParams($searchParams);

Patrick Müller's avatar
Patrick Müller committed
        $binds = [];
        $where = [];
Patrick Müller's avatar
Patrick Müller committed

        if ($countOnly) {
            $sql = "SELECT COUNT(*)";
        } else {
            $sql = "SELECT id";
        }

Patrick Müller's avatar
Patrick Müller committed
        $sql .= " FROM `".self::getTable()."`";
Patrick Müller's avatar
Patrick Müller committed

        if (!empty($searchParams['search'])) {
Patrick Müller's avatar
Patrick Müller committed
            $searchColumns = [
Patrick Müller's avatar
Patrick Müller committed
                'id',
                'code',
                'email'
Patrick Müller's avatar
Patrick Müller committed
            ];
Patrick Müller's avatar
Patrick Müller committed

Patrick Müller's avatar
Patrick Müller committed
            $whereOr = [];
Patrick Müller's avatar
Patrick Müller committed

            foreach ($searchColumns as $searchColumn) {
Patrick Müller's avatar
Patrick Müller committed
                $whereOr[] = '`'.$searchColumn.'` LIKE :search';
Patrick Müller's avatar
Patrick Müller committed
            }

            if (!empty($whereOr)) {
Patrick Müller's avatar
Patrick Müller committed
                $where[] = '('.implode(' OR ', $whereOr).')';
Patrick Müller's avatar
Patrick Müller committed

Patrick Müller's avatar
Patrick Müller committed
                $binds['search'] = [
                    'value' => '%'.$searchParams['search'].'%',
Patrick Müller's avatar
Patrick Müller committed
                    'type'  => \PDO::PARAM_STR
Patrick Müller's avatar
Patrick Müller committed
                ];
Patrick Müller's avatar
Patrick Müller committed
            }
        }

        // build WHERE query string
        if (!empty($where)) {
Patrick Müller's avatar
Patrick Müller committed
            $sql .= " WHERE ".implode(" AND ", $where);
Patrick Müller's avatar
Patrick Müller committed
        }

        // ORDER
        if (!empty($searchParams['sortOn'])
        ) {
            $sortOn = Orthos::clear($searchParams['sortOn']);
Patrick Müller's avatar
Patrick Müller committed
            $order  = "ORDER BY ".$sortOn;
Patrick Müller's avatar
Patrick Müller committed

            if (isset($searchParams['sortBy']) &&
                !empty($searchParams['sortBy'])
            ) {
Patrick Müller's avatar
Patrick Müller committed
                $order .= " ".Orthos::clear($searchParams['sortBy']);
Patrick Müller's avatar
Patrick Müller committed
            } else {
                $order .= " ASC";
            }

Patrick Müller's avatar
Patrick Müller committed
            $sql .= " ".$order;
Patrick Müller's avatar
Patrick Müller committed
        } else {
            $sql .= " ORDER BY id DESC";
        }

        // LIMIT
        if (!empty($gridParams['limit'])
            && !$countOnly
        ) {
Patrick Müller's avatar
Patrick Müller committed
            $sql .= " LIMIT ".$gridParams['limit'];
Patrick Müller's avatar
Patrick Müller committed
        } else {
            if (!$countOnly) {
Patrick Müller's avatar
Patrick Müller committed
                $sql .= " LIMIT ".(int)20;
Patrick Müller's avatar
Patrick Müller committed
            }
        }

        $Stmt = QUI::getPDO()->prepare($sql);

        // bind search values
        foreach ($binds as $var => $bind) {
Patrick Müller's avatar
Patrick Müller committed
            $Stmt->bindValue(':'.$var, $bind['value'], $bind['type']);
Patrick Müller's avatar
Patrick Müller committed
        }

        try {
            $Stmt->execute();
            $result = $Stmt->fetchAll(\PDO::FETCH_ASSOC);
        } catch (\Exception $Exception) {
            QUI\System\Log::addError(
Patrick Müller's avatar
Patrick Müller committed
                self::class.' :: search() -> '.$Exception->getMessage()
Patrick Müller's avatar
Patrick Müller committed
            );

Patrick Müller's avatar
Patrick Müller committed
            return [];
Patrick Müller's avatar
Patrick Müller committed
        }

        if ($countOnly) {
            return (int)current(current($result));
        }

        foreach ($result as $row) {
Patrick Müller's avatar
Patrick Müller committed
            $couponCodes[] = self::getCouponCode($row['id']);
Patrick Müller's avatar
Patrick Müller committed
        }

Patrick Müller's avatar
Patrick Müller committed
        return $couponCodes;
Patrick Müller's avatar
Patrick Müller committed
    }

    /**
     * Check if an invite code already eixsts
     *
     * @param string $code
     * @return bool
     */
    public static function existsCode($code)
    {
Patrick Müller's avatar
Patrick Müller committed
        $result = QUI::getDataBase()->fetch([
Patrick Müller's avatar
Patrick Müller committed
            'select' => 'id',
            'from'   => self::getTable(),
Patrick Müller's avatar
Patrick Müller committed
            'where'  => [
Patrick Müller's avatar
Patrick Müller committed
                'code' => $code
Patrick Müller's avatar
Patrick Müller committed
            ],
Patrick Müller's avatar
Patrick Müller committed
            'limit'  => 1
Patrick Müller's avatar
Patrick Müller committed
        ]);
Patrick Müller's avatar
Patrick Müller committed

        return !empty($result);
    }

    /**
     * Get Registration site
     *
     * @return QUI\Projects\Site|false
     */
    public static function getRegistrationSite()
    {
        try {
            $Conf    = QUI::getPackage('quiqqer/coupons')->getConfig();
            $regSite = $Conf->get('settings', 'registrationSite');
        } catch (QUI\Exception $Exception) {
            QUI\System\Log::writeDebugException($Exception);

            return false;
        }
Patrick Müller's avatar
Patrick Müller committed

        if (empty($regSite)) {
            return false;
        }

        try {
            return QUI\Projects\Site\Utils::getSiteByLink($regSite);
        } catch (\Exception $Exception) {
            return false;
        }
    }

    /**
Patrick Müller's avatar
Patrick Müller committed
     * Deletes all CouponCodes that are expired
Patrick Müller's avatar
Patrick Müller committed
     *
     * @param int $days (optional) - Delete expired Codes that are older than X days [default: delete all]
     * @return void
     *
     * @throws \Exception
     */
Patrick Müller's avatar
Patrick Müller committed
    public static function deleteExpiredCouponCodes($days = null)
Patrick Müller's avatar
Patrick Müller committed
    {
        $Now   = new \DateTime();
Patrick Müller's avatar
Patrick Müller committed
        $where = [
            'validUntilDate' => [
Patrick Müller's avatar
Patrick Müller committed
                'type'  => '<=',
                'value' => $Now->format('Y-m-d H:i:s')
Patrick Müller's avatar
Patrick Müller committed
            ]
        ];
Patrick Müller's avatar
Patrick Müller committed

        if (!is_null($days)) {
            $days    = (int)$days;
            $OldDate = new \DateTime();
Patrick Müller's avatar
Patrick Müller committed
            $OldDate->sub(new \DateInterval('P'.$days.'D'));
Patrick Müller's avatar
Patrick Müller committed

Patrick Müller's avatar
Patrick Müller committed
            $where['validUntilDate'] = [
Patrick Müller's avatar
Patrick Müller committed
                'type'  => '<=',
                'value' => $OldDate->format('Y-m-d H:i:s')
Patrick Müller's avatar
Patrick Müller committed
            ];
Patrick Müller's avatar
Patrick Müller committed
        }

        QUI::getDataBase()->delete(
            self::getTable(),
            $where
        );
    }

    /**
Patrick Müller's avatar
Patrick Müller committed
     * Deletes all CouponCodes that have been redeemed
Patrick Müller's avatar
Patrick Müller committed
     *
     * @param int $days (optional) - Delete redeemed Codes that are older than X days [default: delete all]
     * @return void
     *
     * @throws \Exception
     */
Patrick Müller's avatar
Patrick Müller committed
    public static function deleteRedeemedCouponCodes($days = null)
Patrick Müller's avatar
Patrick Müller committed
    {
Patrick Müller's avatar
Patrick Müller committed
        $where = [
            'useDate' => [
Patrick Müller's avatar
Patrick Müller committed
                'type'  => 'NOT',
                'value' => null
Patrick Müller's avatar
Patrick Müller committed
            ]
        ];
Patrick Müller's avatar
Patrick Müller committed

        if (!is_null($days)) {
            $days    = (int)$days;
            $OldDate = new \DateTime();
Patrick Müller's avatar
Patrick Müller committed
            $OldDate->sub(new \DateInterval('P'.$days.'D'));
Patrick Müller's avatar
Patrick Müller committed

Patrick Müller's avatar
Patrick Müller committed
            $where['useDate'] = [
Patrick Müller's avatar
Patrick Müller committed
                'type'  => '<=',
                'value' => $OldDate->format('Y-m-d H:i:s')
Patrick Müller's avatar
Patrick Müller committed
            ];
Patrick Müller's avatar
Patrick Müller committed
        }

        QUI::getDataBase()->delete(
            self::getTable(),
            $where
        );
    }

    /**
Patrick Müller's avatar
Patrick Müller committed
     * Get CouponCode table
Patrick Müller's avatar
Patrick Müller committed
     *
     * @return string
     */
    public static function getTable()
    {
Patrick Müller's avatar
Patrick Müller committed
        return QUI::getDBTableName('quiqqer_coupons');
Patrick Müller's avatar
Patrick Müller committed
    }
}