14 \Magento\SalesRule\Model\Coupon\CodegeneratorInterface
111 $charset = $this->salesRuleCoupon->getCharset(
$format);
114 $charsetSize = count($charset);
115 $split = max(0, (
int)$this->getDash());
116 $length = max(1, (
int)$this->getLength());
117 for (
$i = 0;
$i < $length; ++
$i) {
119 if (($split > 0) && ((
$i % $split) === 0) && (
$i !== 0)) {
120 $char = $splitChar . $char;
125 return $this->getPrefix() .
$code . $this->getSuffix();
135 if ($this->
hasData(
'delimiter')) {
136 return $this->
getData(
'delimiter');
138 return $this->salesRuleCoupon->getCodeSeparator();
148 public function generatePool()
150 $this->generatedCount = 0;
151 $this->generatedCodes = [];
152 $size = $this->getQty();
156 $coupon = $this->couponFactory->create();
157 $nowTimestamp = $this->dateTime->formatDate($this->date->gmtTimestamp());
159 for (
$i = 0;
$i < $size;
$i++) {
162 if ($attempt >= $maxAttempts) {
163 throw new \Magento\Framework\Exception\LocalizedException(
164 __(
'We cannot create the requested Coupon Qty. Please check your settings and try again.')
171 $expirationDate = $this->getToDate();
172 if ($expirationDate instanceof \DateTimeInterface) {
173 $expirationDate = $expirationDate->format(
'Y-m-d H:i:s');
178 ->setUsageLimit($this->getUsesPerCoupon())
180 ->setExpirationDate($expirationDate)
181 ->setCreatedAt($nowTimestamp)
182 ->setType(\
Magento\SalesRule\Helper\Coupon::COUPON_TYPE_SPECIFIC_AUTOGENERATED)
186 $this->generatedCount += 1;
187 $this->generatedCodes[] =
$code;
201 $chars = count($this->salesRuleCoupon->getCharset($this->getFormat()));
202 $size = $this->getQty();
203 $length = (int)$this->getLength();
204 $maxCodes = pow($chars, $length);
205 $probability = $size / $maxCodes;
207 if ($probability > $maxProbability) {
210 $maxCodes = pow($chars, $length);
211 $probability = $size / $maxCodes;
212 }
while ($probability > $maxProbability);
213 $this->setLength($length);
226 && !empty(
$data[
'qty'])
227 && !empty(
$data[
'rule_id'])
228 && !empty(
$data[
'length'])
229 && !empty(
$data[
'format'])
230 && (int)
$data[
'qty'] > 0
231 && (
int)
$data[
'rule_id'] > 0
232 && (int)
$data[
'length'] > 0;
static getRandomNumber($min=0, $max=null)
getData($key='', $index=null)
const COUPON_FORMAT_ALPHANUMERIC
const MAX_PROBABILITY_OF_GUESSING
const MAX_GENERATE_ATTEMPTS
__construct(\Magento\Framework\Model\Context $context, \Magento\Framework\Registry $registry, \Magento\SalesRule\Helper\Coupon $salesRuleCoupon, \Magento\SalesRule\Model\CouponFactory $couponFactory, \Magento\Framework\Stdlib\DateTime\DateTime $date, \Magento\Framework\Stdlib\DateTime $dateTime, \Magento\Framework\Model\ResourceModel\AbstractResource $resource=null, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection=null, array $data=[])