forked from hiqdev/hipanel-module-stock
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModelGridView.php
More file actions
140 lines (132 loc) · 4.86 KB
/
ModelGridView.php
File metadata and controls
140 lines (132 loc) · 4.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php declare(strict_types=1);
/*
* Stock Module for Hipanel
*
* @link https://github.com/hiqdev/hipanel-module-stock
* @package hipanel-module-stock
* @license BSD-3-Clause
* @copyright Copyright (c) 2015-2016, HiQDev (http://hiqdev.com/)
*/
namespace hipanel\modules\stock\grid;
use hipanel\grid\ActionColumn;
use hipanel\grid\BoxedGridView;
use hipanel\grid\RefColumn;
use hipanel\modules\stock\helpers\StockLocationsProvider;
use hipanel\modules\stock\models\Model;
use Yii;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\web\User;
class ModelGridView extends BoxedGridView
{
public function __construct(
private readonly StockLocationsProvider $locationsProvider,
private readonly User $user,
$config = []
)
{
parent::__construct($config);
}
public function columns()
{
return array_merge(parent::columns(), [
'type' => [
'filterOptions' => ['class' => 'narrow-filter'],
'class' => RefColumn::class,
'gtype' => 'type,model',
'i18nDictionary' => 'hipanel.stock.order',
'value' => function ($model) {
return $model->type_label;
},
],
'state' => [
'class' => RefColumn::class,
'gtype' => 'state,model',
'i18nDictionary' => 'hipanel.stock.order',
'value' => function ($model) {
return $model->state_label;
},
],
'brand' => [
'filterOptions' => ['class' => 'narrow-filter'],
'class' => RefColumn::class,
'gtype' => 'type,brand',
'i18nDictionary' => 'hipanel.stock.order',
'value' => function ($model) {
return $model->brand_label;
},
],
'model' => [
'filterAttribute' => 'model_like',
'filterOptions' => ['class' => 'narrow-filter'],
],
'partno' => [
'enableSorting' => false,
'filterAttribute' => 'partno_like',
'format' => 'raw',
'value' => function (Model $model) {
return Html::a(Html::encode($model->partno), [
'@model/view',
'id' => $model->id,
], ['class' => 'text-bold']);
},
],
'descr' => [
'enableSorting' => false,
'filterAttribute' => 'descr_like',
],
'last_prices' => [
'label' => Yii::t('hipanel:stock', 'Last price'),
'enableSorting' => false,
'filter' => false,
'format' => 'raw',
'value' => function (Model $model) {
return $model->showModelPrices($model->last_prices);
},
'visible' => $this->user->can('move.read-all'),
],
'model_group' => [
'label' => Yii::t('hipanel:stock', 'Group'),
'enableSorting' => false,
'filter' => false,
'format' => 'raw',
'value' => function (Model $model) {
$group = Html::encode($model->group);
return Html::a($group, ['@model-group/view', 'id' => $model->group_id], [
'title' => $group,
'style' => 'display: inline-block; width: 120px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;',
]);
},
],
'actions' => [
'class' => ActionColumn::class,
'template' => '{view} {update}',
'header' => Yii::t('hipanel', 'Actions'),
],
], $this->generateStockColumns());
}
private function generateStockColumns(): array
{
$result = [];
$locations = ArrayHelper::index($this->locationsProvider->getAllLocations(), 'id');
foreach ($this->locationsProvider->getLocations() as $key) {
$location = $locations[$key];
$icon = Html::tag('span', null, [
'class' => "fa fa-fw " . $location->icon,
]
);
$label = $location->label;
$result[$key] = [
'attribute' => $key,
'label' => implode(' ', [$icon, $label]),
'headerOptions' => ['title' => $location->type->value, 'style' => 'white-space: nowrap;'],
'encodeLabel' => false,
'enableSorting' => false,
'filter' => false,
'format' => 'raw',
'value' => fn(Model $model) => $model->renderReserves($key),
];
}
return $result;
}
}