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
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class Companies |
12
|
|
|
* |
13
|
|
|
* @package Gewaer\Models |
14
|
|
|
* |
15
|
|
|
* @property Users $user |
16
|
|
|
* @property CompanyBranches $branch |
17
|
|
|
* @property CompanyBranches $branches |
18
|
|
|
* @property Config $config |
19
|
|
|
* @property UserCompanyApps $app |
20
|
|
|
* @property \Phalcon\Di $di |
21
|
|
|
*/ |
22
|
|
|
class Companies extends \Baka\Auth\Models\Companies |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* |
26
|
|
|
* @var integer |
27
|
|
|
*/ |
28
|
|
|
public $id; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
public $name; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
public $profile_image; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
public $website; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* |
50
|
|
|
* @var integer |
51
|
|
|
*/ |
52
|
|
|
public $users_id; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* |
56
|
|
|
* @var integer |
57
|
|
|
*/ |
58
|
|
|
public $has_activities; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* |
62
|
|
|
* @var string |
63
|
|
|
*/ |
64
|
|
|
public $created_at; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* |
68
|
|
|
* @var string |
69
|
|
|
*/ |
70
|
|
|
public $updated_at; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* |
74
|
|
|
* @var integer |
75
|
|
|
*/ |
76
|
|
|
public $is_deleted; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Provide the app plan id |
80
|
|
|
* |
81
|
|
|
* @var integer |
82
|
|
|
*/ |
83
|
|
|
public $appPlanId = null; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Initialize method for model. |
87
|
|
|
*/ |
88
|
25 |
|
public function initialize() |
89
|
|
|
{ |
90
|
25 |
|
parent::initialize(); |
91
|
|
|
|
92
|
25 |
|
$this->setSource('companies'); |
93
|
|
|
|
94
|
25 |
|
$this->belongsTo( |
95
|
25 |
|
'users_id', |
96
|
25 |
|
'Gewaer\Models\Users', |
97
|
25 |
|
'id', |
98
|
25 |
|
['alias' => 'user'] |
99
|
|
|
); |
100
|
|
|
|
101
|
25 |
|
$this->hasMany( |
102
|
25 |
|
'id', |
103
|
25 |
|
'Gewaer\Models\CompanyBranches', |
104
|
25 |
|
'company_id', |
105
|
25 |
|
['alias' => 'branches'] |
106
|
|
|
); |
107
|
|
|
|
108
|
25 |
|
$this->hasMany( |
109
|
25 |
|
'id', |
110
|
25 |
|
'Gewaer\Models\UsersAssociatedCompany', |
111
|
25 |
|
'company_id', |
112
|
25 |
|
['alias' => 'UsersAssociatedCompany'] |
113
|
|
|
); |
114
|
|
|
|
115
|
25 |
|
$this->hasOne( |
116
|
25 |
|
'id', |
117
|
25 |
|
'Gewaer\Models\CompanyBranches', |
118
|
25 |
|
'company_id', |
119
|
|
|
[ |
120
|
25 |
|
'alias' => 'branch', |
121
|
|
|
] |
122
|
|
|
); |
123
|
|
|
|
124
|
25 |
|
$this->hasOne( |
125
|
25 |
|
'id', |
126
|
25 |
|
'Gewaer\Models\UserCompanyApps', |
127
|
25 |
|
'company_id', |
128
|
|
|
[ |
129
|
25 |
|
'alias' => 'app', |
130
|
|
|
'params' => [ |
131
|
25 |
|
'conditions' => 'apps_id = ' . $this->di->getApp()->getId() |
132
|
|
|
] |
133
|
|
|
] |
134
|
|
|
); |
135
|
|
|
|
136
|
25 |
|
$this->hasOne( |
137
|
25 |
|
'id', |
138
|
25 |
|
'Gewaer\Models\UserCompanyApps', |
139
|
25 |
|
'company_id', |
140
|
|
|
[ |
141
|
25 |
|
'alias' => 'apps', |
142
|
|
|
'params' => [ |
143
|
25 |
|
'conditions' => 'apps_id = ' . $this->di->getApp()->getId() |
144
|
|
|
] |
145
|
|
|
] |
146
|
|
|
); |
147
|
|
|
|
148
|
25 |
|
$this->hasOne( |
149
|
25 |
|
'id', |
150
|
25 |
|
'Gewaer\Models\Subscription', |
151
|
25 |
|
'company_id', |
152
|
|
|
[ |
153
|
25 |
|
'alias' => 'subscription', |
154
|
|
|
'params' => [ |
155
|
25 |
|
'conditions' => 'apps_id = ' . $this->di->getApp()->getId() . ' AND ends_at is null AND is_deleted = 0 ', |
156
|
25 |
|
'order' => 'id DESC' |
157
|
|
|
] |
158
|
|
|
] |
159
|
|
|
); |
160
|
|
|
|
161
|
25 |
|
$this->hasMany( |
162
|
25 |
|
'id', |
163
|
25 |
|
'Gewaer\Models\Subscription', |
164
|
25 |
|
'company_id', |
165
|
|
|
[ |
166
|
25 |
|
'alias' => 'subscriptions', |
167
|
|
|
'params' => [ |
168
|
25 |
|
'conditions' => 'apps_id = ' . $this->di->getApp()->getId() . ' AND is_deleted = 0', |
169
|
25 |
|
'order' => 'id DESC' |
170
|
|
|
] |
171
|
|
|
] |
172
|
|
|
); |
173
|
25 |
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Model validation |
177
|
|
|
* |
178
|
|
|
* @return void |
179
|
|
|
*/ |
180
|
4 |
|
public function validation() |
181
|
|
|
{ |
182
|
4 |
|
$validator = new Validation(); |
183
|
|
|
|
184
|
4 |
|
$validator->add( |
185
|
4 |
|
'name', |
186
|
4 |
|
new PresenceOf([ |
187
|
4 |
|
'model' => $this, |
188
|
|
|
'required' => true, |
189
|
|
|
]) |
190
|
|
|
); |
191
|
|
|
|
192
|
4 |
|
return $this->validate($validator); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Returns table name mapped in the model. |
197
|
|
|
* |
198
|
|
|
* @return string |
199
|
|
|
*/ |
200
|
17 |
|
public function getSource() : string |
201
|
|
|
{ |
202
|
17 |
|
return 'companies'; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Confirm if a user belongs to this current company |
207
|
|
|
* |
208
|
|
|
* @param Users $user |
209
|
|
|
* @return boolean |
210
|
|
|
*/ |
211
|
|
|
public function userAssociatedToCompany(Users $user): bool |
212
|
|
|
{ |
213
|
|
|
return is_object($this->getUsersAssociatedCompany('users_id =' . $user->getId())) ? true : false; |
|
|
|
|
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* After creating the company |
218
|
|
|
* |
219
|
|
|
* @return void |
220
|
|
|
*/ |
221
|
2 |
|
public function afterCreate() |
222
|
|
|
{ |
223
|
2 |
|
parent::afterCreate(); |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @var CompanyBranches |
227
|
|
|
*/ |
228
|
2 |
|
$branch = new CompanyBranches(); |
229
|
2 |
|
$branch->company_id = $this->getId(); |
230
|
2 |
|
$branch->users_id = $this->user->getId(); |
231
|
2 |
|
$branch->name = 'Default'; |
232
|
2 |
|
$branch->is_default = 1; |
233
|
2 |
|
$branch->description = ''; |
234
|
2 |
|
if (!$branch->save()) { |
235
|
|
|
throw new ServerErrorHttpException((string)current($branch->getMessages())); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
//look for the default plan for this app |
239
|
2 |
|
$companyApps = new UserCompanyApps(); |
240
|
2 |
|
$companyApps->company_id = $this->getId(); |
241
|
2 |
|
$companyApps->apps_id = $this->di->getApp()->getId(); |
242
|
2 |
|
$companyApps->subscriptions_id = 0; |
243
|
|
|
|
244
|
|
|
//we need to assign this company to a plan |
245
|
2 |
|
if (empty($this->appPlanId)) { |
246
|
2 |
|
$plan = AppsPlans::getDefaultPlan(); |
247
|
2 |
|
$companyApps->stripe_id = $plan->stripe_id; |
248
|
|
|
} |
249
|
|
|
|
250
|
2 |
|
$companyApps->created_at = date('Y-m-d H:i:s'); |
251
|
2 |
|
$companyApps->is_deleted = 0; |
252
|
|
|
|
253
|
2 |
|
if (!$companyApps->save()) { |
254
|
|
|
throw new ServerErrorHttpException((string)current($companyApps->getMessages())); |
255
|
|
|
} |
256
|
2 |
|
} |
257
|
|
|
} |
258
|
|
|
|