1 FlowEssentials_LancamentodeDocumento Lançamento de documento Este Flow permite ao colaborador lançar dados referentes a um documento, seja uma nota, recibo, boleto ou outro que a empresa possua, com a obrigatoriedade de anexar o documento citado. Quando aprovada a etapa de Análise do documento, o mesmo será publicado no ECM e herdará as propriedades de validade e notificação. true true Default Documentos 0 0 0 Default false 1 true false 86400 43200 true 1 Lançamento de documento 13 41254 true false false true false false 0 2 false false false 1 FlowEssentials_LancamentodeDocumento 1 4 Lançar documento Lançar documento 0 false 0 1 true true false 3600 3600 0 0 false 0 true true false 120 100 0 0 false false 0 0 false 0 10 0 false 0 0 0 0 false 1 FlowEssentials_LancamentodeDocumento 1 5 Analisar documento Analisar documento 86400 false 0 Pool Grupo <AssignmentController><Group>Fiscal</Group></AssignmentController> 1 false true false 3600 3600 0 0 false 0 true false false 280 241 0 0 false false 0 0 false Default 0 80 0 false 0 0 0 0 false 0 1 FlowEssentials_LancamentodeDocumento 1 15 Integração Contas a pagar Integração Contas a pagar 0 false 0 1 false false false 0 0 0 0 false 0 true false false 510 475 0 0 false false 0 0 false 0 82 0 false 0 0 0 0 false 1 1 FlowEssentials_LancamentodeDocumento 1 22 Revisar Integração Revisar Integração 0 false 0 1 false true false 3600 3600 0 0 false 0 true false false 510 310 0 0 false false 0 0 false 0 80 0 false 0 0 0 0 false 0 1 FlowEssentials_LancamentodeDocumento 1 24 Intermediário Intermediário Evento intermediário do processo 0 false 0 0 false true false 1 1 0 0 false 0 true false false 570 480 0 0 false false 0 false 0 43 0 false 0 false 15 1 FlowEssentials_LancamentodeDocumento 1 14 Cancelar Cancelar Atividade final do processo 0 false 0 0 false true false 0 1 0 0 false 0 false false false 550 260 0 0 false false 0 0 false 6 65 0 false 0 false 1 FlowEssentials_LancamentodeDocumento 1 17 Fim Fim Atividade final do processo 0 false 0 0 false true false 0 1 0 0 false 0 false false false 790 503 0 0 false false 0 0 false 6 60 0 false 0 false 1 FlowEssentials_LancamentodeDocumento 1 6 false 4 5 false false 0 1 FlowEssentials_LancamentodeDocumento 1 13 false 5 14 Cancelar false false 0 1 FlowEssentials_LancamentodeDocumento 1 16 false 5 15 false false 0 1 FlowEssentials_LancamentodeDocumento 1 18 false 15 17 false false 0 1 FlowEssentials_LancamentodeDocumento 1 25 false 24 22 false false 0 1 FlowEssentials_LancamentodeDocumento 1 26 false 22 15 false false 0 1 servicetask15 FlowEssentials_LancamentodeDocumento 1 function servicetask15(attempt, message) { var payload = buildSe2010Payload(); appendDebugSe2010("servicetask15: payload=" + safeStringify(payload)); var response = invokeSe2010(payload); appendDebugSe2010("servicetask15: status=" + response.httpStatus + " body=" + response.raw); if (!response.success) { throw "Falha ao enviar dados para Protheus SE2010. Status HTTP: " + response.httpStatus + "."; } } function buildSe2010Payload() { var payload = { FILIAL: requiredCard("FILIAL"), PREFIXO: requiredCard("PREFIXO"), NUMERO: requiredCard("NUMERO"), PARCELA: requiredCard("PARCELA"), TIPO: requiredCard("TIPO"), NATUREZA: requiredCard("NATUREZA"), COD_FORN: requiredCard("COD_FORN"), LOJA: requiredCard("LOJA"), EMISSAO: normalizeDate(requiredCard("EMISSAO"), "EMISSAO"), VENCIMENTO: normalizeDate(requiredCard("VENCIMENTO"), "VENCIMENTO"), VENC_REAL: normalizeDate(requiredCard("VENC_REAL"), "VENC_REAL"), VALOR: parseMoney(requiredCard("VALORtotal", "VALOR")), HISTORICO: requiredCard("HISTORICO"), CODIGO_BARRAS: card("CODIGO_BARRAS"), CENTRO_CUSTO: requiredCard("CENTRO_CUSTO") }; return payload; } function invokeSe2010(payload) { var serviceCode = card("service_code_se2010") || "protheus_teste"; var endpoint = card("endpoint_se2010") || "https://comerciode186920.protheus.cloudtotvs.com.br:4050/rest/uf_se2010"; var requestData = { companyId: getCompanyIdSafe(), serviceCode: serviceCode, endpoint: endpoint, method: "post", timeoutService: "120", headers: { "Content-Type": "application/json", "Accept": "application/json" }, options: { encoding: "UTF-8", mediaType: "application/json", useSSL: true }, params: payload }; var clientService = fluigAPI.getAuthorizeClientService(); var vo = clientService.invoke(JSON.stringify(requestData)); var raw = vo ? String(vo.getResult() || "") : ""; if (!raw) { throw "Resposta vazia da API SE2010."; } var httpStatus = getHttpStatus(vo); var parsed = parseJsonSafe(raw); var success = httpStatus >= 200 && httpStatus < 300; if (parsed && parsed.success === false) { success = false; } return { success: success, httpStatus: httpStatus, raw: raw, parsed: parsed }; } function requiredCard() { var value = ""; for (var i = 0; i < arguments.length; i++) { value = card(arguments[i]); if (value) { break; } } if (!value) { throw "Campo obrigatorio nao informado para envio Protheus: " + arguments[0]; } return value; } function card(name) { return ((hAPI.getCardValue(name) || "") + "").replace(/^\s+|\s+$/g, ""); } function normalizeDate(value, fieldName) { var digits = (value || "").replace(/\D/g, ""); if (digits.length !== 8) { throw "Data invalida no campo " + fieldName + ". Informe no formato YYYYMMDD."; } return digits; } function parseMoney(value) { var raw = (value || "").toString().replace(/\s/g, ""); if (!raw) { return 0; } if (raw.indexOf(",") >= 0) { raw = raw.replace(/\./g, "").replace(",", "."); } else { var dotCount = (raw.match(/\./g) || []).length; if (dotCount > 1) { raw = raw.replace(/\./g, ""); } } var parsed = parseFloat(raw); if (isNaN(parsed)) { throw "Valor invalido para envio Protheus: " + value; } return parseFloat(parsed.toFixed(2)); } function getCompanyIdSafe() { try { if (typeof getValue === "function") { return String(getValue("WKCompany") || "1"); } } catch (e) { // ignore } return "1"; } function getHttpStatus(vo) { try { var status = parseInt(String(vo.getHttpStatusResult() || "0"), 10); if (!isNaN(status) && status > 0) { return status; } } catch (e) { // ignore } return 200; } function parseJsonSafe(value) { try { return JSON.parse(value); } catch (e) { return null; } } function safeStringify(value) { try { return JSON.stringify(value); } catch (e) { return String(value); } } function appendDebugSe2010(message) { try { var prefix = "[SE2010] "; var atual = (hAPI.getCardValue("debug_publicacao") || "") + ""; var linha = prefix + message; hAPI.setCardValue("debug_publicacao", atual ? (atual + "\n" + linha) : linha); } catch (e) { log.warn("appendDebugSe2010 falhou: " + e); } } 1 beforeStateEntry FlowEssentials_LancamentodeDocumento 1 function beforeStateEntry(sequenceId) { appendDebugPublicacao("beforeStateEntry sequenceId=" + sequenceId); if (sequenceId == 7) { anexarDocumentoDoCardSeNecessario(); publicarDocumento(); } if (sequenceId == 7) { var attachments = hAPI.listAttachments(); var hasAttachment = attachments && attachments.size() > 0; appendDebugPublicacao("validacao anexos: total=" + (attachments ? attachments.size() : 0)); if (!hasAttachment) { appendDebugPublicacao("validacao anexos: nenhum anexo encontrado apos processamento."); throw "\n\nNao identificamos nenhum documento anexado, volte ao formulario e anexe pelo menos um documento para avancar a solicitacao.\n\n"; } } } function anexarDocumentoDoCardSeNecessario() { try { var attachments = hAPI.listAttachments(); if (attachments && attachments.size() > 0) { appendDebugPublicacao("anexarDocumentoDoCardSeNecessario: solicitacao ja possui " + attachments.size() + " anexo(s)."); return; } var anexoIdStr = (hAPI.getCardValue("anexo_documento_id") || "") + ""; anexoIdStr = anexoIdStr.replace(/^\s+|\s+$/g, ""); if (!anexoIdStr) { appendDebugPublicacao("anexarDocumentoDoCardSeNecessario: campo anexo_documento_id vazio."); return; } var anexoId = parseInt(anexoIdStr, 10); if (isNaN(anexoId) || anexoId <= 0) { appendDebugPublicacao("anexarDocumentoDoCardSeNecessario: anexo_documento_id invalido: " + anexoIdStr); return; } hAPI.attachDocument(anexoId); appendDebugPublicacao("anexarDocumentoDoCardSeNecessario: hAPI.attachDocument OK documentId=" + anexoId); var attachmentsDepois = hAPI.listAttachments(); appendDebugPublicacao("anexarDocumentoDoCardSeNecessario: total anexos apos attachDocument = " + (attachmentsDepois ? attachmentsDepois.size() : 0)); } catch (e) { appendDebugPublicacao("anexarDocumentoDoCardSeNecessario: ERRO " + e); } } function appendDebugPublicacao(msg) { try { var atual = (hAPI.getCardValue("debug_publicacao") || "") + ""; var prefix = atual ? atual + "\n" : ""; hAPI.setCardValue("debug_publicacao", prefix + msg); } catch (e) { log.warn("appendDebugPublicacao falhou: " + e); } } 1 publicarDocumento FlowEssentials_LancamentodeDocumento 1 function publicarDocumento() { var radioGed = (hAPI.getCardValue("radio_ged") || "") + ""; var idPastaSelecionadaRaw = (hAPI.getCardValue("idPastaSelecionada") || "") + ""; log.info("PUBLICAR_DOCUMENTO LANCAMENTO_DOCUMENTO ########## radio_ged=" + radioGed + " idPastaSelecionada=" + idPastaSelecionadaRaw); appendDebugPublicacao("publicarDocumento: radio_ged=" + radioGed + " idPastaSelecionada=" + idPastaSelecionadaRaw); if (radioGed != "publica_ged") { log.info("PUBLICAR_DOCUMENTO LANCAMENTO_DOCUMENTO ########## Nao publicar no ECM (radio_ged diferente de publica_ged)."); appendDebugPublicacao("publicarDocumento: radio_ged diferente de publica_ged."); return; } var attachments = hAPI.listAttachments(); log.info("PUBLICAR_DOCUMENTO LANCAMENTO_DOCUMENTO ########## attachments.size=" + (attachments ? attachments.size() : 0)); appendDebugPublicacao("publicarDocumento: attachments.size=" + (attachments ? attachments.size() : 0)); for (var i = 0; i < attachments.size(); i++) { var docDto = attachments.get(i); log.info("PUBLICAR_DOCUMENTO LANCAMENTO_DOCUMENTO ########## analisando anexo documentId=" + docDto.getDocumentId() + " version=" + docDto.getVersion() + " documentType=" + docDto.getDocumentType()); appendDebugPublicacao("publicarDocumento: analisando documentId=" + docDto.getDocumentId() + " version=" + docDto.getVersion() + " type=" + docDto.getDocumentType()); try { docAPI.copyDocumentToUploadArea(docDto.getDocumentId(), docDto.getVersion()); docDto.setDocumentId(0); var idPastaSelecionada = parseInt((hAPI.getCardValue("idPastaSelecionada") || "") + "", 10); if (isNaN(idPastaSelecionada) || idPastaSelecionada <= 0) { log.warn("PUBLICAR_DOCUMENTO LANCAMENTO_DOCUMENTO ########## idPastaSelecionada invalido. Documento nao sera publicado no ECM."); appendDebugPublicacao("publicarDocumento: idPastaSelecionada invalido."); continue; } docDto.setParentDocumentId(idPastaSelecionada); var attachArray = new java.util.ArrayList(); var mainAttach = docAPI.newAttachment(); mainAttach.setFileName(docDto.getPhisicalFile()); mainAttach.setPrincipal(true); mainAttach.setAttach(false); attachArray.add(mainAttach); docDto.setActiveVersion(true); docDto.setColleagueId(getValue("WKUser")); docDto.setPublisherId(getValue("WKUser")); if (hAPI.getCardValue("radio_data_validade") == "com_data_validade") { docDto.setExpires(true); if (hAPI.getCardValue("inicio_validade") != "") { var inicioValidade = (hAPI.getCardValue("inicio_validade") || "") + ""; var dataInicioSplit = inicioValidade.split("-"); var anoInicio = dataInicioSplit[0]; var mesInicio = parseFloat(dataInicioSplit[1]) - 1; var diaInicio = dataInicioSplit[2]; var dInicio = new Date(anoInicio, mesInicio, diaInicio); docDto.setValidationStartDate(dInicio); } if (hAPI.getCardValue("expiracao_validade") != "") { var dataVencimento = (hAPI.getCardValue("expiracao_validade") || "") + ""; var dataSplit = dataVencimento.split("-"); var ano = dataSplit[0]; var mes = parseFloat(dataSplit[1]) - 1; var dia = dataSplit[2]; var d = new Date(ano, mes, dia); docDto.setExpirationDate(d); } if (hAPI.getCardValue("dias_antes_expiracao") != "") { var diasAntesExpiracao = parseInt(hAPI.getCardValue("dias_antes_expiracao"), 10); docDto.setNotificationDays(diasAntesExpiracao); } } else { docDto.setExpires(false); } log.info("PUBLICAR_DOCUMENTO LANCAMENTO_DOCUMENTO ########## before create document"); var doc = docAPI.createDocument(docDto, attachArray, null, null, null); log.info("PUBLICAR_DOCUMENTO LANCAMENTO_DOCUMENTO ########## DOCUMENTO CRIADO COM O ID: " + doc.getDocumentId()); appendDebugPublicacao("publicarDocumento: documento publicado no ECM com id=" + doc.getDocumentId()); } catch (e) { log.error("PUBLICAR_DOCUMENTO LANCAMENTO_DOCUMENTO ########## falha ao publicar anexo documentId=" + docDto.getDocumentId() + " erro=" + e); appendDebugPublicacao("publicarDocumento: ERRO ao publicar documentId=" + docDto.getDocumentId() + " erro=" + e); } } } function appendDebugPublicacao(msg) { try { var atual = (hAPI.getCardValue("debug_publicacao") || "") + ""; var prefix = atual ? atual + "\n" : ""; hAPI.setCardValue("debug_publicacao", prefix + msg); } catch (e) { log.warn("appendDebugPublicacao falhou: " + e); } } FFFFFF 611 1401 10 10 Lançamento de documento 1 0 1 FlowEssentials_LancamentodeDocumento 1 1 DBE8FF 203 1371 40 10 Solicitante 2 1 1 FlowEssentials_LancamentodeDocumento 1 2 E2E2E2 203 1371 40 213 Responsável por documentos 2 1 1 FlowEssentials_LancamentodeDocumento 1 3 82b0b7 205 1371 40 416 SwimLane 2 1 1 FlowEssentials_LancamentodeDocumento 1 6 1 FlowEssentials_LancamentodeDocumento 13 6 1 137 279 1 FlowEssentials_LancamentodeDocumento 13 16 1 332 520 1 FlowEssentials_LancamentodeDocumento 1 15 0 Integração executada com sucesso FlowEssentials_LancamentodeDocumento.servicetask15 1 0 0 0 FlowEssentials_LancamentodeDocumento 13 5 approval title 0 0 FlowEssentials_LancamentodeDocumento 13 5 approval description 0 0 FlowEssentials_LancamentodeDocumento 13 5 approval highlight 0 0 FlowEssentials_LancamentodeDocumento 13 5 approval approve 0 0 FlowEssentials_LancamentodeDocumento 13 5 approval reject 0 0 FlowEssentials_LancamentodeDocumento 13 22 approval title 0 0 FlowEssentials_LancamentodeDocumento 13 22 approval description 0 0 FlowEssentials_LancamentodeDocumento 13 22 approval highlight 0 0 FlowEssentials_LancamentodeDocumento 13 22 approval approve 0 0 FlowEssentials_LancamentodeDocumento 13 22 approval reject