From 58ffdd1ea2dd3e558a3a421d204b7b62f85cf537 Mon Sep 17 00:00:00 2001
From: Henning Leutz <leutz@pcsg.de>
Date: Wed, 24 Apr 2024 07:42:28 +0200
Subject: [PATCH] feat: use user / group uuids

---
 src/QUI/ERP/Coupons/CouponCode.php       | 27 +++++++++++++++++++++---
 src/QUI/ERP/Coupons/Products/Handler.php |  6 +++---
 2 files changed, 27 insertions(+), 6 deletions(-)

diff --git a/src/QUI/ERP/Coupons/CouponCode.php b/src/QUI/ERP/Coupons/CouponCode.php
index 4793245..99f766f 100644
--- a/src/QUI/ERP/Coupons/CouponCode.php
+++ b/src/QUI/ERP/Coupons/CouponCode.php
@@ -15,6 +15,7 @@
 use function in_array;
 use function is_array;
 use function is_null;
+use function is_numeric;
 use function json_decode;
 use function json_encode;
 use function method_exists;
@@ -154,10 +155,30 @@ public function __construct(int $id)
             $this->userIds = json_decode($data['userIds'], true);
         }
 
+        // migrate user ids
+        foreach ($this->userIds as $k => $userId) {
+            if (is_numeric($userId)) {
+                try {
+                    $this->userIds[$k] = QUI::getUsers()->get($userId)->getUUID();
+                } catch (QUI\Exception) {
+                }
+            }
+        }
+
         if (!empty($data['groupIds'])) {
             $this->groupIds = json_decode($data['groupIds'], true);
         }
 
+        // migrate user ids
+        foreach ($this->groupIds as $k => $groupId) {
+            if (is_numeric($groupId)) {
+                try {
+                    $this->groupIds[$k] = QUI::getGroups()->get($groupId)->getUUID();
+                } catch (QUI\Exception) {
+                }
+            }
+        }
+
         if (!empty($data['maxUsages'])) {
             $this->maxUsages = $data['maxUsages'];
         }
@@ -293,7 +314,7 @@ public function redeem(QUI\Interfaces\Users\User $User = null, QUI\ERP\Order\Ord
         $Now = new DateTime();
 
         $usage = [
-            'userId' => $User->getId(),
+            'userId' => $User->getUUID(),
             'date' => $Now->format('Y-m-d H:i:s'),
             'orderPrefixedId' => false
         ];
@@ -396,7 +417,7 @@ public function checkRedemption(QUI\Interfaces\Users\User $User)
 
         // Restriction to QUIQQER user(s)
         if (!empty($this->userIds)) {
-            if (in_array($User->getId(), $this->userIds)) {
+            if (in_array($User->getUUID(), $this->userIds)) {
                 if (
                     $this->maxUsages !== Handler::MAX_USAGE_UNLIMITED
                     && $this->hasUserRedeemed($User)
@@ -539,7 +560,7 @@ public function isValid(): bool
      */
     public function hasUserRedeemed(QUI\Interfaces\Users\User $User): bool
     {
-        $userId = $User->getId();
+        $userId = $User->getUUID();
 
         foreach ($this->usages as $usage) {
             if ($usage['userId'] === $userId) {
diff --git a/src/QUI/ERP/Coupons/Products/Handler.php b/src/QUI/ERP/Coupons/Products/Handler.php
index a464737..a4239af 100644
--- a/src/QUI/ERP/Coupons/Products/Handler.php
+++ b/src/QUI/ERP/Coupons/Products/Handler.php
@@ -111,8 +111,8 @@ public static function createCouponCodesFromOrder(QUI\ERP\Order\AbstractOrder $O
                     \rename($couponPdfFile, $newCouponPdfFile);
 
                     // Add PDF file to customer files
-                    CustomerFiles::addFileToCustomer($Customer->getId(), $newCouponPdfFile);
-                    CustomerFiles::addFileToDownloadEntry($Customer->getId(), \basename($newCouponPdfFile));
+                    CustomerFiles::addFileToCustomer($Customer->getUUID(), $newCouponPdfFile);
+                    CustomerFiles::addFileToDownloadEntry($Customer->getUUID(), \basename($newCouponPdfFile));
 
                     $customerDir = CustomerFiles::getFolderPath($Customer);
                     $couponFilePathCustomerDir = $customerDir . DIRECTORY_SEPARATOR . \basename($newCouponPdfFile);
@@ -210,7 +210,7 @@ protected static function createCouponCodeFromProduct(
             $isTransferable = $Product->getFieldValue(self::PRODUCT_FIELD_ID_TRANSFERABLE);
 
             if (empty($isTransferable)) {
-                $couponAttributes['userIds'] = [$User->getId()];
+                $couponAttributes['userIds'] = [$User->getUUID()];
             }
         }
 
-- 
GitLab