Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CouponManagementService.php
Go to the documentation of this file.
1 <?php
7 
14 {
18  protected $couponFactory;
19 
23  protected $ruleFactory;
24 
28  protected $collectionFactory;
29 
33  protected $couponGenerator;
34 
38  protected $resourceModel;
39 
44 
53  public function __construct(
54  \Magento\SalesRule\Model\CouponFactory $couponFactory,
55  \Magento\SalesRule\Model\RuleFactory $ruleFactory,
56  \Magento\SalesRule\Model\ResourceModel\Coupon\CollectionFactory $collectionFactory,
57  \Magento\SalesRule\Model\Coupon\Massgenerator $couponGenerator,
58  \Magento\SalesRule\Model\Spi\CouponResourceInterface $resourceModel,
59  \Magento\SalesRule\Api\Data\CouponMassDeleteResultInterfaceFactory $couponMassDeleteResultFactory
60  ) {
61  $this->couponFactory = $couponFactory;
62  $this->ruleFactory = $ruleFactory;
63  $this->collectionFactory = $collectionFactory;
64  $this->couponGenerator = $couponGenerator;
65  $this->resourceModel = $resourceModel;
66  $this->couponMassDeleteResultFactory = $couponMassDeleteResultFactory;
67  }
68 
77  public function generate(\Magento\SalesRule\Api\Data\CouponGenerationSpecInterface $couponSpec)
78  {
79  $data = $this->convertCouponSpec($couponSpec);
80  if (!$this->couponGenerator->validateData($data)) {
81  throw new \Magento\Framework\Exception\InputException();
82  }
83 
84  try {
85  $rule = $this->ruleFactory->create()->load($couponSpec->getRuleId());
86  if (!$rule->getRuleId()) {
87  throw \Magento\Framework\Exception\NoSuchEntityException::singleField(
88  \Magento\SalesRule\Model\Coupon::KEY_RULE_ID,
89  $couponSpec->getRuleId()
90  );
91  }
92  if (!$rule->getUseAutoGeneration()
94  ) {
95  throw new \Magento\Framework\Exception\LocalizedException(
96  __('Specified rule does not allow automatic coupon generation')
97  );
98  }
99 
100  $this->couponGenerator->setData($data);
101  $this->couponGenerator->setData('to_date', $rule->getToDate());
102  $this->couponGenerator->setData('uses_per_coupon', $rule->getUsesPerCoupon());
103  $this->couponGenerator->setData('usage_per_customer', $rule->getUsesPerCustomer());
104 
105  $this->couponGenerator->generatePool();
106  return $this->couponGenerator->getGeneratedCodes();
107  } catch (\Exception $e) {
108  throw new \Magento\Framework\Exception\LocalizedException(
109  __('Error occurred when generating coupons: %1', $e->getMessage())
110  );
111  }
112  }
113 
120  protected function convertCouponSpec(\Magento\SalesRule\Api\Data\CouponGenerationSpecInterface $couponSpec)
121  {
122  $data = [];
123  $data['rule_id'] = $couponSpec->getRuleId();
124  $data['qty'] = $couponSpec->getQuantity();
125  $data['format'] = $couponSpec->getFormat();
126  $data['length'] = $couponSpec->getLength();
127  $data['prefix'] = $couponSpec->getPrefix();
128  $data['suffix'] = $couponSpec->getSuffix();
129  $data['dash'] = $couponSpec->getDelimiterAtEvery();
130 
131  //ensure we have a format
132  if (empty($data['format'])) {
133  $data['format'] = $couponSpec::COUPON_FORMAT_ALPHANUMERIC;
134  }
135 
136  //if specified, use the supplied delimiter
137  if ($couponSpec->getDelimiter()) {
138  $data['delimiter'] = $couponSpec->getDelimiter();
139  }
140  return $data;
141  }
142 
151  public function deleteByIds(array $ids, $ignoreInvalidCoupons = true)
152  {
153  return $this->massDelete('coupon_id', $ids, $ignoreInvalidCoupons);
154  }
155 
164  public function deleteByCodes(array $codes, $ignoreInvalidCoupons = true)
165  {
166  return $this->massDelete('code', $codes, $ignoreInvalidCoupons);
167  }
168 
178  protected function massDelete($fieldName, array $fieldValues, $ignoreInvalid)
179  {
180  $couponsCollection = $this->collectionFactory->create()
181  ->addFieldToFilter(
182  $fieldName,
183  ['in' => $fieldValues]
184  );
185 
186  if (!$ignoreInvalid) {
187  if ($couponsCollection->getSize() != count($fieldValues)) {
188  throw new \Magento\Framework\Exception\LocalizedException(__('Some coupons are invalid.'));
189  }
190  }
191 
192  $results = $this->couponMassDeleteResultFactory->create();
193  $failedItems = [];
194  $fieldValues = array_flip($fieldValues);
196  foreach ($couponsCollection->getItems() as $coupon) {
197  $couponValue = ($fieldName == 'code') ? $coupon->getCode() : $coupon->getCouponId();
198  try {
199  $coupon->delete();
200  } catch (\Exception $e) {
201  $failedItems[] = $couponValue;
202  }
203  unset($fieldValues[$couponValue]);
204  }
205  $results->setFailedItems($failedItems);
206  $results->setMissingItems(array_flip($fieldValues));
207  return $results;
208  }
209 }
convertCouponSpec(\Magento\SalesRule\Api\Data\CouponGenerationSpecInterface $couponSpec)
__construct(\Magento\SalesRule\Model\CouponFactory $couponFactory, \Magento\SalesRule\Model\RuleFactory $ruleFactory, \Magento\SalesRule\Model\ResourceModel\Coupon\CollectionFactory $collectionFactory, \Magento\SalesRule\Model\Coupon\Massgenerator $couponGenerator, \Magento\SalesRule\Model\Spi\CouponResourceInterface $resourceModel, \Magento\SalesRule\Api\Data\CouponMassDeleteResultInterfaceFactory $couponMassDeleteResultFactory)
$results
Definition: popup.phtml:13
$coupon
__()
Definition: __.php:13
deleteByCodes(array $codes, $ignoreInvalidCoupons=true)
generate(\Magento\SalesRule\Api\Data\CouponGenerationSpecInterface $couponSpec)