Skip to content
This repository has been archived by the owner on Sep 13, 2024. It is now read-only.

Commit

Permalink
Added the option to deactivate the Bank for currency updates (#17136)
Browse files Browse the repository at this point in the history
* Added the option to deactivate the Bank for currency updates

---------

Co-authored-by: rskrzypczak <[email protected]>
  • Loading branch information
areksolek and rskrzypczak authored Jun 23, 2023
1 parent 2b1a69b commit d5cf611
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 64 deletions.
1 change: 1 addition & 0 deletions languages/en-US/Settings/CurrencyUpdate.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"LBL_CURRENCIES_SUPPORTED": "List of supported currencies by this bank",
"LBL_CURRENCIES_UNSUPPORTED": "List of unsupported currencies by this bank",
"LBL_SET_BANK_OK": "Successfully saved active bank.",
"LBL_SET_BANK_DEACTIVATION": "Bank successfully deactivated.",
"LBL_SET_BANK_ERROR": "Failed to set active bank.",
"LBL_INFORMATIONS": "Information",
"LBL_WARNING": "Warning!",
Expand Down
1 change: 1 addition & 0 deletions layouts/basic/modules/Settings/CurrencyUpdate/Index.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
<div class="row">
<div class="col-md-5">
<select name="bank" id="bank" class="select2 form-control">
<option value="0" {if !$ACTIVE_BANK}selected{/if}>{\App\Language::translate('LBL_SELECT_OPTION', $QUALIFIED_MODULE)}</option>
{foreach from=$BANK item=key}
<option value="{$key.id}" {if $key.active eq '1'}selected{/if} data-name="{$key.bank_name|escape}">{\App\Language::translate($key.bank_name, $QUALIFIED_MODULE)}</option>
{/foreach}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ class Settings_CurrencyUpdate_GetBankCurrencies_Action extends Settings_Vtiger_B
public function process(App\Request $request)
{
$mode = $request->getMode();
$name = 'Settings_CurrencyUpdate_' . $request->getByType('name') . '_BankModel';
$name = !$request->isEmpty('name') ? ('Settings_CurrencyUpdate_' . $request->getByType('name') . '_BankModel') : '';
$moduleModel = Settings_CurrencyUpdate_Module_Model::getCleanInstance();
$response = new Vtiger_Response();

if ('supported' === $mode) {
$supported = $moduleModel->getSupportedCurrencies($name);
$response->setResult($supported);
Expand Down
3 changes: 1 addition & 2 deletions modules/Settings/CurrencyUpdate/actions/SaveActiveBank.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ public function process(App\Request $request)
$id = $request->getInteger('id');
$qualifiedModule = $request->getModule(false);
$moduleModel = Settings_CurrencyUpdate_Module_Model::getCleanInstance();

if (!$moduleModel->setActiveBankById($id)) {
$return = ['success' => false, 'message' => \App\Language::translate('LBL_SET_BANK_ERROR', $qualifiedModule)];
} else {
$return = ['success' => true, 'message' => \App\Language::translate('LBL_SET_BANK_OK', $qualifiedModule)];
$return = ['success' => true, 'message' => \App\Language::translate(!empty($id) ? 'LBL_SET_BANK_OK' : 'LBL_SET_BANK_DEACTIVATION', $qualifiedModule)];
}
$response = new Vtiger_Response();
$response->setResult($return);
Expand Down
21 changes: 10 additions & 11 deletions modules/Settings/CurrencyUpdate/models/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,6 @@ public function refreshBanks()
$db->createCommand()->insert('yetiforce_currencyupdate_banks', ['bank_name' => $bankClassName, 'active' => 0])->execute();
}
}
if (!$this->getActiveBankId()) {
$db->createCommand()->update('yetiforce_currencyupdate_banks', ['active' => 1], ['bank_name' => 'NBP'])->execute();
}
\App\Cache::delete('ActiveBankForExchangeRate', '');
}

