Completed
Push — master ( b864d9...3fa98b )
by Dmitry
07:42 queued 03:17
created

PriceInput::run()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 10
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 10
loc 10
ccs 0
cts 2
cp 0
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace hipanel\modules\finance\widgets;
4
5
use yii\base\Widget;
6
7 View Code Duplication
class PriceInput extends Widget
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
8
{
9
    public $basePrice = 0;
10
    public $originalPrice = 0;
11
    public $activeField;
12
13
    public function run()
14
    {
15
        $this->registerClientScript();
16
17
        return $this->render('PriceInput', [
18
            'basePrice' => $this->basePrice,
19
            'originalPrice' => $this->originalPrice,
20
            'activeField' => $this->activeField,
21
        ]);
22
    }
23
24
    public function registerClientScript()
25
    {
26
        $this->view->registerJs(<<<'JS'
27
            $('.price-input').on('change mouseup', function () {
28
                var price = parseFloat($(this).val());
29
                if (isNaN(price)) return false;
30
                var base = $(this).closest('td').find('.base-price'),
31
                    basePrice = parseFloat(base.attr('data-original-price')),
32
                    delta = price - basePrice;
33
34
                base.removeClass('text-success text-danger');
35
                base.text(delta.toFixed(2)).addClass(delta >= 0 ? 'text-success' : 'text-danger');
36
            });
37
        
38
            $('.price-input').trigger('change');
39
JS
40
        );
41
42
        $this->view->registerCss('
43
            .base-price { font-weight: bold; }
44
            .form-group.form-group-sm { margin-bottom: 0; }
45
        ');
46
    }
47
}
48