CouponManagementService.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\SalesRule\Model\Service;
  7. /**
  8. * Coupon management service class
  9. *
  10. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  11. */
  12. class CouponManagementService implements \Magento\SalesRule\Api\CouponManagementInterface
  13. {
  14. /**
  15. * @var \Magento\SalesRule\Model\CouponFactory
  16. */
  17. protected $couponFactory;
  18. /**
  19. * @var \Magento\SalesRule\Model\RuleFactory
  20. */
  21. protected $ruleFactory;
  22. /**
  23. * @var \Magento\SalesRule\Model\ResourceModel\Coupon\CollectionFactory
  24. */
  25. protected $collectionFactory;
  26. /**
  27. * @var \Magento\SalesRule\Model\Coupon\Massgenerator
  28. */
  29. protected $couponGenerator;
  30. /**
  31. * @var \Magento\SalesRule\Model\Spi\CouponResourceInterface
  32. */
  33. protected $resourceModel;
  34. /**
  35. * var \Magento\SalesRule\Api\Data\CouponMassDeleteResultInterfaceFactory
  36. */
  37. protected $couponMassDeleteResultFactory;
  38. /**
  39. * @param \Magento\SalesRule\Model\CouponFactory $couponFactory
  40. * @param \Magento\SalesRule\Model\RuleFactory $ruleFactory
  41. * @param \Magento\SalesRule\Model\ResourceModel\Coupon\CollectionFactory $collectionFactory
  42. * @param \Magento\SalesRule\Model\Coupon\Massgenerator $couponGenerator
  43. * @param \Magento\SalesRule\Model\Spi\CouponResourceInterface $resourceModel
  44. * @param \Magento\SalesRule\Api\Data\CouponMassDeleteResultInterfaceFactory $couponMassDeleteResultFactory
  45. */
  46. public function __construct(
  47. \Magento\SalesRule\Model\CouponFactory $couponFactory,
  48. \Magento\SalesRule\Model\RuleFactory $ruleFactory,
  49. \Magento\SalesRule\Model\ResourceModel\Coupon\CollectionFactory $collectionFactory,
  50. \Magento\SalesRule\Model\Coupon\Massgenerator $couponGenerator,
  51. \Magento\SalesRule\Model\Spi\CouponResourceInterface $resourceModel,
  52. \Magento\SalesRule\Api\Data\CouponMassDeleteResultInterfaceFactory $couponMassDeleteResultFactory
  53. ) {
  54. $this->couponFactory = $couponFactory;
  55. $this->ruleFactory = $ruleFactory;
  56. $this->collectionFactory = $collectionFactory;
  57. $this->couponGenerator = $couponGenerator;
  58. $this->resourceModel = $resourceModel;
  59. $this->couponMassDeleteResultFactory = $couponMassDeleteResultFactory;
  60. }
  61. /**
  62. * Generate coupon for a rule
  63. *
  64. * @param \Magento\SalesRule\Api\Data\CouponGenerationSpecInterface $couponSpec
  65. * @return string[]
  66. * @throws \Magento\Framework\Exception\InputException
  67. * @throws \Magento\Framework\Exception\LocalizedException
  68. */
  69. public function generate(\Magento\SalesRule\Api\Data\CouponGenerationSpecInterface $couponSpec)
  70. {
  71. $data = $this->convertCouponSpec($couponSpec);
  72. if (!$this->couponGenerator->validateData($data)) {
  73. throw new \Magento\Framework\Exception\InputException();
  74. }
  75. try {
  76. $rule = $this->ruleFactory->create()->load($couponSpec->getRuleId());
  77. if (!$rule->getRuleId()) {
  78. throw \Magento\Framework\Exception\NoSuchEntityException::singleField(
  79. \Magento\SalesRule\Model\Coupon::KEY_RULE_ID,
  80. $couponSpec->getRuleId()
  81. );
  82. }
  83. if (!$rule->getUseAutoGeneration()
  84. && $rule->getCouponType() != \Magento\SalesRule\Model\Rule::COUPON_TYPE_AUTO
  85. ) {
  86. throw new \Magento\Framework\Exception\LocalizedException(
  87. __('Specified rule does not allow automatic coupon generation')
  88. );
  89. }
  90. $this->couponGenerator->setData($data);
  91. $this->couponGenerator->setData('to_date', $rule->getToDate());
  92. $this->couponGenerator->setData('uses_per_coupon', $rule->getUsesPerCoupon());
  93. $this->couponGenerator->setData('usage_per_customer', $rule->getUsesPerCustomer());
  94. $this->couponGenerator->generatePool();
  95. return $this->couponGenerator->getGeneratedCodes();
  96. } catch (\Exception $e) {
  97. throw new \Magento\Framework\Exception\LocalizedException(
  98. __('Error occurred when generating coupons: %1', $e->getMessage())
  99. );
  100. }
  101. }
  102. /**
  103. * Convert CouponGenerationSpecInterface to data array expected by Massgenerator
  104. *
  105. * @param \Magento\SalesRule\Api\Data\CouponGenerationSpecInterface $couponSpec
  106. * @return array
  107. */
  108. protected function convertCouponSpec(\Magento\SalesRule\Api\Data\CouponGenerationSpecInterface $couponSpec)
  109. {
  110. $data = [];
  111. $data['rule_id'] = $couponSpec->getRuleId();
  112. $data['qty'] = $couponSpec->getQuantity();
  113. $data['format'] = $couponSpec->getFormat();
  114. $data['length'] = $couponSpec->getLength();
  115. $data['prefix'] = $couponSpec->getPrefix();
  116. $data['suffix'] = $couponSpec->getSuffix();
  117. $data['dash'] = $couponSpec->getDelimiterAtEvery();
  118. //ensure we have a format
  119. if (empty($data['format'])) {
  120. $data['format'] = $couponSpec::COUPON_FORMAT_ALPHANUMERIC;
  121. }
  122. //if specified, use the supplied delimiter
  123. if ($couponSpec->getDelimiter()) {
  124. $data['delimiter'] = $couponSpec->getDelimiter();
  125. }
  126. return $data;
  127. }
  128. /**
  129. * Delete coupon by coupon ids.
  130. *
  131. * @param int[] $ids
  132. * @param bool $ignoreInvalidCoupons
  133. * @return \Magento\SalesRule\Api\Data\CouponMassDeleteResultInterface
  134. * @throws \Magento\Framework\Exception\LocalizedException
  135. */
  136. public function deleteByIds(array $ids, $ignoreInvalidCoupons = true)
  137. {
  138. return $this->massDelete('coupon_id', $ids, $ignoreInvalidCoupons);
  139. }
  140. /**
  141. * Delete coupon by coupon codes.
  142. *
  143. * @param string[] codes
  144. * @param bool $ignoreInvalidCoupons
  145. * @return \Magento\SalesRule\Api\Data\CouponMassDeleteResultInterface
  146. * @throws \Magento\Framework\Exception\LocalizedException
  147. */
  148. public function deleteByCodes(array $codes, $ignoreInvalidCoupons = true)
  149. {
  150. return $this->massDelete('code', $codes, $ignoreInvalidCoupons);
  151. }
  152. /**
  153. * Delete coupons by filter
  154. *
  155. * @param string $fieldName
  156. * @param string[] fieldValues
  157. * @param bool $ignoreInvalid
  158. * @return \Magento\SalesRule\Api\Data\CouponMassDeleteResultInterface
  159. * @throws \Magento\Framework\Exception\LocalizedException
  160. */
  161. protected function massDelete($fieldName, array $fieldValues, $ignoreInvalid)
  162. {
  163. $couponsCollection = $this->collectionFactory->create()
  164. ->addFieldToFilter(
  165. $fieldName,
  166. ['in' => $fieldValues]
  167. );
  168. if (!$ignoreInvalid) {
  169. if ($couponsCollection->getSize() != count($fieldValues)) {
  170. throw new \Magento\Framework\Exception\LocalizedException(__('Some coupons are invalid.'));
  171. }
  172. }
  173. $results = $this->couponMassDeleteResultFactory->create();
  174. $failedItems = [];
  175. $fieldValues = array_flip($fieldValues);
  176. /** @var \Magento\SalesRule\Model\Coupon $coupon */
  177. foreach ($couponsCollection->getItems() as $coupon) {
  178. $couponValue = ($fieldName == 'code') ? $coupon->getCode() : $coupon->getCouponId();
  179. try {
  180. $coupon->delete();
  181. } catch (\Exception $e) {
  182. $failedItems[] = $couponValue;
  183. }
  184. unset($fieldValues[$couponValue]);
  185. }
  186. $results->setFailedItems($failedItems);
  187. $results->setMissingItems(array_flip($fieldValues));
  188. return $results;
  189. }
  190. }