|
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
|
|
|
use yii\i18n\Formatter; |
|
18
|
|
|
use yii\web\User; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Class PricePresenter contains methods that present price properties. |
|
22
|
|
|
* You can override this class to add custom presentations support. |
|
23
|
|
|
* |
|
24
|
|
|
* @author Dmytro Naumenko <[email protected]> |
|
25
|
|
|
*/ |
|
26
|
|
|
class PricePresenter |
|
27
|
|
|
{ |
|
28
|
|
|
protected Formatter $formatter; |
|
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
protected User $user; |
|
31
|
|
|
|
|
32
|
|
|
protected string $priceAttribute = 'price'; |
|
33
|
|
|
|
|
34
|
|
|
public function __construct(Formatter $formatter, User $user) |
|
35
|
|
|
{ |
|
36
|
|
|
$this->formatter = $formatter; |
|
37
|
|
|
$this->user = $user; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param string $priceAttribute |
|
42
|
|
|
* @return $this |
|
43
|
|
|
*/ |
|
44
|
|
|
public function setPriceAttribute(string $priceAttribute): self |
|
45
|
|
|
{ |
|
46
|
|
|
$this->priceAttribute = $priceAttribute; |
|
47
|
|
|
|
|
48
|
|
|
return $this; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param Price $price |
|
53
|
|
|
* @throws \yii\base\InvalidConfigException |
|
54
|
|
|
* @return string |
|
55
|
|
|
*/ |
|
56
|
|
|
public function renderPrice(Price $price): string |
|
57
|
|
|
{ |
|
58
|
|
|
$unit = $formula = ''; |
|
59
|
|
|
if ($price->getUnitLabel()) { |
|
60
|
|
|
$unit = ' ' . Yii::t('hipanel:finance', 'per {unit}', ['unit' => $price->getUnitLabel()]); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$activeFormulas = array_filter($price->getFormulaLines(), fn ($el) => $el['is_actual']); |
|
64
|
|
|
if (!empty($activeFormulas)) { |
|
65
|
|
|
$formula = ArraySpoiler::widget([ |
|
66
|
|
|
'data' => $activeFormulas, |
|
67
|
|
|
'formatter' => function ($v) { |
|
68
|
|
|
return Html::tag('kbd', $v['formula'], ['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
|
|
|
* @param string $attribute |
|
88
|
|
|
* @return string |
|
89
|
|
|
*/ |
|
90
|
|
|
public function renderInfo(Price $price, string $attribute = 'quantity'): string |
|
91
|
|
|
{ |
|
92
|
|
|
if (!$price->isQuantityPredefined()) { |
|
93
|
|
|
return Yii::t('hipanel:finance', '{icon} Quantity: {quantity}', [ |
|
94
|
|
|
'icon' => Html::tag('i', '', ['class' => 'fa fa-calculator']), |
|
95
|
|
|
'quantity' => Html::tag('b', '<i class="fa fa-spin fa-refresh"></i>', ['data-dynamic-quantity' => true]), |
|
96
|
|
|
]); |
|
97
|
|
|
} |
|
98
|
|
|
if ($price->isOveruse()) { |
|
99
|
|
|
return Yii::t('hipanel:finance', '{coins} {amount,number} {unit}{aggregated}', [ |
|
100
|
|
|
'coins' => Html::tag('i', '', ['class' => 'fa fa-money', 'title' => Yii::t('hipanel.finance.price', 'Prepaid amount')]), |
|
101
|
|
|
'amount' => $price->{$attribute}, |
|
102
|
|
|
'unit' => $price->getUnitLabel(), |
|
103
|
|
|
'aggregated' => $price->hasAttribute('count_aggregated_traffic') && $price->count_aggregated_traffic |
|
104
|
|
|
? Html::tag('span', Yii::t('hipanel.finance.price', 'Aggregated'), ['class' => 'label bg-olive pull-right']) |
|
105
|
|
|
: '', |
|
106
|
|
|
]); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
if ($price->getSubtype() === 'hardware' && $this->user->can('part.read')) { |
|
110
|
|
|
return $price->object->label; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
return ''; // Do not display any information unless we are sure what we are displaying |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|