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\models; |
||
12 | |||
13 | use hipanel\helpers\ArrayHelper; |
||
14 | use hipanel\modules\finance\providers\BillTypesProvider; |
||
15 | use hipanel\modules\server\helpers\consumptionPriceFormatter\MultiplePrice; |
||
16 | use hipanel\modules\server\helpers\consumptionPriceFormatter\PriceFormatterStrategy; |
||
17 | use hipanel\modules\server\helpers\consumptionPriceFormatter\SinglePrice; |
||
18 | use Yii; |
||
19 | |||
20 | /** |
||
21 | * Class Consumption. |
||
22 | * |
||
23 | * @property string $type |
||
24 | * @property float[] $value |
||
25 | * @property float[] $overuse |
||
26 | */ |
||
27 | class Consumption extends \hipanel\base\Model |
||
28 | { |
||
29 | use \hipanel\base\ModelTrait; |
||
30 | |||
31 | /** |
||
32 | * @var PriceFormatterStrategy |
||
33 | */ |
||
34 | private $priceFormatterStrategy; |
||
35 | |||
36 | public function rules() |
||
37 | { |
||
38 | return [ |
||
39 | [['id', 'object_id'], 'integer'], |
||
40 | [['value', 'overuse', 'prices'], 'safe'], |
||
41 | [['type', 'limit', 'time', 'unit', 'action_unit', 'currency'], 'string'], |
||
42 | [['price'], 'number'], |
||
43 | ]; |
||
44 | } |
||
45 | |||
46 | /** |
||
47 | * Get type label. |
||
48 | * @throws \yii\base\InvalidConfigException |
||
49 | * @return string |
||
50 | */ |
||
51 | public function getTypeLabel(): string |
||
52 | { |
||
53 | $provider = Yii::createObject(BillTypesProvider::class); |
||
54 | $provider->keepUnusedTypes(); |
||
55 | $types = ArrayHelper::index($provider->getTypes(), 'name'); |
||
56 | if (!isset($types[$this->type])) { |
||
57 | return '--'; |
||
58 | } |
||
59 | |||
60 | return $types[$this->type]->label; |
||
61 | } |
||
62 | |||
63 | public function getCurrentValue(): ?string |
||
64 | { |
||
65 | return $this->value[$this->getCurrent()]; |
||
66 | } |
||
67 | |||
68 | public function getCurrentOveruse(): ?string |
||
69 | { |
||
70 | return $this->overuse[$this->getCurrent()]; |
||
71 | } |
||
72 | |||
73 | public function getPreviousValue(): ?string |
||
74 | { |
||
75 | return $this->value[$this->getPrevious()]; |
||
76 | } |
||
77 | |||
78 | public function getPreviousOveruse(): ?string |
||
79 | { |
||
80 | return $this->overuse[$this->getPrevious()]; |
||
81 | } |
||
82 | |||
83 | private function getCurrent(): string |
||
84 | { |
||
85 | return date('m'); |
||
86 | } |
||
87 | |||
88 | private function getPrevious(): string |
||
89 | { |
||
90 | return date('m', strtotime('-1month')); |
||
91 | } |
||
92 | |||
93 | public function getFormattedPrice(): string |
||
94 | { |
||
95 | if ($this->prices) { |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
96 | $this->setPriceFormatterStrategy(new MultiplePrice()); |
||
97 | } else { |
||
98 | $this->setPriceFormatterStrategy(new SinglePrice()); |
||
99 | } |
||
100 | |||
101 | return $this->priceFormatterStrategy->showPrice($this); |
||
102 | } |
||
103 | |||
104 | public function hasFormattedAttributes(): bool |
||
105 | { |
||
106 | return $this->limit || ($this->price || $this->prices); |
||
0 ignored issues
–
show
The property
prices does not exist on hipanel\modules\server\models\Consumption . Since you implemented __get , consider adding a @property annotation.
Loading history...
The property
price does not exist on hipanel\modules\server\models\Consumption . Since you implemented __get , consider adding a @property annotation.
Loading history...
The property
limit does not exist on hipanel\modules\server\models\Consumption . Since you implemented __get , consider adding a @property annotation.
Loading history...
|
|||
107 | } |
||
108 | |||
109 | /** |
||
110 | * @param PriceFormatterStrategy $priceFormatterStrategy |
||
111 | * @return Consumption |
||
112 | */ |
||
113 | private function setPriceFormatterStrategy(PriceFormatterStrategy $priceFormatterStrategy): Consumption |
||
114 | { |
||
115 | $this->priceFormatterStrategy = $priceFormatterStrategy; |
||
116 | |||
117 | return $this; |
||
118 | } |
||
119 | } |
||
120 |