Xml   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 316
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 210
c 2
b 0
f 0
dl 0
loc 316
rs 10
wmc 13

4 Methods

Rating   Name   Duplication   Size   Complexity  
A dataus() 0 3 1
A upload() 0 11 3
B gravaarquivo() 0 40 7
B mapa() 0 228 2
1
<?php
2
3
namespace AwaCode\ImportNfe;
4
5
use Exception;
6
7
/**
8
 * Class AwaCode Xml
9
 *
10
 * @author Rhyann Carvalhais <https://github.com/rhyannc>
11
 * @package AwaCode\Importnfe
12
 */
13
class Xml extends ImportNfe
14
{
15
    /**
16
     * Allow xml,
17
     * @var array allowed file types
18
     * https://www.freeformatter.com/mime-types-list.html
19
     */
20
    protected static  $allowTypes = [
21
        "text/xml",
22
    ];
23
24
    /**
25
     * Extensões permitidas para tipos.
26
     * @var array
27
     */
28
    protected static  $extensions = [
29
        "xml"
30
    ];
31
32
    /**
33
     * @param array $xml
34
     * @param string $name
35
     * @return string
36
     * @throws Exception
37
     */
38
    public function upload(array $xml): string
39
    {
40
        $this->ext($xml);
41
42
        if (!in_array($xml['type'], static::$allowTypes) || !in_array($this->ext, static::$extensions)) {
43
            throw new Exception("Não é um tipo ou extensão XML de NFe válido");
44
        }
45
46
        $this->name($xml["name"]);
47
        move_uploaded_file($xml['tmp_name'], "{$this->path}/{$this->name}");
48
        return "{$this->path}/{$this->name}";
49
50
    }
51
52
    public function gravaarquivo()
53
    {
54
        // OSB: SO SALVA NFE EM AMBIENTE DE PRODUÇÃO E COM NUMERO DE  AUTORIZAÇÃO
55
        //Diretorio onde estao os arquivos
56
        $directory = dir($this->path);
57
58
        //WHILE ENTRA NA PASTA ARQUIVOS E FAZ A VERIFICAÇÃO DE TODOS OS XML LA DENTRO
59
        $p = 0;
60
        $nota = array();
61
        $nota = (object) $nota;
62
63
        while(($arquivo = $directory->read()) !== false)
64
         {
65
            if (empty($arquivo) || $arquivo == '..' || $arquivo == '.') continue;
66
67
            $lexml   =  $this->path .'/'. $arquivo;
68
            $xml     =  simplexml_load_file($lexml);
69
70
            $environ =  $xml->NFe->infNFe->ide->tpAmb;
71
72
             if (empty($xml->protNFe->infProt->nProt))
73
             {
74
                 echo "<h4>Arquivo sem dados de Protocolo!</h4>";
75
                 rename($lexml,"./{$this->pathError}/$arquivo");
76
                 continue;
77
             }
78
             if ($environ != 1)  //se diferente de 1 ( 1 = Produção 2 = Homologação )
79
             {
80
                 echo "<h4>Documento emitido em ambiente de homologação!</h4>";
81
                 rename($lexml,"./{$this->pathError}/$arquivo");
82
                 continue;
83
             }
84
85
             ++$p;
86
             $nota->items[$p] = $this->mapa( $xml );
87
             rename($lexml,"./{$this->pathImport}/$arquivo");
88
89
         }
90
91
        return $nota;
92
    }
93
94
95
    public function dataus($data)
96
    {
97
        return date("Y-m-d", strtotime($data));
98
    }
99
100
101
    public function mapa($xml)
102
    {
103
104
        $chave = $xml->NFe->infNFe->attributes()->Id;
105
        $chave = strtr(strtoupper($chave), array("NFE" => NULL));
106
107
        //IDE
108
        @$cUF =         $xml->NFe->infNFe->ide->cUF;    	 //<cUF>41</cUF>  Código do Estado do Fator gerador
109
        @$cNF =         $xml->NFe->infNFe->ide->cNF;       	 //<cNF>21284519</cNF>   Código número da nfe
110
        @$natOp =       $xml->NFe->infNFe->ide->natOp;       //<natOp>V E N D A</natOp>  Resumo da Natureza de operação
111
        @$indPag =      $xml->NFe->infNFe->ide->indPag;      //<indPag>2</indPag> 0 – pagamento à vista; 1 – pagamento à prazo; 2 - outros
112
        @$mod =         $xml->NFe->infNFe->ide->mod;         //<mod>55</mod> Modelo do documento Fiscal
113
        @$serie =       $xml->NFe->infNFe->ide->serie;    	 //<serie>2</serie>
114
        @$nNF =         $xml->NFe->infNFe->ide->nNF;   	     //<nNF>19685</nNF> Número da Nota Fiscal
115
        @$dhEmi =       $xml->NFe->infNFe->ide->dhEmi;       //<dEmi>2011-09-06</dEmi> Data de emissão da Nota Fiscal
116
        @$dhEmi =       $this->dataus(@$dhEmi);
117
        @$dhSaiEnt =    $xml->NFe->infNFe->ide->dhSaiEnt;    //<dSaiEnt>2011-09-06</dSaiEnt> Data de entrada ou saida da Nota Fiscal
118
        @$tpNF =        $xml->NFe->infNFe->ide->tpNF;        //<tpNF>1</tpNF>  0-entrada / 1-saída
119
        @$cMunFG =      $xml->NFe->infNFe->ide->cMunFG;      //<cMunFG>4106407</cMunFG> Código do municipio Tabela do IBGE
120
        @$tpImp =       $xml->NFe->infNFe->ide->tpImp;       //<tpImp>1</tpImp>
121
        @$tpEmis =      $xml->NFe->infNFe->ide->tpEmis;      //<tpEmis>1</tpEmis>
122
        @$cDV =         $xml->NFe->infNFe->ide->cDV;         //<cDV>0</cDV>
123
        $finNFe =       $xml->NFe->infNFe->ide->finNFe;      //<finNFe>1</finNFe>
124
        $procEmi =      $xml->NFe->infNFe->ide->procEmi;     //<procEmi>0</procEmi>
125
        $verProc =      $xml->NFe->infNFe->ide->verProc;     //<verProc>2.0.0</verProc>
126
        @$tpAmb =       $xml->NFe->infNFe->ide->tpAmb;       //<tpAmb>1</tpAmb> 1 = Produção 2 = Homologação.
127
128
        //INFPROT
129
        $xMotivo =      $xml->protNFe->infProt->xMotivo;
130
        $nProt =        $xml->protNFe->infProt->nProt;
131
132
        //EMIT
133
        $emit_CPF =     $xml->NFe->infNFe->emit->CPF;
134
        $emit_CNPJ =    $xml->NFe->infNFe->emit->CNPJ;
135
        $emit_xNome =   $xml->NFe->infNFe->emit->xNome;
136
        $emit_IE =   $xml->NFe->infNFe->emit->IE;
137
        $emit_xFant =   $xml->NFe->infNFe->emit->xFant;
138
        $emit_cpf_cnpj = $emit_CNPJ . $emit_CPF;
139
140
        //ENDEREMIT
141
        $emit_xLgr =    $xml->NFe->infNFe->emit->enderEmit->xLgr;
142
        $emit_nro=      $xml->NFe->infNFe->emit->enderEmit->nro;
143
        $emit_comp=      $xml->NFe->infNFe->emit->enderEmit->xCpl;
144
        $emit_xBairro = $xml->NFe->infNFe->emit->enderEmit->xBairro;
145
        $emit_cMun =    $xml->NFe->infNFe->emit->enderEmit->cMun;
146
        $emit_xMun =    $xml->NFe->infNFe->emit->enderEmit->xMun;
147
        $emit_UF =      $xml->NFe->infNFe->emit->enderEmit->UF;
148
        $emit_CEP =     $xml->NFe->infNFe->emit->enderEmit->CEP;
149
        $emit_cPais =   $xml->NFe->infNFe->emit->enderEmit->cPais;
150
        $emit_xPais =   $xml->NFe->infNFe->emit->enderEmit->xPais;
151
        $emit_fone =    $xml->NFe->infNFe->emit->enderEmit->fone;
152
153
        //DEST
154
        $dest_CPF   =   $xml->NFe->infNFe->dest->CPF;
155
        $dest_CNPJ  =   $xml->NFe->infNFe->dest->CNPJ;
156
        $dest_xNome =   $xml->NFe->infNFe->dest->xNome;
157
        $dest_xFant =   $xml->NFe->infNFe->dest->xFant;
158
        $dest_IE    =   $xml->NFe->infNFe->dest->IE;
159
        $dest_cpf_cnpj = $dest_CNPJ . $dest_CPF;
160
161
        //ENDERDEST
162
        @$dest_xLgr =    $xml->NFe->infNFe->dest->enderDest->xLgr;
163
        @$dest_nro=      $xml->NFe->infNFe->dest->enderDest->nro;
164
        @$dest_xBairro = $xml->NFe->infNFe->dest->enderDest->xBairro;
165
        @$dest_cMun =    $xml->NFe->infNFe->dest->enderDest->cMun;
166
        @$dest_xMun =    $xml->NFe->infNFe->dest->enderDest->xMun;
167
        @$dest_UF =      $xml->NFe->infNFe->dest->enderDest->UF;
168
        @$dest_CEP =     $xml->NFe->infNFe->dest->enderDest->CEP;
169
        @$dest_cPais =   $xml->NFe->infNFe->dest->enderDest->cPais;
170
        @$dest_xPais =   $xml->NFe->infNFe->dest->enderDest->xPais;
171
        @$dest_fone =    $xml->NFe->infNFe->dest->enderDest->fone;
172
173
        //TRANSP
174
        $transp_mod   = $xml->NFe->infNFe->transp->modFrete;
175
        $transp_CPF   = $xml->NFe->infNFe->transp->transporta->CPF;
176
        $transp_CNPJ  = $xml->NFe->infNFe->transp->transporta->CNPJ;
177
        $transp_xNome = $xml->NFe->infNFe->transp->transporta->xNome;
178
        $transp_IE    = $xml->NFe->infNFe->transp->transporta->IE;
179
        $transp_cpf_cnpj = $transp_CNPJ .$transp_CPF;
180
181
        //VOL
182
        $transp_qVol  = $xml->NFe->infNFe->transp->vol->qVol;
183
        $transp_esp   = $xml->NFe->infNFe->transp->vol->esp;
184
        $transp_marca = $xml->NFe->infNFe->transp->vol->marca;
185
186
        //VEICTRANSP
187
        $transp_placa = $xml->NFe->infNFe->transp->veicTransp->placa;
188
        $transp_UF =    $xml->NFe->infNFe->transp->veicTransp->UF;
189
190
        //TOTAL
191
        $vBC =          $xml->NFe->infNFe->total->ICMSTot->vBC;
192
        $vICMS =        $xml->NFe->infNFe->total->ICMSTot->vICMS;
193
        $vBCST =        $xml->NFe->infNFe->total->ICMSTot->vBCST;
194
        $vST =          $xml->NFe->infNFe->total->ICMSTot->vST;
195
        $vProd =        $xml->NFe->infNFe->total->ICMSTot->vProd;
196
        $vNF =          $xml->NFe->infNFe->total->ICMSTot->vNF;
197
        $vFrete =       $xml->NFe->infNFe->total->ICMSTot->vFrete;
198
        $vSeg =         $xml->NFe->infNFe->total->ICMSTot->vSeg;
199
        $vDesc =        $xml->NFe->infNFe->total->ICMSTot->vDesc;
200
        $vIPI =         $xml->NFe->infNFe->total->ICMSTot->vIPI;
201
        $vOutro =       $xml->NFe->infNFe->total->ICMSTot->vOutro;
202
203
        $dadosnfe = [
204
                     //IDE
205
                     "chave"         =>   "$chave",
206
                     "cUF"           =>   "$cUF",
207
                     "cNF"           =>   "$cNF",
208
                     "natOp"         =>   "$natOp",
209
                     "indPag"       =>    "$indPag",
210
                     "mod"           =>   "$mod",
211
                     "serie"         =>   "$serie",
212
                     "nNF"           =>   "$nNF",
213
                     "dhEmi"         =>   "$dhEmi",
214
                     "dhSaiEnt"      =>   "$dhSaiEnt",
215
                     "tpNF"          =>   "$tpNF",
216
                     "cMunFG"        =>   "$cMunFG",
217
                     "tpImp"         =>   "$tpImp",
218
                     "tpEmis"        =>   "$tpEmis",
219
                     "cDV"           =>   "$cDV",
220
                     "finNFe"        =>   "$finNFe",
221
                     "procEmi"       =>   "$procEmi",
222
                     "verProc"       =>   "$verProc",
223
                     "tpAmb"         =>   "$tpAmb",
224
225
                     //INFPROT
226
                     "xMotivo"       =>   "$xMotivo",
227
                     "nProt"         =>   "$nProt",
228
229
                     //EMIT
230
                     "emit_cpf_cnpj" =>   "$emit_cpf_cnpj",
231
                     "emit_xNome"    =>   "$emit_xNome",
232
                     "emit_xFant"    =>   "$emit_xFant",
233
                     "emit_ie"       =>   "$emit_IE",
234
235
                     //ENDEREMIT
236
                     "emit_xLgr"     =>   "$emit_xLgr",
237
                     "emit_nro"      =>   "$emit_nro",
238
                     "emit_comp"     =>   "$emit_comp",
239
                     "emit_xBairro"  =>   "$emit_xBairro",
240
                     "emit_cMun"    =>   "$emit_cMun",
241
                     "emit_xMun"    =>   "$emit_xMun",
242
                     "emit_UF"      =>   "$emit_UF",
243
                     "emit_CEP"     =>   "$emit_CEP",
244
                     "emit_cPais"   =>   "$emit_cPais",
245
                     "emit_xPais"   =>   "$emit_xPais",
246
                     "emit_fone"    =>   "$emit_fone",
247
248
                     //DEST
249
                     "dest_cpf_cnpj"  => "$dest_cpf_cnpj",
250
                     "dest_xNome"   =>   "$dest_xNome",
251
                     "dest_xFant"   =>   "$dest_xFant",
252
                     "dest_IE"      =>   "$dest_IE",
253
254
                     //ENDERDEST
255
                     "dest_xLgr"    =>   "$dest_xLgr",
256
                     "dest_nro"     =>   "$dest_nro",
257
                     "dest_xBairro" =>   "$dest_xBairro",
258
                     "dest_cMun"    =>   "$dest_cMun",
259
                     "dest_xMun"    =>   "$dest_xMun",
260
                     "dest_UF"      =>   "$dest_UF",
261
                     "dest_CEP"     =>   "$dest_CEP",
262
                     "dest_cPais"   =>   "$dest_cPais",
263
                     "dest_xPais"   =>   "$dest_xPais",
264
                     "dest_fone"    =>   "$dest_fone",
265
266
                     //TRANSP
267
                     "transp_mod"   =>   "$transp_mod",
268
                     "transp_cpf_cnpj"   =>   "$transp_cpf_cnpj",
269
                     "transp_xNome" =>   "$transp_xNome",
270
                     "transp_IE"    =>   "$transp_IE",
271
272
                     //VOL
273
                     "transp_qVol"  =>   "$transp_qVol",
274
                     "transp_esp"   =>   "$transp_esp",
275
                     "transp_marca" =>   "$transp_marca",
276
277
                     //VEICTRANSP
278
                     "transp_placa" =>   "$transp_placa",
279
                     "transp_UF"    =>   "$transp_UF",
280
281
                     //TOTAL
282
                     "vBC"          =>   "$vBC",
283
                     "vICMS"        =>   "$vICMS",
284
                     "vBCST"        =>   "$vBCST",
285
                     "vST"          =>   "$vST",
286
                     "vProd"        =>   "$vProd",
287
                     "vNF"          =>   "$vNF",
288
                     "vFrete"       =>   "$vFrete",
289
                     "vSeg"         =>   "$vSeg",
290
                     "vDesc"        =>   "$vDesc",
291
                     "vIPI"         =>   "$vIPI",
292
                     "vOutro"       =>   "$vOutro"
293
            ];
294
295
        $i =0;
296
        foreach($xml->NFe->infNFe->det as $item)
297
        {
298
            ++$i;
299
            $codigo = $item->prod->cProd;
300
            $cean = $item->prod->cEAN;
301
            $xProd = $item->prod->xProd;
302
            $NCM = $item->prod->NCM;
303
            $CFOP = $item->prod->CFOP;
304
            $cbenef = $item->prod->cBenef;
305
            $uCom = $item->prod->uCom;
306
            $qCom = $item->prod->qCom;
307
            $vUnCom = $item->prod->vUnCom;
308
            $vProd = $item->prod->vProd;
309
            $indTot = $item->prod->indTot;
310
311
            $arr_itens =[
312
                "codigo"      =>   "$codigo",
313
                "cean"        =>   "$cean",
314
                "xProd"       =>   "$xProd",
315
                "NCM"         =>   "$NCM",
316
                "CFOP"        =>   "$CFOP",
317
                "cbenef"      =>   "$cbenef",
318
                "uCom"        =>   "$uCom",
319
                "qCom"        =>   "$qCom",
320
                "vUnCom"      =>   "$vUnCom",
321
                "vProd"       =>   "$vProd",
322
                "indTot"      =>   "$indTot"
323
            ];
324
325
            $dadosnfe['itens'][$i]= $arr_itens;
326
        }
327
328
        return ($dadosnfe);
329
    }
330
}
331