2022-05-09 17:50:14 +08:00
|
|
|
/* global dropdownSelector GLOBAL_CONSTANTS I18n SmartAnnotation formatDecimalValue Decimal */
|
2022-01-26 17:28:43 +08:00
|
|
|
|
|
|
|
var RepositoryStockValues = (function() {
|
|
|
|
const UNIT_SELECTOR = '#repository-stock-value-units';
|
|
|
|
|
2022-04-13 20:22:22 +08:00
|
|
|
function updateChangeAmount($element) {
|
|
|
|
if (!$element.val()) {
|
|
|
|
$('.stock-final-container .value').text('-');
|
|
|
|
return;
|
|
|
|
}
|
2022-05-09 17:50:14 +08:00
|
|
|
if (!($element.val() >= 0)) return;
|
|
|
|
|
|
|
|
let currentAmount = new Decimal($element.data('currentAmount') || 0);
|
|
|
|
let inputAmount = new Decimal($element.val());
|
|
|
|
let newAmount;
|
2022-04-13 20:22:22 +08:00
|
|
|
|
|
|
|
switch ($element.data('operator')) {
|
|
|
|
case 'set':
|
|
|
|
newAmount = inputAmount;
|
|
|
|
break;
|
|
|
|
case 'add':
|
2022-05-09 17:50:14 +08:00
|
|
|
newAmount = currentAmount.plus(inputAmount);
|
2022-04-13 20:22:22 +08:00
|
|
|
break;
|
|
|
|
case 'remove':
|
2022-05-09 17:50:14 +08:00
|
|
|
newAmount = currentAmount.minus(inputAmount);
|
2022-04-13 20:22:22 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
newAmount = currentAmount;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
$('#change_amount').val(inputAmount);
|
|
|
|
|
|
|
|
$('#repository_stock_value_amount').val(newAmount);
|
2022-04-15 18:08:29 +08:00
|
|
|
$('.stock-final-container').toggleClass('negative', newAmount < 0);
|
2022-04-13 20:22:22 +08:00
|
|
|
$('.stock-final-container .value').text(
|
|
|
|
formatDecimalValue(String(newAmount), $('#stock-input-amount').data('decimals'))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-01-26 17:28:43 +08:00
|
|
|
function initManageAction() {
|
2022-04-13 20:22:22 +08:00
|
|
|
let amountChanged = false;
|
|
|
|
|
2022-01-26 17:28:43 +08:00
|
|
|
$('.repository-show').on('click', '.manage-repository-stock-value-link', function() {
|
2022-04-22 17:13:00 +08:00
|
|
|
let colIndex = this.parentNode.cellIndex;
|
|
|
|
let rowIndex = this.parentNode.parentNode.rowIndex;
|
|
|
|
|
2022-01-26 17:28:43 +08:00
|
|
|
$.ajax({
|
|
|
|
url: $(this).closest('tr').data('manage-stock-url'),
|
|
|
|
type: 'GET',
|
|
|
|
dataType: 'json',
|
|
|
|
success: (result) => {
|
|
|
|
var $manageModal = $('#manage-repository-stock-value-modal');
|
|
|
|
$manageModal.find('.modal-content').html(result.html);
|
|
|
|
|
|
|
|
dropdownSelector.init(UNIT_SELECTOR, {
|
|
|
|
singleSelect: true,
|
|
|
|
closeOnSelect: true,
|
2022-03-30 20:03:07 +08:00
|
|
|
noEmptyOption: true,
|
2022-01-26 17:28:43 +08:00
|
|
|
selectAppearance: 'simple',
|
|
|
|
onChange: function() {
|
2022-03-30 20:03:07 +08:00
|
|
|
let unit = '';
|
|
|
|
if (dropdownSelector.getValues(UNIT_SELECTOR) > 0) {
|
|
|
|
unit = dropdownSelector.getLabels(UNIT_SELECTOR);
|
|
|
|
}
|
|
|
|
$('.stock-final-container .units').text(unit);
|
2022-03-09 21:19:15 +08:00
|
|
|
$('.repository-stock-reminder-value .units').text(
|
|
|
|
I18n.t('repository_stock_values.manage_modal.units_remaining', {
|
2022-03-30 20:03:07 +08:00
|
|
|
unit: unit
|
2022-03-09 21:19:15 +08:00
|
|
|
})
|
|
|
|
);
|
2022-01-26 17:28:43 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-03-22 21:35:27 +08:00
|
|
|
$manageModal.find(`
|
|
|
|
.dropdown-selector-container .input-field,
|
|
|
|
.dropdown-selector-container .search-field
|
|
|
|
`).attr('tabindex', 2);
|
|
|
|
|
2022-04-22 17:13:00 +08:00
|
|
|
$manageModal.find('form').on('ajax:success', function(_, data) {
|
2022-01-26 17:28:43 +08:00
|
|
|
$manageModal.modal('hide');
|
2022-05-09 17:20:38 +08:00
|
|
|
let $cell = $('.dataTable').find(
|
2022-04-22 17:13:00 +08:00
|
|
|
`tr:nth-child(${rowIndex}) td:nth-child(${colIndex + 1})`
|
2022-05-09 17:20:38 +08:00
|
|
|
);
|
|
|
|
$cell.parent().data('manage-stock-url', data.manageStockUrl);
|
|
|
|
$cell.html(
|
2022-04-22 17:13:00 +08:00
|
|
|
$.fn.dataTable.render.RepositoryStockValue(data)
|
|
|
|
);
|
2022-01-26 17:28:43 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
$('.stock-operator-option').click(function() {
|
|
|
|
var $stockInput = $('#stock-input-amount');
|
|
|
|
$('.stock-operator-option').removeClass('btn-primary').addClass('btn-secondary');
|
|
|
|
$(this).removeClass('btn-secondary').addClass('btn-primary');
|
|
|
|
$stockInput.data('operator', $(this).data('operator'));
|
|
|
|
|
|
|
|
dropdownSelector.selectValues(UNIT_SELECTOR, $('#initial_units').val());
|
2022-02-02 17:33:02 +08:00
|
|
|
$('#operator').val($(this).data('operator'));
|
2022-01-26 17:28:43 +08:00
|
|
|
switch ($(this).data('operator')) {
|
|
|
|
case 'set':
|
|
|
|
dropdownSelector.enableSelector(UNIT_SELECTOR);
|
2022-04-13 20:22:22 +08:00
|
|
|
if (!amountChanged) { $stockInput.val($stockInput.data('currentAmount')); }
|
2022-01-26 17:28:43 +08:00
|
|
|
break;
|
|
|
|
case 'add':
|
2022-04-13 20:22:22 +08:00
|
|
|
if (!amountChanged) { $stockInput.val(''); }
|
2022-01-26 17:28:43 +08:00
|
|
|
dropdownSelector.disableSelector(UNIT_SELECTOR);
|
|
|
|
break;
|
|
|
|
case 'remove':
|
2022-04-13 20:22:22 +08:00
|
|
|
if (!amountChanged) { $stockInput.val(''); }
|
2022-01-26 17:28:43 +08:00
|
|
|
dropdownSelector.disableSelector(UNIT_SELECTOR);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2022-04-13 20:22:22 +08:00
|
|
|
|
|
|
|
updateChangeAmount($('#stock-input-amount'));
|
2022-01-26 17:28:43 +08:00
|
|
|
});
|
|
|
|
|
2022-03-28 18:43:09 +08:00
|
|
|
$('#stock-input-amount, #low_stock_threshold').on('input focus', function() {
|
|
|
|
let decimals = $(this).data('decimals');
|
2022-03-30 20:03:07 +08:00
|
|
|
this.value = formatDecimalValue(this.value, decimals);
|
2022-03-28 18:43:09 +08:00
|
|
|
});
|
|
|
|
|
2023-04-25 21:12:19 +08:00
|
|
|
SmartAnnotation.init($('#repository-stock-value-comment')[0], false);
|
2022-03-30 20:03:07 +08:00
|
|
|
|
2022-04-28 21:30:20 +08:00
|
|
|
$('#repository-stock-value-comment').on('input', function() {
|
2022-02-10 17:55:58 +08:00
|
|
|
$(this).closest('.sci-input-container').toggleClass(
|
|
|
|
'error',
|
|
|
|
this.value.length > GLOBAL_CONSTANTS.NAME_MAX_LENGTH
|
|
|
|
);
|
|
|
|
$('.update-repository-stock').toggleClass(
|
|
|
|
'disabled',
|
|
|
|
this.value.length > GLOBAL_CONSTANTS.NAME_MAX_LENGTH
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2022-03-09 21:19:15 +08:00
|
|
|
$('#reminder-selector-checkbox').on('change', function() {
|
2022-03-30 20:03:07 +08:00
|
|
|
let valueContainer = $('.repository-stock-reminder-value');
|
|
|
|
valueContainer.toggleClass('hidden', !this.checked);
|
2022-03-09 21:19:15 +08:00
|
|
|
if (!this.checked) {
|
2022-03-30 20:03:07 +08:00
|
|
|
$(this).data('reminder-value', valueContainer.find('input').val());
|
|
|
|
valueContainer.find('input').val(null);
|
|
|
|
} else {
|
|
|
|
valueContainer.find('input').val($(this).data('reminder-value'));
|
|
|
|
valueContainer.find('input').focus();
|
2022-03-09 21:19:15 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
$('.update-repository-stock').on('click', function() {
|
2022-03-30 20:03:07 +08:00
|
|
|
let reminderError = $('#reminder-selector-checkbox')[0].checked
|
|
|
|
&& $('.repository-stock-reminder-value').find('input').val() === '';
|
2022-03-09 21:19:15 +08:00
|
|
|
$('.repository-stock-reminder-value').find('.sci-input-container').toggleClass('error', reminderError);
|
|
|
|
});
|
|
|
|
|
2022-01-26 17:28:43 +08:00
|
|
|
$('#stock-input-amount').on('input', function() {
|
2022-04-13 20:22:22 +08:00
|
|
|
amountChanged = true;
|
|
|
|
updateChangeAmount($(this));
|
2022-03-30 20:03:07 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
$manageModal.on('ajax:beforeSend', 'form', function() {
|
|
|
|
let status = true;
|
|
|
|
if (!(dropdownSelector.getValues(UNIT_SELECTOR) > 0)) {
|
|
|
|
dropdownSelector.showError(UNIT_SELECTOR, I18n.t('repository_stock_values.manage_modal.unit_error'));
|
|
|
|
status = false;
|
|
|
|
} else {
|
|
|
|
dropdownSelector.hideError(UNIT_SELECTOR);
|
|
|
|
}
|
2022-04-25 18:04:07 +08:00
|
|
|
let stockInput = $('#stock-input-amount');
|
|
|
|
if (stockInput.val().length && stockInput.val() >= 0) {
|
|
|
|
stockInput.parent().removeClass('error');
|
2022-03-30 20:03:07 +08:00
|
|
|
} else {
|
2022-04-25 18:04:07 +08:00
|
|
|
stockInput.parent().addClass('error');
|
|
|
|
if (stockInput.val().length === 0) {
|
|
|
|
stockInput.parent()
|
|
|
|
.attr(
|
|
|
|
'data-error-text',
|
|
|
|
I18n.t('repository_stock_values.manage_modal.amount_error')
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
stockInput.parent()
|
|
|
|
.attr(
|
|
|
|
'data-error-text',
|
|
|
|
I18n.t('repository_stock_values.manage_modal.negative_error')
|
|
|
|
);
|
|
|
|
}
|
2022-03-30 20:03:07 +08:00
|
|
|
status = false;
|
|
|
|
}
|
|
|
|
|
2022-04-25 18:04:07 +08:00
|
|
|
let reminderInput = $('.repository-stock-reminder-value input');
|
|
|
|
if ($('#reminder-selector-checkbox')[0].checked) {
|
|
|
|
if (reminderInput.val().length && reminderInput.val() >= 0) {
|
|
|
|
reminderInput.parent().removeClass('error');
|
|
|
|
} else {
|
|
|
|
reminderInput.parent().addClass('error');
|
|
|
|
if (reminderInput.val().length === 0) {
|
|
|
|
reminderInput.parent()
|
|
|
|
.attr(
|
|
|
|
'data-error-text',
|
|
|
|
I18n.t('repository_stock_values.manage_modal.amount_error')
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
reminderInput.parent()
|
|
|
|
.attr(
|
|
|
|
'data-error-text',
|
|
|
|
I18n.t('repository_stock_values.manage_modal.negative_error')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
status = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-30 20:03:07 +08:00
|
|
|
return status;
|
2022-01-26 17:28:43 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
$manageModal.modal('show');
|
2022-04-19 19:44:53 +08:00
|
|
|
amountChanged = false;
|
2022-03-22 21:35:27 +08:00
|
|
|
$('#stock-input-amount').focus();
|
2022-04-08 17:09:37 +08:00
|
|
|
$('#stock-input-amount')[0].selectionStart = $('#stock-input-amount')[0].value.length;
|
|
|
|
$('#stock-input-amount')[0].selectionEnd = $('#stock-input-amount')[0].value.length;
|
2022-01-26 17:28:43 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
init: () => {
|
|
|
|
initManageAction();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}());
|
|
|
|
|
|
|
|
RepositoryStockValues.init();
|