1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace hipanel\modules\finance\forms; |
4
|
|
|
|
5
|
|
|
use hipanel\modules\finance\models\Tariff; |
6
|
|
|
use Yii; |
7
|
|
|
use yii\base\InvalidConfigException; |
8
|
|
|
use yii\helpers\ArrayHelper; |
9
|
|
|
|
10
|
|
|
abstract class AbstractTariffForm extends \yii\base\Model |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var int Tariff ID |
14
|
|
|
*/ |
15
|
|
|
public $id; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var string Tariff name |
19
|
|
|
*/ |
20
|
|
|
public $name; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var int Parent tariff ID |
24
|
|
|
*/ |
25
|
|
|
public $parent_id; |
26
|
|
|
/** @var Tariff */ |
27
|
|
|
public $baseTariff; |
28
|
|
|
/** @var Tariff[] */ |
29
|
|
|
public $baseTariffs; |
30
|
|
|
/** @var Tariff */ |
31
|
|
|
protected $tariff; |
32
|
|
|
/** @var Resource[] */ |
33
|
|
|
protected $_resources; |
34
|
|
|
|
35
|
|
|
public function init() |
36
|
|
|
{ |
37
|
|
|
$this->initTariff(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
protected function initTariff() |
41
|
|
|
{ |
42
|
|
|
$this->selectBaseTariff(); |
43
|
|
|
$this->ensureTariff(); |
44
|
|
|
$this->ensureScenario(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
protected function ensureTariff() |
48
|
|
|
{ |
49
|
|
|
if ($this->getTariff() instanceof Tariff) { |
50
|
|
|
return true; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return $this->setDefaultTariff(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
protected function ensureScenario() |
57
|
|
|
{ |
58
|
|
|
foreach ($this->tariff->resources as $resource) { |
59
|
|
|
$resource->scenario = $this->scenario; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
protected function setDefaultTariff() |
64
|
|
|
{ |
65
|
|
|
$this->setTariff($this->baseTariff); |
66
|
|
|
|
67
|
|
|
return true; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** @inheritdoc */ |
71
|
|
|
public function rules() |
72
|
|
|
{ |
73
|
|
|
return [ |
74
|
|
|
[['name'], 'required', 'on' => ['create', 'update']], |
75
|
|
|
[['parent_id', 'id'], 'integer', 'on' => ['create', 'update']], |
76
|
|
|
[['parent_id'], 'required', 'on' => ['create']], |
77
|
|
|
[['id'], 'required', 'on' => ['update']], |
78
|
|
|
]; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** @inheritdoc */ |
82
|
|
|
public function fields() |
83
|
|
|
{ |
84
|
|
|
return ArrayHelper::merge(array_combine($this->attributes(), $this->attributes()), [ |
85
|
|
|
'resources' => '_resources' |
86
|
|
|
]); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** @inheritdoc */ |
90
|
|
|
public function attributes() |
91
|
|
|
{ |
92
|
|
|
return [ |
93
|
|
|
'id', |
94
|
|
|
'parent_id', |
95
|
|
|
'name', |
96
|
|
|
]; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function getResources() |
100
|
|
|
{ |
101
|
|
|
return $this->_resources; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param array $resources |
106
|
|
|
* @throws InvalidConfigException when not implemented |
107
|
|
|
*/ |
108
|
|
|
public function setResources($resources) |
109
|
|
|
{ |
110
|
|
|
throw new InvalidConfigException("Method load must be implemented"); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function getResourceTypes() |
114
|
|
|
{ |
115
|
|
|
return reset($this->baseTariff->resources)->getAvailableTypes(); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function attributeLabels() |
119
|
|
|
{ |
120
|
|
|
return [ |
121
|
|
|
'name' => Yii::t('hipanel/finance/tariff', 'Name'), |
122
|
|
|
]; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param array $data to be loaded |
127
|
|
|
* @return bool |
128
|
|
|
* @throws InvalidConfigException when not implemented |
129
|
|
|
*/ |
130
|
|
|
public function load($data) |
131
|
|
|
{ |
132
|
|
|
throw new InvalidConfigException("Method load must be implemented"); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function selectBaseTariff() |
136
|
|
|
{ |
137
|
|
|
if (!isset($this->parent_id)) { |
138
|
|
|
$this->parent_id = ArrayHelper::getValue(reset($this->baseTariffs), 'id'); |
|
|
|
|
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
$filtered = array_filter($this->baseTariffs, function ($model) { |
142
|
|
|
return $model->id == $this->parent_id; |
143
|
|
|
}); |
144
|
|
|
|
145
|
|
|
if (count($filtered) !== 1) { |
146
|
|
|
Yii::error('Found ' . count($filtered) . ' base tariffs. Must be exactly one'); |
147
|
|
|
return false; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
$this->baseTariff = reset($filtered); |
151
|
|
|
$this->parent_id = $this->baseTariff->id; |
152
|
|
|
|
153
|
|
|
return true; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
public function insert($runValidation = true, $attributes = null, $options = []) |
|
|
|
|
157
|
|
|
{ |
158
|
|
|
throw new InvalidConfigException("Method load must be implemented"); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
public function update($runValidation = true, $attributes = null, $options = []) |
|
|
|
|
162
|
|
|
{ |
163
|
|
|
throw new InvalidConfigException("Method load must be implemented"); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @return Tariff |
168
|
|
|
*/ |
169
|
|
|
public function getTariff() |
170
|
|
|
{ |
171
|
|
|
return $this->tariff; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
public function setTariff($tariff) |
175
|
|
|
{ |
176
|
|
|
if ($tariff === null) { |
177
|
|
|
return false; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
$this->tariff = $tariff; |
181
|
|
|
|
182
|
|
|
$this->id = $tariff->id; |
183
|
|
|
$this->name = $tariff->name; |
184
|
|
|
|
185
|
|
|
return true; |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|