Completed
Push — master ( 96c4d7...0211a3 )
by Dmitry
11:07
created

BillController::getTypesAndGroups()   B

Complexity

Conditions 6
Paths 9

Size

Total Lines 26
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 26
ccs 0
cts 0
cp 0
rs 8.439
cc 6
eloc 14
nc 9
nop 0
crap 42
1
<?php
2
3
/*
4
 * Finance module for HiPanel
5
 *
6
 * @link      https://github.com/hiqdev/hipanel-module-finance
7
 * @package   hipanel-module-finance
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2015-2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hipanel\modules\finance\controllers;
13
14
use hipanel\actions\IndexAction;
15
use hipanel\actions\OrientationAction;
16
use hipanel\actions\SmartCreateAction;
17
use hipanel\actions\SmartPerformAction;
18
use hipanel\actions\SmartUpdateAction;
19
use hipanel\actions\ValidateFormAction;
20
use hipanel\actions\ViewAction;
21
use hipanel\modules\finance\forms\BillImportForm;
22
use Yii;
23
use yii\filters\AccessControl;
24
25 1
class BillController extends \hipanel\base\CrudController
26
{
27
    public function behaviors()
28
    {
29
        return array_merge(parent::behaviors(), [
30
            'access-bill' => [
31
                'class' => AccessControl::class,
32
                'only' => ['index', 'view', 'create', 'update', 'delete'],
33 1
                'rules' => [
34
                    [
35 1
                        'allow' => true,
36
                        'roles' => ['manage', 'deposit'],
37
                        'actions' => ['index', 'view'],
38
                    ],
39
                    [
40 1
                        'allow' => true,
41 1
                        'roles' => ['create-bills'],
42
                        'actions' => ['create', 'import'],
43
                    ],
44 1
                    [
45
                        'allow' => true,
46
                        'roles' => ['update-bills'],
47 1
                        'actions' => ['update'],
48
                    ],
49 1
                    [
50 1
                        'allow' => true,
51
                        'roles' => ['delete-bills'],
52
                        'actions' => ['delete'],
53
                    ],
54
                ],
55
            ],
56
        ]);
57
    }
58
59
    public function actions()
60
    {
61
        return [
62
            'set-orientation' => [
63
                'class' => OrientationAction::class,
64
                'allowedRoutes' => [
65
                    '@bill/index',
66
                ],
67
            ],
68
            'index' => [
69
                'class' => IndexAction::class,
70
                'data' => function ($action) {
71
                    return [
72
                        'types' => $action->controller->getPaymentTypes(),
73 1
                    ];
74 1
                },
75 1
            ],
76
            'view' => [
77 1
                'class' => ViewAction::class,
78 1
            ],
79 1
            'validate-form' => [
80
                'class' => ValidateFormAction::class,
81 1
            ],
82 1
            'create' => [
83 1
                'class' => SmartCreateAction::class,
84 1
                'data' => function ($action) {
0 ignored issues
show
Unused Code introduced by
The parameter $action is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
85
                    list($billTypes, $billGroupLabels) = $this->getTypesAndGroups();
86
87
                    return compact('billTypes', 'billGroupLabels');
88
                },
89
                'success' => Yii::t('hipanel/finance', 'Bill was created successfully'),
90
            ],
91
            'update' => [
92
                'class' => SmartUpdateAction::class,
93
                'success' => Yii::t('hipanel/finance', 'Bill was updated successfully'),
94
                'data' => function ($action) {
0 ignored issues
show
Unused Code introduced by
The parameter $action is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
95
                    list($billTypes, $billGroupLabels) = $this->getTypesAndGroups();
96
97
                    return compact('billTypes', 'billGroupLabels');
98
                },
99
            ],
100
            'delete' => [
101
                'class' => SmartPerformAction::class,
102
                'success' => Yii::t('hipanel/finance', 'Bill was deleted successfully'),
103
            ],
104
        ];
105
    }
106
107
    public function actionImport()
108
    {
109
        list($billTypes, $billGroupLabels) = $this->getTypesAndGroups();
110
111
        $model = new BillImportForm();
112
        $model->setTypes($billTypes);
113
114
        if (Yii::$app->request->isPost && $model->load(Yii::$app->request->post())) {
115
            $models = $model->parse();
116
117
            if ($models !== false) {
118
                return $this->render('create', [
119
                    'models' => $models,
120
                    'model' => reset($models),
121
                    'billTypes' => $billTypes,
122
                    'billGroupLabels' => $billGroupLabels,
123
                ]);
124
            }
125
        }
126
127
        return $this->render('import', ['model' => $model]);
128
    }
129
130
    /**
131
     * @return array
132
     */
133
    public function getPaymentTypes()
134
    {
135
        $options = ['orderby' => 'name_asc'];
136
137
        if (Yii::$app->user->can('support')) {
138
            $options['with_hierarchy'] = true;
139
        }
140
141
        return $this->getRefs('type,bill', 'hipanel/finance', $options);
142
    }
143
144
    /**
145
     * @return array
146
     */
147
    private function getTypesAndGroups()
148
    {
149
        $billTypes = [];
150
        $billGroupLabels = [];
151
152
        $types = $this->getPaymentTypes();
153
154
        foreach ($types as $key => $title) {
155
            list($type, $name) = explode(',', $key);
156
157
            if (!isset($billTypes[$type])) {
158
                $billTypes[$type] = [];
159
                $billGroupLabels[$type] = ['label' => $title];
160
            }
161
162
            if (isset($name)) {
163
                foreach ($types as $k => $t) {
164
                    if (strpos($k, $type . ',') === 0) {
165
                        $billTypes[$type][$k] = $t;
166
                    }
167
                }
168
            }
169
        }
170
171
        return [$billTypes, $billGroupLabels];
172
    }
173
}
174