Completed
Push — master ( 5e0ad4...117a3a )
by Dmitry
10:56
created

SalesInPlanGridView::init()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 0
cts 3
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 0
crap 12
1
<?php
2
3
namespace hipanel\modules\finance\grid;
4
5
use hipanel\modules\finance\menus\SalePricesActionsMenu;
6
use hipanel\modules\finance\models\Sale;
7
use hiqdev\yii2\menus\grid\MenuColumn;
8
use Yii;
9
use yii\base\InvalidConfigException;
10
use yii\data\ArrayDataProvider;
11
12
/**
13
 * Class SalesInPlanGridView
14
 *
15
 * @author Dmytro Naumenko <[email protected]>
16
 */
17
class SalesInPlanGridView extends SaleGridView
18
{
19
    /**
20
     * @var array
21
     */
22
    public $pricesBySoldObject;
23
24
    /**
25
     * @throws \yii\base\InvalidConfigException
26
     */
27
    public function init()
28
    {
29
        parent::init();
30
31
        if (!isset($this->pricesBySoldObject)) {
32
            throw new InvalidConfigException("Property 'pricesBySoldObject' must be set");
33
        }
34
        if (empty($this->afterRow)) {
35
            $this->initAfterRow();
36
        }
37
    }
38
39
    public function columns()
40
    {
41
        return array_merge(parent::columns(), [
42
            'object_label' => [
43
                'format' => 'raw',
44
                'value' => function (Sale $sale) {
45
                    $prices = $this->pricesBySoldObject[$sale->object_id ?? $sale->tariff_id] ?? [];
46
                    foreach ($prices as $price) {
47
                        if ($price->object->id === $sale->object_id) {
48
                            return $price->object->label;
49
                        }
50
                    }
51
52
                    return '';
53
                },
54
            ],
55
            'price_related_actions' => [
56
                'class' => MenuColumn::class,
57
                'menuClass' => SalePricesActionsMenu::class,
58
                'menuButtonOptions' => [
59
                    'icon' => '<i class="fa fa-plus"></i>&nbsp;'
60
                        . Yii::t('hipanel.finance.price', 'Prices')
61
                        . '&nbsp;<span class="caret"></span>'
62
                ]
63
            ]
64
        ]);
65
    }
66
67
    private function initAfterRow()
68
    {
69
        $this->afterRow = function (Sale $sale) {
70
            $prices = $this->pricesBySoldObject[$sale->object_id ?? $sale->tariff_id];
71
            if (empty($prices)) {
72
                return '';
73
            }
74
75
            return PriceGridView::widget([
76
                'boxed' => false,
77
                'showHeader' => true,
78
                'showFooter' => false,
79
                'options' => [
80
                    'tag' => 'tr',
81
                    'id' => crc32($sale->id ?? microtime(true)),
82
                ],
83
                'layout' => '<td colspan="' . count($this->columns) . '">{items}</td>',
84
                'emptyText' => Yii::t('hipanel.finance.price', 'No prices found'),
85
                'dataProvider' => (new ArrayDataProvider([
86
                    'allModels' => $prices,
87
                    'pagination' => false,
88
                ])),
89
                'columns' => [
90
                    'checkbox',
91
                    'object->name',
92
                    'object->label',
93
                    'price',
94
                    'type',
95
                    'note',
96
                ],
97
            ]);
98
        };
99
    }
100
}
101