2022-02-10 17:55:58 +08:00
|
|
|
/* global SmartAnnotation I18n MyModuleRepositories GLOBAL_CONSTANTS */
|
2022-01-28 18:10:56 +08:00
|
|
|
var MyModuleStockConsumption = (function() {
|
|
|
|
const CONSUMPTION_MODAL = '#consumeRepositoryStockValueModal';
|
|
|
|
const WARNING_MODAL = '#consumeRepositoryStockValueModalWarning';
|
|
|
|
|
2022-04-11 16:47:24 +08:00
|
|
|
function formatDecimalValue(value, decimals) {
|
|
|
|
let regexp = decimals === 0 ? /[^0-9-]/g : /[^0-9.-]/g;
|
|
|
|
return value.replace(regexp, '').match(new RegExp(`^-?\\d*(\\.\\d{0,${decimals}})?`))[0];
|
|
|
|
}
|
|
|
|
|
2022-01-28 18:10:56 +08:00
|
|
|
function initManageAction() {
|
|
|
|
$('.task-section').on('click', '.manage-repository-consumed-stock-value-link', function(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
$.ajax({
|
|
|
|
url: $(this).attr('href'),
|
|
|
|
type: 'GET',
|
|
|
|
dataType: 'json',
|
|
|
|
success: (result) => {
|
|
|
|
var $manageModal = $(CONSUMPTION_MODAL);
|
|
|
|
$manageModal.find('.modal-content').html(result.html);
|
|
|
|
$manageModal.modal('show');
|
2022-03-22 20:33:12 +08:00
|
|
|
$('#stock_consumption').focus();
|
2022-01-28 18:10:56 +08:00
|
|
|
SmartAnnotation.init($(CONSUMPTION_MODAL + ' #comment')[0]);
|
|
|
|
|
2022-04-11 16:47:24 +08:00
|
|
|
$('#stock_consumption').on('input', function() {
|
2022-01-28 18:10:56 +08:00
|
|
|
let initialValue = parseFloat($(this).data('initial-value'));
|
|
|
|
let initialStock = parseFloat($(this).data('initial-stock'));
|
2022-04-11 16:47:24 +08:00
|
|
|
let decimals = $(this).data('decimals');
|
|
|
|
this.value = formatDecimalValue(String(this.value), decimals);
|
2022-01-28 18:10:56 +08:00
|
|
|
let finalValue = initialValue - ($(this).val() || 0) + initialStock;
|
2022-04-11 16:47:24 +08:00
|
|
|
$('.stock-final-container .value')
|
|
|
|
.text(formatDecimalValue(String(finalValue), $('#stock_consumption').data('decimals')));
|
2022-01-28 18:10:56 +08:00
|
|
|
$('.stock-final-container').toggleClass('error', finalValue <= 0);
|
2022-04-11 16:47:24 +08:00
|
|
|
$(this).closest('.sci-input-container').toggleClass('error', this.value === '');
|
2022-01-28 18:10:56 +08:00
|
|
|
$('.update-consumption-button').attr('disabled', $(this).val() === '');
|
|
|
|
});
|
|
|
|
|
|
|
|
$(CONSUMPTION_MODAL + ' form').on('ajax:success', function() {
|
|
|
|
MyModuleRepositories.reloadSimpletable();
|
2022-01-29 00:53:56 +08:00
|
|
|
MyModuleRepositories.reloadFullViewTable();
|
2022-01-28 18:10:56 +08:00
|
|
|
$manageModal.modal('hide');
|
|
|
|
$(WARNING_MODAL).modal('hide');
|
|
|
|
});
|
|
|
|
|
2022-02-10 17:55:58 +08:00
|
|
|
$(CONSUMPTION_MODAL + ' #comment').on('keyup change', function() {
|
|
|
|
$(this).closest('.sci-input-container').toggleClass(
|
|
|
|
'error',
|
|
|
|
this.value.length > GLOBAL_CONSTANTS.NAME_MAX_LENGTH
|
|
|
|
);
|
|
|
|
$('.update-consumption-button').attr(
|
|
|
|
'disabled',
|
2022-04-11 16:47:24 +08:00
|
|
|
this.value.length > GLOBAL_CONSTANTS.NAME_MAX_LENGTH || $('#stock_consumption').val() === ''
|
2022-02-10 17:55:58 +08:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2022-01-28 18:10:56 +08:00
|
|
|
$('.update-consumption-button').on('click', function(event, skipValidation) {
|
|
|
|
if (parseFloat($('.stock-final-container .value').text()) < 0 && !skipValidation) {
|
|
|
|
event.preventDefault();
|
|
|
|
$manageModal.modal('hide');
|
|
|
|
$(WARNING_MODAL).modal('show');
|
|
|
|
let units = $(CONSUMPTION_MODAL).find('.consumption-container .units').text();
|
|
|
|
let value = $('#stock_consumption').val();
|
2022-04-04 17:07:46 +08:00
|
|
|
$(WARNING_MODAL).find('.modal-body p').html(
|
|
|
|
I18n.t('my_modules.repository.stock_warning_modal.description_html', { value: `${value} ${units}` })
|
2022-01-28 18:10:56 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function initWarningModal() {
|
2022-03-22 20:33:12 +08:00
|
|
|
$(WARNING_MODAL).on('keydown', function(e) {
|
|
|
|
if (e.key === 'Escape') {
|
|
|
|
$(WARNING_MODAL).modal('hide');
|
|
|
|
$(CONSUMPTION_MODAL).modal('show');
|
|
|
|
$('#stock_consumption').focus();
|
|
|
|
} else if (e.key === 'Enter') {
|
|
|
|
$('.update-consumption-button').trigger('click', [true]);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
$(WARNING_MODAL).on('click', '.cancel-consumption', function(e) {
|
2022-01-28 18:10:56 +08:00
|
|
|
$(WARNING_MODAL).modal('hide');
|
|
|
|
$(CONSUMPTION_MODAL).modal('show');
|
2022-03-22 20:33:12 +08:00
|
|
|
$('#stock_consumption').focus();
|
2022-01-28 18:10:56 +08:00
|
|
|
}).on('click', '.confirm-consumption-button', function() {
|
|
|
|
$('.update-consumption-button').trigger('click', [true]);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
init: () => {
|
|
|
|
initManageAction();
|
|
|
|
initWarningModal();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}());
|
|
|
|
|
|
|
|
MyModuleStockConsumption.init();
|