Completed
Pull Request — master (#62)
by Dmitry
05:57
created

GroupedByServerChargesGridView::initAfterRow()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 37
ccs 0
cts 30
cp 0
rs 9.328
c 0
b 0
f 0
cc 2
nc 1
nop 0
crap 6
1
<?php
2
3
namespace hipanel\modules\finance\grid;
4
5
use hipanel\modules\finance\models\Charge;
6
use hipanel\modules\finance\helpers\ChargeSort;
7
use \yii\data\ArrayDataProvider;
8
9
/**
10
 * Class GroupedChargesGridView
11
 */
12
class GroupedByServerChargesGridView extends BillGridView
13
{
14
15
    /**
16
     * @var Charge[]
17
     */
18
    public $idToNameObject;
19
20
    /**
21
     * @var Charge[][]
22
     */
23
    public $chargesByMainObject;
24
25
    public function init()
26
    {
27
        parent::init();
28
        $this->initAfterRow();
29
    }
30
31
    public function columns()
32
    {
33
        return array_merge(parent::columns(), [
34
            'object_link' => [
35
                'format' => 'html',
36
                'attribute' => 'common_object_name',
37
            ],
38
        ]);
39
    }
40
41
    private function initAfterRow(): void
42
    {
43
        /**
44
         * @param Charge $obj
45
         * @return string
46
         */
47
        $this->afterRow = function (Charge $obj) {
48
            $model = $this->chargesByMainObject[$obj->common_object_id];
49
            if (empty($model)) {
50
                return '';
51
            }
52
            return GroupedChargesGridView::widget([
53
                'boxed'        => false,
54
                'showHeader'   => true,
55
                'showFooter'   => false,
56
                'options'      => [
57
                    'tag' => 'tr',
58
                    'id'  => crc32($model->id ?? microtime(true)),
59
                ],
60
                'layout'       => '<td colspan="' . \count($this->columns) . '">{items}</td>',
61
                'dataProvider' => new ArrayDataProvider([
62
                    'allModels'  => ChargeSort::anyCharges()
63
                        ->values($model, true),
64
                    'sort'       => false,
65
                    'pagination' => false
66
                ]),
67
                'filterModel'  => $model,
68
                'tableOptions' => [
69
                    'class' => 'table table-striped table-bordered'
70
                ],
71
                'columns'      => [
72
                    'type_label', 'label',
73
                    'quantity', 'sum', 'sum_with_children', 'time',
74
                ],
75
            ]);
76
        };
77
    }
78
}
79