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 | use Gewaer\Traits\UsersAssociatedTrait; |
||||
13 | |||||
14 | /** |
||||
15 | * Class Companies |
||||
16 | * |
||||
17 | * @package Gewaer\Models |
||||
18 | * |
||||
19 | * @property Users $user |
||||
20 | * @property Users $userData |
||||
21 | * @property DefaultCompany $default_company |
||||
22 | * @property CompaniesBranches $branch |
||||
23 | * @property CompaniesBranches $branches |
||||
24 | * @property Config $config |
||||
25 | * @property UserCompanyApps $app |
||||
26 | * @property \Phalcon\Di $di |
||||
27 | */ |
||||
28 | class Companies extends \Gewaer\CustomFields\AbstractCustomFieldsModel |
||||
29 | { |
||||
30 | use ModelSettingsTrait; |
||||
31 | use UsersAssociatedTrait; |
||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
32 | |||||
33 | const DEFAULT_COMPANY = 'DefaulCompany'; |
||||
34 | const PAYMENT_GATEWAY_CUSTOMER_KEY = 'payment_gateway_customer_id'; |
||||
35 | |||||
36 | /** |
||||
37 | * |
||||
38 | * @var integer |
||||
39 | */ |
||||
40 | public $id; |
||||
41 | |||||
42 | /** |
||||
43 | * |
||||
44 | * @var string |
||||
45 | */ |
||||
46 | public $name; |
||||
47 | |||||
48 | /** |
||||
49 | * |
||||
50 | * @var string |
||||
51 | */ |
||||
52 | public $profile_image; |
||||
53 | |||||
54 | /** |
||||
55 | * |
||||
56 | * @var string |
||||
57 | */ |
||||
58 | public $website; |
||||
59 | |||||
60 | /** |
||||
61 | * |
||||
62 | * @var integer |
||||
63 | */ |
||||
64 | public $users_id; |
||||
65 | |||||
66 | /** |
||||
67 | * |
||||
68 | * @var integer |
||||
69 | */ |
||||
70 | public $has_activities; |
||||
71 | |||||
72 | /** |
||||
73 | * |
||||
74 | * @var string |
||||
75 | */ |
||||
76 | public $created_at; |
||||
77 | |||||
78 | /** |
||||
79 | * |
||||
80 | * @var string |
||||
81 | */ |
||||
82 | public $updated_at; |
||||
83 | |||||
84 | /** |
||||
85 | * |
||||
86 | * @var integer |
||||
87 | */ |
||||
88 | public $is_deleted; |
||||
89 | |||||
90 | /** |
||||
91 | * Provide the app plan id |
||||
92 | * |
||||
93 | * @var integer |
||||
94 | */ |
||||
95 | public $appPlanId = null; |
||||
96 | |||||
97 | /** |
||||
98 | * |
||||
99 | * @var integer |
||||
100 | */ |
||||
101 | public $currency_id; |
||||
102 | |||||
103 | /** |
||||
104 | * |
||||
105 | * @var string |
||||
106 | */ |
||||
107 | public $language; |
||||
108 | |||||
109 | /** |
||||
110 | * |
||||
111 | * @var string |
||||
112 | */ |
||||
113 | public $timezone; |
||||
114 | |||||
115 | /** |
||||
116 | * |
||||
117 | * @var string |
||||
118 | */ |
||||
119 | public $currency; |
||||
120 | |||||
121 | /** |
||||
122 | * Initialize method for model. |
||||
123 | */ |
||||
124 | 23 | public function initialize() |
|||
125 | { |
||||
126 | 23 | $this->setSource('companies'); |
|||
127 | |||||
128 | 23 | $this->belongsTo('users_id', 'Baka\Auth\Models\Users', 'id', ['alias' => 'user']); |
|||
129 | 23 | $this->hasMany('id', 'Baka\Auth\Models\CompanySettings', 'id', ['alias' => 'settings']); |
|||
130 | |||||
131 | 23 | $this->belongsTo( |
|||
132 | 23 | 'users_id', |
|||
133 | 23 | 'Gewaer\Models\Users', |
|||
134 | 23 | 'id', |
|||
135 | 23 | ['alias' => 'user'] |
|||
136 | ); |
||||
137 | |||||
138 | 23 | $this->hasMany( |
|||
139 | 23 | 'id', |
|||
140 | 23 | 'Gewaer\Models\CompaniesBranches', |
|||
141 | 23 | 'companies_id', |
|||
142 | 23 | ['alias' => 'branches'] |
|||
143 | ); |
||||
144 | |||||
145 | 23 | $this->hasMany( |
|||
146 | 23 | 'id', |
|||
147 | 23 | 'Gewaer\Models\CompaniesCustomFields', |
|||
148 | 23 | 'companies_id', |
|||
149 | 23 | ['alias' => 'fields'] |
|||
150 | ); |
||||
151 | |||||
152 | 23 | $this->hasMany( |
|||
153 | 23 | 'id', |
|||
154 | 23 | 'Gewaer\CustomFields\CustomFields', |
|||
155 | 23 | 'companies_id', |
|||
156 | 23 | ['alias' => 'custom-fields'] |
|||
157 | ); |
||||
158 | |||||
159 | 23 | $this->hasMany( |
|||
160 | 23 | 'id', |
|||
161 | 23 | 'Gewaer\Models\UsersAssociatedCompanies', |
|||
162 | 23 | 'companies_id', |
|||
163 | 23 | ['alias' => 'UsersAssociatedCompanies'] |
|||
164 | ); |
||||
165 | |||||
166 | 23 | $this->hasMany( |
|||
167 | 23 | 'id', |
|||
168 | 23 | 'Gewaer\Models\UsersAssociatedApps', |
|||
169 | 23 | 'companies_id', |
|||
170 | 23 | ['alias' => 'UsersAssociatedApps'] |
|||
171 | ); |
||||
172 | |||||
173 | 23 | $this->hasOne( |
|||
174 | 23 | 'id', |
|||
175 | 23 | 'Gewaer\Models\CompaniesBranches', |
|||
176 | 23 | 'companies_id', |
|||
177 | [ |
||||
178 | 23 | 'alias' => 'branch', |
|||
179 | ] |
||||
180 | ); |
||||
181 | |||||
182 | 23 | $this->hasOne( |
|||
183 | 23 | 'id', |
|||
184 | 23 | 'Gewaer\Models\UserCompanyApps', |
|||
185 | 23 | 'companies_id', |
|||
186 | [ |
||||
187 | 23 | 'alias' => 'app', |
|||
188 | 'params' => [ |
||||
189 | 23 | 'conditions' => 'apps_id = ' . $this->di->getApp()->getId() |
|||
190 | ] |
||||
191 | ] |
||||
192 | ); |
||||
193 | |||||
194 | 23 | $this->hasOne( |
|||
195 | 23 | 'id', |
|||
196 | 23 | 'Gewaer\Models\UserCompanyApps', |
|||
197 | 23 | 'companies_id', |
|||
198 | [ |
||||
199 | 23 | 'alias' => 'apps', |
|||
200 | 'params' => [ |
||||
201 | 23 | 'conditions' => 'apps_id = ' . $this->di->getApp()->getId() |
|||
202 | ] |
||||
203 | ] |
||||
204 | ); |
||||
205 | |||||
206 | 23 | $this->hasOne( |
|||
207 | 23 | 'id', |
|||
208 | 23 | 'Gewaer\Models\Subscription', |
|||
209 | 23 | 'companies_id', |
|||
210 | [ |
||||
211 | 23 | 'alias' => 'subscription', |
|||
212 | 'params' => [ |
||||
213 | 23 | 'conditions' => 'apps_id = ' . $this->di->getApp()->getId() . ' AND ends_at is null AND is_deleted = 0 ', |
|||
214 | 23 | 'order' => 'id DESC' |
|||
215 | ] |
||||
216 | ] |
||||
217 | ); |
||||
218 | |||||
219 | 23 | $this->hasMany( |
|||
220 | 23 | 'id', |
|||
221 | 23 | 'Gewaer\Models\Subscription', |
|||
222 | 23 | 'companies_id', |
|||
223 | [ |
||||
224 | 23 | 'alias' => 'subscriptions', |
|||
225 | 'params' => [ |
||||
226 | 23 | 'conditions' => 'apps_id = ' . $this->di->getApp()->getId() . ' AND is_deleted = 0', |
|||
227 | 23 | 'order' => 'id DESC' |
|||
228 | ] |
||||
229 | ] |
||||
230 | ); |
||||
231 | |||||
232 | 23 | $this->hasMany( |
|||
233 | 23 | 'id', |
|||
234 | 23 | 'Gewaer\Models\UserWebhooks', |
|||
235 | 23 | 'companies_id', |
|||
236 | 23 | ['alias' => 'user-webhooks'] |
|||
237 | ); |
||||
238 | |||||
239 | 23 | $systemModule = SystemModules::getSystemModuleByModelName(self::class); |
|||
240 | 23 | $this->hasMany( |
|||
241 | 23 | 'id', |
|||
242 | 23 | 'Gewaer\Models\FileSystem', |
|||
243 | 23 | 'entity_id', |
|||
244 | [ |
||||
245 | 23 | 'alias' => 'filesystem', |
|||
246 | 23 | 'conditions' => 'system_modules_id = ?0', |
|||
247 | 23 | 'bind' => [$systemModule->getId()] |
|||
248 | ] |
||||
249 | ); |
||||
250 | 23 | } |
|||
251 | |||||
252 | /** |
||||
253 | * Model validation |
||||
254 | * |
||||
255 | * @return void |
||||
256 | */ |
||||
257 | 5 | public function validation() |
|||
258 | { |
||||
259 | 5 | $validator = new Validation(); |
|||
260 | |||||
261 | 5 | $validator->add( |
|||
262 | 5 | 'name', |
|||
263 | 5 | new PresenceOf([ |
|||
264 | 5 | 'model' => $this, |
|||
265 | 'required' => true, |
||||
266 | ]) |
||||
267 | ); |
||||
268 | |||||
269 | 5 | return $this->validate($validator); |
|||
270 | } |
||||
271 | |||||
272 | /** |
||||
273 | * Register a company given a user and name |
||||
274 | * |
||||
275 | * @param Users $user |
||||
276 | * @param string $name |
||||
277 | * @return Companies |
||||
278 | */ |
||||
279 | public static function register(Users $user, string $name): Companies |
||||
280 | { |
||||
281 | $company = new self(); |
||||
282 | $company->name = $name; |
||||
283 | $company->users_id = $user->getId(); |
||||
284 | |||||
285 | if (!$company->save()) { |
||||
286 | throw new Exception(current($company->getMessages())); |
||||
287 | } |
||||
288 | |||||
289 | return $company; |
||||
290 | } |
||||
291 | |||||
292 | /** |
||||
293 | * Returns table name mapped in the model. |
||||
294 | * |
||||
295 | * @return string |
||||
296 | */ |
||||
297 | 15 | public function getSource() : string |
|||
298 | { |
||||
299 | 15 | return 'companies'; |
|||
300 | } |
||||
301 | |||||
302 | /** |
||||
303 | * Confirm if a user belongs to this current company |
||||
304 | * |
||||
305 | * @param Users $user |
||||
306 | * @return boolean |
||||
307 | */ |
||||
308 | 2 | public function userAssociatedToCompany(Users $user): bool |
|||
309 | { |
||||
310 | 2 | return is_object($this->getUsersAssociatedCompanies('users_id =' . $user->getId())) ? true : false; |
|||
1 ignored issue
–
show
The method
getUsersAssociatedCompanies() does not exist on Gewaer\Models\Companies . Since you implemented __call , consider adding a @method annotation.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
311 | } |
||||
312 | |||||
313 | /** |
||||
314 | * Get the stripe customer id from the |
||||
315 | * |
||||
316 | * @return ?string |
||||
317 | */ |
||||
318 | 1 | public function getPaymentGatewayCustomerId(): ?string |
|||
319 | { |
||||
320 | 1 | return $this->getSettings(self::PAYMENT_GATEWAY_CUSTOMER_KEY); |
|||
321 | } |
||||
322 | |||||
323 | /** |
||||
324 | * Before crate company |
||||
325 | * |
||||
326 | * @return void |
||||
327 | */ |
||||
328 | 3 | public function beforeCreate() |
|||
329 | { |
||||
330 | 3 | parent::beforeCreate(); |
|||
331 | |||||
332 | 3 | $this->language = $this->di->getApp()->getSettings('language'); |
|||
333 | 3 | $this->timezone = $this->di->getApp()->getSettings('timezone'); |
|||
334 | 3 | $this->currency_id = Currencies::findFirstByCode($this->di->getApp()->getSettings('currency'))->getId(); |
|||
335 | 3 | } |
|||
336 | |||||
337 | /** |
||||
338 | * After creating the company |
||||
339 | * |
||||
340 | * @return void |
||||
341 | */ |
||||
342 | 3 | public function afterCreate() |
|||
343 | { |
||||
344 | 3 | parent::afterCreate(); |
|||
345 | |||||
346 | //setup the user notificatoin setting |
||||
347 | 3 | $this->setSettings('notifications', $this->user->email); |
|||
348 | |||||
349 | //now thta we setup de company and associated with the user we need to setup this as its default company |
||||
350 | 3 | if (!UserConfig::findFirst(['conditions' => 'users_id = ?0 and name = ?1', 'bind' => [$this->user->getId(), self::DEFAULT_COMPANY]])) { |
|||
351 | 1 | $userConfig = new UserConfig(); |
|||
352 | 1 | $userConfig->users_id = $this->user->getId(); |
|||
353 | 1 | $userConfig->name = self::DEFAULT_COMPANY; |
|||
354 | 1 | $userConfig->value = $this->getId(); |
|||
355 | |||||
356 | 1 | if (!$userConfig->save()) { |
|||
357 | throw new Exception((string)current($userConfig->getMessages())); |
||||
358 | } |
||||
359 | } |
||||
360 | |||||
361 | 3 | $this->associate($this->user, $this); |
|||
362 | 3 | $this->di->getApp()->associate($this->user, $this); |
|||
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 = UsersAssociatedCompanies::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 |