Convert currencies

This commit is contained in:
Hillel Coren 2017-12-21 09:47:17 +02:00
parent d8be149db1
commit 4683f66fc6
2 changed files with 46 additions and 20 deletions

View File

@ -159,7 +159,7 @@
{!! Former::select('invoice_currency_id')->addOption('','') {!! Former::select('invoice_currency_id')->addOption('','')
->label(trans('texts.invoice_currency')) ->label(trans('texts.invoice_currency'))
->data_placeholder(Utils::getFromCache($account->getCurrencyId(), 'currencies')->name) ->data_placeholder(Utils::getFromCache($account->getCurrencyId(), 'currencies')->name)
->data_bind('combobox: invoiceCurrencyId, disable: true') ->data_bind('combobox: invoice_currency_id, disable: true')
->fromQuery($currencies, 'name', 'id') !!} ->fromQuery($currencies, 'name', 'id') !!}
</span> </span>
<span style="display:none;" data-bind="visible: client_id"> <span style="display:none;" data-bind="visible: client_id">
@ -354,6 +354,12 @@
onClientChange(); onClientChange();
}); });
$('#invoice_currency_id, #expense_currency_id').on('change', function() {
setTimeout(function() {
model.updateExchangeRate();
}, 1);
})
@if ($data) @if ($data)
// this means we failed so we'll reload the previous state // this means we failed so we'll reload the previous state
window.model = new ViewModel({!! $data !!}); window.model = new ViewModel({!! $data !!});
@ -478,26 +484,19 @@
} }
}, self); }, self);
self.updateExchangeRate = function() {
self.invoiceCurrencyId = ko.computed({ var fromCode = self.expenseCurrencyCode();
read: function () { var toCode = self.invoiceCurrencyCode();
return self.invoice_currency_id(); if (currencyMap[fromCode].exchange_rate && currencyMap[toCode].exchange_rate) {
}, var rate = fx.convert(1, {
write: function(invoiceCurrencyId) { from: fromCode,
self.invoice_currency_id(invoiceCurrencyId); to: toCode,
var fromCode = self.expenseCurrencyCode(); });
var toCode = self.invoiceCurrencyCode(); self.exchange_rate(roundToFour(rate, true));
if (currencyMap[fromCode].exchange_rate && currencyMap[toCode].exchange_rate) { } else {
var rate = fx.convert(1, { self.exchange_rate(1);
from: fromCode,
to: toCode,
});
self.exchange_rate(roundToFour(rate));
} else {
self.exchange_rate(1);
}
} }
}, self); }
self.getCurrency = function(currencyId) { self.getCurrency = function(currencyId) {
return currencyMap[currencyId || self.account_currency_id()]; return currencyMap[currencyId || self.account_currency_id()];

View File

@ -201,6 +201,12 @@
toggleDatePicker('payment_date'); toggleDatePicker('payment_date');
}); });
$('#exchange_currency_id').on('change', function() {
setTimeout(function() {
model.updateExchangeRate();
}, 1);
})
if (isStorageSupported()) { if (isStorageSupported()) {
if (localStorage.getItem('last:send_email_receipt')) { if (localStorage.getItem('last:send_email_receipt')) {
$('#email_receipt').prop('checked', true); $('#email_receipt').prop('checked', true);
@ -262,6 +268,21 @@
} }
}, self); }, self);
self.updateExchangeRate = function() {
var fromCode = self.paymentCurrencyCode();
var toCode = self.exchangeCurrencyCode();
if (currencyMap[fromCode].exchange_rate && currencyMap[toCode].exchange_rate) {
var rate = fx.convert(1, {
from: fromCode,
to: toCode,
});
self.exchange_rate(roundToFour(rate, true));
} else {
self.exchange_rate(1);
}
}
self.getCurrency = function(currencyId) { self.getCurrency = function(currencyId) {
return currencyMap[currencyId || self.account_currency_id()]; return currencyMap[currencyId || self.account_currency_id()];
}; };
@ -336,6 +357,9 @@
if (window.model) { if (window.model) {
model.client_id(clientId); model.client_id(clientId);
setTimeout(function() {
model.updateExchangeRate();
}, 1);
} }
}); });
@ -358,6 +382,9 @@
} }
} }
model.client_id(client ? client.public_id : 0); model.client_id(client ? client.public_id : 0);
setTimeout(function() {
model.updateExchangeRate();
}, 1);
}); });
$invoiceSelect.combobox({highlighter: comboboxHighlighter}); $invoiceSelect.combobox({highlighter: comboboxHighlighter});