1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Rinvex\Subscriptions\Models; |
6
|
|
|
|
7
|
|
|
use Spatie\Sluggable\SlugOptions; |
8
|
|
|
use Rinvex\Support\Traits\HasSlug; |
9
|
|
|
use Spatie\EloquentSortable\Sortable; |
10
|
|
|
use Illuminate\Database\Eloquent\Model; |
11
|
|
|
use Rinvex\Cacheable\CacheableEloquent; |
12
|
|
|
use Illuminate\Database\Eloquent\Builder; |
13
|
|
|
use Rinvex\Support\Traits\HasTranslations; |
14
|
|
|
use Rinvex\Support\Traits\ValidatingTrait; |
15
|
|
|
use Spatie\EloquentSortable\SortableTrait; |
16
|
|
|
use Rinvex\Subscriptions\Contracts\PlanContract; |
17
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Rinvex\Subscriptions\Models\Plan. |
21
|
|
|
* |
22
|
|
|
* @property int $id |
23
|
|
|
* @property string $slug |
24
|
|
|
* @property array $name |
25
|
|
|
* @property array $description |
26
|
|
|
* @property bool $is_active |
27
|
|
|
* @property float $price |
28
|
|
|
* @property float $signup_fee |
29
|
|
|
* @property string $currency |
30
|
|
|
* @property int $trial_period |
31
|
|
|
* @property string $trial_interval |
|
|
|
|
32
|
|
|
* @property int $invoice_period |
|
|
|
|
33
|
|
|
* @property string $invoice_interval |
|
|
|
|
34
|
|
|
* @property int $grace_period |
35
|
|
|
* @property string $grace_interval |
|
|
|
|
36
|
|
|
* @property int $prorate_day |
37
|
|
|
* @property int $prorate_period |
|
|
|
|
38
|
|
|
* @property int $prorate_extend_due |
|
|
|
|
39
|
|
|
* @property int $active_subscribers_limit |
|
|
|
|
40
|
|
|
* @property int $sort_order |
41
|
|
|
* @property \Carbon\Carbon $created_at |
42
|
|
|
* @property \Carbon\Carbon $updated_at |
43
|
|
|
* @property \Carbon\Carbon $deleted_at |
44
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection|\Rinvex\Subscriptions\Models\PlanFeature[] $features |
45
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection|\Rinvex\Subscriptions\Models\PlanSubscription[] $subscriptions |
|
|
|
|
46
|
|
|
* |
47
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\Plan active() |
48
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\Plan inactive() |
49
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\Plan ordered($direction = 'asc') |
50
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\Plan whereActiveSubscribersLimit($value) |
|
|
|
|
51
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\Plan whereCreatedAt($value) |
52
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\Plan whereCurrency($value) |
53
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\Plan whereDeletedAt($value) |
54
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\Plan whereDescription($value) |
55
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\Plan whereGraceInterval($value) |
56
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\Plan whereGracePeriod($value) |
57
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\Plan whereId($value) |
58
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\Plan whereInvoiceInterval($value) |
59
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\Plan whereInvoicePeriod($value) |
60
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\Plan whereIsActive($value) |
61
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\Plan whereName($value) |
62
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\Plan wherePrice($value) |
63
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\Plan whereProrateDay($value) |
64
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\Plan whereProrateExtendDue($value) |
65
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\Plan whereProratePeriod($value) |
66
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\Plan whereSignupFee($value) |
67
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\Plan whereSlug($value) |
68
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\Plan whereSortOrder($value) |
69
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\Plan whereTrialInterval($value) |
70
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\Plan whereTrialPeriod($value) |
71
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\Plan whereUpdatedAt($value) |
72
|
|
|
* @mixin \Eloquent |
73
|
|
|
*/ |
74
|
|
|
class Plan extends Model implements PlanContract, Sortable |
75
|
|
|
{ |
76
|
|
|
use HasSlug; |
77
|
|
|
use SortableTrait; |
78
|
|
|
use HasTranslations; |
79
|
|
|
use ValidatingTrait; |
80
|
|
|
use CacheableEloquent; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* {@inheritdoc} |
84
|
|
|
*/ |
85
|
|
|
protected $fillable = [ |
86
|
|
|
'slug', |
87
|
|
|
'name', |
88
|
|
|
'description', |
89
|
|
|
'is_active', |
90
|
|
|
'price', |
91
|
|
|
'signup_fee', |
92
|
|
|
'currency', |
93
|
|
|
'trial_period', |
94
|
|
|
'trial_interval', |
95
|
|
|
'invoice_period', |
96
|
|
|
'invoice_interval', |
97
|
|
|
'grace_period', |
98
|
|
|
'grace_interval', |
99
|
|
|
'prorate_day', |
100
|
|
|
'prorate_period', |
101
|
|
|
'prorate_extend_due', |
102
|
|
|
'active_subscribers_limit', |
103
|
|
|
'sort_order', |
104
|
|
|
]; |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* {@inheritdoc} |
108
|
|
|
*/ |
109
|
|
|
protected $casts = [ |
110
|
|
|
'slug' => 'string', |
111
|
|
|
'is_active' => 'boolean', |
112
|
|
|
'price' => 'float', |
113
|
|
|
'signup_fee' => 'float', |
114
|
|
|
'currency' => 'string', |
115
|
|
|
'trial_period' => 'integer', |
116
|
|
|
'trial_interval' => 'string', |
117
|
|
|
'invoice_period' => 'integer', |
118
|
|
|
'invoice_interval' => 'string', |
119
|
|
|
'grace_period' => 'integer', |
120
|
|
|
'grace_interval' => 'string', |
121
|
|
|
'prorate_day' => 'integer', |
122
|
|
|
'prorate_period' => 'integer', |
123
|
|
|
'prorate_extend_due' => 'integer', |
124
|
|
|
'active_subscribers_limit' => 'integer', |
125
|
|
|
'sort_order' => 'integer', |
126
|
|
|
'deleted_at' => 'datetime', |
127
|
|
|
]; |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* {@inheritdoc} |
131
|
|
|
*/ |
132
|
|
|
protected $observables = [ |
133
|
|
|
'validating', |
134
|
|
|
'validated', |
135
|
|
|
]; |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* The attributes that are translatable. |
139
|
|
|
* |
140
|
|
|
* @var array |
141
|
|
|
*/ |
142
|
|
|
public $translatable = [ |
143
|
|
|
'name', |
144
|
|
|
'description', |
145
|
|
|
]; |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* The sortable settings. |
149
|
|
|
* |
150
|
|
|
* @var array |
151
|
|
|
*/ |
152
|
|
|
public $sortable = [ |
153
|
|
|
'order_column_name' => 'sort_order', |
154
|
|
|
]; |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* The default rules that the model will validate against. |
158
|
|
|
* |
159
|
|
|
* @var array |
160
|
|
|
*/ |
161
|
|
|
protected $rules = []; |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Whether the model should throw a |
165
|
|
|
* ValidationException if it fails validation. |
166
|
|
|
* |
167
|
|
|
* @var bool |
168
|
|
|
*/ |
169
|
|
|
protected $throwValidationExceptions = true; |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Create a new Eloquent model instance. |
173
|
|
|
* |
174
|
|
|
* @param array $attributes |
175
|
|
|
*/ |
176
|
|
|
public function __construct(array $attributes = []) |
177
|
|
|
{ |
178
|
|
|
parent::__construct($attributes); |
179
|
|
|
|
180
|
|
|
$this->setTable(config('rinvex.subscriptions.tables.plans')); |
181
|
|
|
$this->setRules([ |
182
|
|
|
'slug' => 'required|alpha_dash|max:150|unique:'.config('rinvex.subscriptions.tables.plans').',slug', |
183
|
|
|
'name' => 'required|string|max:150', |
184
|
|
|
'description' => 'nullable|string|max:10000', |
185
|
|
|
'is_active' => 'sometimes|boolean', |
186
|
|
|
'price' => 'required|numeric', |
187
|
|
|
'signup_fee' => 'required|numeric', |
188
|
|
|
'currency' => 'required|alpha|size:3', |
189
|
|
|
'trial_period' => 'sometimes|integer|max:10000', |
190
|
|
|
'trial_interval' => 'sometimes|string|in:h,d,w,m', |
191
|
|
|
'invoice_period' => 'sometimes|integer|max:10000', |
192
|
|
|
'invoice_interval' => 'sometimes|string|in:h,d,w,m', |
193
|
|
|
'grace_period' => 'sometimes|integer|max:10000', |
194
|
|
|
'grace_interval' => 'sometimes|string|in:h,d,w,m', |
195
|
|
|
'sort_order' => 'nullable|integer|max:10000000', |
196
|
|
|
'prorate_day' => 'nullable|integer|max:150', |
197
|
|
|
'prorate_period' => 'nullable|integer|max:150', |
198
|
|
|
'prorate_extend_due' => 'nullable|integer|max:150', |
199
|
|
|
'active_subscribers_limit' => 'nullable|integer|max:10000', |
200
|
|
|
]); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Get the active plans. |
205
|
|
|
* |
206
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
207
|
|
|
* |
208
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
209
|
|
|
*/ |
210
|
|
|
public function scopeActive(Builder $builder): Builder |
211
|
|
|
{ |
212
|
|
|
return $builder->where('is_active', true); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Get the inactive plans. |
217
|
|
|
* |
218
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
219
|
|
|
* |
220
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
221
|
|
|
*/ |
222
|
|
|
public function scopeInactive(Builder $builder): Builder |
223
|
|
|
{ |
224
|
|
|
return $builder->where('is_active', false); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Get the options for generating the slug. |
229
|
|
|
* |
230
|
|
|
* @return \Spatie\Sluggable\SlugOptions |
231
|
|
|
*/ |
232
|
|
|
public function getSlugOptions(): SlugOptions |
233
|
|
|
{ |
234
|
|
|
return SlugOptions::create() |
235
|
|
|
->doNotGenerateSlugsOnUpdate() |
236
|
|
|
->generateSlugsFrom('name') |
237
|
|
|
->saveSlugsTo('slug'); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* The plan may have many features. |
242
|
|
|
* |
243
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
244
|
|
|
*/ |
245
|
|
|
public function features(): HasMany |
246
|
|
|
{ |
247
|
|
|
return $this->hasMany(config('rinvex.subscriptions.models.plan_feature'), 'plan_id', 'id'); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* The plan may have many subscriptions. |
252
|
|
|
* |
253
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
254
|
|
|
*/ |
255
|
|
|
public function subscriptions(): HasMany |
256
|
|
|
{ |
257
|
|
|
return $this->hasMany(config('rinvex.subscriptions.models.plan_subscription'), 'plan_id', 'id'); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Check if plan is free. |
262
|
|
|
* |
263
|
|
|
* @return bool |
264
|
|
|
*/ |
265
|
|
|
public function isFree(): bool |
266
|
|
|
{ |
267
|
|
|
return (float) $this->price <= 0.00; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* Check if plan has trial. |
272
|
|
|
* |
273
|
|
|
* @return bool |
274
|
|
|
*/ |
275
|
|
|
public function hasTrial(): bool |
276
|
|
|
{ |
277
|
|
|
return $this->trial_period && $this->trial_interval; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* Check if plan has grace. |
282
|
|
|
* |
283
|
|
|
* @return bool |
284
|
|
|
*/ |
285
|
|
|
public function hasGrace(): bool |
286
|
|
|
{ |
287
|
|
|
return $this->grace_period && $this->grace_interval; |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* Get plan feature by the given slug. |
292
|
|
|
* |
293
|
|
|
* @param string $featureSlug |
294
|
|
|
* |
295
|
|
|
* @return \Rinvex\Subscriptions\Models\PlanFeature|null |
296
|
|
|
*/ |
297
|
|
|
public function getFeatureBySlug(string $featureSlug) |
298
|
|
|
{ |
299
|
|
|
return $this->features()->where('slug', $featureSlug)->first(); |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* Activate the plan. |
304
|
|
|
* |
305
|
|
|
* @return $this |
306
|
|
|
*/ |
307
|
|
|
public function activate() |
308
|
|
|
{ |
309
|
|
|
$this->update(['is_active' => true]); |
310
|
|
|
|
311
|
|
|
return $this; |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* Deactivate the plan. |
316
|
|
|
* |
317
|
|
|
* @return $this |
318
|
|
|
*/ |
319
|
|
|
public function deactivate() |
320
|
|
|
{ |
321
|
|
|
$this->update(['is_active' => false]); |
322
|
|
|
|
323
|
|
|
return $this; |
324
|
|
|
} |
325
|
|
|
} |
326
|
|
|
|
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.