2019-12-21 05:25:32 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Webkul\CartRule\Repositories;
|
|
|
|
|
|
|
|
|
|
use Webkul\Core\Eloquent\Repository;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* CartRuleCoupon Reposotory
|
|
|
|
|
*
|
|
|
|
|
* @author Jitendra Singh <jitendra@webkul.com>
|
|
|
|
|
* @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com)
|
|
|
|
|
*/
|
|
|
|
|
class CartRuleCouponRepository extends Repository
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
|
|
|
|
protected $charsets = [
|
|
|
|
|
'alphanumeric' => 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789',
|
|
|
|
|
'alphabetical' => 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
|
2020-03-04 06:32:53 +00:00
|
|
|
'numeric' => '0123456789',
|
2019-12-21 05:25:32 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Specify Model class name
|
|
|
|
|
*
|
|
|
|
|
* @return mixed
|
|
|
|
|
*/
|
|
|
|
|
function model()
|
|
|
|
|
{
|
|
|
|
|
return 'Webkul\CartRule\Contracts\CartRuleCoupon';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates coupons for cart rule
|
|
|
|
|
*
|
2020-03-05 05:34:57 +00:00
|
|
|
* @param array $data
|
|
|
|
|
* @param int $cartRuleId
|
2019-12-21 05:25:32 +00:00
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function generateCoupons($data, $cartRuleId)
|
|
|
|
|
{
|
|
|
|
|
$cartRule = app('Webkul\CartRule\Repositories\CartRuleRepository')->findOrFail($cartRuleId);
|
|
|
|
|
|
|
|
|
|
for ($i = 0; $i < $data['coupon_qty']; $i++) {
|
|
|
|
|
parent::create([
|
2020-02-27 08:03:03 +00:00
|
|
|
'cart_rule_id' => $cartRuleId,
|
|
|
|
|
'code' => $data['code_prefix'] . $this->getRandomString($data['code_format'], $data['code_length']) . $data['code_suffix'],
|
|
|
|
|
'usage_limit' => $cartRule->uses_per_coupon ?? 0,
|
|
|
|
|
'usage_per_customer' => $cartRule->usage_per_customer ?? 0,
|
|
|
|
|
'is_primary' => 0,
|
2020-03-04 06:32:53 +00:00
|
|
|
'expired_at' => $cartRule->ends_till ?: null,
|
2020-02-27 08:03:03 +00:00
|
|
|
]);
|
2019-12-21 05:25:32 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates coupons for cart rule
|
|
|
|
|
*
|
2020-03-05 05:34:57 +00:00
|
|
|
* @param string $format
|
|
|
|
|
* @param int $length
|
2019-12-21 05:25:32 +00:00
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function getRandomString($format, $length)
|
|
|
|
|
{
|
|
|
|
|
$couponCode = '';
|
|
|
|
|
|
|
|
|
|
for ($i = 0; $i < $length; $i++) {
|
|
|
|
|
$couponCode .= $this->charsets[$format][rand(0, strlen($this->charsets[$format]) - 1)];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $couponCode;
|
|
|
|
|
}
|
|
|
|
|
}
|