Faturar Pedido Por Evento

Boa tarde. Eu estou com um projeto que irá funcionar da seguinte maneira. Será criado um evento onde esse evento irá faturar um pedido. Antes de fazer qualquer consulta ou elaborar a lógica para faturar o pedido em específicos, estou tentanto faturar um pedido teste por evento. Com isso eu captura o campo adicional que eu criei e chamo o evento, o problema estar que ele estar acusando o erro de “Já existe uma transação em andamento.” eu dei uma olhada e poderia ser resolvido se fosse uma ação agendada e removesse a flag de Transação automática, mas por ser evento eu usei: hnd.execWithTX(new JapeSession.TXBlock() , mas mesmo assim o erro continua. Alguém poderia dar uma luz do que posso fazer ?

public void afterUpdate(PersistenceEvent event) throws Exception {
	    // TODO Auto-generated method stub
	    DynamicVO cab = (DynamicVO) event.getVo();
	    BigDecimal nunota = cab.asBigDecimal("NUNOTA");
	    SessionHandle hnd = null;                                                                                                                                                          
	    if ((cab.asString("TIPMOV").equals("V") && cab.asString("STATUSNOTA").equals("A")) ||
	        (cab.asString("TIPMOV").equals("V") && cab.asString("STATUSNOTA").equals("L"))
	    ) {
	        if (cab.asString("AD_TESTECONFIRMA") != null) {
	              faturarNotaFiscal(nunota, BigDecimal.valueOf(9167), "1");}
	            } }
	public void faturarNotaFiscal(final BigDecimal nunota, final BigDecimal codTipOperacao, final String serie) throws Exception {
	    final ServiceContext ctx = new ServiceContext(null);
	    ctx.setAutentication(AuthenticationInfo.getCurrent());
	    ctx.makeCurrent();
	    SPBeanUtils.setupContext(ctx);
	    SessionHandle hnd = null;

	    try {
	        hnd = JapeSession.open();

	                hnd.execWithTX(new JapeSession.TXBlock() {
	                public void doWithTx() throws Exception {
	                SessionHandle hnd = null;
	                hnd = JapeSession.open();
	                
	                hnd.setCanTimeout(true);
	                hnd.setPriorityLevel(JapeSession.LOW_PRIORITY);
	                CentralFaturamento.ConfiguracaoFaturamento cfg = getConfiguracaoFaturamento(codTipOperacao, serie);
	                Collection<BigDecimal> notasSelecao = new ArrayList<>();
	                notasSelecao.add(nunota);
	                Map<BigDecimal, BigDecimal> notaMoedaMap = new HashMap<>();
	                FaturamentoHelper.faturar(ctx, hnd, cfg, notasSelecao, notaMoedaMap);
	            }
	        });

	    } catch (Exception e) {
	        e.printStackTrace();
	        MGEModelException.throwMe(e);
	        System.out.println("Erro ao faturar nota fiscal" + e.getCause() + e.getMessage());
	    } finally {
	        if (hnd != null) {
	            JapeSession.close(hnd);
	        }
	    }
	}

	public static CentralFaturamento.ConfiguracaoFaturamento getConfiguracaoFaturamento(BigDecimal codTipOperacao, String serie) {
	    CentralFaturamento.ConfiguracaoFaturamento cfg = new CentralFaturamento.ConfiguracaoFaturamento();

	    cfg.setCodTipOper(codTipOperacao);
	    cfg.setSerie(serie);
	    cfg.setDtFaturamento(TimeUtils.getNow());
	    cfg.setDtEntSai(TimeUtils.getNow());
	    cfg.setValidarData(true);
	    cfg.setFaturamentoNormal(true);
	    cfg.setUmaNota(false);
	    cfg.setEhWizardFaturamento(true);
	    cfg.setNfeDevolucaoViaRecusa(false);
	    cfg.setConfirmarNota(true);
	    return cfg;
	}`

8 respostas