wrong implementation use each document for its own expense (or parse)

This commit is contained in:
paulwer 2024-06-22 17:55:07 +02:00
parent d0cad92fce
commit 46f3fd3866

View File

@ -166,22 +166,29 @@ class InboundMailEngine
return; return;
} }
/** @var \App\Models\Expense $expense */ // prepare data
$expense = null; $expense_vendor = $this->getVendor($company, $email);
$this->processHtmlBodyToDocument($email);
// check documents for EDocument xml $parsed_expense_ids = []; // used to check if an expense was already matched within this job
// check documents => optimal when parsed from any source => else create an expense for each document
foreach ($email->documents as $document) { foreach ($email->documents as $document) {
/** @var \App\Models\Expense $expense */
$expense = null;
// check if document can be parsed to an expense // check if document can be parsed to an expense
try { try {
$expense_obj = (new ParseEDocument($document->get(), $document->getFilename()))->run(); $expense = (new ParseEDocument($document->get(), $document->getFilename()))->run();
// throw error, when multiple parseable files are registered // check if expense was already matched within this job and skip if true
if ($expense && $expense_obj) if (array_search($expense->id, $parsed_expense_ids)) {
throw new \Exception('Multiple parseable Invoice documents found in email. Please use only one Invoice document per email.'); $this->saveDocument($document, $expense);
continue;
$expense = $expense_obj; }
array_push($parsed_expenses, $expense->id);
} catch (\Exception $err) { } catch (\Exception $err) {
// throw error, only, when its not expected // throw error, only, when its not expected
@ -194,41 +201,35 @@ class InboundMailEngine
} }
} }
} // populate missing data with data from email
if (!$expense)
$expense = ExpenseFactory::create($company->id, $company->owner()->id);
// populate missing data with data from email if (!$expense->public_notes)
if (!$expense) $expense->public_notes = $email->subject;
$expense = ExpenseFactory::create($company->id, $company->owner()->id);
if (!$expense->public_notes) if (!$expense->private_notes)
$expense->public_notes = $email->subject; $expense->private_notes = $email->text_body;
if (!$expense->private_notes) if (!$expense->date)
$expense->private_notes = $email->text_body; $expense->date = $email->date;
if (!$expense->date) if (!$expense->vendor_id && $expense_vendor)
$expense->date = $email->date;
if (!$expense->vendor_id) {
$expense_vendor = $this->getVendor($company, $email);
if ($expense_vendor)
$expense->vendor_id = $expense_vendor->id; $expense->vendor_id = $expense_vendor->id;
// handle documents
$documents = [];
array_push($documents, $document);
// handle email document
if ($email->body_document !== null)
array_push($documents, $email->body_document);
$expense->saveQuietly();
$this->saveDocuments($documents, $expense);
} }
// 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