Novo git da alteração
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
function servicetask114(attempt, message) {
|
||||
log.info("🚀 ST92 — Verifica se já existe código de cotação no formulário");
|
||||
|
||||
var numSC = hAPI.getCardValue("numeroSCProtheus");
|
||||
var numCot = hAPI.getCardValue("cotacaoSCProtheus");
|
||||
|
||||
log.info("📌 SC: " + numSC + " | Cotação: " + numCot);
|
||||
|
||||
if (!numSC) {
|
||||
throw "❌ Número da SC não informado no formulário.";
|
||||
}
|
||||
|
||||
// se não tiver cotação ainda, força o loop da atividade
|
||||
if (!numCot || numCot === "000000" || numCot === "000001") {
|
||||
throw "⏳ Cotação ainda não disponível para a SC " + numSC;
|
||||
}
|
||||
|
||||
// se já tiver código, só registra log e deixa o processo seguir
|
||||
log.info("✅ Cotação " + numCot + " encontrada no formulário. Processo segue.");
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
function toProtheusDate(d) {
|
||||
if (!d) return "";
|
||||
if (d.indexOf("/") > -1) {
|
||||
var parts = d.split("/");
|
||||
return parts[2] + parts[1] + parts[0]; // dd/MM/yyyy -> yyyyMMdd
|
||||
}
|
||||
if (d.indexOf("-") > -1) {
|
||||
return d.replace(/-/g, ""); // yyyy-MM-dd -> yyyyMMdd
|
||||
}
|
||||
return d; // já está no formato certo
|
||||
}
|
||||
|
||||
function hojeProtheus() {
|
||||
var d = new Date();
|
||||
var yyyy = d.getFullYear();
|
||||
var mm = d.getMonth() + 1;
|
||||
var dd = d.getDate();
|
||||
if (mm < 10) mm = "0" + mm;
|
||||
if (dd < 10) dd = "0" + dd;
|
||||
return "" + yyyy + mm + dd; // yyyyMMdd
|
||||
}
|
||||
|
||||
function servicetask82(attempt, message) {
|
||||
log.info("🚀 Iniciando servicetask82");
|
||||
|
||||
var qtdItens = hAPI.getChildrenIndexes("tbItens");
|
||||
log.info("📦 Total de itens na tabela: " + qtdItens.length);
|
||||
|
||||
var itens = [];
|
||||
for (var i = 0; i < qtdItens.length; i++) {
|
||||
var index = qtdItens[i];
|
||||
var item = {
|
||||
"C1_PRODUTO": String(hAPI.getCardValue("Codproduto___" + index)),
|
||||
"C1_QUANT": Number(hAPI.getCardValue("qtd___" + index)),
|
||||
"C1_DATPRF": toProtheusDate(String(hAPI.getCardValue("dataNec___" + index)))
|
||||
};
|
||||
itens.push(item);
|
||||
log.info("📌 Item adicionado: " + JSON.stringify(item));
|
||||
}
|
||||
|
||||
var payload = {
|
||||
"C1_SOLICIT": String(hAPI.getCardValue("usuarioSolicitante")),
|
||||
"C1_EMISSAO": hojeProtheus(),
|
||||
"C1_FILENT": String(hAPI.getCardValue("filialProtheus")).trim(),
|
||||
"C1_OBS": String(hAPI.getCardValue("observacoes")),
|
||||
"itens": itens
|
||||
};
|
||||
|
||||
log.info("📦 Payload montado:");
|
||||
log.info(JSON.stringify(payload));
|
||||
|
||||
var data1 = {
|
||||
companyId: '1',
|
||||
serviceCode: 'Postprod',
|
||||
endpoint: '/rest/UF_MATA110',
|
||||
method: 'post',
|
||||
timeoutService: '100',
|
||||
params: payload, // objeto puro
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
};
|
||||
|
||||
try {
|
||||
var clientService = fluigAPI.getAuthorizeClientService();
|
||||
var response = clientService.invoke(JSON.stringify(data1)); // SEMPRE stringify aqui
|
||||
|
||||
log.info("📬 Resposta da API Protheus:");
|
||||
log.dir(response);
|
||||
|
||||
if (response && response.getResult()) {
|
||||
var result = JSON.parse(response.getResult());
|
||||
if (result.sucesso) {
|
||||
hAPI.setCardValue("numeroSCProtheus", result.C1_NUM || "");
|
||||
hAPI.setCardValue("statusSCProtheus", "SC cadastrada com sucesso");
|
||||
hAPI.setCardValue("solicitanteSCProtheus", result.C1_SOLICIT || "");
|
||||
hAPI.setCardValue("emissaoSCProtheus", result.C1_EMISSAO || "");
|
||||
hAPI.setCardValue("qtdItensSCProtheus", String(result.quantidade_itens || ""));
|
||||
hAPI.setCardValue("dataCadastroSCProtheus", result.data_cadastro || "");
|
||||
hAPI.setCardValue("horaCadastroSCProtheus", result.hora_cadastro || "");
|
||||
} else {
|
||||
hAPI.setCardValue("statusSCProtheus", "Erro: " + result.erro);
|
||||
throw "Erro Protheus: " + result.message;
|
||||
}
|
||||
} else {
|
||||
throw "Sem resposta da API Protheus";
|
||||
}
|
||||
} catch (e) {
|
||||
log.error("❌ Erro ao enviar SC para o Protheus: " + e);
|
||||
throw e;
|
||||
}
|
||||
|
||||
|
||||
|
||||
log.info("🏁 Finalização da servicetask82");
|
||||
}
|
||||
Reference in New Issue
Block a user