752 lines
22 KiB
JavaScript
752 lines
22 KiB
JavaScript
$(document).ready(function () {
|
|
try {
|
|
FLUIGC.calendar('#dataNec');
|
|
} catch (e) {
|
|
console.warn("Erro ao iniciar calendário:", e);
|
|
}
|
|
|
|
toggleBotaoAddItem(); // inicia escondido
|
|
|
|
/* ========= Eventos ========= */
|
|
$('#precoRef, #quantidade').on('input', recalcTotal);
|
|
|
|
// formata o teto de preço só quando o usuário sair do campo
|
|
$('#precoRef').on('blur', function() {
|
|
const p = brMoneyToFloat($(this).val());
|
|
$(this).val(floatToBRL(p));
|
|
recalcTotal();
|
|
});
|
|
|
|
$('#qtdMais').on('click', function () {
|
|
let v = parseInt($('#quantidade').val() || '0', 10);
|
|
$('#quantidade').val(v + 1);
|
|
recalcTotal();
|
|
});
|
|
|
|
$('#qtdMenos').on('click', function () {
|
|
let v = parseInt($('#quantidade').val() || '0', 10);
|
|
v = Math.max(0, v - 1);
|
|
$('#quantidade').val(v);
|
|
recalcTotal();
|
|
});
|
|
|
|
$('#btnBuscaProd').on('click', consultarProdutos);
|
|
$('#btnLimpaProd').on('click', limparCamposPrincipais);
|
|
$('#btnAddItem').on('click', adicionarItem);
|
|
});
|
|
|
|
/* ========= Total / Quantidade ========= */
|
|
function recalcTotal() {
|
|
const q = parseFloat($('#quantidade').val() || '0');
|
|
const p = brMoneyToFloat($('#precoRef').val());
|
|
const t = (q * p) || 0;
|
|
$('#total').val(floatToBRL(t));
|
|
|
|
}
|
|
|
|
function setSelectedZoomItem(selectedItem) {
|
|
if (selectedItem.inputId === "estabelecimento") {
|
|
// 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.");
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
|
|
function removedZoomItem(removedItem) {
|
|
if (removedItem.inputId === "estabelecimento") {
|
|
// 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 = "dsComprasProdutos"; // dataset de produtos
|
|
let todosProdutos = [];
|
|
let paginaAtual = 1;
|
|
const itensPorPagina = 10;
|
|
|
|
/* ========= Utils ========= */
|
|
function escapeHTML(txt) {
|
|
return $('<div>').text(txt == null ? '' : String(txt)).html();
|
|
}
|
|
|
|
function brMoneyToFloat(v) {
|
|
if (!v) return 0;
|
|
return parseFloat(
|
|
String(v)
|
|
.replace(/R\$/g, '') // remove "R$"
|
|
.replace(/\s/g, '') // remove espaços
|
|
.replace(/\./g, '') // remove pontos de milhar
|
|
.replace(',', '.') // troca vírgula por ponto
|
|
) || 0;
|
|
}
|
|
|
|
function floatToBRL(n) {
|
|
try {
|
|
return new Intl.NumberFormat('pt-BR', { style: 'currency', currency: 'BRL' }).format(n || 0);
|
|
} catch (_) {
|
|
return 'R$ ' + (n || 0).toFixed(2).replace('.', ',');
|
|
}
|
|
}
|
|
|
|
/* ========= Modal Consulta Produtos ========= */
|
|
function consultarProdutos() {
|
|
FLUIGC.modal({
|
|
title: 'Consulta de Produtos',
|
|
content:
|
|
'<div id="buscaProduto" style="margin-bottom:10px;">' +
|
|
'<input type="text" id="filtroDescricao" class="form-control" placeholder="Buscar por nome..." onkeyup="filtrarProdutos()">' +
|
|
'</div>' +
|
|
'<div id="listaProdutos"></div>' +
|
|
'<div id="paginacaoProdutos" style="margin-top:10px; text-align:center;"></div>',
|
|
id: 'modalConsultaProdutos',
|
|
size: 'large',
|
|
actions: [{ 'label': 'Fechar', 'autoClose': true }]
|
|
}, function (err) {
|
|
if (!err) carregaListaProdutos();
|
|
});
|
|
}
|
|
|
|
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);
|
|
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);
|
|
}
|
|
|
|
paginaAtual = 1;
|
|
renderizaProdutos(filtro);
|
|
}
|
|
|
|
|
|
function filtrarProdutos() {
|
|
paginaAtual = 1;
|
|
renderizaProdutos();
|
|
}
|
|
|
|
function renderizaProdutos(filtro) {
|
|
filtro = (filtro || $('#filtroDescricao').val() || '').toLowerCase();
|
|
|
|
// Agora filtra no cache (sem chamar dataset de novo)
|
|
const produtosFiltrados = todosProdutos.filter(p =>
|
|
(p.descricao || '').toLowerCase().includes(filtro) ||
|
|
(p.codigo || '').toLowerCase().includes(filtro) ||
|
|
(p.medida || '').toLowerCase().includes(filtro)
|
|
);
|
|
|
|
const inicio = (paginaAtual - 1) * itensPorPagina;
|
|
const fim = inicio + itensPorPagina;
|
|
const produtosPagina = produtosFiltrados.slice(inicio, fim);
|
|
|
|
let html = `
|
|
<table class="table table-striped">
|
|
<thead>
|
|
<tr>
|
|
<th>Codigo</th>
|
|
<th>Descricao</th>
|
|
<th>UM</th>
|
|
<th>Ultimo Preco</th>
|
|
<th>Acao</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
`;
|
|
|
|
if (produtosPagina.length === 0) {
|
|
html += `
|
|
<tr>
|
|
<td colspan="5" class="text-center text-muted">
|
|
Nenhum produto encontrado
|
|
</td>
|
|
</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';
|
|
|
|
html += `
|
|
<tr>
|
|
<td>${codigo}</td>
|
|
<td>${descricao}</td>
|
|
<td>${um}</td>
|
|
<td>${preco}</td>
|
|
<td>
|
|
<button class="btn btn-sm btn-success selecionar-produto"
|
|
data-codigo="${codigo}"
|
|
data-descricao="${descricao}"
|
|
data-preco="${preco}">
|
|
Selecionar
|
|
</button>
|
|
</td>
|
|
</tr>`;
|
|
});
|
|
}
|
|
|
|
html += `</tbody></table>`;
|
|
$('#listaProdutos').html(html);
|
|
|
|
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) {
|
|
const totalPaginas = Math.ceil(totalItens / itensPorPagina);
|
|
let html = '';
|
|
if (totalPaginas > 1) {
|
|
if (paginaAtual > 1) {
|
|
html += '<button class="btn btn-sm btn-default" onclick="mudarPagina(' + (paginaAtual - 1) + ')">Anterior</button> ';
|
|
}
|
|
html += ' Pagina ' + paginaAtual + ' de ' + totalPaginas + ' ';
|
|
if (paginaAtual < totalPaginas) {
|
|
html += '<button class="btn btn-sm btn-default" onclick="mudarPagina(' + (paginaAtual + 1) + ')">Proxima</button>';
|
|
}
|
|
}
|
|
$('#paginacaoProdutos').html(html);
|
|
}
|
|
|
|
function mudarPagina(n) {
|
|
paginaAtual = n;
|
|
renderizaProdutos();
|
|
}
|
|
|
|
/* ========= Seleção ========= */
|
|
function selecionarProduto(codigo, descricao, preco) {
|
|
$('#produtoCod').val(codigo);
|
|
$('#descProduto').val(codigo + ' - ' + descricao);
|
|
$('#ultimoPreco').val(preco);
|
|
|
|
FLUIGC.toast({ title: 'Produto', message: 'Selecionado: ' + descricao, type: 'success' });
|
|
$('#modalConsultaProdutos').modal('hide');
|
|
|
|
toggleBotaoAddItem(); // 👈 mostra botão
|
|
}
|
|
|
|
function toggleBotaoAddItem() {
|
|
const temProduto = $('#produtoCod').val() || $('#descProduto').val();
|
|
if (temProduto && temProduto.trim() !== "") {
|
|
$('#btnAddItem').show();
|
|
} else {
|
|
$('#btnAddItem').hide();
|
|
}
|
|
}
|
|
/* ========= Adicionar Item ========= */
|
|
function adicionarItem() {
|
|
const cod = $('#produtoCod').val();
|
|
const desc = $('#descProduto').val();
|
|
const qtd = $('#quantidade').val();
|
|
|
|
if (!cod) {
|
|
FLUIGC.toast({ title: 'Atenção', message: 'Selecione um produto/serviço', type: 'warning' });
|
|
return;
|
|
}
|
|
|
|
const idx = wdkAddChild('tbItens');
|
|
|
|
setTimeout(() => {
|
|
$('input[name="Codproduto___' + idx + '"]').val(cod);
|
|
|
|
const somenteDesc = desc.includes('-') ? desc.split('-').slice(1).join('-').trim() : desc;
|
|
$('input[name="produtoDesc___' + idx + '"]').val(somenteDesc);
|
|
$('input[name="qtd___' + idx + '"]').val(qtd).on('input', function () { recalcLinha(this); });
|
|
$('input[name="ultimoPreco___' + idx + '"]').val(prec).on('input', function () { recalcLinha(this); });
|
|
|
|
atualizarIndex();
|
|
atualizarTotais();
|
|
}, 0);
|
|
|
|
FLUIGC.toast({ title: 'Item', message: 'Adicionado à lista', type: 'success' });
|
|
|
|
limparCamposPrincipais();
|
|
}
|
|
|
|
/* ========= Limpar campos principais ========= */
|
|
function limparCamposPrincipais() {
|
|
$('#produtoCod, #descProduto').val('');
|
|
$('#quantidade').val('0');
|
|
$('#precoRef').val('0,00');
|
|
$('#fnComprovante').val('');
|
|
|
|
|
|
toggleBotaoAddItem(); // 👈 mostra botão
|
|
}
|
|
|
|
/* ========= Eventos delegados ========= */
|
|
$(document).on('click', '.selecionar-produto', function () {
|
|
const codigo = $(this).data('codigo');
|
|
const descricao = $(this).data('descricao');
|
|
const preco = $(this).data('preco');
|
|
selecionarProduto(codigo, descricao, preco);
|
|
});
|
|
|
|
|
|
/* ========= Index das linhas ========= */
|
|
function atualizarIndex() {
|
|
$('#tbItens tbody tr.tableRow').each(function (i) {
|
|
if (!$(this).is('[detail=true]')) {
|
|
$(this).find('.tableIndex').text(i);
|
|
}
|
|
});
|
|
}
|
|
|
|
|
|
/* ========= Remover linha ========= */
|
|
function fnWdkRemoveChild(el) {
|
|
$(el).closest('tr').remove();
|
|
atualizarIndex();
|
|
atualizarTotais();
|
|
}
|
|
|
|
/* ========= Link do produto ========= */
|
|
function handleProductLink(el) {
|
|
const $row = $(el).closest("tr");
|
|
const $hidden = $row.find("input.produto-link");
|
|
let linkAtual = $hidden.val();
|
|
|
|
if (!linkAtual) {
|
|
// ainda não tem link → pedir ao usuário
|
|
let novoLink = prompt("Insira o link do produto:");
|
|
if (novoLink && novoLink.trim() !== "") {
|
|
$hidden.val(novoLink.trim()); // grava no hidden (vai persistir no Fluig)
|
|
}
|
|
} else {
|
|
// já existe link → abrir
|
|
window.open(linkAtual, "_blank");
|
|
}
|
|
}
|
|
|
|
$(function () {
|
|
var steps = [
|
|
{ state: 1, label: "Solicitação" },
|
|
{ state: 121, label: "Validando Necessidade" },
|
|
{ state: 82, label: "Enviando pedido" },
|
|
{ state: 114, label: "Cotação Iniciada" },
|
|
{ state: 105, label: "Indicar Cotação" },
|
|
{ state: 137, label: "Confirmação da cotação" },
|
|
{ state: 133, label: "Aprovação Gestor" },
|
|
{ state: 147, label: "Aprovação Ger. Financeiro" },
|
|
{ state: 158, label: "Aprovação CEO" },
|
|
{ state: 18, label: "Recebimento do Produto" },
|
|
{ state: 24, label: "Problema" },
|
|
{ state: 52, label: "Fim" }
|
|
];
|
|
|
|
var current = parseInt($("#activity").val() || "0", 10);
|
|
var idxAtual = steps.findIndex(function (s) { return s.state === current; });
|
|
if (idxAtual < 0) idxAtual = 0;
|
|
|
|
$("#wizard-steps .step-item").each(function (i) {
|
|
if (i < idxAtual) $(this).addClass("done");
|
|
if (i === idxAtual) $(this).addClass("active");
|
|
});
|
|
|
|
// clique nos steps
|
|
$("#wizard-steps .step-item").on("click", function () {
|
|
var index = $(this).index();
|
|
var step = steps[index];
|
|
if (!step) return;
|
|
|
|
// 👉 se já estava ativo, mostra tudo de novo
|
|
if ($(this).hasClass("active") && $(".activity:visible").length === 1) {
|
|
$(".activity").show();
|
|
$("#wizard-steps .step-item").removeClass("active");
|
|
return;
|
|
}
|
|
|
|
// senão, mostra só a activity clicada
|
|
$(".activity").hide();
|
|
$(".activity-" + step.state).show();
|
|
|
|
$("#wizard-steps .step-item").removeClass("active");
|
|
$(this).addClass("active");
|
|
});
|
|
});
|
|
|
|
|
|
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 () {
|
|
preencherResumoSC();
|
|
consultarAndamentoSC();
|
|
|
|
$(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 idCampo = String($(this).attr("id") || "");
|
|
if (idCampo.indexOf("___") < 0) return;
|
|
var idx = idCampo.split("___")[1];
|
|
|
|
// pega qtd e preço da linha
|
|
var qtd = parseNumeroCotacao($("#qtdc___" + idx).val() || "0");
|
|
var preco = parseNumeroCotacao($("#preco___" + idx).val() || "0");
|
|
|
|
// calcula total da linha
|
|
var total = qtd * preco;
|
|
|
|
// atualiza campo total da linha
|
|
$("#total___" + idx).val(total.toFixed(2));
|
|
|
|
// se marcado, acumula na soma
|
|
if ($(this).is(":checked")) {
|
|
soma += total;
|
|
}
|
|
});
|
|
|
|
// 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() {
|
|
recalcularTotalCotacao();
|
|
});
|
|
|
|
// recalcula ao abrir a atividade
|
|
$(document).ready(function() {
|
|
recalcularTotalCotacao();
|
|
});
|
|
|
|
// garante que o total seja salvo antes de enviar a atividade
|
|
function beforeSendValidate(numState, nextState) {
|
|
console.log("Executando beforeSendValidate, recalculando total...");
|
|
recalcularTotalCotacao();
|
|
return true; // precisa retornar true senão bloqueia o envio
|
|
}
|