Completed
Push — master ( 267413...040341 )
by Dmitry
05:20
created

SalePricesActionsMenu   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
lcom 1
cbo 4
dl 0
loc 64
rs 10
c 1
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A items() 0 4 1
A getGenerationButtons() 0 15 2
A suggestionLink() 0 4 1
B suggestionTypesByObject() 0 30 4
1
<?php
2
3
namespace hipanel\modules\finance\menus;
4
5
use hipanel\modules\finance\models\FakeSale;
6
use hipanel\modules\finance\models\Sale;
7
use Yii;
8
9
/**
10
 * Class TariffActionsMenu
11
 *
12
 * @author Dmytro Naumenko <[email protected]>
13
 */
14
class SalePricesActionsMenu extends \hiqdev\yii2\menus\Menu
15
{
16
    /**
17
     * @var \hipanel\modules\finance\models\Sale
18
     */
19
    public $model;
20
21
    public function items()
22
    {
23
        return $this->getGenerationButtons();
24
    }
25
26
    protected function getGenerationButtons()
27
    {
28
        $buttons = [];
29
30
        foreach ($this->suggestionTypesByObject() as $config) {
31
            $buttons[] = [
32
                'label' => $config['label'],
33
                'icon' => $config['icon'],
34
                'url' => $this->suggestionLink($config['type']),
35
                'visible' => Yii::$app->user->can('plan.update'),
36
            ];
37
        }
38
39
        return $buttons;
40
    }
41
42
    protected function suggestionLink($type)
43
    {
44
        return ['@price/suggest', 'plan_id' => $this->model->tariff_id, 'object_id' => $this->model->object_id, 'type' => $type];
45
    }
46
47
    protected function suggestionTypesByObject()
48
    {
49
        if ($this->model instanceof FakeSale && empty($this->model->tariff_type)) {
50
            return [
51
                [
52
                    'type' => 'default',
53
                    'label' => Yii::t('hipanel.finance.price', 'Main prices'),
54
                    'icon' => 'fa-plus',
55
                ],
56
            ];
57
        }
58
59
        switch ($this->model->tariff_type) {
60
            case Sale::SALE_TYPE_SERVER:
61
                return [
62
                    [
63
                        'type' => 'default',
64
                        'label' => Yii::t('hipanel.finance.price', 'Main prices'),
65
                        'icon' => 'fa-plus',
66
                    ],
67
                    [
68
                        'type' => 'parts',
69
                        'label' => Yii::t('hipanel.finance.price', 'Part prices'),
70
                        'icon' => 'fa-hdd-o',
71
                    ]
72
                ];
73
        }
74
75
        return [];
76
    }
77
}
78