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 | * |
||
103 | * @var string |
||
104 | */ |
||
105 | public $language; |
||
106 | |||
107 | /** |
||
108 | * |
||
109 | * @var string |
||
110 | */ |
||
111 | public $timezone; |
||
112 | |||
113 | /** |
||
114 | * |
||
115 | * @var string |
||
116 | */ |
||
117 | public $currency; |
||
118 | |||
119 | /** |
||
120 | * Initialize method for model. |
||
121 | */ |
||
122 | 23 | public function initialize() |
|
123 | { |
||
124 | 23 | $this->setSource('companies'); |
|
125 | |||
126 | 23 | $this->belongsTo('users_id', 'Baka\Auth\Models\Users', 'id', ['alias' => 'user']); |
|
127 | 23 | $this->hasMany('id', 'Baka\Auth\Models\CompanySettings', 'id', ['alias' => 'settings']); |
|
128 | |||
129 | 23 | $this->belongsTo( |
|
130 | 23 | 'users_id', |
|
131 | 23 | 'Gewaer\Models\Users', |
|
132 | 23 | 'id', |
|
133 | 23 | ['alias' => 'user'] |
|
134 | ); |
||
135 | |||
136 | 23 | $this->hasMany( |
|
137 | 23 | 'id', |
|
138 | 23 | 'Gewaer\Models\CompaniesBranches', |
|
139 | 23 | 'companies_id', |
|
140 | 23 | ['alias' => 'branches'] |
|
141 | ); |
||
142 | |||
143 | 23 | $this->hasMany( |
|
144 | 23 | 'id', |
|
145 | 23 | 'Gewaer\Models\CompaniesCustomFields', |
|
146 | 23 | 'companies_id', |
|
147 | 23 | ['alias' => 'fields'] |
|
148 | ); |
||
149 | |||
150 | 23 | $this->hasMany( |
|
151 | 23 | 'id', |
|
152 | 23 | 'Gewaer\CustomFields\CustomFields', |
|
153 | 23 | 'companies_id', |
|
154 | 23 | ['alias' => 'custom-fields'] |
|
155 | ); |
||
156 | |||
157 | 23 | $this->hasMany( |
|
158 | 23 | 'id', |
|
159 | 23 | 'Gewaer\Models\UsersAssociatedCompany', |
|
160 | 23 | 'companies_id', |
|
161 | 23 | ['alias' => 'UsersAssociatedCompany'] |
|
162 | ); |
||
163 | |||
164 | 23 | $this->hasOne( |
|
165 | 23 | 'id', |
|
166 | 23 | 'Gewaer\Models\CompaniesBranches', |
|
167 | 23 | 'companies_id', |
|
168 | [ |
||
169 | 23 | 'alias' => 'branch', |
|
170 | ] |
||
171 | ); |
||
172 | |||
173 | 23 | $this->hasOne( |
|
174 | 23 | 'id', |
|
175 | 23 | 'Gewaer\Models\UserCompanyApps', |
|
176 | 23 | 'companies_id', |
|
177 | [ |
||
178 | 23 | 'alias' => 'app', |
|
179 | 'params' => [ |
||
180 | 23 | 'conditions' => 'apps_id = ' . $this->di->getApp()->getId() |
|
181 | ] |
||
182 | ] |
||
183 | ); |
||
184 | |||
185 | 23 | $this->hasOne( |
|
186 | 23 | 'id', |
|
187 | 23 | 'Gewaer\Models\UserCompanyApps', |
|
188 | 23 | 'companies_id', |
|
189 | [ |
||
190 | 23 | 'alias' => 'apps', |
|
191 | 'params' => [ |
||
192 | 23 | 'conditions' => 'apps_id = ' . $this->di->getApp()->getId() |
|
193 | ] |
||
194 | ] |
||
195 | ); |
||
196 | |||
197 | 23 | $this->hasOne( |
|
198 | 23 | 'id', |
|
199 | 23 | 'Gewaer\Models\Subscription', |
|
200 | 23 | 'companies_id', |
|
201 | [ |
||
202 | 23 | 'alias' => 'subscription', |
|
203 | 'params' => [ |
||
204 | 23 | 'conditions' => 'apps_id = ' . $this->di->getApp()->getId() . ' AND ends_at is null AND is_deleted = 0 ', |
|
205 | 23 | 'order' => 'id DESC' |
|
206 | ] |
||
207 | ] |
||
208 | ); |
||
209 | |||
210 | 23 | $this->hasMany( |
|
211 | 23 | 'id', |
|
212 | 23 | 'Gewaer\Models\Subscription', |
|
213 | 23 | 'companies_id', |
|
214 | [ |
||
215 | 23 | 'alias' => 'subscriptions', |
|
216 | 'params' => [ |
||
217 | 23 | 'conditions' => 'apps_id = ' . $this->di->getApp()->getId() . ' AND is_deleted = 0', |
|
218 | 23 | 'order' => 'id DESC' |
|
219 | ] |
||
220 | ] |
||
221 | ); |
||
222 | |||
223 | 23 | $this->hasMany( |
|
224 | 23 | 'id', |
|
225 | 23 | 'Gewaer\Models\UserWebhooks', |
|
226 | 23 | 'companies_id', |
|
227 | 23 | ['alias' => 'user-webhooks'] |
|
228 | ); |
||
229 | |||
230 | 23 | $systemModule = SystemModules::getSystemModuleByModelName(self::class); |
|
231 | 23 | $this->hasMany( |
|
232 | 23 | 'id', |
|
233 | 23 | 'Gewaer\Models\FileSystem', |
|
234 | 23 | 'entity_id', |
|
235 | [ |
||
236 | 23 | 'alias' => 'filesystem', |
|
237 | 23 | 'conditions' => 'system_modules_id = ?0', |
|
238 | 23 | 'bind' => [$systemModule->getId()] |
|
239 | ] |
||
240 | ); |
||
241 | 23 | } |
|
242 | |||
243 | /** |
||
244 | * Model validation |
||
245 | * |
||
246 | * @return void |
||
247 | */ |
||
248 | 5 | public function validation() |
|
249 | { |
||
250 | 5 | $validator = new Validation(); |
|
251 | |||
252 | 5 | $validator->add( |
|
253 | 5 | 'name', |
|
254 | 5 | new PresenceOf([ |
|
255 | 5 | 'model' => $this, |
|
256 | 'required' => true, |
||
257 | ]) |
||
258 | ); |
||
259 | |||
260 | 5 | return $this->validate($validator); |
|
261 | } |
||
262 | |||
263 | /** |
||
264 | * Register a company given a user and name |
||
265 | * |
||
266 | * @param Users $user |
||
267 | * @param string $name |
||
268 | * @return Companies |
||
269 | */ |
||
270 | public static function register(Users $user, string $name): Companies |
||
271 | { |
||
272 | $company = new self(); |
||
273 | $company->name = $name; |
||
274 | $company->users_id = $user->getId(); |
||
275 | |||
276 | if (!$company->save()) { |
||
277 | throw new Exception(current($company->getMessages())); |
||
278 | } |
||
279 | |||
280 | return $company; |
||
281 | } |
||
282 | |||
283 | /** |
||
284 | * Returns table name mapped in the model. |
||
285 | * |
||
286 | * @return string |
||
287 | */ |
||
288 | 15 | public function getSource() : string |
|
289 | { |
||
290 | 15 | return 'companies'; |
|
291 | } |
||
292 | |||
293 | /** |
||
294 | * Confirm if a user belongs to this current company |
||
295 | * |
||
296 | * @param Users $user |
||
297 | * @return boolean |
||
298 | */ |
||
299 | 2 | public function userAssociatedToCompany(Users $user): bool |
|
300 | { |
||
301 | 2 | return is_object($this->getUsersAssociatedCompany('users_id =' . $user->getId())) ? true : false; |
|
302 | } |
||
303 | |||
304 | /** |
||
305 | * Get the stripe customer id from the |
||
306 | * |
||
307 | * @return ?string |
||
308 | */ |
||
309 | 1 | public function getPaymentGatewayCustomerId(): ?string |
|
310 | { |
||
311 | 1 | return $this->getSettings(self::PAYMENT_GATEWAY_CUSTOMER_KEY); |
|
312 | } |
||
313 | |||
314 | /** |
||
315 | * Before crate company |
||
316 | * |
||
317 | * @return void |
||
318 | */ |
||
319 | 3 | public function beforeCreate() |
|
320 | { |
||
321 | 3 | parent::beforeCreate(); |
|
322 | |||
323 | |||
324 | 3 | $this->language = AppsSettings::getDefaultAppsSettingsByName('language'); |
|
325 | 3 | $this->timezone = AppsSettings::getDefaultAppsSettingsByName('timezone'); |
|
326 | 3 | $this->currency = Currencies::findFirstByCode(AppsSettings::getDefaultAppsSettingsByName('currency'))->getId(); |
|
1 ignored issue
–
show
Bug
introduced
by
![]() |
|||
327 | 3 | } |
|
328 | |||
329 | /** |
||
330 | * After creating the company |
||
331 | * |
||
332 | * @return void |
||
333 | */ |
||
334 | 3 | public function afterCreate() |
|
335 | { |
||
336 | 3 | parent::afterCreate(); |
|
337 | |||
338 | //setup the user notificatoin setting |
||
339 | 3 | $this->setSettings('notifications', $this->user->email); |
|
340 | |||
341 | //multi user asociation |
||
342 | 3 | $usersAssociatedCompany = new UsersAssociatedCompany(); |
|
343 | 3 | $usersAssociatedCompany->users_id = $this->user->getId(); |
|
344 | 3 | $usersAssociatedCompany->companies_id = $this->getId(); |
|
345 | 3 | $usersAssociatedCompany->identify_id = $this->user->getId(); |
|
346 | 3 | $usersAssociatedCompany->user_active = 1; |
|
347 | 3 | $usersAssociatedCompany->user_role = 'admin'; |
|
348 | 3 | if (!$usersAssociatedCompany->save()) { |
|
349 | throw new Exception((string)current($usersAssociatedCompany->getMessages())); |
||
350 | } |
||
351 | |||
352 | //now thta we setup de company and associated with the user we need to setup this as its default company |
||
353 | 3 | if (!UserConfig::findFirst(['conditions' => 'users_id = ?0 and name = ?1', 'bind' => [$this->user->getId(), self::DEFAULT_COMPANY]])) { |
|
354 | 1 | $userConfig = new UserConfig(); |
|
355 | 1 | $userConfig->users_id = $this->user->getId(); |
|
356 | 1 | $userConfig->name = self::DEFAULT_COMPANY; |
|
357 | 1 | $userConfig->value = $this->getId(); |
|
358 | |||
359 | 1 | if (!$userConfig->save()) { |
|
360 | throw new Exception((string)current($userConfig->getMessages())); |
||
361 | } |
||
362 | } |
||
363 | |||
364 | /** |
||
365 | * @var CompaniesBranches |
||
366 | */ |
||
367 | 3 | $branch = new CompaniesBranches(); |
|
368 | 3 | $branch->companies_id = $this->getId(); |
|
369 | 3 | $branch->users_id = $this->user->getId(); |
|
370 | 3 | $branch->name = 'Default'; |
|
371 | 3 | $branch->is_default = 1; |
|
372 | 3 | $branch->description = ''; |
|
373 | 3 | if (!$branch->save()) { |
|
374 | throw new ServerErrorHttpException((string)current($branch->getMessages())); |
||
375 | } |
||
376 | |||
377 | //look for the default plan for this app |
||
378 | 3 | $companyApps = new UserCompanyApps(); |
|
379 | 3 | $companyApps->companies_id = $this->getId(); |
|
380 | 3 | $companyApps->apps_id = $this->di->getApp()->getId(); |
|
381 | //$companyApps->subscriptions_id = 0; |
||
382 | |||
383 | //we need to assign this company to a plan |
||
384 | 3 | if (empty($this->appPlanId)) { |
|
385 | 3 | $plan = AppsPlans::getDefaultPlan(); |
|
386 | 3 | $companyApps->stripe_id = $plan->stripe_id; |
|
387 | } |
||
388 | |||
389 | //If the newly created company is not the default then we create a new subscription with the same user |
||
390 | 3 | if ($this->di->getUserData()->default_company != $this->getId()) { |
|
391 | 3 | $this->setSettings(self::PAYMENT_GATEWAY_CUSTOMER_KEY, $this->startFreeTrial()); |
|
392 | } |
||
393 | |||
394 | 3 | $companyApps->subscriptions_id = $this->subscription->getId(); |
|
395 | 3 | $companyApps->created_at = date('Y-m-d H:i:s'); |
|
396 | 3 | $companyApps->is_deleted = 0; |
|
397 | |||
398 | 3 | if (!$companyApps->save()) { |
|
399 | throw new ServerErrorHttpException((string)current($companyApps->getMessages())); |
||
400 | } |
||
401 | 3 | } |
|
402 | |||
403 | /** |
||
404 | * Get the default company the users has selected |
||
405 | * |
||
406 | * @param Users $user |
||
407 | * @return Companies |
||
408 | */ |
||
409 | public static function getDefaultByUser(Users $user): Companies |
||
410 | { |
||
411 | //verify the user has a default company |
||
412 | $defaultCompany = UserConfig::findFirst([ |
||
413 | 'conditions' => 'users_id = ?0 and name = ?1', |
||
414 | 'bind' => [$user->getId(), self::DEFAULT_COMPANY], |
||
415 | ]); |
||
416 | |||
417 | //found it |
||
418 | if (is_object($defaultCompany)) { |
||
419 | return self::findFirst($defaultCompany->value); |
||
420 | } |
||
421 | |||
422 | //second try |
||
423 | $defaultCompany = UsersAssociatedCompany::findFirst([ |
||
424 | 'conditions' => 'users_id = ?0 and user_active =?1', |
||
425 | 'bind' => [$user->getId(), 1], |
||
426 | ]); |
||
427 | |||
428 | if (is_object($defaultCompany)) { |
||
429 | return self::findFirst($defaultCompany->companies_id); |
||
430 | } |
||
431 | |||
432 | throw new Exception(_("User doesn't have an active company")); |
||
433 | } |
||
434 | |||
435 | /** |
||
436 | * After the model was update we need to update its custom fields |
||
437 | * |
||
438 | * @return void |
||
439 | */ |
||
440 | 3 | public function afterUpdate() |
|
441 | { |
||
442 | //only clean and change custom fields if they are been sent |
||
443 | 3 | if (!empty($this->customFields)) { |
|
444 | //replace old custom with new |
||
445 | 1 | $allCustomFields = $this->getAllCustomFields(); |
|
446 | 1 | if (is_array($allCustomFields)) { |
|
447 | 1 | foreach ($this->customFields as $key => $value) { |
|
448 | 1 | $allCustomFields[$key] = $value; |
|
449 | } |
||
450 | } |
||
451 | |||
452 | 1 | if (!empty($allCustomFields)) { |
|
453 | //set |
||
454 | 1 | $this->setCustomFields($allCustomFields); |
|
455 | //clean old |
||
456 | 1 | $this->cleanCustomFields($this->getId()); |
|
457 | //save new |
||
458 | 1 | $this->saveCustomFields(); |
|
459 | } |
||
460 | } |
||
461 | 3 | } |
|
462 | |||
463 | /** |
||
464 | * Start a free trial for a new company |
||
465 | * |
||
466 | * @return string //the customer id |
||
467 | */ |
||
468 | 3 | public function startFreeTrial() : ?string |
|
469 | { |
||
470 | 3 | $defaultPlan = AppsPlans::getDefaultPlan(); |
|
471 | 3 | $trialEndsAt = Carbon::now()->addDays($this->di->getApp()->plan->free_trial_dates); |
|
472 | |||
473 | //Lets create a new default subscription without payment method |
||
474 | 3 | $this->user->newSubscription($defaultPlan->name, $defaultPlan->stripe_id, $this, $this->di->getApp()) |
|
475 | 3 | ->trialDays($defaultPlan->free_trial_dates) |
|
476 | 3 | ->create(); |
|
477 | |||
478 | //ook for the subscription and update the missing info |
||
479 | 3 | $subscription = $this->subscription; |
|
480 | 3 | $subscription->apps_plans_id = $this->di->getApp()->default_apps_plan_id; |
|
481 | 3 | $subscription->trial_ends_days = $trialEndsAt->diffInDays(Carbon::now()); |
|
482 | 3 | $subscription->is_freetrial = 1; |
|
483 | 3 | $subscription->is_active = 1; |
|
484 | |||
485 | 3 | if (!$subscription->save()) { |
|
486 | throw new ServerErrorHttpException((string)'Subscription for new company couldnt be created ' . current($this->getMessages())); |
||
487 | } |
||
488 | |||
489 | 3 | return $this->user->stripe_id; |
|
490 | } |
||
491 | } |
||
492 |