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