From 28f312cae2fcd3b3ef9b7114d0e4957d478b5d71 Mon Sep 17 00:00:00 2001 From: HorstOeko Date: Mon, 9 Dec 2024 14:43:34 +0100 Subject: [PATCH] #206 Fix DateTime-Handling (strings with linebreaks) --- build/phpunit.xml | 1 + src/ZugferdObjectHelper.php | 3 + tests/assets/xml_issue_206.xml | 123 ++++++++++++++++++++++++ tests/testcases/issues/Issue206Test.php | 26 +++++ 4 files changed, 153 insertions(+) create mode 100644 tests/assets/xml_issue_206.xml create mode 100644 tests/testcases/issues/Issue206Test.php diff --git a/build/phpunit.xml b/build/phpunit.xml index 433f7bb0..80ff106d 100644 --- a/build/phpunit.xml +++ b/build/phpunit.xml @@ -55,6 +55,7 @@ ../tests/testcases/issues/Issue43Test.php ../tests/testcases/issues/Issue104Test.php ../tests/testcases/issues/Issue113Test.php + ../tests/testcases/issues/Issue206Test.php diff --git a/src/ZugferdObjectHelper.php b/src/ZugferdObjectHelper.php index 36536f86..94688de0 100644 --- a/src/ZugferdObjectHelper.php +++ b/src/ZugferdObjectHelper.php @@ -1437,6 +1437,9 @@ public function toDateTime(?string $dateTimeString, ?string $format): ?DateTime if (self::isNullOrEmpty($dateTimeString) || self::isNullOrEmpty($format)) { return null; } + + $dateTimeString = trim($dateTimeString); + if ($format == "102") { return DateTime::createFromFormat("Ymd", $dateTimeString); } elseif ($format == "101") { diff --git a/tests/assets/xml_issue_206.xml b/tests/assets/xml_issue_206.xml new file mode 100644 index 00000000..a1b3ec6e --- /dev/null +++ b/tests/assets/xml_issue_206.xml @@ -0,0 +1,123 @@ + + + + + urn:cen.eu:en16931:2017#compliant#urn:factur-x.eu:1p0:basic + + + + 471102 + 380 + + + 20200305 + + + + Rechnung gemäß Bestellung vom 01.03.2020. + + + Lieferant GmbH + Lieferantenstraße 20 + 80333 München + Deutschland + Geschäftsführer: Hans Muster + Handelsregisternummer: H A 123 + + + + Unsere GLN: 4000001123452 + Ihre GLN: 4000001987658 + Ihre Kundennummer: GE2020211 + Zahlbar innerhalb 30 Tagen netto bis 04.04.2020, 3% Skonto innerhalb 10 Tagen bis 15.03.2020. + + + + + + + 1 + + + 4012345001235 + GTIN: 4012345001235, Unsere Art.-Nr.: TB100A4, Trennblätter A4 + + + + 9.90 + + + + 20.0000 + + + + VAT + S + 19 + + + 198.00 + + + + + + Lieferant GmbH + + 80333 + Lieferantenstraße 20 + München + DE + + + 201/113/40209 + + + DE123456789 + + + + Kunden AG Mitte + + 69876 + Hans Muster + Kundenstraße 15 + Frankfurt + DE + + + + + + + 20200305 + + + + + EUR + + 37.62 + VAT + 198.00 + S + 19.00 + + + + 20200404 + + + + 198.00 + 0.00 + 0.00 + 198.00 + 37.62 + 235.62 + 235.62 + + + + \ No newline at end of file diff --git a/tests/testcases/issues/Issue206Test.php b/tests/testcases/issues/Issue206Test.php new file mode 100644 index 00000000..279d7b38 --- /dev/null +++ b/tests/testcases/issues/Issue206Test.php @@ -0,0 +1,26 @@ +getDocumentInformation( + $documentNo, + $documentTypeCode, + $documentDate, + $invoiceCurrency, + $taxCurrency, + $documentName, + $documentLanguage, + $effectiveSpecifiedPeriod + ); + + $this->assertEquals((\DateTime::createFromFormat('Ymd', '20200305'))->format('Ymd'), $documentDate->format('Ymd')); + } +}