Skip to content
Code-Schnipsel Gruppen Projekte
Events.php 26,5 KiB
Newer Older
<?php

namespace QUI\ERP\Coupons;

use QUI;
Patrick Müller's avatar
Patrick Müller committed
use QUI\ERP\Order\AbstractOrder;
use QUI\ERP\Products\Handler\Fields;
use QUI\ERP\Products\Interfaces\ProductInterface;
use Quiqqer\Engine\Collector;
use QUI\ERP\Order\Basket\Basket;
use QUI\ERP\Order\Basket\BasketGuest;
use QUI\ERP\Coupons\Handler as CouponsHandler;
use QUI\ERP\Discount\EventHandling as DiscountEvents;
use QUI\ERP\Coupons\Products\DigitalCouponProductType;
use QUI\ERP\Coupons\Products\PhysicalCouponProductType;
use QUI\ERP\Coupons\Products\CouponProductException;
use QUI\ERP\Accounting\Calc as ErpCalc;
use QUI\ERP\Coupons\Products\Handler as CouponProductsHandler;

/**
 * Class Events
 *
 * Global Event Handler for quiqqer/payment-paypal
 */
class Events
{
Patrick Müller's avatar
Patrick Müller committed
    /**
     * quiqqer/quiqqer: onPackageSetup
     *
     * @param QUI\Package\Package $Package
     * @return void
     */
    public static function onPackageSetup(QUI\Package\Package $Package)
    {
        try {
            self::createProductFields();
        } catch (\Exception $Exception) {
            QUI\System\Log::writeException($Exception);
        }
    }

