Completed
Push — master ( ee3737...cee0de )
by Andrii
13:36
created

src/views/bill/create-exchange.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
use hipanel\modules\client\widgets\combo\ClientCombo;
4
use hipanel\modules\finance\forms\CurrencyExchangeForm;
5
use hipanel\modules\finance\models\ExchangeRate;
6
use hipanel\widgets\Box;
7
use hiqdev\combo\StaticCombo;
8
use yii\helpers\Html;
9
use yii\web\View;
10
use yii\widgets\ActiveForm;
11
12
/**
13
 * @var View $this
14
 * @var bool $canSupport
15
 * @var CurrencyExchangeForm $model
16
 * @var ExchangeRate[] $rates
17
 */
18
19
$this->title = Yii::t('hipanel:finance', 'Create currency exchange');
20
$this->params['breadcrumbs'][] = ['label' => Yii::t('hipanel:finance', 'Payments'), 'url' => ['index']];
21
$this->params['breadcrumbs'][] = $this->title;
22
23
?>
24
25
<?php $form = ActiveForm::begin([
26
    'id' => 'rates-form',
27
    'enableClientValidation' => true,
28
    'options' => [
29
        'data-rates' => array_map(static fn(ExchangeRate $model) => $model->getAttributes(), $rates),
0 ignored issues
show
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_PAAMAYIM_NEKUDOTAYIM
Loading history...
30
    ],
31
]) ?>
32
    <div class="bill-create-exchange">
33
        <div class="row">
34
            <div class="col-lg-6 col-md-8">
35
                <?php Box::begin() ?>
36
                <?= $canSupport ? $form->field($model, 'client_id')->widget(ClientCombo::class) : Html::activeHiddenInput($model, 'client_id') ?>
37
                <div class="row">
38
                    <div class="col-md-2">
39
                        <?= $form->field($model, 'sum')->textInput([
40
                            'data-attribute' => 'sum',
41
                            'value' => $model->sum ?: 1,
42
                        ])->label(false) ?>
43
                    </div>
44
                    <div class="col-md-3">
45
                        <?= $form->field($model, 'from')->widget(StaticCombo::class, [
46
                            'pluginOptions' => [
47
                                'select2Options' => [
48
                                    'allowClear' => false,
49
                                ],
50
                            ],
51
                            'inputOptions' => [
52
                                'data-attribute' => 'from',
53
                            ],
54
                        ])->label(false); ?>
55
                    </div>
56
                    <div class="col-md-1">
57
                        <i class="fa fa-long-arrow-right" style="padding: 10px"></i>
58
                    </div>
59
                    <div class="col-md-2">
60
                        <?= $form->field($model, 'result')->textInput([
61
                            'data-attribute' => 'result',
62
                        ])->label(false) ?>
63
                    </div>
64
                    <div class="col-md-3">
65
                        <?= $form->field($model, 'to')->widget(StaticCombo::class, [
66
                            'pluginOptions' => [
67
                                'select2Options' => [
68
                                    'allowClear' => false,
69
                                ],
70
                            ],
71
                            'inputOptions' => [
72
                                'data-attribute' => 'to',
73
                            ],
74
                        ])->label(false);
75
                        ?>
76
                    </div>
77
                </div>
78
                <?php Box::end() ?>
79
            </div>
80
        </div>
81
        <div class="row">
82
            <div class="col-md-12">
83
                <?= Html::submitButton(Yii::t('hipanel', 'Create'), ['class' => 'btn btn-success']) ?>
84
            </div>
85
        </div>
86
    </div>
87
<?php ActiveForm::end() ?>
88
89
<?php $this->registerJs(<<<'JS'
90
(function ($, window, document, undefined) {
91
var pluginName = "currencyExchanger";
92
93
function Plugin(element, options) {
94
    var _this = this;
95
    this.element = $(element);
96
97
    this.currency = this.element.find('[data-attribute=from]');
98
    this.targetCurrency = this.element.find('[data-attribute=to]');
99
    this.sum = this.element.find('[data-attribute=sum]');
100
    this.targetSum = this.element.find('[data-attribute=result]');
101
102
    this.rates = {};
103
104
    this._name = pluginName;
105
    this.init();
106
107
    return {
108
        startQuerier: function () {
109
            return _this.startQuerier();
110
        }
111
    };
112
}
113
114
Plugin.prototype = {
115
    init: function () {
116
        this.rates = this.element.data('rates');
117
118
        this.attachListeners();
119
        this.updateCurrency();
120
    },
121
    attachListeners: function () {
122
        this.currency.on('change', this.updateTargetCurrency.bind(this));
123
        this.targetCurrency.on('change', this.updateTargetSum.bind(this));
124
        this.sum.on('keyup change', this.updateTargetSum.bind(this));
125
        this.targetSum.on('keyup change', this.updateSum.bind(this));
126
    },
127
    updateCurrency: function () {
128
        var currencies = $.map(this.rates, function (rate) {
129
            return rate.from;
130
        });
131
132
        this.setCurrencies(this.currency, $.unique(currencies));
133
        this.updateTargetCurrency();
134
    },
135
    getCorrespondingCurrencies: function (currency) {
136
        return $.map(this.rates, function (rate) {
137
            if (rate.from === currency) {
138
                return rate.to;
139
            }
140
        });
141
    },
142
    getCurrencyPair: function (from, to) {
143
        var pair = false;
144
145
        $.each(this.rates, function () {
146
            if (this.from === from && this.to === to) {
147
                pair = this;
148
                return false;
149
            }
150
        });
151
152
        return pair;
153
    },
154
    getRate: function () {
155
        var pair = this.getCurrencyPair(this.currency.val(), this.targetCurrency.val());
156
157
        if (pair === false) {
158
            return 1;
159
        }
160
161
        return pair.rate;
162
    },
163
    updateTargetSum: function () {
164
        var rate = this.getRate(),
165
            value = Math.round(this.sum.val() * rate * 100) / 100;
166
167
        if (isNaN(value)) {
168
            return;
169
        }
170
171
        this.targetSum.val(value);
172
    },
173
    updateSum: function () {
174
        var rate = this.getRate(),
175
            value = Math.round(this.targetSum.val() * (1/rate) * 100) / 100;
176
177
        if (isNaN(value)) {
178
            return;
179
        }
180
181
        this.sum.val(value);
182
    },
183
    updateTargetCurrency: function () {
184
        var availableCurrencies = this.getCorrespondingCurrencies(this.currency.val());
185
186
        this.setCurrencies(this.targetCurrency, availableCurrencies);
187
        this.updateTargetSum();
188
    },
189
    setCurrencies: function (element, currencies) {
190
        var data = $.map(currencies, function (currency) {
191
            return {id: currency, value: currency};
192
        });
193
194
        element.data('field').clearOptions();
195
        element.data('field').ensureOptions(data);
196
    }
197
};
198
199
$.fn[pluginName] = function (options) {
200
    if (!$(this).data("plugin_" + pluginName)) {
201
        $(this).data("plugin_" + pluginName, new Plugin(this, options));
202
    }
203
204
    return $(this).data("plugin_" + pluginName);
205
};
206
})(jQuery, window, document);
207
208
$('#rates-form').currencyExchanger();
209
JS
210
);
211