From d6d788df395ae8cf449914cfa0419daf51bcb2f0 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Patrick=20M=C3=BCller?= <p.mueller@pcsg.de>
Date: Mon, 18 Oct 2021 16:09:01 +0200
Subject: [PATCH] refactor: discount update -> set leading and trailing commas
 for user_groups db column

---
 src/QUI/ERP/Discount/Discount.php | 41 +++++++++++++++++++++++++++++++
 1 file changed, 41 insertions(+)

diff --git a/src/QUI/ERP/Discount/Discount.php b/src/QUI/ERP/Discount/Discount.php
index a0be90c..86c298a 100644
--- a/src/QUI/ERP/Discount/Discount.php
+++ b/src/QUI/ERP/Discount/Discount.php
@@ -579,4 +579,45 @@ public function toPriceFactor($Locale = null, $Customer = null)
             'visible'     => $hideDiscounts ? false : true
         ]);
     }
+
+    /**
+     * Update the CRUD child
+     *
+     * @throws QUI\ExceptionStack|QUI\Exception
+     */
+    public function update()
+    {
+        $this->Events->fireEvent('saveBegin');
+        $this->Events->fireEvent('updateBegin');
+
+        $needles   = $this->Factory->getChildAttributes();
+        $savedData = [];
+
+        foreach ($needles as $needle) {
+            if (!\array_key_exists($needle, $this->attributes)) {
+                continue;
+            }
+
+            $value = $this->getAttribute($needle);
+
+            switch ($needle) {
+                case 'user_groups':
+                    if (!empty($value)) {
+                        $value = ','.$value.',';
+                    }
+                    break;
+            }
+
+            $savedData[$needle] = $value;
+        }
+
+        QUI::getDataBase()->update(
+            $this->Factory->getDataBaseTableName(),
+            $savedData,
+            ['id' => $this->getId()]
+        );
+
+        $this->Events->fireEvent('saveEnd');
+        $this->Events->fireEvent('updateEnd');
+    }
 }
-- 
GitLab