|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace hipanel\modules\server\models; |
|
4
|
|
|
|
|
5
|
|
|
use hipanel\helpers\ArrayHelper; |
|
6
|
|
|
use hipanel\modules\finance\providers\BillTypesProvider; |
|
7
|
|
|
use hipanel\modules\server\helpers\consumptionPriceFormatter\MultiplePrice; |
|
8
|
|
|
use hipanel\modules\server\helpers\consumptionPriceFormatter\PriceFormatterStrategy; |
|
9
|
|
|
use hipanel\modules\server\helpers\consumptionPriceFormatter\SinglePrice; |
|
10
|
|
|
use Yii; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class Consumption |
|
14
|
|
|
* |
|
15
|
|
|
* @property string $type |
|
16
|
|
|
* @property float[] $value |
|
17
|
|
|
* @property float[] $overuse |
|
18
|
|
|
*/ |
|
19
|
|
|
class Consumption extends \hipanel\base\Model |
|
20
|
|
|
{ |
|
21
|
|
|
use \hipanel\base\ModelTrait; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var PriceFormatterStrategy |
|
25
|
|
|
*/ |
|
26
|
|
|
private $priceFormatterStrategy; |
|
27
|
|
|
|
|
28
|
|
|
public function rules() |
|
29
|
|
|
{ |
|
30
|
|
|
return [ |
|
31
|
|
|
[['id', 'object_id'], 'integer'], |
|
32
|
|
|
[['value', 'overuse', 'prices'], 'safe'], |
|
33
|
|
|
[['type', 'limit', 'time', 'unit', 'action_unit', 'currency'], 'string'], |
|
34
|
|
|
[['price'], 'number'], |
|
35
|
|
|
]; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Get type label |
|
40
|
|
|
* @return string |
|
41
|
|
|
* @throws \yii\base\InvalidConfigException |
|
42
|
|
|
*/ |
|
43
|
|
|
public function getTypeLabel(): string |
|
44
|
|
|
{ |
|
45
|
|
|
$provider = Yii::createObject(BillTypesProvider::class); |
|
46
|
|
|
$types = ArrayHelper::index($provider->getTypes(), 'name'); |
|
47
|
|
|
if (!isset($types[$this->type])) { |
|
48
|
|
|
return '--'; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
return $types[$this->type]->label; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function getCurrentValue(): ?string |
|
55
|
|
|
{ |
|
56
|
|
|
return $this->value[$this->getCurrent()]; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function getCurrentOveruse(): ?string |
|
60
|
|
|
{ |
|
61
|
|
|
return $this->overuse[$this->getCurrent()]; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function getPreviousValue(): ?string |
|
65
|
|
|
{ |
|
66
|
|
|
return $this->value[$this->getPrevious()]; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function getPreviousOveruse(): ?string |
|
70
|
|
|
{ |
|
71
|
|
|
return $this->overuse[$this->getPrevious()]; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
private function getCurrent(): string |
|
75
|
|
|
{ |
|
76
|
|
|
return date('m'); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
private function getPrevious(): string |
|
80
|
|
|
{ |
|
81
|
|
|
return date('m', strtotime('-1month')); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function getFormattedPrice(): string |
|
85
|
|
|
{ |
|
86
|
|
|
if ($this->prices) { |
|
|
|
|
|
|
87
|
|
|
$this->setPriceFormatterStrategy(new MultiplePrice()); |
|
88
|
|
|
} else { |
|
89
|
|
|
$this->setPriceFormatterStrategy(new SinglePrice()); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
return $this->priceFormatterStrategy->showPrice($this); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @param PriceFormatterStrategy $priceFormatterStrategy |
|
97
|
|
|
* @return Consumption |
|
98
|
|
|
*/ |
|
99
|
|
|
private function setPriceFormatterStrategy(PriceFormatterStrategy $priceFormatterStrategy): Consumption |
|
100
|
|
|
{ |
|
101
|
|
|
$this->priceFormatterStrategy = $priceFormatterStrategy; |
|
102
|
|
|
|
|
103
|
|
|
return $this; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|