rework mindee limits

This commit is contained in:
paulwer 2024-06-23 09:32:51 +02:00
parent aa12fe3976
commit e3da9914ba
3 changed files with 45 additions and 26 deletions

View File

@ -20,6 +20,7 @@ use App\Models\Vendor;
use App\Services\AbstractService; use App\Services\AbstractService;
use App\Utils\TempFile; use App\Utils\TempFile;
use App\Utils\Traits\SavesDocuments; use App\Utils\Traits\SavesDocuments;
use Cache;
use Exception; use Exception;
use Mindee\Client; use Mindee\Client;
use Mindee\Product\Invoice\InvoiceV4; use Mindee\Product\Invoice\InvoiceV4;
@ -45,22 +46,15 @@ class MindeeEDocument extends AbstractService
$user = auth()->user(); $user = auth()->user();
$api_key = config('services.mindee.api_key'); $api_key = config('services.mindee.api_key');
if (!$api_key) if (!$api_key)
throw new Exception('Mindee API key not configured'); throw new Exception('Mindee API key not configured');
$this->checkLimits();
// check global contingent // perform parsing
// TODO: add contingent for each company
$mindeeClient = new Client($api_key); $mindeeClient = new Client($api_key);
// Load a file from disk
$inputSource = $mindeeClient->sourceFromFile($this->file); $inputSource = $mindeeClient->sourceFromFile($this->file);
// Parse the file
$result = $mindeeClient->parse(InvoiceV4::class, $inputSource); $result = $mindeeClient->parse(InvoiceV4::class, $inputSource);
$this->incrementRequestCounts();
/** @var \Mindee\Product\Invoice\InvoiceV4Document $prediction */ /** @var \Mindee\Product\Invoice\InvoiceV4Document $prediction */
$prediction = $result->document->inference->prediction; $prediction = $result->document->inference->prediction;
@ -86,12 +80,12 @@ class MindeeEDocument extends AbstractService
$expense->currency_id = Currency::whereCode($invoiceCurrency)->first()->id; $expense->currency_id = Currency::whereCode($invoiceCurrency)->first()->id;
$expense->save(); $expense->save();
$this->saveDocument($this->file, $expense); $this->saveDocuments([
$this->file,
TempFile::UploadedFileFromRaw(strval($result->document), $documentno . "_mindee_orc_result.txt", "text/plain")
], $expense);
$expense->saveQuietly(); $expense->saveQuietly();
// if ($taxCurrency && $taxCurrency != $invoiceCurrency) {
// $expense->private_notes = ctrans("texts.tax_currency_mismatch");
// }
$expense->uses_inclusive_taxes = True; $expense->uses_inclusive_taxes = True;
$expense->amount = $grandTotalAmount; $expense->amount = $grandTotalAmount;
$counter = 1; $counter = 1;
@ -100,10 +94,7 @@ class MindeeEDocument extends AbstractService
$expense->{"tax_rate$counter"} = $taxesElem->rate; $expense->{"tax_rate$counter"} = $taxesElem->rate;
$counter++; $counter++;
} }
$taxid = null;
if (array_key_exists("VA", $taxtype)) {
$taxid = $taxtype["VA"];
}
$vendor = Vendor::where('email', $prediction->supplierEmail)->first(); $vendor = Vendor::where('email', $prediction->supplierEmail)->first();
if (!empty($vendor)) { if (!empty($vendor)) {
@ -114,13 +105,13 @@ class MindeeEDocument extends AbstractService
$vendor->name = $prediction->supplierName; $vendor->name = $prediction->supplierName;
$vendor->email = $prediction->supplierEmail; $vendor->email = $prediction->supplierEmail;
$vendor->currency_id = Currency::whereCode($invoiceCurrency)->first()->id; $vendor->currency_id = Currency::whereCode($invoiceCurrency)->first()?->id;
$vendor->phone = $prediction->supplierPhoneNumber; $vendor->phone = $prediction->supplierPhoneNumber;
// $vendor->address1 = $address_1; // TODO: we only have the full address string // $vendor->address1 = $address_1; // TODO: we only have the full address string from mindee returned
// $vendor->address2 = $address_2; // $vendor->address2 = $address_2;
// $vendor->city = $city; // $vendor->city = $city;
// $vendor->postal_code = $postcode; // $vendor->postal_code = $postcode;
$vendor->country_id = Country::where('iso_3166_2', $country)->first()->id || Country::where('iso_3166_3', $country)->first()->id; // could be 2 or 3 length $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(); $vendor->save();
$expense->vendor_id = $vendor->id; $expense->vendor_id = $vendor->id;
@ -135,5 +126,33 @@ class MindeeEDocument extends AbstractService
$expense->save(); $expense->save();
return $expense; return $expense;
} }
private function checkLimits()
{
$user = auth()->user();
Cache::add('mindeeTotalDailyRequests', 0, now()->endOfDay());
Cache::add('mindeeTotalMonthlyRequests', 0, now()->endOfMonth());
Cache::add('mindeeAccountDailyRequests' . $user->company->account->id, 0, now()->endOfDay());
Cache::add('mindeeAccountMonthlyRequests' . $user->company->account->id, 0, now()->endOfMonth());
if (config('services.mindee.daily_limit') != 0 && Cache::get('mindeeTotalDailyRequests') > config('services.mindee.daily_limit'))
throw new Exception('Mindee daily limit reached');
if (config('services.mindee.monthly_limit') != 0 && Cache::get('mindeeTotalMonthlyRequests') > config('services.mindee.monthly_limit'))
throw new Exception('Mindee monthly limit reached');
if (config('services.mindee.account_daily_limit') != 0 && Cache::get('mindeeAccountDailyRequests' . $user->company->account->id) > config('services.mindee.account_daily_limit'))
throw new Exception('Mindee daily limit reached for account: ' . $user->company->account->id);
if (config('services.mindee.account_monthly_limit') != 0 && Cache::get('mindeeAccountMonthlyRequests' . $user->company->account->id) > config('services.mindee.account_monthly_limit'))
throw new Exception('Mindee monthly limit reached for account: ' . $user->company->account->id);
}
private function incrementRequestCounts()
{
$user = auth()->user();
Cache::increment('mindeeTotalDailyRequests');
Cache::increment('mindeeTotalMonthlyRequests');
Cache::increment('mindeeAccountDailyRequests' . $user->company->account->id);
Cache::increment('mindeeAccountMonthlyRequests' . $user->company->account->id);
}
} }

