1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Finance module for HiPanel |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/hiqdev/hipanel-module-finance |
6
|
|
|
* @package hipanel-module-finance |
7
|
|
|
* @license BSD-3-Clause |
8
|
|
|
* @copyright Copyright (c) 2015-2019, HiQDev (http://hiqdev.com/) |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace hipanel\modules\finance\forms; |
12
|
|
|
|
13
|
|
|
use hipanel\helpers\ArrayHelper; |
14
|
|
|
use hipanel\modules\finance\models\ServerResource; |
15
|
|
|
use yii\web\UnprocessableEntityHttpException; |
16
|
|
|
|
17
|
|
|
class VdsTariffForm extends AbstractTariffForm |
18
|
|
|
{ |
19
|
|
|
public $note; |
20
|
|
|
public $label; |
21
|
|
|
|
22
|
|
View Code Duplication |
public function load($data, $formName = null) |
23
|
|
|
{ |
24
|
|
|
$this->setAttributes($data[$this->formName()]); |
25
|
|
|
$this->setResources($data[(new ServerResource())->formName()]); |
26
|
|
|
|
27
|
|
|
return true; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
View Code Duplication |
public function rules() |
31
|
|
|
{ |
32
|
|
|
$rules = parent::rules(); |
33
|
|
|
$rules[] = [['note', 'label'], 'safe', 'on' => ['create', 'update']]; |
34
|
|
|
|
35
|
|
|
return $rules; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @return \hipanel\modules\finance\models\ServerResource[] |
40
|
|
|
*/ |
41
|
|
View Code Duplication |
public function getHardwareResources() |
42
|
|
|
{ |
43
|
|
|
/** @var ServerResource[] $resources */ |
44
|
|
|
$resources = array_filter($this->tariff->resources, function ($model) { |
45
|
|
|
/** @var ServerResource $model */ |
46
|
|
|
return $model->isHardwareTypeCorrect(); |
47
|
|
|
}); |
48
|
|
|
|
49
|
|
|
if (empty($resources)) { |
50
|
|
|
return []; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$order = array_keys(reset($resources)->getHardwareTypes()); |
54
|
|
|
|
55
|
|
|
return $this->sortResourcesByDefinedOrder($resources, $order, 'model_type'); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param ServerResource[] $resources |
60
|
|
|
* @param array $order array of ordered values. $resources array will be re-ordered according this order |
61
|
|
|
* @param string $key the key that will be used to re-order |
62
|
|
|
* @return array |
63
|
|
|
*/ |
64
|
|
View Code Duplication |
private function sortResourcesByDefinedOrder($resources, $order, $key) |
65
|
|
|
{ |
66
|
|
|
$result = []; |
67
|
|
|
$resources = ArrayHelper::index($resources, $key); |
68
|
|
|
|
69
|
|
|
foreach ($order as $type) { |
70
|
|
|
if (isset($resources[$type])) { |
71
|
|
|
$result[] = $resources[$type]; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $result; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return \hipanel\modules\finance\models\ServerResource[] |
80
|
|
|
*/ |
81
|
|
View Code Duplication |
public function getOveruseResources() |
82
|
|
|
{ |
83
|
|
|
/** @var ServerResource[] $resources */ |
84
|
|
|
$resources = array_filter($this->tariff->resources, function ($model) { |
85
|
|
|
/** @var ServerResource $model */ |
86
|
|
|
return $model->isTypeCorrect(); |
87
|
|
|
}); |
88
|
|
|
if (empty($resources)) { |
89
|
|
|
return []; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$order = array_keys(reset($resources)->getTypes()); |
93
|
|
|
|
94
|
|
|
return $this->sortResourcesByDefinedOrder($resources, $order, 'type'); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
View Code Duplication |
public function getParentOveruseResource($type_id) |
98
|
|
|
{ |
99
|
|
|
return reset(array_filter($this->parentTariff->resources, function ($resource) use ($type_id) { |
|
|
|
|
100
|
|
|
/** @var ServerResource $resource */ |
101
|
|
|
return strcmp($resource->type_id, $type_id) === 0 && $resource->isTypeCorrect(); |
102
|
|
|
})); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @return \hipanel\modules\finance\models\ServerResource[] |
107
|
|
|
*/ |
108
|
|
View Code Duplication |
public function getParentHardwareResource($object_id) |
109
|
|
|
{ |
110
|
|
|
return reset(array_filter($this->parentTariff->resources, function ($resource) use ($object_id) { |
|
|
|
|
111
|
|
|
/** @var ServerResource $resource */ |
112
|
|
|
return strcmp($resource->object_id, $object_id) === 0 && $resource->isHardwareTypeCorrect(); |
113
|
|
|
})); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** {@inheritdoc} */ |
117
|
|
View Code Duplication |
public function setResources($resources) |
118
|
|
|
{ |
119
|
|
|
$result = []; |
120
|
|
|
foreach ((array) $resources as $resource) { |
121
|
|
|
if ($resource instanceof ServerResource) { |
122
|
|
|
$result[] = $resource; |
123
|
|
|
continue; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
$model = new ServerResource(['scenario' => $this->scenario]); |
127
|
|
|
|
128
|
|
|
if ($model->load($resource, '') && $model->validate()) { |
129
|
|
|
$result[] = $model; |
130
|
|
|
} else { |
131
|
|
|
throw new UnprocessableEntityHttpException('Failed to load resource model: ' . reset($model->getFirstErrors())); |
|
|
|
|
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
$this->_resources = $result; |
136
|
|
|
|
137
|
|
|
return $this; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|