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

SalePricesActionsMenu::items()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
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