att
This commit is contained in:
@@ -46,15 +46,21 @@ function recalcTotal() {
|
||||
|
||||
function setSelectedZoomItem(selectedItem) {
|
||||
if (selectedItem.inputId === "estabelecimento") {
|
||||
// joga o código da filial (cnpj, etc.)
|
||||
$("#filialdest").val(selectedItem.CNPJ); // supondo que o dataset dsSysCompany devolva o campo CNPJ
|
||||
$("#filialest").val(selectedItem.ESTADO); // supondo que o dataset dsSysCompany devolva o campo ESTADO
|
||||
$("#filialprotheus").val(selectedItem.CODIGO); // supondo que o dataset dsSysCompany devolva o campo CODIGO
|
||||
}
|
||||
if (selectedItem.inputId === "centro_custo") {
|
||||
// joga o código da filial (cnpj, etc.)
|
||||
$("#gestor_cc").val(selectedItem.idGestor); // supondo que o dataset Centro_custo devolva o campo GestorCentroCusto
|
||||
$("#codigocentroCusto").val(selectedItem.codigoCentroCusto); // supondo que o dataset Centro_custo devolva o campo GestorCentroCusto
|
||||
// dsFiliais: guarda dados da filial e define gestor da próxima atividade
|
||||
var codigoProtheus = String(selectedItem.PROTHEUS || selectedItem.protheus || "").trim();
|
||||
|
||||
$("#filialdest").val(selectedItem.LOJA || "");
|
||||
$("#filialest").val(selectedItem.UF || "");
|
||||
$("#filialprotheus").val(codigoProtheus);
|
||||
$("#centro_custo").val(selectedItem.RESPONSAVEL_LOJA || selectedItem.LOJA || "");
|
||||
$("#codigocentroCusto").val(codigoProtheus);
|
||||
|
||||
var gestorLoja = (selectedItem.COLLEAGUE_ID || selectedItem.LOGIN_LOJA || "").trim();
|
||||
$("#gestor_cc").val(gestorLoja);
|
||||
|
||||
if (!codigoProtheus) {
|
||||
console.warn("Filial selecionada sem campo PROTHEUS no dsFiliais.");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -62,14 +68,18 @@ function setSelectedZoomItem(selectedItem) {
|
||||
|
||||
function removedZoomItem(removedItem) {
|
||||
if (removedItem.inputId === "estabelecimento") {
|
||||
// Quando remover a empresa, limpa os campos relacionados
|
||||
$("#filialDesc").val('');
|
||||
$("#filialest").val('');
|
||||
// Quando remover a filial, limpa os campos relacionados
|
||||
$("#filialdest").val("");
|
||||
$("#filialest").val("");
|
||||
$("#filialprotheus").val("");
|
||||
$("#centro_custo").val("");
|
||||
$("#codigocentroCusto").val("");
|
||||
$("#gestor_cc").val("");
|
||||
}
|
||||
}
|
||||
|
||||
/* ========= Config ========= */
|
||||
const DATASET_PRODUTOS = "dsProd"; // dataset de produtos
|
||||
const DATASET_PRODUTOS = "dsComprasProdutos"; // dataset de produtos
|
||||
let todosProdutos = [];
|
||||
let paginaAtual = 1;
|
||||
const itensPorPagina = 10;
|
||||
@@ -120,7 +130,10 @@ function carregaListaProdutos(filtro) {
|
||||
// Só chama o dataset se ainda não carregou nada
|
||||
if (todosProdutos.length === 0) {
|
||||
let dataset = DatasetFactory.getDataset(DATASET_PRODUTOS, null, null, null);
|
||||
todosProdutos = dataset && dataset.values ? dataset.values : [];
|
||||
const values = dataset && dataset.values ? dataset.values : [];
|
||||
todosProdutos = values
|
||||
.map(normalizarProduto)
|
||||
.filter(p => p.codigo && p.descricao);
|
||||
console.log("Produtos carregados do dataset:", todosProdutos.length);
|
||||
}
|
||||
|
||||
@@ -171,10 +184,10 @@ function renderizaProdutos(filtro) {
|
||||
</tr>`;
|
||||
} else {
|
||||
produtosPagina.forEach(produto => {
|
||||
const codigo = escapeHTML(produto['codigo']);
|
||||
const descricao = escapeHTML(produto['descricao']);
|
||||
const um = escapeHTML(produto['medida'] || '');
|
||||
const preco = produto['ultimo_preco'] || '0,00';
|
||||
const codigo = escapeHTML(produto.codigo);
|
||||
const descricao = escapeHTML(produto.descricao);
|
||||
const um = escapeHTML(produto.medida || '');
|
||||
const preco = produto.ultimo_preco || '0,00';
|
||||
|
||||
html += `
|
||||
<tr>
|
||||
@@ -200,6 +213,15 @@ function renderizaProdutos(filtro) {
|
||||
renderizaPaginacao(produtosFiltrados.length);
|
||||
}
|
||||
|
||||
function normalizarProduto(row) {
|
||||
return {
|
||||
codigo: String(row.B1_COD || row.codigo || "").trim(),
|
||||
descricao: String(row.B1_DESC || row.descricao || "").trim(),
|
||||
medida: String(row.B1_UM || row.medida || "").trim(),
|
||||
ultimo_preco: String(row.B1_UPRC || row.ultimo_preco || "0,00").trim()
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
function renderizaPaginacao(totalItens) {
|
||||
@@ -375,29 +397,325 @@ $(function () {
|
||||
});
|
||||
|
||||
|
||||
function valorCampo(id) {
|
||||
return String($("#" + id).val() || "").trim();
|
||||
}
|
||||
|
||||
function setLabel(id, valor) {
|
||||
var texto = String(valor || "").trim();
|
||||
$("#" + id).text(texto || "-");
|
||||
}
|
||||
|
||||
function badgeClassByStatus(status) {
|
||||
var s = String(status || "").toLowerCase();
|
||||
if (!s) return "badge bg-secondary";
|
||||
if (s.indexOf("erro") >= 0 || s.indexOf("reprov") >= 0 || s.indexOf("cancel") >= 0) return "badge bg-danger";
|
||||
if (s.indexOf("sucesso") >= 0 || s.indexOf("aprov") >= 0 || s.indexOf("gerad") >= 0 || s.indexOf("liberad") >= 0) return "badge bg-success";
|
||||
if (s.indexOf("aguard") >= 0 || s.indexOf("pend") >= 0) return "badge bg-warning";
|
||||
return "badge bg-info";
|
||||
}
|
||||
|
||||
function setBadge(selector, valor) {
|
||||
var texto = String(valor || "").trim() || "-";
|
||||
$(selector)
|
||||
.text(texto)
|
||||
.removeClass("bg-secondary bg-success bg-danger bg-warning bg-info")
|
||||
.addClass(badgeClassByStatus(texto));
|
||||
}
|
||||
|
||||
function normalizarStatusCadastro(statusCadastro, numeroSC) {
|
||||
var numero = String(numeroSC || "").trim();
|
||||
var status = String(statusCadastro || "").trim();
|
||||
var s = status.toLowerCase();
|
||||
|
||||
if (!numero) return status;
|
||||
if (!status) return "SC cadastrada com sucesso";
|
||||
if (s.indexOf("sucesso") >= 0 || s.indexOf("cadastr") >= 0) return "SC cadastrada com sucesso";
|
||||
return status;
|
||||
}
|
||||
|
||||
function normalizarAndamento(andamento, cotacao, pedido) {
|
||||
var atual = String(andamento || "").trim();
|
||||
if (pedido) return "Pedido gerado";
|
||||
if (cotacao && (!atual || atual.toLowerCase().indexOf("pedido") < 0)) return "Cotacao gerada";
|
||||
return atual;
|
||||
}
|
||||
|
||||
function normalizarDataProtheus(data) {
|
||||
var d = String(data || "").trim();
|
||||
if (/^\d{8}$/.test(d)) {
|
||||
return d.substring(6, 8) + "/" + d.substring(4, 6) + "/" + d.substring(0, 4);
|
||||
}
|
||||
return d;
|
||||
}
|
||||
|
||||
function limparNumeroDocumento(valor) {
|
||||
var v = String(valor || "").trim();
|
||||
if (!v || /^0+$/.test(v)) return "";
|
||||
return v;
|
||||
}
|
||||
|
||||
function comporDataHora(data, hora) {
|
||||
var d = String(data || "").trim();
|
||||
var h = String(hora || "").trim();
|
||||
if (d && h && h !== "-") return d + " as " + h;
|
||||
return d || h || "";
|
||||
}
|
||||
|
||||
function montarEventosTimelineSC(dados) {
|
||||
var eventos = [];
|
||||
var momentoSolicitacao = comporDataHora(dados.dataCadastro, dados.horaCadastro);
|
||||
var momentoSC = dados.emissao || momentoSolicitacao;
|
||||
|
||||
eventos.push({
|
||||
classe: (dados.numero || dados.solicitante || momentoSolicitacao) ? "done" : "pending",
|
||||
titulo: "Solicitacao criada",
|
||||
momento: momentoSolicitacao,
|
||||
detalhe: dados.solicitante ? "Solicitante: " + dados.solicitante : ""
|
||||
});
|
||||
|
||||
if (dados.numero) {
|
||||
eventos.push({
|
||||
classe: "done",
|
||||
titulo: "SC " + dados.numero + " cadastrada",
|
||||
momento: momentoSC,
|
||||
detalhe: dados.statusCadastro ? "Status: " + dados.statusCadastro : ""
|
||||
});
|
||||
} else {
|
||||
eventos.push({
|
||||
classe: "pending",
|
||||
titulo: "Aguardando geracao da SC",
|
||||
momento: "",
|
||||
detalhe: "A SC sera exibida assim que for criada no Protheus."
|
||||
});
|
||||
}
|
||||
|
||||
if (dados.cotacao) {
|
||||
eventos.push({
|
||||
classe: "done",
|
||||
titulo: "Cotacao " + dados.cotacao + " gerada",
|
||||
momento: "",
|
||||
detalhe: "Cotacao disponivel para analise."
|
||||
});
|
||||
} else {
|
||||
eventos.push({
|
||||
classe: "pending",
|
||||
titulo: "Aguardando cotacao",
|
||||
momento: "",
|
||||
detalhe: "Ainda nao existe cotacao vinculada a SC."
|
||||
});
|
||||
}
|
||||
|
||||
if (dados.pedido) {
|
||||
eventos.push({
|
||||
classe: "done",
|
||||
titulo: "Pedido " + dados.pedido + " gerado",
|
||||
momento: "",
|
||||
detalhe: "Compra convertida em pedido."
|
||||
});
|
||||
} else {
|
||||
eventos.push({
|
||||
classe: "pending",
|
||||
titulo: "Aguardando pedido",
|
||||
momento: "",
|
||||
detalhe: "O pedido sera criado apos a aprovacao final da cotacao."
|
||||
});
|
||||
}
|
||||
|
||||
return eventos;
|
||||
}
|
||||
|
||||
function renderizarTimelineSC(dados) {
|
||||
var eventos = montarEventosTimelineSC(dados);
|
||||
var html = eventos.map(function (evento) {
|
||||
var titulo = escapeHTML(evento.titulo);
|
||||
var momento = escapeHTML(evento.momento || "");
|
||||
var detalhe = escapeHTML(evento.detalhe || "");
|
||||
var classe = evento.classe === "done" ? "done" : "pending";
|
||||
|
||||
return [
|
||||
'<li class="sc-timeline-item ' + classe + '">',
|
||||
' <div class="sc-timeline-title-row">',
|
||||
' <span class="sc-timeline-event">' + titulo + '</span>',
|
||||
momento ? (' <span class="sc-timeline-time">' + momento + '</span>') : "",
|
||||
" </div>",
|
||||
detalhe ? (' <div class="sc-timeline-detail">' + detalhe + "</div>") : "",
|
||||
"</li>"
|
||||
].join("");
|
||||
}).join("");
|
||||
|
||||
$("#scTimeline").html(html);
|
||||
}
|
||||
|
||||
function focarTimelineSC() {
|
||||
var secao = $("#scTimelineSection");
|
||||
if (!secao.length) return;
|
||||
|
||||
secao.addClass("is-focus");
|
||||
setTimeout(function () {
|
||||
secao.removeClass("is-focus");
|
||||
}, 900);
|
||||
|
||||
try {
|
||||
secao.get(0).scrollIntoView({ behavior: "smooth", block: "nearest" });
|
||||
} catch (e) {
|
||||
// fallback para navegadores sem smooth scroll
|
||||
secao.get(0).scrollIntoView();
|
||||
}
|
||||
}
|
||||
|
||||
function abrirTimelineSC() {
|
||||
var secao = $("#scTimelineSection");
|
||||
if (!secao.length) return;
|
||||
secao.addClass("is-open");
|
||||
$("#cardNumeroSCHint").text("Clique para ocultar a linha do tempo");
|
||||
}
|
||||
|
||||
function fecharTimelineSC() {
|
||||
var secao = $("#scTimelineSection");
|
||||
if (!secao.length) return;
|
||||
secao.removeClass("is-open is-focus");
|
||||
$("#cardNumeroSCHint").text("Clique para ver a linha do tempo");
|
||||
}
|
||||
|
||||
function alternarTimelineSC() {
|
||||
var secao = $("#scTimelineSection");
|
||||
if (!secao.length) return;
|
||||
|
||||
if (secao.hasClass("is-open")) {
|
||||
fecharTimelineSC();
|
||||
return;
|
||||
}
|
||||
|
||||
abrirTimelineSC();
|
||||
focarTimelineSC();
|
||||
}
|
||||
|
||||
function montarStatusAndamento(scRow, cotacao, pedido) {
|
||||
if (pedido) return "Pedido gerado";
|
||||
if (cotacao) return "Cotacao gerada";
|
||||
|
||||
var statusApi = String(scRow.STATUS || "").trim();
|
||||
if (statusApi) return statusApi;
|
||||
|
||||
var aprov = String(scRow.C1_APROV || "").trim().toUpperCase();
|
||||
if (aprov === "B") return "Aguardando cotacao";
|
||||
if (aprov === "L") return "Liberada";
|
||||
if (aprov === "R") return "Reprovada";
|
||||
if (aprov) return "Status Protheus: " + aprov;
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
function preencherResumoSC() {
|
||||
var numero = valorCampo("numeroSCProtheus");
|
||||
var statusCadastro = valorCampo("statusSCProtheus");
|
||||
var solicitante = valorCampo("solicitanteSCProtheus");
|
||||
var emissao = normalizarDataProtheus(valorCampo("emissaoSCProtheus"));
|
||||
var qtdItens = valorCampo("qtdItensSCProtheus");
|
||||
var dataCadastro = normalizarDataProtheus(valorCampo("dataCadastroSCProtheus"));
|
||||
var horaCadastro = valorCampo("horaCadastroSCProtheus");
|
||||
var cotacao = limparNumeroDocumento(valorCampo("cotacaoSCProtheus"));
|
||||
var pedido = limparNumeroDocumento(valorCampo("pedidoSCProtheus"));
|
||||
var andamento = valorCampo("statusAtendimento");
|
||||
var statusCadastroPadrao = normalizarStatusCadastro(statusCadastro, numero);
|
||||
|
||||
if (!andamento && numero) {
|
||||
if (pedido) andamento = "Pedido gerado";
|
||||
else if (cotacao) andamento = "Cotacao gerada";
|
||||
else andamento = "Em andamento";
|
||||
}
|
||||
andamento = normalizarAndamento(andamento, cotacao, pedido);
|
||||
|
||||
setLabel("numeroSCProtheus_label", numero);
|
||||
setLabel("solicitanteSCProtheus_label", solicitante);
|
||||
setLabel("emissaoSCProtheus_label", emissao);
|
||||
setLabel("qtdItensSCProtheus_label", qtdItens);
|
||||
setLabel("dataCadastroSCProtheus_label", dataCadastro);
|
||||
setLabel("horaCadastroSCProtheus_label", horaCadastro);
|
||||
setLabel("cotacaoSC_label", cotacao);
|
||||
setLabel("pedidoSC_label", pedido);
|
||||
|
||||
setBadge("#statusSCProtheus_label", statusCadastroPadrao || (numero ? "SC cadastrada com sucesso" : ""));
|
||||
setBadge("#statusSC_label", andamento);
|
||||
|
||||
renderizarTimelineSC({
|
||||
numero: numero,
|
||||
statusCadastro: statusCadastroPadrao,
|
||||
andamento: andamento,
|
||||
solicitante: solicitante,
|
||||
emissao: emissao,
|
||||
dataCadastro: dataCadastro,
|
||||
horaCadastro: horaCadastro,
|
||||
cotacao: cotacao,
|
||||
pedido: pedido
|
||||
});
|
||||
}
|
||||
|
||||
function consultarAndamentoSC() {
|
||||
var numero = valorCampo("numeroSCProtheus");
|
||||
if (!numero || typeof DatasetFactory === "undefined" || typeof ConstraintType === "undefined") {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
var cNumero = DatasetFactory.createConstraint("numeroSCProtheus", numero, numero, ConstraintType.MUST);
|
||||
var ds = DatasetFactory.getDataset("ds_consultaSC", null, [cNumero], null);
|
||||
var row = (ds && ds.values && ds.values.length > 0) ? ds.values[0] : null;
|
||||
|
||||
if (!row) return;
|
||||
if (String(row.sucesso || "").toLowerCase() !== "true") return;
|
||||
|
||||
var cotacao = limparNumeroDocumento(row.C1_COTACAO);
|
||||
var pedido = limparNumeroDocumento(row.C1_PEDIDO);
|
||||
|
||||
if (cotacao) $("#cotacaoSCProtheus").val(cotacao);
|
||||
if (pedido) $("#pedidoSCProtheus").val(pedido);
|
||||
|
||||
if (row.C1_SOLICIT) $("#solicitanteSCProtheus").val(String(row.C1_SOLICIT).trim());
|
||||
if (row.C1_EMISSAO) $("#emissaoSCProtheus").val(normalizarDataProtheus(row.C1_EMISSAO));
|
||||
|
||||
var andamento = montarStatusAndamento(row, cotacao, pedido);
|
||||
if (andamento) $("#statusAtendimento").val(andamento);
|
||||
|
||||
preencherResumoSC();
|
||||
} catch (e) {
|
||||
console.warn("Nao foi possivel consultar andamento da SC no ds_consultaSC:", e);
|
||||
}
|
||||
}
|
||||
|
||||
$(document).ready(function () {
|
||||
// Quando carregar a página, já pinta o status se tiver valor nos inputs
|
||||
let status = $("#statusSCProtheus").val() || "";
|
||||
let badgeClass = "badge bg-secondary";
|
||||
preencherResumoSC();
|
||||
consultarAndamentoSC();
|
||||
|
||||
if (status.toLowerCase().includes("sucesso")) badgeClass = "badge bg-success";
|
||||
if (status.toLowerCase().includes("erro")) badgeClass = "badge bg-danger";
|
||||
|
||||
$("#statusSCProtheus_label")
|
||||
.text(status || "-")
|
||||
.removeClass()
|
||||
.addClass(badgeClass);
|
||||
$(document).on("click", "#cardNumeroSC", function () {
|
||||
alternarTimelineSC();
|
||||
});
|
||||
});
|
||||
|
||||
function parseNumeroCotacao(valor) {
|
||||
var texto = String(valor || "").trim();
|
||||
if (!texto) return 0;
|
||||
|
||||
if (texto.indexOf(",") >= 0) {
|
||||
texto = texto.replace(/\./g, "").replace(",", ".");
|
||||
}
|
||||
|
||||
texto = texto.replace(/[^\d.-]/g, "");
|
||||
return parseFloat(texto) || 0;
|
||||
}
|
||||
|
||||
function recalcularTotalCotacao() {
|
||||
var soma = 0;
|
||||
|
||||
$("input[name^='selecionado___']").each(function() {
|
||||
var idx = $(this).attr("id").split("___")[1];
|
||||
var idCampo = String($(this).attr("id") || "");
|
||||
if (idCampo.indexOf("___") < 0) return;
|
||||
var idx = idCampo.split("___")[1];
|
||||
|
||||
// pega qtd e preço da linha
|
||||
var qtd = parseFloat($("#qtdc___" + idx).val() || "0");
|
||||
var preco = parseFloat($("#preco___" + idx).val() || "0");
|
||||
var qtd = parseNumeroCotacao($("#qtdc___" + idx).val() || "0");
|
||||
var preco = parseNumeroCotacao($("#preco___" + idx).val() || "0");
|
||||
|
||||
// calcula total da linha
|
||||
var total = qtd * preco;
|
||||
@@ -413,6 +731,7 @@ function recalcularTotalCotacao() {
|
||||
|
||||
// Atualiza o hidden e dispara change pro Fluig gravar
|
||||
$("#valorTotalCotacao").val(soma.toFixed(2)).trigger("change");
|
||||
$("#valorTotalCotacaoLabel").text(floatToBRL(soma));
|
||||
}
|
||||
// dispara sempre que marcar/desmarcar
|
||||
$(document).on("change", "input[name^='selecionado___']", function() {
|
||||
|
||||
Reference in New Issue
Block a user