From 62d3b9789e35f965197c212370fbd9abb364cc8f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Patrick=20M=C3=BCller?= <p.mueller@pcsg.de>
Date: Tue, 6 Jul 2021 10:38:06 +0200
Subject: [PATCH] refactor: different default fields for physical / digital
 products

---
 src/QUI/ERP/Coupons/Events.php           | 20 ++++++++++++--------
 src/QUI/ERP/Coupons/Products/Handler.php | 15 ++++++++++++---
 2 files changed, 24 insertions(+), 11 deletions(-)

diff --git a/src/QUI/ERP/Coupons/Events.php b/src/QUI/ERP/Coupons/Events.php
index ecd2590..a7e214b 100644
--- a/src/QUI/ERP/Coupons/Events.php
+++ b/src/QUI/ERP/Coupons/Events.php
@@ -650,16 +650,20 @@ public static function onQuiqqerProductsProductCreate(ProductInterface $Product)
         $UniqueProduct->calc();
 
         $fields = [
-            CouponProductsHandler::PRODUCT_FIELD_ID_TRANSFERABLE              => true,
-            CouponProductsHandler::PRODUCT_FIELD_ID_SEND_MAIL                 => $isDigital,
-            CouponProductsHandler::PRODUCT_FIELD_ID_GENERATE_PDF              => $isDigital,
-            CouponProductsHandler::PRODUCT_FIELD_ID_COUPON_AMOUNT             => $UniqueProduct->getPrice()->getValue(),
-            CouponProductsHandler::PRODUCT_FIELD_ID_DAYS_VALID                => 1095, // 3 years
-            CouponProductsHandler::PRODUCT_FIELD_ID_COUPON_DESCRIPTION        => null,
-            CouponProductsHandler::PRODUCT_FIELD_ID_IS_SINGLE_PURPOSE_COUPON  => false,
-            CouponProductsHandler::PRODUCT_FIELD_ID_USER_DELIVERY_TYPE_SELECT => false
+//            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
         ];
 
+        // 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;
+        }
+
         foreach ($fields as $fieldId => $value) {
             try {
                 $Field = Fields::getField($fieldId);
diff --git a/src/QUI/ERP/Coupons/Products/Handler.php b/src/QUI/ERP/Coupons/Products/Handler.php
index c4a8150..ce8da5a 100644
--- a/src/QUI/ERP/Coupons/Products/Handler.php
+++ b/src/QUI/ERP/Coupons/Products/Handler.php
@@ -79,6 +79,13 @@ public static function createCouponCodesFromOrder(QUI\ERP\Order\AbstractOrder $O
                     )
                 );
 
+                /**
+                 * Physical coupons are not sent via mail or PDF
+                 */
+                if ($Product instanceof PhysicalCouponProductType) {
+                    continue;
+                }
+
                 // Generate coupon PDF
                 if ($Product->getFieldValue(self::PRODUCT_FIELD_ID_GENERATE_PDF)) {
                     $couponPdfFile = self::createCouponCodePdf($CouponCode, $Product);
@@ -197,10 +204,12 @@ protected static function createCouponCodeFromProduct(
         $couponAttributes['validUntilDate'] = $ValidUntil->format('Y-m-d');
 
         // Transferable (=usable by other users or guests)
-        $isTransferable = $Product->getFieldValue(self::PRODUCT_FIELD_ID_TRANSFERABLE);
+        if ($Product->hasField(self::PRODUCT_FIELD_ID_TRANSFERABLE)) {
+            $isTransferable = $Product->getFieldValue(self::PRODUCT_FIELD_ID_TRANSFERABLE);
 
-        if (empty($isTransferable)) {
-            $couponAttributes['userIds'] = [$User->getId()];
+            if (empty($isTransferable)) {
+                $couponAttributes['userIds'] = [$User->getId()];
+            }
         }
 
         return CouponsHandler::createCouponCode([$Discount->getId()], $couponAttributes);
-- 
GitLab