1
+ <?php
2
+
3
+ /**
4
+ * Este arquivo é parte do programa GNRE PHP
5
+ * GNRE PHP é um software livre; você pode redistribuí-lo e/ou
6
+ * modificá-lo dentro dos termos da Licença Pública Geral GNU como
7
+ * publicada pela Fundação do Software Livre (FSF); na versão 2 da
8
+ * Licença, ou (na sua opinião) qualquer versão.
9
+ * Este programa é distribuído na esperança de que possa ser útil,
10
+ * mas SEM NENHUMA GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer
11
+ * MERCADO ou APLICAÇÃO EM PARTICULAR. Veja a
12
+ * Licença Pública Geral GNU para maiores detalhes.
13
+ * Você deve ter recebido uma cópia da Licença Pública Geral GNU
14
+ * junto com este programa, se não, escreva para a Fundação do Software
15
+ * Livre(FSF) Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16
+ */
17
+
18
+ namespace Sped \Gnre \Sefaz ;
19
+
20
+ use DOMDocument ;
21
+
22
+ class LoteV2 extends Lote {
23
+
24
+ public $ ambienteDeTesteV2 = false ;
25
+
26
+ /**
27
+ * @param bool $ambienteDeTesteV2
28
+ * @return LoteV2
29
+ */
30
+ public function setAmbienteDeTesteV2 (bool $ ambienteDeTesteV2 ): LoteV2
31
+ {
32
+ $ this ->ambienteDeTesteV2 = $ ambienteDeTesteV2 ;
33
+ return $ this ;
34
+ }
35
+
36
+ public function getSoapEnvelop ($ gnre , $ loteGnre )
37
+ {
38
+ $ soapEnv = $ gnre ->createElement ('soap12:Envelope ' );
39
+ $ soapEnv ->setAttribute ('xmlns:xsi ' , 'http://www.w3.org/2001/XMLSchema-instance ' );
40
+ $ soapEnv ->setAttribute ('xmlns:xsd ' , 'http://www.w3.org/2001/XMLSchema ' );
41
+ $ soapEnv ->setAttribute ('xmlns:soap12 ' , 'http://www.w3.org/2003/05/soap-envelope ' );
42
+
43
+ $ gnreCabecalhoSoap = $ gnre ->createElement ('gnreCabecMsg ' );
44
+ $ gnreCabecalhoSoap ->setAttribute ('xmlns ' , 'http://www.gnre.pe.gov.br/wsdl/processar ' );
45
+ $ gnreCabecalhoSoap ->appendChild ($ gnre ->createElement ('versaoDados ' , '2.00 ' ));
46
+
47
+ $ soapHeader = $ gnre ->createElement ('soap12:Header ' );
48
+ $ soapHeader ->appendChild ($ gnreCabecalhoSoap );
49
+
50
+ $ soapEnv ->appendChild ($ soapHeader );
51
+ $ gnre ->appendChild ($ soapEnv );
52
+
53
+ $ action = $ this ->ambienteDeTesteV2 ?
54
+ 'http://www.testegnre.pe.gov.br/webservice/GnreLoteRecepcao ' :
55
+ 'http://www.gnre.pe.gov.br/webservice/GnreLoteRecepcao ' ;
56
+
57
+ $ gnreDadosMsg = $ gnre ->createElement ('gnreDadosMsg ' );
58
+ $ gnreDadosMsg ->setAttribute ('xmlns ' , $ action );
59
+
60
+ $ gnreDadosMsg ->appendChild ($ loteGnre );
61
+
62
+ $ soapBody = $ gnre ->createElement ('soap12:Body ' );
63
+ $ soapBody ->appendChild ($ gnreDadosMsg );
64
+
65
+ $ soapEnv ->appendChild ($ soapBody );
66
+ }
67
+
68
+ public function toXml () {
69
+ $ gnre = new DOMDocument ('1.0 ' , 'UTF-8 ' );
70
+ $ gnre ->formatOutput = false ;
71
+ $ gnre ->preserveWhiteSpace = false ;
72
+
73
+ $ loteGnre = $ gnre ->createElement ('TLote_GNRE ' );
74
+
75
+ $ loteXmlns = $ gnre ->createAttribute ('xmlns ' );
76
+ $ loteXmlns ->value = 'http://www.gnre.pe.gov.br ' ;
77
+
78
+ $ loteVersao = $ gnre ->createAttribute ('versao ' );
79
+ $ loteVersao ->value = '2.00 ' ;
80
+
81
+ $ loteGnre ->appendChild ($ loteVersao );
82
+ $ loteGnre ->appendChild ($ loteXmlns );
83
+
84
+ $ guia = $ gnre ->createElement ('guias ' );
85
+
86
+ foreach ($ this ->getGuias () as $ gnreGuia ) {
87
+ $ estado = $ gnreGuia ->c01_UfFavorecida ;
88
+ $ guiaEstado = $ this ->getEstadoFactory ()->create ($ estado );
89
+ $ dados = $ gnre ->createElement ('TDadosGNRE ' );
90
+ $ dadosVersao = $ gnre ->createAttribute ('versao ' );
91
+ $ dadosVersao ->value = '2.00 ' ;
92
+ $ dados ->appendChild ($ dadosVersao );
93
+
94
+ $ ufFavorecida = $ gnre ->createElement ('ufFavorecida ' , $ estado );
95
+ $ tipoGnre = $ gnre ->createElement ('tipoGnre ' , '0 ' );
96
+ $ valorGNRE = $ gnre ->createElement ('valorGNRE ' , $ gnreGuia ->c10_valorTotal );
97
+ $ dataPagamento = $ gnre ->createElement ('dataPagamento ' , $ gnreGuia ->c33_dataPagamento );
98
+ $ identificadorGuia = $ gnre ->createElement ('identificadorGuia ' , '1 ' );
99
+
100
+ $ contribuinteEmitente = $ gnre ->createElement ('contribuinteEmitente ' );
101
+
102
+ $ identificacao = $ gnre ->createElement ('identificacao ' );
103
+ if ($ gnreGuia ->c27_tipoIdentificacaoEmitente == parent ::EMITENTE_PESSOA_JURIDICA ) {
104
+ $ identificacaoChild = $ gnre ->createElement ('CNPJ ' , $ gnreGuia ->c03_idContribuinteEmitente );
105
+ } else {
106
+ $ identificacaoChild = $ gnre ->createElement ('CPF ' , $ gnreGuia ->c03_idContribuinteEmitente );
107
+ }
108
+ $ identificacao ->appendChild ($ identificacaoChild );
109
+
110
+ $ razaoSocial = $ gnre ->createElement ('razaoSocial ' , $ gnreGuia ->c16_razaoSocialEmitente );
111
+ $ endereco = $ gnre ->createElement ('endereco ' , $ gnreGuia ->c18_enderecoEmitente );
112
+ $ municipio = $ gnre ->createElement ('municipio ' , $ gnreGuia ->c19_municipioEmitente );
113
+ $ uf = $ gnre ->createElement ('uf ' , $ gnreGuia ->c20_ufEnderecoEmitente );
114
+ $ cep = $ gnre ->createElement ('cep ' , $ gnreGuia ->c21_cepEmitente );
115
+ $ telefone = $ gnre ->createElement ('telefone ' , $ gnreGuia ->c22_telefoneEmitente );
116
+
117
+ $ contribuinteEmitente ->appendChild ($ identificacao );
118
+ $ contribuinteEmitente ->appendChild ($ razaoSocial );
119
+ $ contribuinteEmitente ->appendChild ($ endereco );
120
+ $ contribuinteEmitente ->appendChild ($ municipio );
121
+ $ contribuinteEmitente ->appendChild ($ uf );
122
+ $ contribuinteEmitente ->appendChild ($ cep );
123
+ $ contribuinteEmitente ->appendChild ($ telefone );
124
+
125
+ $ itensGNRE = $ gnre ->createElement ('itensGNRE ' );
126
+
127
+ $ item = $ gnre ->createElement ('item ' );
128
+
129
+ $ receita = $ gnre ->createElement ('receita ' , $ gnreGuia ->c02_receita );
130
+ $ documentoOrigem = $ gnre ->createElement ('documentoOrigem ' , $ gnreGuia ->c04_docOrigem );
131
+ $ tipoDoc = $ gnre ->createAttribute ('tipo ' );
132
+ $ tipoDoc ->value = $ gnreGuia ->c28_tipoDocOrigem ;
133
+ $ documentoOrigem ->appendChild ($ tipoDoc );
134
+ $ referencia = $ gnre ->createElement ('referencia ' );
135
+ $ periodo = $ gnre ->createElement ('periodo ' , '0 ' );
136
+ $ referencia ->appendChild ($ periodo );
137
+ if ($ gnreGuia ->mes !=null ) {
138
+ $ mes = $ gnre ->createElement ('mes ' , $ gnreGuia ->mes );
139
+ $ referencia ->appendChild ($ mes );
140
+ }
141
+ if ($ gnreGuia ->ano !=null ) {
142
+ $ ano = $ gnre ->createElement ('ano ' , $ gnreGuia ->ano );
143
+ $ referencia ->appendChild ($ ano );
144
+ }
145
+ if ($ gnreGuia ->parcela !=null ) {
146
+ $ parcela = $ gnre ->createElement ('parcela ' , $ gnreGuia ->parcela );
147
+ $ referencia ->appendChild ($ parcela );
148
+ }
149
+ $ dataVencimento = $ gnre ->createElement ('dataVencimento ' , $ gnreGuia ->c14_dataVencimento );
150
+ $ valor11 = $ gnre ->createElement ('valor ' , $ gnreGuia ->c06_valorPrincipal );
151
+ $ tipo11 = $ gnre ->createAttribute ('tipo ' );
152
+ $ tipo11 ->value = '11 ' ;
153
+ $ valor11 ->appendChild ($ tipo11 );
154
+ $ valor21 = $ gnre ->createElement ('valor ' , $ gnreGuia ->c06_valorPrincipal );
155
+ $ tipo21 = $ gnre ->createAttribute ('tipo ' );
156
+ $ tipo21 ->value = '21 ' ;
157
+ $ valor21 ->appendChild ($ tipo21 );
158
+
159
+ $ contribuinteDestinatario = $ gnre ->createElement ('contribuinteDestinatario ' );
160
+ $ identificacao = $ gnre ->createElement ('identificacao ' );
161
+
162
+ if ($ gnreGuia ->c34_tipoIdentificacaoDestinatario == parent ::DESTINATARIO_PESSOA_JURIDICA ) {
163
+ $ destinatarioContribuinteDocumento = $ gnre ->createElement ('CNPJ ' , $ gnreGuia ->c35_idContribuinteDestinatario );
164
+ } else {
165
+ $ destinatarioContribuinteDocumento = $ gnre ->createElement ('CPF ' , $ gnreGuia ->c35_idContribuinteDestinatario );
166
+ }
167
+ $ identificacao ->appendChild ($ destinatarioContribuinteDocumento );
168
+ if ($ gnreGuia ->c36_inscricaoEstadualDestinatario !='' ) {
169
+ $ IE = $ gnre ->createElement ('IE ' , $ gnreGuia ->c36_inscricaoEstadualDestinatario );
170
+ $ identificacao ->appendChild ($ IE );
171
+ }
172
+ $ razaoSocial = $ gnre ->createElement ('razaoSocial ' , $ gnreGuia ->c37_razaoSocialDestinatario );
173
+ $ municipio = $ gnre ->createElement ('municipio ' , $ gnreGuia ->c38_municipioDestinatario );
174
+ $ contribuinteDestinatario ->appendChild ($ identificacao );
175
+ $ contribuinteDestinatario ->appendChild ($ razaoSocial );
176
+ $ contribuinteDestinatario ->appendChild ($ municipio );
177
+
178
+
179
+
180
+ $ item ->appendChild ($ receita );
181
+ $ item ->appendChild ($ documentoOrigem );
182
+ $ item ->appendChild ($ referencia );
183
+ $ item ->appendChild ($ dataVencimento );
184
+ $ item ->appendChild ($ valor11 );
185
+ $ item ->appendChild ($ valor21 );
186
+ $ item ->appendChild ($ contribuinteDestinatario );
187
+
188
+ $ camposExtras = $ this ->gerarCamposExtras ($ gnre , $ gnreGuia );
189
+ if ($ camposExtras != null ) {
190
+ $ item ->appendChild ($ camposExtras );
191
+ }
192
+
193
+ $ itensGNRE ->appendChild ($ item );
194
+
195
+
196
+ $ dados ->appendChild ($ ufFavorecida );
197
+ $ dados ->appendChild ($ tipoGnre );
198
+ $ dados ->appendChild ($ contribuinteEmitente );
199
+ $ dados ->appendChild ($ itensGNRE );
200
+ $ dados ->appendChild ($ valorGNRE );
201
+ $ dados ->appendChild ($ dataPagamento );
202
+ $ dados ->appendChild ($ identificadorGuia );
203
+
204
+
205
+
206
+
207
+ $ guia ->appendChild ($ dados );
208
+ $ gnre ->appendChild ($ loteGnre );
209
+ $ loteGnre ->appendChild ($ guia );
210
+ }
211
+
212
+ $ this ->getSoapEnvelop ($ gnre , $ loteGnre );
213
+
214
+ return $ gnre ->saveXML ();
215
+ }
216
+
217
+ public function gerarCamposExtras ($ gnre , $ gnreGuia ) {
218
+ if (is_array ($ gnreGuia ->c39_camposExtras ) && count ($ gnreGuia ->c39_camposExtras ) > 0 ) {
219
+ $ c39_camposExtras = $ gnre ->createElement ('camposExtras ' );
220
+ foreach ($ gnreGuia ->c39_camposExtras as $ key => $ campos ) {
221
+ $ campoExtra = $ gnre ->createElement ('campoExtra ' );
222
+ $ codigo = $ gnre ->createElement ('codigo ' , $ campos ['campoExtra ' ]['codigo ' ]);
223
+ $ valor = $ gnre ->createElement ('valor ' , $ campos ['campoExtra ' ]['valor ' ]);
224
+ $ campoExtra ->appendChild ($ codigo );
225
+ $ campoExtra ->appendChild ($ valor );
226
+ $ c39_camposExtras ->appendChild ($ campoExtra );
227
+ }
228
+ return $ c39_camposExtras ;
229
+ }
230
+ }
231
+
232
+ public function getCodigoDoc ($ uf , $ difa = false ) {
233
+ $ doc = '10 ' ;
234
+ switch ($ uf ) {
235
+ case 'AC ' : $ doc = '10 ' ; break ;//acre
236
+ case 'AL ' : $ doc = '10 ' ; break ;//alagoas
237
+ case 'AP ' : $ doc = '10 ' ; break ;//amapa
238
+ case 'AM ' : $ doc = '22 ' ; break ;//amazon
239
+ case 'BA ' : $ doc = '10 ' ; break ;
240
+ case 'CE ' : $ doc = '10 ' ; break ;
241
+ case 'DF ' : $ doc = '10 ' ; break ;
242
+ case 'ES ' : $ doc = '10 ' ; break ;
243
+ case 'GO ' : $ doc = '10 ' ; break ;
244
+ case 'MA ' : $ doc = '10 ' ; break ;//maranhao
245
+ case 'MT ' : $ doc = '10 ' ; break ;//mato grosso
246
+ case 'MS ' : $ doc = '10 ' ; break ;//matro grosso sol
247
+ case 'MG ' : $ doc = '10 ' ; break ;
248
+ case 'PA ' : $ doc = '10 ' ; break ;//para
249
+ case 'PB ' : $ doc = '10 ' ; break ;//paraiba
250
+ case 'PR ' : $ doc = '10 ' ; break ;
251
+ case 'PE ' : $ doc = $ difa ?'24 ' :'22 ' ; break ;//pernan
252
+ case 'PI ' : $ doc = '10 ' ; break ;//piaiu
253
+ case 'RJ ' : $ doc = '24 ' ; break ;
254
+ case 'RN ' : $ doc = '10 ' ; break ;
255
+ case 'RS ' : $ doc = '22 ' ; break ;
256
+ case 'RO ' : $ doc = '10 ' ; break ;//Rondonia
257
+ case 'RR ' : $ doc = '10 ' ; break ;//Roraima
258
+ case 'SC ' : $ doc = '24 ' ; break ;
259
+ case 'SP ' : $ doc = '10 ' ; break ;
260
+ case 'SE ' : $ doc = '10 ' ; break ;
261
+ case 'TO ' : $ doc = '10 ' ; break ;
262
+ }
263
+ return $ doc ;
264
+ }
265
+
266
+ public function getNumDoc ($ uf ) {
267
+ $ doc = 'numero ' ;
268
+ switch ($ uf ) {
269
+ case 'AC ' : $ doc = 'numero ' ; break ;//acre
270
+ case 'AL ' : $ doc = 'numero ' ; break ;//alagoas
271
+ case 'AP ' : $ doc = 'numero ' ; break ;//amapa
272
+ case 'AM ' : $ doc = 'chave ' ; break ;//amazon
273
+ case 'BA ' : $ doc = 'numero ' ; break ;
274
+ case 'CE ' : $ doc = 'numero ' ; break ;
275
+ case 'DF ' : $ doc = 'numero ' ; break ;
276
+ case 'ES ' : $ doc = 'numero ' ; break ;
277
+ case 'GO ' : $ doc = 'numero ' ; break ;
278
+ case 'MA ' : $ doc = 'numero ' ; break ;//maranhao
279
+ case 'MT ' : $ doc = 'numero ' ; break ;//mato grosso
280
+ case 'MS ' : $ doc = 'numero ' ; break ;//matro grosso sol
281
+ case 'MG ' : $ doc = 'numero ' ; break ;
282
+ case 'PA ' : $ doc = 'numero ' ; break ;//para
283
+ case 'PB ' : $ doc = 'numero ' ; break ;//paraiba
284
+ case 'PR ' : $ doc = 'numero ' ; break ;
285
+ case 'PE ' : $ doc = 'chave ' ; break ;//pernan
286
+ case 'PI ' : $ doc = 'numero ' ; break ;//piaiu
287
+ case 'RJ ' : $ doc = 'chave ' ; break ;
288
+ case 'RN ' : $ doc = 'numero ' ; break ;
289
+ case 'RS ' : $ doc = 'chave ' ; break ;
290
+ case 'RO ' : $ doc = 'numero ' ; break ;//Rondonia
291
+ case 'RR ' : $ doc = 'numero ' ; break ;//Roraima
292
+ case 'SC ' : $ doc = 'chave ' ; break ;
293
+ case 'SP ' : $ doc = 'numero ' ; break ;
294
+ case 'SE ' : $ doc = 'numero ' ; break ;
295
+ case 'TO ' : $ doc = 'numero ' ; break ;
296
+ }
297
+ return $ doc ;
298
+ }
299
+
300
+ }
0 commit comments