1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-15 23:52:33 +01:00
invoiceninja/app/Services/EDocument/Imports/ParseEDocument.php

80 lines
2.8 KiB
PHP
Raw Normal View History

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;
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-06-22 18:52:25 +02:00
public function __construct(private UploadedFile $file)
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 */
$account = auth()->user()->account;
2024-06-26 19:05:14 +02:00
// ZUGFERD - try to parse via Zugferd lib
2024-06-22 18:52:25 +02:00
$zugferd_exception = null;
try {
2024-06-23 09:36:54 +02:00
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"):
2024-06-25 05:50:28 +02:00
return (new ZugferdEDocument($this->file))->run();
2024-06-23 09:36:54 +02:00
}
2024-06-22 18:52:25 +02:00
} catch (Exception $e) {
$zugferd_exception = $e;
}
2024-06-26 19:05:14 +02:00
// MINDEE OCR - try to parse via mindee external service
2024-06-22 18:52:25 +02:00
$mindee_exception = null;
2024-06-25 05:48:16 +02:00
if (config('services.mindee.api_key') && (Ninja::isSelfHost() || (Ninja::isHosted() && $account->isPaid() && $account->plan == 'enterprise')))
2024-06-24 17:40:27 +02:00
try {
2024-06-25 05:50:28 +02:00
return (new MindeeEDocument($this->file))->run();
2024-06-24 17:40:27 +02:00
} catch (Exception $e) {
// ignore not available exceptions
$mindee_exception = $e;
}
2024-06-22 18:52:25 +02:00
// log exceptions and throw error
if ($zugferd_exception)
nlog("Zugferd Exception: " . $zugferd_exception->getMessage());
if ($mindee_exception)
2024-06-24 16:30:00 +02:00
nlog("Mindee Exception: " . $mindee_exception->getMessage());
throw new Exception("File type not supported or issue while parsing", 409);
2024-06-22 15:02:53 +02:00
}
}