3131
3232using OpenAC . Net . Core . Extensions ;
3333using OpenAC . Net . DFe . Core ;
34+ using OpenAC . Net . DFe . Core . Document ;
3435using OpenAC . Net . DFe . Core . Serializer ;
3536using OpenAC . Net . NFSe . Commom ;
3637using OpenAC . Net . NFSe . Commom . Interface ;
3738using OpenAC . Net . NFSe . Commom . Model ;
3839using OpenAC . Net . NFSe . Commom . Types ;
3940using OpenAC . Net . NFSe . Configuracao ;
4041using OpenAC . Net . NFSe . Nota ;
42+ using System ;
43+ using System . Collections . Generic ;
44+ using System . Linq ;
4145using System . Xml . Linq ;
4246
4347namespace OpenAC . Net . NFSe . Providers ;
@@ -55,55 +59,201 @@ public ProviderDSF203(ConfigNFSe config, OpenMunicipioNFSe municipio) : base(con
5559
5660 #region Methods
5761
62+ protected override string GetNamespace ( )
63+ {
64+ return "" ;
65+ }
66+
5867 protected override IServiceClient GetClient ( TipoUrl tipo )
5968 {
6069 return new DSF203ServiceClient ( this , tipo , null ) ;
6170 }
6271
63- protected override void AssinarEnviar ( RetornoEnviar retornoWebservice )
72+ protected override void TratarRetornoEnviar ( RetornoEnviar retornoWebservice , NotaServicoCollection notas )
6473 {
65- //retornoWebservice.XmlEnvio = XmlSigning.AssinarXmlTodos(retornoWebservice.XmlEnvio, "Rps", "InfDeclaracaoPrestacaoServico", Certificado);
66- retornoWebservice . XmlEnvio = XmlSigning . AssinarXml ( retornoWebservice . XmlEnvio , "EnviarLoteRpsEnvio" , "LoteRps" , Certificado ) ;
74+ var xmlRet = XDocument . Parse ( retornoWebservice . XmlRetorno ) ;
75+ MensagemErro ( retornoWebservice , xmlRet , "EnviarLoteRpsResposta" ) ;
76+ if ( retornoWebservice . Erros . Any ( ) ) return ;
77+
78+ var rootElement = xmlRet . Root . ElementAnyNs ( "EnviarLoteRpsResposta" ) ;
79+
80+ retornoWebservice . Data = rootElement . ElementAnyNs ( "DataRecebimento" ) ? . GetValue < DateTime > ( ) ?? DateTime . MinValue ;
81+ retornoWebservice . Protocolo = rootElement . ElementAnyNs ( "Protocolo" ) ? . GetValue < string > ( ) ?? string . Empty ;
82+ retornoWebservice . Sucesso = ! retornoWebservice . Protocolo . IsEmpty ( ) ;
83+
84+ if ( ! retornoWebservice . Sucesso ) return ;
85+
86+ foreach ( NotaServico nota in notas )
87+ {
88+ nota . NumeroLote = retornoWebservice . Lote ;
89+ }
6790 }
6891
69- /// <inheritdoc />
70- protected override void AssinarEnviarSincrono ( RetornoEnviar retornoWebservice )
92+ protected override void TratarRetornoConsultarLoteRps ( RetornoConsultarLoteRps retornoWebservice , NotaServicoCollection notas )
7193 {
72- //retornoWebservice.XmlEnvio = XmlSigning.AssinarXmlTodos(retornoWebservice.XmlEnvio, "Rps", "InfDeclaracaoPrestacaoServico", Certificado);
73- retornoWebservice . XmlEnvio = XmlSigning . AssinarXml ( retornoWebservice . XmlEnvio , "EnviarLoteRpsSincronoEnvio" , "LoteRps" , Certificado ) ;
94+ var xmlRet = XDocument . Parse ( retornoWebservice . XmlRetorno ) ;
95+ MensagemErro ( retornoWebservice , xmlRet , "ConsultarLoteRpsResposta" ) ;
96+ if ( retornoWebservice . Erros . Any ( ) ) return ;
97+
98+ var retornoLote = xmlRet . Root . ElementAnyNs ( "ConsultarLoteRpsResposta" ) ;
99+ var listaNfse = retornoLote ? . ElementAnyNs ( "ListaNfse" ) ;
100+ if ( listaNfse == null )
101+ {
102+ retornoWebservice . Erros . Add ( new EventoRetorno { Codigo = "0" , Descricao = "Lista de NFSe não encontrada! (ListaNfse)" } ) ;
103+ return ;
104+ }
105+
106+ retornoWebservice . Sucesso = true ;
107+ retornoWebservice . Situacao = retornoLote . ElementAnyNs ( "Situacao" ) ? . GetValue < string > ( ) ;
108+
109+ var notasServico = new List < NotaServico > ( ) ;
110+
111+ foreach ( var compNfse in listaNfse . ElementsAnyNs ( "CompNfse" ) )
112+ {
113+ var nfse = compNfse . ElementAnyNs ( "Nfse" ) . ElementAnyNs ( "InfNfse" ) ;
114+ var numeroNFSe = nfse . ElementAnyNs ( "Numero" ) ? . GetValue < string > ( ) ?? string . Empty ;
115+ var chaveNFSe = nfse . ElementAnyNs ( "CodigoVerificacao" ) ? . GetValue < string > ( ) ?? string . Empty ;
116+ var dataNFSe = nfse . ElementAnyNs ( "DataEmissao" ) ? . GetValue < DateTime > ( ) ?? DateTime . Now ;
117+ var numeroRps = nfse ? . ElementAnyNs ( "DeclaracaoPrestacaoServico" ) ?
118+ . ElementAnyNs ( "InfDeclaracaoPrestacaoServico" ) ?
119+ . ElementAnyNs ( "Rps" ) ?
120+ . ElementAnyNs ( "IdentificacaoRps" ) ?
121+ . ElementAnyNs ( "Numero" ) ?
122+ . GetValue < string > ( ) ?? string . Empty ;
123+
124+ GravarNFSeEmDisco ( compNfse . ToString ( ) , $ "NFSe-{ numeroNFSe } -{ chaveNFSe } -.xml", dataNFSe ) ;
125+
126+ var nota = notas . FirstOrDefault ( x => x . IdentificacaoRps . Numero == numeroRps ) ;
127+ if ( nota == null )
128+ {
129+ nota = LoadXml ( compNfse . ToString ( ) ) ;
130+ }
131+ else
132+ {
133+ nota . IdentificacaoNFSe . Numero = numeroNFSe ;
134+ nota . IdentificacaoNFSe . Chave = chaveNFSe ;
135+ nota . XmlOriginal = compNfse . AsString ( ) ;
136+ }
137+
138+ notas . Add ( nota ) ;
139+ notasServico . Add ( nota ) ;
140+ }
141+
142+ retornoWebservice . Notas = notasServico . ToArray ( ) ;
74143 }
75144
76- //protected override XElement WriteValoresRps(NotaServico nota)
77- //{
78- // var valores = new XElement("Valores");
79-
80- // valores.AddChild(AddTag(TipoCampo.De2, "", "ValorServicos", 1, 15, Ocorrencia.Obrigatoria, nota.Servico.Valores.ValorServicos));
81- // valores.AddChild(AddTag(TipoCampo.De2, "", "ValorDeducoes", 1, 15, Ocorrencia.MaiorQueZero, nota.Servico.Valores.ValorDeducoes));
82- // valores.AddChild(AddTag(TipoCampo.De2, "", "ValorPis", 1, 15, Ocorrencia.Obrigatoria, nota.Servico.Valores.ValorPis));
83- // valores.AddChild(AddTag(TipoCampo.De2, "", "ValorCofins", 1, 15, Ocorrencia.Obrigatoria, nota.Servico.Valores.ValorCofins));
84- // valores.AddChild(AddTag(TipoCampo.De2, "", "ValorInss", 1, 15, Ocorrencia.Obrigatoria, nota.Servico.Valores.ValorInss));
85- // valores.AddChild(AddTag(TipoCampo.De2, "", "ValorIr", 1, 15, Ocorrencia.Obrigatoria, nota.Servico.Valores.ValorIr));
86- // valores.AddChild(AddTag(TipoCampo.De2, "", "ValorCsll", 1, 15, Ocorrencia.Obrigatoria, nota.Servico.Valores.ValorCsll));
87- // valores.AddChild(AddTag(TipoCampo.De2, "", "OutrasRetencoes", 1, 15, Ocorrencia.MaiorQueZero, nota.Servico.Valores.OutrasRetencoes));
88- // valores.AddChild(AddTag(TipoCampo.De2, "", "ValTotTributos", 1, 15, Ocorrencia.MaiorQueZero, nota.Servico.Valores.ValTotTributos));
89- // valores.AddChild(AddTag(TipoCampo.De2, "", "ValorIss", 1, 15, Ocorrencia.Obrigatoria, nota.Servico.Valores.ValorIss));
90-
91- // if (nota.RegimeEspecialTributacao == RegimeEspecialTributacao.SimplesNacional)
92- // valores.AddChild(AddTag(TipoCampo.De2, "", "Aliquota", 1, 5, Ocorrencia.MaiorQueZero, nota.Servico.Valores.Aliquota));
93-
94- // valores.AddChild(AddTag(TipoCampo.De2, "", "DescontoIncondicionado", 1, 15, Ocorrencia.Obrigatoria, nota.Servico.Valores.DescontoIncondicionado));
95- // valores.AddChild(AddTag(TipoCampo.De2, "", "DescontoCondicionado", 1, 15, Ocorrencia.Obrigatoria, nota.Servico.Valores.DescontoCondicionado));
96-
97- // return valores;
98- //}
99-
100- //protected override void ValidarSchema(RetornoWebservice retorno, string schema)
101- //{
102- // base.ValidarSchema(retorno, schema);
103- // if(retorno.Erros.Count > 0) return;
104-
105- // retorno.XmlEnvio = retorno.XmlEnvio.Replace(" xmlns=\"http://www.abrasf.org.br/nfse.xsd\"", "");
106- //}
145+ protected override void TratarRetornoConsultarNFSeRps ( RetornoConsultarNFSeRps retornoWebservice , NotaServicoCollection notas )
146+ {
147+ var xmlRet = XDocument . Parse ( retornoWebservice . XmlRetorno ) ;
148+ MensagemErro ( retornoWebservice , xmlRet , "ConsultarNfseRpsResposta" ) ;
149+ if ( retornoWebservice . Erros . Any ( ) ) return ;
150+
151+ var compNfse = xmlRet . Root . ElementAnyNs ( "ConsultarNfseRpsResposta" ) ? . ElementAnyNs ( "CompNfse" ) ;
152+
153+ if ( compNfse == null )
154+ {
155+ retornoWebservice . Erros . Add ( new EventoRetorno { Codigo = "0" , Descricao = "Nota Fiscal não encontrada! (CompNfse)" } ) ;
156+ return ;
157+ }
158+
159+ var nfse = compNfse . ElementAnyNs ( "Nfse" ) . ElementAnyNs ( "InfNfse" ) ;
160+ var numeroNFSe = nfse . ElementAnyNs ( "Numero" ) ? . GetValue < string > ( ) ?? string . Empty ;
161+ var chaveNFSe = nfse . ElementAnyNs ( "CodigoVerificacao" ) ? . GetValue < string > ( ) ?? string . Empty ;
162+ var dataNFSe = nfse . ElementAnyNs ( "DataEmissao" ) ? . GetValue < DateTime > ( ) ?? DateTime . Now ;
163+ var numeroRps = nfse . ElementAnyNs ( "DeclaracaoPrestacaoServico" ) ?
164+ . ElementAnyNs ( "InfDeclaracaoPrestacaoServico" ) ?
165+ . ElementAnyNs ( "Rps" ) ?
166+ . ElementAnyNs ( "IdentificacaoRps" ) ?
167+ . ElementAnyNs ( "Numero" ) . GetValue < string > ( ) ?? string . Empty ;
168+
169+ GravarNFSeEmDisco ( compNfse . AsString ( true ) , $ "NFSe-{ numeroNFSe } -{ chaveNFSe } -.xml", dataNFSe ) ;
170+
171+ var nota = notas . FirstOrDefault ( x => x . IdentificacaoRps . Numero == numeroRps ) ;
172+
173+ if ( nota == null )
174+ {
175+ nota = notas . Load ( compNfse . ToString ( ) ) ;
176+ }
177+ else
178+ {
179+ nota . IdentificacaoNFSe . Numero = numeroNFSe ;
180+ nota . IdentificacaoNFSe . Chave = chaveNFSe ;
181+ nota . IdentificacaoNFSe . DataEmissao = dataNFSe ;
182+ nota . XmlOriginal = compNfse . ToString ( ) ;
183+
184+ var nfseCancelamento = compNfse . ElementAnyNs ( "NfseCancelamento" ) ;
185+
186+ if ( nfseCancelamento != null )
187+ {
188+ nota . Situacao = SituacaoNFSeRps . Cancelado ;
189+
190+ var confirmacaoCancelamento = nfseCancelamento
191+ . ElementAnyNs ( "Confirmacao" ) ;
192+
193+ if ( confirmacaoCancelamento != null )
194+ {
195+ var pedido = confirmacaoCancelamento . ElementAnyNs ( "Pedido" ) ;
196+
197+ if ( pedido != null )
198+ {
199+ var codigoCancelamento = pedido
200+ . ElementAnyNs ( "InfPedidoCancelamento" )
201+ . ElementAnyNs ( "CodigoCancelamento" )
202+ . GetValue < string > ( ) ;
203+
204+ nota . Cancelamento . Pedido . CodigoCancelamento = codigoCancelamento ;
205+ }
206+ }
207+
208+ nota . Cancelamento . DataHora = confirmacaoCancelamento
209+ . ElementAnyNs ( "DataHora" )
210+ . GetValue < DateTime > ( ) ;
211+ }
212+ }
213+
214+ retornoWebservice . Nota = nota ;
215+ retornoWebservice . Sucesso = true ;
216+ }
217+
218+ protected override void TratarRetornoCancelarNFSe ( RetornoCancelar retornoWebservice , NotaServicoCollection notas )
219+ {
220+ var xmlRet = XDocument . Parse ( retornoWebservice . XmlRetorno ) ;
221+ MensagemErro ( retornoWebservice , xmlRet , "CancelarNfseResposta" ) ;
222+ if ( retornoWebservice . Erros . Any ( ) ) return ;
223+
224+ var confirmacaoCancelamento = xmlRet . Root . ElementAnyNs ( "CancelarNfseResposta" ) ? . ElementAnyNs ( "RetCancelamento" ) ? . ElementAnyNs ( "NfseCancelamento" ) ? . ElementAnyNs ( "Confirmacao" ) ;
225+ if ( confirmacaoCancelamento == null )
226+ {
227+ retornoWebservice . Erros . Add ( new EventoRetorno { Codigo = "0" , Descricao = "Confirmação do cancelamento não encontrada!" } ) ;
228+ return ;
229+ }
230+
231+ var nota = notas . FirstOrDefault ( x => x . IdentificacaoNFSe . Numero . Trim ( ) == retornoWebservice . NumeroNFSe ) ;
232+ if ( nota == null ) return ;
233+
234+ retornoWebservice . Data = confirmacaoCancelamento . ElementAnyNs ( "DataHora" ) ? . GetValue < DateTime > ( ) ?? DateTime . MinValue ;
235+ retornoWebservice . Sucesso = retornoWebservice . Data != DateTime . MinValue ;
236+
237+ nota . Situacao = SituacaoNFSeRps . Cancelado ;
238+ nota . Cancelamento . Pedido . CodigoCancelamento = retornoWebservice . CodigoCancelamento ;
239+ nota . Cancelamento . DataHora = retornoWebservice . Data ;
240+ nota . Cancelamento . MotivoCancelamento = retornoWebservice . Motivo ;
241+ }
242+
243+ protected override void AssinarConsultarLoteRps ( RetornoConsultarLoteRps retornoWebservice )
244+ {
245+ retornoWebservice . XmlEnvio = XmlSigning . AssinarXml ( retornoWebservice . XmlEnvio , "ConsultarLoteRpsEnvio" , "" , Certificado ) ;
246+ }
247+
248+ protected override void AssinarConsultarNFSeRps ( RetornoConsultarNFSeRps retornoWebservice )
249+ {
250+ retornoWebservice . XmlEnvio = XmlSigning . AssinarXml ( retornoWebservice . XmlEnvio , "ConsultarNfseRpsEnvio" , "" , Certificado ) ;
251+ }
252+
253+ protected override void AssinarCancelarNFSe ( RetornoCancelar retornoWebservice )
254+ {
255+ retornoWebservice . XmlEnvio = XmlSigning . AssinarXml ( retornoWebservice . XmlEnvio , "Pedido" , "InfPedidoCancelamento" , Certificado ) ;
256+ }
107257
108258 #endregion Methods
109259}
0 commit comments