Completed
Push — master ( 8c1c91...aeff30 )
by Dmitry
05:55
created

BillController   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 145
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 32.94%

Importance

Changes 0
Metric Value
wmc 9
c 0
b 0
f 0
lcom 1
cbo 5
dl 0
loc 145
ccs 28
cts 85
cp 0.3294
rs 10

5 Methods

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