diff --git a/events.xml b/events.xml index f061d54bc4132bc07c6922a21e76466a0103d99d..f267a3e3c895402b6d0115c714f6a578f9faf23f 100644 --- a/events.xml +++ b/events.xml @@ -41,5 +41,5 @@ <event on="onQuiqqerOrderSuccessful" fire="\QUI\ERP\Coupons\Events::removeCouponsFromSession"/> <event on="onQuiqqer::order::orderProcessFinish" fire="\QUI\ERP\Coupons\Events::removeCouponsFromSession"/> - + <event on="onQuiqqerMigrationV2" fire="\QUI\ERP\Coupons\Events::onQuiqqerMigrationV2"/> </events> diff --git a/src/QUI/ERP/Coupons/CouponCode.php b/src/QUI/ERP/Coupons/CouponCode.php index 87a36449817fe9ad355061565101bfbac5bab07b..d8af822743a13cd3fcd25881456695c54d9216a2 100644 --- a/src/QUI/ERP/Coupons/CouponCode.php +++ b/src/QUI/ERP/Coupons/CouponCode.php @@ -151,11 +151,11 @@ public function __construct(int $id) $this->title = $data['title']; if (!empty($data['usages'])) { - $this->usages = json_decode($data['usages'], true); + $this->usages = json_decode($data['usages'], true) ?? []; } if (!empty($data['userIds'])) { - $this->userIds = json_decode($data['userIds'], true); + $this->userIds = json_decode($data['userIds'], true) ?? []; } // migrate user ids @@ -169,7 +169,7 @@ public function __construct(int $id) } if (!empty($data['groupIds'])) { - $this->groupIds = json_decode($data['groupIds'], true); + $this->groupIds = json_decode($data['groupIds'], true) ?? []; } // migrate user ids @@ -187,7 +187,7 @@ public function __construct(int $id) } if (!empty($data['discountIds'])) { - $this->discountIds = json_decode($data['discountIds'], true); + $this->discountIds = json_decode($data['discountIds'], true) ?? []; } $this->CreateDate = new DateTime($data['createDate']); diff --git a/src/QUI/ERP/Coupons/Events.php b/src/QUI/ERP/Coupons/Events.php index d61be62d630c5b1d742b911b113e1ea9e37d541b..c2e5774aecfb16769c9acf304b9e26b087bc3f15 100644 --- a/src/QUI/ERP/Coupons/Events.php +++ b/src/QUI/ERP/Coupons/Events.php @@ -16,12 +16,14 @@ use QUI\ERP\Products\Handler\Fields; use QUI\ERP\Products\Interfaces\ProductInterface; use QUI\Smarty\Collector; +use QUI\System\Console\Tools\MigrationV2; use function array_merge; use function array_search; use function array_unique; use function in_array; use function is_array; +use function is_numeric; use function is_string; use function json_decode; use function json_encode; @@ -776,4 +778,58 @@ public static function onQuiqqerOrderCreated(AbstractOrder $Order): void { QUI\ERP\Coupons\Products\Handler::createCouponCodesFromOrder($Order); } + + public static function onQuiqqerMigrationV2(MigrationV2 $Console): void + { + $Console->writeLn('- Migrate coupons'); + $table = QUI::getDBTableName('quiqqer_coupons'); + + $result = QUI::getDataBase()->fetch([ + 'from' => $table + ]); + + foreach ($result as $entry) { + $id = $entry['id']; + $userIds = $entry['userIds']; + $groupIds = $entry['groupIds']; + + if (!empty($userIds)) { + $userIds = json_decode($userIds, true) ?? []; + + foreach ($userIds as $k => $userId) { + if (is_numeric($userId)) { + try { + $userIds[$k] = QUI::getUsers()->get($userId)->getUUID(); + } catch (QUI\Exception) { + } + } + } + + QUI::getDataBase()->update( + $table, + ['userIds' => json_encode($userIds)], + ['id' => $id] + ); + } + + if (!empty($groupIds)) { + $groupIds = json_decode($groupIds, true) ?? []; + + foreach ($groupIds as $k => $groupId) { + if (is_numeric($groupId)) { + try { + $groupIds[$k] = QUI::getGroups()->get($groupId)->getUUID(); + } catch (QUI\Exception) { + } + } + } + + QUI::getDataBase()->update( + $table, + ['groupIds' => json_encode($groupIds)], + ['id' => $id] + ); + } + } + } }