From dcdbb6b64c98d4925ec976205726e575ba57d638 Mon Sep 17 00:00:00 2001 From: Thomas Zilio Date: Fri, 7 Dec 2018 10:56:49 +0100 Subject: [PATCH] Aggiunta importazione P7M --- CHANGELOG.md | 1 + plugins/importFE/rows.php | 27 ++++- plugins/importFE/src/FatturaElettronica.php | 12 +-- src/Util/XML.php | 112 ++++++++++++++++++++ 4 files changed, 143 insertions(+), 9 deletions(-) create mode 100644 src/Util/XML.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 0ddc2ff79..02bd59614 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -46,6 +46,7 @@ Il formato utilizzato รจ basato sulle linee guida di [Keep a Changelog](http://k ### Aggiunto (Added) - Nodi secondari per la Fatturazione Elettronica + - Importazione di Fatture Elettroniche in formato P7M - Messaggi informativi in vari campi ### Fixed diff --git a/plugins/importFE/rows.php b/plugins/importFE/rows.php index 247d74a23..4acafc460 100644 --- a/plugins/importFE/rows.php +++ b/plugins/importFE/rows.php @@ -69,7 +69,10 @@ $righe = $fattura_pa->getRighe(); if (!empty($righe)) { echo ' -

'.tr('Righe').'

+

+ '.tr('Righe').' + +

@@ -111,9 +114,29 @@ if (!empty($righe)) { echo '
'; + + echo ' + '; } else { echo ' -

Non ci sono righe nella fattura.

'; +

'.tr('Non ci sono righe nella fattura').'.

'; } echo ' diff --git a/plugins/importFE/src/FatturaElettronica.php b/plugins/importFE/src/FatturaElettronica.php index c04fcecc4..c5816b285 100644 --- a/plugins/importFE/src/FatturaElettronica.php +++ b/plugins/importFE/src/FatturaElettronica.php @@ -11,6 +11,7 @@ use Modules\Fatture\Tipo as TipoFattura; use Modules\Anagrafiche\Anagrafica; use Modules\Anagrafiche\Tipo as TipoAnagrafica; use Modules\Anagrafiche\Nazione; +use Util\XML; use Uploads; use Modules; use UnexpectedValueException; @@ -36,11 +37,7 @@ class FatturaElettronica public function __construct($file) { $this->file = static::getImportDirectory().'/'.$file; - - $xml = simplexml_load_file($this->file, 'SimpleXMLElement', LIBXML_NOCDATA); - $json = json_decode(json_encode($xml), true); - - $this->xml = $json; + $this->xml = XML::readFile($this->file); // Individuazione fattura pre-esistente $dati_generali = $this->getBody()['DatiGenerali']['DatiGeneraliDocumento']; @@ -50,8 +47,8 @@ class FatturaElettronica $fattura = Fattura::where([ 'progressivo_invio' => $progressivo_invio, + 'numero_esterno' => $numero, 'data' => $data, - 'numero' => $numero, ])->first(); if (!empty($fattura)) { @@ -240,7 +237,7 @@ class FatturaElettronica $result = isset($result[0]) ? $result : [$result]; - return $result; + return array_clean($result); } public function saveAllegati() @@ -273,6 +270,7 @@ class FatturaElettronica // Registrazione XML come allegato $filename = Uploads::upload($this->file, array_merge($info, [ 'name' => tr('Fattura Elettronica'), + 'original' => basename($this->file), ])); } diff --git a/src/Util/XML.php b/src/Util/XML.php new file mode 100644 index 000000000..1af9903b3 --- /dev/null +++ b/src/Util/XML.php @@ -0,0 +1,112 @@ +/', $string, $matches, PREG_OFFSET_CAPTURE); + $lastMatch = end($matches[0]); + + $result = substr($string, 0, $lastMatch[1] + strlen($lastMatch[0]) + 1); + + return static::sanitizeXML($result); + } + + /** + * Removes invalid characters from a UTF-8 XML string + * + * @param string a XML string potentially containing invalid characters + * @return string + * + * @source https://www.ryadel.com/php-eliminare-caratteri-non-validi-file-stringa-xml-utf8-utf-8/ + */ + protected static function sanitizeXML($string) + { + if (!empty($string)) { + $regex = '/( + [\xC0-\xC1] # Invalid UTF-8 Bytes + | [\xF5-\xFF] # Invalid UTF-8 Bytes + | \xE0[\x80-\x9F] # Overlong encoding of prior code point + | \xF0[\x80-\x8F] # Overlong encoding of prior code point + | [\xC2-\xDF](?![\x80-\xBF]) # Invalid UTF-8 Sequence Start + | [\xE0-\xEF](?![\x80-\xBF]{2}) # Invalid UTF-8 Sequence Start + | [\xF0-\xF4](?![\x80-\xBF]{3}) # Invalid UTF-8 Sequence Start + | (?<=[\x0-\x7F\xF5-\xFF])[\x80-\xBF] # Invalid UTF-8 Sequence Middle + | (?= 0x20) && ($current <= 0xD7FF)) || + (($current >= 0xE000) && ($current <= 0xFFFD)) || + (($current >= 0x10000) && ($current <= 0x10FFFF))) { + $result .= chr($current); + } else { + $ret; // use this to strip invalid character(s) + // $ret .= " "; // use this to replace them with spaces + } + } + $string = $result; + } + return $string; + } +}