View File

@ -69,8 +69,7 @@ class ZugferdEDocument extends AbstractService
$expense->currency_id = Currency::whereCode($invoiceCurrency)->first()->id; $expense->currency_id = Currency::whereCode($invoiceCurrency)->first()->id;
$expense->save(); $expense->save();
$documents = []; $documents = [$this->file];
array_push($documents, $this->file);
if ($this->file->getExtension() == "xml") if ($this->file->getExtension() == "xml")
array_push($documents, TempFile::UploadedFileFromRaw($visualizer->renderPdf(), $documentno . "_visualiser.pdf", "application/pdf")); array_push($documents, TempFile::UploadedFileFromRaw($visualizer->renderPdf(), $documentno . "_visualiser.pdf", "application/pdf"));
$this->saveDocuments($documents, $expense); $this->saveDocuments($documents, $expense);

View File

@ -53,9 +53,10 @@ return [
'mindee' => [ 'mindee' => [
'api_key' => env('MINDEE_API_KEY'), 'api_key' => env('MINDEE_API_KEY'),
'global_contingent_month' => env('MINDEE_GLOBAL_CONTINGENT_MONTH', 1000), 'daily_limit' => env('MINDEE_DAILY_LIMIT', 100),
'company_contingent_month' => env('MINDEE_COMPANY_CONTINGENT_MONTH', 500), 'monthly_limit' => env('MINDEE_MONTHLY_LIMIT', 250),
'company_contingent_month_enterprise' => env('MINDEE_COMPANY_CONTINGENT_MONTH', 500), 'account_daily_limit' => env('MINDEE_ACCOUNT_DAILY_LIMIT', 0),
'account_monthly_limit' => env('MINDEE_ACCOUNT_MONTHLY_LIMIT', 0),
], ],
'apple' => [ 'apple' => [