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