156 lines
5.4 KiB
JavaScript
156 lines
5.4 KiB
JavaScript
function documentoValido(valor) {
|
|
var v = String(valor || "").trim().toUpperCase();
|
|
if (!v) return false;
|
|
if (v === "000000" || v === "000001") return false;
|
|
if (/^0+$/.test(v)) return false;
|
|
if (/^X+$/.test(v)) return false;
|
|
return true;
|
|
}
|
|
|
|
function consultarComFallback(clientService, endpoints, contexto) {
|
|
var ultimoErro = "";
|
|
|
|
for (var i = 0; i < endpoints.length; i++) {
|
|
try {
|
|
var req = {
|
|
companyId: "1",
|
|
serviceCode: "Postprod",
|
|
endpoint: endpoints[i],
|
|
method: "get",
|
|
timeoutService: "60000",
|
|
headers: {
|
|
"Content-Type": "application/json"
|
|
}
|
|
};
|
|
|
|
log.info("[ST114] Consulta " + contexto + " => " + endpoints[i]);
|
|
var response = clientService.invoke(JSON.stringify(req));
|
|
if (!response || !response.getResult()) {
|
|
ultimoErro = "Sem resposta";
|
|
continue;
|
|
}
|
|
|
|
var body = JSON.parse(response.getResult());
|
|
if (body && body.sucesso) {
|
|
return { sucesso: true, body: body };
|
|
}
|
|
|
|
ultimoErro = (body && (body.erro || body.message)) ? String(body.erro || body.message) : "Resposta sem sucesso";
|
|
} catch (e) {
|
|
ultimoErro = String(e);
|
|
}
|
|
}
|
|
|
|
return { sucesso: false, erro: ultimoErro || ("Falha na consulta de " + contexto) };
|
|
}
|
|
|
|
function apurarAssinaturas(pedidos) {
|
|
var resumo = {
|
|
total: 0,
|
|
aprovadas: 0,
|
|
pendentes: 0,
|
|
bloqueadas: 0,
|
|
rejeitadas: 0,
|
|
outros: 0
|
|
};
|
|
|
|
for (var i = 0; i < pedidos.length; i++) {
|
|
var alcadas = (pedidos[i] && pedidos[i].ALCADAS) ? pedidos[i].ALCADAS : [];
|
|
for (var j = 0; j < alcadas.length; j++) {
|
|
var status = String((alcadas[j] && alcadas[j].CR_STATUS) || "").trim();
|
|
if (!status) continue;
|
|
|
|
resumo.total++;
|
|
if (status === "03") {
|
|
resumo.aprovadas++;
|
|
} else if (status === "01" || status === "02") {
|
|
resumo.pendentes++;
|
|
} else if (status === "04") {
|
|
resumo.bloqueadas++;
|
|
} else if (status === "06" || status === "07") {
|
|
resumo.rejeitadas++;
|
|
} else {
|
|
resumo.outros++;
|
|
}
|
|
}
|
|
}
|
|
|
|
return resumo;
|
|
}
|
|
|
|
function servicetask114(attempt, message) {
|
|
log.info("[ST114] Inicio - valida cotacao, pedido e assinaturas");
|
|
|
|
var numSC = String(hAPI.getCardValue("numeroSCProtheus") || "").trim();
|
|
var numCotForm = String(hAPI.getCardValue("cotacaoSCProtheus") || "").trim();
|
|
var numPedForm = String(hAPI.getCardValue("pedidoSCProtheus") || "").trim();
|
|
|
|
if (!numSC) {
|
|
throw "Numero da SC nao informado no formulario.";
|
|
}
|
|
|
|
var clientService = fluigAPI.getAuthorizeClientService();
|
|
|
|
var consultaSC = consultarComFallback(clientService, [
|
|
"/UF_MATA110/" + numSC,
|
|
"/rest/UF_MATA110/" + numSC,
|
|
"/rest/uf_mata110/" + numSC
|
|
], "SC " + numSC);
|
|
|
|
if (!consultaSC.sucesso) {
|
|
throw "Nao foi possivel consultar a SC " + numSC + ": " + consultaSC.erro;
|
|
}
|
|
|
|
var solicitacoes = (consultaSC.body && consultaSC.body.solicitacoes) ? consultaSC.body.solicitacoes : [];
|
|
if (!solicitacoes.length) {
|
|
throw "SC " + numSC + " sem retorno de dados no Protheus.";
|
|
}
|
|
|
|
var sc = solicitacoes[0] || {};
|
|
var numCot = documentoValido(sc.C1_COTACAO) ? String(sc.C1_COTACAO).trim() : numCotForm;
|
|
var numPed = documentoValido(sc.C1_PEDIDO) ? String(sc.C1_PEDIDO).trim() : numPedForm;
|
|
|
|
if (!documentoValido(numCot)) {
|
|
hAPI.setCardValue("statusAtendimento", "Aguardando cotacao");
|
|
throw "Cotacao ainda nao disponivel para a SC " + numSC + ".";
|
|
}
|
|
hAPI.setCardValue("cotacaoSCProtheus", numCot);
|
|
|
|
if (!documentoValido(numPed)) {
|
|
hAPI.setCardValue("statusAtendimento", "Aguardando pedido");
|
|
throw "Pedido ainda nao gerado para a SC " + numSC + ".";
|
|
}
|
|
hAPI.setCardValue("pedidoSCProtheus", numPed);
|
|
|
|
var consultaPedido = consultarComFallback(clientService, [
|
|
"/UF_MATA120/" + numPed,
|
|
"/rest/UF_MATA120/" + numPed,
|
|
"/rest/uf_mata120/" + numPed
|
|
], "Pedido " + numPed);
|
|
|
|
if (!consultaPedido.sucesso) {
|
|
throw "Nao foi possivel consultar o pedido " + numPed + ": " + consultaPedido.erro;
|
|
}
|
|
|
|
var pedidos = (consultaPedido.body && consultaPedido.body.pedidos) ? consultaPedido.body.pedidos : [];
|
|
var assinatura = apurarAssinaturas(pedidos);
|
|
|
|
if (assinatura.total === 0) {
|
|
hAPI.setCardValue("statusAtendimento", "Pedido sem alcadas");
|
|
throw "Pedido " + numPed + " sem alcadas de assinatura retornadas.";
|
|
}
|
|
|
|
if (assinatura.bloqueadas > 0 || assinatura.rejeitadas > 0) {
|
|
hAPI.setCardValue("statusAtendimento", "Pedido bloqueado/rejeitado");
|
|
throw "Pedido " + numPed + " bloqueado/rejeitado. Nao pode seguir para recebimento.";
|
|
}
|
|
|
|
if (assinatura.aprovadas < assinatura.total) {
|
|
hAPI.setCardValue("statusAtendimento", "Pedido pendente de assinatura");
|
|
throw "Pedido " + numPed + " ainda nao esta 100% assinado (" + assinatura.aprovadas + "/" + assinatura.total + ").";
|
|
}
|
|
|
|
hAPI.setCardValue("statusAtendimento", "Pedido 100% assinado");
|
|
log.info("[ST114] Pedido " + numPed + " 100% assinado. Fluxo liberado para receber produto/servico.");
|
|
}
|