|
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\Url; |
|
16
|
|
|
use hipanel\modules\finance\menus\BillActionsMenu; |
|
17
|
|
|
use hipanel\modules\finance\widgets\BillTypeFilter; |
|
18
|
|
|
use hipanel\widgets\ArraySpoiler; |
|
19
|
|
|
use hiqdev\yii2\menus\grid\MenuColumn; |
|
20
|
|
|
use Yii; |
|
21
|
|
|
use yii\helpers\Html; |
|
22
|
|
|
|
|
23
|
|
|
class BillGridView extends \hipanel\grid\BoxedGridView |
|
24
|
|
|
{ |
|
25
|
|
|
public static function defaultColumns() |
|
26
|
|
|
{ |
|
27
|
|
|
return [ |
|
28
|
|
|
'bill' => [ |
|
29
|
|
|
'class' => MainColumn::class, |
|
30
|
|
|
'attribute' => 'bill', |
|
31
|
|
|
'filterAttribute' => 'bill_like', |
|
32
|
|
|
], |
|
33
|
|
|
'time' => [ |
|
34
|
|
|
'format' => 'html', |
|
35
|
|
|
'filter' => false, |
|
36
|
|
|
'contentOptions' => ['class' => 'text-nowrap'], |
|
37
|
|
View Code Duplication |
'value' => function ($model) { |
|
|
|
|
|
|
38
|
|
|
list($date, $time) = explode(' ', $model->time, 2); |
|
39
|
|
|
|
|
40
|
|
|
if (in_array($model->gtype, [ |
|
41
|
|
|
'discount', 'domain', 'monthly', 'overuse', 'premium_package', |
|
42
|
|
|
'feature', 'intercept', 'periodic', |
|
43
|
|
|
], true)) { |
|
44
|
|
|
return Yii::$app->formatter->asDate($date, 'LLLL y'); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
return $time === '00:00:00' ? Yii::$app->formatter->asDate($date) : Yii::$app->formatter->asDateTime($model->time); |
|
48
|
|
|
}, |
|
49
|
|
|
], |
|
50
|
|
|
'sum' => [ |
|
51
|
|
|
'class' => CurrencyColumn::class, |
|
52
|
|
|
'attribute' => 'sum', |
|
53
|
|
|
'colors' => ['danger' => 'warning'], |
|
54
|
|
|
'headerOptions' => ['class' => 'text-right'], |
|
55
|
|
|
'contentOptions' => function ($model) { |
|
56
|
|
|
return ['class' => 'text-right' . ($model->sum > 0 ? ' text-bold' : '')]; |
|
57
|
|
|
}, |
|
58
|
|
|
], |
|
59
|
|
|
'sum_editable' => [ |
|
60
|
|
|
'class' => CurrencyColumn::class, |
|
61
|
|
|
'attribute' => 'sum', |
|
62
|
|
|
'colors' => ['danger' => 'warning'], |
|
63
|
|
|
'headerOptions' => ['class' => 'text-right'], |
|
64
|
|
|
'urlCallback' => function ($model, $key) { |
|
|
|
|
|
|
65
|
|
|
return Yii::$app->user->can('bill.update') ? Url::to(['bill/update', 'id' => $model->id]) : null; |
|
66
|
|
|
}, |
|
67
|
|
|
'contentOptions' => function ($model) { |
|
68
|
|
|
return ['class' => 'text-right' . ($model->sum > 0 ? ' text-bold' : '')]; |
|
69
|
|
|
}, |
|
70
|
|
|
], |
|
71
|
|
|
'quantity' => [ |
|
72
|
|
|
'headerOptions' => ['class' => 'text-right'], |
|
73
|
|
|
'contentOptions' => ['class' => 'text-right text-bold'], |
|
74
|
|
|
], |
|
75
|
|
|
'balance' => [ |
|
76
|
|
|
'class' => CurrencyColumn::class, |
|
77
|
|
|
'headerOptions' => ['class' => 'text-right'], |
|
78
|
|
|
'contentOptions' => function ($model, $key, $index) { |
|
79
|
|
|
return ['class' => 'text-right' . ($index ? '' : ' text-bold')]; |
|
80
|
|
|
}, |
|
81
|
|
|
], |
|
82
|
|
|
'gtype' => [ |
|
83
|
|
|
'attribute' => 'gtype', |
|
84
|
|
|
], |
|
85
|
|
|
'type_label' => [ |
|
86
|
|
|
'filter' => function ($column, $filterModel) { |
|
87
|
|
|
return BillTypeFilter::widget([ |
|
88
|
|
|
'options' => ['class' => 'form-control text-right'], |
|
89
|
|
|
'attribute' => 'ftype', |
|
90
|
|
|
'model' => $filterModel, |
|
91
|
|
|
]); |
|
92
|
|
|
}, |
|
93
|
|
|
'format' => 'raw', |
|
94
|
|
|
'headerOptions' => ['class' => 'text-right'], |
|
95
|
|
|
'contentOptions' => function ($model) { |
|
|
|
|
|
|
96
|
|
|
return ['class' => 'text-right']; |
|
97
|
|
|
}, |
|
98
|
|
View Code Duplication |
'value' => function ($model) { |
|
|
|
|
|
|
99
|
|
|
static $colors = [ |
|
100
|
|
|
'correction' => 'normal', |
|
101
|
|
|
'exchange' => 'warning', |
|
102
|
|
|
'deposit' => 'success', |
|
103
|
|
|
]; |
|
104
|
|
|
$color = $colors[$model->gtype] ?: 'muted'; |
|
105
|
|
|
|
|
106
|
|
|
return Html::tag('b', Yii::t('hipanel:finance', $model->type_label), ['class' => "text-$color"]); |
|
107
|
|
|
}, |
|
108
|
|
|
], |
|
109
|
|
|
'description' => [ |
|
110
|
|
|
'attribute' => 'descr', |
|
111
|
|
|
'format' => 'raw', |
|
112
|
|
|
'value' => function ($model) { |
|
113
|
|
|
$descr = $model->descr ?: $model->label; |
|
114
|
|
|
$text = mb_strlen($descr) > 70 ? ArraySpoiler::widget(['data' => $descr]) : $descr; |
|
115
|
|
|
$tariff = $model->tariff ? Html::tag('span', |
|
116
|
|
|
Yii::t('hipanel', 'Tariff') . ': ' . Html::a($model->tariff, |
|
117
|
|
|
['@tariff/view', 'id' => $model->tariff_id]), ['class' => 'pull-right']) : ''; |
|
118
|
|
|
$amount = static::billQuantity($model); |
|
119
|
|
|
$object = static::objectTag($model); |
|
120
|
|
|
|
|
121
|
|
|
return $tariff . $amount . ' ' . implode('<br>', array_filter([$object, $text])); |
|
122
|
|
|
}, |
|
123
|
|
|
], |
|
124
|
|
|
'tariff_link' => [ |
|
125
|
|
|
'attribute' => 'tariff', |
|
126
|
|
|
'format' => 'html', |
|
127
|
|
|
'value' => function ($model) { |
|
128
|
|
|
return static::tariffLink($model); |
|
129
|
|
|
}, |
|
130
|
|
|
], |
|
131
|
|
|
'object' => [ |
|
132
|
|
|
'attribute' => 'object', |
|
133
|
|
|
'format' => 'html', |
|
134
|
|
|
'value' => function ($model) { |
|
135
|
|
|
return static::objectTag($model); |
|
136
|
|
|
}, |
|
137
|
|
|
], |
|
138
|
|
|
'actions' => [ |
|
139
|
|
|
'class' => MenuColumn::class, |
|
140
|
|
|
'menuClass' => BillActionsMenu::class, |
|
141
|
|
|
], |
|
142
|
|
|
]; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
public static function tariffLink($model) |
|
146
|
|
|
{ |
|
147
|
|
|
return Html::a($model->tariff, ['@tariff/view', 'id' => $model->tariff_id]); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
public static function objectTag($model) |
|
151
|
|
|
{ |
|
152
|
|
|
return $model->object ? implode(': ', [$model->class_label, static::objectLink($model)]) : ''; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Creates link to object details page. |
|
157
|
|
|
* @param Model $model |
|
158
|
|
|
*/ |
|
159
|
|
|
public static function objectLink($model) |
|
160
|
|
|
{ |
|
161
|
|
|
return $model->class === 'device' |
|
162
|
|
|
? Html::a($model->object, ['@server/view', 'id' => $model->object_id]) |
|
163
|
|
|
: Html::tag('b', $model->object); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* @param Model $model |
|
168
|
|
|
* @return null|string |
|
169
|
|
|
*/ |
|
170
|
|
|
public static function billQuantity($model) |
|
171
|
|
|
{ |
|
172
|
|
|
switch ($model->type) { |
|
173
|
|
|
case 'support_time': |
|
174
|
|
|
$text = Yii::t('hipanel:finance', '{quantity, time, HH:mm} hour(s)', |
|
175
|
|
|
['quantity' => ceil($model->quantity * 3600)]); |
|
176
|
|
|
break; |
|
177
|
|
|
case 'server_traf_max': |
|
178
|
|
|
case 'backup_du': |
|
179
|
|
|
$text = Yii::$app->formatter->asShortSize($model->quantity * 1024 * 1024 * 1024); |
|
180
|
|
|
break; |
|
181
|
|
|
case 'ip_num': |
|
182
|
|
|
$text = Yii::t('hipanel:finance', '{quantity} IP', ['quantity' => $model->quantity]); |
|
183
|
|
|
break; |
|
184
|
|
|
case 'monthly': |
|
185
|
|
|
$days = ceil($model->quantity * date('t', strtotime($model->time))); |
|
186
|
|
|
$text = Yii::t('hipanel:finance', '{quantity, plural, one{# day} other{# days}}', ['quantity' => $days]); |
|
187
|
|
|
break; |
|
188
|
|
|
default: |
|
189
|
|
|
return null; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
return Html::tag('nobr', Html::tag('b', $text)); |
|
193
|
|
|
} |
|
194
|
|
|
} |
|
195
|
|
|
|
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.