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