Skip to content
Code-Schnipsel Gruppen Projekte
create.php 2,01 KiB
Newer Older
  • Learn to ignore specific revisions
  • Patrick Müller's avatar
    Patrick Müller committed
    <?php
    
    /**
     * This file contains package_quiqqer_coupons_ajax_create
     */
    
    use QUI\ERP\Coupons\Handler;
    use QUI\Utils\Security\Orthos;
    
    /**
     * Create new InviteCode(s)
     *
     * @param array $attributes
     * @return bool - success
     */
    QUI::$Ajax->registerFunction(
        'package_quiqqer_coupons_ajax_create',
        function ($attributes) {
            $attributes = Orthos::clearArray(json_decode($attributes, true));
    
            try {
                $amount      = 1;
    
    Patrick Müller's avatar
    Patrick Müller committed
                $couponCodes = [];
    
    Patrick Müller's avatar
    Patrick Müller committed
    
                if (!empty($attributes['amount'])) {
                    $amount = (int)$attributes['amount'];
                    unset($attributes['amount']);
                }
    
                for ($i = 0; $i < $amount; $i++) {
    
    Patrick Müller's avatar
    Patrick Müller committed
                    $couponCodes[] = Handler::createCouponCode($attributes);
                }
    
                /** @var \QUI\ERP\Coupons\CouponCode $Code */
                foreach ($couponCodes as $Code) {
                    $Code->redeem();
    
    Patrick Müller's avatar
    Patrick Müller committed
                }
            } catch (\QUI\ERP\Coupons\CouponCodeException $Exception) {
                QUI::getMessagesHandler()->addError(
                    QUI::getLocale()->get(
                        'quiqqer/invitecode',
                        'message.ajax.create.error',
    
    Patrick Müller's avatar
    Patrick Müller committed
                        [
    
    Patrick Müller's avatar
    Patrick Müller committed
                            'error' => $Exception->getMessage()
    
    Patrick Müller's avatar
    Patrick Müller committed
                        ]
    
    Patrick Müller's avatar
    Patrick Müller committed
                    )
                );
    
                return false;
            } catch (QUI\Permissions\Exception $Exception) {
                throw $Exception;
            } catch (\Exception $Exception) {
                QUI\System\Log::writeException($Exception);
    
                QUI::getMessagesHandler()->addError(
                    QUI::getLocale()->get(
                        'quiqqer/invitecode',
                        'message.ajax.general_error'
                    )
                );
    
                return false;
            }
    
            QUI::getMessagesHandler()->addSuccess(
                QUI::getLocale()->get(
                    'quiqqer/invitecode',
                    'message.ajax.create.success'
                )
            );
    
            return true;
        },
    
    Patrick Müller's avatar
    Patrick Müller committed
        ['attributes'],
    
    Patrick Müller's avatar
    Patrick Müller committed
        'Permission::checkAdminUser'
    );