1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Canvas\Models; |
4
|
|
|
|
5
|
|
|
use Phalcon\Cashier\Subscription as PhalconSubscription; |
6
|
|
|
use Canvas\Exception\ServerErrorHttpException; |
7
|
|
|
use Phalcon\Di; |
8
|
|
|
use Carbon\Carbon; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Trait Subscription. |
12
|
|
|
* |
13
|
|
|
* @package Canvas\Models |
14
|
|
|
* |
15
|
|
|
* @property Users $user |
16
|
|
|
* @property AppsPlans $appPlan |
17
|
|
|
* @property CompanyBranches $branches |
18
|
|
|
* @property Companies $company |
19
|
|
|
* @property UserCompanyApps $app |
20
|
|
|
* @property \Phalcon\Di $di |
21
|
|
|
* |
22
|
|
|
*/ |
23
|
|
|
class Subscription extends PhalconSubscription |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* |
27
|
|
|
* @var integer |
28
|
|
|
*/ |
29
|
|
|
public $apps_plans_id = 0; |
|
|
|
|
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* |
33
|
|
|
* @var integer |
34
|
|
|
*/ |
35
|
|
|
public $user_id; |
|
|
|
|
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* |
39
|
|
|
* @var integer |
40
|
|
|
*/ |
41
|
|
|
public $companies_id; |
|
|
|
|
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* |
45
|
|
|
* @var integer |
46
|
|
|
*/ |
47
|
|
|
public $apps_id; |
|
|
|
|
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* |
51
|
|
|
* @var string |
52
|
|
|
*/ |
53
|
|
|
public $name; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* |
57
|
|
|
* @var string |
58
|
|
|
*/ |
59
|
|
|
public $stripe_id; |
|
|
|
|
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* |
63
|
|
|
* @var string |
64
|
|
|
*/ |
65
|
|
|
public $stripe_plan; |
|
|
|
|
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* |
69
|
|
|
* @var integer |
70
|
|
|
*/ |
71
|
|
|
public $quantity; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* |
75
|
|
|
* @var integer |
76
|
|
|
*/ |
77
|
|
|
public $payment_frequency_id; |
|
|
|
|
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* |
81
|
|
|
* @var string |
82
|
|
|
*/ |
83
|
|
|
public $trial_ends_at; |
|
|
|
|
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* |
87
|
|
|
* @var integer |
88
|
|
|
*/ |
89
|
|
|
public $trial_ends_days; |
|
|
|
|
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* |
93
|
|
|
* @var integer |
94
|
|
|
*/ |
95
|
|
|
public $is_freetrial; |
|
|
|
|
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* |
99
|
|
|
* @var integer |
100
|
|
|
*/ |
101
|
|
|
public $is_active; |
|
|
|
|
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* |
105
|
|
|
* @var integer |
106
|
|
|
*/ |
107
|
|
|
public $paid; |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* |
111
|
|
|
* @var string |
112
|
|
|
*/ |
113
|
|
|
public $charge_date; |
|
|
|
|
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* |
117
|
|
|
* @var string |
118
|
|
|
*/ |
119
|
|
|
public $ends_at; |
|
|
|
|
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* |
123
|
|
|
* @var date |
124
|
|
|
*/ |
125
|
|
|
public $grace_period_ends; |
|
|
|
|
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* |
129
|
|
|
* @var datetime |
130
|
|
|
*/ |
131
|
|
|
public $next_due_payment; |
|
|
|
|
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* |
135
|
|
|
* @var integer |
136
|
|
|
*/ |
137
|
|
|
public $is_cancelled; |
|
|
|
|
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* |
141
|
|
|
* @var string |
142
|
|
|
*/ |
143
|
|
|
public $created_at; |
|
|
|
|
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* |
147
|
|
|
* @var string |
148
|
|
|
*/ |
149
|
|
|
public $updated_at; |
|
|
|
|
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* |
153
|
|
|
* @var integer |
154
|
|
|
*/ |
155
|
|
|
public $is_deleted; |
|
|
|
|
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Initialize. |
159
|
|
|
* |
160
|
|
|
* @return void |
161
|
|
|
*/ |
162
|
|
|
public function initialize() |
163
|
|
|
{ |
164
|
|
|
$this->belongsTo('user_id', 'Canvas\Models\Users', 'id', ['alias' => 'user']); |
165
|
|
|
|
166
|
|
|
$this->belongsTo( |
167
|
|
|
'companies_id', |
168
|
|
|
'Canvas\Models\Companies', |
169
|
|
|
'id', |
170
|
|
|
['alias' => 'company'] |
171
|
|
|
); |
172
|
|
|
|
173
|
|
|
$this->belongsTo( |
174
|
|
|
'apps_id', |
175
|
|
|
'Canvas\Models\Apps', |
176
|
|
|
'id', |
177
|
|
|
['alias' => 'app'] |
178
|
|
|
); |
179
|
|
|
|
180
|
|
|
$this->belongsTo( |
181
|
|
|
'apps_plans_id', |
182
|
|
|
'Canvas\Models\AppsPlans', |
183
|
|
|
'id', |
184
|
|
|
['alias' => 'appPlan'] |
185
|
|
|
); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Get the active subscription for this company app. |
190
|
|
|
* |
191
|
|
|
* @return void |
|
|
|
|
192
|
|
|
*/ |
193
|
|
|
public static function getActiveForThisApp() : Subscription |
194
|
|
|
{ |
195
|
|
|
$subscription = self::findFirst([ |
196
|
|
|
'conditions' => 'companies_id = ?0 and apps_id = ?1 and is_deleted = 0', |
197
|
|
|
'bind' => [Di::getDefault()->getUserData()->currentCompanyId(), Di::getDefault()->getApp()->getId()] |
198
|
|
|
]); |
199
|
|
|
|
200
|
|
|
if (!is_object($subscription)) { |
201
|
|
|
throw new ServerErrorHttpException(_('No active subscription for this app ' . Di::getDefault()->getApp()->getId() . ' at the company ' . Di::getDefault()->getUserData()->currentCompanyId())); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
return $subscription; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Get subscription by user's default company;. |
209
|
|
|
* @param Users $user |
210
|
|
|
* @return Subscription |
211
|
|
|
*/ |
212
|
|
|
public static function getByDefaultCompany(Users $user): Subscription |
213
|
|
|
{ |
214
|
|
|
$subscription = self::findFirst([ |
215
|
|
|
'conditions' => 'user_id = ?0 and companies_id = ?1 and apps_id = ?2 and is_deleted = 0', |
216
|
|
|
'bind' => [$user->getId(), $user->defaultCompany->getId(), Di::getDefault()->getApp()->getId()] |
217
|
|
|
]); |
218
|
|
|
|
219
|
|
|
if (!is_object($subscription)) { |
220
|
|
|
throw new ServerErrorHttpException('No active subscription for default company'); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
return $subscription; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Search current company's app setting with key paid to verify payment status for current company. |
228
|
|
|
* |
229
|
|
|
* @param Users $user |
230
|
|
|
* @return bool |
231
|
|
|
*/ |
232
|
|
|
public static function getPaymentStatus(Users $user): bool |
|
|
|
|
233
|
|
|
{ |
234
|
|
|
//if its not subscription based return true to ignore any payment status |
235
|
|
|
if (!Di::getDefault()->getApp()->subscriptioBased()) { |
236
|
|
|
return true; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
if (!$user->defaultCompany->get('paid')) { |
|
|
|
|
240
|
|
|
return false; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
return true; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Determine if the subscription is active. |
248
|
|
|
* |
249
|
|
|
* @return bool |
250
|
|
|
*/ |
251
|
|
|
public function active(): bool |
|
|
|
|
252
|
|
|
{ |
253
|
|
|
return (bool) $this->is_active; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* Is the subscriptoin paid? |
258
|
|
|
* |
259
|
|
|
* @return boolean |
260
|
|
|
*/ |
261
|
|
|
public function paid(): bool |
|
|
|
|
262
|
|
|
{ |
263
|
|
|
return (bool) $this->paid; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* Given a not active subscription activate it |
268
|
|
|
* |
269
|
|
|
* @return void |
|
|
|
|
270
|
|
|
*/ |
271
|
|
|
public function activate(): bool |
|
|
|
|
272
|
|
|
{ |
273
|
|
|
$this->is_active = 1; |
274
|
|
|
$this->paid = 1; |
275
|
|
|
$this->grace_period_ends = ''; |
|
|
|
|
276
|
|
|
$this->ends_at = Carbon::now()->addDays(30)->toDateTimeString(); |
277
|
|
|
$this->next_due_payment = $this->ends_at; |
278
|
|
|
$this->is_cancelled = 0; |
279
|
|
|
return $this->update(); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* Determine if the subscription is within its trial period. |
284
|
|
|
* |
285
|
|
|
* @return bool |
286
|
|
|
*/ |
287
|
|
|
public function onTrial() |
|
|
|
|
288
|
|
|
{ |
289
|
|
|
return (bool)$this->is_freetrial; |
290
|
|
|
} |
291
|
|
|
} |
292
|
|
|
|
This check examines a number of code elements and verifies that they conform to the given naming conventions.
You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.