From e3da9914ba66dba493ab282a507ffd4313853c28 Mon Sep 17 00:00:00 2001 From: paulwer Date: Sun, 23 Jun 2024 09:32:51 +0200 Subject: [PATCH] rework mindee limits --- .../EDocument/Imports/MindeeEDocument.php | 61 ++++++++++++------- .../EDocument/Imports/ZugferdEDocument.php | 3 +- config/services.php | 7 ++- 3 files changed, 45 insertions(+), 26 deletions(-) diff --git a/app/Services/EDocument/Imports/MindeeEDocument.php b/app/Services/EDocument/Imports/MindeeEDocument.php index e352bf6df39d..7e5245337998 100644 --- a/app/Services/EDocument/Imports/MindeeEDocument.php +++ b/app/Services/EDocument/Imports/MindeeEDocument.php @@ -20,6 +20,7 @@ use App\Models\Vendor; use App\Services\AbstractService; use App\Utils\TempFile; use App\Utils\Traits\SavesDocuments; +use Cache; use Exception; use Mindee\Client; use Mindee\Product\Invoice\InvoiceV4; @@ -45,22 +46,15 @@ class MindeeEDocument extends AbstractService $user = auth()->user(); $api_key = config('services.mindee.api_key'); - if (!$api_key) throw new Exception('Mindee API key not configured'); + $this->checkLimits(); - // check global contingent - // TODO: add contingent for each company - - + // perform parsing $mindeeClient = new Client($api_key); - - - // Load a file from disk $inputSource = $mindeeClient->sourceFromFile($this->file); - - // Parse the file $result = $mindeeClient->parse(InvoiceV4::class, $inputSource); + $this->incrementRequestCounts(); /** @var \Mindee\Product\Invoice\InvoiceV4Document $prediction */ $prediction = $result->document->inference->prediction; @@ -86,12 +80,12 @@ class MindeeEDocument extends AbstractService $expense->currency_id = Currency::whereCode($invoiceCurrency)->first()->id; $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(); - // if ($taxCurrency && $taxCurrency != $invoiceCurrency) { - // $expense->private_notes = ctrans("texts.tax_currency_mismatch"); - // } $expense->uses_inclusive_taxes = True; $expense->amount = $grandTotalAmount; $counter = 1; @@ -100,10 +94,7 @@ class MindeeEDocument extends AbstractService $expense->{"tax_rate$counter"} = $taxesElem->rate; $counter++; } - $taxid = null; - if (array_key_exists("VA", $taxtype)) { - $taxid = $taxtype["VA"]; - } + $vendor = Vendor::where('email', $prediction->supplierEmail)->first(); if (!empty($vendor)) { @@ -114,13 +105,13 @@ class MindeeEDocument extends AbstractService $vendor->name = $prediction->supplierName; $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->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->city = $city; // $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(); $expense->vendor_id = $vendor->id; @@ -135,5 +126,33 @@ class MindeeEDocument extends AbstractService $expense->save(); 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); + } } diff --git a/app/Services/EDocument/Imports/ZugferdEDocument.php b/app/Services/EDocument/Imports/ZugferdEDocument.php index 21a6388ff3ce..36fe1b77d124 100644 --- a/app/Services/EDocument/Imports/ZugferdEDocument.php +++ b/app/Services/EDocument/Imports/ZugferdEDocument.php @@ -69,8 +69,7 @@ class ZugferdEDocument extends AbstractService $expense->currency_id = Currency::whereCode($invoiceCurrency)->first()->id; $expense->save(); - $documents = []; - array_push($documents, $this->file); + $documents = [$this->file]; if ($this->file->getExtension() == "xml") array_push($documents, TempFile::UploadedFileFromRaw($visualizer->renderPdf(), $documentno . "_visualiser.pdf", "application/pdf")); $this->saveDocuments($documents, $expense); diff --git a/config/services.php b/config/services.php index 48e8b75f4769..f7f2d0ecea44 100644 --- a/config/services.php +++ b/config/services.php @@ -53,9 +53,10 @@ return [ 'mindee' => [ 'api_key' => env('MINDEE_API_KEY'), - 'global_contingent_month' => env('MINDEE_GLOBAL_CONTINGENT_MONTH', 1000), - 'company_contingent_month' => env('MINDEE_COMPANY_CONTINGENT_MONTH', 500), - 'company_contingent_month_enterprise' => env('MINDEE_COMPANY_CONTINGENT_MONTH', 500), + 'daily_limit' => env('MINDEE_DAILY_LIMIT', 100), + 'monthly_limit' => env('MINDEE_MONTHLY_LIMIT', 250), + 'account_daily_limit' => env('MINDEE_ACCOUNT_DAILY_LIMIT', 0), + 'account_monthly_limit' => env('MINDEE_ACCOUNT_MONTHLY_LIMIT', 0), ], 'apple' => [