1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Finance module for HiPanel |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/hiqdev/hipanel-module-finance |
6
|
|
|
* @package hipanel-module-finance |
7
|
|
|
* @license BSD-3-Clause |
8
|
|
|
* @copyright Copyright (c) 2015-2019, HiQDev (http://hiqdev.com/) |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace hipanel\modules\finance\grid\presenters\price; |
12
|
|
|
|
13
|
|
|
use hipanel\modules\finance\models\Price; |
14
|
|
|
use hipanel\widgets\ArraySpoiler; |
15
|
|
|
use Yii; |
16
|
|
|
use yii\bootstrap\Html; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class PricePresenter contains methods that present price properties. |
20
|
|
|
* You can override this class to add custom presentations support. |
21
|
|
|
* |
22
|
|
|
* @author Dmytro Naumenko <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
class PricePresenter |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var \yii\i18n\Formatter |
28
|
|
|
*/ |
29
|
|
|
private $formatter; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
private $priceAttribute = 'price'; |
35
|
|
|
|
36
|
|
|
public function __construct() |
37
|
|
|
{ |
38
|
|
|
$this->formatter = Yii::$app->formatter; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param string $priceAttribute |
43
|
|
|
* @return $this |
44
|
|
|
*/ |
45
|
|
|
public function setPriceAttribute(string $priceAttribute): self |
46
|
|
|
{ |
47
|
|
|
$this->priceAttribute = $priceAttribute; |
48
|
|
|
|
49
|
|
|
return $this; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param Price $price |
54
|
|
|
* @throws \yii\base\InvalidConfigException |
55
|
|
|
* @return string |
56
|
|
|
*/ |
57
|
|
|
public function renderPrice(Price $price): string |
58
|
|
|
{ |
59
|
|
|
$unit = $formula = ''; |
60
|
|
View Code Duplication |
if ($price->getUnitLabel()) { |
|
|
|
|
61
|
|
|
$unit = ' ' . Yii::t('hipanel:finance', 'per {unit}', ['unit' => $price->getUnitLabel()]); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if (\count($price->formulaLines()) > 0) { |
65
|
|
|
$formula = ArraySpoiler::widget([ |
66
|
|
|
'data' => $price->formulaLines(), |
67
|
|
|
'formatter' => function ($v) { |
68
|
|
|
return Html::tag('kbd', $v, ['class' => 'javascript']); |
69
|
|
|
}, |
70
|
|
|
'visibleCount' => 0, |
71
|
|
|
'delimiter' => '<br />', |
72
|
|
|
'button' => [ |
73
|
|
|
'label' => '∑', |
74
|
|
|
'popoverOptions' => [ |
75
|
|
|
'placement' => 'bottom', |
76
|
|
|
'html' => true, |
77
|
|
|
], |
78
|
|
|
], |
79
|
|
|
]); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return Html::tag('strong', $this->formatter->asCurrency($price->{$this->priceAttribute}, $price->currency)) . $unit . $formula; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param Price $price |
87
|
|
|
* @return string |
88
|
|
|
*/ |
89
|
|
|
public function renderInfo(Price $price): string |
90
|
|
|
{ |
91
|
|
|
if (!$price->isQuantityPredefined()) { |
92
|
|
|
return Yii::t('hipanel:finance', '{icon} Quantity: {quantity}', [ |
93
|
|
|
'icon' => Html::tag('i', '', ['class' => 'fa fa-calculator']), |
94
|
|
|
'quantity' => Html::tag('b', '<i class="fa fa-spin fa-refresh"></i>', ['data-dynamic-quantity' => true]), |
95
|
|
|
]); |
96
|
|
|
} |
97
|
|
|
if ($price->isOveruse()) { |
98
|
|
|
return Yii::t('hipanel:finance', '{coins} {amount,number} {unit}', [ |
99
|
|
|
'coins' => Html::tag('i', '', ['class' => 'fa fa-money', 'title' => Yii::t('hipanel.finance.price', 'Prepaid amount')]), |
100
|
|
|
'amount' => $price->quantity, |
101
|
|
|
'unit' => $price->getUnitLabel(), |
102
|
|
|
]); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
if ($price->getSubtype() === 'hardware') { |
106
|
|
|
return $price->object->label; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return ''; // Do not display any information unless we are sure what we are displaying |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.