Expand Down Expand Up @@ -189,10 +186,10 @@ public function getRatesHistory(int $bankId, string $dateStart, string $dateEnd)
*/
public function getSupportedCurrencies($bankName = null)
{
if (!\App\RequestUtil::isNetConnection()) {
if (!\App\RequestUtil::isNetConnection() || (empty($bankName) && empty($this->getActiveBankName()))) {
return [];
}
if (!$bankName) {
if (empty($bankName)) {
$bankName = 'Settings_CurrencyUpdate_' . $this->getActiveBankName() . '_BankModel';
}
$currencies = [];
Expand All @@ -214,14 +211,15 @@ public function getSupportedCurrencies($bankName = null)
*/
public function getUnSupportedCurrencies($bankName = null)
{
if (!\App\RequestUtil::isNetConnection()) {
if (!\App\RequestUtil::isNetConnection() || (empty($bankName) && empty($this->getActiveBankName()))) {
return [];
}
if (!$bankName) {
if (empty($bankName)) {
$bankName = 'Settings_CurrencyUpdate_' . $this->getActiveBankName() . '_BankModel';
}
$bank = new $bankName();
$supported = $bank->getSupportedCurrencies($bankName);
$unsupported = [];
$supported = $bank->getSupportedCurrencies();
$dataReader = (new \App\Db\Query())->select(['currency_name', 'currency_code'])
->from('vtiger_currency_info')
->where(['currency_status' => 'Active', 'deleted' => 0])
Expand All @@ -232,7 +230,6 @@ public function getUnSupportedCurrencies($bankName = null)
$unsupported[$name] = $code;
}
$dataReader->close();

return array_diff($unsupported, $supported);
}

Expand Down Expand Up @@ -315,8 +312,10 @@ public function getActiveBankId()
public function setActiveBankById($bankId)
{
$db = \App\Db::getInstance();
$db->createCommand()->update('yetiforce_currencyupdate_banks', ['active' => 0])->execute();
$result = $db->createCommand()->update('yetiforce_currencyupdate_banks', ['active' => 1], ['id' => $bankId])->execute();
$result = $db->createCommand()->update('yetiforce_currencyupdate_banks', ['active' => 0])->execute();
if (!empty($bankId)) {
$result = $db->createCommand()->update('yetiforce_currencyupdate_banks', ['active' => 1], ['id' => $bankId])->execute();
}
\App\Cache::delete('ActiveBankForExchangeRate', '');

return (bool) $result;
Expand Down
6 changes: 5 additions & 1 deletion modules/Settings/CurrencyUpdate/views/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function process(App\Request $request)

$history = $moduleModel->getRatesHistory($selectBankId, $request->isEmpty('duedate') ? date('Y-m-01') : $dateStart, $dateEnd);
$bankTab = [];

$active = false;
$db = new \App\Db\Query();
$db->from('yetiforce_currencyupdate_banks');
$dataReader = $db->createCommand()->query();
Expand All @@ -57,6 +57,9 @@ public function process(App\Request $request)
$bankName = $row['bank_name'];
$bankTab[$i]['bank_name'] = $bankName;
$bankTab[$i]['active'] = $row['active'];
if (1 === $row['active']) {
$active = true;
}
++$i;
}
$dataReader->close();
Expand All @@ -71,6 +74,7 @@ public function process(App\Request $request)
$viewer->assign('MODULENAME', 'CurrencyUpdate');
$viewer->assign('DATE', ($request->has('duedate') ? (new Vtiger_Date_UIType())->getDisplayValue($dateStart) : ''));
$viewer->assign('CURRNUM', $curr_num);
$viewer->assign('ACTIVE_BANK', $active);
$viewer->assign('BANK', $bankTab);
$viewer->assign('HISTORIA', $history);
$viewer->assign('MAINCURR', $mainCurrencyInfo);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ jQuery.Class(
* Updates information about supported and unsupported currencies for currently chosen bank,
*/
registerBankChange: function (container) {
const thisInstance = this;
container.find('#bank').on('change', function () {
let bankName = container.find('#bank option:selected').data('name');
container.find('#alertSpan').html('');
Expand All @@ -38,67 +39,25 @@ jQuery.Class(
}
}),
params = {};
params.data = {
parent: app.getParentModuleName(),
module: app.getModuleName(),
action: 'GetBankCurrencies',
mode: 'supported',
name: bankName
};
params.dataType = 'json';
AppConnector.request(params).done(function (data) {
let response = data['result'],
html = '',
name;
for (name in response) {
html += '<p><strong>' + name + '</strong> - ' + response[name] + '</p>';
}
container.find('#infoSpan').html(html);
});

params.data = {
parent: app.getParentModuleName(),
module: app.getModuleName(),
action: 'GetBankCurrencies',
mode: 'unsupported',
name: bankName
};
params.dataType = 'json';
AppConnector.request(params).done(function (data) {
let response = data['result'];
if (jQuery.isEmptyObject(response)) {
if (!container.find('#unsupportedCurrencies').hasClass('d-none')) {
container.find('#unsupportedCurrencies').addClass('d-none');
}
if (!container.find('#alertBlock').hasClass('d-none')) {
container.find('#alertBlock').addClass('d-none');
}
} else {
container.find('#unsupportedCurrencies').removeClass('d-none');
}
let html = '',
name;
for (name in response) {
html += '<p><strong>' + name + '</strong> - ' + response[name] + '</p>';
}
container.find('#alertSpan').html(html);
});

let bankId = jQuery('#bank option:selected').val();
params.data = {
parent: app.getParentModuleName(),
module: app.getModuleName(),
action: 'SaveActiveBank',
name: bankName,
id: bankId
};
params.dataType = 'json';
AppConnector.request(params).done(function (data) {
params.data.action = 'SaveActiveBank';
AppConnector.request(params).done((data) => {
let response = data['result'];
if (response['success']) {
app.showNotify({
text: response['message'],
type: 'success'
});
params.data.action = 'GetBankCurrencies';
thisInstance.supportedCurrencies(container, params);
thisInstance.unsupportedCurrencies(container, params);
} else {
app.showNotify({
text: response['message'],
Expand All @@ -110,6 +69,53 @@ jQuery.Class(
});
});
},

/**
* Supported currencies.
* @param {jQuery} container
* @param {object} params
*/
supportedCurrencies(container, params) {
params.data.mode = 'supported';
AppConnector.request(params).done((data) => {
let response = data['result'],
html = '',
name;
for (name in response) {
html += '<p><strong>' + name + '</strong> - ' + response[name] + '</p>';
}
container.find('#infoSpan').html(html);
});
},

/**
* Unsupported currencies.
* @param {jQuery} container
* @param {object} params
*/
unsupportedCurrencies(container, params) {
params.data.mode = 'unsupported';
AppConnector.request(params).done((data) => {
let response = data['result'];
if (jQuery.isEmptyObject(response)) {
if (!container.find('#unsupportedCurrencies').hasClass('d-none')) {
container.find('#unsupportedCurrencies').addClass('d-none');
}
if (!container.find('#alertBlock').hasClass('d-none')) {
container.find('#alertBlock').addClass('d-none');
}
} else {
container.find('#unsupportedCurrencies').removeClass('d-none');
}
let html = '',
name;
for (name in response) {
html += '<p><strong>' + name + '</strong> - ' + response[name] + '</p>';
}
container.find('#alertSpan').html(html);
});
},

/**
* Register events
*/
Expand Down

0 comments on commit d5cf611

Please sign in to comment.