mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-07-08 13:54:30 -04:00
integrate ParseEDocument to InboundMailEngine
This commit is contained in:
parent
e586455be3
commit
45e2b6aeb8
@ -11,7 +11,6 @@
|
|||||||
|
|
||||||
namespace App\Services\InboundMail;
|
namespace App\Services\InboundMail;
|
||||||
|
|
||||||
use App\Events\Expense\ExpenseWasCreated;
|
|
||||||
use App\Factory\ExpenseFactory;
|
use App\Factory\ExpenseFactory;
|
||||||
use App\Jobs\Util\SystemLogger;
|
use App\Jobs\Util\SystemLogger;
|
||||||
use App\Libraries\MultiDB;
|
use App\Libraries\MultiDB;
|
||||||
@ -19,8 +18,8 @@ use App\Models\ClientContact;
|
|||||||
use App\Models\Company;
|
use App\Models\Company;
|
||||||
use App\Models\SystemLog;
|
use App\Models\SystemLog;
|
||||||
use App\Models\VendorContact;
|
use App\Models\VendorContact;
|
||||||
|
use App\Services\EDocument\Imports\ParseEDocument;
|
||||||
use App\Services\InboundMail\InboundMail;
|
use App\Services\InboundMail\InboundMail;
|
||||||
use App\Utils\Ninja;
|
|
||||||
use App\Utils\TempFile;
|
use App\Utils\TempFile;
|
||||||
use App\Utils\Traits\GeneratesCounter;
|
use App\Utils\Traits\GeneratesCounter;
|
||||||
use App\Utils\Traits\SavesDocuments;
|
use App\Utils\Traits\SavesDocuments;
|
||||||
@ -167,78 +166,69 @@ class InboundMailEngine
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// handle vendor assignment
|
/** @var \App\Models\Expense $expense */
|
||||||
$expense_vendor = $this->getVendor($company, $email);
|
$expense = null;
|
||||||
|
|
||||||
// handle documents
|
// check documents for EDocument xml
|
||||||
$this->processHtmlBodyToDocument($email);
|
|
||||||
|
|
||||||
// handle documents seperatly
|
|
||||||
foreach ($email->documents as $document) {
|
foreach ($email->documents as $document) {
|
||||||
|
|
||||||
/** @var \App\Models\Expense $expense */
|
// check if document can be parsed to an expense
|
||||||
$expense = null;
|
try {
|
||||||
|
|
||||||
// TODO: check if document is a ZugferdEDocument and can be handled that way
|
$expense_obj = (new ParseEDocument($document->get(), $document->getFilename()))->run();
|
||||||
if ($document->extension() === 'pdf' || $document->extension() === 'xml') {
|
|
||||||
try {
|
|
||||||
|
|
||||||
$expense = (new ZugferdEDocument($document->get()))->run();
|
// throw error, when multiple parseable files are registered
|
||||||
|
if ($expense && $expense_obj)
|
||||||
|
throw new \Exception('Multiple parseable Invoice documents found in email. Please use only one Invoice document per email.');
|
||||||
|
|
||||||
} catch (\Exception $err) {
|
$expense = $expense_obj;
|
||||||
|
|
||||||
// throw error, when its not the DocumentNotFoundError
|
} catch (\Exception $err) {
|
||||||
if (!($exception instanceof \horstoeko\zugferd\exception\ZugferdFileNotFoundException)) {
|
// throw error, only, when its not expected
|
||||||
nlog("An error occured while processing InboundEmail document with ZugferdEDocument: {$err}");
|
switch (true) {
|
||||||
|
case ($err->getMessage() === 'E-Invoice standard not supported'):
|
||||||
|
case ($err->getMessage() === 'File type not supported'):
|
||||||
|
break;
|
||||||
|
default:
|
||||||
throw $err;
|
throw $err;
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
// save additional context of the email to the document
|
|
||||||
if ($expense) {
|
|
||||||
|
|
||||||
if (!$expense->vendor_id && $expense_vendor)
|
|
||||||
$expense->vendor_id = $expense_vendor->id;
|
|
||||||
|
|
||||||
$documents = [];
|
|
||||||
if ($email->body_document !== null)
|
|
||||||
array_push($documents, $email->body_document);
|
|
||||||
|
|
||||||
$expense->saveQuietly();
|
|
||||||
|
|
||||||
$this->saveDocuments($documents, $expense);
|
|
||||||
|
|
||||||
continue;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: check if document can be handled by OCR solution
|
}
|
||||||
|
|
||||||
// create expense just from document
|
// populate missing data with data from email
|
||||||
|
if (!$expense)
|
||||||
$expense = ExpenseFactory::create($company->id, $company->owner()->id);
|
$expense = ExpenseFactory::create($company->id, $company->owner()->id);
|
||||||
|
|
||||||
|
if (!$expense->public_notes)
|
||||||
$expense->public_notes = $email->subject;
|
$expense->public_notes = $email->subject;
|
||||||
|
|
||||||
|
if (!$expense->private_notes)
|
||||||
$expense->private_notes = $email->text_body;
|
$expense->private_notes = $email->text_body;
|
||||||
|
|
||||||
|
if (!$expense->date)
|
||||||
$expense->date = $email->date;
|
$expense->date = $email->date;
|
||||||
|
|
||||||
|
if (!$expense->vendor_id) {
|
||||||
|
$expense_vendor = $this->getVendor($company, $email);
|
||||||
|
|
||||||
if ($expense_vendor)
|
if ($expense_vendor)
|
||||||
$expense->vendor_id = $expense_vendor->id;
|
$expense->vendor_id = $expense_vendor->id;
|
||||||
|
|
||||||
$documents = [];
|
|
||||||
array_push($documents, $document);
|
|
||||||
if ($email->body_document !== null)
|
|
||||||
array_push($documents, $email->body_document);
|
|
||||||
|
|
||||||
$expense->saveQuietly();
|
|
||||||
|
|
||||||
$this->saveDocuments($documents, $expense);
|
|
||||||
|
|
||||||
event(new ExpenseWasCreated($expense, $expense->company, Ninja::eventVars(null))); // @turbo124 please check, I copied from API-Controller
|
|
||||||
event('eloquent.created: App\Models\Expense', $expense); // @turbo124 please check, I copied from API-Controller
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// handle documents
|
||||||
|
$documents = [];
|
||||||
|
array_push($documents, $document);
|
||||||
|
|
||||||
|
// handle email document
|
||||||
|
$this->processHtmlBodyToDocument($email);
|
||||||
|
if ($email->body_document !== null)
|
||||||
|
array_push($documents, $email->body_document);
|
||||||
|
|
||||||
|
$expense->saveQuietly();
|
||||||
|
|
||||||
|
$this->saveDocuments($documents, $expense);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// HELPERS
|
// HELPERS
|
||||||
|
Loading…
x
Reference in New Issue
Block a user