Completed
Push — master ( 3bfee6...c87263 )
by Dmitry
29:30
created

PriceController::actionSuggest()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 31
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 31
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 22
nc 2
nop 3
1
<?php
2
3
namespace hipanel\modules\finance\controllers;
4
5
use hipanel\actions\IndexAction;
6
use hipanel\actions\SmartCreateAction;
7
use hipanel\actions\SmartUpdateAction;
8
use hipanel\actions\ValidateFormAction;
9
use hipanel\actions\ViewAction;
10
use hipanel\base\CrudController;
11
use hipanel\modules\finance\models\Plan;
12
use hipanel\modules\finance\models\Price;
13
use Yii;
14
15
class PriceController extends CrudController
16
{
17
    public function actions()
18
    {
19
        return array_merge(parent::actions(), [
20
            'create' => [
21
                'class' => SmartCreateAction::class,
22
                '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...
23
                    $plan = null;
24
                    if ($plan_id = Yii::$app->request->get('plan_id')) {
25
                        $plan = Plan::findOne(['id' => $plan_id]);
26
                    }
27
28
                    return compact('plan');
29
                },
30
            ],
31
            'update' => [
32
                'class' => SmartUpdateAction::class,
33
            ],
34
            'index' => [
35
                'class' => IndexAction::class,
36
            ],
37
            'view' => [
38
                'class' => ViewAction::class,
39
            ],
40
            'set-note' => [
41
                'class' => SmartUpdateAction::class,
42
                'success' => Yii::t('hipanel', 'Note changed'),
43
            ],
44
            'validate-form' => [
45
                'class' => ValidateFormAction::class,
46
            ],
47
        ]);
48
    }
49
50
    public function actionSuggest($object_id, $plan_id, $type = 'default')
51
    {
52
        $plan = Plan::findOne(['id' => $plan_id]);
53
54
        $suggestions = (new Price)->batchQuery('suggest', [
55
            'object_id' => $object_id,
56
            'plan_id' => $plan_id,
57
            'type' => $type
58
        ]);
59
60
        $models = [];
61
        foreach ($suggestions as $suggestion) {
62
            $models[] = new Price([
63
                'type' => $suggestion['type']['name'],
64
                'type_id' => $suggestion['type']['id'],
65
                'object_id' => $suggestion['target']['id'],
66
                'object' => $suggestion['target']['name'],
67
                'unit' => $suggestion['prepaid']['unit'],
68
                'quantity' => $suggestion['prepaid']['quantity'],
69
                'unit_id' => 0, // todo
70
                'price' => $suggestion['price']['amount'] / 100,
71
                'currency' => $suggestion['price']['currency']
72
            ]);
73
        }
74
75
        return $this->render('create', [
76
            'model' => reset($models),
77
            'models' => $models,
78
            'plan' => $plan
79
        ]);
80
    }
81
}
82