|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Canvas\Models; |
|
4
|
|
|
|
|
5
|
|
|
use Phalcon\Cashier\Subscription as PhalconSubscription; |
|
|
|
|
|
|
6
|
|
|
use Canvas\Http\Exception\InternalServerErrorException; |
|
7
|
|
|
use Phalcon\Di; |
|
8
|
|
|
use Carbon\Carbon; |
|
|
|
|
|
|
9
|
|
|
use Phalcon\Db\RawValue; |
|
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Trait Subscription. |
|
13
|
|
|
* |
|
14
|
|
|
* @package Canvas\Models |
|
15
|
|
|
* |
|
16
|
|
|
* @property Users $user |
|
17
|
|
|
* @property AppsPlans $appPlan |
|
18
|
|
|
* @property CompanyBranches $branches |
|
19
|
|
|
* @property Companies $company |
|
20
|
|
|
* @property UserCompanyApps $app |
|
21
|
|
|
* @property \Phalcon\Di $di |
|
22
|
|
|
* |
|
23
|
|
|
*/ |
|
24
|
|
|
class Subscription extends PhalconSubscription |
|
25
|
|
|
{ |
|
26
|
|
|
const DEFAULT_GRACE_PERIOD_DAYS = 5; |
|
27
|
|
|
/** |
|
28
|
|
|
* |
|
29
|
|
|
* @var integer |
|
30
|
|
|
*/ |
|
31
|
|
|
public $apps_plans_id = 0; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* |
|
35
|
|
|
* @var integer |
|
36
|
|
|
*/ |
|
37
|
|
|
public $user_id; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* |
|
41
|
|
|
* @var integer |
|
42
|
|
|
*/ |
|
43
|
|
|
public $companies_id; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* |
|
47
|
|
|
* @var integer |
|
48
|
|
|
*/ |
|
49
|
|
|
public $apps_id; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* |
|
53
|
|
|
* @var string |
|
54
|
|
|
*/ |
|
55
|
|
|
public $name; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* |
|
59
|
|
|
* @var string |
|
60
|
|
|
*/ |
|
61
|
|
|
public $stripe_id; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* |
|
65
|
|
|
* @var string |
|
66
|
|
|
*/ |
|
67
|
|
|
public $stripe_plan; |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* |
|
71
|
|
|
* @var integer |
|
72
|
|
|
*/ |
|
73
|
|
|
public $quantity; |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* |
|
77
|
|
|
* @var integer |
|
78
|
|
|
*/ |
|
79
|
|
|
public $payment_frequency_id; |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* |
|
83
|
|
|
* @var string |
|
84
|
|
|
*/ |
|
85
|
|
|
public $trial_ends_at; |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* |
|
89
|
|
|
* @var integer |
|
90
|
|
|
*/ |
|
91
|
|
|
public $trial_ends_days; |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* |
|
95
|
|
|
* @var integer |
|
96
|
|
|
*/ |
|
97
|
|
|
public $is_freetrial; |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* |
|
101
|
|
|
* @var integer |
|
102
|
|
|
*/ |
|
103
|
|
|
public $is_active; |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* |
|
107
|
|
|
* @var integer |
|
108
|
|
|
*/ |
|
109
|
|
|
public $paid; |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* |
|
113
|
|
|
* @var string |
|
114
|
|
|
*/ |
|
115
|
|
|
public $charge_date; |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* |
|
119
|
|
|
* @var string |
|
120
|
|
|
*/ |
|
121
|
|
|
public $ends_at; |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* |
|
125
|
|
|
* @var date |
|
|
|
|
|
|
126
|
|
|
*/ |
|
127
|
|
|
public $grace_period_ends; |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* |
|
131
|
|
|
* @var datetime |
|
|
|
|
|
|
132
|
|
|
*/ |
|
133
|
|
|
public $next_due_payment; |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* |
|
137
|
|
|
* @var integer |
|
138
|
|
|
*/ |
|
139
|
|
|
public $is_cancelled; |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* |
|
143
|
|
|
* @var string |
|
144
|
|
|
*/ |
|
145
|
|
|
public $created_at; |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* |
|
149
|
|
|
* @var string |
|
150
|
|
|
*/ |
|
151
|
|
|
public $updated_at; |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* |
|
155
|
|
|
* @var integer |
|
156
|
|
|
*/ |
|
157
|
|
|
public $is_deleted; |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* Initialize. |
|
161
|
|
|
* |
|
162
|
|
|
* @return void |
|
163
|
|
|
*/ |
|
164
|
|
|
public function initialize() |
|
165
|
|
|
{ |
|
166
|
|
|
$this->belongsTo('user_id', 'Canvas\Models\Users', 'id', ['alias' => 'user']); |
|
167
|
|
|
|
|
168
|
|
|
$this->belongsTo( |
|
169
|
|
|
'companies_id', |
|
170
|
|
|
'Canvas\Models\Companies', |
|
171
|
|
|
'id', |
|
172
|
|
|
['alias' => 'company'] |
|
173
|
|
|
); |
|
174
|
|
|
|
|
175
|
|
|
$this->belongsTo( |
|
176
|
|
|
'apps_id', |
|
177
|
|
|
'Canvas\Models\Apps', |
|
178
|
|
|
'id', |
|
179
|
|
|
['alias' => 'app'] |
|
180
|
|
|
); |
|
181
|
|
|
|
|
182
|
|
|
$this->belongsTo( |
|
183
|
|
|
'apps_plans_id', |
|
184
|
|
|
'Canvas\Models\AppsPlans', |
|
185
|
|
|
'id', |
|
186
|
|
|
['alias' => 'appPlan'] |
|
187
|
|
|
); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* Get the active subscription for this company app. |
|
192
|
|
|
* |
|
193
|
|
|
* @return Subscription |
|
194
|
|
|
*/ |
|
195
|
|
|
public static function getActiveForThisApp(): Subscription |
|
196
|
|
|
{ |
|
197
|
|
|
return self::getByDefaultCompany(Di::getDefault()->getUserData()); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* Get subscription by user's default company;. |
|
202
|
|
|
* @param Users $user |
|
203
|
|
|
* @return Subscription |
|
204
|
|
|
*/ |
|
205
|
|
|
public static function getByDefaultCompany(Users $user): Subscription |
|
206
|
|
|
{ |
|
207
|
|
|
$subscription = self::findFirst([ |
|
208
|
|
|
'conditions' => 'companies_id = ?0 and apps_id = ?1 and is_deleted = 0', |
|
209
|
|
|
'bind' => [$user->getDefaultCompany()->getId(), Di::getDefault()->getApp()->getId()] |
|
210
|
|
|
]); |
|
211
|
|
|
|
|
212
|
|
|
if (!$subscription) { |
|
213
|
|
|
throw new InternalServerErrorException('No active subscription for the company: ' . $user->getDefaultCompany()->name); |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
return $subscription; |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* Search current company's app setting with key paid to verify payment status for current company. |
|
221
|
|
|
* |
|
222
|
|
|
* @param Users $user |
|
223
|
|
|
* @return bool |
|
224
|
|
|
*/ |
|
225
|
|
|
public static function getPaymentStatus(Users $user): bool |
|
226
|
|
|
{ |
|
227
|
|
|
//if its not subscription based return true to ignore any payment status |
|
228
|
|
|
if (!Di::getDefault()->getApp()->subscriptioBased()) { |
|
229
|
|
|
return true; |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
if (!self::getByDefaultCompany($user)->paid()) { |
|
233
|
|
|
return false; |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
return true; |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* Determine if the subscription is active. |
|
241
|
|
|
* |
|
242
|
|
|
* @return bool |
|
243
|
|
|
*/ |
|
244
|
|
|
public function active(): bool |
|
245
|
|
|
{ |
|
246
|
|
|
if (!Di::getDefault()->getApp()->subscriptioBased()) { |
|
247
|
|
|
return true; |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
return (bool) $this->is_active; |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
/** |
|
254
|
|
|
* Is the subscriptoin paid? |
|
255
|
|
|
* |
|
256
|
|
|
* @return boolean |
|
257
|
|
|
*/ |
|
258
|
|
|
public function paid(): bool |
|
259
|
|
|
{ |
|
260
|
|
|
if (!Di::getDefault()->getApp()->subscriptioBased()) { |
|
261
|
|
|
return true; |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
return (bool) $this->paid; |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
/** |
|
268
|
|
|
* Given a not active subscription activate it. |
|
269
|
|
|
* |
|
270
|
|
|
* @return void |
|
271
|
|
|
*/ |
|
272
|
|
|
public function activate(): bool |
|
273
|
|
|
{ |
|
274
|
|
|
$this->is_active = 1; |
|
275
|
|
|
$this->paid = 1; |
|
276
|
|
|
$this->grace_period_ends = new RawValue('NULL'); |
|
277
|
|
|
$this->ends_at = Carbon::now()->addDays(30)->toDateTimeString(); |
|
278
|
|
|
$this->next_due_payment = $this->ends_at; |
|
279
|
|
|
$this->is_cancelled = 0; |
|
280
|
|
|
return $this->update(); |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
|
|
/** |
|
284
|
|
|
* Determine if the subscription is within its trial period. |
|
285
|
|
|
* |
|
286
|
|
|
* @return bool |
|
287
|
|
|
*/ |
|
288
|
|
|
public function onTrial() |
|
289
|
|
|
{ |
|
290
|
|
|
return (bool) $this->is_freetrial; |
|
291
|
|
|
} |
|
292
|
|
|
|
|
293
|
|
|
/** |
|
294
|
|
|
* Get actual subscription. |
|
295
|
|
|
*/ |
|
296
|
|
|
public static function getActiveSubscription(): self |
|
297
|
|
|
{ |
|
298
|
|
|
$userSubscription = PhalconSubscription::findFirstOrFail([ |
|
299
|
|
|
'conditions' => 'companies_id = ?0 and apps_id = ?1 and is_deleted = 0', |
|
300
|
|
|
'bind' => [Di::getDefault()->getUserData()->currentCompanyId(), Di::getDefault()->getApp()->getId()] |
|
301
|
|
|
]); |
|
302
|
|
|
|
|
303
|
|
|
return Di::getDefault()->getUserData()->subscription($userSubscription->name); |
|
304
|
|
|
} |
|
305
|
|
|
|
|
306
|
|
|
/** |
|
307
|
|
|
* Validate subscription status by grace period date and update grace period date. |
|
308
|
|
|
* |
|
309
|
|
|
* @return void |
|
310
|
|
|
*/ |
|
311
|
|
|
public function validateByGracePeriod(): void |
|
312
|
|
|
{ |
|
313
|
|
|
if (!is_null($this->grace_period_ends)) { |
|
314
|
|
|
if (($this->charge_date == $this->grace_period_ends) && !$this->paid) { |
|
|
|
|
|
|
315
|
|
|
$this->is_active = 0; |
|
316
|
|
|
$this->grace_period_ends = Carbon::now()->addDays(Subscription::DEFAULT_GRACE_PERIOD_DAYS)->toDateTimeString(); |
|
317
|
|
|
} |
|
318
|
|
|
} else { |
|
319
|
|
|
$this->grace_period_ends = Carbon::now()->addDays(Subscription::DEFAULT_GRACE_PERIOD_DAYS)->toDateTimeString(); |
|
320
|
|
|
} |
|
321
|
|
|
} |
|
322
|
|
|
} |
|
323
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths