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\ModelException; |
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
|
12 |
|
public function initialize() |
81
|
|
|
{ |
82
|
12 |
|
parent::initialize(); |
83
|
|
|
|
84
|
12 |
|
$this->setSource('companies'); |
85
|
|
|
|
86
|
12 |
|
$this->belongsTo( |
87
|
12 |
|
'users_id', |
88
|
12 |
|
'Gewaer\Models\Users', |
89
|
12 |
|
'id', |
90
|
12 |
|
['alias' => 'user'] |
91
|
|
|
); |
92
|
|
|
|
93
|
12 |
|
$this->hasMany( |
94
|
12 |
|
'id', |
95
|
12 |
|
'Gewaer\Models\CompanyBranches', |
96
|
12 |
|
'company_id', |
97
|
12 |
|
['alias' => 'branches'] |
98
|
|
|
); |
99
|
|
|
|
100
|
12 |
|
$this->hasOne( |
101
|
12 |
|
'id', |
102
|
12 |
|
'Gewaer\Models\CompanyBranches', |
103
|
12 |
|
'company_id', |
104
|
|
|
[ |
105
|
12 |
|
'alias' => 'branch', |
106
|
|
|
] |
107
|
|
|
); |
108
|
|
|
|
109
|
12 |
|
$this->hasOne( |
110
|
12 |
|
'id', |
111
|
12 |
|
'Gewaer\Models\UserCompanyApps', |
112
|
12 |
|
'company_id', |
113
|
|
|
[ |
114
|
12 |
|
'alias' => 'app', |
115
|
|
|
'params' => [ |
116
|
12 |
|
'conditions' => 'apps_id = '.$this->di->getApp()->getId() |
117
|
|
|
] |
118
|
|
|
] |
119
|
|
|
); |
120
|
12 |
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Model validation |
124
|
|
|
* |
125
|
|
|
* @return void |
126
|
|
|
*/ |
127
|
4 |
|
public function validation() |
128
|
|
|
{ |
129
|
4 |
|
$validator = new Validation(); |
130
|
|
|
|
131
|
4 |
|
$validator->add( |
132
|
4 |
|
'name', |
133
|
4 |
|
new PresenceOf([ |
134
|
4 |
|
'model' => $this, |
135
|
|
|
'required' => true, |
136
|
|
|
]) |
137
|
|
|
); |
138
|
|
|
|
139
|
4 |
|
return $this->validate($validator); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Returns table name mapped in the model. |
144
|
|
|
* |
145
|
|
|
* @return string |
146
|
|
|
*/ |
147
|
12 |
|
public function getSource() : string |
148
|
|
|
{ |
149
|
12 |
|
return 'companies'; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* After creating the company |
154
|
|
|
* |
155
|
|
|
* @return void |
156
|
|
|
*/ |
157
|
2 |
|
public function afterCreate() |
158
|
|
|
{ |
159
|
2 |
|
parent::afterCreate(); |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @var CompanyBranches |
163
|
|
|
*/ |
164
|
2 |
|
$branch = new CompanyBranches(); |
165
|
2 |
|
$branch->company_id = $this->getId(); |
166
|
2 |
|
$branch->users_id = $this->user->getId(); |
167
|
2 |
|
$branch->name = 'Default'; |
168
|
2 |
|
$branch->is_default = 1; |
169
|
2 |
|
$branch->description = ''; |
170
|
2 |
|
if (!$branch->save()) { |
171
|
|
|
throw new ModelException((string)current($branch->getMessages())); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
//assign default branch to the user |
175
|
2 |
|
if (empty($this->user->default_company_branch)) { |
176
|
2 |
|
$this->user->default_company_branch = $branch->getId(); |
177
|
2 |
|
$this->user->update(); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
//look for the default plan for this app |
181
|
2 |
|
$companyApps = new UserCompanyApps(); |
182
|
2 |
|
$companyApps->company_id = $this->getId(); |
183
|
2 |
|
$companyApps->apps_id = $this->di->getApp()->getId(); |
184
|
|
|
|
185
|
|
|
//we need to assign this company to a plan |
186
|
2 |
|
if (empty($this->appPlanId)) { |
187
|
2 |
|
$plan = AppsPlans::getDefaultPlan(); |
188
|
2 |
|
$companyApps->stripe_id = $plan->stripe_id; |
189
|
|
|
} |
190
|
|
|
|
191
|
2 |
|
$companyApps->subscriptions_id = 0; |
192
|
2 |
|
$companyApps->created_at = date('Y-m-d H:i:s'); |
193
|
2 |
|
$companyApps->is_deleted = 0; |
194
|
|
|
|
195
|
2 |
|
if (!$companyApps->save()) { |
196
|
|
|
throw new ModelException((string)current($companyApps->getMessages())); |
197
|
|
|
} |
198
|
2 |
|
} |
199
|
|
|
} |
200
|
|
|
|