    /**
     * Template event quiqqer/order: onQuiqqer::order::orderProcessBasketEnd
     *
     * @param Collector $Collector
     * @param BasketGuest $Basket
    public static function templateOrderProcessBasketEnd(Collector $Collector, $Basket, $Order)
        if (!($Basket instanceof Basket)
            && !($Basket instanceof QUI\ERP\Order\Basket\BasketOrder)
        ) {
        if (isset($Order) && isset($_GET['coupon'])) {
            try {
                $code = Handler::sanitizeCode($_GET['coupon']);

                $CouponCode = Handler::getCouponCodeByCode($code);
                $CouponCode->checkRedemption(QUI::getUserBySession());
                $CouponCode->addToOrder($Order);
            } catch (\Exception $Exception) {
            }
        }

        $Collector->append(
            '<div data-qui="package/quiqqer/coupons/bin/frontend/controls/CouponCodeInput"></div>'
        );
    }

    /**
     * @param QUI\ERP\Order\OrderProcess $OrderProcess
     * @throws QUI\ERP\Order\Exception
     * @throws QUI\Exception
     */
    public static function onOrderProcess(QUI\ERP\Order\OrderProcess $OrderProcess)
    {
        $CurrentStep = $OrderProcess->getCurrentStep();
        $currentStep = $CurrentStep->getType();

        if ($currentStep !== QUI\ERP\Order\Controls\OrderProcess\Basket::class) {
            return;
        }

        $coupons = QUI::getSession()->get('quiqqer-coupons');

        if (\is_string($coupons)) {
            $coupons = \json_decode($coupons, true);
        }

        if (!\is_array($coupons)) {
            $coupons = [];
        }

        if (isset($_GET['coupon'])) {
            $coupons[] = $_GET['coupon'];
        }

        $coupons = \array_unique($coupons);

        if (empty($coupons)) {
        $Order = $OrderProcess->getOrder();

        foreach ($coupons as $coupon) {
            self::addCouponToOrder($Order, $coupon);
        }
    }

    /**
     * @param QUI\ERP\Order\Basket\Basket $Basket
     * @param $pos
     */
    public static function onQuiqqerOrderBasketRemovePos(
        QUI\ERP\Order\Basket\Basket $Basket,
        $pos
    ) {
        $Order = null;
            $Order = $Basket->getOrder();
        } catch (QUI\Exception $Exception) {
            $Orders = QUI\ERP\Order\Handler::getInstance();
            try {
                $Order = $Orders->getLastOrderInProcessFromUser(QUI::getUserBySession());
            } catch (QUI\Exception $Exception) {
            }
        }
        if (!$Order) {
            QUI::getSession()->remove('quiqqer-coupons');
            return;
        }
        $Article      = $Order->getArticles()->getArticle($pos);
        $customData   = $Article->getCustomData();
        $orderCoupons = $Order->getDataEntry('quiqqer-coupons');
        $articleCouponCode = false;

        if (isset($customData['package']) && isset($customData['code'])) {
            $articleCouponCode = $customData['code'];
        }

        if (!$articleCouponCode) {
            return;
        }

        // custom data has code params, so article is an coupon code
        // we need to delete it
        if (\in_array($articleCouponCode, $orderCoupons)) {
            $pos = \array_search($articleCouponCode, $orderCoupons);
            unset($orderCoupons[$pos]);

            $Order->setData('quiqqer-coupons', $orderCoupons);

            try {
                $Order->save();
            } catch (QUI\Exception $Exception) {
            }
        }

        // look at session coupons
        // we need to delete it
        $coupons = QUI::getSession()->get('quiqqer-coupons');

        if (\is_string($coupons)) {
            $coupons = \json_decode($coupons, true);
        }

        if (!\is_array($coupons) || empty($coupons)) {
            return;
        }

        if (!\in_array($customData['code'], $coupons)) {
            return;
        }

        // remove code from session
        // because code is deleted
        $newCouponList = [];

        foreach ($coupons as $coupon) {
            if ($customData['code'] !== $coupon) {
                $newCouponList[] = $coupon;
            }
        }

        if (empty($newCouponList)) {
            QUI::getSession()->remove('quiqqer-coupons');
        } else {
            QUI::getSession()->set('quiqqer-coupons', \json_encode($newCouponList));
    /**
     * event - on price factor init
     *
     * @param $Basket
     * @param QUI\ERP\Order\AbstractOrder $Order
     * @param QUI\ERP\Products\Product\ProductList $Products
     */
    public static function onQuiqqerOrderBasketToOrder(
        $Basket,
        QUI\ERP\Order\AbstractOrder $Order,
        QUI\ERP\Products\Product\ProductList $Products
        $coupons        = $Order->getDataEntry('quiqqer-coupons');
        $sessionCoupons = QUI::getSession()->get('quiqqer-coupons');
        if (!\is_array($coupons)) {
            $coupons = [];
        if (\is_string($sessionCoupons)) {
            $sessionCoupons = \json_decode($sessionCoupons, true);

            if (\is_array($sessionCoupons)) {
                $coupons = \array_merge($coupons, $sessionCoupons);
            }

            $coupons = \array_unique($coupons);

            self::addSessionCouponsToOrder($Order, $sessionCoupons);
        $PriceFactors = $Products->getPriceFactors();
        $products     = $Products->toArray();
        $productCount = $Products->count();
        $subSum       = $products['calculations']['subSum'];
        $checkRedeemable = !$Order->isSuccessful(); // if order is successful we dont need a check
        $OrderInProcess  = $Order->getAttribute('OrderInProcess');
        $added           = false;

        if ($Order->getAttribute('inOrderCreation')) {
            $checkRedeemable = false;
        }

        if ($OrderInProcess instanceof QUI\ERP\Order\OrderInProcess
            && $OrderInProcess->getAttribute('inOrderCreation')
        ) {
            $checkRedeemable = false;
        }

        foreach ($coupons as $coupon) {
            /* @var $Coupon CouponCode */
            try {
                $Coupon = Handler::getCouponCodeByCode($coupon);
            } catch (\Exception $Exception) {
                continue;
            }

            // coupon check
            if ($checkRedeemable && !$Coupon->isRedeemable($Order->getCustomer())) {
                continue;
            }

            /* @var $Discount QUI\ERP\Discount\Discount */
            $discounts = $Coupon->getDiscounts();

            foreach ($discounts as $Discount) {
                if (!DiscountEvents::isDiscountUsableWithQuantity($Discount, $productCount)) {
                    continue;
                }

Henning Leutz's avatar
Henning Leutz committed
                if ($Discount->getAttribute('scope') === QUI\ERP\Discount\Handler::DISCOUNT_SCOPE_GRAND_TOTAL) {
                    // do nothing for this scope
                    // since this scope requires all price factors etc, this cannot be calculated here
                } elseif (!DiscountEvents::isDiscountUsableWithPurchaseValue($Discount, $subSum)) {
                $PriceFactor = $Discount->toPriceFactor(null, $Order->getCustomer());
                $PriceFactor->setTitle(
                    QUI::getLocale()->get('quiqqer/coupons', 'coupon.discount.title', [
                        'code' => $Coupon->getCode()
                    ])
Henning Leutz's avatar
Henning Leutz committed
                $isUnique     = $Discount->getAttribute('scope') === QUI\ERP\Discount\Handler::DISCOUNT_SCOPE_UNIQUE;
                $everyProduct = $Discount->getAttribute('scope') === QUI\ERP\Discount\Handler::DISCOUNT_SCOPE_EVERY_PRODUCT;

                if ($everyProduct || $isUnique) {
                    // add to the product
Henning Leutz's avatar
Henning Leutz committed
                    $products     = $Products->getProducts();
                    $alreadyAdded = false;
Henning Leutz's avatar
Henning Leutz committed

                    foreach ($products as $Product) {
                        if ($Discount->canUsedWith($Product) === false) {
                            continue;
                        }

Henning Leutz's avatar
Henning Leutz committed
                        if ($isUnique && $alreadyAdded) {
                            continue;
                        }

                        if ($Product instanceof QUI\ERP\Products\Product\UniqueProduct) {
                            $Product->getPriceFactors()->add($PriceFactor);
Henning Leutz's avatar
Henning Leutz committed
                            $added        = true;
                            $alreadyAdded = true;
                $PriceFactors->addToEnd($PriceFactor);
            }
        }

        if ($added) {
            try {
                $Products->recalculation();
            } catch (QUI\Exception $Exception) {
                QUI\System\Log::writeDebugException($Exception);
            }

    /**
     * quiqqer/order: onQuiqqerOrderSuccessful
     *
     * Redeem coupons used in (completed) orders
     *
     * @param QUI\ERP\Order\Order|QUI\ERP\Order\OrderInProcess $Order
     * @return void
     */
    public static function onQuiqqerOrderSuccessful($Order)
    {
        $coupons = $Order->getDataEntry('quiqqer-coupons');

        foreach ($coupons as $couponCode) {
            try {
                $CouponCode = CouponsHandler::getCouponCodeByCode($couponCode);
                $CouponCode->redeem($Order->getCustomer(), $Order);
            } catch (\Exception $Exception) {
                QUI\System\Log::writeException($Exception);
            }
        }
    }

    /**
     * @param $Order
     * @param $coupons
     */
    protected static function addSessionCouponsToOrder($Order, $coupons)
    {
        if (!\is_array($coupons)) {
            return;
        }

        // coupons as article if not added
        $Articles     = $Order->getArticles();
        $isInArticles = function ($code) use ($Articles) {
            foreach ($Articles as $Article) {
                $customData = $Article->getCustomData();

                if (isset($customData['code']) && $customData['code'] === $code) {
                    return true;
                }
            }

            return false;
        };

        foreach ($coupons as $coupon) {
            if ($isInArticles($coupon) === false) {
                self::addCouponToOrder($Order, $coupon);
            }
        }
    }

    /**
     * @param $Order
     * @param $coupon
     */
    protected static function addCouponToOrder($Order, $coupon)
    {
        if (!($Order instanceof QUI\ERP\Order\OrderInProcess)) {
            return;
        }

        try {
            $code = Handler::sanitizeCode($coupon);

            $CouponCode = Handler::getCouponCodeByCode($code);
            $CouponCode->checkRedemption(QUI::getUserBySession());
Henning Leutz's avatar
Henning Leutz committed
            $CouponCode->checkOrderRedemption($Order);

            $coupons   = $Order->getDataEntry('quiqqer-coupons');
            $coupons[] = $code;

            $coupons = \array_unique($coupons);

            $Order->setData('quiqqer-coupons', $coupons);
            $Order->update();

            $CouponCode->addToOrder($Order);
        } catch (\Exception $Exception) {
        }
    }
     * Removes all coupons from the current session.
Henning Leutz's avatar
Henning Leutz committed
     *
     * @return void
Henning Leutz's avatar
Henning Leutz committed
     */
    public static function removeCouponsFromSession()
    {
        QUI::getSession()->remove('quiqqer-coupons');
    }
Patrick Müller's avatar
Patrick Müller committed
    /**
     * Create all fixed product fields that quiqqer/stock-management provides
     *
     * @return void
     * @throws QUI\Exception
     */
    protected static function createProductFields()
    {
        $fields = [
            CouponProductsHandler::PRODUCT_FIELD_ID_TRANSFERABLE                    => [
Patrick Müller's avatar
Patrick Müller committed
                'title'         => [
Patrick Müller's avatar
Patrick Müller committed
                    'de' => 'Gutschein-Code ist übertragbar',
                    'en' => 'Coupon code is transferable'
                ],
                'description'   => [
                    'de' => 'Übertragbare Gutscheine sind auch von anderen Personen als dem Käufer einlösbar.'
                            .' Nicht übertragbare Gutscheine können nur vom Käufer eingelöst werden, wenn dieser'
                            .' eingeloggt ist.',
                    'en' => 'Transferable coupons are also redeemable by persons other than the buyer.'
                            .' Non-transferable vouchers can only be redeemed by the buyer when logged in.'
                ],
Patrick Müller's avatar
Patrick Müller committed
                'type'          => Fields::TYPE_BOOL,
                'public'        => false,
                'standard'      => false,
                'requiredField' => false
Patrick Müller's avatar
Patrick Müller committed
            ],
            CouponProductsHandler::PRODUCT_FIELD_ID_SEND_MAIL                       => [
                'title'         => [
                    'de' => 'Gutschein-Code per E-Mail senden',
                    'en' => 'Send coupon code via email'
                ],
                'description'   => [
                    'de' => 'Der Gutschein-Code wird dem Käufer per E-Mail gesendet. Gilt nicht, wenn der Benutzer zw.'
                            .' Post- und Mail-Versand wählen kann und den Postversand auswählt.'
                            .' Wird der Gutschein auch als PDF-Datei generiert, wird die PDF-Datei an diese E-Mail angehanden.',
                    'en' => 'The coupon code is sent to the buyer via email. It is not sent if the customer is able to'
                            .' choose between email and mail delivery type and chooses delivery by mail.'
                            .' If the coupon is also generated as a PDF file, the file is attached to this email.'
                ],
                'type'          => Fields::TYPE_BOOL,
                'public'        => false,
                'standard'      => false,
                'requiredField' => false
            ],
            CouponProductsHandler::PRODUCT_FIELD_ID_GENERATE_PDF                    => [
Patrick Müller's avatar
Patrick Müller committed
                'title'         => [
Patrick Müller's avatar
Patrick Müller committed
                    'de' => 'Gutschein-Code als PDF bereitstellen',
Patrick Müller's avatar
Patrick Müller committed
                    'en' => 'Provide coupon code as PDF'
Patrick Müller's avatar
Patrick Müller committed
                ],
                'description'   => [
                    'de' => 'Der Gutschein wird auch als PDF-Datei erstellt und dem Käufer (je nach Wahl) per E-Mail gesendet.'
                            .' Zusätzlich wird die PDF-Datei dem Käufer in seinem Frontend-Profil (sofern eingerichtet)'
                            .' bereitgestellt.',
                    'en' => 'The coupon is also generated as a PDF file and is sent to the customer (depending on choice)'
                            .' via email. Additionally, the PDF file is made available via the customers frontend profile'
                            .' (if set up).'
                ],
Patrick Müller's avatar
Patrick Müller committed
                'type'          => Fields::TYPE_BOOL,
                'public'        => false,
                'standard'      => false,
                'requiredField' => false
Patrick Müller's avatar
Patrick Müller committed
            ],
            CouponProductsHandler::PRODUCT_FIELD_ID_COUPON_AMOUNT                   => [
Patrick Müller's avatar
Patrick Müller committed
                'title'         => [
Patrick Müller's avatar
Patrick Müller committed
                    'de' => 'Gutschein Wert',
                    'en' => 'Coupon amount'
Patrick Müller's avatar
Patrick Müller committed
                ],
Patrick Müller's avatar
Patrick Müller committed
                'type'          => Fields::TYPE_FLOAT,
                'public'        => false,
                'standard'      => false,
                'requiredField' => true
Patrick Müller's avatar
Patrick Müller committed
            ],
            CouponProductsHandler::PRODUCT_FIELD_ID_DAYS_VALID                      => [
Patrick Müller's avatar
Patrick Müller committed
                'title'         => [
Patrick Müller's avatar
Patrick Müller committed
                    'de' => 'Gutschein-Code Gültigkeit (Tage)',
                    'en' => 'Coupon code validity (days)'
                ],
Patrick Müller's avatar
Patrick Müller committed
                'type'          => Fields::TYPE_INT,
                'public'        => false,
                'standard'      => false,
                'requiredField' => true
Patrick Müller's avatar
Patrick Müller committed
            ],
            CouponProductsHandler::PRODUCT_FIELD_ID_COUPON_DESCRIPTION              => [
Patrick Müller's avatar
Patrick Müller committed
                'title'         => [
Patrick Müller's avatar
Patrick Müller committed
                    'de' => 'Gutschein-Beschreibung',
                    'en' => 'Coupon description'
                ],
                'description'   => [
                    'de' => 'Diese Beschreibung taucht unter dem Titel "GUTSCHEIN" auf der generierten PDF-Datei auf.',
                    'en' => 'This description appears under the title caption "COUPON" on the generated PDF file.'
                ],
Patrick Müller's avatar
Patrick Müller committed
                'type'          => Fields::TYPE_INPUT_MULTI_LANG,
                'public'        => false,
                'standard'      => false,
                'requiredField' => false
            CouponProductsHandler::PRODUCT_FIELD_ID_IS_SINGLE_PURPOSE_COUPON        => [
Patrick Müller's avatar
Patrick Müller committed
                'title'         => [
                    'de' => 'Ist Einzweck-Gutschein (Besteuerung bei Gutschein-Kauf)',
                    'en' => 'Is single purpose coupon (taxation on voucher purchase)'
                'description'   => [
                    'de' => 'Einzweck-Gutscheine sind solche, bei denen die Besteuerung und der Leistungsort bereits'
                            .' beim Gutschein-Kauf feststehen. Beispiel: Gutschein für eine Massage in einem Spa.'
                            .' Alles andere (wie z.B. Wertgutscheine für den Einsatz unabhängig vom Artikel) sind'
                            .' Mehrzweck-Gutscheine und werden bei Einkauf nicht besteuert.',
                    'en' => 'Single-purpose coupons are those for which the taxation and place of performance are already'
                            .' determined at the time of coupon purchase. Example: voucher for a massage in a spa.'
                            .' Everything else (such as money value coupons for use regardless of the item) are'
                            .' multi-purpose coupons and are not taxed at the time of purchase.'
                ],
Patrick Müller's avatar
Patrick Müller committed
                'type'          => Fields::TYPE_BOOL,
                'public'        => false,
                'standard'      => false,
                'requiredField' => false
            CouponProductsHandler::PRODUCT_FIELD_ID_USER_DELIVERY_TYPE_SELECT       => [
                    'de' => 'Gutschein - Versand',
                    'en' => 'Coupon delivery'
                ],
                'type'          => Fields::TYPE_ATTRIBUTE_LIST,
                'public'        => true,
                'standard'      => false,
                'requiredField' => true,
                'options'       => [
                    'entries' => [
                        [
                            'title'     => [
                                'en' => 'via email'
                            ],
                            'sum'       => 0,
                            'type'      => ErpCalc::CALCULATION_COMPLEMENT,
                            'selected'  => true,
                            'userinput' => false
                        ],
                        [
                            'title'     => [
                                'de' => 'per Post',
                                'en' => 'via mail'
                            ],
                            'sum'       => 0,
                            'type'      => ErpCalc::CALCULATION_COMPLEMENT,
                            'selected'  => false,
                            'userinput' => false
                        ]
                    ]
                ]
            ],
            CouponProductsHandler::PRODUCT_FIELD_ID_USER_DELIVERY_TYPE_SELECT_ALLOW => [
                    'de' => 'Kunde darf Gutschein - Versandart wählen',
                    'en' => 'Customer can choose coupon delivery type'
                ],
                'description'   => [
                    'de' => 'Ist diese Funktion aktiviert, kann der Kunde beim Artikel im Shop wählen, ob der Gutschein'
                            .' per E - Mail oder Post versandt werden soll.',
                    'en' => 'if this option is enabled, the customer can choose at the article in the store whether the'
                            .' coupon should be sent by e - mail or by(physical) mail.'
                ],
                'type'          => Fields::TYPE_BOOL,
                'public'        => false,
                'standard'      => false,
                'requiredField' => false
Patrick Müller's avatar
Patrick Müller committed
            ]
        ];

        $fieldsCreated = false;

        foreach ($fields as $fieldId => $field) {
            try {
                Fields::getField($fieldId);
                continue;
            } catch (\Exception $Exception) {
                // Field does not exist -> create it
            }

            try {
                Fields::createField([
                    'id'            => $fieldId,
                    'type'          => $field['type'],
                    'titles'        => $field['title'],
                    'workingtitles' => $field['title'],
                    'description'   => !empty($field['description']) ? $field['description'] : null,
Patrick Müller's avatar
Patrick Müller committed
                    'systemField'   => 0,
                    'standardField' => !empty($field['standard']) ? 1 : 0,
                    'publicField'   => !empty($field['public']) ? 1 : 0,
Patrick Müller's avatar
Patrick Müller committed
                    'options'       => !empty($field['options']) ? $field['options'] : null,
                    'requiredField' => !empty($field['requiredField']) ? 1 : 0
Patrick Müller's avatar
Patrick Müller committed
                ]);
            } catch (\Exception $Exception) {
                QUI\System\Log::writeException($Exception);
                continue;
            }

            $fieldsCreated = true;
        }

        if ($fieldsCreated) {
            QUI\Translator::publish('quiqqer / products');
Patrick Müller's avatar
Patrick Müller committed
        }
    }

    /**
     * Assign plan product fields to a product
     *
     * @param ProductInterface $Product
     * @return void
Patrick Müller's avatar
Patrick Müller committed
     */
    public static function onQuiqqerProductsProductCreate(ProductInterface $Product)
    {
        if (!($Product instanceof DigitalCouponProductType) && !($Product instanceof PhysicalCouponProductType)) {
Patrick Müller's avatar
Patrick Müller committed
            return;
        }

        $isDigital     = $Product instanceof DigitalCouponProductType;
Patrick Müller's avatar
Patrick Müller committed
        $UniqueProduct = $Product->createUniqueProduct();
        $UniqueProduct->calc();

Patrick Müller's avatar
Patrick Müller committed
        $fields = [
//            CouponProductsHandler::PRODUCT_FIELD_ID_TRANSFERABLE             => true,
            CouponProductsHandler::PRODUCT_FIELD_ID_COUPON_AMOUNT            => $UniqueProduct->getPrice()->getValue(),
            CouponProductsHandler::PRODUCT_FIELD_ID_DAYS_VALID               => 1095, // 3 years
            CouponProductsHandler::PRODUCT_FIELD_ID_IS_SINGLE_PURPOSE_COUPON => false
Patrick Müller's avatar
Patrick Müller committed
        ];

        // Digital coupons get some extra fields
        if ($isDigital) {
            $fields[CouponProductsHandler::PRODUCT_FIELD_ID_SEND_MAIL]                 = true;
            $fields[CouponProductsHandler::PRODUCT_FIELD_ID_GENERATE_PDF]              = true;
            $fields[CouponProductsHandler::PRODUCT_FIELD_ID_USER_DELIVERY_TYPE_SELECT] = false;
            $fields[CouponProductsHandler::PRODUCT_FIELD_ID_COUPON_DESCRIPTION]        = null;
        }

Patrick Müller's avatar
Patrick Müller committed
        foreach ($fields as $fieldId => $value) {
            try {
                $Field = Fields::getField($fieldId);
                $Field->setValue($value);

                $Product->addOwnField($Field);
            } catch (\Exception $Exception) {
                QUI\System\Log::writeException($Exception);
            }
        }

        // No VAT tax types -> Choose first one found
        $noVatTaxTypes = QUI\ERP\Coupons\Products\Handler::getNoVatTaxTypes();

        if (!empty($noVatTaxTypes)) {
            $Product->getField(Fields::FIELD_VAT)->setValue($noVatTaxTypes[0]->getId());
        }

Patrick Müller's avatar
Patrick Müller committed
        try {
            $Product->update(QUI::getUsers()->getSystemUser());
        } catch (\Exception $Exception) {
            QUI\System\Log::writeException($Exception);
        }
    }

    /**
     * quiqqer/products: onQuiqqerProductsProductActivate
     *
     * Check if a coupon product has the correct tax type!
     *
     * @param ProductInterface $Product
     *
     * @throws CouponProductException
     * @throws QUI\Exception
     */
    public static function onQuiqqerProductsProductActivate(ProductInterface $Product)
    {
        if (!($Product instanceof DigitalCouponProductType) && !($Product instanceof PhysicalCouponProductType)) {
        $isSinglePurposeCoupon = $Product->getFieldValue(CouponProductsHandler::PRODUCT_FIELD_ID_IS_SINGLE_PURPOSE_COUPON);

        if (!empty($isSinglePurposeCoupon)) {
            return;
        }

        $productTaxTypeId = (int)$Product->getFieldValue(Fields::FIELD_VAT);
        $noVatTaxTypes    = QUI\ERP\Coupons\Products\Handler::getNoVatTaxTypes();

        foreach ($noVatTaxTypes as $TaxType) {
            if ($TaxType->getId() === $productTaxTypeId) {
                return;
            }
        }

        throw new CouponProductException([
            'exception.CouponProduct.no_vat_tax_type_required',
            [
                'productTitle' => $Product->getTitle(),
                'productId'    => $Product->getId()
            ]
        ]);
    }

Patrick Müller's avatar
Patrick Müller committed
    /**
     * quiqqer/order: onQuiqqerOrderCreated
     *
     * Parse coupon attributes from order and create coupon codes for the buyer.
     *
     * @param AbstractOrder $Order
     * @return void
     */
    public static function onQuiqqerOrderCreated(AbstractOrder $Order)
    {
Patrick Müller's avatar
Patrick Müller committed
        QUI\ERP\Coupons\Products\Handler::createCouponCodesFromOrder($Order);
Patrick Müller's avatar
Patrick Müller committed
    }