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-2017, 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\StringHelper; |
16
|
|
|
use hipanel\helpers\Url; |
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\widgets\BillTypeFilter; |
21
|
|
|
use hipanel\modules\finance\widgets\ColoredBalance; |
22
|
|
|
use hipanel\widgets\ArraySpoiler; |
23
|
|
|
use hiqdev\yii2\menus\grid\MenuColumn; |
24
|
|
|
use Yii; |
25
|
|
|
use yii\helpers\Html; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Class BillGridView |
29
|
|
|
* |
30
|
|
|
* @author Dmytro Naumenko <[email protected]> |
31
|
|
|
*/ |
32
|
|
|
class BillGridView extends \hipanel\grid\BoxedGridView |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* @var QuantityFormatterFactoryInterface |
36
|
|
|
*/ |
37
|
|
|
private $quantityFactory; |
38
|
|
|
|
39
|
|
|
public function __construct(QuantityFormatterFactoryInterface $quantityFactory, array $config = []) |
40
|
|
|
{ |
41
|
|
|
parent::__construct($config); |
42
|
|
|
|
43
|
|
|
$this->quantityFactory = $quantityFactory; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public $currencies = []; |
47
|
|
|
|
48
|
|
|
public function columns() |
49
|
|
|
{ |
50
|
|
|
return array_merge(parent::columns(), [ |
51
|
|
|
'bill' => [ |
52
|
|
|
'class' => MainColumn::class, |
53
|
|
|
'attribute' => 'bill', |
54
|
|
|
'filterAttribute' => 'bill_like', |
55
|
|
|
], |
56
|
|
|
'time' => [ |
57
|
|
|
'format' => 'html', |
58
|
|
|
'filter' => false, |
59
|
|
|
'contentOptions' => ['class' => 'text-nowrap'], |
60
|
|
View Code Duplication |
'value' => function ($model) { |
|
|
|
|
61
|
|
|
list($date, $time) = explode(' ', $model->time, 2); |
62
|
|
|
|
63
|
|
|
if (\in_array($model->gtype, [ |
64
|
|
|
'discount', 'domain', 'monthly', 'overuse', 'premium_package', |
65
|
|
|
'feature', 'intercept', 'periodic', |
66
|
|
|
], true)) { |
67
|
|
|
return Yii::$app->formatter->asDate($date, 'LLLL y'); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $time === '00:00:00' ? Yii::$app->formatter->asDate($date) : Yii::$app->formatter->asDateTime($model->time); |
71
|
|
|
}, |
72
|
|
|
], |
73
|
|
|
'sum' => [ |
74
|
|
|
'class' => CurrencyColumn::class, |
75
|
|
|
'attribute' => 'sum', |
76
|
|
|
'colors' => ['danger' => 'warning'], |
77
|
|
|
'headerOptions' => ['class' => 'text-right'], |
78
|
|
|
'contentOptions' => function ($model) { |
79
|
|
|
return ['class' => 'text-right' . ($model->sum > 0 ? ' text-bold' : '')]; |
80
|
|
|
}, |
81
|
|
|
], |
82
|
|
|
'sum_editable' => [ |
83
|
|
|
'class' => CurrencyColumn::class, |
84
|
|
|
'format' => 'raw', |
85
|
|
|
'attribute' => 'sum', |
86
|
|
|
'colors' => ['danger' => 'warning'], |
87
|
|
|
'headerOptions' => ['class' => 'text-right'], |
88
|
|
|
'urlCallback' => function ($model, $key) { |
|
|
|
|
89
|
|
|
return Yii::$app->user->can('bill.read') |
90
|
|
|
? Url::to(['@bill/view', 'id' => $model->id]) |
91
|
|
|
: null; |
92
|
|
|
}, |
93
|
|
|
'contentOptions' => function ($model) { |
94
|
|
|
return ['class' => 'text-right' . ($model->sum > 0 ? ' text-bold' : '')]; |
95
|
|
|
}, |
96
|
|
|
], |
97
|
|
|
'quantity' => [ |
98
|
|
|
'format' => 'raw', |
99
|
|
|
'headerOptions' => ['class' => 'text-right'], |
100
|
|
|
'contentOptions' => ['class' => 'text-right text-bold'], |
101
|
|
|
'value' => function (Bill $bill) { |
102
|
|
|
return $this->formatQuantity($bill); |
103
|
|
|
} |
104
|
|
|
], |
105
|
|
|
'balance' => [ |
106
|
|
|
'attribute' => 'balance', |
107
|
|
|
'format' => 'raw', |
108
|
|
|
'headerOptions' => ['class' => 'text-right'], |
109
|
|
|
'contentOptions' => function ($model, $key, $index) { |
110
|
|
|
return ['class' => 'text-right' . ($index ? '' : ' text-bold')]; |
111
|
|
|
}, |
112
|
|
|
'value' => function ($model) { |
113
|
|
|
return ColoredBalance::widget(compact('model')); |
114
|
|
|
}, |
115
|
|
|
'filterAttribute' => 'currency_in', |
116
|
|
|
'filterOptions' => ['class' => 'narrow-filter'], |
117
|
|
|
'filter' => function ($column, $filterModel) { |
118
|
|
|
$currencies = array_combine(array_keys($this->currencies), array_map(function ($k) { |
119
|
|
|
return StringHelper::getCurrencySymbol($k); |
120
|
|
|
}, array_keys($this->currencies))); |
121
|
|
|
|
122
|
|
|
return Html::activeDropDownList($filterModel, 'currency_in', $currencies, ['class' => 'form-control', 'prompt' => '--']); |
123
|
|
|
} |
124
|
|
|
], |
125
|
|
|
'gtype' => [ |
126
|
|
|
'attribute' => 'gtype', |
127
|
|
|
], |
128
|
|
|
'type_label' => [ |
129
|
|
|
'filter' => function ($column, $filterModel) { |
130
|
|
|
return BillTypeFilter::widget([ |
131
|
|
|
'options' => ['class' => 'form-control text-right'], |
132
|
|
|
'attribute' => 'ftype', |
133
|
|
|
'model' => $filterModel, |
134
|
|
|
]); |
135
|
|
|
}, |
136
|
|
|
'format' => 'raw', |
137
|
|
|
'headerOptions' => ['class' => 'text-right'], |
138
|
|
|
'contentOptions' => function ($model) { |
|
|
|
|
139
|
|
|
return ['class' => 'text-right']; |
140
|
|
|
}, |
141
|
|
View Code Duplication |
'value' => function ($model) { |
|
|
|
|
142
|
|
|
static $colors = [ |
143
|
|
|
'correction' => 'normal', |
144
|
|
|
'exchange' => 'warning', |
145
|
|
|
'deposit' => 'success', |
146
|
|
|
]; |
147
|
|
|
$color = $colors[$model->gtype] ?: 'muted'; |
148
|
|
|
|
149
|
|
|
return Html::tag('b', Yii::t('hipanel:finance', $model->type_label), ['class' => "text-$color"]); |
150
|
|
|
}, |
151
|
|
|
], |
152
|
|
|
'description' => [ |
153
|
|
|
'attribute' => 'descr', |
154
|
|
|
'format' => 'raw', |
155
|
|
|
'value' => function ($model) { |
156
|
|
|
$descr = $model->descr ?: $model->label; |
157
|
|
|
$text = mb_strlen($descr) > 70 ? ArraySpoiler::widget(['data' => $descr]) : $descr; |
158
|
|
|
$tariff = $model->tariff ? Html::tag('span', |
159
|
|
|
Yii::t('hipanel', 'Tariff') . ': ' . Html::a($model->tariff, |
160
|
|
|
['@tariff/view', 'id' => $model->tariff_id]), ['class' => 'pull-right']) : ''; |
161
|
|
|
$amount = $this->formatQuantity($model); |
162
|
|
|
$object = $this->objectTag($model); |
163
|
|
|
|
164
|
|
|
return $tariff . $amount . ' ' . implode('<br>', array_filter([$object, $text])); |
165
|
|
|
}, |
166
|
|
|
], |
167
|
|
|
'tariff_link' => [ |
168
|
|
|
'attribute' => 'tariff', |
169
|
|
|
'format' => 'html', |
170
|
|
|
'value' => function ($model) { |
171
|
|
|
return $this->tariffLink($model); |
172
|
|
|
}, |
173
|
|
|
], |
174
|
|
|
'object' => [ |
175
|
|
|
'attribute' => 'object', |
176
|
|
|
'format' => 'html', |
177
|
|
|
'value' => function ($model) { |
178
|
|
|
return $this->objectTag($model); |
179
|
|
|
}, |
180
|
|
|
], |
181
|
|
|
'actions' => [ |
182
|
|
|
'class' => MenuColumn::class, |
183
|
|
|
'menuClass' => BillActionsMenu::class, |
184
|
|
|
], |
185
|
|
|
]); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
public function tariffLink($model): string |
189
|
|
|
{ |
190
|
|
|
return Html::a($model->tariff, ['@tariff/view', 'id' => $model->tariff_id]); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
public function objectTag($model): string |
194
|
|
|
{ |
195
|
|
|
return $model->object ? implode(': ', [$model->class_label, $this->objectLink($model)]) : ''; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Creates link to object details page. |
200
|
|
|
* |
201
|
|
|
* @param Bill $model |
202
|
|
|
* @return string |
203
|
|
|
*/ |
204
|
|
|
public function objectLink(Bill $model): string |
205
|
|
|
{ |
206
|
|
|
return $model->class === 'device' |
|
|
|
|
207
|
|
|
? Html::a($model->object, ['@server/view', 'id' => $model->object_id]) |
|
|
|
|
208
|
|
|
: Html::tag('b', $model->object); |
|
|
|
|
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
private function formatQuantity(Bill $bill): string |
212
|
|
|
{ |
213
|
|
|
$billQty = $this->quantityFactory->forBill($bill); |
214
|
|
|
|
215
|
|
|
if ($billQty !== null) { |
216
|
|
|
return Html::tag('nobr', Html::tag('b', $billQty->format())); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
return ''; |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.