|
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-2019, HiQDev (http://hiqdev.com/) |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace hipanel\modules\finance\widgets; |
|
12
|
|
|
|
|
13
|
|
|
use Money\Formatter\DecimalMoneyFormatter; |
|
14
|
|
|
use Money\Money; |
|
15
|
|
|
use Money\MoneyFormatter; |
|
16
|
|
|
use Yii; |
|
17
|
|
|
use yii\base\Widget; |
|
18
|
|
|
use yii\bootstrap\ActiveField; |
|
19
|
|
|
|
|
20
|
|
|
class PriceInput extends Widget |
|
21
|
|
|
{ |
|
22
|
|
|
/** @var Money */ |
|
23
|
|
|
public $basePrice; |
|
24
|
|
|
|
|
25
|
|
|
/** @var Money */ |
|
26
|
|
|
public $originalPrice; |
|
27
|
|
|
|
|
28
|
|
|
/** @var ActiveField */ |
|
29
|
|
|
public $activeField; |
|
30
|
|
|
|
|
31
|
|
|
/** @var MoneyFormatter */ |
|
32
|
|
|
private $moneyFormatter; |
|
33
|
|
|
|
|
34
|
|
|
public function __construct(DecimalMoneyFormatter $decimalMoneyFormatter, $config = []) |
|
35
|
|
|
{ |
|
36
|
|
|
parent::__construct($config); |
|
37
|
|
|
$this->moneyFormatter = $decimalMoneyFormatter; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function run() |
|
41
|
|
|
{ |
|
42
|
|
|
$this->registerClientScript(); |
|
43
|
|
|
$lang = Yii::$app->language; |
|
44
|
|
|
|
|
45
|
|
|
return $this->render('PriceInput', [ |
|
46
|
|
|
'basePrice' => $this->moneyFormatter->format($this->basePrice), |
|
47
|
|
|
'originalPrice' => $this->moneyFormatter->format($this->originalPrice), |
|
48
|
|
|
'activeField' => $this->activeField, |
|
49
|
|
|
'currency' => !$this->areCurrenciesSame() ? $this->originalPrice->getCurrency() : '', |
|
50
|
|
|
'lang' => $lang . '-' . strtoupper($lang), |
|
51
|
|
|
]); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
private function areCurrenciesSame(): bool |
|
55
|
|
|
{ |
|
56
|
|
|
return $this->basePrice->getCurrency()->equals($this->originalPrice->getCurrency()); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
private function registerClientScript() |
|
60
|
|
|
{ |
|
61
|
|
|
$this->view->registerJs(<<<'JS' |
|
62
|
|
|
$('.price-input').on('change mouseup', function () { |
|
63
|
|
|
var basePrice = parseFloat($(this).val()); |
|
64
|
|
|
if (isNaN(basePrice)) { |
|
65
|
|
|
return false; |
|
66
|
|
|
} |
|
67
|
|
|
var originalObj = $(this).closest('td').find('.base-price'), |
|
68
|
|
|
originalPrice = parseFloat(originalObj.attr('data-original-price')), |
|
69
|
|
|
lang = originalObj.attr('data-lang'), |
|
70
|
|
|
currency = originalObj.attr('data-currency'), |
|
71
|
|
|
delta = basePrice - originalPrice; |
|
72
|
|
|
if (currency !== '') { |
|
73
|
|
|
var originalPrice = new Intl.NumberFormat(lang, {style: 'currency', currency: currency}).format(originalPrice); |
|
74
|
|
|
originalObj.text(originalPrice).addClass('text-gray'); |
|
75
|
|
|
return; |
|
76
|
|
|
} |
|
77
|
|
|
originalObj.removeClass('text-success text-danger'); |
|
78
|
|
|
originalObj.text(delta.toFixed(2)).addClass(delta >= 0 ? 'text-success' : 'text-danger'); |
|
79
|
|
|
}); |
|
80
|
|
|
|
|
81
|
|
|
$('.price-input').trigger('change'); |
|
82
|
|
|
JS |
|
83
|
|
|
); |
|
84
|
|
|
|
|
85
|
|
|
$this->view->registerCss(' |
|
86
|
|
|
.base-price { font-weight: bold; } |
|
87
|
|
|
.form-group.form-group-sm { margin-bottom: 0; } |
|
88
|
|
|
'); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|