diff --git a/ajax/create.php b/ajax/create.php index 092c3b245b0204edf0c44024a124dce740af860b..f388f32d29424c1adebf4c7ee9e92919942db3ac 100644 --- a/ajax/create.php +++ b/ajax/create.php @@ -36,13 +36,6 @@ function ($attributes) { for ($i = 0; $i < $amount; $i++) { $couponCodes[] = Handler::createCouponCode($discountIds, $attributes); } - - // @todo TEST-CODE - REMOVE LATER! - /** @var \QUI\ERP\Coupons\CouponCode $Code */ - foreach ($couponCodes as $Code) { - $Code->redeem(); - } - //********************************************* } catch (\QUI\ERP\Coupons\CouponCodeException $Exception) { QUI::getMessagesHandler()->addError( QUI::getLocale()->get( diff --git a/bin/backend/controls/Manager.Usages.html b/bin/backend/controls/Manager.Usages.html index 5a3b5ec5004f500acc16960276d2a9106f727d26..8e8a8d6b7ee867633b02898996c8c3a1a737f8c9 100644 --- a/bin/backend/controls/Manager.Usages.html +++ b/bin/backend/controls/Manager.Usages.html @@ -5,9 +5,12 @@ <th class="quiqqer-coupons-manager-usages-tbl-date"> {{headerDate}} </th> - <th> + <th class="quiqqer-coupons-manager-usages-tbl-user"> {{headerUser}} </th> + <th> + {{headerOrderPrefixedId}} + </th> </tr> </thead> <tbody></tbody> diff --git a/bin/backend/controls/Manager.css b/bin/backend/controls/Manager.css index 63977b11469e8a721630359c2e3b1c5b6138cfb5..e085853ac5d84e209dc4e8d669cd0c36ed747d54 100644 --- a/bin/backend/controls/Manager.css +++ b/bin/backend/controls/Manager.css @@ -82,5 +82,9 @@ } .quiqqer-coupons-manager-usages-tbl-date { - width: 150px; + width: 100px; +} + +.quiqqer-coupons-manager-usages-tbl-user { + width: 200px; } \ No newline at end of file diff --git a/bin/backend/controls/Manager.js b/bin/backend/controls/Manager.js index 8bf5a0d497a30b2a45db06c518d03819b7d30a5b..0ffa025f8370239bb37722b14074f6d59182c5ca 100644 --- a/bin/backend/controls/Manager.js +++ b/bin/backend/controls/Manager.js @@ -572,7 +572,7 @@ define('package/quiqqer/coupons/bin/backend/controls/Manager', [ $showUsages: function (RowData) { new QUIConfirm({ maxHeight: 550, - maxWidth : 400, + maxWidth : 600, autoclose: false, title: QUILocale.get(lg, 'controls.Manager.usages.title', {code: RowData.code}), @@ -591,8 +591,9 @@ define('package/quiqqer/coupons/bin/backend/controls/Manager', [ var lgPrefix = 'controls.Manager.usages.template.'; Popup.setContent(Mustache.render(templateUsages, { - headerUser: QUILocale.get(lg, lgPrefix + 'headerUser'), - headerDate: QUILocale.get(lg, lgPrefix + 'headerDate') + headerUser : QUILocale.get(lg, lgPrefix + 'headerUser'), + headerDate : QUILocale.get(lg, lgPrefix + 'headerDate'), + headerOrderPrefixedId: QUILocale.get(lg, lgPrefix + 'headerOrderPrefixedId') })); var Content = Popup.getContent(); @@ -601,10 +602,14 @@ define('package/quiqqer/coupons/bin/backend/controls/Manager', [ for (var i = 0, len = RowData.usages.length; i < len; i++) { var usage = RowData.usages[i]; - new Element('tr', { + var Row = new Element('tr', { html: '<td>' + usage.date + '</td>' + '<td>' + usage.userId + ' (' + usage.userName + ')</td>' }).inject(TableBody); + + new Element('td', { + html: usage.orderPrefixedId ? usage.orderPrefixedId : '-' + }).inject(Row); } } } diff --git a/events.xml b/events.xml index 65b98cf3cd0dbd0b40f65ca413afad16ac93befc..faefcbafd958a8513b136e13bf1d9e149e138c8a 100644 --- a/events.xml +++ b/events.xml @@ -6,4 +6,8 @@ <event on="onQuiqqerOrderBasketToOrder" fire="\QUI\ERP\Coupons\Events::onQuiqqerOrderBasketToOrder" /> + <event on="onQuiqqerOrderSuccessful" + fire="\QUI\ERP\Coupons\Events::onQuiqqerOrderSuccessful" + /> + </events> \ No newline at end of file diff --git a/locale.xml b/locale.xml index 31135910ba3d8df5e3d5a7eafb95c87feca9bed7..f55c3de45030f38f0a0f579379d4694b122e387e 100644 --- a/locale.xml +++ b/locale.xml @@ -332,6 +332,10 @@ <de><![CDATA[Datum]]></de> <en><![CDATA[Date]]></en> </locale> + <locale name="controls.Manager.usages.template.headerOrderPrefixedId"> + <de><![CDATA[Bestellung]]></de> + <en><![CDATA[Order]]></en> + </locale> <locale name="controls.Manager.usages.btn_ok"> <de><![CDATA[OK]]></de> <en><![CDATA[OK]]></en> diff --git a/src/QUI/ERP/Coupons/CouponCode.php b/src/QUI/ERP/Coupons/CouponCode.php index 9a1b40c235bb8fbfe772787f1751c98537509fc7..837695e90bd70c79a84cfc50e66016c3fbc184bd 100644 --- a/src/QUI/ERP/Coupons/CouponCode.php +++ b/src/QUI/ERP/Coupons/CouponCode.php @@ -233,11 +233,12 @@ public function getDiscounts() * Hint: This may invalidate the code for future use * * @param QUI\Users\User $User - The user that redeems the CouponCode [if omitted use Session User] + * @param QUI\ERP\Order\Order $Order (optional) - Link redemption to a specific Order * @return void * @throws CouponCodeException * @throws QUI\Exception */ - public function redeem($User = null) + public function redeem($User = null, $Order = null) { if (is_null($User)) { $User = QUI::getUserBySession(); @@ -245,12 +246,20 @@ public function redeem($User = null) $this->checkRedemption($User); - $Now = new \DateTime(); - $this->usages[] = [ - 'userId' => $User->getId(), - 'date' => $Now->format('Y-m-d H:i:s') + $Now = new \DateTime(); + + $usage = [ + 'userId' => $User->getId(), + 'date' => $Now->format('Y-m-d H:i:s'), + 'orderPrefixedId' => false ]; + if (!is_null($Order) && $Order instanceof QUI\ERP\Order\Order) { + $usage['orderPrefixedId'] = $Order->getPrefixedId(); + } + + $this->usages[] = $usage; + QUI::getDataBase()->update( Handler::getTable(), [ diff --git a/src/QUI/ERP/Coupons/Events.php b/src/QUI/ERP/Coupons/Events.php index 8e2cdbcab076e2d64ae7c219fefcb002b806d956..7cec89ab8c9d7ee79097b688d9b3a75399413752 100644 --- a/src/QUI/ERP/Coupons/Events.php +++ b/src/QUI/ERP/Coupons/Events.php @@ -6,6 +6,7 @@ use Quiqqer\Engine\Collector; use QUI\ERP\Order\Basket\Basket; use QUI\ERP\Order\Basket\BasketGuest; +use QUI\ERP\Coupons\Handler as CouponsHandler; /** * Class Events @@ -90,4 +91,30 @@ public static function onQuiqqerOrderBasketToOrder( } catch (QUI\Exception $Exception) { } } + + /** + * quiqqer/order: onQuiqqerOrderSuccessful + * + * Redeem coupons used in (completed) orders + * + * @param QUI\ERP\Order\Order|QUI\ERP\Order\OrderInProcess $Order + * @return void + */ + public static function onQuiqqerOrderSuccessful($Order) + { + $coupons = $Order->getDataEntry('quiqqer-coupons'); + + if (empty($coupons)) { + return; + } + + foreach ($coupons as $couponCode) { + try { + $CouponCode = CouponsHandler::getCouponCodeByCode($couponCode); + $CouponCode->redeem($Order->getCustomer(), $Order); + } catch (\Exception $Exception) { + QUI\System\Log::writeException($Exception); + } + } + } }