diff --git a/ajax/convertWithSign.php b/ajax/convertWithSign.php index 862e49a4cfae37ed2efdf4c3e1c04fc6be5d4d8a..16c2a3f8a11c4745f14566d6e90a2ae0e04a7525 100644 --- a/ajax/convertWithSign.php +++ b/ajax/convertWithSign.php @@ -20,12 +20,29 @@ function ($data) { $amount = $entry['amount']; $currencyFrom = $entry['from']; $currencyTo = $entry['to']; + $converted = QUI\ERP\Currency\Calc::convert($amount, $currencyFrom, $currencyTo); + $convertedRound = $converted; + + $numberAsString = strval($converted); + $exploded = explode('.', $numberAsString); + $numberOfDecimalPlaces = isset($exploded[1]) ? strlen($exploded[1]) : 0; + + if ($numberOfDecimalPlaces > 4) { + $CurrencyTo = QUI\ERP\Currency\Handler::getCurrency($currencyTo); + + $priceRounded = round($converted, 4); + $PriceDisplay = new QUI\ERP\Money\Price($priceRounded, $CurrencyTo); + $convertedRound = '~' . $PriceDisplay->getDisplayPrice(); + } else { + $convertedRound = QUI\ERP\Currency\Calc::convertWithSign($convertedRound, $currencyFrom, $currencyTo); + } $result[] = [ 'amount' => $entry['amount'], 'from' => $entry['from'], 'to' => $entry['to'], 'converted' => QUI\ERP\Currency\Calc::convertWithSign($amount, $currencyFrom, $currencyTo), + 'convertedRound' => $convertedRound, 'id' => $entry['id'] ]; } diff --git a/bin/Currency.js b/bin/Currency.js index 0898633c08aa228ab27a2a0e7605396c4012c8e8..e8a5c8f62b3afc63ee71ae96fb84d51b7676a448 100644 --- a/bin/Currency.js +++ b/bin/Currency.js @@ -11,8 +11,8 @@ define('package/quiqqer/currency/bin/Currency', [ 'Ajax', 'package/quiqqer/currency/bin/classes/BulkConverting' -], function (QUI, QUIDOM, QUIAjax, BulkConverting) { - "use strict"; +], function(QUI, QUIDOM, QUIAjax, BulkConverting) { + 'use strict'; let Converter = null; let def = 'EUR'; @@ -36,7 +36,7 @@ define('package/quiqqer/currency/bin/Currency', [ Extends: QUIDOM, Type: 'package/quiqqer/currency/bin/Currency', - initialize: function (options) { + initialize: function(options) { this.parent(options); this.$currency = def; @@ -56,9 +56,9 @@ define('package/quiqqer/currency/bin/Currency', [ * * @param {String} currencyCode */ - setCurrency: function (currencyCode) { - this.getCurrencies().then(function (currencies) { - const found = currencies.find(function (Currency) { + setCurrency: function(currencyCode) { + this.getCurrencies().then(function(currencies) { + const found = currencies.find(function(Currency) { return Currency.code === currencyCode; }); @@ -79,7 +79,7 @@ define('package/quiqqer/currency/bin/Currency', [ * @param {Boolean} [refresh] - Refresh data from database * @return {Promise} */ - getCurrency: function (currencyCode, refresh) { + getCurrency: function(currencyCode, refresh) { currencyCode = currencyCode || this.$currency; refresh = refresh || false; @@ -113,7 +113,7 @@ define('package/quiqqer/currency/bin/Currency', [ * * @returns {Promise} */ - getCurrencies: function () { + getCurrencies: function() { if (Object.getLength(this.$currencies)) { return Promise.resolve(this.$currencies); } @@ -147,7 +147,7 @@ define('package/quiqqer/currency/bin/Currency', [ * * @returns {Promise<Array>} */ - getCurrencyTypes: function () { + getCurrencyTypes: function() { if (this.$currencyTypes.length) { return Promise.resolve(this.$currencyTypes); } @@ -171,7 +171,7 @@ define('package/quiqqer/currency/bin/Currency', [ * @param {String} currencyTo * @returns {Promise} */ - convert: function (amount, currencyFrom, currencyTo) { + convert: function(amount, currencyFrom, currencyTo) { currencyTo = currencyTo || this.$currency; currencyFrom = currencyFrom || this.$currency; @@ -193,7 +193,7 @@ define('package/quiqqer/currency/bin/Currency', [ * @param {String} currencyTo * @returns {Promise} */ - convertWithSign: function (amount, currencyFrom, currencyTo) { + convertWithSign: function(amount, currencyFrom, currencyTo) { currencyTo = currencyTo || this.$currency; currencyFrom = currencyFrom || this.$currency; @@ -240,7 +240,7 @@ define('package/quiqqer/currency/bin/Currency', [ return; } - resolve(result[i].converted); + resolve(result[i]); }); }); } diff --git a/bin/settings/AllowedCurrencies.js b/bin/settings/AllowedCurrencies.js index 5f10a6ff16a4b7faf2827d18ea7ea58167239572..5e21c6eb1dba62dc5b5dae2cb85ff62e19bf9a42 100644 --- a/bin/settings/AllowedCurrencies.js +++ b/bin/settings/AllowedCurrencies.js @@ -19,8 +19,8 @@ define('package/quiqqer/currency/bin/settings/AllowedCurrencies', [ 'css!package/quiqqer/currency/bin/settings/AllowedCurrencies.css' -], function (QUI, QUIControl, QUISwitch, QUIConfirm, QUIPrompt, QUIAjax, QUILocale, Grid, CurrencyWindow) { - "use strict"; +], function(QUI, QUIControl, QUISwitch, QUIConfirm, QUIPrompt, QUIAjax, QUILocale, Grid, CurrencyWindow) { + 'use strict'; const lg = 'quiqqer/currency'; @@ -43,7 +43,7 @@ define('package/quiqqer/currency/bin/settings/AllowedCurrencies', [ values: {} }, - initialize: function (options) { + initialize: function(options) { this.parent(options); this.$Input = null; @@ -59,7 +59,7 @@ define('package/quiqqer/currency/bin/settings/AllowedCurrencies', [ /** * event : on import */ - $onImport: function () { + $onImport: function() { let i, len; this.$Input = this.getElm(); @@ -164,7 +164,7 @@ define('package/quiqqer/currency/bin/settings/AllowedCurrencies', [ textimage: 'fa fa-edit', disabled: true, events: { - onClick: function () { + onClick: function() { self.openUpdateDialog(self.$Grid.getSelectedData()[0].code); } } @@ -178,8 +178,8 @@ define('package/quiqqer/currency/bin/settings/AllowedCurrencies', [ textimage: 'fa fa-trash', disabled: true, events: { - onClick: function () { - const currencies = self.$Grid.getSelectedData().map(function (C) { + onClick: function() { + const currencies = self.$Grid.getSelectedData().map(function(C) { return C.code; }); @@ -193,28 +193,30 @@ define('package/quiqqer/currency/bin/settings/AllowedCurrencies', [ this.$Grid.setWidth(width); this.$Grid.addEvents({ - onClick: function () { + onClick: function() { const selected = self.$Grid.getSelectedIndices(), buttons = self.$Grid.getButtons(); - const Edit = buttons.filter(function (Btn) { + const Edit = buttons.filter(function(Btn) { return Btn.getAttribute('name') === 'edit'; })[0]; - const Delete = buttons.filter(function (Btn) { + const Delete = buttons.filter(function(Btn) { return Btn.getAttribute('name') === 'delete'; })[0]; if (selected.length === 1) { Edit.enable(); Delete.enable(); - } else if (selected.length > 1) { - Edit.disable(); - Delete.enable(); + } else { + if (selected.length > 1) { + Edit.disable(); + Delete.enable(); + } } }, - onDblClick: function () { + onDblClick: function() { self.openUpdateDialog(self.$Grid.getSelectedData()[0].code); }, @@ -227,8 +229,8 @@ define('package/quiqqer/currency/bin/settings/AllowedCurrencies', [ /** * refresh the currency list */ - refresh: function () { - return this.getCurrencies().then(function (list) { + refresh: function() { + return this.getCurrencies().then(function(list) { let data = [], values = this.getAttribute('values'); @@ -247,7 +249,7 @@ define('package/quiqqer/currency/bin/settings/AllowedCurrencies', [ data = data.splice(start, perPage); - data.each(function (entry, i) { + data.each(function(entry, i) { data[i].allowed = new QUISwitch({ status: (typeof values[entry.code] !== 'undefined'), currency: entry.code, @@ -273,11 +275,11 @@ define('package/quiqqer/currency/bin/settings/AllowedCurrencies', [ const buttons = this.$Grid.getButtons(); - const Edit = buttons.filter(function (Btn) { + const Edit = buttons.filter(function(Btn) { return Btn.getAttribute('name') === 'edit'; })[0]; - const Delete = buttons.filter(function (Btn) { + const Delete = buttons.filter(function(Btn) { return Btn.getAttribute('name') === 'delete'; })[0]; @@ -290,7 +292,7 @@ define('package/quiqqer/currency/bin/settings/AllowedCurrencies', [ /** * update values to the input field */ - update: function () { + update: function() { const allowed = [], values = this.getAttribute('values'); @@ -313,8 +315,8 @@ define('package/quiqqer/currency/bin/settings/AllowedCurrencies', [ * Return all available currencies * @returns {Promise} */ - getCurrencies: function () { - return new Promise(function (resolve, reject) { + getCurrencies: function() { + return new Promise(function(resolve, reject) { QUIAjax.get('package_quiqqer_currency_ajax_getCurrencies', resolve, { 'package': 'quiqqer/currency', onError: reject @@ -328,12 +330,12 @@ define('package/quiqqer/currency/bin/settings/AllowedCurrencies', [ * @param {Array|String} currencies * @returns {Promise} */ - deleteCurrency: function (currencies) { + deleteCurrency: function(currencies) { if (typeOf(currencies) === 'string') { currencies = [currencies]; } - return new Promise(function (resolve, reject) { + return new Promise(function(resolve, reject) { QUIAjax.post('package_quiqqer_currency_ajax_delete', resolve, { 'package': 'quiqqer/currency', currencies: JSON.encode(currencies), @@ -348,8 +350,8 @@ define('package/quiqqer/currency/bin/settings/AllowedCurrencies', [ * @param {String} currency * @returns {Promise} */ - createCurrency: function (currency) { - return new Promise(function (resolve, reject) { + createCurrency: function(currency) { + return new Promise(function(resolve, reject) { QUIAjax.post('package_quiqqer_currency_ajax_create', resolve, { 'package': 'quiqqer/currency', currency: currency, @@ -363,7 +365,7 @@ define('package/quiqqer/currency/bin/settings/AllowedCurrencies', [ * * @param {Object} Switch */ - $onCurrencyStatusChange: function (Switch) { + $onCurrencyStatusChange: function(Switch) { const currency = Switch.getAttribute('currency'); const values = this.getAttribute('values'); @@ -398,7 +400,7 @@ define('package/quiqqer/currency/bin/settings/AllowedCurrencies', [ * * @param {Event} event */ - $switchCurrencyStatus: function (event) { + $switchCurrencyStatus: function(event) { const Target = event.target; const SwitchContainer = Target.getParent().getElement('.qui-switch'); @@ -415,8 +417,8 @@ define('package/quiqqer/currency/bin/settings/AllowedCurrencies', [ * @param {Object} Switch * @return {Promise} */ - $changeAutoUpdate: function (Switch) { - return new Promise(function (resolve, reject) { + $changeAutoUpdate: function(Switch) { + return new Promise(function(resolve, reject) { QUIAjax.post('package_quiqqer_currency_ajax_setAutoupdate', resolve, { 'package': 'quiqqer/currency', currency: Switch.getAttribute('currency'), @@ -431,8 +433,8 @@ define('package/quiqqer/currency/bin/settings/AllowedCurrencies', [ * * @returns {Promise} */ - $importFromECB: function () { - return new Promise(function (resolve, reject) { + $importFromECB: function() { + return new Promise(function(resolve, reject) { QUIAjax.post('package_quiqqer_currency_ajax_importFromECB', resolve, { 'package': 'quiqqer/currency', onError: reject @@ -449,7 +451,7 @@ define('package/quiqqer/currency/bin/settings/AllowedCurrencies', [ * * @param {String} currency */ - openUpdateDialog: function (currency) { + openUpdateDialog: function(currency) { new CurrencyWindow({ currency: currency, events: { @@ -463,7 +465,7 @@ define('package/quiqqer/currency/bin/settings/AllowedCurrencies', [ * * @param {Array} currencies */ - openDeleteDialog: function (currencies) { + openDeleteDialog: function(currencies) { if (!currencies.length) { return; } @@ -501,17 +503,17 @@ define('package/quiqqer/currency/bin/settings/AllowedCurrencies', [ maxWidth: 600, autoclose: false, events: { - onSubmit: function (Win) { + onSubmit: function(Win) { Win.Loader.show(); - self.deleteCurrency(currencies).then(function () { + self.deleteCurrency(currencies).then(function() { - self.refresh().then(function () { + self.refresh().then(function() { self.update(); Win.Loader.hide(); Win.close(); }); - }, function () { + }, function() { Win.Loader.hide(); }); } @@ -522,7 +524,7 @@ define('package/quiqqer/currency/bin/settings/AllowedCurrencies', [ /** * Opens the create dialog */ - openCreateDialog: function () { + openCreateDialog: function() { const self = this; new QUIPrompt({ @@ -533,13 +535,13 @@ define('package/quiqqer/currency/bin/settings/AllowedCurrencies', [ maxHeight: 300, maxWidth: 450, events: { - onSubmit: function (value, Win) { + onSubmit: function(value, Win) { Win.Loader.show(); - self.createCurrency(value).then(function () { + self.createCurrency(value).then(function() { Win.close(); self.openUpdateDialog(value); - }, function () { + }, function() { Win.Loader.hide(); }); } diff --git a/bin/settings/CurrencyList.js b/bin/settings/CurrencyList.js index f15759e5240a62c33a2d01e056fa40873f0e608c..067109f8105bccbe7813036b8cbb63aee3a79624 100644 --- a/bin/settings/CurrencyList.js +++ b/bin/settings/CurrencyList.js @@ -9,12 +9,12 @@ define('package/quiqqer/currency/bin/settings/CurrencyList', [ 'qui/controls/buttons/Select', 'Ajax' -], function (QUI, QUIControl, QUISelect, QUIAjax) { - "use strict"; +], function(QUI, QUIControl, QUISelect, QUIAjax) { + 'use strict'; return new Class({ - Type : 'package/quiqqer/currency/bin/settings/CurrencyList', + Type: 'package/quiqqer/currency/bin/settings/CurrencyList', Extends: QUIControl, Binds: [ @@ -22,7 +22,7 @@ define('package/quiqqer/currency/bin/settings/CurrencyList', [ '$onSelectChange' ], - initialize: function (options) { + initialize: function(options) { this.parent(options); this.$Input = null; @@ -36,7 +36,7 @@ define('package/quiqqer/currency/bin/settings/CurrencyList', [ /** * event : on import */ - $onImport: function () { + $onImport: function() { this.$Input = this.getElm(); this.$Input.type = 'hidden'; @@ -46,15 +46,15 @@ define('package/quiqqer/currency/bin/settings/CurrencyList', [ this.$Select = new QUISelect({ showIcons: false, - events : { + events: { onChange: this.$onSelectChange }, - styles : { + styles: { width: '100%' } }).inject(this.$Elm); - this.getCurrencies().then(function (result) { + this.getCurrencies().then(function(result) { for (let i in result) { if (!result.hasOwnProperty(i)) { continue; @@ -84,11 +84,11 @@ define('package/quiqqer/currency/bin/settings/CurrencyList', [ * Return all available currencies * @returns {Promise} */ - getCurrencies: function () { - return new Promise(function (resolve, reject) { + getCurrencies: function() { + return new Promise(function(resolve, reject) { QUIAjax.get('package_quiqqer_currency_ajax_getAllowedCurrencies', resolve, { 'package': 'quiqqer/currency', - onError : reject + onError: reject }); }); }, @@ -99,7 +99,7 @@ define('package/quiqqer/currency/bin/settings/CurrencyList', [ * @param {String} value * @param {Object} Select - qui/controls/buttons/Select */ - $onSelectChange: function (value, Select) { + $onSelectChange: function(value, Select) { this.$Input.value = value; this.fireEvent('change', [ this, diff --git a/events.xml b/events.xml index 441b99e00138d20dc482e5fc0438df88ec96c5d1..e16283ae37c8aef6d3fc1251f9cf5ea1c0d8d4f6 100644 --- a/events.xml +++ b/events.xml @@ -1,5 +1,6 @@ <?xml version="1.0" encoding="UTF-8"?> <events> <event on="onTemplateGetHeader" fire="\QUI\ERP\Currency\EventHandler::onTemplateGetHeader"/> + <event on="onPackageConfigSave" fire="\QUI\ERP\Currency\EventHandler::onPackageConfigSave"/> <!--<event on="onPackageInstall" fire="\QUI\ERP\Currency\EventHandler::onPackageInstall"/>--> </events> diff --git a/src/QUI/ERP/Currency/EventHandler.php b/src/QUI/ERP/Currency/EventHandler.php index ac45c46a4c5b54bc0cada3c131312d854ae3ac93..0c02f0a866bd43b62560118b99d73da91260f8fb 100644 --- a/src/QUI/ERP/Currency/EventHandler.php +++ b/src/QUI/ERP/Currency/EventHandler.php @@ -7,6 +7,7 @@ namespace QUI\ERP\Currency; use QUI; +use QUI\Package\Package; use function json_encode; @@ -41,4 +42,20 @@ public static function onTemplateGetHeader(QUI\Template $TemplateManager) ); } } + + /** + * Clears the currency list cache if the package config of 'quiqqer/currency' is saved + * + * @param Package $Package The package object. + * @param array $params Additional parameters passed to the method. + * @return void + */ + public static function onPackageConfigSave(Package $Package, array $params) + { + if ($Package->getName() !== 'quiqqer/currency') { + return; + } + + QUI\Cache\Manager::clear('quiqqer/currency/list'); + } }