ChargeGridView::columns()   C
last analyzed

Complexity

Conditions 8
Paths 1

Size

Total Lines 133

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
dl 0
loc 133
ccs 0
cts 119
cp 0
rs 6.7555
c 0
b 0
f 0
cc 8
nc 1
nop 0
crap 72

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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-2019, 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\modules\client\grid\ClientColumn;
16
use hipanel\modules\finance\logic\bill\QuantityFormatterFactoryInterface;
17
use hipanel\modules\finance\models\Charge;
18
use hipanel\modules\finance\models\ChargeSearch;
19
use hipanel\modules\finance\widgets\BillType;
20
use hipanel\modules\finance\widgets\BillTypeFilter;
21
use hipanel\modules\finance\widgets\LinkToObjectResolver;
22
use hipanel\widgets\IconStateLabel;
23
use hiqdev\combo\StaticCombo;
24
use hiqdev\higrid\DataColumn;
25
use Yii;
26
use yii\helpers\Html;
27
28
/**
29
 * Class ChargeGridView.
30
 *
31
 * @author Dmytro Naumenko <[email protected]>
32
 */
33
class ChargeGridView extends \hipanel\grid\BoxedGridView
34
{
35
    /**
36
     * @var QuantityFormatterFactoryInterface
37
     */
38
    private $formatterFactory;
39
40
    /**
41
     * ChargeGridView constructor.
42
     *
43
     * @param QuantityFormatterFactoryInterface $formatterFactory
44
     * @param array $config
45
     */
46
    public function __construct(QuantityFormatterFactoryInterface $formatterFactory, array $config = [])
47
    {
48
        parent::__construct($config);
49
        $this->formatterFactory = $formatterFactory;
50
    }
51
52
    public function columns()
53
    {
54
        return array_merge(parent::columns(), [
55
            'label' => [
56
                'attribute' => 'label_ilike',
57
                'sortAttribute' => 'label_ilike',
58
                'label' => Yii::t('hipanel', 'Description'),
59
                'value' => function (Charge $model): string {
60
                    return $model->label ?? '';
61
                },
62
            ],
63
            'tariff' => [
64
                'attribute' => 'tariff_id',
65
                'label' => Yii::t('hipanel', 'Plan'),
66
                'filter' => false,
67
                'format' => 'html',
68
                'value' => function (Charge $model): string {
69
                    return $this->tariffLink($model);
70
                },
71
            ],
72
            'type_label' => [
73
                'label' => Yii::t('hipanel', 'Type'),
74
                'format' => 'raw',
75
                'value' => function (Charge $model) {
76
                    return BillType::widget([
77
                        'model' => $model,
78
                        'field' => 'ftype',
79
                        'labelField' => 'type_label',
80
                    ]);
81
                },
82
                'filterAttribute' => 'type',
83
                'filter' => function (DataColumn $column, ChargeSearch $filterModel): string {
84
                    return BillTypeFilter::widget([
85
                        'options' => ['class' => 'form-control text-right', 'style' => 'max-width: 12em'],
86
                        'attribute' => 'ftype',
87
                        'model' => $filterModel,
88
                    ]);
89
                },
90
            ],
91
            'sum' => [
92
                'class' => CurrencyColumn::class,
93
                'attribute' => 'sum',
94
                'sortAttribute' => 'sum',
95
                'colors' => ['danger' => 'warning'],
96
                'headerOptions' => ['class' => 'text-right'],
97
                'filter' => false,
98
                'contentOptions' => function ($model) {
99
                    return ['class' => 'text-right' . ($model->sum > 0 ? ' text-bold' : '')];
100
                },
101
            ],
102
            'name' => [
103
                'attribute' => 'name_ilike',
104
                'label' => Yii::t('hipanel', 'Object'),
105
                'format' => 'raw',
106
                'value' => function (Charge $model) {
107
                    $result = LinkToObjectResolver::widget([
108
                        'model' => $model,
109
                        'idAttribute' => 'object_id',
110
                        'typeAttribute' => 'class',
111
                        'labelAttribute' => 'name',
112
                    ]) . ($model->label ? " &ndash; $model->label" : '');
113
114
                    if ($model->commonObject->id !== null && $model->commonObject->id !== $model->latestCommonObject->id) {
115
                        $result .= ' ' . Html::tag(
116
                            'span',
117
                            Yii::t('hipanel:finance', 'Now it is in {objectLink}', [
118
                                'objectLink' => LinkToObjectResolver::widget([
119
                                    'model'          => $model->latestCommonObject,
120
                                    'idAttribute'    => 'id',
121
                                    'labelAttribute' => 'name',
122
                                    'typeAttribute'  => 'type',
123
                                ]),
124
                            ]),
125
                            ['class' => 'badge', 'style' => 'background-color: #f89406;']
126
                        );
127
                    }
128
129
                    return $result;
130
                },
131
            ],
132
            'quantity' => [
133
                'attribute' => 'quantity',
134
                'format' => 'raw',
135
                'filter' => false,
136
                'value' => function (Charge $model) {
137
                    return $this->renderQuantity($model);
138
                },
139
            ],
140
            'time' => [
141
                'format' => 'raw',
142
                'filter' => false,
143
                'contentOptions' => ['class' => 'text-nowrap'],
144
                'value' => function ($model) {
145
                    list($date, $time) = explode(' ', $model->time, 2);
146
147
                    return $model->isMonthly() && $time === '00:00:00'
148
                        ? Yii::$app->formatter->asDate($date, 'LLLL y')
149
                        : Yii::$app->formatter->asDateTime($model->time);
150
                },
151
            ],
152
            'is_payed' => [
153
                'attribute' => 'is_payed',
154
                'format' => 'raw',
155
                'enableSorting' => false,
156
                'filter' => $this->filterModel !== null
157
                    ? StaticCombo::widget([
158
                          'attribute' => 'is_payed',
159
                          'model' => $this->filterModel,
160
                          'data' => [
161
                              0 => Yii::t('hipanel:finance', 'Charge not paid'),
162
                              1 => Yii::t('hipanel:finance', 'Charge paid'),
163
                          ],
164
                          'hasId' => true,
165
                          'inputOptions' => ['id' => 'is_payed'],
166
                      ])
167
                    : false,
168
                'contentOptions' => ['class' => 'text-center'],
169
                'headerOptions' => ['class' => 'text-center'],
170
                'value' => static function (Charge $model) {
171
                    return IconStateLabel::widget([
172
                        'model' => $model,
173
                        'attribute' => 'is_payed',
174
                        'icons' => ['fa-check', 'fa-times'],
175
                        'colors' => ['#00a65a', '#dd4b39'],
176
                        'messages' => [
177
                            Yii::t('hipanel:finance', 'Charge paid'),
178
                            Yii::t('hipanel:finance', 'Charge not paid'),
179
                        ],
180
                    ]);
181
                }
182
            ]
183
        ]);
184
    }
185
186
    /**
187
     * @param Charge $model
188
     * @return string|null
189
     */
190
    public function tariffLink(Charge $model): ?string
191
    {
192
        $canSeeLink = Yii::$app->user->can('plan.create');
193
194
        return $canSeeLink ? Html::a($model->tariff, ['@plan/view', 'id' => $model->tariff_id]) : $model->tariff;
195
    }
196
197
    /**
198
     * @return string
199
     */
200
    private function renderQuantity(Charge $charge): string
201
    {
202
        $formatter = $this->formatterFactory->forCharge($charge);
203
204
        if ($formatter !== null) {
205
            return Html::tag('nobr', Html::tag('b', $formatter->format()));
206
        }
207
208
        return '';
209
    }
210
}
211