Skip to content

Commit 95479e6

Browse files
committed
Merge branch 'ACP2E-1195' of https://github.com/magento-l3/magento2ce into PR-2023-02-24
2 parents 7fa1884 + 6cb25db commit 95479e6

File tree

3 files changed

+119
-2
lines changed
  • app/code/Magento/SalesRule
  • dev/tests/integration/testsuite/Magento/SalesRule/Model

3 files changed

+119
-2
lines changed

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

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ public function execute()
9393
}
9494

9595
$session = $this->_objectManager->get(\Magento\Backend\Model\Session::class);
96-
9796
$validateResult = $model->validateData(new \Magento\Framework\DataObject($data));
9897
if ($validateResult !== true) {
9998
foreach ($validateResult as $errorMessage) {
@@ -120,13 +119,14 @@ public function execute()
120119
$data['actions'] = $data['rule']['actions'];
121120
}
122121
unset($data['rule']);
122+
123+
$data = $this->updateCouponData($data);
123124
$model->loadPost($data);
124125

125126
$useAutoGeneration = (int)(
126127
!empty($data['use_auto_generation']) && $data['use_auto_generation'] !== 'false'
127128
);
128129
$model->setUseAutoGeneration($useAutoGeneration);
129-
130130
$session->setPageData($model->getData());
131131

132132
$model->save();
@@ -177,4 +177,21 @@ private function checkRuleExists(\Magento\SalesRule\Model\Rule $model): bool
177177
}
178178
return true;
179179
}
180+
181+
/**
182+
* Update data related to Coupon
183+
*
184+
* @param array $data
185+
* @return array
186+
*/
187+
private function updateCouponData(array $data): array
188+
{
189+
if (isset($data['uses_per_coupon']) && $data['uses_per_coupon'] === '') {
190+
$data['uses_per_coupon'] = 0;
191+
}
192+
if (isset($data['uses_per_customer']) && $data['uses_per_customer'] === '') {
193+
$data['uses_per_customer'] = 0;
194+
}
195+
return $data;
196+
}
180197
}

app/code/Magento/SalesRule/Model/ResourceModel/Coupon.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
namespace Magento\SalesRule\Model\ResourceModel;
77

88
use Magento\Framework\Model\AbstractModel;
9+
use Magento\Framework\Model\ResourceModel\Db\Context;
10+
use Magento\Framework\Stdlib\DateTime;
11+
use Magento\Framework\Stdlib\DateTime\DateTime as Date;
912

1013
/**
1114
* SalesRule Resource Coupon
@@ -15,6 +18,33 @@
1518
class Coupon extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb implements
1619
\Magento\SalesRule\Model\Spi\CouponResourceInterface
1720
{
21+
/**
22+
* @var Date
23+
*/
24+
private $date;
25+
26+
/**
27+
* @var DateTime
28+
*/
29+
private $dateTime;
30+
31+
/**
32+
* @param Context $context
33+
* @param Date $date
34+
* @param DateTime $dateTime
35+
* @param string|null $connectionName
36+
*/
37+
public function __construct(
38+
Context $context,
39+
Date $date,
40+
DateTime $dateTime,
41+
$connectionName = null
42+
) {
43+
parent::__construct($context, $connectionName);
44+
$this->date = $date;
45+
$this->dateTime = $dateTime;
46+
}
47+
1848
/**
1949
* Constructor adds unique fields
2050
*
@@ -37,6 +67,9 @@ public function _beforeSave(AbstractModel $object)
3767
// maintain single primary coupon per rule
3868
$object->setIsPrimary($object->getIsPrimary() ? 1 : null);
3969

70+
$object->setUsageLimit($object->getUsageLimit() ?? 0);
71+
$object->setUsagePerCustomer($object->getUsagePerCustomer() ?? 0);
72+
$object->setCreatedAt($object->getCreatedAt() ?? $this->dateTime->formatDate($this->date->gmtTimestamp()));
4073
return parent::_beforeSave($object);
4174
}
4275

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\SalesRule\Model;
9+
10+
use Magento\Framework\Api\SearchCriteriaBuilder;
11+
use Magento\SalesRule\Api\CouponRepositoryInterface;
12+
use Magento\TestFramework\Helper\Bootstrap;
13+
14+
/**
15+
* @magentoDbIsolation enabled
16+
* @magentoAppIsolation enabled
17+
*/
18+
class CouponTest extends \PHPUnit\Framework\TestCase
19+
{
20+
/**
21+
* @var CouponRepositoryInterface
22+
*/
23+
private $couponRepository;
24+
25+
/**
26+
* @var RuleFactory
27+
*/
28+
private $ruleFactory;
29+
30+
public function setUp(): void
31+
{
32+
parent::setUp();
33+
/** @var CouponRepositoryInterface couponRepository */
34+
$this->couponRepository = Bootstrap::getObjectManager()->create(CouponRepositoryInterface::class);
35+
/** @var RuleFactory ruleFactory */
36+
$this->ruleFactory = Bootstrap::getObjectManager()->create(RuleFactory::class);
37+
}
38+
39+
/**
40+
* Check that non-autogenerated coupon contains necessary fields received from sales rule
41+
*/
42+
public function testNonAutogeneratedCouponBelongingToRule()
43+
{
44+
$couponCode = '_coupon__code_';
45+
$rule = $this->ruleFactory->create();
46+
$rule->setCouponType(2)
47+
->setUseAutoGeneration(0)
48+
->setCouponCode($couponCode)
49+
->setUsesPerCustomer(null)
50+
->setUsesPerCoupon(null)
51+
->save();
52+
53+
/** @var SearchCriteriaBuilder $criteriaBuilder */
54+
$criteriaBuilder = Bootstrap::getObjectManager()->create(SearchCriteriaBuilder::class);
55+
$couponSearchResult = $this->couponRepository->getList(
56+
$criteriaBuilder->addFilter('code', $couponCode, 'like')->create()
57+
);
58+
$coupons = $couponSearchResult->getItems();
59+
$coupon = array_pop($coupons);
60+
61+
$this->assertEquals(0, $coupon->getUsagePerCustomer());
62+
$this->assertEquals(0, $coupon->getUsageLimit());
63+
$this->assertEquals(0, $coupon->getTimesUsed());
64+
$this->assertEquals(0, $coupon->getType());
65+
$this->assertNotEmpty($coupon->getCreatedAt());
66+
}
67+
}

0 commit comments

Comments
 (0)