function displayFields(form, customHTML) { var activity = getValue('WKNumState'); var userId = getValue('WKUser'); form.setShowDisabledFields(true); form.setHidePrintLink(true); form.setValue("WKNumProces", getValue('WKNumProces')); form.setValue("activity", activity); form.setValue("formMode", form.getFormMode()); form.setValue("empresa", getValue("WKCompany")); var c1 = DatasetFactory.createConstraint("colleagueId", userId, userId, ConstraintType.MUST); var filter = new Array(c1); var fields = new Array("colleagueName"); var retorno = DatasetFactory.getDataset("colleague", fields, filter, null); //requester if (activity == 0) { form.setValue("usuario_abertura", retorno.getValue(0, "colleagueName")); form.setValue("requesterMail", retorno.getValue(0, "mail")); form.setValue("requesterId", userId); } else { form.setValue("currentUserName", retorno.getValue(0, "colleagueName")); form.setValue("currentUsermail", retorno.getValue(0, "mail")); form.setValue("currentUserId", userId); } // ✅ Quando for atividade de aprovação do gestor if (activity == 121) { var hoje = new java.text.SimpleDateFormat("dd/MM/yyyy").format(new java.util.Date()); form.setValue("user_validacao_gestor", retorno.getValue(0, "colleagueName")); form.setValue("data_validacao_gestor", hoje); } if (activity == 133) { var hoje = new java.text.SimpleDateFormat("dd/MM/yyyy").format(new java.util.Date()); form.setValue("gestor_validacao_aprova", retorno.getValue(0, "colleagueName")); form.setValue("data_validacao_aprova_gestor", hoje); } if (activity == 137) { var hoje = new java.text.SimpleDateFormat("dd/MM/yyyy").format(new java.util.Date()); form.setValue("user_validacao_aprova", retorno.getValue(0, "colleagueName")); form.setValue("data_validacao_aprova", hoje); } if (activity == 147) { var hoje = new java.text.SimpleDateFormat("dd/MM/yyyy").format(new java.util.Date()); form.setValue("gerf_validacao_aprova", retorno.getValue(0, "colleagueName")); form.setValue("data_validacao_aprova_gerf", hoje); } if (activity == 18) { var hoje = new java.text.SimpleDateFormat("dd/MM/yyyy").format(new java.util.Date()); form.setValue("userValidacaoItens", retorno.getValue(0, "colleagueName")); form.setValue("dataValidacaoItens", hoje); } // ✅ Injetar dados da SC do Protheus (se já tiverem sido preenchidos no servicetask) var camposSC = [ "numeroSCProtheus", "statusSCProtheus", "solicitanteSCProtheus", "emissaoSCProtheus", "qtdItensSCProtheus", "dataCadastroSCProtheus", "horaCadastroSCProtheus" ]; camposSC.forEach(function(campo) { var valor = form.getValue(campo); if (valor) { customHTML.append(""); } }); // cor no status var status = form.getValue("statusSCProtheus"); if (status) { var badgeClass = "badge bg-secondary"; if (status.toLowerCase().indexOf("sucesso") >= 0) badgeClass = "badge bg-success"; if (status.toLowerCase().indexOf("erro") >= 0) badgeClass = "badge bg-danger"; customHTML.append(""); } // Consultar informações adicionais da SC no Protheus var numSC = form.getValue("numeroSCProtheus"); if (numSC) { try { var c1 = DatasetFactory.createConstraint("numeroSCProtheus", numSC, numSC, ConstraintType.MUST); var ds = DatasetFactory.getDataset("ds_consultaSC", null, [c1], null); if (ds && ds.rowsCount > 0) { var status = ds.getValue(0, "STATUS") || ""; var cotacao = ds.getValue(0, "C1_COTACAO") || ""; var pedido = ds.getValue(0, "C1_PEDIDO") || ""; // grava nos hidden inputs form.setValue("statusAtendimento", status); form.setValue("cotacaoSCProtheus", cotacao); form.setValue("pedidoSCProtheus", pedido); // mostra nos cards (HTML) customHTML.append(""); customHTML.append(""); customHTML.append(""); } } catch (e) { log.error("❌ Erro ao consultar dataset ds_consultaSC: " + e); } } try { var msgs = []; var numeroSC = form.getValue("numeroSCProtheus"); var cotacaoSC = form.getValue("cotacaoSCProtheus"); var pedidoSC = form.getValue("pedidoSCProtheus"); if (numeroSC) msgs.push("📌 Gerou SC Protheus nº " + numeroSC); if (cotacaoSC) msgs.push("📌 SC originou Cotação nº " + cotacaoSC); if (pedidoSC) msgs.push("📌 Cotação finalizou → Pedido nº " + pedidoSC); if (msgs.length === 0) { msgs.push("ℹ️ Nenhum dado de integração Protheus encontrado no formulário"); } // Monta conteúdo HTML do histórico var htmlHistorico = ""; // Script para abrir modal ao clicar no botão customHTML.append(""); } catch (e) { log.error("[displayFields] Erro ao montar histórico: " + e); } var numCot = form.getValue("cotacaoSCProtheus"); var numSC = form.getValue("numeroSCProtheus"); // pega a SC atual function formatCurrency(v) { if (!v || isNaN(v)) return "R$ 0,00"; var num = parseFloat(v).toFixed(2); return "R$ " + num.replace(".", ",").replace(/\B(?=(\d{3})+(?!\d))/g, "."); } function formatDate(v) { if (!v || v.length !== 8) return v || ""; var y = v.substring(0,4); var m = v.substring(4,6); var d = v.substring(6,8); return d + "/" + m + "/" + y; } function formatPrazo(v) { if (!v) return ""; return v + " dias úteis"; } if (!numCot) { customHTML.append(""); return; } var c = DatasetFactory.createConstraint("numCotacao", numCot, numCot, ConstraintType.MUST); var ds = DatasetFactory.getDataset("dsComprasCotacao", null, [c], null); var precoFormatado = formatCurrency(preco); var totalFormatado = formatCurrency(total); if (ds && ds.rowsCount > 0) { for (var i = 0; i < ds.rowsCount; i++) { var item = ds.getValue(i, "C8_ITEM"); var desc = ds.getValue(i, "B1_DESC"); var qtd = ds.getValue(i, "C8_QUANT"); var preco = ds.getValue(i, "C8_PRECO"); var total = ds.getValue(i, "C8_TOTAL"); // adiciona linha na tabela pai-filho customHTML.append(""); } } else { customHTML.append(""); } try { var numPedido = form.getValue("pedidoSCProtheus"); // hidden no formulário if (!numPedido) { customHTML.append(""); return; } var c = DatasetFactory.createConstraint("numeroPed", numPedido, numPedido, ConstraintType.MUST); var ds = DatasetFactory.getDataset("dsComprasGateway", null, [c], null); if (!ds || ds.rowsCount === 0) { customHTML.append(""); return; } var html = ""; for (var i = 0; i < ds.rowsCount; i++) { var rowNum = ds.getValue(i, "C7_NUM"); var desc = ds.getValue(i, "B1_DESC"); var qtd = ds.getValue(i, "C7_QUANT"); var valor = ds.getValue(i, "C7_TOTAL"); var un = ds.getValue(i, "B1_UM"); var pr = ds.getValue(i, "C7_DATPRF"); html += "
" + "
Pedido nº " + (rowNum || "-") + "
" + "
" + "
Descrição
" + (desc || "-") + "
" + "
Quantidade
" + parseInt(qtd || 0) + " " + (un || "") + "
" + "
Valor
" + formatCurrency(valor) + "
" + "
Data de Entrega
" + formatDate(pr) + "
" + "
" + "
"; } customHTML.append(""); } catch (e) { var msg = "❌ Erro displayFields Pedido: " + (e && e.message ? e.message : e); log.error(msg); customHTML.append(""); } // Se estou na atividade do Gestor do CC (ex: 133) if (activity == 133) { // Copia a justificativa do solicitante para o campo de exibição form.setValue("observacoesdosolicitante", form.getValue("observacoes")); } // Se estou na atividade do GERF (ex: 147) if (activity == 147) { form.setValue("justi_decisao__soli", form.getValue("observacoes")); // solicitante form.setValue("justi_decisao_gc", form.getValue("justi_decisao_aprova_gestor")); // gestor } // Se estou na atividade do CEO (ex: 158) if (activity == 158) { form.setValue("justi_decisao_solic", form.getValue("observacoes")); // solicitante form.setValue("justi_decisaogc", form.getValue("justi_decisao_aprova_gestor")); // gestor form.setValue("justi_decisao_gerf", form.getValue("justi_decisao_aprova_gerente")); // gerente financeiro } }