Completed
Push — master ( 4964bd...7a50d9 )
by Dmitry
05:40
created

PriceController::actionSuggest()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 27
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 27
rs 8.8571
cc 2
eloc 17
nc 2
nop 3
1
<?php
2
3
namespace hipanel\modules\finance\controllers;
4
5
use hipanel\actions\IndexAction;
6
use hipanel\actions\RedirectAction;
7
use hipanel\actions\SmartCreateAction;
8
use hipanel\actions\SmartDeleteAction;
9
use hipanel\actions\SmartUpdateAction;
10
use hipanel\actions\ValidateFormAction;
11
use hipanel\actions\ViewAction;
12
use hipanel\base\CrudController;
13
use hipanel\helpers\ArrayHelper;
14
use hipanel\modules\finance\models\TargetObject;
15
use hipanel\modules\finance\models\Plan;
16
use hipanel\modules\finance\models\Price;
17
use Yii;
18
use yii\rest\DeleteAction;
19
20
class PriceController extends CrudController
21
{
22
    public function actions()
23
    {
24
        return array_merge(parent::actions(), [
25
            'create' => [
26
                'class' => SmartCreateAction::class,
27
                'data' => function ($action, $data) {
0 ignored issues
show
Unused Code introduced by
The parameter $action is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $data is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
28
                    $plan = null;
29
                    if ($plan_id = Yii::$app->request->get('plan_id')) {
30
                        $plan = Plan::findOne(['id' => $plan_id]);
31
                    }
32
33
                    return compact('plan');
34
                },
35
                'success' => Yii::t('hipanel.finance.price', 'Prices were successfully created')
36
            ],
37
            'create-suggested' => [
38
                'class' => SmartCreateAction::class,
39
                'scenario' => 'create',
40
                'POST' => [
41
                    'save' => true,
42
                    'success' => [
43
                        'class' => RedirectAction::class,
44
                        'url' => function (RedirectAction $action) {
45
                            return ['@plan/view', 'id' => $action->collection->getModel()->plan_id];
46
                        },
47
                    ],
48
                ],
49
                'success' => Yii::t('hipanel.finance.price', 'Prices were successfully created')
50
            ],
51
            'update' => [
52
                'class' => SmartUpdateAction::class,
53
                'success' => Yii::t('hipanel.finance.price', 'Prices were successfully updated')
54
            ],
55
            'index' => [
56
                'class' => IndexAction::class,
57
            ],
58
            'view' => [
59
                'class' => ViewAction::class,
60
            ],
61
            'delete' => [
62
                'class' => SmartDeleteAction::class,
63
                'success' => Yii::t('hipanel.finance.price', 'Prices were successfully deleted')
64
            ],
65
            'set-note' => [
66
                'class' => SmartUpdateAction::class,
67
                'success' => Yii::t('hipanel', 'Note changed'),
68
            ],
69
            'validate-form' => [
70
                'class' => ValidateFormAction::class,
71
            ],
72
        ]);
73
    }
74
75
    public function actionSuggest($object_id, $plan_id, $type = 'default')
76
    {
77
        $plan = Plan::findOne(['id' => $plan_id]);
78
79
        $suggestions = (new Price)->batchQuery('suggest', [
80
            'object_id' => $object_id,
81
            'plan_id' => $plan_id,
82
            'type' => $type,
83
        ]);
84
85
        $models = [];
86
        foreach ($suggestions as $suggestion) {
87
            $object = ArrayHelper::remove($suggestion, 'object');
88
89
            $price = new Price($suggestion);
90
            $price->populateRelation('object', new TargetObject($object));
91
92
            $models[] = $price;
93
        }
94
95
        return $this->render('suggested', [
96
            'type' => $type,
97
            'model' => reset($models),
98
            'models' => $models,
99
            'plan' => $plan,
100
        ]);
101
    }
102
}
103