@@ -57,10 +57,11 @@ discard block |
||
57 | 57 | */ |
58 | 58 | public function __construct($datos, $normalizar = true) |
59 | 59 | { |
60 | - if (is_array($datos)) |
|
61 | - $this->setDatos($datos, $normalizar); |
|
62 | - else if (is_string($datos)) |
|
63 | - $this->loadXML($datos); |
|
60 | + if (is_array($datos)) { |
|
61 | + $this->setDatos($datos, $normalizar); |
|
62 | + } else if (is_string($datos)) { |
|
63 | + $this->loadXML($datos); |
|
64 | + } |
|
64 | 65 | $this->timestamp = date('Y-m-d\TH:i:s'); |
65 | 66 | } |
66 | 67 | |
@@ -121,8 +122,9 @@ discard block |
||
121 | 122 | if ($normalizar) { |
122 | 123 | $this->normalizar($datos); |
123 | 124 | $method = 'normalizar_'.$this->tipo; |
124 | - if (method_exists($this, $method)) |
|
125 | - $this->$method($datos); |
|
125 | + if (method_exists($this, $method)) { |
|
126 | + $this->$method($datos); |
|
127 | + } |
|
126 | 128 | $this->normalizar_final($datos); |
127 | 129 | } |
128 | 130 | $this->tipo_general = $this->getTipoGeneral($this->tipo); |
@@ -199,8 +201,9 @@ discard block |
||
199 | 201 | */ |
200 | 202 | public function getJSON() |
201 | 203 | { |
202 | - if (!$this->getDatos()) |
|
203 | - return false; |
|
204 | + if (!$this->getDatos()) { |
|
205 | + return false; |
|
206 | + } |
|
204 | 207 | return json_encode($this->datos, JSON_PRETTY_PRINT); |
205 | 208 | } |
206 | 209 | |
@@ -225,9 +228,10 @@ discard block |
||
225 | 228 | */ |
226 | 229 | private function getTipoGeneral($dte) |
227 | 230 | { |
228 | - foreach ($this->tipos as $tipo => $codigos) |
|
229 | - if (in_array($dte, $codigos)) |
|
231 | + foreach ($this->tipos as $tipo => $codigos) { |
|
232 | + if (in_array($dte, $codigos)) |
|
230 | 233 | return $tipo; |
234 | + } |
|
231 | 235 | \sasco\LibreDTE\Log::write( |
232 | 236 | \sasco\LibreDTE\Estado::DTE_ERROR_TIPO, |
233 | 237 | \sasco\LibreDTE\Estado::get(\sasco\LibreDTE\Estado::DTE_ERROR_TIPO, $dte) |
@@ -266,10 +270,12 @@ discard block |
||
266 | 270 | public function getEmisor() |
267 | 271 | { |
268 | 272 | $nodo = $this->xml->xpath('/DTE/'.$this->tipo_general.'/Encabezado/Emisor/RUTEmisor')->item(0); |
269 | - if ($nodo) |
|
270 | - return $nodo->nodeValue; |
|
271 | - if (!$this->getDatos()) |
|
272 | - return false; |
|
273 | + if ($nodo) { |
|
274 | + return $nodo->nodeValue; |
|
275 | + } |
|
276 | + if (!$this->getDatos()) { |
|
277 | + return false; |
|
278 | + } |
|
273 | 279 | return $this->datos['Encabezado']['Emisor']['RUTEmisor']; |
274 | 280 | } |
275 | 281 | |
@@ -282,10 +288,12 @@ discard block |
||
282 | 288 | public function getReceptor() |
283 | 289 | { |
284 | 290 | $nodo = $this->xml->xpath('/DTE/'.$this->tipo_general.'/Encabezado/Receptor/RUTRecep')->item(0); |
285 | - if ($nodo) |
|
286 | - return $nodo->nodeValue; |
|
287 | - if (!$this->getDatos()) |
|
288 | - return false; |
|
291 | + if ($nodo) { |
|
292 | + return $nodo->nodeValue; |
|
293 | + } |
|
294 | + if (!$this->getDatos()) { |
|
295 | + return false; |
|
296 | + } |
|
289 | 297 | return $this->datos['Encabezado']['Receptor']['RUTRecep']; |
290 | 298 | } |
291 | 299 | |
@@ -298,10 +306,12 @@ discard block |
||
298 | 306 | public function getFechaEmision() |
299 | 307 | { |
300 | 308 | $nodo = $this->xml->xpath('/DTE/'.$this->tipo_general.'/Encabezado/IdDoc/FchEmis')->item(0); |
301 | - if ($nodo) |
|
302 | - return $nodo->nodeValue; |
|
303 | - if (!$this->getDatos()) |
|
304 | - return false; |
|
309 | + if ($nodo) { |
|
310 | + return $nodo->nodeValue; |
|
311 | + } |
|
312 | + if (!$this->getDatos()) { |
|
313 | + return false; |
|
314 | + } |
|
305 | 315 | return $this->datos['Encabezado']['IdDoc']['FchEmis']; |
306 | 316 | } |
307 | 317 | |
@@ -314,10 +324,12 @@ discard block |
||
314 | 324 | public function getMontoTotal() |
315 | 325 | { |
316 | 326 | $nodo = $this->xml->xpath('/DTE/'.$this->tipo_general.'/Encabezado/Totales/MntTotal')->item(0); |
317 | - if ($nodo) |
|
318 | - return $nodo->nodeValue; |
|
319 | - if (!$this->getDatos()) |
|
320 | - return false; |
|
327 | + if ($nodo) { |
|
328 | + return $nodo->nodeValue; |
|
329 | + } |
|
330 | + if (!$this->getDatos()) { |
|
331 | + return false; |
|
332 | + } |
|
321 | 333 | return $this->datos['Encabezado']['Totales']['MntTotal']; |
322 | 334 | } |
323 | 335 | |
@@ -330,10 +342,12 @@ discard block |
||
330 | 342 | public function getMoneda() |
331 | 343 | { |
332 | 344 | $nodo = $this->xml->xpath('/DTE/'.$this->tipo_general.'/Encabezado/Totales/TpoMoneda')->item(0); |
333 | - if ($nodo) |
|
334 | - return $nodo->nodeValue; |
|
335 | - if (!$this->getDatos()) |
|
336 | - return false; |
|
345 | + if ($nodo) { |
|
346 | + return $nodo->nodeValue; |
|
347 | + } |
|
348 | + if (!$this->getDatos()) { |
|
349 | + return false; |
|
350 | + } |
|
337 | 351 | return $this->datos['Encabezado']['Totales']['TpoMoneda']; |
338 | 352 | } |
339 | 353 | |
@@ -376,8 +390,9 @@ discard block |
||
376 | 390 | return false;*/ |
377 | 391 | $xml = new \sasco\LibreDTE\XML(); |
378 | 392 | $TED = $this->xml->getElementsByTagName('TED')->item(0); |
379 | - if (!$TED) |
|
380 | - return '<TED/>'; |
|
393 | + if (!$TED) { |
|
394 | + return '<TED/>'; |
|
395 | + } |
|
381 | 396 | $xml->loadXML($TED->C14N()); |
382 | 397 | $xml->documentElement->removeAttributeNS('http://www.w3.org/2001/XMLSchema-instance', 'xsi'); |
383 | 398 | $xml->documentElement->removeAttributeNS('http://www.sii.cl/SiiDte', ''); |
@@ -574,10 +589,12 @@ discard block |
||
574 | 589 | */ |
575 | 590 | private function calcularNetoIVA($total, $tasa = null) |
576 | 591 | { |
577 | - if ($tasa === 0 or $tasa === false) |
|
578 | - return [0, 0]; |
|
579 | - if ($tasa === null) |
|
580 | - $tasa = \sasco\LibreDTE\Sii::getIVA(); |
|
592 | + if ($tasa === 0 or $tasa === false) { |
|
593 | + return [0, 0]; |
|
594 | + } |
|
595 | + if ($tasa === null) { |
|
596 | + $tasa = \sasco\LibreDTE\Sii::getIVA(); |
|
597 | + } |
|
581 | 598 | // WARNING: el IVA obtenido puede no ser el NETO*(TASA/100) |
582 | 599 | // se calcula el monto neto y luego se obtiene el IVA haciendo la resta |
583 | 600 | // entre el total y el neto, ya que hay casos de borde como: |
@@ -693,8 +710,9 @@ discard block |
||
693 | 710 | } |
694 | 711 | // si existe descuento o recargo global se normalizan |
695 | 712 | if (!empty($datos['DscRcgGlobal'])) { |
696 | - if (!isset($datos['DscRcgGlobal'][0])) |
|
697 | - $datos['DscRcgGlobal'] = [$datos['DscRcgGlobal']]; |
|
713 | + if (!isset($datos['DscRcgGlobal'][0])) { |
|
714 | + $datos['DscRcgGlobal'] = [$datos['DscRcgGlobal']]; |
|
715 | + } |
|
698 | 716 | $NroLinDR = 1; |
699 | 717 | foreach ($datos['DscRcgGlobal'] as &$dr) { |
700 | 718 | $dr = array_merge([ |
@@ -1357,8 +1375,9 @@ discard block |
||
1357 | 1375 | */ |
1358 | 1376 | private function normalizar_detalle(array &$datos) |
1359 | 1377 | { |
1360 | - if (!isset($datos['Detalle'][0])) |
|
1361 | - $datos['Detalle'] = [$datos['Detalle']]; |
|
1378 | + if (!isset($datos['Detalle'][0])) { |
|
1379 | + $datos['Detalle'] = [$datos['Detalle']]; |
|
1380 | + } |
|
1362 | 1381 | $item = 1; |
1363 | 1382 | foreach ($datos['Detalle'] as &$d) { |
1364 | 1383 | $d = array_merge([ |
@@ -1420,8 +1439,9 @@ discard block |
||
1420 | 1439 | ]; |
1421 | 1440 | } |
1422 | 1441 | if ($d['PrcItem']) { |
1423 | - if (!$d['QtyItem']) |
|
1424 | - $d['QtyItem'] = 1; |
|
1442 | + if (!$d['QtyItem']) { |
|
1443 | + $d['QtyItem'] = 1; |
|
1444 | + } |
|
1425 | 1445 | if (empty($d['MontoItem'])) { |
1426 | 1446 | $d['MontoItem'] = $this->round( |
1427 | 1447 | (float)$d['QtyItem'] * (float)$d['PrcItem'], |
@@ -1487,8 +1507,9 @@ discard block |
||
1487 | 1507 | private function normalizar_aplicar_descuentos_recargos(array &$datos) |
1488 | 1508 | { |
1489 | 1509 | if (!empty($datos['DscRcgGlobal'])) { |
1490 | - if (!isset($datos['DscRcgGlobal'][0])) |
|
1491 | - $datos['DscRcgGlobal'] = [$datos['DscRcgGlobal']]; |
|
1510 | + if (!isset($datos['DscRcgGlobal'][0])) { |
|
1511 | + $datos['DscRcgGlobal'] = [$datos['DscRcgGlobal']]; |
|
1512 | + } |
|
1492 | 1513 | foreach ($datos['DscRcgGlobal'] as &$dr) { |
1493 | 1514 | $dr = array_merge([ |
1494 | 1515 | 'NroLinDR' => false, |
@@ -1555,8 +1576,9 @@ discard block |
||
1555 | 1576 | $montos = []; |
1556 | 1577 | foreach ($datos['Detalle'] as &$d) { |
1557 | 1578 | if (!empty($d['CodImpAdic'])) { |
1558 | - if (!isset($montos[$d['CodImpAdic']])) |
|
1559 | - $montos[$d['CodImpAdic']] = 0; |
|
1579 | + if (!isset($montos[$d['CodImpAdic']])) { |
|
1580 | + $montos[$d['CodImpAdic']] = 0; |
|
1581 | + } |
|
1560 | 1582 | $montos[$d['CodImpAdic']] += $d['MontoItem']; |
1561 | 1583 | } |
1562 | 1584 | } |
@@ -1635,10 +1657,12 @@ discard block |
||
1635 | 1657 | } |
1636 | 1658 | if (empty($datos['Encabezado']['Totales']['MntTotal'])) { |
1637 | 1659 | $datos['Encabezado']['Totales']['MntTotal'] = $datos['Encabezado']['Totales']['MntNeto']; |
1638 | - if (!empty($datos['Encabezado']['Totales']['IVA'])) |
|
1639 | - $datos['Encabezado']['Totales']['MntTotal'] += $datos['Encabezado']['Totales']['IVA']; |
|
1640 | - if (!empty($datos['Encabezado']['Totales']['MntExe'])) |
|
1641 | - $datos['Encabezado']['Totales']['MntTotal'] += $datos['Encabezado']['Totales']['MntExe']; |
|
1660 | + if (!empty($datos['Encabezado']['Totales']['IVA'])) { |
|
1661 | + $datos['Encabezado']['Totales']['MntTotal'] += $datos['Encabezado']['Totales']['IVA']; |
|
1662 | + } |
|
1663 | + if (!empty($datos['Encabezado']['Totales']['MntExe'])) { |
|
1664 | + $datos['Encabezado']['Totales']['MntTotal'] += $datos['Encabezado']['Totales']['MntExe']; |
|
1665 | + } |
|
1642 | 1666 | } |
1643 | 1667 | } else { |
1644 | 1668 | if (!$datos['Encabezado']['Totales']['MntTotal'] and !empty($datos['Encabezado']['Totales']['MntExe'])) { |
@@ -1664,8 +1688,10 @@ discard block |
||
1664 | 1688 | } |
1665 | 1689 | // si hay impuesto de crédito a constructoras del 65% se descuenta del total |
1666 | 1690 | if (!empty($datos['Encabezado']['Totales']['CredEC'])) { |
1667 | - if ($datos['Encabezado']['Totales']['CredEC']===true) |
|
1668 | - $datos['Encabezado']['Totales']['CredEC'] = round($datos['Encabezado']['Totales']['IVA'] * 0.65); // TODO: mover a constante o método |
|
1691 | + if ($datos['Encabezado']['Totales']['CredEC']===true) { |
|
1692 | + $datos['Encabezado']['Totales']['CredEC'] = round($datos['Encabezado']['Totales']['IVA'] * 0.65); |
|
1693 | + } |
|
1694 | + // TODO: mover a constante o método |
|
1669 | 1695 | $datos['Encabezado']['Totales']['MntTotal'] -= $datos['Encabezado']['Totales']['CredEC']; |
1670 | 1696 | } |
1671 | 1697 | } |