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