DomainServicePriceGridView   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 6
dl 0
loc 58
ccs 0
cts 41
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A columns() 0 16 5
B getPriceGrid() 0 29 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\modules\finance\models\DomainServicePrice;
14
use hipanel\modules\finance\widgets\PriceDifferenceWidget;
15
use hipanel\modules\finance\widgets\ResourcePriceWidget;
16
use yii\helpers\Html;
17
18
class DomainServicePriceGridView extends PriceGridView
19
{
20
    /**
21
     * @var DomainServicePrice[]
22
     */
23
    public $parentPrices;
24
25
    public function columns()
26
    {
27
        $columns = parent::columns();
28
        foreach ($this->dataProvider->getModels() as $models) {
29
            foreach ($models as $model) {
30
                if (strpos($model->type, 'purchase') !== false) {
31
                    $columns['purchase'] = $this->getPriceGrid($model->type);
32
                }
33
                if (strpos($model->type, 'renew') !== false) {
34
                    $columns['renewal'] = $this->getPriceGrid($model->type);
35
                }
36
            }
37
        }
38
39
        return $columns;
40
    }
41
42
    /**
43
     * @param string $type
44
     * @return array
45
     */
46
    private function getPriceGrid(string $type): array
47
    {
48
        return [
49
            'label' =>  DomainServicePrice::getOperations()[$type],
50
            'contentOptions' => ['class' => 'text-center'],
51
            'format' => 'raw',
52
            'value' => function ($prices) use ($type) {
53
                /** @var DomainServicePrice[] $prices */
54
                if (!isset($prices[$type])) {
55
                    return '';
56
                }
57
                $price = $prices[$type];
58
                $parent = $this->parentPrices[$type] ?? null;
59
                $parentValue = $parent ? PriceDifferenceWidget::widget([
60
                    'new' => $price->getMoney(),
61
                    'old' => $parent->getMoney(),
62
                ]) : '';
63
                $priceValue = floatval($price->price) ||
64
                (!floatval($price->price) && $parentValue) ?
65
                    ResourcePriceWidget::widget([
66
                        'price' => $price->getMoney(),
67
                    ]) : '';
68
                $options = ['class' => 'col-md-6'];
69
70
                return Html::tag('div', $priceValue, $options) .
71
                    Html::tag('div', $parentValue, $options);
72
            },
73
        ];
74
    }
75
}
76