From ec0df164ce7732dcac7aea073710a3888b3cd6d1 Mon Sep 17 00:00:00 2001 From: paulwer Date: Wed, 26 Jun 2024 19:24:57 +0200 Subject: [PATCH] log error in all cases for better debugging experience + minor find vendor improvements --- .../EDocument/Imports/MindeeEDocument.php | 5 ++- .../EDocument/Imports/ParseEDocument.php | 32 ++++++++----------- .../EDocument/Imports/ZugferdEDocument.php | 12 ++++++- 3 files changed, 26 insertions(+), 23 deletions(-) diff --git a/app/Services/EDocument/Imports/MindeeEDocument.php b/app/Services/EDocument/Imports/MindeeEDocument.php index a37e0bf9197a..7bc3b962354c 100644 --- a/app/Services/EDocument/Imports/MindeeEDocument.php +++ b/app/Services/EDocument/Imports/MindeeEDocument.php @@ -100,9 +100,8 @@ class MindeeEDocument extends AbstractService $vendor = null; $vendor_contact = VendorContact::where("company_id", $user->company()->id)->where("email", $prediction->supplierEmail)->first(); if ($vendor_contact) - return $vendor = $vendor_contact->vendor; - - if ($vendor) + $vendor = $vendor_contact->vendor; + if (!$vendor) $vendor = Vendor::where("company_id", $user->company()->id)->where("name", $prediction->supplierName)->first(); if ($vendor) { diff --git a/app/Services/EDocument/Imports/ParseEDocument.php b/app/Services/EDocument/Imports/ParseEDocument.php index 73776e4aaa9e..b790caaf026c 100644 --- a/app/Services/EDocument/Imports/ParseEDocument.php +++ b/app/Services/EDocument/Imports/ParseEDocument.php @@ -44,35 +44,29 @@ class ParseEDocument extends AbstractService $account = auth()->user()->account; // ZUGFERD - try to parse via Zugferd lib - $zugferd_exception = null; - try { - switch (true) { - case $this->file->getExtension() == 'pdf': - case $this->file->getExtension() == 'xml' && stristr($this->file->get(), "urn:cen.eu:en16931:2017"): - case $this->file->getExtension() == 'xml' && stristr($this->file->get(), "urn:cen.eu:en16931:2017#compliant#urn:xeinkauf.de:kosit:xrechnung_3.0"): - case $this->file->getExtension() == 'xml' && stristr($this->file->get(), "urn:cen.eu:en16931:2017#compliant#urn:xeinkauf.de:kosit:xrechnung_2.1"): - case $this->file->getExtension() == 'xml' && stristr($this->file->get(), "urn:cen.eu:en16931:2017#compliant#urn:xeinkauf.de:kosit:xrechnung_2.0"): + switch (true) { + case $this->file->getExtension() == 'pdf': + case $this->file->getExtension() == 'xml' && stristr($this->file->get(), "urn:cen.eu:en16931:2017"): + case $this->file->getExtension() == 'xml' && stristr($this->file->get(), "urn:cen.eu:en16931:2017#compliant#urn:xeinkauf.de:kosit:xrechnung_3.0"): + case $this->file->getExtension() == 'xml' && stristr($this->file->get(), "urn:cen.eu:en16931:2017#compliant#urn:xeinkauf.de:kosit:xrechnung_2.1"): + case $this->file->getExtension() == 'xml' && stristr($this->file->get(), "urn:cen.eu:en16931:2017#compliant#urn:xeinkauf.de:kosit:xrechnung_2.0"): + try { return (new ZugferdEDocument($this->file))->run(); - } - } catch (Exception $e) { - $zugferd_exception = $e; + } catch (Exception $e) { + nlog("Zugferd Exception: " . $e->getMessage()); + } } // MINDEE OCR - try to parse via mindee external service - $mindee_exception = null; if (config('services.mindee.api_key') && (Ninja::isSelfHost() || (Ninja::isHosted() && $account->isPaid() && $account->plan == 'enterprise'))) try { return (new MindeeEDocument($this->file))->run(); } catch (Exception $e) { - // ignore not available exceptions - $mindee_exception = $e; + if (!($e->getMessage() == 'Unsupported document type')) + nlog("Mindee Exception: " . $e->getMessage()); } - // log exceptions and throw error - if ($zugferd_exception) - nlog("Zugferd Exception: " . $zugferd_exception->getMessage()); - if ($mindee_exception) - nlog("Mindee Exception: " . $mindee_exception->getMessage()); + // NO PARSER OR ERROR throw new Exception("File type not supported or issue while parsing", 409); } } diff --git a/app/Services/EDocument/Imports/ZugferdEDocument.php b/app/Services/EDocument/Imports/ZugferdEDocument.php index e685cbe6ad8e..f654708d548a 100644 --- a/app/Services/EDocument/Imports/ZugferdEDocument.php +++ b/app/Services/EDocument/Imports/ZugferdEDocument.php @@ -18,6 +18,7 @@ use App\Models\Country; use App\Models\Currency; use App\Models\Expense; use App\Models\Vendor; +use App\Models\VendorContact; use App\Services\AbstractService; use App\Utils\TempFile; use App\Utils\Traits\SavesDocuments; @@ -46,6 +47,7 @@ class ZugferdEDocument extends AbstractService public function run(): Expense { $user = auth()->user(); + $this->document = ZugferdDocumentReader::readAndGuessFromContent($this->file->get()); $this->document->getDocumentInformation($documentno, $documenttypecode, $documentdate, $invoiceCurrency, $taxCurrency, $documentname, $documentlanguage, $effectiveSpecifiedPeriod); $this->document->getDocumentSummation($grandTotalAmount, $duePayableAmount, $lineTotalAmount, $chargeTotalAmount, $allowanceTotalAmount, $taxBasisTotalAmount, $taxTotalAmount, $roundingAmount, $totalPrepaidAmount); @@ -93,11 +95,19 @@ class ZugferdEDocument extends AbstractService $this->document->getDocumentSellerContact($person_name, $person_department, $contact_phone, $contact_fax, $contact_email); $this->document->getDocumentSellerAddress($address_1, $address_2, $address_3, $postcode, $city, $country, $subdivision); $this->document->getDocumentSellerTaxRegistration($taxtype); + $taxid = null; if (array_key_exists("VA", $taxtype)) { $taxid = $taxtype["VA"]; } - $vendor = Vendor::where('vat_number', $taxid)->first(); + $vendor = Vendor::where("company_id", $user->company()->id)->where('vat_number', $taxid)->first(); + if (!$vendor) { + $vendor_contact = VendorContact::where("company_id", $user->company()->id)->where("email", $contact_email)->first(); + if ($vendor_contact) + $vendor = $vendor_contact->vendor; + } + if (!$vendor) + $vendor = Vendor::where("company_id", $user->company()->id)->where("name", $person_name)->first(); if (!empty($vendor)) { // Vendor found