Skip to content

Instantly share code, notes, and snippets.

@ffdesousa
Last active May 10, 2020 16:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ffdesousa/5e5490c4e8ab168f82c0514617675a25 to your computer and use it in GitHub Desktop.
Save ffdesousa/5e5490c4e8ab168f82c0514617675a25 to your computer and use it in GitHub Desktop.
function servicetask37(attempt, message) {
try {
var now = new Date;
var coligada = hAPI.getCardValue('coligada');
var codColigada = hAPI.getCardValue('codColigada');
var referencia = hAPI.getCardValue('referencia');
var dataVencimento = hAPI.getCardValue('dataVencimento');
var fornecedor = hAPI.getCardValue('fornecedorNome');
fornecedor += " - ";
fornecedor += "Ref. " + referencia;
fornecedor += " - ";
fornecedor += "Vencimento. " + dataVencimento;
var parentId = FolderStructure(399,('0' + codColigada).slice(-2) + ' - ' + coligada , fornecedor, now.getFullYear() ,('0' + (now.getMonth()+1)).slice(-2),('0'+ now.getDate()).slice(-2));
hAPI.setCardValue('idFolder',parentId);
movDocGed(parentId);
} catch(error) {
throw error;
}
}
function createFolder(parentId, DocumentDescription ) {
try {
var dto = docAPI.newDocumentDto();
dto.setDocumentDescription(DocumentDescription);
dto.setDocumentType("1");
dto.setParentDocumentId(parentId);
dto.setDocumentTypeId("");
var folder = docAPI.createFolder(dto, null, null);
log.info("PASTA CRIADA COM O ID :" + folder.getDocumentId());
return folder.getDocumentId();
} catch (e) {
log.error("Problemas na criação da pasta:\n" + e);
}
}
function CheckFolderExist (parentId, childrenDescrition) {
var that = this;
var retorno = [];
var c1 = DatasetFactory.createConstraint("parentDocumentId",parentId, parentId, ConstraintType.MUST);
var c2 = DatasetFactory.createConstraint("deleted",false, false, ConstraintType.MUST);
var c3 = DatasetFactory.createConstraint("activeVersion",true, true, ConstraintType.MUST);
var c4 = DatasetFactory.createConstraint("documentDescription",childrenDescrition, childrenDescrition, ConstraintType.MUST);
var sortingFields = new Array("documentDescription");
var constraints = new Array(c1,c2,c3,c4);
var resultado = DatasetFactory.getDataset("document", null, constraints, sortingFields);
if(resultado.rowsCount > 0) {
return resultado.getValue(0, 'documentPK.documentId');
}
return createFolder(parentId,childrenDescrition);
}
function FolderStructure (parentId ,coligada, documentDescription, ano, mes, dia) {
var idFolderColigada = 0;
var idFolderAno = 0;
var idFolderMes = 0;
var idFolderDia = 0;
var idFolderMovimento = 0;
if (idFolderColigada == 0)
{
idFolderColigada = CheckFolderExist(parentId, coligada);
}
if (idFolderColigada > 0)
{
idFolderAno = CheckFolderExist(idFolderColigada, ano);
}
if (idFolderAno > 0)
{
idFolderMes = CheckFolderExist(idFolderAno, mes);
}
if (idFolderMes > 0)
{
idFolderDia = CheckFolderExist(idFolderMes, dia);
}
if (idFolderDia > 0)
{
idFolderMovimento = CheckFolderExist(idFolderDia, documentDescription);
}
return idFolderMovimento;
}
function movDocGed(parentId) {
var attachments = hAPI.listAttachments();
for ( var i = 0; i < attachments.size(); i++) {
var docDto = attachments.get(i);
if (docDto.getDocumentType() == "7") {
docAPI.copyDocumentToUploadArea(docDto.getDocumentId(), docDto.getVersion());
docDto.setDocumentId(0);
// Criar uma pasta para armazenar os anexos como documentos, concedendo à mesma as permissões de segurança e aprovação necessárias
docDto.setParentDocumentId(parentId);
var attachArray = new java.util.ArrayList();
var mainAttach = docAPI.newAttachment();
mainAttach.setFileName(docDto.getPhisicalFile());
mainAttach.setPrincipal(true);
mainAttach.setAttach(false);
attachArray.add(mainAttach);
// Adicionando aprovadores
docDto.setActiveVersion(true);
docDto.setColleagueId(getValue("WKUser")); // Informar o usuário logado.
docDto.setPublisherId(getValue("WKUser")); // Informar o publicador.
try {
var doc = docAPI.createDocument(docDto, attachArray, null, null,null);
log.info("DOCUMENTO CRIADO COM O ID: " + doc.getDocumentId());
} catch (e) {
log.error("Problemas na criação do documento:\n" + e);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment