Skip to content

Commit 03dc06d

Browse files
faraz-glsreenia806
authored andcommitted
AC-8753: Coupon quantity generation checks
1 parent 2117252 commit 03dc06d

File tree

6 files changed

+209
-62
lines changed

6 files changed

+209
-62
lines changed

app/code/Magento/SalesRule/Controller/Adminhtml/Promo/Quote/Generate.php

Lines changed: 72 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,76 +3,105 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
7+
68
namespace Magento\SalesRule\Controller\Adminhtml\Promo\Quote;
79

10+
use Magento\Backend\App\Action\Context;
811
use Magento\Framework\App\Action\HttpPostActionInterface;
12+
use Magento\Framework\App\Config\ScopeConfigInterface;
13+
use Magento\Framework\App\ObjectManager;
14+
use Magento\Framework\App\Response\Http\FileFactory;
15+
use Magento\Framework\Exception\InputException;
16+
use Magento\Framework\Exception\LocalizedException;
917
use Magento\Framework\Filter\FilterInput;
18+
use Magento\Framework\Json\Helper\Data;
1019
use Magento\Framework\MessageQueue\PublisherInterface;
20+
use Magento\Framework\Registry;
21+
use Magento\Framework\Stdlib\DateTime\Filter\Date;
1122
use Magento\SalesRule\Api\Data\CouponGenerationSpecInterfaceFactory;
23+
use Magento\SalesRule\Controller\Adminhtml\Promo\Quote;
1224
use Magento\SalesRule\Model\CouponGenerator;
1325
use Magento\SalesRule\Model\Quote\GetCouponCodeLengthInterface;
26+
use Magento\SalesRule\Model\RegistryConstants;
27+
use Magento\SalesRule\Model\Rule;
28+
use Magento\Store\Model\ScopeInterface;
29+
use Psr\Log\LoggerInterface;
1430

