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
|
|
|
class Companies extends \Baka\Auth\Models\Companies |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* |
14
|
|
|
* @var integer |
15
|
|
|
*/ |
16
|
|
|
public $id; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
public $name; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
public $profile_image; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
public $website; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* |
38
|
|
|
* @var integer |
39
|
|
|
*/ |
40
|
|
|
public $users_id; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
public $created_at; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* |
50
|
|
|
* @var string |
51
|
|
|
*/ |
52
|
|
|
public $updated_at; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* |
56
|
|
|
* @var integer |
57
|
|
|
*/ |
58
|
|
|
public $is_deleted; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Provide the app plan id |
62
|
|
|
* |
63
|
|
|
* @var integer |
64
|
|
|
*/ |
65
|
|
|
public $appPlanId = null; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Initialize method for model. |
69
|
|
|
*/ |
70
|
6 |
|
public function initialize() |
71
|
|
|
{ |
72
|
6 |
|
parent::initialize(); |
73
|
|
|
|
74
|
6 |
|
$this->setSource('companies'); |
75
|
|
|
|
76
|
6 |
|
$this->belongsTo( |
77
|
6 |
|
'users_id', |
78
|
6 |
|
'Gewaer\Models\Users', |
79
|
6 |
|
'id', |
80
|
6 |
|
['alias' => 'user'] |
81
|
|
|
); |
82
|
|
|
|
83
|
6 |
|
$this->hasMany( |
84
|
6 |
|
'id', |
85
|
6 |
|
'Gewaer\Models\CompanyBranches', |
86
|
6 |
|
'company_id', |
87
|
6 |
|
['alias' => 'branches'] |
88
|
|
|
); |
89
|
6 |
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Model validation |
93
|
|
|
* |
94
|
|
|
* @return void |
95
|
|
|
*/ |
96
|
2 |
|
public function validation() |
97
|
|
|
{ |
98
|
2 |
|
$validator = new Validation(); |
99
|
|
|
|
100
|
2 |
|
$validator->add( |
101
|
2 |
|
'name', |
102
|
2 |
|
new PresenceOf([ |
103
|
2 |
|
'model' => $this, |
104
|
|
|
'required' => true, |
105
|
|
|
]) |
106
|
|
|
); |
107
|
|
|
|
108
|
2 |
|
return $this->validate($validator); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Returns table name mapped in the model. |
113
|
|
|
* |
114
|
|
|
* @return string |
115
|
|
|
*/ |
116
|
6 |
|
public function getSource() : string |
117
|
|
|
{ |
118
|
6 |
|
return 'companies'; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* After creating the company |
123
|
|
|
* |
124
|
|
|
* @return void |
125
|
|
|
*/ |
126
|
1 |
|
public function afterCreate() |
127
|
|
|
{ |
128
|
1 |
|
parent::afterCreate(); |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @var CompanyBranches |
132
|
|
|
*/ |
133
|
1 |
|
$branch = new CompanyBranches(); |
134
|
1 |
|
$branch->company_id = $this->getId(); |
135
|
1 |
|
$branch->users_id = $this->user->getId(); |
|
|
|
|
136
|
1 |
|
$branch->name = 'Default'; |
137
|
1 |
|
$branch->description = ''; |
|
|
|
|
138
|
1 |
|
if (!$branch->save()) { |
139
|
|
|
throw new ModelException((string) current($branch->getMessages())); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
//assign default branch to the user |
143
|
1 |
|
if (empty($this->user->default_company_branch)) { |
144
|
1 |
|
$this->user->default_company_branch = $branch->getId(); |
|
|
|
|
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
//we need to assign this company to a plan |
148
|
1 |
|
if (empty($this->appPlanId)) { |
149
|
1 |
|
$plan = AppsPlans::getDefaultPlan(); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
//look for the default plan for this app |
153
|
|
|
$companyApps = new UserCompanyApps(); |
154
|
|
|
$companyApps->company_id = $this->getId(); |
155
|
|
|
$companyApps->apps_id = $this->di->getApp()->getId(); |
|
|
|
|
156
|
|
|
$companyApps->stripe_id = $plan->stripe_id; |
|
|
|
|
157
|
|
|
$companyApps->subscriptions_id = 0; |
158
|
|
|
|
159
|
|
|
if (!$companyApps->save()) { |
160
|
|
|
throw new ModelException((string) current($companyApps->getMessages())); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|