Skip to content
Code-Schnipsel Gruppen Projekte
create.php 2,03 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 CouponCode(s)
    
    Patrick Müller's avatar
    Patrick Müller committed
     *
     * @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']);
                }
    
    
                $discountIds = [];
    
                if (!empty($attributes['discountIds'])) {
                    $discountIds = explode(',', $attributes['discountIds']);
                }
    
    
    Patrick Müller's avatar
    Patrick Müller committed
                for ($i = 0; $i < $amount; $i++) {
    
                    $couponCodes[] = Handler::createCouponCode($discountIds, $attributes);
    
    Patrick Müller's avatar
    Patrick Müller committed
                }
    
    Patrick Müller's avatar
    Patrick Müller committed
            } catch (\QUI\ERP\Coupons\CouponCodeException $Exception) {
                QUI::getMessagesHandler()->addError(
                    QUI::getLocale()->get(
    
                        'quiqqer/coupons',
    
    Patrick Müller's avatar
    Patrick Müller committed
                        '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/coupons',
    
    Patrick Müller's avatar
    Patrick Müller committed
                        'message.ajax.general_error'
                    )
                );
    
                return false;
            }
    
            QUI::getMessagesHandler()->addSuccess(
                QUI::getLocale()->get(
    
                    'quiqqer/coupons',
    
    Patrick Müller's avatar
    Patrick Müller committed
                    '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'
    );