|
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\helpers\Url; |
|
16
|
|
|
use hipanel\modules\finance\helpers\CurrencyFilter; |
|
17
|
|
|
use hipanel\modules\finance\logic\bill\QuantityFormatterFactoryInterface; |
|
18
|
|
|
use hipanel\modules\finance\menus\BillActionsMenu; |
|
19
|
|
|
use hipanel\modules\finance\models\Bill; |
|
20
|
|
|
use hipanel\modules\finance\models\Charge; |
|
21
|
|
|
use hipanel\modules\finance\widgets\BillType; |
|
22
|
|
|
use hipanel\modules\finance\widgets\BillTypeFilter; |
|
23
|
|
|
use hipanel\modules\finance\widgets\ColoredBalance; |
|
24
|
|
|
use hipanel\modules\finance\widgets\LinkToObjectResolver; |
|
25
|
|
|
use hipanel\widgets\ArraySpoiler; |
|
26
|
|
|
use hiqdev\yii2\menus\grid\MenuColumn; |
|
27
|
|
|
use Yii; |
|
28
|
|
|
use yii\helpers\Html; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Class BillGridView. |
|
32
|
|
|
* |
|
33
|
|
|
* @author Dmytro Naumenko <[email protected]> |
|
34
|
|
|
*/ |
|
35
|
|
|
class BillGridView extends \hipanel\grid\BoxedGridView |
|
36
|
|
|
{ |
|
37
|
|
|
/** |
|
38
|
|
|
* @var QuantityFormatterFactoryInterface |
|
39
|
|
|
*/ |
|
40
|
|
|
private $quantityFactory; |
|
41
|
|
|
|
|
42
|
|
|
public function __construct(QuantityFormatterFactoryInterface $quantityFactory, array $config = []) |
|
43
|
|
|
{ |
|
44
|
|
|
parent::__construct($config); |
|
45
|
|
|
|
|
46
|
|
|
$this->quantityFactory = $quantityFactory; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public $currencies = []; |
|
50
|
|
|
|
|
51
|
|
|
public function columns() |
|
52
|
|
|
{ |
|
53
|
|
|
return array_merge(parent::columns(), [ |
|
54
|
|
|
'bill' => [ |
|
55
|
|
|
'class' => MainColumn::class, |
|
56
|
|
|
'attribute' => 'bill', |
|
57
|
|
|
'filterAttribute' => 'bill_like', |
|
58
|
|
|
], |
|
59
|
|
|
'time' => [ |
|
60
|
|
|
'format' => 'html', |
|
61
|
|
|
'filter' => false, |
|
62
|
|
|
'label' => Yii::t('hipanel', 'Time'), |
|
63
|
|
|
'sortAttribute' => 'time', |
|
64
|
|
|
'contentOptions' => ['class' => 'text-nowrap'], |
|
65
|
|
|
'value' => function (Bill $model) { |
|
66
|
|
|
list($date, $time) = explode(' ', $model->time, 2); |
|
|
|
|
|
|
67
|
|
|
|
|
68
|
|
|
return in_array($model->gtype, [ |
|
|
|
|
|
|
69
|
|
|
'discount', |
|
70
|
|
|
'domain', |
|
71
|
|
|
'monthly', |
|
72
|
|
|
'overuse', |
|
73
|
|
|
'premium_package', |
|
74
|
|
|
'feature', |
|
75
|
|
|
'intercept', |
|
76
|
|
|
'periodic', |
|
77
|
|
|
], true) && $time === '00:00:00' |
|
78
|
|
|
? Yii::$app->formatter->asDate($date, 'LLLL y') |
|
79
|
|
|
: Yii::$app->formatter->asDateTime($model->time); |
|
|
|
|
|
|
80
|
|
|
}, |
|
81
|
|
|
], |
|
82
|
|
|
'sum' => [ |
|
83
|
|
|
'class' => CurrencyColumn::class, |
|
84
|
|
|
'attribute' => 'sum', |
|
85
|
|
|
'colors' => ['danger' => 'warning'], |
|
86
|
|
|
'headerOptions' => ['class' => 'text-right'], |
|
87
|
|
|
'contentOptions' => function (Bill $model) { |
|
88
|
|
|
return ['class' => 'text-right' . ($model->sum > 0 ? ' text-bold' : '')]; |
|
|
|
|
|
|
89
|
|
|
}, |
|
90
|
|
|
], |
|
91
|
|
|
'sum_editable' => [ |
|
92
|
|
|
'class' => CurrencyColumn::class, |
|
93
|
|
|
'format' => 'raw', |
|
94
|
|
|
'attribute' => 'sum', |
|
95
|
|
|
'colors' => ['danger' => 'warning'], |
|
96
|
|
|
'headerOptions' => ['class' => 'text-right'], |
|
97
|
|
|
'urlCallback' => function ($model, $key) { |
|
|
|
|
|
|
98
|
|
|
return Yii::$app->user->can('bill.read') |
|
99
|
|
|
? Url::to(['@bill/view', 'id' => $model->id]) |
|
100
|
|
|
: null; |
|
101
|
|
|
}, |
|
102
|
|
|
'contentOptions' => function (Bill $model) { |
|
103
|
|
|
return ['class' => 'text-right' . ($model->sum > 0 ? ' text-bold' : '')]; |
|
|
|
|
|
|
104
|
|
|
}, |
|
105
|
|
|
], |
|
106
|
|
|
'quantity' => [ |
|
107
|
|
|
'format' => 'raw', |
|
108
|
|
|
'headerOptions' => ['class' => 'text-right'], |
|
109
|
|
|
'contentOptions' => ['class' => 'text-right text-bold'], |
|
110
|
|
|
'value' => function (Bill $bill) { |
|
111
|
|
|
return $this->formatQuantity($bill); |
|
112
|
|
|
}, |
|
113
|
|
|
], |
|
114
|
|
|
'balance' => [ |
|
115
|
|
|
'attribute' => 'balance', |
|
116
|
|
|
'format' => 'raw', |
|
117
|
|
|
'headerOptions' => ['class' => 'text-right'], |
|
118
|
|
|
'contentOptions' => function ($model, $key, $index) { |
|
119
|
|
|
return ['class' => 'text-right' . ($index ? '' : ' text-bold')]; |
|
120
|
|
|
}, |
|
121
|
|
|
'value' => function (Bill $model) { |
|
122
|
|
|
return ColoredBalance::widget(compact('model')); |
|
123
|
|
|
}, |
|
124
|
|
|
'filterAttribute' => 'currency_in', |
|
125
|
|
|
'filterOptions' => ['class' => 'narrow-filter'], |
|
126
|
|
|
'filter' => function ($column, $filterModel) { |
|
127
|
|
|
$currencies = CurrencyFilter::addSymbolAndFilter($this->currencies); |
|
128
|
|
|
return Html::activeDropDownList($filterModel, 'currency_in', $currencies, ['class' => 'form-control', 'prompt' => '--']); |
|
129
|
|
|
}, |
|
130
|
|
|
], |
|
131
|
|
|
'gtype' => [ |
|
132
|
|
|
'attribute' => 'gtype', |
|
133
|
|
|
], |
|
134
|
|
|
'type_label' => [ |
|
135
|
|
|
'filter' => function ($column, $filterModel) { |
|
136
|
|
|
return BillTypeFilter::widget([ |
|
137
|
|
|
'options' => ['class' => 'form-control text-right', 'style' => 'max-width: 12em'], |
|
138
|
|
|
'attribute' => 'ftype', |
|
139
|
|
|
'model' => $filterModel, |
|
140
|
|
|
]); |
|
141
|
|
|
}, |
|
142
|
|
|
'sortAttribute' => 'type', |
|
143
|
|
|
'format' => 'raw', |
|
144
|
|
|
'headerOptions' => ['class' => 'text-right'], |
|
145
|
|
|
'contentOptions' => function (Bill $model) { |
|
|
|
|
|
|
146
|
|
|
return ['class' => 'text-right']; |
|
147
|
|
|
}, |
|
148
|
|
|
'value' => function (Bill $model) { |
|
149
|
|
|
return BillType::widget([ |
|
150
|
|
|
'model' => $model, |
|
151
|
|
|
'field' => 'ftype', |
|
152
|
|
|
'labelField' => 'type_label', |
|
153
|
|
|
]); |
|
154
|
|
|
}, |
|
155
|
|
|
], |
|
156
|
|
|
'description' => [ |
|
157
|
|
|
'attribute' => 'descr', |
|
158
|
|
|
'format' => 'raw', |
|
159
|
|
|
'value' => function (Bill $model) { |
|
160
|
|
|
$descr = $model->descr ?: $model->label; |
|
|
|
|
|
|
161
|
|
|
$text = mb_strlen($descr) > 70 ? ArraySpoiler::widget(['data' => $descr]) : $descr; |
|
162
|
|
|
$tariff = $model->tariff ? Html::tag('span', |
|
|
|
|
|
|
163
|
|
|
Yii::t('hipanel', 'Tariff') . ': ' . $this->tariffLink($model), ['class' => 'pull-right']) : ''; |
|
164
|
|
|
$amount = $this->formatQuantity($model); |
|
165
|
|
|
$object = $this->objectTag($model); |
|
166
|
|
|
|
|
167
|
|
|
return $tariff . $amount . ' ' . implode('<br>', array_filter([$object, $text])); |
|
168
|
|
|
}, |
|
169
|
|
|
'exportedValue' => function (Bill $model): string { |
|
170
|
|
|
$text = $model->descr ?: $model->label; |
|
|
|
|
|
|
171
|
|
|
$tariff = $model->tariff ? |
|
|
|
|
|
|
172
|
|
|
Html::tag('span', Yii::t('hipanel', 'Tariff') . ': ' . $this->tariffLink($model), ['class' => 'pull-right']) : ''; |
|
173
|
|
|
$amount = $this->formatQuantity($model); |
|
174
|
|
|
$object = $this->objectTag($model); |
|
175
|
|
|
|
|
176
|
|
|
return $tariff . ' ' . $amount . ' ' . implode('<br>', array_filter([$object, $text])); |
|
177
|
|
|
}, |
|
178
|
|
|
], |
|
179
|
|
|
'tariff_link' => [ |
|
180
|
|
|
'attribute' => 'tariff', |
|
181
|
|
|
'format' => 'html', |
|
182
|
|
|
'value' => function (Bill $model) { |
|
183
|
|
|
return $this->tariffLink($model); |
|
184
|
|
|
}, |
|
185
|
|
|
], |
|
186
|
|
|
'object' => [ |
|
187
|
|
|
'attribute' => 'object', |
|
188
|
|
|
'format' => 'html', |
|
189
|
|
|
'value' => function (Bill $model) { |
|
190
|
|
|
return $this->objectTag($model); |
|
191
|
|
|
}, |
|
192
|
|
|
], |
|
193
|
|
|
'actions' => [ |
|
194
|
|
|
'class' => MenuColumn::class, |
|
195
|
|
|
'menuClass' => BillActionsMenu::class, |
|
196
|
|
|
], |
|
197
|
|
|
'common_object_link' => [ |
|
198
|
|
|
'format' => 'html', |
|
199
|
|
|
'value' => function (Charge $model) { |
|
200
|
|
|
$link = LinkToObjectResolver::widget([ |
|
201
|
|
|
'model' => $model->commonObject, |
|
202
|
|
|
'labelAttribute' => 'name', |
|
203
|
|
|
'idAttribute' => 'id', |
|
204
|
|
|
'typeAttribute' => 'type', |
|
205
|
|
|
'customLinks' => [ |
|
206
|
|
|
'part' => '@server/view', |
|
207
|
|
|
], |
|
208
|
|
|
]); |
|
209
|
|
|
|
|
210
|
|
|
return $link; |
|
211
|
|
|
}, |
|
212
|
|
|
], |
|
213
|
|
|
]); |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
public function tariffLink(Bill $model): ?string |
|
217
|
|
|
{ |
|
218
|
|
|
$canSeeLink = Yii::$app->user->can('plan.read'); |
|
219
|
|
|
|
|
220
|
|
|
return $canSeeLink ? Html::a($model->tariff, ['@plan/view', 'id' => $model->tariff_id]) : $model->tariff; |
|
|
|
|
|
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
public function objectTag($model): string |
|
224
|
|
|
{ |
|
225
|
|
|
return $model->object ? implode(': ', [Yii::t('hipanel', $model->class_label), $this->objectLink($model)]) : ''; |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
/** |
|
229
|
|
|
* Creates link to object details page. |
|
230
|
|
|
* |
|
231
|
|
|
* @param Bill $model |
|
232
|
|
|
* @return string |
|
233
|
|
|
*/ |
|
234
|
|
|
public function objectLink(Bill $model): string |
|
235
|
|
|
{ |
|
236
|
|
|
return $model->class === 'device' && Yii::getAlias('@server/view', false) |
|
|
|
|
|
|
237
|
|
|
? Html::a($model->object, ['@server/view', 'id' => $model->object_id]) |
|
|
|
|
|
|
238
|
|
|
: Html::tag('b', $model->object); |
|
|
|
|
|
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
private function formatQuantity(Bill $bill): string |
|
242
|
|
|
{ |
|
243
|
|
|
$billQty = $this->quantityFactory->forBill($bill); |
|
244
|
|
|
|
|
245
|
|
|
if ($billQty !== null) { |
|
246
|
|
|
return Html::tag('nobr', Html::tag('b', $billQty->format())); |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
return ''; |
|
250
|
|
|
} |
|
251
|
|
|
} |
|
252
|
|
|
|
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@propertyannotation 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.