1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Gewaer\Models; |
5
|
|
|
|
6
|
|
|
use Phalcon\Validation; |
7
|
|
|
use Phalcon\Validation\Validator\PresenceOf; |
8
|
|
|
use Gewaer\Exception\ServerErrorHttpException; |
9
|
|
|
use Exception; |
10
|
|
|
use Carbon\Carbon; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class Companies |
14
|
|
|
* |
15
|
|
|
* @package Gewaer\Models |
16
|
|
|
* |
17
|
|
|
* @property Users $user |
18
|
|
|
* @property Users $userData |
19
|
|
|
* @property DefaultCompany $default_company |
20
|
|
|
* @property CompaniesBranches $branch |
21
|
|
|
* @property CompaniesBranches $branches |
22
|
|
|
* @property Config $config |
23
|
|
|
* @property UserCompanyApps $app |
24
|
|
|
* @property \Phalcon\Di $di |
25
|
|
|
*/ |
26
|
|
|
class Companies extends \Gewaer\CustomFields\AbstractCustomFieldsModel |
27
|
|
|
{ |
28
|
|
|
const DEFAULT_COMPANY = 'DefaulCompany'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* |
32
|
|
|
* @var integer |
33
|
|
|
*/ |
34
|
|
|
public $id; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
public $name; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
public $profile_image; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* |
50
|
|
|
* @var string |
51
|
|
|
*/ |
52
|
|
|
public $website; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* |
56
|
|
|
* @var integer |
57
|
|
|
*/ |
58
|
|
|
public $users_id; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* |
62
|
|
|
* @var integer |
63
|
|
|
*/ |
64
|
|
|
public $has_activities; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* |
68
|
|
|
* @var string |
69
|
|
|
*/ |
70
|
|
|
public $created_at; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* |
74
|
|
|
* @var string |
75
|
|
|
*/ |
76
|
|
|
public $updated_at; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* |
80
|
|
|
* @var integer |
81
|
|
|
*/ |
82
|
|
|
public $is_deleted; |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Provide the app plan id |
86
|
|
|
* |
87
|
|
|
* @var integer |
88
|
|
|
*/ |
89
|
|
|
public $appPlanId = null; |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* |
93
|
|
|
* @var integer |
94
|
|
|
*/ |
95
|
|
|
public $currency_id; |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Initialize method for model. |
99
|
|
|
*/ |
100
|
23 |
|
public function initialize() |
101
|
|
|
{ |
102
|
23 |
|
$this->setSource('companies'); |
103
|
|
|
|
104
|
23 |
|
$this->belongsTo('users_id', 'Baka\Auth\Models\Users', 'id', ['alias' => 'user']); |
105
|
23 |
|
$this->hasMany('id', 'Baka\Auth\Models\CompanySettings', 'id', ['alias' => 'settings']); |
106
|
|
|
|
107
|
23 |
|
$this->belongsTo( |
108
|
23 |
|
'users_id', |
109
|
23 |
|
'Gewaer\Models\Users', |
110
|
23 |
|
'id', |
111
|
23 |
|
['alias' => 'user'] |
112
|
|
|
); |
113
|
|
|
|
114
|
23 |
|
$this->hasMany( |
115
|
23 |
|
'id', |
116
|
23 |
|
'Gewaer\Models\CompaniesBranches', |
117
|
23 |
|
'companies_id', |
118
|
23 |
|
['alias' => 'branches'] |
119
|
|
|
); |
120
|
|
|
|
121
|
23 |
|
$this->hasMany( |
122
|
23 |
|
'id', |
123
|
23 |
|
'Gewaer\Models\CompaniesCustomFields', |
124
|
23 |
|
'companies_id', |
125
|
23 |
|
['alias' => 'fields'] |
126
|
|
|
); |
127
|
|
|
|
128
|
23 |
|
$this->hasMany( |
129
|
23 |
|
'id', |
130
|
23 |
|
'Gewaer\CustomFields\CustomFields', |
131
|
23 |
|
'companies_id', |
132
|
23 |
|
['alias' => 'custom-fields'] |
133
|
|
|
); |
134
|
|
|
|
135
|
23 |
|
$this->hasMany( |
136
|
23 |
|
'id', |
137
|
23 |
|
'Gewaer\Models\UsersAssociatedCompany', |
138
|
23 |
|
'companies_id', |
139
|
23 |
|
['alias' => 'UsersAssociatedCompany'] |
140
|
|
|
); |
141
|
|
|
|
142
|
23 |
|
$this->hasOne( |
143
|
23 |
|
'id', |
144
|
23 |
|
'Gewaer\Models\CompaniesBranches', |
145
|
23 |
|
'companies_id', |
146
|
|
|
[ |
147
|
23 |
|
'alias' => 'branch', |
148
|
|
|
] |
149
|
|
|
); |
150
|
|
|
|
151
|
23 |
|
$this->hasOne( |
152
|
23 |
|
'id', |
153
|
23 |
|
'Gewaer\Models\UserCompanyApps', |
154
|
23 |
|
'companies_id', |
155
|
|
|
[ |
156
|
23 |
|
'alias' => 'app', |
157
|
|
|
'params' => [ |
158
|
23 |
|
'conditions' => 'apps_id = ' . $this->di->getApp()->getId() |
159
|
|
|
] |
160
|
|
|
] |
161
|
|
|
); |
162
|
|
|
|
163
|
23 |
|
$this->hasOne( |
164
|
23 |
|
'id', |
165
|
23 |
|
'Gewaer\Models\UserCompanyApps', |
166
|
23 |
|
'companies_id', |
167
|
|
|
[ |
168
|
23 |
|
'alias' => 'apps', |
169
|
|
|
'params' => [ |
170
|
23 |
|
'conditions' => 'apps_id = ' . $this->di->getApp()->getId() |
171
|
|
|
] |
172
|
|
|
] |
173
|
|
|
); |
174
|
|
|
|
175
|
23 |
|
$this->hasOne( |
176
|
23 |
|
'id', |
177
|
23 |
|
'Gewaer\Models\Subscription', |
178
|
23 |
|
'companies_id', |
179
|
|
|
[ |
180
|
23 |
|
'alias' => 'subscription', |
181
|
|
|
'params' => [ |
182
|
23 |
|
'conditions' => 'apps_id = ' . $this->di->getApp()->getId() . ' AND ends_at is null AND is_deleted = 0 ', |
183
|
23 |
|
'order' => 'id DESC' |
184
|
|
|
] |
185
|
|
|
] |
186
|
|
|
); |
187
|
|
|
|
188
|
23 |
|
$this->hasMany( |
189
|
23 |
|
'id', |
190
|
23 |
|
'Gewaer\Models\Subscription', |
191
|
23 |
|
'companies_id', |
192
|
|
|
[ |
193
|
23 |
|
'alias' => 'subscriptions', |
194
|
|
|
'params' => [ |
195
|
23 |
|
'conditions' => 'apps_id = ' . $this->di->getApp()->getId() . ' AND is_deleted = 0', |
196
|
23 |
|
'order' => 'id DESC' |
197
|
|
|
] |
198
|
|
|
] |
199
|
|
|
); |
200
|
|
|
|
201
|
23 |
|
$this->hasMany( |
202
|
23 |
|
'id', |
203
|
23 |
|
'Gewaer\Models\UserWebhooks', |
204
|
23 |
|
'companies_id', |
205
|
23 |
|
['alias' => 'user-webhooks'] |
206
|
|
|
); |
207
|
|
|
|
208
|
23 |
|
$systemModule = SystemModules::getSystemModuleByModelName(self::class); |
209
|
23 |
|
$this->hasMany( |
210
|
23 |
|
'id', |
211
|
23 |
|
'Gewaer\Models\FileSystem', |
212
|
23 |
|
'entity_id', |
213
|
|
|
[ |
214
|
23 |
|
'alias' => 'filesystem', |
215
|
23 |
|
'conditions' => 'system_modules_id = ?0', |
216
|
23 |
|
'bind' => [$systemModule->getId()] |
217
|
|
|
] |
218
|
|
|
); |
219
|
23 |
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Model validation |
223
|
|
|
* |
224
|
|
|
* @return void |
225
|
|
|
*/ |
226
|
4 |
|
public function validation() |
227
|
|
|
{ |
228
|
4 |
|
$validator = new Validation(); |
229
|
|
|
|
230
|
4 |
|
$validator->add( |
231
|
4 |
|
'name', |
232
|
4 |
|
new PresenceOf([ |
233
|
4 |
|
'model' => $this, |
234
|
|
|
'required' => true, |
235
|
|
|
]) |
236
|
|
|
); |
237
|
|
|
|
238
|
4 |
|
return $this->validate($validator); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Register a company given a user and name |
243
|
|
|
* |
244
|
|
|
* @param Users $user |
245
|
|
|
* @param string $name |
246
|
|
|
* @return Companies |
247
|
|
|
*/ |
248
|
|
|
public static function register(Users $user, string $name): Companies |
249
|
|
|
{ |
250
|
|
|
$company = new self(); |
251
|
|
|
$company->name = $name; |
252
|
|
|
$company->users_id = $user->getId(); |
253
|
|
|
|
254
|
|
|
if (!$company->save()) { |
255
|
|
|
throw new Exception(current($company->getMessages())); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
return $company; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Returns table name mapped in the model. |
263
|
|
|
* |
264
|
|
|
* @return string |
265
|
|
|
*/ |
266
|
15 |
|
public function getSource() : string |
267
|
|
|
{ |
268
|
15 |
|
return 'companies'; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* Confirm if a user belongs to this current company |
273
|
|
|
* |
274
|
|
|
* @param Users $user |
275
|
|
|
* @return boolean |
276
|
|
|
*/ |
277
|
|
|
public function userAssociatedToCompany(Users $user): bool |
278
|
|
|
{ |
279
|
|
|
return is_object($this->getUsersAssociatedCompany('users_id =' . $user->getId())) ? true : false; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* After creating the company |
284
|
|
|
* |
285
|
|
|
* @return void |
286
|
|
|
*/ |
287
|
3 |
|
public function afterCreate() |
288
|
|
|
{ |
289
|
3 |
|
parent::afterCreate(); |
290
|
|
|
|
291
|
|
|
//setup the user notificatoin setting |
292
|
3 |
|
$companySettings = new CompaniesSettings(); |
293
|
3 |
|
$companySettings->companies_id = $this->getId(); |
294
|
3 |
|
$companySettings->name = 'notifications'; |
295
|
3 |
|
$companySettings->value = $this->user->email; |
296
|
3 |
|
if (!$companySettings->save()) { |
297
|
|
|
throw new Exception((string)current($companySettings->getMessages())); |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
//multi user asociation |
301
|
3 |
|
$usersAssociatedCompany = new UsersAssociatedCompany(); |
302
|
3 |
|
$usersAssociatedCompany->users_id = $this->user->getId(); |
303
|
3 |
|
$usersAssociatedCompany->companies_id = $this->getId(); |
304
|
3 |
|
$usersAssociatedCompany->identify_id = $this->user->getId(); |
305
|
3 |
|
$usersAssociatedCompany->user_active = 1; |
306
|
3 |
|
$usersAssociatedCompany->user_role = 'admin'; |
307
|
3 |
|
if (!$usersAssociatedCompany->save()) { |
308
|
|
|
throw new Exception((string)current($usersAssociatedCompany->getMessages())); |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
//now thta we setup de company and associated with the user we need to setup this as its default company |
312
|
3 |
|
if (!UserConfig::findFirst(['conditions' => 'users_id = ?0 and name = ?1', 'bind' => [$this->user->getId(), self::DEFAULT_COMPANY]])) { |
313
|
1 |
|
$userConfig = new UserConfig(); |
314
|
1 |
|
$userConfig->users_id = $this->user->getId(); |
315
|
1 |
|
$userConfig->name = self::DEFAULT_COMPANY; |
316
|
1 |
|
$userConfig->value = $this->getId(); |
317
|
|
|
|
318
|
1 |
|
if (!$userConfig->save()) { |
319
|
|
|
throw new Exception((string)current($userConfig->getMessages())); |
320
|
|
|
} |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
/** |
324
|
|
|
* @var CompaniesBranches |
325
|
|
|
*/ |
326
|
3 |
|
$branch = new CompaniesBranches(); |
327
|
3 |
|
$branch->companies_id = $this->getId(); |
328
|
3 |
|
$branch->users_id = $this->user->getId(); |
329
|
3 |
|
$branch->name = 'Default'; |
330
|
3 |
|
$branch->is_default = 1; |
331
|
3 |
|
$branch->description = ''; |
332
|
3 |
|
if (!$branch->save()) { |
333
|
|
|
throw new ServerErrorHttpException((string)current($branch->getMessages())); |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
//look for the default plan for this app |
337
|
3 |
|
$companyApps = new UserCompanyApps(); |
338
|
3 |
|
$companyApps->companies_id = $this->getId(); |
339
|
3 |
|
$companyApps->apps_id = $this->di->getApp()->getId(); |
340
|
3 |
|
$companyApps->subscriptions_id = 0; |
341
|
|
|
|
342
|
|
|
//we need to assign this company to a plan |
343
|
3 |
|
if (empty($this->appPlanId)) { |
344
|
3 |
|
$plan = AppsPlans::getDefaultPlan(); |
345
|
3 |
|
$companyApps->stripe_id = $plan->stripe_id; |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
//If the newly created company is not the default then we create a new subscription with the same user |
349
|
3 |
|
if ($this->userData->default_company != $this->getId()) { |
350
|
3 |
|
$subscription = $this->startCompanySubscription(); |
351
|
|
|
|
352
|
3 |
|
if (!is_object($subscription)) { |
353
|
|
|
throw new Exception('Subscription for new company could not be created'); |
354
|
|
|
} |
355
|
|
|
} |
356
|
|
|
|
357
|
3 |
|
$companyApps->created_at = date('Y-m-d H:i:s'); |
358
|
3 |
|
$companyApps->is_deleted = 0; |
359
|
|
|
|
360
|
3 |
|
if (!$companyApps->save()) { |
361
|
|
|
throw new ServerErrorHttpException((string)current($companyApps->getMessages())); |
362
|
|
|
} |
363
|
3 |
|
} |
364
|
|
|
|
365
|
|
|
/** |
366
|
|
|
* Get the default company the users has selected |
367
|
|
|
* |
368
|
|
|
* @param Users $user |
369
|
|
|
* @return Companies |
370
|
|
|
*/ |
371
|
|
|
public static function getDefaultByUser(Users $user): Companies |
372
|
|
|
{ |
373
|
|
|
//verify the user has a default company |
374
|
|
|
$defaultCompany = UserConfig::findFirst([ |
375
|
|
|
'conditions' => 'users_id = ?0 and name = ?1', |
376
|
|
|
'bind' => [$user->getId(), self::DEFAULT_COMPANY], |
377
|
|
|
]); |
378
|
|
|
|
379
|
|
|
//found it |
380
|
|
|
if (is_object($defaultCompany)) { |
381
|
|
|
return self::findFirst($defaultCompany->value); |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
//second try |
385
|
|
|
$defaultCompany = UsersAssociatedCompany::findFirst([ |
386
|
|
|
'conditions' => 'users_id = ?0 and user_active =?1', |
387
|
|
|
'bind' => [$user->getId(), 1], |
388
|
|
|
]); |
389
|
|
|
|
390
|
|
|
if (is_object($defaultCompany)) { |
391
|
|
|
return self::findFirst($defaultCompany->companies_id); |
392
|
|
|
} |
393
|
|
|
|
394
|
|
|
throw new Exception(_("User doesn't have an active company")); |
395
|
|
|
} |
396
|
|
|
|
397
|
|
|
/** |
398
|
|
|
* After the model was update we need to update its custom fields |
399
|
|
|
* |
400
|
|
|
* @return void |
401
|
|
|
*/ |
402
|
2 |
|
public function afterUpdate() |
403
|
|
|
{ |
404
|
|
|
//only clean and change custom fields if they are been sent |
405
|
2 |
|
if (!empty($this->customFields)) { |
406
|
|
|
//replace old custom with new |
407
|
1 |
|
$allCustomFields = $this->getAllCustomFields(); |
408
|
1 |
|
if (is_array($allCustomFields)) { |
409
|
1 |
|
foreach ($this->customFields as $key => $value) { |
410
|
1 |
|
$allCustomFields[$key] = $value; |
411
|
|
|
} |
412
|
|
|
} |
413
|
|
|
|
414
|
1 |
|
if (!empty($allCustomFields)) { |
415
|
|
|
//set |
416
|
1 |
|
$this->setCustomFields($allCustomFields); |
417
|
|
|
//clean old |
418
|
1 |
|
$this->cleanCustomFields($this->getId()); |
419
|
|
|
//save new |
420
|
1 |
|
$this->saveCustomFields(); |
421
|
|
|
} |
422
|
|
|
} |
423
|
2 |
|
} |
424
|
|
|
|
425
|
|
|
/** |
426
|
|
|
* Start a free trial for a new company |
427
|
|
|
* |
428
|
|
|
* @return Subscription |
429
|
|
|
*/ |
430
|
3 |
|
public function startCompanySubscription() : Subscription |
431
|
|
|
{ |
432
|
3 |
|
$defaultPlan = AppsPlans::getDefaultPlan(); |
433
|
3 |
|
$trialEndsAt = Carbon::now()->addDays($this->di->getApp()->plan->free_trial_dates); |
434
|
|
|
|
435
|
3 |
|
$subscription = new Subscription(); |
436
|
3 |
|
$subscription->user_id = $this->di->getUserData()->getId(); |
437
|
3 |
|
$subscription->companies_id = $this->getId(); |
438
|
3 |
|
$subscription->apps_id = $this->di->getApp()->getId(); |
439
|
3 |
|
$subscription->apps_plans_id = $this->di->getApp()->default_apps_plan_id; |
440
|
3 |
|
$subscription->name = $defaultPlan->name; |
441
|
3 |
|
$subscription->stripe_id = $defaultPlan->stripe_id; |
442
|
3 |
|
$subscription->stripe_plan = $defaultPlan->stripe_plan; |
443
|
3 |
|
$subscription->quantity = 1; |
444
|
3 |
|
$subscription->trial_ends_at = $trialEndsAt->toDateTimeString(); |
445
|
3 |
|
$subscription->trial_ends_days = $trialEndsAt->diffInDays(Carbon::now()); |
446
|
3 |
|
$subscription->is_freetrial = 1; |
447
|
3 |
|
$subscription->is_active = 1; |
448
|
|
|
|
449
|
3 |
|
if (!$subscription->save()) { |
450
|
|
|
throw new ServerErrorHttpException((string)current($this->getMessages())); |
451
|
|
|
} |
452
|
|
|
|
453
|
3 |
|
$this->update(); |
454
|
|
|
|
455
|
3 |
|
return $subscription; |
456
|
|
|
} |
457
|
|
|
} |
458
|
|
|
|