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) { |
|
|
|
|
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) { |
|
|
|
|
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) { |
|
|
|
|
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
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.