2024-06-22 15:02:53 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Services\EDocument\Imports;
|
|
|
|
|
2024-08-28 07:02:43 +02:00
|
|
|
use App\Models\Company;
|
2024-06-22 15:02:53 +02:00
|
|
|
use App\Models\Expense;
|
|
|
|
use App\Services\AbstractService;
|
2024-06-24 17:40:27 +02:00
|
|
|
use App\Utils\Ninja;
|
2024-06-22 15:02:53 +02:00
|
|
|
use Exception;
|
2024-06-22 18:52:25 +02:00
|
|
|
use Illuminate\Http\UploadedFile;
|
2024-06-22 15:02:53 +02:00
|
|
|
|
|
|
|
class ParseEDocument extends AbstractService
|
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
2024-08-28 07:02:43 +02:00
|
|
|
public function __construct(private UploadedFile $file, private Company $company)
|
2024-06-22 15:02:53 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the service.
|
2024-06-22 18:52:25 +02:00
|
|
|
* the service will parse the file with all available libraries of the system and will return an expense, when possible
|
2024-06-22 15:02:53 +02:00
|
|
|
*
|
2024-06-26 19:05:14 +02:00
|
|
|
* @developer the function should be implemented with local first aproach to save costs of external providers (like mindee ocr)
|
|
|
|
*
|
2024-06-22 15:02:53 +02:00
|
|
|
* @return Expense
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
public function run(): Expense
|
|
|
|
{
|
2024-06-22 18:52:25 +02:00
|
|
|
|
2024-06-25 05:48:16 +02:00
|
|
|
/** @var \App\Models\Account $account */
|
2024-08-28 07:02:43 +02:00
|
|
|
$account = $this->company->owner()->account;
|
2024-06-25 05:48:16 +02:00
|
|
|
|
2024-08-28 09:29:11 +02:00
|
|
|
$extension = $this->file->getClientOriginalExtension() ?: $this->file->getExtension();
|
|
|
|
$mimetype = $this->file->getClientMimeType() ?: $$this->file->getMimeType();
|
|
|
|
|
2024-06-26 19:05:14 +02:00
|
|
|
// ZUGFERD - try to parse via Zugferd lib
|
2024-06-26 19:24:57 +02:00
|
|
|
switch (true) {
|
2024-08-28 09:29:11 +02:00
|
|
|
case ($extension == 'pdf' || $mimetype == 'application/pdf'):
|
|
|
|
case ($extension == 'xml' || $mimetype == 'application/xml') && stristr($this->file->get(), "urn:cen.eu:en16931:2017"):
|
|
|
|
case ($extension == 'xml' || $mimetype == 'application/xml') && stristr($this->file->get(), "urn:cen.eu:en16931:2017#compliant#urn:xeinkauf.de:kosit:xrechnung_3.0"):
|
|
|
|
case ($extension == 'xml' || $mimetype == 'application/xml') && stristr($this->file->get(), "urn:cen.eu:en16931:2017#compliant#urn:xeinkauf.de:kosit:xrechnung_2.1"):
|
|
|
|
case ($extension == 'xml' || $mimetype == 'application/xml') && stristr($this->file->get(), "urn:cen.eu:en16931:2017#compliant#urn:xeinkauf.de:kosit:xrechnung_2.0"):
|
2024-06-26 19:24:57 +02:00
|
|
|
try {
|
2024-08-28 07:02:43 +02:00
|
|
|
return (new ZugferdEDocument($this->file, $this->company))->run();
|
2024-06-26 19:24:57 +02:00
|
|
|
} catch (Exception $e) {
|
2024-09-02 05:07:01 +02:00
|
|
|
nlog($this->file->get());
|
2024-06-26 19:24:57 +02:00
|
|
|
nlog("Zugferd Exception: " . $e->getMessage());
|
|
|
|
}
|
2024-06-22 18:52:25 +02:00
|
|
|
}
|
|
|
|
|
2024-06-26 19:05:14 +02:00
|
|
|
// MINDEE OCR - try to parse via mindee external service
|
2024-06-30 13:07:09 +02:00
|
|
|
if (config('services.mindee.api_key') && !(Ninja::isHosted() && !($account->isPaid() && $account->plan == 'enterprise')))
|
2024-08-28 09:29:11 +02:00
|
|
|
switch (true) {
|
|
|
|
case ($extension == 'pdf' || $mimetype == 'application/pdf'):
|
|
|
|
case ($extension == 'heic' || $extension == 'heic' || $extension == 'png' || $extension == 'jpg' || $extension == 'jpeg' || $extension == 'webp' || str_starts_with($mimetype, 'image/')):
|
|
|
|
try {
|
|
|
|
return (new MindeeEDocument($this->file, $this->company))->run();
|
|
|
|
} catch (Exception $e) {
|
|
|
|
if (!($e->getMessage() == 'Unsupported document type'))
|
|
|
|
nlog("Mindee Exception: " . $e->getMessage());
|
|
|
|
}
|
2024-06-24 17:40:27 +02:00
|
|
|
}
|
2024-06-22 18:52:25 +02:00
|
|
|
|
2024-06-26 19:24:57 +02:00
|
|
|
// NO PARSER OR ERROR
|
2024-06-26 17:34:32 +02:00
|
|
|
throw new Exception("File type not supported or issue while parsing", 409);
|
2024-06-22 15:02:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|