Completed
Push — master ( 9535ad...707351 )
by Andrii
06:02
created

ValueColumn::regesterClientScript()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 44

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 44
ccs 0
cts 43
cp 0
rs 9.216
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
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\grid;
12
13
use hipanel\modules\finance\assets\PriceEstimator;
14
use Yii;
15
use yii\grid\Column;
16
use yii\helpers\Html;
17
use yii\helpers\Url;
18
19
class ValueColumn extends Column
20
{
21
    public $attribute = 'value';
22
23
    /**
24
     * @var integer
25
     */
26
    private $planId;
27
28
    public function init()
29
    {
30
        parent::init();
31
        $this->header = Yii::t('hipanel.finance.plan', 'Estimated value');
32
        $this->planId = $this->findPlanId();
33
        $this->regesterClientScript();
34
    }
35
36
    protected function renderDataCellContent($model, $key, $index)
37
    {
38
        $fields = Html::hiddenInput("[{$model->id}]object_id", $model->object_id);
39
        $fields .= Html::hiddenInput("[{$model->id}]type", $model->type);
40
41
        return Html::tag('span', Html::tag('span', Html::tag('i', $fields, [
42
            'class' => 'fa fa-refresh fa-spin fa-fw',
43
            'data' => ['id' => $model->id],
44
        ]), ['class' => 'price-estimates']), ['class' => 'price-item', 'data' => ['id' => $model->id]]);
45
    }
46
47
    private function regesterClientScript()
48
    {
49
        $calculateValueUrl = Url::toRoute(['@plan/calculate-value', 'planId' => $this->planId]);
50
        $view = Yii::$app->view;
51
        $view->registerAssetBundle(PriceEstimator::class);
52
        $view->registerJs(/** @lang ECMAScript 6 */
53
            "
54
        ;(function ($, window, document, undefined) {
55
            let Estimator = $('#bulk-plan').priceEstimator({
56
                rowSelector: '.price-item',
57
            });
58
            function drawPlanTotal(rows) {
59
                let totalCell = $('#plan-monthly-value'), sum = '&mdash;';
60
                totalCell.html('');
61
                Object.keys(rows).forEach(period => {
62
                    let estimate = rows[period];
63
                    if (estimate) {
64
                        sum = estimate['sumFormatted'];
65
                    }
66
67
                    if (totalCell.html().length === 0) {
68
                        totalCell.append($('<strong>').attr({title: period}).html(sum));
69
                    } else {
70
                        totalCell.append('; ');
71
                        totalCell.append($('<i>').attr({title: period}).html(sum));
72
                    }
73
                });
74
            }
75
            $.ajax({
76
                method: 'post',
77
                url: '{$calculateValueUrl}',
78
                success: json => {
79
                    Object.keys(json).forEach(period => Estimator.rememberEstimates(period, json[period].targets));
80
                    Estimator.drawEstimates();
81
                    drawPlanTotal(json);
82
                },
83
                error: xhr => {
84
                    hipanel.notify.error(xhr.statusText);
85
                    $('.price-estimates').text('--');
86
                }
87
            });
88
        })(jQuery, window, document);
89
");
90
    }
91
92
    private function findPlanId()
93
    {
94
        return Yii::$app->request->get('id');
95
    }
96
}
97