1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace hipanel\modules\finance\models; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use yii\helpers\Inflector; |
7
|
|
|
use yii\helpers\StringHelper; |
8
|
|
|
use hipanel\models\Ref; |
9
|
|
|
use hipanel\modules\finance\models\factories\PriceModelFactory; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class Price |
13
|
|
|
* |
14
|
|
|
* @property int $id |
15
|
|
|
* @property int $plan_id |
16
|
|
|
* @property string|int $object_id |
17
|
|
|
* @property string|float $price |
18
|
|
|
* @property string $currency |
19
|
|
|
* @property string|int $main_object_id |
20
|
|
|
* @property string $unit |
21
|
|
|
* @property string $type |
22
|
|
|
* @property string $quantity |
23
|
|
|
* @property string $formula |
24
|
|
|
* |
25
|
|
|
* @property TargetObject $object |
26
|
|
|
* @property Plan $plan |
27
|
|
|
* |
28
|
|
|
* @author Dmytro Naumenko <[email protected]> |
29
|
|
|
*/ |
30
|
|
|
class Price extends \hipanel\base\Model |
31
|
|
|
{ |
32
|
|
|
use \hipanel\base\ModelTrait; |
33
|
|
|
|
34
|
|
|
const SCENARIO_CREATE = 'create'; |
35
|
|
|
const SCENARIO_UPDATE = 'update'; |
36
|
|
|
const SCENARIO_DELETE = 'delete'; |
37
|
|
|
|
38
|
|
|
public function rules() |
39
|
|
|
{ |
40
|
|
|
return array_merge(parent::rules(), [ |
41
|
|
|
[['id', 'parent_id', 'plan_id', 'object_id', 'type_id', 'unit_id', 'currency_id', 'main_object_id'], 'integer'], |
42
|
|
|
[['type', 'plan_name', 'unit', 'currency', 'note', 'data'], 'string'], |
43
|
|
|
[['quantity', 'price'], 'number'], |
44
|
|
|
[['class'], 'string'], // todo: probably, refactor is needed |
45
|
|
|
|
46
|
|
|
[['plan_id', 'type', 'price', 'currency'], 'required', 'on' => ['create', 'update']], |
47
|
|
|
[['id'], 'required', 'on' => ['update', 'set-note', 'delete']], |
48
|
|
|
[['class'], 'default', 'value' => function ($model) { |
|
|
|
|
49
|
|
|
return (new \ReflectionClass($this))->getShortName(); |
50
|
|
|
}], |
51
|
|
|
[['class'], 'string'], |
52
|
|
|
[['formula'], 'string', 'on' => ['create', 'update']] // TODO syn check |
53
|
|
|
]); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function attributeLabels() |
57
|
|
|
{ |
58
|
|
|
return [ |
59
|
|
|
'plan_id' => Yii::t('hipanel:finance', 'Tariff plan'), |
60
|
|
|
'plan' => Yii::t('hipanel:finance', 'Tariff plan'), |
61
|
|
|
'quantity' => Yii::t('hipanel:finance', 'Prepaid'), |
62
|
|
|
'unit' => Yii::t('hipanel:finance', 'Unit'), |
63
|
|
|
'price' => Yii::t('hipanel:finance', 'Price'), |
64
|
|
|
'formula' => Yii::t('hipanel.finance.price', 'Formula'), |
65
|
|
|
'note' => Yii::t('hipanel', 'Note'), |
66
|
|
|
'type' => Yii::t('hipanel', 'Type'), |
67
|
|
|
]; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function getTypeOptions() |
71
|
|
|
{ |
72
|
|
|
return Ref::getList('type,bill', null, [ |
73
|
|
|
'select' => 'oname_label', |
74
|
|
|
'pnames' => 'monthly,overuse', |
75
|
|
|
'with_recursive' => 1, |
76
|
|
|
'mapOptions' => ['from' => 'oname'], |
77
|
|
|
]); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function getUnitOptions() |
81
|
|
|
{ |
82
|
|
|
$unitGroup = [ |
83
|
|
|
'speed' => ['bps', 'kbps', 'mbps', 'gbps', 'tbps'], |
84
|
|
|
'size' => ['mb', 'mb10', 'mb100', 'gb', 'tb'], |
85
|
|
|
]; |
86
|
|
|
|
87
|
|
|
$availableUnitsByPriceType = [ |
88
|
|
|
'overuse,ip_num' => ['items'], |
89
|
|
|
'overuse,support_time' => ['hour'], |
90
|
|
|
'overuse,backup_du' => $unitGroup['size'], |
91
|
|
|
'overuse,server_traf_max' => $unitGroup['size'], |
92
|
|
|
'overuse,server_traf95_max' => $unitGroup['speed'], |
93
|
|
|
'overuse,server_du' => $unitGroup['size'], |
94
|
|
|
'overuse,server_ssd' => $unitGroup['size'], |
95
|
|
|
'overuse,server_sata' => $unitGroup['size'], |
96
|
|
|
]; |
97
|
|
|
|
98
|
|
|
$units = Ref::getList('type,unit', 'hipanel.finance.units', [ |
99
|
|
|
'with_recursive' => 1, |
100
|
|
|
'select' => 'oname_label', |
101
|
|
|
'mapOptions' => ['from' => 'oname'], |
102
|
|
|
]); |
103
|
|
|
|
104
|
|
|
$possibleTypes = $availableUnitsByPriceType[$this->type] ?? []; |
105
|
|
|
return array_intersect_key($units, array_combine($possibleTypes, $possibleTypes)); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function getUnitLabel() |
109
|
|
|
{ |
110
|
|
|
return $this->getUnitOptions()[$this->unit] ?? null; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function getCurrencyOptions() |
114
|
|
|
{ |
115
|
|
|
return Ref::getList('type,currency'); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function getObject() |
119
|
|
|
{ |
120
|
|
|
return $this->hasOne(TargetObject::class, ['id' => 'id']); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function getPlan() |
124
|
|
|
{ |
125
|
|
|
return $this->hasOne(Plan::class, ['id' => 'plan_id']); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public static function tableName() |
129
|
|
|
{ |
130
|
|
|
return Inflector::camel2id(StringHelper::basename(__CLASS__), '-'); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function isOveruse() |
134
|
|
|
{ |
135
|
|
|
return strpos($this->type, 'overuse,') === 0; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function getSubtype() |
139
|
|
|
{ |
140
|
|
|
[, $subtype] = explode(',', $this->type); |
|
|
|
|
141
|
|
|
return $subtype; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* {@inheritdoc} |
146
|
|
|
*/ |
147
|
|
|
public static function instantiate($row) |
148
|
|
|
{ |
149
|
|
|
if (empty($row['class'])) { |
150
|
|
|
return parent::instance($row); |
|
|
|
|
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** @var PriceModelFactory $factory */ |
154
|
|
|
$factory = Yii::$container->get(PriceModelFactory::class); |
155
|
|
|
return $factory->build($row['class']); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
public function formulaLines(): array |
159
|
|
|
{ |
160
|
|
|
if (strlen($this->formula) === 0) { |
161
|
|
|
return []; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
return explode("\n", $this->formula); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.