Completed
Push — master ( 76d596...34b53d )
by Dmitry
04:27
created

BillController::getPaymentTypes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
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\RedirectAction;
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\client\controllers\ContactController;
22
use hipanel\modules\finance\forms\BillImportForm;
23
use hipanel\modules\finance\forms\CurrencyExchangeForm;
24
use hipanel\modules\finance\models\Bill;
25
use hipanel\modules\finance\models\ExchangeRate;
26
use hipanel\modules\finance\providers\BillTypesProvider;
27
use hipanel\modules\finance\providers\ExchangeRatesProvider;
28
use Yii;
29
use yii\base\Module;
30
use yii\filters\AccessControl;
31
32
class BillController extends \hipanel\base\CrudController
33
{
34
    /**
35
     * @var BillTypesProvider
36
     */
37
    private $billTypesProvider;
38
39
    public function __construct($id, Module $module, BillTypesProvider $billTypesProvider, array $config = [])
40
    {
41
        parent::__construct($id, $module, $config);
42
43
44
        $this->billTypesProvider = $billTypesProvider;
45
    }
46
47
    public function behaviors()
48
    {
49
        return array_merge(parent::behaviors(), [
50
            'access-bill' => [
51
                'class' => AccessControl::class,
52
                'only' => ['index', 'view', 'create', 'update', 'delete', 'create-exchange'],
53
                'rules' => [
54
                    [
55
                        'allow' => true,
56
                        'roles' => ['manage', 'deposit'],
57
                        'actions' => ['index', 'view'],
58
                    ],
59
                    [
60
                        'allow' => true,
61
                        'roles' => ['bill.create'],
62
                        'actions' => ['create', 'import', 'copy', 'create-exchange'],
63
                    ],
64
                    [
65
                        'allow' => true,
66
                        'roles' => ['bill.update'],
67
                        'actions' => ['update'],
68
                    ],
69
                    [
70
                        'allow' => true,
71
                        'roles' => ['bill.delete'],
72
                        'actions' => ['delete'],
73
                    ],
74
                ],
75
            ],
76
        ]);
77
    }
78
79
    public function actions()
80
    {
81
        return [
82
            'set-orientation' => [
83
                'class' => OrientationAction::class,
84
                'allowedRoutes' => [
85
                    '@bill/index',
86
                ],
87
            ],
88
            'index' => [
89
                'class' => IndexAction::class,
90
                '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...
91
                    list($billTypes, $billGroupLabels) = $this->getTypesAndGroups();
92
                    $rates = $this->getExchangeRates();
93
94
                    return compact('billTypes', 'billGroupLabels', 'rates');
95
                },
96
            ],
97
            'view' => [
98
                'class' => ViewAction::class,
99
            ],
100
            'validate-form' => [
101
                'class' => ValidateFormAction::class,
102
            ],
103
            'create' => [
104
                'class' => SmartCreateAction::class,
105
                '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...
106
                    list($billTypes, $billGroupLabels) = $this->getTypesAndGroups();
107
108
                    return compact('billTypes', 'billGroupLabels');
109
                },
110
                'success' => Yii::t('hipanel:finance', 'Payment was created successfully'),
111
            ],
112
            'update' => [
113
                'class' => SmartUpdateAction::class,
114
                'success' => Yii::t('hipanel:finance', 'Payment was updated successfully'),
115
                '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...
116
                    list($billTypes, $billGroupLabels) = $this->getTypesAndGroups();
117
118
                    return compact('billTypes', 'billGroupLabels');
119
                },
120
            ],
121
            'copy' => [
122
                'class' => SmartUpdateAction::class,
123
                'scenario' => 'create',
124
                'data' => function ($action, $data) {
125
                    foreach ($data['models'] as $model) {
126
                        /** @var Bill $model */
127
                        $model->prepareToCopy();
128
                    }
129
130
                    list($billTypes, $billGroupLabels) = $this->getTypesAndGroups();
131
132
                    return compact('billTypes', 'billGroupLabels');
133
                },
134
            ],
135
            'delete' => [
136
                'class' => SmartPerformAction::class,
137
                'success' => Yii::t('hipanel:finance', 'Payment was deleted successfully'),
138
            ],
139
            'requisites' => [
140
                'class' => RedirectAction::class,
141
                'url' => 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...
142
                    $identity = Yii::$app->user->identity;
143
                    $seller = $identity->type === $identity::TYPE_RESELLER ? $identity->username : $identity->seller;
144
                    if ($seller === 'bullet') {
145
                        $seller = 'dsr';
146
                    }
147
148
                    return array_merge(ContactController::getSearchUrl(['client' => $seller]), ['representation' => 'requisites']);
149
                },
150
            ],
151
        ];
152
    }
153
154
    public function actionImport()
155
    {
156
        $model = new BillImportForm([
157
            'billTypes' => array_filter($this->getPaymentTypes(), function ($key) {
158
                // Kick out items that are categories names, but not real types
159
                return strpos($key, ',') !== false;
160
            }, ARRAY_FILTER_USE_KEY),
161
        ]);
162
163
        if (Yii::$app->request->isPost && $model->load(Yii::$app->request->post())) {
164
            $models = $model->parse();
165
166
            if ($models !== false) {
167
                list($billTypes, $billGroupLabels) = $this->getTypesAndGroups();
168
169
                return $this->render('create', [
170
                    'models' => $models,
171
                    'model' => reset($models),
172
                    'billTypes' => $billTypes,
173
                    'billGroupLabels' => $billGroupLabels,
174
                ]);
175
            }
176
        }
177
178
        return $this->render('import', ['model' => $model]);
179
    }
180
181
    public function actionCreateExchange()
182
    {
183
        $model = new CurrencyExchangeForm();
184
185
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
186
            if ($id = $model->save()) {
0 ignored issues
show
Unused Code introduced by
$id is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
187
                Yii::$app->session->addFlash('success', Yii::t('hipanel:finance', 'Currency was exchanged successfully'));
188
                return $this->redirect(['@bill']);
189
            }
190
        }
191
192
        return $this->render('create-exchange', [
193
            'model' => $model,
194
            'rates' => $this->getExchangeRates()
195
        ]);
196
    }
197
198
    /**
199
     * @return array
200
     */
201
    public function getPaymentTypes()
202
    {
203
        return $this->billTypesProvider->getTypesList();
204
    }
205
206
    /**
207
     * @return array
208
     */
209
    private function getTypesAndGroups()
210
    {
211
        return $this->billTypesProvider->getGroupedList();
212
    }
213
214
    private function getExchangeRates()
215
    {
216
        return Yii::$app->cache->getOrSet(['exchange-rates', Yii::$app->user->id], function () {
217
            return ExchangeRate::find()->select(['from', 'to', 'rate'])->all();
218
        }, 3600);
219
    }
220
}
221