Completed
Push — master ( 82639b...2d93b0 )
by Andrii
04:55
created

PurseController::actionGenerateAll()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 21
ccs 0
cts 10
cp 0
rs 8.7624
cc 5
eloc 14
nc 3
nop 0
crap 30
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-2017, 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\modules\finance\widgets\StatisticTableGenerator;
20
use hipanel\modules\finance\models\Purse;
21
use hiqdev\hiart\ResponseErrorException;
22
use hipanel\modules\document\models\Statistic as DocumentStatisticModel;
23
use Yii;
24
25 1
class PurseController extends \hipanel\base\CrudController
26
{
27
    public function actions()
28
    {
29 1
        return array_merge(parent::actions(), [
30 1
            'index' => [
31
                'class' => IndexAction::class,
32 1
            ],
33 1
            'view' => [
34
                'class' => ViewAction::class,
35 1
            ],
36 1
            'update' => [
37
                'class' => SmartUpdateAction::class,
38 1
            ],
39 1
            'update-contact' => [
40
                'class' => SmartUpdateAction::class,
41 1
            ],
42 1
            'update-requisite' => [
43
                'class' => SmartUpdateAction::class,
44 1
            ],
45 1
            'validate-form' => [
46
                'class' => ValidateFormAction::class,
47 1
            ],
48 1
            'invoice-archive' => [
49 1
                'class' => RedirectAction::class,
50
                'error' => Yii::t('hipanel', 'Under construction'),
51 1
            ],
52 1
            'generate-and-save-monthly-document' => [
53 1
                'class' => SmartPerformAction::class,
54
                'success' => Yii::t('hipanel:finance', 'Document updated'),
55 1
            ],
56 1
            'generate-and-save-document' => [
57 1
                'class' => SmartPerformAction::class,
58 1
                'success' => Yii::t('hipanel:finance', 'Document updated'),
59
            ],
60
        ]);
61
    }
62
63
    public function actionGenerateAll()
64
    {
65
        $request = Yii::$app->request;
66
        $model = new DocumentStatisticModel();
67
        $statisticByTypes = DocumentStatisticModel::batchPerform('get-stats', $model->getAttributes([
68
            'types',
69
            'since',
70
        ]));
71
72
        $type = $request->post('type');
73
        if ($request->isAjax && $type && in_array($type, explode(',', $model->types))) {
74
            return StatisticTableGenerator::widget(['type' => $type, 'statistic' => $statisticByTypes[$type]]);
75
        } else {
76
            if ($request->isPost) {
77
                Purse::batchPerform('generate-and-save-all-monthly-documents', [
78
                    'type' => $type,
79
                ]);
80
            }
81
            return $this->render('generate-all', ['statisticByTypes' => $statisticByTypes]);
82
        }
83
    }
84
85
    public function actionGenerateMonthlyDocument($id, $type, $month = null)
86
    {
87
        return $this->generateDocument('generate-monthly-document', compact('id', 'type', 'month'));
88
    }
89
90
    public function actionGenerateDocument($id, $type)
91
    {
92
        return $this->generateDocument('generate-document', compact('id', 'type'));
93
    }
94
95
    public function generateDocument($action, $params)
96
    {
97
        try {
98
            $data = Purse::perform($action, $params);
99
        } catch (ResponseErrorException $e) {
100
            Yii::$app->getSession()->setFlash('error', Yii::t('hipanel:finance', 'Failed to generate document! Check requisites!'));
0 ignored issues
show
Bug introduced by
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...
101
102
            return $this->redirect(['@client/view', 'id' => $params['id']]);
103
        }
104
        $this->asPdf();
105
106
        return $data;
107
    }
108
109
    protected function asPdf()
110
    {
111
        $response = Yii::$app->getResponse();
112
        $response->format = $response::FORMAT_RAW;
113
        $response->getHeaders()->add('content-type', 'application/pdf');
114
    }
115
116
    public function actionPreGenerateDocument($type)
117
    {
118
        $purse = new Purse(['scenario' => 'generate-and-save-monthly-document']);
119
        if ($purse->load(Yii::$app->request->post()) && $purse->validate()) {
120
            return $this->redirect([
121
                '@purse/generate-monthly-document',
122
                'id' => $purse->id,
0 ignored issues
show
Documentation introduced by
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...
123
                'type' => $type,
124
                'month' => $purse->month,
0 ignored issues
show
Documentation introduced by
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...
125
            ]);
126
        }
127
    }
128
}
129