1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Gabievi\Promocodes\Models; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @method static byCode(string $code) |
10
|
|
|
* @method static pluck(string $string) |
11
|
|
|
* @method static insert(array $records) |
12
|
|
|
*/ |
13
|
|
|
class Promocode extends Model |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Indicates if the model should be timestamped. |
17
|
|
|
* |
18
|
|
|
* @var bool |
19
|
|
|
*/ |
20
|
|
|
public $timestamps = false; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The attributes that are mass assignable. |
24
|
|
|
* |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
protected $fillable = ['code', 'reward', 'is_disposable', 'expires_at', 'amount_codes']; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The attributes that should be cast to native types. |
31
|
|
|
* |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
protected $casts = [ |
35
|
|
|
'is_disposable' => 'boolean', |
36
|
|
|
'data' => 'array', |
37
|
|
|
]; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* The attributes that should be mutated to dates. |
41
|
|
|
* |
42
|
|
|
* @var array |
43
|
|
|
*/ |
44
|
|
|
protected $dates = ['expires_at']; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Promocode constructor. |
48
|
|
|
* |
49
|
|
|
* @param array $attributes |
50
|
|
|
*/ |
51
|
|
|
public function __construct(array $attributes = []) |
52
|
|
|
{ |
53
|
|
|
parent::__construct($attributes); |
54
|
|
|
|
55
|
|
|
$this->table = config('promocodes.table', 'promocodes'); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Get the users who is related promocode. |
60
|
|
|
* |
61
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
62
|
|
|
*/ |
63
|
|
|
public function users() |
64
|
|
|
{ |
65
|
|
|
return $this->belongsToMany(config('promocodes.user_model'), config('promocodes.relation_table'), |
66
|
|
|
config('promocodes.foreign_pivot_key', 'user_id'), config('promocodes.related_pivot_key', 'user_id')) |
67
|
|
|
->withPivot('used_at'); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Query builder to find promocode using code. |
72
|
|
|
* |
73
|
|
|
* @param $query |
74
|
|
|
* @param $code |
75
|
|
|
* |
76
|
|
|
* @return mixed |
77
|
|
|
*/ |
78
|
|
|
public function scopeByCode($query, $code) |
79
|
|
|
{ |
80
|
|
|
return $query->where('code', $code); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Query builder to get disposable codes. |
85
|
|
|
* |
86
|
|
|
* @param $query |
87
|
|
|
* @return mixed |
88
|
|
|
*/ |
89
|
|
|
public function scopeIsDisposable($query) |
90
|
|
|
{ |
91
|
|
|
return $query->where('is_disposable', true); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Query builder to get non-disposable codes. |
96
|
|
|
* |
97
|
|
|
* @param $query |
98
|
|
|
* @return mixed |
99
|
|
|
*/ |
100
|
|
|
public function scopeIsNotDisposable($query) |
101
|
|
|
{ |
102
|
|
|
return $query->where('is_disposable', false); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Query builder to get expired promotion codes. |
107
|
|
|
* |
108
|
|
|
* @param $query |
109
|
|
|
* @return mixed |
110
|
|
|
*/ |
111
|
|
|
public function scopeExpired($query) |
112
|
|
|
{ |
113
|
|
|
return $query->whereNotNull('expires_at')->whereDate('expires_at', '<=', Carbon::now()); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Check if code is disposable (ont-time). |
118
|
|
|
* |
119
|
|
|
* @return bool |
120
|
|
|
*/ |
121
|
|
|
public function isDisposable() |
122
|
|
|
{ |
123
|
|
|
return $this->is_disposable; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Check if code is expired. |
128
|
|
|
* |
129
|
|
|
* @return bool |
130
|
|
|
*/ |
131
|
|
|
public function isExpired() |
132
|
|
|
{ |
133
|
|
|
return $this->expires_at ? Carbon::now()->gte($this->expires_at) : false; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Check if code amount is over. |
138
|
|
|
* |
139
|
|
|
* @return bool |
140
|
|
|
*/ |
141
|
|
|
public function isOverAmount() |
142
|
|
|
{ |
143
|
|
|
if (is_null($this->amount_codes)) { |
144
|
|
|
return false; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
return $this->amount_codes <= 0 ? true : false; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|