Completed
Push — master ( d558ad...b35973 )
by Dmitry
11s
created

PlanController   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 4
c 0
b 0
f 0
lcom 1
cbo 7
dl 0
loc 70
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A behaviors() 0 13 1
B actions() 0 42 1
A actionCreatePrices() 0 10 2
1
<?php
2
3
namespace hipanel\modules\finance\controllers;
4
5
use hipanel\actions\Action;
6
use hipanel\actions\IndexAction;
7
use hipanel\actions\SmartCreateAction;
8
use hipanel\actions\SmartUpdateAction;
9
use hipanel\actions\ValidateFormAction;
10
use hipanel\actions\ViewAction;
11
use hipanel\base\CrudController;
12
use hipanel\modules\finance\helpers\PlanInternalsGrouper;
13
use hipanel\modules\finance\models\Plan;
14
use hipanel\filters\EasyAccessControl;
15
use hiqdev\hiart\Query;
16
use Yii;
17
use yii\base\Event;
18
use yii\web\NotFoundHttpException;
19
20
class PlanController extends CrudController
21
{
22
    public function behaviors()
23
    {
24
        return array_merge(parent::behaviors(), [
25
            [
26
                'class' => EasyAccessControl::class,
27
                'actions' => [
28
                    'create' => 'plan.create',
29
                    'update' => 'plan.update',
30
                    '*' => 'plan.read',
31
                ],
32
            ],
33
        ]);
34
    }
35
36
    public function actions()
37
    {
38
        return array_merge(parent::actions(), [
39
            'create' => [
40
                'class' => SmartCreateAction::class,
41
            ],
42
            'update' => [
43
                'class' => SmartUpdateAction::class,
44
            ],
45
            'index' => [
46
                'class' => IndexAction::class,
47
            ],
48
            'view' => [
49
                'class' => ViewAction::class,
50
                'on beforePerform' => function (Event $event) {
51
                    $action = $event->sender;
52
                    $action->getDataProvider()->query
53
                        ->joinWith('sales')
54
                        ->with([
55
                            'prices' => function (Query $query) {
56
                                $query
57
                                    ->addSelect('main_object_id')
58
                                    ->joinWith('object')
59
                                    ->limit('ALL');
60
                            },
61
                        ]);
62
                },
63
                'data' => function (Action $action, array $data) {
64
                    return array_merge($data, [
65
                        'grouper' => new PlanInternalsGrouper($data['model']),
66
                    ]);
67
                },
68
            ],
69
            'set-note' => [
70
                'class' => SmartUpdateAction::class,
71
                'success' => Yii::t('hipanel', 'Note changed'),
72
            ],
73
            'validate-form' => [
74
                'class' => ValidateFormAction::class,
75
            ],
76
        ]);
77
    }
78
79
    public function actionCreatePrices($id)
80
    {
81
        $plan = Plan::findOne(['id' => $id]);
82
        if ($plan === null) {
83
            throw new NotFoundHttpException('Not found');
84
        }
85
        $this->layout = false;
86
87
        return $this->renderAjax('_createPrices', ['plan' => $plan]);
88
    }
89
}
90