Completed
Push — master ( 14088c...41674b )
by Dmitry
03:58
created

BillController::actionImport()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 26
ccs 0
cts 0
cp 0
rs 8.5806
cc 4
eloc 15
nc 3
nop 0
crap 20
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\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
        /** @var BillTypesProvider $provider */
155
        $provider = Yii::createObject(BillTypesProvider::class);
156
157
        return $provider->getTypesList();
158
    }
159
160
    /**
161
     * @return array
162
     */
163
    private function getTypesAndGroups()
164
    {
165
        /** @var BillTypesProvider $provider */
166
        $provider = Yii::createObject(BillTypesProvider::class);
167
168
        return $provider->getGroupedList();
169
    }
170
}
171