1531
/**
1632
* Generate promo quote
1733
*
1834
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1935
*/
20-
class Generate extends \Magento\SalesRule\Controller\Adminhtml\Promo\Quote implements HttpPostActionInterface
36+
class Generate extends Quote implements HttpPostActionInterface
2137
{
2238
/**
2339
* @var CouponGenerator
2440
*/
25-
private $couponGenerator;
41+
private CouponGenerator $couponGenerator;
2642

2743
/**
2844
* @var PublisherInterface
2945
*/
30-
private $messagePublisher;
46+
private PublisherInterface $messagePublisher;
3147

3248
/**
3349
* @var CouponGenerationSpecInterfaceFactory
3450
*/
35-
private $generationSpecFactory;
51+
private CouponGenerationSpecInterfaceFactory $generationSpecFactory;
3652

3753
/**
3854
* @var GetCouponCodeLengthInterface
3955
*/
40-
private $getCouponCodeLength;
56+
private GetCouponCodeLengthInterface $getCouponCodeLength;
57+
58+
/**
59+
* @var ScopeConfigInterface
60+
*/
61+
private ScopeConfigInterface $scopeConfig;
62+
63+
/**
64+
* Handle coupon quantity system name
65+
*/
66+
private const XML_COUPON_QUANTITY = 'promo/auto_generated_coupon_codes/quantity';
4167

4268
/**
4369
* Generate constructor.
44-
* @param \Magento\Backend\App\Action\Context $context
45-
* @param \Magento\Framework\Registry $coreRegistry
46-
* @param \Magento\Framework\App\Response\Http\FileFactory $fileFactory
47-
* @param \Magento\Framework\Stdlib\DateTime\Filter\Date $dateFilter
70+
* @param Context $context
71+
* @param Registry $coreRegistry
72+
* @param FileFactory $fileFactory
73+
* @param Date $dateFilter
4874
* @param CouponGenerator|null $couponGenerator
4975
* @param PublisherInterface|null $publisher
5076
* @param CouponGenerationSpecInterfaceFactory|null $generationSpecFactory
5177
* @param GetCouponCodeLengthInterface|null $getCouponCodeLength
78+
* @param ScopeConfigInterface|null $scopeConfig
5279
*/
5380
public function __construct(
54-
\Magento\Backend\App\Action\Context $context,
55-
\Magento\Framework\Registry $coreRegistry,
56-
\Magento\Framework\App\Response\Http\FileFactory $fileFactory,
57-
\Magento\Framework\Stdlib\DateTime\Filter\Date $dateFilter,
81+
Context $context,
82+
Registry $coreRegistry,
83+
FileFactory $fileFactory,
84+
Date $dateFilter,
5885
CouponGenerator $couponGenerator = null,
5986
PublisherInterface $publisher = null,
6087
CouponGenerationSpecInterfaceFactory $generationSpecFactory = null,
61-
GetCouponCodeLengthInterface $getCouponCodeLength = null
88+
GetCouponCodeLengthInterface $getCouponCodeLength = null,
89+
ScopeConfigInterface $scopeConfig = null
6290
) {
6391
parent::__construct($context, $coreRegistry, $fileFactory, $dateFilter);
6492
$this->couponGenerator = $couponGenerator ?:
6593
$this->_objectManager->get(CouponGenerator::class);
66-
$this->messagePublisher = $publisher ?: \Magento\Framework\App\ObjectManager::getInstance()
94+
$this->messagePublisher = $publisher ?: ObjectManager::getInstance()
6795
->get(PublisherInterface::class);
6896
$this->generationSpecFactory = $generationSpecFactory ?:
69-
\Magento\Framework\App\ObjectManager::getInstance()->get(
97+
ObjectManager::getInstance()->get(
7098
CouponGenerationSpecInterfaceFactory::class
7199
);
72100
$this->getCouponCodeLength = $getCouponCodeLength ?:
73-
\Magento\Framework\App\ObjectManager::getInstance()->get(
101+
ObjectManager::getInstance()->get(
74102
GetCouponCodeLengthInterface::class
75103
);
104+
$this->scopeConfig = $scopeConfig;
76105
}
77106

78107
/**
@@ -81,7 +110,7 @@ public function __construct(
81110
* @return void
82111
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
83112
*/
84-
public function execute()
113+
public function execute(): void
85114
{
86115
if (!$this->getRequest()->isAjax()) {
87116
$this->_forward('noroute');
@@ -90,13 +119,13 @@ public function execute()
90119
$result = [];
91120
$this->_initRule();
92121

93-
$rule = $this->_coreRegistry->registry(\Magento\SalesRule\Model\RegistryConstants::CURRENT_SALES_RULE);
122+
$rule = $this->_coreRegistry->registry(RegistryConstants::CURRENT_SALES_RULE);
94123

95124
$data = $this->getRequest()->getParams();
96125

97126
if (!$rule->getId()) {
98127
$result['error'] = __('Rule is not defined');
99-
} elseif ((int) $rule->getCouponType() !== \Magento\SalesRule\Model\Rule::COUPON_TYPE_AUTO
128+
} elseif ((int) $rule->getCouponType() !== Rule::COUPON_TYPE_AUTO
100129
&& !$rule->getUseAutoGeneration()) {
101130
$result['error'] =
102131
__('The rule coupon settings changed. Please save the rule before using auto-generation.');
@@ -123,7 +152,7 @@ public function execute()
123152
$result['error'] = __(
124153
'Something went wrong while validating coupon code length. Please review the log and try again.'
125154
);
126-
$this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e);
155+
$this->_objectManager->get(LoggerInterface::class)->critical($e);
127156
}
128157
} else {
129158
try {
@@ -132,29 +161,40 @@ public function execute()
132161
$data = $inputFilter->getUnescaped();
133162
}
134163

135-
$data['quantity'] = isset($data['qty']) ? $data['qty'] : null;
136-
137-
$couponSpec = $this->generationSpecFactory->create(['data' => $data]);
138-
139-
$this->messagePublisher->publish('sales_rule.codegenerator', $couponSpec);
140-
$this->messageManager->addSuccessMessage(
141-
__('Message is added to queue, wait to get your coupons soon')
164+
$data['quantity'] = $data['qty'] ?? null;
165+
$couponQuantity = (int)$this->scopeConfig->getValue(
166+
self::XML_COUPON_QUANTITY,
167+
ScopeInterface::SCOPE_STORE
142168
);
169+
if ($data['quantity'] && $couponQuantity && $data['quantity'] > $couponQuantity) {
170+
$this->messageManager->addErrorMessage(
171+
__(
172+
'Coupon qty should be less than or equal to the coupon qty in the store configuration.'
173+
)
174+
);
175+
} else {
176+
$couponSpec = $this->generationSpecFactory->create(['data' => $data]);
177+
178+
$this->messagePublisher->publish('sales_rule.codegenerator', $couponSpec);
179+
$this->messageManager->addSuccessMessage(
180+
__('Message is added to queue, wait to get your coupons soon')
181+
);
182+
}
143183
$this->_view->getLayout()->initMessages();
144184
$result['messages'] = $this->_view->getLayout()->getMessagesBlock()->getGroupedHtml();
145-
} catch (\Magento\Framework\Exception\InputException $inputException) {
185+
} catch (InputException $inputException) {
146186
$result['error'] = __('Invalid data provided');
147-
} catch (\Magento\Framework\Exception\LocalizedException $e) {
187+
} catch (LocalizedException $e) {
148188
$result['error'] = $e->getMessage();
149189
} catch (\Exception $e) {
150190
$result['error'] = __(
151191
'Something went wrong while generating coupons. Please review the log and try again.'
152192
);
153-
$this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e);
193+
$this->_objectManager->get(LoggerInterface::class)->critical($e);
154194
}
155195
}
156196
$this->getResponse()->representJson(
157-
$this->_objectManager->get(\Magento\Framework\Json\Helper\Data::class)->jsonEncode($result)
197+
$this->_objectManager->get(Data::class)->jsonEncode($result)
158198
);
159199
}
160200
}

0 commit comments

Comments
 (0)