1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Server module for HiPanel |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/hiqdev/hipanel-module-server |
6
|
|
|
* @package hipanel-module-server |
7
|
|
|
* @license BSD-3-Clause |
8
|
|
|
* @copyright Copyright (c) 2015-2019, HiQDev (http://hiqdev.com/) |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace hipanel\modules\server\widgets; |
12
|
|
|
|
13
|
|
|
use hipanel\modules\finance\logic\bill\QuantityFormatterFactory; |
14
|
|
|
use hipanel\modules\finance\logic\bill\QuantityFormatterFactoryInterface; |
15
|
|
|
use hipanel\modules\server\models\Consumption; |
16
|
|
|
use hipanel\modules\server\models\Server; |
17
|
|
|
use yii\base\Widget; |
18
|
|
|
|
19
|
|
|
class ResourceConsumptionTable extends Widget |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var Server |
23
|
|
|
*/ |
24
|
|
|
public $model; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var QuantityFormatterFactoryInterface|QuantityFormatterFactory |
28
|
|
|
*/ |
29
|
|
|
private $quantityFormatterFactory; |
30
|
|
|
|
31
|
|
|
public function __construct(QuantityFormatterFactoryInterface $quantityFormatterFactory, array $config = []) |
32
|
|
|
{ |
33
|
|
|
parent::__construct($config); |
34
|
|
|
$this->quantityFormatterFactory = $quantityFormatterFactory; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function run(): string |
38
|
|
|
{ |
39
|
|
|
return $this->render('ResourceConsumptionTable', [ |
40
|
|
|
'model' => $this->model, |
41
|
|
|
]); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function getFormatted(Consumption $model, ?string $currentQuantity): string |
45
|
|
|
{ |
46
|
|
|
if (strpos($model->type, 'monthly,') === 0) { |
47
|
|
|
return ''; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
if ($currentQuantity === null) { |
51
|
|
|
return ''; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$consumption = new class($currentQuantity, $model->getAttributes()) extends Consumption { |
55
|
|
|
/** |
56
|
|
|
* @var string |
57
|
|
|
*/ |
58
|
|
|
public $quantity; |
59
|
|
|
|
60
|
|
|
public function __construct(string $quantity, array $config = []) |
61
|
|
|
{ |
62
|
|
|
parent::__construct($config); |
63
|
|
|
|
64
|
|
|
$this->quantity = $quantity; |
65
|
|
|
} |
66
|
|
|
}; |
67
|
|
|
|
68
|
|
|
$formatter = $this->quantityFormatterFactory->forConsumption($consumption); |
69
|
|
|
|
70
|
|
|
if ($formatter === null) { |
71
|
|
|
return ''; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $formatter->format(); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|