From 3c0da14239a4c619613727ea1604d071083b4e25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20M=C3=BCller?= <p.mueller@pcsg.de> Date: Wed, 30 Jun 2021 13:54:21 +0200 Subject: [PATCH] feat: coupon code now saved in order history; option to let customer choose coupon code delivery type --- locale.xml | 8 ++ products.xml | 1 + src/QUI/ERP/Coupons/Events.php | 81 +++++++++++++++---- .../Coupons/Products/CouponProductType.php | 52 ++++++++++-- src/QUI/ERP/Coupons/Products/Handler.php | 53 +++++++++++- 5 files changed, 173 insertions(+), 22 deletions(-) diff --git a/locale.xml b/locale.xml index 75e45f9..e297c93 100644 --- a/locale.xml +++ b/locale.xml @@ -138,6 +138,14 @@ Beste Grüße<br/> <de><![CDATA[[productTitle]__[date]]]></de> <en><![CDATA[[productTitle]__[date]]]></en> </locale> + <locale name="DownloadProduct.Order.history.coupon_code"> + <de><![CDATA[Gutschein-Code generiert für Bestellung von Produkt "[productTitle]" (#[productId]) -> [couponCode] ([couponAmount])]]></de> + <en><![CDATA[Coupon code generated for order of product "[productTitle]" (#[productId]) -> [couponCode] ([couponAmount])]]></en> + </locale> + <locale name="DownloadProduct.Order.history.coupon_code_send_mail"> + <de><![CDATA[Gutschein-Code "[couponCode]" per E-Mail an Kunden versandt ([customerMail]).]]></de> + <en><![CDATA[Sent coupon code "[couponCode]" to customer via email ([customoerMail]).]]></en> + </locale> <locale name="CouponCode.tpl.header"> <de><![CDATA[G U T S C H E I N]]></de> <en><![CDATA[C O U P O N]]></en> diff --git a/products.xml b/products.xml index 7c2d97d..2baaab1 100644 --- a/products.xml +++ b/products.xml @@ -14,6 +14,7 @@ <field>673</field> <field>674</field> <field>675</field> + <field>679</field> </fields> </fieldCategory> </fieldCategories> diff --git a/src/QUI/ERP/Coupons/Events.php b/src/QUI/ERP/Coupons/Events.php index 8d3543a..cccce0d 100644 --- a/src/QUI/ERP/Coupons/Events.php +++ b/src/QUI/ERP/Coupons/Events.php @@ -13,6 +13,7 @@ use QUI\ERP\Discount\EventHandling as DiscountEvents; use QUI\ERP\Coupons\Products\CouponProductType; use QUI\ERP\Coupons\Products\CouponProductException; +use QUI\ERP\Accounting\Calc as ErpCalc; /** * Class Events @@ -433,7 +434,7 @@ public static function removeCouponsFromSession() protected static function createProductFields() { $fields = [ - CouponProductType::PRODUCT_FIELD_ID_TRANSFERABLE => [ + CouponProductType::PRODUCT_FIELD_ID_TRANSFERABLE => [ 'title' => [ 'de' => 'Gutschein-Code ist übertragbar', 'en' => 'Coupon code is transferable' @@ -443,7 +444,7 @@ protected static function createProductFields() 'standard' => false, 'requiredField' => false ], - CouponProductType::PRODUCT_FIELD_ID_SEND_MAIL => [ + CouponProductType::PRODUCT_FIELD_ID_SEND_MAIL => [ 'title' => [ 'de' => 'Gutschein-Code per E-Mail senden', 'en' => 'Send coupon code via email' @@ -453,7 +454,7 @@ protected static function createProductFields() 'standard' => false, 'requiredField' => false ], - CouponProductType::PRODUCT_FIELD_ID_GENERATE_PDF => [ + CouponProductType::PRODUCT_FIELD_ID_GENERATE_PDF => [ 'title' => [ 'de' => 'Gutschein-Code als PDF bereitstellen', 'en' => 'Provide coupon code as PDF' @@ -463,7 +464,7 @@ protected static function createProductFields() 'standard' => false, 'requiredField' => false ], - CouponProductType::PRODUCT_FIELD_ID_COUPON_AMOUNT => [ + CouponProductType::PRODUCT_FIELD_ID_COUPON_AMOUNT => [ 'title' => [ 'de' => 'Gutschein Wert', 'en' => 'Coupon amount' @@ -473,7 +474,7 @@ protected static function createProductFields() 'standard' => false, 'requiredField' => true ], - CouponProductType::PRODUCT_FIELD_ID_DAYS_VALID => [ + CouponProductType::PRODUCT_FIELD_ID_DAYS_VALID => [ 'title' => [ 'de' => 'Gutschein-Code Gültigkeit (Tage)', 'en' => 'Coupon code validity (days)' @@ -483,7 +484,7 @@ protected static function createProductFields() 'standard' => false, 'requiredField' => true ], - CouponProductType::PRODUCT_FIELD_ID_COUPON_DESCRIPTION => [ + CouponProductType::PRODUCT_FIELD_ID_COUPON_DESCRIPTION => [ 'title' => [ 'de' => 'Gutschein-Beschreibung', 'en' => 'Coupon description' @@ -493,7 +494,7 @@ protected static function createProductFields() 'standard' => false, 'requiredField' => true ], - CouponProductType::PRODUCT_FIELD_ID_IS_SINGLE_PURPOSE_COUPON => [ + CouponProductType::PRODUCT_FIELD_ID_IS_SINGLE_PURPOSE_COUPON => [ 'title' => [ 'de' => 'Ist Einzweck-Gutschein (Besteuerung bei Gutschein-Kauf)', 'en' => 'Is single purpose coupon (taxation on voucher purchase)' @@ -502,6 +503,56 @@ protected static function createProductFields() 'public' => false, 'standard' => false, 'requiredField' => false + ], + CouponProductType::PRODUCT_FIELD_ID_USER_DELIVERY_TYPE_SELECT => [ + 'title' => [ + 'de' => 'Gutschein-Versand', + 'en' => 'Coupon delivery' + ], + 'type' => Fields::TYPE_ATTRIBUTE_LIST, + 'public' => true, + 'standard' => false, + 'requiredField' => true, + 'options' => [ + 'entries' => [ + [ + 'title' => [ + 'de' => 'per E-Mail', + '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 + ] + ] + ] + ], + CouponProductType::PRODUCT_FIELD_ID_USER_DELIVERY_TYPE_SELECT_ALLOW => [ + 'title' => [ + '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 ] ]; @@ -521,6 +572,7 @@ protected static function createProductFields() 'type' => $field['type'], 'titles' => $field['title'], 'workingtitles' => $field['title'], + 'description' => !empty($field['description']) ? $field['description'] : null, 'systemField' => 0, 'standardField' => !empty($field['standard']) ? 1 : 0, 'publicField' => !empty($field['public']) ? 1 : 0, @@ -558,13 +610,14 @@ public static function onQuiqqerProductsProductCreate(ProductInterface $Product) $UniqueProduct->calc(); $fields = [ - CouponProductType::PRODUCT_FIELD_ID_TRANSFERABLE => true, - CouponProductType::PRODUCT_FIELD_ID_SEND_MAIL => true, - CouponProductType::PRODUCT_FIELD_ID_GENERATE_PDF => true, - CouponProductType::PRODUCT_FIELD_ID_COUPON_AMOUNT => $UniqueProduct->getPrice()->getValue(), - CouponProductType::PRODUCT_FIELD_ID_DAYS_VALID => 1095, // 3 years - CouponProductType::PRODUCT_FIELD_ID_COUPON_DESCRIPTION => null, - CouponProductType::PRODUCT_FIELD_ID_IS_SINGLE_PURPOSE_COUPON => false, + CouponProductType::PRODUCT_FIELD_ID_TRANSFERABLE => true, + CouponProductType::PRODUCT_FIELD_ID_SEND_MAIL => true, + CouponProductType::PRODUCT_FIELD_ID_GENERATE_PDF => true, + CouponProductType::PRODUCT_FIELD_ID_COUPON_AMOUNT => $UniqueProduct->getPrice()->getValue(), + CouponProductType::PRODUCT_FIELD_ID_DAYS_VALID => 1095, // 3 years + CouponProductType::PRODUCT_FIELD_ID_COUPON_DESCRIPTION => null, + CouponProductType::PRODUCT_FIELD_ID_IS_SINGLE_PURPOSE_COUPON => false, + CouponProductType::PRODUCT_FIELD_ID_USER_DELIVERY_TYPE_SELECT => false ]; foreach ($fields as $fieldId => $value) { diff --git a/src/QUI/ERP/Coupons/Products/CouponProductType.php b/src/QUI/ERP/Coupons/Products/CouponProductType.php index 38a4c01..26c4fa2 100644 --- a/src/QUI/ERP/Coupons/Products/CouponProductType.php +++ b/src/QUI/ERP/Coupons/Products/CouponProductType.php @@ -15,13 +15,51 @@ class CouponProductType extends DigitalProduct /** * Special fields for coupon products */ - const PRODUCT_FIELD_ID_TRANSFERABLE = 670; - const PRODUCT_FIELD_ID_GENERATE_PDF = 671; - const PRODUCT_FIELD_ID_COUPON_AMOUNT = 672; - const PRODUCT_FIELD_ID_DAYS_VALID = 673; - const PRODUCT_FIELD_ID_COUPON_DESCRIPTION = 674; - const PRODUCT_FIELD_ID_IS_SINGLE_PURPOSE_COUPON = 675; - const PRODUCT_FIELD_ID_SEND_MAIL = 676; + const PRODUCT_FIELD_ID_TRANSFERABLE = 670; + const PRODUCT_FIELD_ID_GENERATE_PDF = 671; + const PRODUCT_FIELD_ID_COUPON_AMOUNT = 672; + const PRODUCT_FIELD_ID_DAYS_VALID = 673; + const PRODUCT_FIELD_ID_COUPON_DESCRIPTION = 674; + const PRODUCT_FIELD_ID_IS_SINGLE_PURPOSE_COUPON = 675; + const PRODUCT_FIELD_ID_SEND_MAIL = 676; + const PRODUCT_FIELD_ID_USER_DELIVERY_TYPE_SELECT = 678; + const PRODUCT_FIELD_ID_USER_DELIVERY_TYPE_SELECT_ALLOW = 679; + + /** + * Model constructor + * + * @param integer $pid - Product-ID + * @param array $product - Product Data + * + * @throws QUI\ERP\Products\Product\Exception + * @throws QUI\Exception + */ + public function __construct($pid, $product = []) + { + parent::__construct($pid, $product); + + if (!empty($this->fields[self::PRODUCT_FIELD_ID_USER_DELIVERY_TYPE_SELECT_ALLOW])) { + $userDeliveryTypeSelectAllow = $this->getFieldValue( + self::PRODUCT_FIELD_ID_USER_DELIVERY_TYPE_SELECT_ALLOW + ); + + if ($userDeliveryTypeSelectAllow) { + try { + $Field = QUI\ERP\Products\Handler\Fields::getField( + self::PRODUCT_FIELD_ID_USER_DELIVERY_TYPE_SELECT + ); + + $Field->setProduct($this); + + $this->fields[$Field->getId()] = $Field; + } catch (QUI\Exception $Exception) { + QUI\System\Log::writeException($Exception); + } + } else { + unset($this->fields[self::PRODUCT_FIELD_ID_USER_DELIVERY_TYPE_SELECT]); + } + } + } /** * @param QUI\Locale $Locale diff --git a/src/QUI/ERP/Coupons/Products/Handler.php b/src/QUI/ERP/Coupons/Products/Handler.php index 68a392a..bb78553 100644 --- a/src/QUI/ERP/Coupons/Products/Handler.php +++ b/src/QUI/ERP/Coupons/Products/Handler.php @@ -30,6 +30,7 @@ class Handler public static function createCouponCodesFromOrder(QUI\ERP\Order\AbstractOrder $Order): void { $Customer = $Order->getCustomer(); + $Currency = $Order->getCurrency(); /** @var QUI\ERP\Accounting\Article $Article */ foreach ($Order->getArticles() as $Article) { @@ -49,6 +50,21 @@ public static function createCouponCodesFromOrder(QUI\ERP\Order\AbstractOrder $O $CouponCode = self::createCouponCodeFromProduct($Product, $Order, $Customer); $couponFilePathCustomerDir = null; + $Order->addHistory( + QUI::getLocale()->get( + 'quiqqer/coupons', + 'DownloadProduct.Order.history.coupon_code', + [ + 'couponCode' => $CouponCode->getCode(), + 'productTitle' => $Product->getTitle(), + 'productId' => $Product->getId(), + 'couponAmount' => $Currency->format( + $Product->getFieldValue(CouponProductType::PRODUCT_FIELD_ID_COUPON_AMOUNT) + ) + ] + ) + ); + // Generate coupon PDF if ($Product->getFieldValue(CouponProductType::PRODUCT_FIELD_ID_GENERATE_PDF)) { $couponPdfFile = self::createCouponCodePdf($CouponCode, $Product); @@ -80,8 +96,43 @@ public static function createCouponCodesFromOrder(QUI\ERP\Order\AbstractOrder $O } // Send coupon via email - if ($Product->getFieldValue(CouponProductType::PRODUCT_FIELD_ID_SEND_MAIL)) { + + /* + * Check if customer explicitly chose "send by email" + */ + $deliveryTypeFieldData = $Article->getCustomField( + CouponProductType::PRODUCT_FIELD_ID_USER_DELIVERY_TYPE_SELECT + ); + + if ($deliveryTypeFieldData) { + switch ($deliveryTypeFieldData['value']) { + // by mail + case 1: + $sendMail = false; + break; + + // by email + default: + $sendMail = true; + + } + } else { + $sendMail = $Product->getFieldValue(CouponProductType::PRODUCT_FIELD_ID_SEND_MAIL); + } + + if ($sendMail) { self::sendCouponMail($CouponCode, $Product, $Customer, $couponFilePathCustomerDir); + + $Order->addHistory( + QUI::getLocale()->get( + 'quiqqer/coupons', + 'DownloadProduct.Order.history.coupon_code_send_mail', + [ + 'couponCode' => $CouponCode->getCode(), + 'customerMail' => QUI\ERP\Customer\Utils::getInstance()->getEmailByCustomer($Customer) + ] + ) + ); } } catch (\Exception $Exception) { if ($Exception->getCode() === 404) { -- GitLab