CertificatePriceGridView::getPriceGrid()   B
last analyzed

Complexity

Conditions 7
Paths 2

Size

Total Lines 40

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
dl 0
loc 40
ccs 0
cts 36
cp 0
rs 8.3466
c 0
b 0
f 0
cc 7
nc 2
nop 2
crap 56
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\ColspanColumn;
14
use hipanel\modules\finance\models\CertificatePrice;
15
use hipanel\modules\finance\widgets\PriceDifferenceWidget;
16
use hipanel\modules\finance\widgets\ResourcePriceWidget;
17
use Yii;
18
use yii\helpers\Html;
19
20
class CertificatePriceGridView extends PriceGridView
21
{
22
    /**
23
     * @var CertificatePrice[]
24
     */
25
    public $parentPrices;
26
27
    public function columns()
28
    {
29
        return array_merge(parent::columns(), [
30
            'purchase' => $this->getPriceGrid('Purchase', 'certificate,certificate_purchase'),
31
            'renewal' => $this->getPriceGrid('Renewal', 'certificate,certificate_renewal'),
32
            'certificate' => [
33
                'label' => Yii::t('hipanel:finance:tariff', 'Name'),
34
                'value' => function ($prices) {
35
                    /** @var CertificatePrice[] $prices */
36
                    return current($prices)->object->label;
37
                },
38
            ],
39
        ]);
40
    }
41
42
    /**
43
     * @param string $name column label
44
     * @param string $type certificate type
45
     * @return array
46
     */
47
    private function getPriceGrid(string $name, string $type): array
48
    {
49
        $result = [
50
            'label' => Yii::t('hipanel:finance:tariff', $name),
51
            'class' => ColspanColumn::class,
52
            'headerOptions' => [
53
                'class' => 'text-center',
54
            ],
55
            'columns' => [],
56
        ];
57
        foreach (CertificatePrice::getPeriods() as $period => $label) {
58
            $result['columns'][] = [
59
                'label' => Yii::t('hipanel:finance:tariff', $label),
60
                'contentOptions' => ['class' => 'text-center'],
61
                'format' => 'raw',
62
                'value' => function ($prices) use ($type, $period) {
63
                    /** @var CertificatePrice[] $prices */
64
                    if (!isset($prices[$type])) {
65
                        return '';
66
                    }
67
                    $price = $prices[$type];
68
                    $parent = $this->parentPrices[$price->object_id][$type] ?? null;
69
                    $parentValue = $parent ? PriceDifferenceWidget::widget([
70
                        'new' => $price->getMoneyForPeriod($period),
71
                        'old' => $parent->getMoneyForPeriod($period),
72
                    ]) : '';
73
                    $priceValue = floatval($price->getPriceForPeriod($period)) ||
74
                    (!floatval($price->getPriceForPeriod($period)) && $parentValue) ?
75
                        ResourcePriceWidget::widget([
76
                            'price' => $price->getMoneyForPeriod($period),
77
                        ]) : '';
78
                    $options = ['class' => 'prices-cell'];
79
80
                    return Html::tag('div', "<div class='left-table-item'>$priceValue</div><div class='right-table-item'>$parentValue</div>", $options);
81
                },
82
            ];
83
        }
84
85
        return $result;
86
    }
87
}
88