Completed
Push — master ( e6d047...07dbbd )
by Andrii
05:21
created

BillController   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 167
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 95.65%

Importance

Changes 0
Metric Value
wmc 15
c 0
b 0
f 0
lcom 1
cbo 4
dl 0
loc 167
ccs 22
cts 23
cp 0.9565
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
B actionImport() 0 26 4
A getPaymentTypes() 0 10 2
B getTypesAndGroups() 0 26 6
B behaviors() 0 31 1
A actions() 0 61 2
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 hipanel\modules\finance\models\Bill;
23
use Yii;
24
use yii\filters\AccessControl;
25
26
class BillController extends \hipanel\base\CrudController
27
{
28
    public function behaviors()
29
    {
30
        return array_merge(parent::behaviors(), [
31
            'access-bill' => [
32
                'class' => AccessControl::class,
33
                'only' => ['index', 'view', 'create', 'update', 'delete'],
34
                'rules' => [
35
                    [
36
                        'allow' => true,
37
                        'roles' => ['manage', 'deposit'],
38
                        'actions' => ['index', 'view'],
39
                    ],
40
                    [
41
                        'allow' => true,
42
                        'roles' => ['bill.create'],
43
                        'actions' => ['create', 'import', 'copy'],
44
                    ],
45
                    [
46
                        'allow' => true,
47
                        'roles' => ['bill.update'],
48
                        'actions' => ['update'],
49
                    ],
50
                    [
51
                        'allow' => true,
52
                        'roles' => ['bill.delete'],
53
                        'actions' => ['delete'],
54
                    ],
55
                ],
56
            ],
57
        ]);
58
    }
59
60 1
    public function actions()
61
    {
62
        return [
63
            'set-orientation' => [
64
                'class' => OrientationAction::class,
65
                'allowedRoutes' => [
66
                    '@bill/index',
67
                ],
68 1
            ],
69
            'index' => [
70 1
                'class' => IndexAction::class,
71
                '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...
72
                    list($billTypes, $billGroupLabels) = $this->getTypesAndGroups();
73
74
                    return compact('billTypes', 'billGroupLabels');
75 1
                },
76 1
            ],
77
            'view' => [
78
                'class' => ViewAction::class,
79 1
            ],
80
            'validate-form' => [
81
                'class' => ValidateFormAction::class,
82 1
            ],
83
            'create' => [
84 1
                'class' => SmartCreateAction::class,
85
                '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...
86
                    list($billTypes, $billGroupLabels) = $this->getTypesAndGroups();
87
88
                    return compact('billTypes', 'billGroupLabels');
89 1
                },
90 1
                'success' => Yii::t('hipanel:finance', 'Payment was created successfully'),
91 1
            ],
92
            'update' => [
93 1
                'class' => SmartUpdateAction::class,
94 1
                'success' => Yii::t('hipanel:finance', 'Payment was updated successfully'),
95
                '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...
96
                    list($billTypes, $billGroupLabels) = $this->getTypesAndGroups();
97
98
                    return compact('billTypes', 'billGroupLabels');
99 1
                },
100
            ],
101
            'copy' => [
102 1
                'class' => SmartUpdateAction::class,
103 1
                'scenario' => 'create',
104
                'data' => function ($action, $data) {
105
                    foreach ($data['models'] as $model) {
106
                        /** @var Bill $model */
107
                        $model->prepareToCopy();
108
                    }
109
110
                    list($billTypes, $billGroupLabels) = $this->getTypesAndGroups();
111
112
                    return compact('billTypes', 'billGroupLabels');
113 1
                }
114 1
            ],
115
            'delete' => [
116 1
                'class' => SmartPerformAction::class,
117 1
                'success' => Yii::t('hipanel:finance', 'Payment was deleted successfully'),
118 1
            ],
119 1
        ];
120
    }
121
122
    public function actionImport()
123
    {
124
        $model = new BillImportForm([
125
            'billTypes' => array_filter($this->getPaymentTypes(), function ($key) {
126
                // Kick out items that are categories names, but not real types
127
                return (strpos($key, ',') !== false);
128
            }, ARRAY_FILTER_USE_KEY)
129
        ]);
130
131
        if (Yii::$app->request->isPost && $model->load(Yii::$app->request->post())) {
132
            $models = $model->parse();
133
134
            if ($models !== false) {
135
                list($billTypes, $billGroupLabels) = $this->getTypesAndGroups();
136
137
                return $this->render('create', [
138
                    'models' => $models,
139
                    'model' => reset($models),
140
                    'billTypes' => $billTypes,
141
                    'billGroupLabels' => $billGroupLabels,
142
                ]);
143
            }
144
        }
145
146
        return $this->render('import', ['model' => $model]);
147
    }
148
149
    /**
150
     * @return array
151
     */
152
    public function getPaymentTypes()
153
    {
154
        $options = ['orderby' => 'name_asc'];
155
156
        if (Yii::$app->user->can('support')) {
157
            $options['with_hierarchy'] = true;
158
        }
159
160
        return $this->getRefs('type,bill', 'hipanel:finance', $options);
161
    }
162
163
    /**
164
     * @return array
165
     */
166
    private function getTypesAndGroups()
167
    {
168
        $billTypes = [];
169
        $billGroupLabels = [];
170
171
        $types = $this->getPaymentTypes();
172
173
        foreach ($types as $key => $title) {
174
            list($type, $name) = explode(',', $key);
175
176
            if (!isset($billTypes[$type])) {
177
                $billTypes[$type] = [];
178
                $billGroupLabels[$type] = ['label' => $title];
179
            }
180
181
            if (isset($name)) {
182
                foreach ($types as $k => $t) {
183
                    if (strpos($k, $type . ',') === 0) {
184
                        $billTypes[$type][$k] = $t;
185
                    }
186
                }
187
            }
188
        }
189
190
        return [$billTypes, $billGroupLabels];
191
    }
192
}
193