Subversion Repositories Integrator Subversion

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
695 blopes 1
package nfe.util;
2
 
3
import br.com.swconsultoria.nfe.Assinar;
4
import br.com.swconsultoria.nfe.dom.ConfiguracoesNfe;
5
import br.com.swconsultoria.nfe.dom.Evento;
6
import br.com.swconsultoria.nfe.dom.enuns.AssinaturaEnum;
7
import br.com.swconsultoria.nfe.dom.enuns.EventosEnum;
8
import br.com.swconsultoria.nfe.exception.NfeException;
9
import br.com.swconsultoria.nfe.schema.envEpec.*;
10
 
11
import javax.xml.bind.JAXBException;
12
import java.util.Collections;
13
import java.util.List;
14
 
15
/**
16
 * @author Samuel Oliveira - samuk.exe@hotmail.com
17
 * Data: 02/03/2019 - 22:51
18
 */
19
public class EpecUtil {
20
 
21
    private EpecUtil() {}
22
 
23
    /**
24
     * MOnta o Evento de epec Lote
25
     *
26
     * @param epec
27
     * @param configuracao
28
     * @return
29
     * @throws NfeException
30
     */
31
    public static TEnvEvento montaEpec(Evento epec, ConfiguracoesNfe configuracao) throws NfeException {
32
        return montaEpec(Collections.singletonList(epec), configuracao);
33
    }
34
 
35
    /**
36
     * MOnta o Evento de epec Lote
37
     *
38
     * @param listaEpec
39
     * @param configuracao
40
     * @return
41
     * @throws NfeException
42
     */
43
    public static TEnvEvento montaEpec(List<Evento> listaEpec, ConfiguracoesNfe configuracao) throws NfeException {
44
 
45
        if (listaEpec.size() > 20) {
46
            throw new NfeException("Podem ser enviados no máximo 20 eventos no Lote.");
47
        }
48
 
49
        TEnvEvento enviEvento = new TEnvEvento();
50
        enviEvento.setVersao(ConstantesUtil.VERSAO.EVENTO_EPEC);
51
        enviEvento.setIdLote("1");
52
 
53
        listaEpec.forEach(epec -> {
54
 
55
            String id = "ID" + EventosEnum.EPEC.getCodigo() + epec.getChave() + "01";
56
 
57
            TEvento eventoEpec = new TEvento();
58
            eventoEpec.setVersao(ConstantesUtil.VERSAO.EVENTO_EPEC);
59
 
60
            TEvento.InfEvento infoEvento = new TEvento.InfEvento();
61
            infoEvento.setId(id);
62
            infoEvento.setCOrgao("91");
63
            infoEvento.setTpAmb(configuracao.getAmbiente().getCodigo());
64
 
65
            infoEvento.setCPF(epec.getCpf());
66
            infoEvento.setCNPJ(epec.getCnpj());
67
 
68
            infoEvento.setChNFe(epec.getChave());
69
            infoEvento.setDhEvento(XmlNfeUtil.dataNfe(epec.getDataEvento(), configuracao.getZoneId()));
70
            infoEvento.setTpEvento(EventosEnum.EPEC.getCodigo());
71
            infoEvento.setNSeqEvento("1");
72
            infoEvento.setVerEvento(ConstantesUtil.VERSAO.EVENTO_EPEC);
73
 
74
            TEvento.InfEvento.DetEvento detEvento = new TEvento.InfEvento.DetEvento();
75
            detEvento.setVersao(ConstantesUtil.VERSAO.EVENTO_EPEC);
76
            detEvento.setDescEvento("EPEC");
77
            detEvento.setCOrgaoAutor(configuracao.getEstado().getCodigoUF());
78
            detEvento.setTpAutor("1");
79
            detEvento.setVerAplic("1.0.0");
80
            detEvento.setDhEmi(XmlNfeUtil.dataNfe(epec.getDataEvento(), configuracao.getZoneId()));
81
            detEvento.setTpNF(epec.getEventoEpec().getTipoNF());
82
            detEvento.setIE(epec.getEventoEpec().getIeEmitente());
83
 
84
            TEvento.InfEvento.DetEvento.Dest dest = new TEvento.InfEvento.DetEvento.Dest();
85
            dest.setUF(TUf.valueOf(epec.getEventoEpec().getEstadoDestinatario().toString()));
86
            dest.setCNPJ(epec.getEventoEpec().getCnpjDestinatario());
87
            dest.setCPF(epec.getEventoEpec().getCpfDestinatario());
88
            dest.setIE(epec.getEventoEpec().getIeDestinatario());
89
            dest.setVNF(epec.getEventoEpec().getvNF());
90
            dest.setVICMS(epec.getEventoEpec().getvICMS());
91
            dest.setVST(epec.getEventoEpec().getvST());
92
            detEvento.setDest(dest);
93
 
94
            infoEvento.setDetEvento(detEvento);
95
            eventoEpec.setInfEvento(infoEvento);
96
 
97
            enviEvento.getEvento().add(eventoEpec);
98
 
99
        });
100
 
101
        return enviEvento;
102
    }
103
 
104
    /**
105
     * Cria o ProcEvento de CCe
106
     *
107
     * @param config
108
     * @param enviEvento
109
     * @param retorno
110
     * @return
111
     * @throws JAXBException
112
     * @throws NfeException
113
     */
114
    public static String criaProcEventoEpec(ConfiguracoesNfe config, TEnvEvento enviEvento, TRetEnvEvento retorno) throws JAXBException, NfeException {
115
 
116
        String xml = XmlNfeUtil.objectToXml(enviEvento, config.getEncode());
117
        xml = xml.replaceAll(" xmlns:ns2=\"http://www.w3.org/2000/09/xmldsig#\"", "");
118
        xml = xml.replace("<evento v", "<evento xmlns=\"http://www.portalfiscal.inf.br/nfe\" v");
119
 
120
        String assinado = Assinar.assinaNfe(ConfiguracoesUtil.iniciaConfiguracoes(config), xml, AssinaturaEnum.EVENTO);
121
 
122
        TProcEvento procEvento = new TProcEvento();
123
        procEvento.setEvento(XmlNfeUtil.xmlToObject(assinado, TEnvEvento.class).getEvento().get(0));
124
        procEvento.setRetEvento(retorno.getRetEvento().get(0));
125
        procEvento.setVersao(ConstantesUtil.VERSAO.EVENTO_EPEC);
126
 
127
        return XmlNfeUtil.objectToXml(procEvento, config.getEncode());
128
    }
129
 
130
}