Completed
Push — master ( c6b5de...db50e7 )
by Dmitry
04:45
created

src/controllers/PurseController.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\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()
0 ignored issues
show
This method seems to be duplicated in your project.

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.

Loading history...
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
                Purse::batchPerform('generate-and-save-all-monthly-documents', [
92
                    'type' => $type,
93
                    'client_types' => $type === 'acceptance' ? 'employee' : null,
94
                ]);
95
            }
96
97
            return $this->render('generate-all', ['statisticByTypes' => $statisticByTypes]);
98
        }
99
    }
100
101
    public function actionGenerateMonthlyDocument($id, $type, $month = null)
102
    {
103
        return $this->generateDocument('generate-monthly-document', compact('id', 'type', 'month'));
104
    }
105
106
    public function actionGenerateDocument($id, $type)
107
    {
108
        return $this->generateDocument('generate-document', compact('id', 'type'));
109
    }
110
111
    public function generateDocument($action, $params)
112
    {
113
        try {
114
            $data = Purse::perform($action, $params);
115
        } catch (ResponseErrorException $e) {
116
            Yii::$app->getSession()->setFlash('error', Yii::t('hipanel:finance', 'Failed to generate document! Check requisites!'));
117
118
            return $this->redirect(['@client/view', 'id' => $params['id']]);
119
        }
120
        $this->asPdf();
121
122
        return $data;
123
    }
124
125
    protected function asPdf()
126
    {
127
        $response = Yii::$app->getResponse();
128
        $response->format = $response::FORMAT_RAW;
129
        $response->getHeaders()->add('content-type', 'application/pdf');
130
    }
131
132
    public function actionPreGenerateDocument($type)
133
    {
134
        $purse = new Purse(['scenario' => 'generate-and-save-monthly-document']);
135
        if ($purse->load(Yii::$app->request->post()) && $purse->validate()) {
136
            return $this->redirect([
137
                '@purse/generate-monthly-document',
138
                'id' => $purse->id,
139
                'type' => $type,
140
                'month' => $purse->month,
141
            ]);
142
        }
143
    }
144
}
145