2023-05-17 16:17:51 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2023-05-19 14:01:10 +00:00
|
|
|
use Spatie\Activitylog\LogOptions;
|
|
|
|
use Spatie\Activitylog\Traits\LogsActivity;
|
2023-05-17 16:17:51 +00:00
|
|
|
use Carbon\Carbon;
|
|
|
|
|
|
|
|
class Coupon extends Model
|
|
|
|
{
|
2023-05-19 14:01:10 +00:00
|
|
|
use HasFactory, LogsActivity;
|
|
|
|
|
|
|
|
public function getActivitylogOptions(): LogOptions
|
|
|
|
{
|
|
|
|
return LogOptions::defaults()
|
|
|
|
->logOnlyDirty()
|
|
|
|
->logOnly(['*'])
|
|
|
|
->dontSubmitEmptyLogs();
|
|
|
|
}
|
2023-05-17 16:17:51 +00:00
|
|
|
|
2023-05-18 14:33:42 +00:00
|
|
|
/**
|
|
|
|
* @var string[]
|
|
|
|
*/
|
2023-05-17 16:17:51 +00:00
|
|
|
protected $fillable = [
|
|
|
|
'code',
|
|
|
|
'type',
|
|
|
|
'value',
|
|
|
|
'uses',
|
|
|
|
'max_uses',
|
|
|
|
'expires_at'
|
|
|
|
];
|
|
|
|
|
2023-05-18 14:33:42 +00:00
|
|
|
/**
|
|
|
|
* @var string[]
|
|
|
|
*/
|
|
|
|
protected $casts = [
|
|
|
|
'value' => 'float',
|
|
|
|
'uses' => 'integer',
|
|
|
|
'max_uses' => 'integer',
|
|
|
|
'expires_at' => 'timestamp'
|
|
|
|
];
|
|
|
|
|
2023-05-17 16:17:51 +00:00
|
|
|
/**
|
|
|
|
* Returns the date format used by the coupons.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function formatDate(): string
|
|
|
|
{
|
|
|
|
return 'Y-MM-DD HH:mm:ss';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the current state of the coupon.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getStatus()
|
|
|
|
{
|
|
|
|
if ($this->uses >= $this->max_uses) {
|
|
|
|
return 'USES_LIMIT_REACHED';
|
|
|
|
}
|
|
|
|
|
2023-05-18 01:13:10 +00:00
|
|
|
if (!is_null($this->expires_at)) {
|
2023-05-18 19:52:26 +00:00
|
|
|
if ($this->expires_at <= Carbon::now(config('app.timezone'))->timestamp) {
|
2023-05-17 16:17:51 +00:00
|
|
|
return __('EXPIRED');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return __('VALID');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if a user has already exceeded the uses of a coupon.
|
|
|
|
*
|
|
|
|
* @param Request $request The request being made.
|
|
|
|
* @param CouponSettings $coupon_settings The instance of the coupon settings.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2023-05-18 01:13:10 +00:00
|
|
|
public function isLimitsUsesReached($requestUser, $coupon_settings): bool
|
2023-05-17 16:17:51 +00:00
|
|
|
{
|
2023-05-18 01:13:10 +00:00
|
|
|
$coupon_uses = $requestUser->coupons()->where('id', $this->id)->count();
|
2023-05-17 16:17:51 +00:00
|
|
|
|
|
|
|
return $coupon_uses >= $coupon_settings->max_uses_per_user ? true : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate a specified quantity of coupon codes.
|
|
|
|
*
|
|
|
|
* @param int $amount Amount of coupons to be generated.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function generateRandomCoupon(int $amount = 10): array
|
|
|
|
{
|
|
|
|
$coupons = [];
|
|
|
|
|
|
|
|
for ($i = 0; $i < $amount; $i++) {
|
|
|
|
$random_coupon = strtoupper(bin2hex(random_bytes(3)));
|
|
|
|
|
|
|
|
$coupons[] = $random_coupon;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $coupons;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Increments the use of a coupon.
|
|
|
|
*
|
|
|
|
* @param string $code Coupon Code.
|
|
|
|
* @param int $amount Amount to increment.
|
|
|
|
*
|
2023-05-19 14:01:10 +00:00
|
|
|
* @return bool
|
2023-05-17 16:17:51 +00:00
|
|
|
*/
|
2023-05-19 14:01:10 +00:00
|
|
|
public function incrementUses(string $code, int $amount = 1): bool
|
2023-05-17 16:17:51 +00:00
|
|
|
{
|
|
|
|
$this->where('code', $code)->increment('uses', $amount);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return BelongsToMany
|
|
|
|
*/
|
|
|
|
public function users()
|
|
|
|
{
|
|
|
|
return $this->belongsToMany(User::class, 'user_coupons');
|
|
|
|
}
|
|
|
|
}
|