SalePricesActionsMenu::suggestionTypesByObject()   B
last analyzed

Complexity

Conditions 8
Paths 6

Size

Total Lines 43

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
dl 0
loc 43
ccs 0
cts 25
cp 0
rs 7.9875
c 0
b 0
f 0
cc 8
nc 6
nop 0
crap 72
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\menus;
12
13
use hipanel\modules\finance\models\FakeGroupingSale;
14
use hipanel\modules\finance\models\FakeSharedSale;
15
use hipanel\modules\finance\models\Sale;
16
use Yii;
17
18
/**
19
 * Class TariffActionsMenu.
20
 *
21
 * @author Dmytro Naumenko <[email protected]>
22
 */
23
class SalePricesActionsMenu extends \hiqdev\yii2\menus\Menu
24
{
25
    /**
26
     * @var \hipanel\modules\finance\models\Sale
27
     */
28
    public $model;
29
30
    public function items()
31
    {
32
        return $this->getGenerationButtons();
33
    }
34
35
    protected function getGenerationButtons()
36
    {
37
        $buttons = [];
38
39
        foreach ($this->suggestionTypesByObject() as $config) {
40
            $buttons[] = [
41
                'label' => $config['label'],
42
                'icon' => $config['icon'],
43
                'url' => $this->suggestionLink($config['type']),
44
                'visible' => Yii::$app->user->can('plan.update'),
45
            ];
46
        }
47
48
        return $buttons;
49
    }
50
51
    protected function suggestionLink($type)
52
    {
53
        $object_id = $this->model->tariff_type === Sale::SALE_TYPE_HARDWARE
54
                        ? $this->model->buyer_id
55
                        : $this->model->object_id;
56
57
        return [
58
            '@price/suggest',
59
            'plan_id' => $this->model->tariff_id,
60
            'object_id' => $object_id,
61
            'type' => $type
62
        ];
63
    }
64
65
    protected function suggestionTypesByObject()
66
    {
67
        switch ($this->model->tariff_type) {
68
            case Sale::SALE_TYPE_SERVER:
69
            case Sale::SALE_TYPE_SWITCH:
70
                return array_filter([
71
                    [
72
                        'type' => 'default',
73
                        'label' => Yii::t('hipanel.finance.price', 'Main prices'),
74
                        'icon' => 'fa-plus',
75
                    ],
76
                    [
77
                        'type' => 'services',
78
                        'label' => Yii::t('hipanel.finance.price', 'Additional services'),
79
                        'icon' => 'fa-codiepie',
80
                    ],
81
                    !$this->model instanceof FakeGroupingSale && !$this->model instanceof FakeSharedSale ? [
82
                        'type' => 'parts',
83
                        'label' => Yii::t('hipanel.finance.price', 'Hardware prices'),
84
                        'icon' => 'fa-hdd-o',
85
                    ] : null,
86
                ]);
87
            case Sale::SALE_TYPE_HARDWARE:
88
                return [
89
                    [
90
                        'type' => 'parts',
91
                        'label' => Yii::t('hipanel.finance.price', 'Hardware prices'),
92
                        'icon' => 'fa-hdd-o',
93
                    ]
94
                ];
95
            case Sale::SALE_TYPE_PCDN:
96
            case Sale::SALE_TYPE_VCDN:
97
                return [
98
                    [
99
                        'type' => 'default',
100
                        'label' => Yii::t('hipanel.finance.price', 'Main prices'),
101
                        'icon' => 'fa-plus',
102
                    ],
103
                ];
104
        }
105
106
        return [];
107
    }
108
}
109