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; |
12
|
|
|
|
13
|
|
|
use hipanel\grid\CurrencyColumn; |
14
|
|
|
use hipanel\grid\MainColumn; |
15
|
|
|
use hipanel\modules\client\grid\ClientColumn; |
16
|
|
|
use hipanel\modules\finance\logic\bill\QuantityFormatterFactoryInterface; |
17
|
|
|
use hipanel\modules\finance\models\Charge; |
18
|
|
|
use hipanel\modules\finance\models\ChargeSearch; |
19
|
|
|
use hipanel\modules\finance\widgets\BillType; |
20
|
|
|
use hipanel\modules\finance\widgets\BillTypeFilter; |
21
|
|
|
use hipanel\modules\finance\widgets\LinkToObjectResolver; |
22
|
|
|
use hiqdev\higrid\DataColumn; |
23
|
|
|
use Yii; |
24
|
|
|
use yii\helpers\Html; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Class ChargeGridView. |
28
|
|
|
* |
29
|
|
|
* @author Dmytro Naumenko <[email protected]> |
30
|
|
|
*/ |
31
|
|
|
class ChargeGridView extends \hipanel\grid\BoxedGridView |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* @var QuantityFormatterFactoryInterface |
35
|
|
|
*/ |
36
|
|
|
private $formatterFactory; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* ChargeGridView constructor. |
40
|
|
|
* |
41
|
|
|
* @param QuantityFormatterFactoryInterface $formatterFactory |
42
|
|
|
* @param array $config |
43
|
|
|
*/ |
44
|
|
|
public function __construct(QuantityFormatterFactoryInterface $formatterFactory, array $config = []) |
45
|
|
|
{ |
46
|
|
|
parent::__construct($config); |
47
|
|
|
$this->formatterFactory = $formatterFactory; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function columns() |
51
|
|
|
{ |
52
|
|
|
return array_merge(parent::columns(), [ |
53
|
|
|
'label' => [ |
54
|
|
|
'attribute' => 'label_ilike', |
55
|
|
|
'sortAttribute' => 'label_ilike', |
56
|
|
|
'label' => Yii::t('hipanel', 'Description'), |
57
|
|
|
'value' => function (Charge $model): string { |
58
|
|
|
return $model->label ?? ''; |
|
|
|
|
59
|
|
|
}, |
60
|
|
|
], |
61
|
|
|
'tariff' => [ |
62
|
|
|
'attribute' => 'tariff_id', |
63
|
|
|
'label' => Yii::t('hipanel', 'Plan'), |
64
|
|
|
'filter' => false, |
65
|
|
|
'format' => 'html', |
66
|
|
|
'value' => function (Charge $model): string { |
67
|
|
|
return $this->tariffLink($model); |
68
|
|
|
}, |
69
|
|
|
], |
70
|
|
|
'type_label' => [ |
71
|
|
|
'label' => Yii::t('hipanel', 'Type'), |
72
|
|
|
'format' => 'raw', |
73
|
|
|
'value' => function (Charge $model) { |
74
|
|
|
return BillType::widget([ |
75
|
|
|
'model' => $model, |
76
|
|
|
'field' => 'ftype', |
77
|
|
|
'labelField' => 'type_label', |
78
|
|
|
]); |
79
|
|
|
}, |
80
|
|
|
'filterAttribute' => 'type', |
81
|
|
|
'filter' => function (DataColumn $column, ChargeSearch $filterModel): string { |
82
|
|
|
return BillTypeFilter::widget([ |
83
|
|
|
'options' => ['class' => 'form-control text-right', 'style' => 'max-width: 12em'], |
84
|
|
|
'attribute' => 'ftype', |
85
|
|
|
'model' => $filterModel, |
86
|
|
|
]); |
87
|
|
|
}, |
88
|
|
|
], |
89
|
|
|
'sum' => [ |
90
|
|
|
'class' => CurrencyColumn::class, |
91
|
|
|
'attribute' => 'sum', |
92
|
|
|
'sortAttribute' => 'sum', |
93
|
|
|
'colors' => ['danger' => 'warning'], |
94
|
|
|
'headerOptions' => ['class' => 'text-right'], |
95
|
|
|
'filter' => false, |
96
|
|
|
'contentOptions' => function ($model) { |
97
|
|
|
return ['class' => 'text-right' . ($model->sum > 0 ? ' text-bold' : '')]; |
98
|
|
|
}, |
99
|
|
|
], |
100
|
|
|
'name' => [ |
101
|
|
|
'attribute' => 'name_ilike', |
102
|
|
|
'label' => Yii::t('hipanel', 'Object'), |
103
|
|
|
'format' => 'raw', |
104
|
|
|
'value' => function (Charge $model) { |
105
|
|
|
$result = LinkToObjectResolver::widget([ |
106
|
|
|
'model' => $model, |
107
|
|
|
'idAttribute' => 'object_id', |
108
|
|
|
'typeAttribute' => 'class', |
109
|
|
|
'labelAttribute' => 'name', |
110
|
|
|
]) . ($model->label ? " – $model->label" : ''); |
|
|
|
|
111
|
|
|
|
112
|
|
|
if ($model->commonObject->id !== null && $model->commonObject->id !== $model->latestCommonObject->id) { |
113
|
|
|
$result .= ' ' . Html::tag( |
114
|
|
|
'span', |
115
|
|
|
Yii::t('hipanel:finance', 'Now it is in {objectLink}', [ |
116
|
|
|
'objectLink' => LinkToObjectResolver::widget([ |
117
|
|
|
'model' => $model->latestCommonObject, |
118
|
|
|
'idAttribute' => 'id', |
119
|
|
|
'labelAttribute' => 'name', |
120
|
|
|
'typeAttribute' => 'type', |
121
|
|
|
]), |
122
|
|
|
]), |
123
|
|
|
['class' => 'badge', 'style' => 'background-color: #f89406;'] |
124
|
|
|
); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return $result; |
128
|
|
|
}, |
129
|
|
|
], |
130
|
|
|
'quantity' => [ |
131
|
|
|
'attribute' => 'quantity', |
132
|
|
|
'format' => 'raw', |
133
|
|
|
'filter' => false, |
134
|
|
|
'value' => function (Charge $model) { |
135
|
|
|
return $this->renderQuantity($model); |
136
|
|
|
}, |
137
|
|
|
], |
138
|
|
|
'time' => [ |
139
|
|
|
'format' => 'raw', |
140
|
|
|
'filter' => false, |
141
|
|
|
'contentOptions' => ['class' => 'text-nowrap'], |
142
|
|
|
'value' => function ($model) { |
143
|
|
|
list($date, $time) = explode(' ', $model->time, 2); |
144
|
|
|
|
145
|
|
|
return $model->isMonthly() && $time === '00:00:00' |
146
|
|
|
? Yii::$app->formatter->asDate($date, 'LLLL y') |
147
|
|
|
: Yii::$app->formatter->asDateTime($model->time); |
148
|
|
|
}, |
149
|
|
|
], |
150
|
|
|
]); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @param Charge $model |
155
|
|
|
* @return string|null |
156
|
|
|
*/ |
157
|
|
|
public function tariffLink(Charge $model): ?string |
158
|
|
|
{ |
159
|
|
|
$canSeeLink = Yii::$app->user->can('plan.create'); |
160
|
|
|
|
161
|
|
|
return $canSeeLink ? Html::a($model->tariff, ['@plan/view', 'id' => $model->tariff_id]) : $model->tariff; |
|
|
|
|
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @return string |
166
|
|
|
*/ |
167
|
|
|
private function renderQuantity(Charge $charge): string |
168
|
|
|
{ |
169
|
|
|
$formatter = $this->formatterFactory->forCharge($charge); |
170
|
|
|
|
171
|
|
|
if ($formatter !== null) { |
172
|
|
|
return Html::tag('nobr', Html::tag('b', $formatter->format())); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
return ''; |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.