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