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
|
|
|
public function behaviors() |
29
|
|
|
{ |
30
|
|
|
return array_merge(parent::behaviors(), [ |
31
|
|
|
[ |
32
|
|
|
'class' => EasyAccessControl::class, |
33
|
|
|
'actions' => [ |
34
|
|
|
'update' => 'bill.update', |
35
|
|
|
'*' => 'bill.read', |
36
|
|
|
], |
37
|
|
|
], |
38
|
|
|
]); |
39
|
|
|
} |
40
|
|
|
|
41
|
1 |
|
public function actions() |
42
|
|
|
{ |
43
|
1 |
|
return array_merge(parent::actions(), [ |
44
|
1 |
|
'index' => [ |
45
|
|
|
'class' => IndexAction::class, |
46
|
|
|
], |
47
|
|
|
'view' => [ |
48
|
|
|
'class' => ViewAction::class, |
49
|
|
|
], |
50
|
|
|
'update' => [ |
51
|
|
|
'class' => SmartUpdateAction::class, |
52
|
|
|
], |
53
|
|
|
'update-contact' => [ |
54
|
|
|
'class' => SmartUpdateAction::class, |
55
|
|
|
], |
56
|
|
|
'update-requisite' => [ |
57
|
|
|
'class' => SmartUpdateAction::class, |
58
|
|
|
], |
59
|
|
|
'validate-form' => [ |
60
|
|
|
'class' => ValidateFormAction::class, |
61
|
|
|
], |
62
|
|
|
'invoice-archive' => [ |
63
|
|
|
'class' => RedirectAction::class, |
64
|
1 |
|
'error' => Yii::t('hipanel', 'Under construction'), |
65
|
|
|
], |
66
|
|
|
'generate-and-save-monthly-document' => [ |
67
|
|
|
'class' => SmartPerformAction::class, |
68
|
1 |
|
'success' => Yii::t('hipanel:finance', 'Document updated'), |
69
|
|
|
], |
70
|
|
|
'generate-and-save-document' => [ |
71
|
|
|
'class' => SmartPerformAction::class, |
72
|
1 |
|
'success' => Yii::t('hipanel:finance', 'Document updated'), |
73
|
|
|
], |
74
|
|
|
]); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function actionGenerateAll() |
78
|
|
|
{ |
79
|
|
|
$request = Yii::$app->request; |
80
|
|
|
$model = new DocumentStatisticModel(); |
81
|
|
|
$statisticByTypes = DocumentStatisticModel::batchPerform('get-stats', $model->getAttributes([ |
82
|
|
|
'types', |
83
|
|
|
'since', |
84
|
|
|
])); |
85
|
|
|
|
86
|
|
|
$type = $request->post('type'); |
87
|
|
|
if ($request->isAjax && $type && in_array($type, explode(',', $model->types), true)) { |
88
|
|
|
return StatisticTableGenerator::widget(['type' => $type, 'statistic' => $statisticByTypes[$type]]); |
89
|
|
|
} else { |
90
|
|
|
if ($request->isPost) { |
91
|
|
|
try { |
92
|
|
|
Purse::batchPerform('generate-and-save-all-monthly-documents', [ |
93
|
|
|
'type' => $type, |
94
|
|
|
'client_types' => $type === 'acceptance' ? 'employee' : null, |
95
|
|
|
]); |
96
|
|
|
} catch (ResponseErrorException $e) { |
97
|
|
|
Yii::$app->getSession()->setFlash('error', Yii::t('hipanel:finance', 'Failed to generate document! Check requisites!')); |
|
|
|
|
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $this->render('generate-all', ['statisticByTypes' => $statisticByTypes]); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function actionGenerateMonthlyDocument($id, $type, $month = null) |
106
|
|
|
{ |
107
|
|
|
return $this->generateDocument('generate-monthly-document', compact('id', 'type', 'month')); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function actionGenerateDocument($id, $type) |
111
|
|
|
{ |
112
|
|
|
return $this->generateDocument('generate-document', compact('id', 'type')); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function generateDocument($action, $params) |
116
|
|
|
{ |
117
|
|
|
try { |
118
|
|
|
$data = Purse::perform($action, $params); |
119
|
|
|
} catch (ResponseErrorException $e) { |
120
|
|
|
Yii::$app->getSession()->setFlash('error', Yii::t('hipanel:finance', 'Failed to generate document! Check requisites!')); |
|
|
|
|
121
|
|
|
|
122
|
|
|
return $this->redirect(['@client/view', 'id' => $params['id']]); |
123
|
|
|
} |
124
|
|
|
$this->asPdf(); |
125
|
|
|
|
126
|
|
|
return $data; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
protected function asPdf() |
130
|
|
|
{ |
131
|
|
|
$response = Yii::$app->getResponse(); |
132
|
|
|
$response->format = $response::FORMAT_RAW; |
133
|
|
|
$response->getHeaders()->add('content-type', 'application/pdf'); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function actionPreGenerateDocument($type) |
137
|
|
|
{ |
138
|
|
|
$purse = new Purse(['scenario' => 'generate-and-save-monthly-document']); |
139
|
|
|
if ($purse->load(Yii::$app->request->post()) && $purse->validate()) { |
140
|
|
|
return $this->redirect([ |
141
|
|
|
'@purse/generate-monthly-document', |
142
|
|
|
'id' => $purse->id, |
143
|
|
|
'type' => $type, |
144
|
|
|
'month' => $purse->month, |
145
|
|
|
]); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: