|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace hipanel\modules\finance\grid; |
|
4
|
|
|
|
|
5
|
|
|
use hipanel\grid\BoxedGridView; |
|
6
|
|
|
use hipanel\grid\DataColumn; |
|
7
|
|
|
use hipanel\modules\finance\helpers\ResourceConfigurator; |
|
8
|
|
|
use hipanel\modules\finance\models\proxy\Resource; |
|
9
|
|
|
use hiqdev\yii2\daterangepicker\DateRangePicker; |
|
10
|
|
|
use Yii; |
|
11
|
|
|
use yii\db\ActiveRecordInterface; |
|
12
|
|
|
use yii\helpers\Html; |
|
13
|
|
|
use yii\web\JsExpression; |
|
14
|
|
|
|
|
15
|
|
|
class ResourceGridView extends BoxedGridView |
|
16
|
|
|
{ |
|
17
|
|
|
public ResourceConfigurator $configurator; |
|
|
|
|
|
|
18
|
|
|
|
|
19
|
|
|
public function columns() |
|
20
|
|
|
{ |
|
21
|
|
|
$columns = $this->configurator->getColumns(); |
|
22
|
|
|
$columns['date'] = [ |
|
23
|
|
|
'format' => 'html', |
|
24
|
|
|
'attribute' => 'date', |
|
25
|
|
|
'label' => Yii::t('hipanel', 'Date'), |
|
26
|
|
|
'filter' => DateRangePicker::widget([ |
|
27
|
|
|
'model' => $this->filterModel, |
|
28
|
|
|
'attribute' => 'time_from', |
|
29
|
|
|
'attribute2' => 'time_till', |
|
30
|
|
|
'defaultRanges' => false, |
|
31
|
|
|
'dateFormat' => 'yyyy-MM-dd', |
|
32
|
|
|
'options' => [ |
|
33
|
|
|
'class' => 'form-control', |
|
34
|
|
|
'id' => 'grid_time_range', |
|
35
|
|
|
], |
|
36
|
|
|
'clientOptions' => [ |
|
37
|
|
|
'maxDate' => new JsExpression('moment()'), |
|
38
|
|
|
], |
|
39
|
|
|
'clientEvents' => [ |
|
40
|
|
|
'apply.daterangepicker' => new JsExpression(/** @lang ECMAScript 6 */ "(event, picker) => { |
|
41
|
|
|
const form = $(picker.element[0]).closest('form'); |
|
42
|
|
|
const start = picker.startDate.format('yyyy-MM-dd'.toUpperCase()); |
|
43
|
|
|
const end = picker.endDate.format('yyyy-MM-dd'.toUpperCase()); |
|
44
|
|
|
|
|
45
|
|
|
$('#grid_time_range').val(start + ' - ' + end); |
|
46
|
|
|
form.find(\"input[name*='time_from']\").val(start); |
|
47
|
|
|
form.find(\"input[name*='time_till']\").val(end); |
|
48
|
|
|
$(event.target).change(); |
|
49
|
|
|
}"), |
|
50
|
|
|
'cancel.daterangepicker' => new JsExpression(/** @lang ECMAScript 6 */ "(event, picker) => { |
|
51
|
|
|
const form = $(picker.element[0]).closest('form'); |
|
52
|
|
|
$('#grid_time_range').val(''); |
|
53
|
|
|
form.find(\"input[name*='time_from']\").val(''); |
|
54
|
|
|
form.find(\"input[name*='time_till']\").val(''); |
|
55
|
|
|
$(event.target).change(); |
|
56
|
|
|
}"), |
|
57
|
|
|
], |
|
58
|
|
|
]), |
|
59
|
|
|
]; |
|
60
|
|
|
$columns['type'] = [ |
|
61
|
|
|
'format' => 'html', |
|
62
|
|
|
'attribute' => 'type', |
|
63
|
|
|
'label' => Yii::t('hipanel', 'Type'), |
|
64
|
|
|
'filter' => $this->configurator->getFilterColumns(), |
|
65
|
|
|
'filterInputOptions' => ['class' => 'form-control', 'id' => null, 'prompt' => '---'], |
|
66
|
|
|
'enableSorting' => false, |
|
67
|
|
|
'value' => fn($model): ?string => $columns[$model->type] ?? $model->type, |
|
68
|
|
|
]; |
|
69
|
|
|
$columns['total'] = [ |
|
70
|
|
|
'format' => 'html', |
|
71
|
|
|
'attribute' => 'total', |
|
72
|
|
|
'label' => Yii::t('hipanel', 'Consumed'), |
|
73
|
|
|
'filter' => false, |
|
74
|
|
|
'value' => fn(Resource $resource): ?string => $resource->buildResourceModel($this->configurator)->decorator()->displayAmountWithUnit(), |
|
75
|
|
|
]; |
|
76
|
|
|
|
|
77
|
|
|
return array_merge(parent::columns(), $columns); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public static function getColumns(ResourceConfigurator $configurator): array |
|
81
|
|
|
{ |
|
82
|
|
|
$columns = []; |
|
83
|
|
|
$columns['object'] = [ |
|
84
|
|
|
'format' => 'html', |
|
85
|
|
|
'attribute' => 'name', |
|
86
|
|
|
'label' => Yii::t('hipanel', 'Object'), |
|
87
|
|
|
'contentOptions' => ['style' => 'display: flex; flex-direction: row; justify-content: space-between; flex-wrap: nowrap;'], |
|
88
|
|
|
'value' => function (ActiveRecordInterface $model) use ($configurator): string { |
|
89
|
|
|
$objectLabel = Html::tag('span', '-', ['class' => 'text-danger']); |
|
90
|
|
|
if ($model->name) { |
|
91
|
|
|
$objectLabel = Html::tag('span', $model->name ?: ' ', ['class' => 'text-bold']); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
return $objectLabel . Html::a(Yii::t('hipanel', 'Detail view'), [$configurator->getToObjectUrl(), 'id' => $model->id], ['class' => 'btn btn-default btn-xs']); |
|
95
|
|
|
}, |
|
96
|
|
|
]; |
|
97
|
|
|
$columns[] = 'client_like'; |
|
98
|
|
|
foreach ($configurator->getColumns() as $type => $label) { |
|
99
|
|
|
$columns[$type] = [ |
|
100
|
|
|
'class' => DataColumn::class, |
|
101
|
|
|
'label' => $label, |
|
102
|
|
|
'format' => 'raw', |
|
103
|
|
|
'headerOptions' => ['class' => 'text-center'], |
|
104
|
|
|
'contentOptions' => ['class' => 'text-center', 'data-type' => $type, 'style' => 'white-space:nowrap;'], |
|
105
|
|
|
'value' => static fn() => '<div class="spinner"><div class="rect1"></div><div class="rect2"></div><div class="rect3"></div><div class="rect4"></div><div class="rect5"></div></div>', |
|
106
|
|
|
]; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
return $columns; |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|