Completed
Push — master ( d0e0e5...0154ba )
by Andrii
05:43 queued 03:12
created

ChargeGridView::tariffLink()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
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 hiqdev\higrid\DataColumn;
23
use Yii;
24
use yii\helpers\Html;
25
26
/**
27
 * Class ChargeGridView.
28
 *
29
 * @author Dmytro Naumenko <[email protected]>
30
 */
31
class ChargeGridView extends \hipanel\grid\BoxedGridView
32
{
33
    /**
34
     * @var QuantityFormatterFactoryInterface
35
     */
36
    private $formatterFactory;
37
38
    /**
39
     * ChargeGridView constructor.
40
     *
41
     * @param QuantityFormatterFactoryInterface $formatterFactory
42
     * @param array $config
43
     */
44
    public function __construct(QuantityFormatterFactoryInterface $formatterFactory, array $config = [])
45
    {
46
        parent::__construct($config);
47
        $this->formatterFactory = $formatterFactory;
48
    }
49
50
    public function columns()
51
    {
52
        return array_merge(parent::columns(), [
53
            'label' => [
54
                'attribute' => 'label_ilike',
55
                'sortAttribute' => 'label_ilike',
56
                'label' => Yii::t('hipanel', 'Description'),
57
                'value' => function (Charge $model): string {
58
                    return $model->label ?? '';
0 ignored issues
show
Documentation introduced by
The property label does not exist on object<hipanel\modules\finance\models\Charge>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
59
                },
60
            ],
61
            'tariff' => [
62
                'attribute' => 'tariff_id',
63
                'label' => Yii::t('hipanel', 'Plan'),
64
                'filter' => false,
65
                'format' => 'html',
66
                'value' => function (Charge $model): string {
67
                    return $this->tariffLink($model);
68
                },
69
            ],
70
            'type_label' => [
71
                'label' => Yii::t('hipanel', 'Type'),
72
                'format' => 'raw',
73
                'value' => function (Charge $model) {
74
                    return BillType::widget([
75
                        'model' => $model,
76
                        'field' => 'ftype',
77
                        'labelField' => 'type_label',
78
                    ]);
79
                },
80
                'filterAttribute' => 'type',
81
                'filter' => function (DataColumn $column, ChargeSearch $filterModel): string {
82
                    return BillTypeFilter::widget([
83
                        'options' => ['class' => 'form-control text-right', 'style' => 'max-width: 12em'],
84
                        'attribute' => 'ftype',
85
                        'model' => $filterModel,
86
                    ]);
87
                },
88
            ],
89
            'sum' => [
90
                'class' => CurrencyColumn::class,
91
                'attribute' => 'sum',
92
                'sortAttribute' => 'sum',
93
                'colors' => ['danger' => 'warning'],
94
                'headerOptions' => ['class' => 'text-right'],
95
                'filter' => false,
96
                'contentOptions' => function ($model) {
97
                    return ['class' => 'text-right' . ($model->sum > 0 ? ' text-bold' : '')];
98
                },
99
            ],
100
            'name' => [
101
                'attribute' => 'name_ilike',
102
                'label' => Yii::t('hipanel', 'Object'),
103
                'format' => 'raw',
104
                'value' => function (Charge $model) {
105
                    $result = LinkToObjectResolver::widget([
106
                        'model' => $model,
107
                        'idAttribute' => 'object_id',
108
                        'typeAttribute' => 'class',
109
                        'labelAttribute' => 'name',
110
                    ]) . ($model->label ? " &ndash; $model->label" : '');
0 ignored issues
show
Documentation introduced by
The property label does not exist on object<hipanel\modules\finance\models\Charge>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
111
112
                    if ($model->commonObject->id !== null && $model->commonObject->id !== $model->latestCommonObject->id) {
113
                        $result .= ' ' . Html::tag(
114
                            'span',
115
                            Yii::t('hipanel:finance', 'Now it is in {objectLink}', [
116
                                'objectLink' => LinkToObjectResolver::widget([
117
                                    'model'          => $model->latestCommonObject,
118
                                    'idAttribute'    => 'id',
119
                                    'labelAttribute' => 'name',
120
                                    'typeAttribute'  => 'type',
121
                                ]),
122
                            ]),
123
                            ['class' => 'badge', 'style' => 'background-color: #f89406;']
124
                        );
125
                    }
126
127
                    return $result;
128
                },
129
            ],
130
            'quantity' => [
131
                'attribute' => 'quantity',
132
                'format' => 'raw',
133
                'filter' => false,
134
                'value' => function (Charge $model) {
135
                    return $this->renderQuantity($model);
136
                },
137
            ],
138
            'time' => [
139
                'format' => 'raw',
140
                'filter' => false,
141
                'contentOptions' => ['class' => 'text-nowrap'],
142
                'value' => function ($model) {
143
                    list($date, $time) = explode(' ', $model->time, 2);
144
145
                    return $model->isMonthly() && $time === '00:00:00'
146
                        ? Yii::$app->formatter->asDate($date, 'LLLL y')
147
                        : Yii::$app->formatter->asDateTime($model->time);
148
                },
149
            ],
150
        ]);
151
    }
152
153
    /**
154
     * @param Charge $model
155
     * @return string|null
156
     */
157
    public function tariffLink(Charge $model): ?string
158
    {
159
        $canSeeLink = Yii::$app->user->can('plan.create');
160
161
        return $canSeeLink ? Html::a($model->tariff, ['@plan/view', 'id' => $model->tariff_id]) : $model->tariff;
0 ignored issues
show
Bug introduced by
The property tariff_id does not seem to exist. Did you mean tariff?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
Documentation introduced by
$model->tariff is of type object<hipanel\modules\finance\models\Tariff>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
162
    }
163
164
    /**
165
     * @return string
166
     */
167
    private function renderQuantity(Charge $charge): string
168
    {
169
        $formatter = $this->formatterFactory->forCharge($charge);
170
171
        if ($formatter !== null) {
172
            return Html::tag('nobr', Html::tag('b', $formatter->format()));
173
        }
174
175
        return '';
176
    }
177
}
178