1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Canvas\Models; |
5
|
|
|
|
6
|
|
|
use Phalcon\Validation; |
7
|
|
|
use Phalcon\Validation\Validator\PresenceOf; |
|
|
|
|
8
|
|
|
use Canvas\Exception\ServerErrorHttpException; |
9
|
|
|
use Exception; |
10
|
|
|
use Carbon\Carbon; |
11
|
|
|
use Baka\Database\Contracts\HashTableTrait; |
12
|
|
|
use Baka\Blameable\BlameableTrait; |
13
|
|
|
use Canvas\Traits\UsersAssociatedTrait; |
14
|
|
|
use Canvas\Traits\FileSystemModelTrait; |
15
|
|
|
use Baka\Blameable\Blameable; |
16
|
|
|
use Phalcon\Traits\EventManagerAwareTrait; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class Companies. |
20
|
|
|
* |
21
|
|
|
* @package Canvas\Models |
22
|
|
|
* |
23
|
|
|
* @property Users $user |
24
|
|
|
* @property Users $userData |
25
|
|
|
* @property DefaultCompany $default_company |
26
|
|
|
* @property CompaniesBranches $branch |
27
|
|
|
* @property CompaniesBranches $branches |
28
|
|
|
* @property Subscription $subscription |
29
|
|
|
* @property Config $config |
30
|
|
|
* @property UserCompanyApps $app |
31
|
|
|
* @property \Phalcon\Di $di |
32
|
|
|
* @property Roles $roles_id |
33
|
|
|
*/ |
34
|
|
|
class Companies extends \Canvas\CustomFields\AbstractCustomFieldsModel |
35
|
|
|
{ |
36
|
|
|
use HashTableTrait; |
37
|
|
|
use UsersAssociatedTrait; |
38
|
|
|
use FileSystemModelTrait; |
39
|
|
|
use BlameableTrait; |
40
|
|
|
use EventManagerAwareTrait; |
41
|
|
|
|
42
|
|
|
const DEFAULT_COMPANY = 'DefaulCompany'; |
43
|
|
|
const PAYMENT_GATEWAY_CUSTOMER_KEY = 'payment_gateway_customer_id'; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* |
47
|
|
|
* @var integer |
48
|
|
|
*/ |
49
|
|
|
public $id; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* |
53
|
|
|
* @var string |
54
|
|
|
*/ |
55
|
|
|
public $name; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* |
59
|
|
|
* @var string |
60
|
|
|
*/ |
61
|
|
|
public $profile_image; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* |
65
|
|
|
* @var string |
66
|
|
|
*/ |
67
|
|
|
public $website; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* |
71
|
|
|
* @var integer |
72
|
|
|
*/ |
73
|
|
|
public $users_id; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* |
77
|
|
|
* @var integer |
78
|
|
|
*/ |
79
|
|
|
public $has_activities; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* |
83
|
|
|
* @var string |
84
|
|
|
*/ |
85
|
|
|
public $created_at; |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* |
89
|
|
|
* @var string |
90
|
|
|
*/ |
91
|
|
|
public $updated_at; |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* |
95
|
|
|
* @var integer |
96
|
|
|
*/ |
97
|
|
|
public $is_deleted; |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Provide the app plan id. |
101
|
|
|
* |
102
|
|
|
* @var integer |
103
|
|
|
*/ |
104
|
|
|
public $appPlanId = null; |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* |
108
|
|
|
* @var integer |
109
|
|
|
*/ |
110
|
|
|
public $currency_id; |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* |
114
|
|
|
* @var string |
115
|
|
|
*/ |
116
|
|
|
public $language; |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* |
120
|
|
|
* @var string |
121
|
|
|
*/ |
122
|
|
|
public $timezone; |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* |
126
|
|
|
* @var string |
127
|
|
|
*/ |
128
|
|
|
public $currency; |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* |
132
|
|
|
* @var integer |
133
|
|
|
*/ |
134
|
|
|
public $system_modules_id = 1; |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* |
138
|
|
|
* @var string |
139
|
|
|
*/ |
140
|
|
|
public $phone; |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Initialize method for model. |
144
|
|
|
*/ |
145
|
|
|
public function initialize() |
146
|
|
|
{ |
147
|
|
|
$this->setSource('companies'); |
148
|
|
|
|
149
|
|
|
$this->keepSnapshots(true); |
150
|
|
|
$this->addBehavior(new Blameable()); |
151
|
|
|
|
152
|
|
|
$this->belongsTo('users_id', 'Baka\Auth\Models\Users', 'id', ['alias' => 'user']); |
153
|
|
|
$this->hasMany('id', 'Baka\Auth\Models\CompanySettings', 'id', ['alias' => 'settings']); |
154
|
|
|
|
155
|
|
|
$this->belongsTo( |
156
|
|
|
'users_id', |
157
|
|
|
'Canvas\Models\Users', |
158
|
|
|
'id', |
159
|
|
|
['alias' => 'user'] |
160
|
|
|
); |
161
|
|
|
|
162
|
|
|
$this->hasMany( |
163
|
|
|
'id', |
164
|
|
|
'Canvas\Models\CompaniesBranches', |
165
|
|
|
'companies_id', |
166
|
|
|
['alias' => 'branches'] |
167
|
|
|
); |
168
|
|
|
|
169
|
|
|
$this->hasMany( |
170
|
|
|
'id', |
171
|
|
|
'Canvas\Models\CompaniesCustomFields', |
172
|
|
|
'companies_id', |
173
|
|
|
['alias' => 'fields'] |
174
|
|
|
); |
175
|
|
|
|
176
|
|
|
$this->hasMany( |
177
|
|
|
'id', |
178
|
|
|
'Canvas\CustomFields\CustomFields', |
179
|
|
|
'companies_id', |
180
|
|
|
['alias' => 'custom-fields'] |
181
|
|
|
); |
182
|
|
|
|
183
|
|
|
$this->hasMany( |
184
|
|
|
'id', |
185
|
|
|
'Canvas\Models\UsersAssociatedCompanies', |
186
|
|
|
'companies_id', |
187
|
|
|
['alias' => 'UsersAssociatedCompanies'] |
188
|
|
|
); |
189
|
|
|
|
190
|
|
|
$this->hasMany( |
191
|
|
|
'id', |
192
|
|
|
'Canvas\Models\UsersAssociatedApps', |
193
|
|
|
'companies_id', |
194
|
|
|
['alias' => 'UsersAssociatedApps'] |
195
|
|
|
); |
196
|
|
|
|
197
|
|
|
$this->hasMany( |
198
|
|
|
'id', |
199
|
|
|
'Canvas\Models\UsersAssociatedApps', |
200
|
|
|
'companies_id', |
201
|
|
|
[ |
202
|
|
|
'alias' => 'UsersAssociatedByApps', |
203
|
|
|
'params' => [ |
204
|
|
|
'conditions' => 'apps_id = ' . $this->di->getApp()->getId() |
205
|
|
|
] |
206
|
|
|
] |
207
|
|
|
); |
208
|
|
|
|
209
|
|
|
$this->hasOne( |
210
|
|
|
'id', |
211
|
|
|
'Canvas\Models\CompaniesBranches', |
212
|
|
|
'companies_id', |
213
|
|
|
[ |
214
|
|
|
'alias' => 'branch', |
215
|
|
|
] |
216
|
|
|
); |
217
|
|
|
|
218
|
|
|
$this->hasOne( |
219
|
|
|
'id', |
220
|
|
|
'Canvas\Models\UserCompanyApps', |
221
|
|
|
'companies_id', |
222
|
|
|
[ |
223
|
|
|
'alias' => 'app', |
224
|
|
|
'params' => [ |
225
|
|
|
'conditions' => 'apps_id = ' . $this->di->getApp()->getId() |
226
|
|
|
] |
227
|
|
|
] |
228
|
|
|
); |
229
|
|
|
|
230
|
|
|
$this->hasOne( |
231
|
|
|
'id', |
232
|
|
|
'Canvas\Models\UserCompanyApps', |
233
|
|
|
'companies_id', |
234
|
|
|
[ |
235
|
|
|
'alias' => 'apps', |
236
|
|
|
'params' => [ |
237
|
|
|
'conditions' => 'apps_id = ' . $this->di->getApp()->getId() |
238
|
|
|
] |
239
|
|
|
] |
240
|
|
|
); |
241
|
|
|
|
242
|
|
|
$this->hasOne( |
243
|
|
|
'id', |
244
|
|
|
'Canvas\Models\Subscription', |
245
|
|
|
'companies_id', |
246
|
|
|
[ |
247
|
|
|
'alias' => 'subscription', |
248
|
|
|
'params' => [ |
249
|
|
|
'conditions' => 'apps_id = ' . $this->di->getApp()->getId() . ' AND is_deleted = 0 ', |
250
|
|
|
'order' => 'id DESC' |
251
|
|
|
] |
252
|
|
|
] |
253
|
|
|
); |
254
|
|
|
|
255
|
|
|
$this->hasMany( |
256
|
|
|
'id', |
257
|
|
|
'Canvas\Models\Subscription', |
258
|
|
|
'companies_id', |
259
|
|
|
[ |
260
|
|
|
'alias' => 'subscriptions', |
261
|
|
|
'params' => [ |
262
|
|
|
'conditions' => 'apps_id = ' . $this->di->getApp()->getId() . ' AND is_deleted = 0', |
263
|
|
|
'order' => 'id DESC' |
264
|
|
|
] |
265
|
|
|
] |
266
|
|
|
); |
267
|
|
|
|
268
|
|
|
$this->hasMany( |
269
|
|
|
'id', |
270
|
|
|
'Canvas\Models\UserWebhooks', |
271
|
|
|
'companies_id', |
272
|
|
|
['alias' => 'user-webhooks'] |
273
|
|
|
); |
274
|
|
|
|
275
|
|
|
$systemModule = SystemModules::getSystemModuleByModelName(self::class); |
276
|
|
|
$this->hasOne( |
277
|
|
|
'id', |
278
|
|
|
'Canvas\Models\FileSystemEntities', |
279
|
|
|
'entity_id', |
280
|
|
|
[ |
281
|
|
|
'alias' => 'files', |
282
|
|
|
'conditions' => 'system_modules_id = ?0', |
283
|
|
|
'bind' => [$systemModule->getId()] |
284
|
|
|
] |
285
|
|
|
); |
286
|
|
|
|
287
|
|
|
$this->hasOne( |
288
|
|
|
'id', |
289
|
|
|
'Canvas\Models\FileSystemEntities', |
290
|
|
|
'entity_id', |
291
|
|
|
[ |
292
|
|
|
'alias' => 'logo', |
293
|
|
|
'conditions' => 'system_modules_id = ?0', |
294
|
|
|
'bind' => [$systemModule->getId()] |
295
|
|
|
] |
296
|
|
|
); |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* Model validation. |
301
|
|
|
* |
302
|
|
|
* @return void |
303
|
|
|
*/ |
304
|
|
|
public function validation() |
305
|
|
|
{ |
306
|
|
|
$validator = new Validation(); |
307
|
|
|
|
308
|
|
|
$validator->add( |
309
|
|
|
'name', |
310
|
|
|
new PresenceOf([ |
311
|
|
|
'model' => $this, |
312
|
|
|
'required' => true, |
313
|
|
|
]) |
314
|
|
|
); |
315
|
|
|
|
316
|
|
|
return $this->validate($validator); |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
/** |
320
|
|
|
* Register a company given a user and name. |
321
|
|
|
* |
322
|
|
|
* @param Users $user |
323
|
|
|
* @param string $name |
324
|
|
|
* @return Companies |
325
|
|
|
*/ |
326
|
|
|
public static function register(Users $user, string $name): Companies |
327
|
|
|
{ |
328
|
|
|
$company = new self(); |
329
|
|
|
$company->name = $name; |
330
|
|
|
$company->users_id = $user->getId(); |
331
|
|
|
|
332
|
|
|
if (!$company->save()) { |
333
|
|
|
throw new Exception(current($company->getMessages())); |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
return $company; |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
/** |
340
|
|
|
* Returns table name mapped in the model. |
341
|
|
|
* |
342
|
|
|
* @return string |
343
|
|
|
*/ |
344
|
|
|
public function getSource() : string |
345
|
|
|
{ |
346
|
|
|
return 'companies'; |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
/** |
350
|
|
|
* Confirm if a user belongs to this current company. |
351
|
|
|
* |
352
|
|
|
* @param Users $user |
353
|
|
|
* @return boolean |
354
|
|
|
*/ |
355
|
|
|
public function userAssociatedToCompany(Users $user): bool |
356
|
|
|
{ |
357
|
|
|
return is_object($this->getUsersAssociatedCompanies('users_id =' . $user->getId())); |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
/** |
361
|
|
|
* Get the stripe customer id from the. |
362
|
|
|
* |
363
|
|
|
* @return ?string |
364
|
|
|
*/ |
365
|
|
|
public function getPaymentGatewayCustomerId(): ?string |
366
|
|
|
{ |
367
|
|
|
return $this->get(self::PAYMENT_GATEWAY_CUSTOMER_KEY); |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
/** |
371
|
|
|
* Before crate company. |
372
|
|
|
* |
373
|
|
|
* @return void |
374
|
|
|
*/ |
375
|
|
|
public function beforeCreate() |
376
|
|
|
{ |
377
|
|
|
parent::beforeCreate(); |
378
|
|
|
|
379
|
|
|
$this->language = $this->di->getApp()->get('language'); |
380
|
|
|
$this->timezone = $this->di->getApp()->get('timezone'); |
381
|
|
|
$this->currency_id = Currencies::findFirstByCode($this->di->getApp()->get('currency'))->getId(); |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
/** |
385
|
|
|
* After creating the company. |
386
|
|
|
* |
387
|
|
|
* @return void |
388
|
|
|
*/ |
389
|
|
|
public function afterCreate() |
390
|
|
|
{ |
391
|
|
|
parent::afterCreate(); |
392
|
|
|
|
393
|
|
|
$this->fire('company:afterSignup', $this); |
394
|
|
|
} |
395
|
|
|
|
396
|
|
|
/** |
397
|
|
|
* Get the default company the users has selected. |
398
|
|
|
* |
399
|
|
|
* @param Users $user |
400
|
|
|
* @return Companies |
401
|
|
|
*/ |
402
|
|
|
public static function getDefaultByUser(Users $user): Companies |
403
|
|
|
{ |
404
|
|
|
//verify the user has a default company |
405
|
|
|
$defaultCompany = UserConfig::findFirst([ |
406
|
|
|
'conditions' => 'users_id = ?0 and name = ?1', |
407
|
|
|
'bind' => [$user->getId(), self::DEFAULT_COMPANY], |
408
|
|
|
]); |
409
|
|
|
|
410
|
|
|
//found it |
411
|
|
|
if (is_object($defaultCompany)) { |
412
|
|
|
return self::findFirst($defaultCompany->value); |
413
|
|
|
} |
414
|
|
|
|
415
|
|
|
//second try |
416
|
|
|
$defaultCompany = UsersAssociatedCompanies::findFirst([ |
417
|
|
|
'conditions' => 'users_id = ?0 and user_active =?1', |
418
|
|
|
'bind' => [$user->getId(), 1], |
419
|
|
|
]); |
420
|
|
|
|
421
|
|
|
if (is_object($defaultCompany)) { |
422
|
|
|
return self::findFirst($defaultCompany->companies_id); |
423
|
|
|
} |
424
|
|
|
|
425
|
|
|
throw new Exception(_("User doesn't have an active company")); |
426
|
|
|
} |
427
|
|
|
|
428
|
|
|
/** |
429
|
|
|
* Start a free trial for a new company. |
430
|
|
|
* |
431
|
|
|
* @return string //the customer id |
432
|
|
|
*/ |
433
|
|
|
public function startFreeTrial() : ?string |
434
|
|
|
{ |
435
|
|
|
$defaultPlan = AppsPlans::getDefaultPlan(); |
436
|
|
|
$trialEndsAt = Carbon::now()->addDays($this->di->getApp()->plan->free_trial_dates); |
437
|
|
|
|
438
|
|
|
//Lets create a new default subscription without payment method |
439
|
|
|
$this->user->newSubscription($defaultPlan->name, $defaultPlan->stripe_id, $this, $this->di->getApp()) |
440
|
|
|
->trialDays($defaultPlan->free_trial_dates) |
441
|
|
|
->create(); |
442
|
|
|
|
443
|
|
|
//ook for the subscription and update the missing info |
444
|
|
|
$subscription = $this->subscription; |
445
|
|
|
$subscription->apps_plans_id = $this->di->getApp()->default_apps_plan_id; |
446
|
|
|
$subscription->trial_ends_days = $trialEndsAt->diffInDays(Carbon::now()); |
447
|
|
|
$subscription->is_freetrial = 1; |
448
|
|
|
$subscription->is_active = 1; |
449
|
|
|
$subscription->payment_frequency_id = 1; |
450
|
|
|
|
451
|
|
|
if (!$subscription->save()) { |
452
|
|
|
throw new ServerErrorHttpException((string)'Subscription for new company couldnt be created ' . current($this->getMessages())); |
453
|
|
|
} |
454
|
|
|
|
455
|
|
|
return $this->user->stripe_id; |
456
|
|
|
} |
457
|
|
|
|
458
|
|
|
/** |
459
|
|
|
* Upload Files. |
460
|
|
|
* |
461
|
|
|
* @todo move this to the baka class |
462
|
|
|
* @return void |
463
|
|
|
*/ |
464
|
|
|
public function afterSave() |
465
|
|
|
{ |
466
|
|
|
//parent::afterSave(); |
467
|
|
|
$this->associateFileSystem(); |
468
|
|
|
} |
469
|
|
|
|
470
|
|
|
/** |
471
|
|
|
* Get an array of the associates users for this company. |
472
|
|
|
* |
473
|
|
|
* @return array |
474
|
|
|
*/ |
475
|
|
|
public function getAssociatedUsersByApp(): array |
476
|
|
|
{ |
477
|
|
|
return array_map(function ($users) { |
478
|
|
|
return $users['users_id']; |
479
|
|
|
}, $this->getUsersAssociatedByApps(['columns' => 'users_id'])->toArray()); |
480
|
|
|
} |
481
|
|
|
|
482
|
|
|
/** |
483
|
|
|
* Overwrite the relationship. |
484
|
|
|
* |
485
|
|
|
* @return void |
486
|
|
|
*/ |
487
|
|
|
public function getLogo() |
488
|
|
|
{ |
489
|
|
|
return $this->getFileByName('logo'); |
|
|
|
|
490
|
|
|
} |
491
|
|
|
} |
492
|
|
|
|
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