Completed
Push — master ( c2bc12...ef5bc2 )
by Dmitry
04:55
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()
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!'));
0 ignored issues
show
The method getSession does only exist in yii\web\Application, but not in yii\console\Application.

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:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
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