|
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\widgets\BillTypeFilter; |
|
17
|
|
|
use hipanel\widgets\ArraySpoiler; |
|
18
|
|
|
use Yii; |
|
19
|
|
|
use yii\helpers\Html; |
|
20
|
|
|
|
|
21
|
|
|
class BillGridView extends \hipanel\grid\BoxedGridView |
|
22
|
|
|
{ |
|
23
|
|
|
public static function defaultColumns() |
|
24
|
|
|
{ |
|
25
|
|
|
return [ |
|
|
|
|
|
|
26
|
|
|
'bill' => [ |
|
27
|
|
|
'class' => MainColumn::class, |
|
28
|
|
|
'attribute' => 'bill', |
|
29
|
|
|
'filterAttribute' => 'bill_like', |
|
30
|
|
|
], |
|
31
|
|
|
'time' => [ |
|
32
|
|
|
'format' => 'html', |
|
33
|
|
|
'filter' => false, |
|
34
|
|
|
'contentOptions' => ['class' => 'text-nowrap'], |
|
35
|
|
|
'value' => function ($model) { |
|
36
|
|
|
list($date, $time) = explode(' ', $model->time, 2); |
|
37
|
|
|
|
|
38
|
|
|
if (in_array($model->gtype, [ |
|
39
|
|
|
'discount', 'domain', 'monthly', 'overuse', 'premium_package', |
|
40
|
|
|
'feature', 'intercept', 'periodic' |
|
41
|
|
|
], true)) { |
|
42
|
|
|
return Yii::$app->formatter->asDate($date, 'LLLL y'); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
return $time === '00:00:00' ? Yii::$app->formatter->asDate($date) : Yii::$app->formatter->asDateTime($model->time); |
|
46
|
|
|
}, |
|
47
|
|
|
], |
|
48
|
|
|
'sum' => [ |
|
49
|
|
|
'class' => CurrencyColumn::class, |
|
50
|
|
|
'attribute' => 'sum', |
|
51
|
|
|
'colors' => ['danger' => 'warning'], |
|
52
|
|
|
'headerOptions' => ['class' => 'text-right'], |
|
53
|
|
|
'contentOptions' => function ($model) { |
|
54
|
|
|
return ['class' => 'text-right' . ($model->sum > 0 ? ' text-bold' : '')]; |
|
55
|
|
|
}, |
|
56
|
|
|
], |
|
57
|
|
|
'sum_editable' => [ |
|
58
|
|
|
'class' => CurrencyColumn::class, |
|
59
|
|
|
'attribute' => 'sum', |
|
60
|
|
|
'colors' => ['danger' => 'warning'], |
|
61
|
|
|
'headerOptions' => ['class' => 'text-right'], |
|
62
|
|
|
'urlCallback' => function ($model, $key) { |
|
|
|
|
|
|
63
|
|
|
return Url::to(['bill/update', 'id' => $model->id]); |
|
64
|
|
|
}, |
|
65
|
|
|
'contentOptions' => function ($model) { |
|
66
|
|
|
return ['class' => 'text-right' . ($model->sum > 0 ? ' text-bold' : '')]; |
|
67
|
|
|
}, |
|
68
|
|
|
], |
|
69
|
|
|
'balance' => [ |
|
70
|
|
|
'class' => CurrencyColumn::class, |
|
71
|
|
|
'headerOptions' => ['class' => 'text-right'], |
|
72
|
|
|
'contentOptions' => function ($model, $key, $index) { |
|
73
|
|
|
return ['class' => 'text-right' . ($index ? '' : ' text-bold')]; |
|
74
|
|
|
}, |
|
75
|
|
|
], |
|
76
|
|
|
'gtype' => [ |
|
77
|
|
|
'attribute' => 'gtype', |
|
78
|
|
|
], |
|
79
|
|
|
'type_label' => [ |
|
80
|
|
|
'class' => \hipanel\grid\DataColumn::class, |
|
81
|
|
|
'filter' => function ($column, $filterModel) { |
|
82
|
|
|
return BillTypeFilter::widget([ |
|
83
|
|
|
'options' => ['class' => 'form-control text-right'], |
|
84
|
|
|
'attribute' => 'ftype', |
|
85
|
|
|
'model' => $filterModel, |
|
86
|
|
|
]); |
|
87
|
|
|
}, |
|
88
|
|
|
'format' => 'raw', |
|
89
|
|
|
'headerOptions' => ['class' => 'text-right'], |
|
90
|
|
|
'contentOptions' => function ($model) { |
|
|
|
|
|
|
91
|
|
|
return ['class' => 'text-right']; |
|
92
|
|
|
}, |
|
93
|
|
|
'value' => function ($model) { |
|
94
|
|
|
static $colors = [ |
|
95
|
|
|
'correction' => 'normal', |
|
96
|
|
|
'exchange' => 'warning', |
|
97
|
|
|
'deposit' => 'success', |
|
98
|
|
|
]; |
|
99
|
|
|
$color = $colors[$model->gtype] ?: 'muted'; |
|
100
|
|
|
|
|
101
|
|
|
return Html::tag('b', Yii::t('hipanel:finance', $model->type_label), ['class' => "text-$color"]); |
|
102
|
|
|
}, |
|
103
|
|
|
], |
|
104
|
|
|
'description' => [ |
|
105
|
|
|
'attribute' => 'descr', |
|
106
|
|
|
'format' => 'raw', |
|
107
|
|
|
'value' => function ($model) { |
|
108
|
|
|
$descr = $model->descr ?: $model->label; |
|
109
|
|
|
$text = mb_strlen($descr) > 70 ? ArraySpoiler::widget(['data' => $descr]) : $descr; |
|
110
|
|
|
$tariff = $model->tariff ? Html::tag('span', |
|
111
|
|
|
Yii::t('hipanel', 'Tariff') . ': ' . Html::a($model->tariff, |
|
112
|
|
|
['@tariff/view', 'id' => $model->tariff_id]), ['class' => 'pull-right']) : ''; |
|
113
|
|
|
$amount = static::billQuantity($model); |
|
114
|
|
|
$object = $model->object ? implode(': ', |
|
115
|
|
|
[$model->class_label, static::objectLink($model)]) : ''; |
|
116
|
|
|
|
|
117
|
|
|
return $tariff . $amount . ' ' . implode('<br>', array_filter([$object, $text])); |
|
118
|
|
|
}, |
|
119
|
|
|
], |
|
120
|
|
|
'tariff' => [ |
|
121
|
|
|
'attribute' => 'tariff', |
|
122
|
|
|
], |
|
123
|
|
|
]; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
public static function objectLink($model) |
|
127
|
|
|
{ |
|
128
|
|
|
return $model->class === 'device' |
|
129
|
|
|
? Html::a($model->object, ['@server/view', 'id' => $model->object_id]) |
|
130
|
|
|
: Html::tag('b', $model->object); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @param $model |
|
135
|
|
|
* @return null|string |
|
136
|
|
|
*/ |
|
137
|
|
|
public static function billQuantity($model) |
|
138
|
|
|
{ |
|
139
|
|
|
switch ($model->type) { |
|
140
|
|
|
case 'support_time': |
|
141
|
|
|
$text = Yii::t('hipanel:finance', '{quantity, time, HH:mm} hour(s)', |
|
142
|
|
|
['quantity' => ceil($model->quantity * 3600)]); |
|
143
|
|
|
break; |
|
144
|
|
|
case 'server_traf_max': |
|
145
|
|
|
case 'backup_du': |
|
146
|
|
|
$text = Yii::$app->formatter->asShortSize($model->quantity * 1024 * 1024 * 1024); |
|
147
|
|
|
break; |
|
148
|
|
|
case 'ip_num': |
|
149
|
|
|
$text = Yii::t('hipanel:finance', '{quantity} IP', ['quantity' => $model->quantity]); |
|
150
|
|
|
break; |
|
151
|
|
|
default: |
|
152
|
|
|
return null; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
return Html::tag('nobr', Html::tag('b', $text)); |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.