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

src/controllers/PurseController.php (2 issues)

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!'));
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,
0 ignored issues
show
The property id does not exist on object<hipanel\modules\finance\models\Purse>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
139
                'type' => $type,
140
                'month' => $purse->month,
0 ignored issues
show
The property month does not exist on object<hipanel\modules\finance\models\Purse>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
141
            ]);
142
        }
143
    }
144
}
145