|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace hipanel\modules\finance\models\proxy; |
|
4
|
|
|
|
|
5
|
|
|
use hipanel\base\Model; |
|
6
|
|
|
use hipanel\base\ModelTrait; |
|
7
|
|
|
use hipanel\modules\finance\helpers\ResourceConfigurator; |
|
8
|
|
|
use hipanel\modules\finance\models\decorators\DecoratedInterface; |
|
9
|
|
|
use hipanel\modules\finance\models\decorators\ResourceDecoratorInterface; |
|
10
|
|
|
use hiqdev\php\units\Quantity; |
|
11
|
|
|
use hiqdev\php\units\Unit; |
|
12
|
|
|
use yii\db\QueryInterface; |
|
13
|
|
|
use Yii; |
|
14
|
|
|
|
|
15
|
|
|
class Resource extends Model implements DecoratedInterface |
|
16
|
|
|
{ |
|
17
|
|
|
use ModelTrait; |
|
18
|
|
|
|
|
19
|
|
|
private DecoratedInterface $resourceModel; |
|
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
public static function tableName(): string |
|
22
|
|
|
{ |
|
23
|
|
|
return 'use'; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @return array|array[] |
|
28
|
|
|
*/ |
|
29
|
|
|
public function rules() |
|
30
|
|
|
{ |
|
31
|
|
|
return [ |
|
32
|
|
|
[['id', 'object_id', 'type_id'], 'integer'], |
|
33
|
|
|
[['last', 'total'], 'number'], |
|
34
|
|
|
[['type', 'aggregation', 'unit'], 'string'], |
|
35
|
|
|
[['time_from', 'time_till', 'date'], 'datetime', 'format' => 'php:Y-m-d'], |
|
36
|
|
|
]; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function buildResourceModel(ResourceConfigurator $configurator): DecoratedInterface |
|
40
|
|
|
{ |
|
41
|
|
|
if (!isset($this->resourceModel)) { |
|
42
|
|
|
$this->resourceModel = $configurator->getResourceModel([ |
|
43
|
|
|
'type' => $this->type, |
|
44
|
|
|
'unit' => $this->unit, |
|
45
|
|
|
'quantity' => $this->getAmount(), |
|
46
|
|
|
]); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
return $this; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* {@inheritdoc} |
|
54
|
|
|
* @return QueryInterface |
|
55
|
|
|
*/ |
|
56
|
|
|
public static function find(array $options = []): QueryInterface |
|
57
|
|
|
{ |
|
58
|
|
|
return new ResourceQuery(get_called_class(), [ |
|
59
|
|
|
'options' => $options, |
|
60
|
|
|
]); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function getAmount() |
|
64
|
|
|
{ |
|
65
|
|
|
if (in_array($this->type, $this->getBandwidthTypes(), true)) { |
|
66
|
|
|
return $this->last; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
return $this->total; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function getConvertedAmount(string $from, string $to) |
|
73
|
|
|
{ |
|
74
|
|
|
return Quantity::create(Unit::create($from), $this->getAmount())->convert(Unit::create($to))->getQuantity(); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function getChartAmount() |
|
78
|
|
|
{ |
|
79
|
|
|
if (in_array($this->type, $this->getBandwidthTypes(), true)) { |
|
80
|
|
|
return round($this->getAmount() / (10 ** 6), 2); |
|
81
|
|
|
} |
|
82
|
|
|
if (in_array($this->type, $this->getTrafficTypes(), true)) { |
|
83
|
|
|
return round($this->getAmount() / (10 ** 9), 2); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return $this->getAmount(); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function getDisplayDate(): string |
|
90
|
|
|
{ |
|
91
|
|
|
if ($this->aggregation === 'month') { |
|
92
|
|
|
return Yii::$app->formatter->asDate(strtotime($this->date), 'LLL y'); |
|
93
|
|
|
} |
|
94
|
|
|
if ($this->aggregation === 'week') { |
|
95
|
|
|
return Yii::$app->formatter->asDate(strtotime($this->date), 'dd LLL y'); |
|
96
|
|
|
} |
|
97
|
|
|
if ($this->aggregation === 'day') { |
|
98
|
|
|
return Yii::$app->formatter->asDate(strtotime($this->date), 'dd LLL y'); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
return Yii::$app->formatter->asDate(strtotime($this->date)); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
private function getTrafficTypes(): array |
|
105
|
|
|
{ |
|
106
|
|
|
return ['server_traf_in', 'server_traf_max', 'server_traf']; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
private function getBandwidthTypes(): array |
|
110
|
|
|
{ |
|
111
|
|
|
return ['server_traf95_in', 'server_traf95_max', 'server_traf95']; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
public function decorator(): ResourceDecoratorInterface |
|
115
|
|
|
{ |
|
116
|
|
|
return $this->resourceModel->decorator(); |
|
117
|
|
|
} |
|
118
|
|
|
} |