From a31e810ec9968e6ba182ab47d29e7d86278a155e Mon Sep 17 00:00:00 2001 From: paulwer Date: Mon, 24 Jun 2024 16:30:00 +0200 Subject: [PATCH] fixes from tests --- app/Http/Controllers/ExpenseController.php | 5 ++-- .../EDocument/Imports/MindeeEDocument.php | 29 +++++++++++++++---- .../EDocument/Imports/ParseEDocument.php | 2 +- 3 files changed, 26 insertions(+), 10 deletions(-) diff --git a/app/Http/Controllers/ExpenseController.php b/app/Http/Controllers/ExpenseController.php index 44444990de0e..091eb519ca5d 100644 --- a/app/Http/Controllers/ExpenseController.php +++ b/app/Http/Controllers/ExpenseController.php @@ -587,10 +587,9 @@ class ExpenseController extends BaseController public function edocument(EDocumentRequest $request): string { if ($request->hasFile("documents")) { - return (new ImportEDocument($request->file("documents")[0]->get(), $request->file("documents")[0]->getClientOriginalName(), $request->file("documents")[0]->getMimeType()))->handle(); - } else { - return "No file found"; + return (new ImportEDocument($request->file("documents")->get(), $request->file("documents")->getClientOriginalName(), $request->file("documents")->getMimeType()))->handle(); } + return "No file found"; } } diff --git a/app/Services/EDocument/Imports/MindeeEDocument.php b/app/Services/EDocument/Imports/MindeeEDocument.php index 7e5245337998..d73c7546c97b 100644 --- a/app/Services/EDocument/Imports/MindeeEDocument.php +++ b/app/Services/EDocument/Imports/MindeeEDocument.php @@ -12,11 +12,13 @@ namespace App\Services\EDocument\Imports; use App\Factory\ExpenseFactory; +use App\Factory\VendorContactFactory; use App\Factory\VendorFactory; 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; @@ -52,14 +54,14 @@ class MindeeEDocument extends AbstractService // perform parsing $mindeeClient = new Client($api_key); - $inputSource = $mindeeClient->sourceFromFile($this->file); + $inputSource = $mindeeClient->sourceFromBytes($this->file->get(), $this->file->getClientOriginalName()); $result = $mindeeClient->parse(InvoiceV4::class, $inputSource); $this->incrementRequestCounts(); /** @var \Mindee\Product\Invoice\InvoiceV4Document $prediction */ $prediction = $result->document->inference->prediction; - if ($prediction->documentType !== 'INVOICE') + if ($prediction->documentType->value !== 'INVOICE') throw new Exception('Unsupported document type'); $grandTotalAmount = $prediction->totalAmount->value; @@ -95,15 +97,20 @@ class MindeeEDocument extends AbstractService $counter++; } - $vendor = Vendor::where('email', $prediction->supplierEmail)->first(); + $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 (!empty($vendor)) { + if ($vendor) + $vendor = Vendor::where("company_id", $user->company()->id)->where("name", $prediction->supplierName)->first(); + + if ($vendor) { // Vendor found $expense->vendor_id = $vendor->id; } else { $vendor = VendorFactory::create($user->company()->id, $user->id); $vendor->name = $prediction->supplierName; - $vendor->email = $prediction->supplierEmail; $vendor->currency_id = Currency::whereCode($invoiceCurrency)->first()?->id; $vendor->phone = $prediction->supplierPhoneNumber; @@ -111,9 +118,19 @@ class MindeeEDocument extends AbstractService // $vendor->address2 = $address_2; // $vendor->city = $city; // $vendor->postal_code = $postcode; - $vendor->country_id = Country::where('iso_3166_2', $country)->first()?->id || Country::where('iso_3166_3', $country)->first()?->id || null; // could be 2 or 3 length + $country = Country::where('iso_3166_2', $country)->first()?->id || Country::where('iso_3166_3', $country)->first()?->id || null; + if ($country) + $vendor->country_id = Country::where('iso_3166_2', $country)->first()?->id || Country::where('iso_3166_3', $country)->first()?->id || null; // could be 2 or 3 length $vendor->save(); + + if ($prediction->supplierEmail) { + $vendor_contact = VendorContactFactory::create($user->company()->id, $user->id); + $vendor_contact->vendor_id = $vendor->id; + $vendor_contact->email = $prediction->supplierEmail; + $vendor_contact->save(); + } + $expense->vendor_id = $vendor->id; } $expense->transaction_reference = $documentno; diff --git a/app/Services/EDocument/Imports/ParseEDocument.php b/app/Services/EDocument/Imports/ParseEDocument.php index 2c1e74e85487..647c6b5b52b6 100644 --- a/app/Services/EDocument/Imports/ParseEDocument.php +++ b/app/Services/EDocument/Imports/ParseEDocument.php @@ -71,7 +71,7 @@ class ParseEDocument extends AbstractService if ($zugferd_exception) nlog("Zugferd Exception: " . $zugferd_exception->getMessage()); if ($mindee_exception) - nlog("Mindee Exception: " . $zugferd_exception->getMessage()); + nlog("Mindee Exception: " . $mindee_exception->getMessage()); throw new Exception("File type not supported or issue while parsing"); } }