-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprocesaXML.php
95 lines (95 loc) · 3.73 KB
/
procesaXML.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
/*
* procesa datos de un XML, devuelve un objeto.
* @ToRo 2017-12-07 https://tar.mx/tema/facturacion-electronica.html
* soporta CFDi 3.3
*/
require_once(__DIR__."/cats.php");
class cfdi {
public $xml;
private $cat = [];
public function parse() {
}
public function __construct() {
@$xml= func_get_arg(0);
if(!empty($xml)) $this->xml = $xml;
$this->cat = $GLOBALS['CAT'];
}
public function run() {
$datos = new stdclass;
if(empty($this->xml)) {
$datos->error = "debe utilizar el método xml(contenido archivo cfdi)";
return $datos;
}
libxml_use_internal_errors(true);
$xml=new DOMDocument();
$ok = $xml->loadXML($this->xml);
if(empty($ok)) {
$datos->error = "no se puede leer el XML";
return $datos;
}
$root = $xml->getElementsByTagName('Comprobante')->item(0);
$datos->id = null;
$datos->version = $ver = ($root->getAttribute('Version') != null) ? $root->getAttribute('Version') : $root->getAttribute('version');
// cabeceras
foreach($this->cat->cabeza["3.3"] AS $tag=>$x) {
$datos->{$tag} = (string) $root->getAttribute($this->cat->cabeza[$ver][$tag]);
}
// emisor
$emite = $root->getElementsByTagName('Emisor')->item(0);
$datos->emisor = new stdclass;
foreach($this->cat->emisor["3.3"] AS $tag=>$x) {
$datos->emisor->{$tag} = (string) $emite->getAttribute($this->cat->emisor[$ver][$tag]);
}
// receptor
$recibe= $root->getElementsByTagName('Receptor')->item(0);
$datos->receptor = new stdclass;
foreach($this->cat->receptor["3.3"] AS $tag=>$x) {
$datos->receptor->{$tag} = (string) $recibe->getAttribute($this->cat->receptor[$ver][$tag]);
}
// conceptos
$losconceptos=[];
$concepto= $root->getElementsByTagName('Concepto');
$i=0;
foreach($concepto AS $t) {
$i++;
$losconceptos[$i] = new stdclass;
foreach($this->cat->conceptos["3.3"] AS $a=>$b) {
if(isset($this->cat->conceptos[$ver][$a])) {
$losconceptos[$i]->{$a} = $t->getAttribute($this->cat->conceptos[$ver][$a]);
}
}
foreach(['Traslado' => 'trasladados','Retencion' => 'retenidos'] AS $x=>$y) {
$traslado = $concepto->item(0)->getElementsByTagName($x);
$ii = 0;
foreach($traslado as $t) {
$ii++;
$losconceptos[$i]->{$y}[$ii] = new stdclass;
foreach($this->cat->ci["3.3"] AS $a=>$b) {
if(isset($this->cat->ci[$ver][$a])) {
$losconceptos[$i]->{$y}[$ii]->{$a} = $t->getAttribute($this->cat->ci[$ver][$a]);
}
}
}
}
}
$datos->conceptos = $losconceptos;
//
$tfd = $root->getElementsByTagName('TimbreFiscalDigital')->item(0);
$datos->id = strtoupper($tfd->getAttribute('UUID'));
$datos->fechatimbrado = $tfd->getAttribute('FechaTimbrado');
$datos->sellosat = $tfd->getAttribute('SelloSAT');
$datos->tfdversion = $tfd->getAttribute('Version');
$datos->tfdrfc = $tfd->getAttribute('RfcProvCertif');
$datos->tfdnosat = $tfd->getAttribute('NoCertificadoSAT');
return $datos;
}
}
/* ejemplo */
if(isset($argv[1]) && isset($argv[1]) == 'TRUE') {
$xml= file_get_contents("test.xml");
$cfdi = new cfdi();
$cfdi->xml = $xml;
$data = $cfdi->run();
print_r($data);
}