Completed
Push — master ( b864d9...3fa98b )
by Dmitry
07:42 queued 03:17
created

CertificatePriceGridView::columns()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 0
cts 6
cp 0
rs 9.7998
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace hipanel\modules\finance\grid;
4
5
use hipanel\grid\ColspanColumn;
6
use hipanel\modules\finance\models\CertificatePrice;
7
use hipanel\modules\finance\widgets\PriceDifferenceWidget;
8
use hipanel\modules\finance\widgets\ResourcePriceWidget;
9
use Yii;
10
11
class CertificatePriceGridView extends PriceGridView
12
{
13
    /**
14
     * @var CertificatePrice[]
15
     */
16
    public $parentPrices;
17
18
    public function columns()
19
    {
20
        return array_merge(parent::columns(), [
21
            'purchase' => $this->getPriceGrid('Purchase', 'certificate,certificate_purchase'),
22
            'renewal' => $this->getPriceGrid('Renewal', 'certificate,certificate_renewal'),
23
            'certificate' => [
24
                'label' => Yii::t('hipanel:finance:tariff', 'Name'),
25
                'value' => function ($prices) {
26
                    /** @var CertificatePrice[] $prices  */
27
                    return current($prices)->object->label;
28
                }
29
            ],
30
        ]);
31
    }
32
33
    private function getPriceGrid($name, $type)
34
    {
35
        $result = [
36
            'label' =>  Yii::t('hipanel:finance:tariff', $name),
37
            'class' => ColspanColumn::class,
38
            'headerOptions' => [
39
                'class' => 'text-center',
40
            ],
41
            'columns' => []
42
        ];
43
        foreach (CertificatePrice::getPeriods() as $period => $label) {
44
            $result['columns'][] = [
45
                'label' => Yii::t('hipanel:finance:tariff', $label),
46
                'contentOptions' => ['class' => 'text-center'],
47
                'format' => 'raw',
48
                'value' => function ($prices) use ($type, $period) {
49
                    /** @var CertificatePrice[] $prices */
50
                    if (!isset($prices[$type])) {
51
                        return '';
52
                    }
53
                    $price = $prices[$type];
54
                    $parent = $this->parentPrices[$price->object_id][$type] ?? null;
55
                    $parent = $parent ? PriceDifferenceWidget::widget([
56
                        'new' => $price->getPriceForPeriod($period),
57
                        'old' => $parent->getPriceForPeriod($period),
58
                    ]) : '';
59
                    $price = floatval($price->getPriceForPeriod($period)) || (!floatval($price->getPriceForPeriod($period)) && $parent) ?
60
                        ResourcePriceWidget::widget([
61
                            'price' => $price->getPriceForPeriod($period),
62
                            'currency' => $price->currency
63
                        ]) : '';
64
                    return "<div class='col-md-6'>$price</div> <div class='col-md-6'>$parent</div>";
65
                }
66
            ];
67
        }
68
        return $result;
69
    }
70
}
71