This commit is contained in:
2026-03-22 16:12:12 -03:00
parent 4bb50f0716
commit 822aa68197
15 changed files with 2878 additions and 1318 deletions
@@ -353,30 +353,67 @@ function clearZoomField(fieldId) {
$("#" + fieldId).val("");
}
var MOTORISTAS_GROUP_ID = "Motoristas";
var motoristasEntregaCache = null;
var motoristasEntregaLoading = false;
var MOTORISTAS_GROUP_DEFAULT_ID = "Motoristas";
var MOTORISTAS_GROUP_BY_UF = {
AL: "motoristaAL",
BA: "motoristaBA",
SE: "motoristaSE"
};
var motoristasEntregaCacheByGroup = {};
var motoristasEntregaLoadingByGroup = {};
var motoristaEntregaGroupAtual = "";
function normalizeUfCode(value) {
return String(value || "").toUpperCase().replace(/[^A-Z]/g, "").substring(0, 2);
}
function resolveMotoristasGroupIdByUf(uf) {
var ufCode = normalizeUfCode(uf);
if (!ufCode) {
return MOTORISTAS_GROUP_DEFAULT_ID;
}
if (MOTORISTAS_GROUP_BY_UF[ufCode]) {
return String(MOTORISTAS_GROUP_BY_UF[ufCode] || "").trim() || MOTORISTAS_GROUP_DEFAULT_ID;
}
// Convencao para facilitar novos estados sem precisar alterar o JS.
return "motorista" + ufCode;
}
function getMotoristasEntregaQueryContext() {
var uf = normalizeUfCode($("#ufDestino").val());
var groupId = resolveMotoristasGroupIdByUf(uf);
return {
uf: uf,
groupId: groupId,
cacheKey: groupId
};
}
function loadMotoristasEntregaSelect(forceReload) {
var select = $("#motoristaEntregaSelecionado");
if (!select.length) return;
if (!forceReload && motoristasEntregaCache && motoristasEntregaCache.length) {
renderMotoristasEntregaOptions(motoristasEntregaCache);
var context = getMotoristasEntregaQueryContext();
var cacheKey = context.cacheKey;
motoristaEntregaGroupAtual = context.groupId;
if (!forceReload && Object.prototype.hasOwnProperty.call(motoristasEntregaCacheByGroup, cacheKey)) {
renderMotoristasEntregaOptions(motoristasEntregaCacheByGroup[cacheKey], context);
return;
}
if (motoristasEntregaLoading) return;
if (motoristasEntregaLoadingByGroup[cacheKey]) return;
motoristasEntregaLoading = true;
motoristasEntregaLoadingByGroup[cacheKey] = true;
select.prop("disabled", true);
var requestPayload = {
name: "ds_motoristas_grupo",
fields: null,
constraints: [{
_field: "GROUP_ID",
_initialValue: MOTORISTAS_GROUP_ID,
_finalValue: MOTORISTAS_GROUP_ID,
_initialValue: context.groupId,
_finalValue: context.groupId,
_type: 1
}],
order: null
@@ -390,14 +427,22 @@ function loadMotoristasEntregaSelect(forceReload) {
data: JSON.stringify(requestPayload)
}).done(function (response) {
var values = ((((response || {}).content || {}).values) || []);
motoristasEntregaCache = normalizeMotoristasEntregaRows(values);
renderMotoristasEntregaOptions(motoristasEntregaCache);
var rows = normalizeMotoristasEntregaRows(values);
motoristasEntregaCacheByGroup[cacheKey] = rows;
if (motoristaEntregaGroupAtual === context.groupId) {
renderMotoristasEntregaOptions(rows, context);
}
}).fail(function (xhr) {
console.error("Falha ao carregar motoristas do dataset:", xhr);
motoristasEntregaCache = [];
renderMotoristasEntregaOptions([]);
console.error("Falha ao carregar motoristas do dataset (" + context.groupId + "):", xhr);
motoristasEntregaCacheByGroup[cacheKey] = [];
if (motoristaEntregaGroupAtual === context.groupId) {
renderMotoristasEntregaOptions([], context);
}
}).always(function () {
motoristasEntregaLoading = false;
motoristasEntregaLoadingByGroup[cacheKey] = false;
if (motoristaEntregaGroupAtual === context.groupId) {
select.prop("disabled", false);
}
});
}
@@ -429,14 +474,21 @@ function normalizeMotoristasEntregaRows(values) {
return out;
}
function renderMotoristasEntregaOptions(rows) {
function renderMotoristasEntregaOptions(rows, context) {
var select = $("#motoristaEntregaSelecionado");
if (!select.length) return;
var selectedValue = String($("#motoristaEntregaLogin").val() || select.val() || "").trim();
var placeholder = "Selecione o motorista";
if (context && context.uf) {
placeholder = "Selecione o motorista da UF " + context.uf;
}
if ((rows || []).length === 0 && context && context.uf) {
placeholder = "Sem motoristas cadastrados para UF " + context.uf;
}
select.empty();
select.append($("<option></option>").val("").text("Selecione o motorista"));
select.append($("<option></option>").val("").text(placeholder));
for (var i = 0; i < rows.length; i++) {
var row = rows[i];
@@ -462,6 +514,12 @@ function renderMotoristasEntregaOptions(rows) {
applySelectedMotoristaEntregaOption();
}
function refreshMotoristasEntregaByUf(forceReload) {
var escolha = String($("input[name='tipoMotoristaEntrega']:checked").val() || "");
if (escolha !== "outro") return;
loadMotoristasEntregaSelect(forceReload === true);
}
function syncMotoristaEntregaSelectFromHidden() {
var select = $("#motoristaEntregaSelecionado");
if (!select.length) return;
@@ -1043,7 +1101,7 @@ var beforeSendValidate = function (numState, nextState) {
throw "'Complemento' é obrigatório.";
} else if ($("#justificativa").val() == "") {
$("#justificativa").parent("div").addClass("errorValidate");
throw "'Qual o motivo da compra?' é obrigatório.";
throw "'Qual o motivo da transferência?' é obrigatório.";
} else {
$("input[id^='quantidadeItem___']").each(function (index, value) {
var linha = $(value).attr("name").split("___")[1];
@@ -1152,6 +1210,9 @@ var beforeSendValidate = function (numState, nextState) {
if ($("#dataEntrega").val() == "") {
throw "'Data da entrega' é obrigatória.";
}
if (String($("#nomerecebedor").val() || "").trim() == "") {
throw "'Nome de quem recebeu a mercadoria' é obrigatório.";
}
} else if (numState == 18) {
var validacaoItens = $("input[name='validacaoItens']:checked").val();
if (validacaoItens == "" || validacaoItens == undefined) {
@@ -1224,6 +1285,7 @@ function setSelectedZoomItem(selectedItem) {
$("#gestorEmail").val(selectedItem["emailGestor"] || "");
$("#gestor_cc").val(selectedItem["COLLEAGUE_ID"] || "");
$("#ufDestino").val(selectedItem["UF"] || "");
refreshMotoristasEntregaByUf(true);
}
if (name_item == "estabelecimento") {
@@ -1275,6 +1337,7 @@ function removedZoomItem(removedItem) {
$("#gestorEmail").val("");
$("#gestor_cc").val("");
$("#ufDestino").val("");
refreshMotoristasEntregaByUf(true);
} else if (name_item == "estabelecimento") {
$("#gestorNomeE").val("");
$("#gestorEmailE").val("");