Completed
Push — master ( 67e324...0b8c6e )
by Andrii
09:22
created

BillController   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 151
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 10
c 0
b 0
f 0
lcom 1
cbo 5
dl 0
loc 151
ccs 0
cts 88
cp 0
rs 10

6 Methods

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