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