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\IndexAction; |
14
|
|
|
use hipanel\actions\RedirectAction; |
15
|
|
|
use hipanel\actions\SmartPerformAction; |
16
|
|
|
use hipanel\actions\SmartUpdateAction; |
17
|
|
|
use hipanel\actions\ValidateFormAction; |
18
|
|
|
use hipanel\actions\ViewAction; |
19
|
|
|
use hipanel\filters\EasyAccessControl; |
20
|
|
|
use hipanel\modules\document\models\Statistic as DocumentStatisticModel; |
21
|
|
|
use hipanel\modules\finance\models\Purse; |
22
|
|
|
use hipanel\modules\finance\widgets\StatisticTableGenerator; |
23
|
|
|
use hiqdev\hiart\ResponseErrorException; |
24
|
|
|
use Yii; |
25
|
|
|
|
26
|
|
|
class PurseController extends \hipanel\base\CrudController |
27
|
|
|
{ |
28
|
|
View Code Duplication |
public function behaviors() |
|
|
|
|
29
|
|
|
{ |
30
|
|
|
return array_merge(parent::behaviors(), [ |
31
|
|
|
[ |
32
|
|
|
'class' => EasyAccessControl::class, |
33
|
|
|
'actions' => [ |
34
|
|
|
'update,update-requisite,update-contact' => 'purse.update', |
35
|
|
|
'pre-generate-document,generate-monthly-document,generate-document' => 'document.generate', |
36
|
|
|
'*' => 'bill.read', |
37
|
|
|
], |
38
|
|
|
], |
39
|
|
|
]); |
40
|
|
|
} |
41
|
1 |
|
|
42
|
|
|
public function actions() |
43
|
1 |
|
{ |
44
|
1 |
|
return array_merge(parent::actions(), [ |
45
|
|
|
'index' => [ |
46
|
|
|
'class' => IndexAction::class, |
47
|
|
|
], |
48
|
|
|
'view' => [ |
49
|
|
|
'class' => ViewAction::class, |
50
|
|
|
], |
51
|
|
|
'update' => [ |
52
|
|
|
'class' => SmartUpdateAction::class, |
53
|
|
|
], |
54
|
|
|
'update-contact' => [ |
55
|
|
|
'class' => SmartUpdateAction::class, |
56
|
|
|
], |
57
|
|
|
'update-requisite' => [ |
58
|
|
|
'class' => SmartUpdateAction::class, |
59
|
|
|
], |
60
|
|
|
'validate-form' => [ |
61
|
|
|
'class' => ValidateFormAction::class, |
62
|
|
|
], |
63
|
|
|
'invoice-archive' => [ |
64
|
1 |
|
'class' => RedirectAction::class, |
65
|
|
|
'error' => Yii::t('hipanel', 'Under construction'), |
66
|
|
|
], |
67
|
|
|
'generate-and-save-monthly-document' => [ |
68
|
1 |
|
'class' => SmartPerformAction::class, |
69
|
|
|
'success' => Yii::t('hipanel:finance', 'Document updated'), |
70
|
|
|
], |
71
|
|
|
'generate-and-save-document' => [ |
72
|
1 |
|
'class' => SmartPerformAction::class, |
73
|
|
|
'success' => Yii::t('hipanel:finance', 'Document updated'), |
74
|
|
|
], |
75
|
|
|
]); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function actionGenerateAll() |
79
|
|
|
{ |
80
|
|
|
$request = Yii::$app->request; |
81
|
|
|
$model = new DocumentStatisticModel(); |
82
|
|
|
$statisticByTypes = DocumentStatisticModel::batchPerform('get-stats', $model->getAttributes([ |
83
|
|
|
'types', |
84
|
|
|
'since', |
85
|
|
|
])); |
86
|
|
|
|
87
|
|
|
$type = $request->post('type'); |
88
|
|
|
if ($request->isAjax && $type && in_array($type, explode(',', $model->types), true)) { |
89
|
|
|
return StatisticTableGenerator::widget(['type' => $type, 'statistic' => $statisticByTypes[$type]]); |
90
|
|
|
} else { |
91
|
|
|
if ($request->isPost) { |
92
|
|
|
try { |
93
|
|
|
Purse::batchPerform('generate-and-save-all-monthly-documents', [ |
94
|
|
|
'type' => $type, |
95
|
|
|
'client_types' => $type === 'acceptance' ? 'employee' : null, |
96
|
|
|
]); |
97
|
|
|
} catch (ResponseErrorException $e) { |
98
|
|
|
Yii::$app->getSession()->setFlash('error', Yii::t('hipanel:finance', 'Failed to generate document! Check requisites!')); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $this->render('generate-all', ['statisticByTypes' => $statisticByTypes]); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function actionGenerateMonthlyDocument($id, $type, $month = null) |
107
|
|
|
{ |
108
|
|
|
return $this->generateDocument('generate-monthly-document', compact('id', 'type', 'month')); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function actionGenerateDocument($id, $type) |
112
|
|
|
{ |
113
|
|
|
return $this->generateDocument('generate-document', compact('id', 'type')); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function generateDocument($action, $params) |
117
|
|
|
{ |
118
|
|
|
try { |
119
|
|
|
$data = Purse::perform($action, $params); |
120
|
|
|
} catch (ResponseErrorException $e) { |
121
|
|
|
Yii::$app->getSession()->setFlash('error', Yii::t('hipanel:finance', 'Failed to generate document! Check requisites!')); |
122
|
|
|
|
123
|
|
|
return $this->redirect(['@client/view', 'id' => $params['id']]); |
124
|
|
|
} |
125
|
|
|
$this->asPdf(); |
126
|
|
|
|
127
|
|
|
return $data; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
protected function asPdf() |
131
|
|
|
{ |
132
|
|
|
$response = Yii::$app->getResponse(); |
133
|
|
|
$response->format = $response::FORMAT_RAW; |
134
|
|
|
$response->getHeaders()->add('content-type', 'application/pdf'); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function actionPreGenerateDocument($type) |
138
|
|
|
{ |
139
|
|
|
$purse = new Purse(['scenario' => 'generate-and-save-monthly-document']); |
140
|
|
|
if ($purse->load(Yii::$app->request->post()) && $purse->validate()) { |
141
|
|
|
return $this->redirect([ |
142
|
|
|
'@purse/generate-monthly-document', |
143
|
|
|
'id' => $purse->id, |
144
|
|
|
'type' => $type, |
145
|
|
|
'month' => $purse->month, |
146
|
|
|
]); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.