Completed
Push — master ( 8c4cd5...9101e8 )
by Andrii
27:14 queued 12:24
created

src/controllers/BillController.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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-2019, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\modules\finance\controllers;
12
13
use hipanel\actions\Action;
14
use hipanel\actions\IndexAction;
15
use hipanel\actions\RedirectAction;
16
use hipanel\actions\SmartCreateAction;
17
use hipanel\actions\SmartDeleteAction;
18
use hipanel\actions\ValidateFormAction;
19
use hipanel\actions\ViewAction;
20
use hipanel\filters\EasyAccessControl;
21
use hipanel\modules\client\controllers\ContactController;
22
use hipanel\modules\finance\actions\BillManagementAction;
23
use hipanel\modules\finance\forms\BillForm;
24
use hipanel\modules\finance\forms\BillImportForm;
25
use hipanel\modules\finance\forms\CurrencyExchangeForm;
26
use hipanel\modules\finance\helpers\ChargesGrouper;
27
use hipanel\modules\finance\models\ExchangeRate;
28
use hipanel\modules\finance\models\Resource;
29
use hipanel\modules\finance\providers\BillTypesProvider;
30
use hiqdev\hiart\ActiveQuery;
31
use hiqdev\hiart\Collection;
32
use Yii;
33
use yii\base\Event;
34
use yii\base\Module;
35
36
class BillController extends \hipanel\base\CrudController
37
{
38
    /**
39
     * @var BillTypesProvider
40
     */
41
    private $billTypesProvider;
42
43
    public function __construct($id, Module $module, BillTypesProvider $billTypesProvider, array $config = [])
44
    {
45
        parent::__construct($id, $module, $config);
46
47
        $this->billTypesProvider = $billTypesProvider;
48
    }
49
50
    public function behaviors()
51
    {
52
        return array_merge(parent::behaviors(), [
53
            'access-bill' => [
54
                'class' => EasyAccessControl::class,
55
                'actions' => [
56
                    'create,copy'           => 'bill.create',
57
                    'create-exchange'       => 'bill.create-exchange',
58
                    'create-transfer'       => 'bill.create',
59
                    'import'                => 'bill.import',
60
                    'update,charge-delete'  => 'bill.update',
61
                    'delete'                => 'bill.delete',
62
                    '*'                     => 'bill.read',
63
                ],
64
            ],
65
        ]);
66
    }
67
68
    public function actions()
69
    {
70
        return array_merge(parent::actions(), [
71
            'index' => [
72
                'class' => IndexAction::class,
73
                'data' => function ($action) {
74
                    list($billTypes, $billGroupLabels) = $this->getTypesAndGroups();
75
                    $rates = $this->getExchangeRates();
76
77
                    return compact('billTypes', 'billGroupLabels', 'rates');
78
                },
79
            ],
80
            'view' => [
81
                'class' => ViewAction::class,
82
                'on beforePerform' => function (Event $event) {
83
                    /** @var \hipanel\actions\SearchAction $action */
84
                    $action = $event->sender;
85
                    $dataProvider = $action->getDataProvider();
86
                    $dataProvider->query
87
                        ->joinWith(['charges' => function (ActiveQuery $query) {
88
                            $query->joinWith('commonObject');
89
                            $query->joinWith('latestCommonObject');
90
                        }])
91
                        ->andWhere(['with_charges' => true]);
92
                },
93
                'data' => function (Action $action, array $data) {
94
                    return array_merge($data, [
95
                        'grouper' => new ChargesGrouper($data['model']->charges),
96
                    ]);
97
                },
98
            ],
99
            'validate-form' => [
100
                'class' => ValidateFormAction::class,
101
            ],
102
            'validate-bill-form' => [
103
                'class' => ValidateFormAction::class,
104
                'collection' => [
105
                    'class' => Collection::class,
106
                    'model' => new BillForm(),
107
                ],
108
            ],
109
            'create' => [
110
                'class' => BillManagementAction::class,
111
            ],
112
            'update' => [
113
                'class' => BillManagementAction::class,
114
            ],
115
            'copy' => [
116
                'class' => BillManagementAction::class,
117
                'view' => 'create',
118
                'scenario' => 'create',
119
                'forceNewRecord' => true,
120
            ],
121
            'create-transfer' => [
122
                'class' => SmartCreateAction::class,
123
                'success' => Yii::t('hipanel:finance', 'Transfer was completed'),
124
                'POST html' => [
125
                    'save' => true,
126
                    'success' => [
127
                        'class' => RedirectAction::class,
128
                        'url' => function ($action) {
0 ignored issues
show
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...
129
                            return ['@bill/index'];
130
                        },
131
                    ],
132
                ],
133
            ],
134
            'delete' => [
135
                'class' => SmartDeleteAction::class,
136
                'success' => Yii::t('hipanel:finance', 'Payment was deleted successfully'),
137
            ],
138
            'charge-delete' => [
139
                'class' => SmartDeleteAction::class,
140
                'success' => Yii::t('hipanel:finance', 'Charge was deleted successfully'),
141
                'collection' => [
142
                    'class'     => Collection::class,
143
                    'model'     => new Resource(['scenario' => 'delete']),
144
                    'scenario'  => 'delete',
145
                ],
146
            ],
147
            'requisites' => [
148
                'class' => RedirectAction::class,
149
                'url' => function ($action) {
150
                    $identity = Yii::$app->user->identity;
151
                    $seller = $identity->type === $identity::TYPE_RESELLER ? $identity->username : $identity->seller;
152
                    if ($seller === 'bullet') {
153
                        $seller = 'dsr';
154
                    }
155
156
                    return array_merge(ContactController::getSearchUrl(['client' => $seller]), ['representation' => 'requisites']);
157
                },
158
            ],
159
        ]);
160
    }
161
162
    public function actionImport()
163
    {
164
        $model = new BillImportForm([
165
            'billTypes' => array_filter($this->getPaymentTypes(), function ($key) {
166
                // Kick out items that are categories names, but not real types
167
                return strpos($key, ',') !== false;
168
            }, ARRAY_FILTER_USE_KEY),
169
        ]);
170
171
        if (Yii::$app->request->isPost && $model->load(Yii::$app->request->post())) {
172
            $models = $model->parse();
173
174
            if ($models !== false) {
175
                $models = BillForm::createMultipleFromBills($models, 'create');
176
                list($billTypes, $billGroupLabels) = $this->getTypesAndGroups();
177
178
                return $this->render('create', [
179
                    'models' => $models,
180
                    'model' => reset($models),
181
                    'billTypes' => $billTypes,
182
                    'billGroupLabels' => $billGroupLabels,
183
                ]);
184
            }
185
        }
186
187
        return $this->render('import', ['model' => $model]);
188
    }
189
190
    public function actionCreateExchange()
191
    {
192
        $model = new CurrencyExchangeForm();
193
194
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
195
            if ($id = $model->save()) {
196
                Yii::$app->session->addFlash('success', Yii::t('hipanel:finance', 'Currency was exchanged successfully'));
197
198
                return $this->redirect(['@bill']);
199
            }
200
        }
201
202
        return $this->render('create-exchange', [
203
            'model' => $model,
204
            'rates' => $this->getExchangeRates(),
205
        ]);
206
    }
207
208
    /**
209
     * @return array
210
     */
211
    public function getPaymentTypes()
212
    {
213
        return $this->billTypesProvider->getTypesList();
214
    }
215
216
    /**
217
     * @return array
218
     */
219
    private function getTypesAndGroups()
220
    {
221
        return $this->billTypesProvider->getGroupedList();
222
    }
223
224
    private function getExchangeRates()
225
    {
226
        return Yii::$app->cache->getOrSet(['exchange-rates', Yii::$app->user->id], function () {
227
            return ExchangeRate::find()->select(['from', 'to', 'rate'])->all();
228
        }, 3600);
229
    }
230
}
231