Refactor error handling and enhance API interactions across Filament pages

- Introduced `ApiErrorTranslator` to normalize and translate API error messages, providing clearer feedback in French.
- Updated all Filament pages (Articles, Documents, Divers, Journaux, Tiers, TablesExplorer) to utilize the new error translation mechanism, improving user experience during API interactions.
- Added validation for required fields before API calls, ensuring users receive immediate feedback when mandatory inputs are missing.
- Implemented tracking properties to distinguish between "never searched" and "searched without results," enhancing the user interface.
- Removed the obsolete `$results` property from the Articles page and added a new `$barcode` property to align with API requirements.
- Updated documentation to reflect changes in API behavior and error handling, including new metadata returned by the `art_list` endpoint.
- Added new tests to verify the functionality of the barcode handling and validation logic.
This commit is contained in:
2026-02-23 10:15:17 +01:00
parent 7df94b64fa
commit bb1bbc2904
29 changed files with 1075 additions and 157 deletions

View File

@@ -4,6 +4,7 @@ namespace App\Filament\Pages;
use App\Exceptions\LogisticsApiException;
use App\Services\LogisticsService;
use App\Support\ApiErrorTranslator;
use Filament\Pages\Page;
use Filament\Support\Icons\Heroicon;
@@ -23,9 +24,9 @@ class Articles extends Page
public string $search = '';
public string $select = 'artid,artname';
public string $select = 'artid,name1';
public int $results = 10;
public string $barcode = '';
public string $stockArticleId = '';
@@ -37,48 +38,62 @@ class Articles extends Page
public ?string $errorMessage = null;
public bool $hasSearched = false;
public bool $hasCheckedStock = false;
public function searchArticles(): void
{
$this->errorMessage = null;
$this->hasSearched = true;
try {
$service = app(LogisticsService::class);
$params = array_filter([
'select' => $this->select,
'results' => $this->results,
'search' => $this->search,
'barcode' => $this->barcode,
]);
$response = $service->artList($params);
$this->data = $response['data'] ?? [];
$this->metadata = $response['metadata'] ?? null;
$this->errorMessage = $response['error'] ?? null;
$this->errorMessage = ApiErrorTranslator::translate($response['error'] ?? null);
} catch (LogisticsApiException $e) {
$this->errorMessage = $e->getMessage();
$this->errorMessage = ApiErrorTranslator::translate($e->getMessage());
$this->data = [];
} catch (\Throwable $e) {
$this->errorMessage = "Erreur inattendue : {$e->getMessage()}";
$this->errorMessage = ApiErrorTranslator::translate($e->getMessage());
$this->data = [];
}
}
public function getStock(): void
{
$this->errorMessage = null;
if (blank($this->stockArticleId)) {
$this->errorMessage = 'Le champ identifiant article (ARTID) est obligatoire pour verifier le stock.';
return;
}
$this->hasCheckedStock = true;
try {
$service = app(LogisticsService::class);
$response = $service->artGetStock($this->stockArticleId);
$this->stockData = $response['data'] ?? [];
$this->errorMessage = $response['error'] ?? null;
$this->errorMessage = ApiErrorTranslator::translate($response['error'] ?? null);
} catch (LogisticsApiException $e) {
$this->errorMessage = $e->getMessage();
$this->errorMessage = ApiErrorTranslator::translate($e->getMessage());
$this->stockData = [];
} catch (\Throwable $e) {
$this->errorMessage = "Erreur inattendue : {$e->getMessage()}";
$this->errorMessage = ApiErrorTranslator::translate($e->getMessage());
$this->stockData = [];
}
}

View File

@@ -4,6 +4,7 @@ namespace App\Filament\Pages;
use App\Exceptions\LogisticsApiException;
use App\Services\LogisticsService;
use App\Support\ApiErrorTranslator;
use Filament\Pages\Page;
use Filament\Support\Icons\Heroicon;
@@ -46,50 +47,72 @@ class Divers extends Page
public ?string $errorMessage = null;
// Tracking
public bool $hasSerial = false;
public bool $hasCodes = false;
public bool $hasUpdatedStock = false;
public function getSerialNumber(): void
{
$this->errorMessage = null;
$this->hasSerial = true;
try {
$service = app(LogisticsService::class);
$response = $service->getSerialNumber();
$this->serialData = is_array($response['data'] ?? null) ? $response['data'] : ['value' => $response['data'] ?? null];
$this->errorMessage = $response['error'] ?? null;
$this->errorMessage = ApiErrorTranslator::translate($response['error'] ?? null);
} catch (LogisticsApiException $e) {
$this->errorMessage = $e->getMessage();
$this->errorMessage = ApiErrorTranslator::translate($e->getMessage());
$this->serialData = [];
} catch (\Throwable $e) {
$this->errorMessage = "Erreur inattendue : {$e->getMessage()}";
$this->errorMessage = ApiErrorTranslator::translate($e->getMessage());
$this->serialData = [];
}
}
public function searchCodes(): void
{
$this->errorMessage = null;
if (blank($this->code)) {
$this->errorMessage = 'Le champ debut de code (code) est obligatoire.';
return;
}
$this->hasCodes = true;
try {
$service = app(LogisticsService::class);
$response = $service->codesList(['code' => $this->code]);
$this->codesData = $response['data'] ?? [];
$this->errorMessage = $response['error'] ?? null;
$this->errorMessage = ApiErrorTranslator::translate($response['error'] ?? null);
} catch (LogisticsApiException $e) {
$this->errorMessage = $e->getMessage();
$this->errorMessage = ApiErrorTranslator::translate($e->getMessage());
$this->codesData = [];
} catch (\Throwable $e) {
$this->errorMessage = "Erreur inattendue : {$e->getMessage()}";
$this->errorMessage = ApiErrorTranslator::translate($e->getMessage());
$this->codesData = [];
}
}
public function updateStock(): void
{
$this->errorMessage = null;
if (blank($this->stkArtId) || blank($this->stkId) || blank($this->stkQty)) {
$this->errorMessage = 'Les champs ARTID, STKID et QTY sont obligatoires.';
return;
}
$this->hasUpdatedStock = true;
try {
$service = app(LogisticsService::class);
@@ -105,12 +128,12 @@ class Divers extends Page
$response = $service->customGeninvUpdatestock($params);
$this->updateStockResult = $response['data'] ?? [];
$this->errorMessage = $response['error'] ?? null;
$this->errorMessage = ApiErrorTranslator::translate($response['error'] ?? null);
} catch (LogisticsApiException $e) {
$this->errorMessage = $e->getMessage();
$this->errorMessage = ApiErrorTranslator::translate($e->getMessage());
$this->updateStockResult = [];
} catch (\Throwable $e) {
$this->errorMessage = "Erreur inattendue : {$e->getMessage()}";
$this->errorMessage = ApiErrorTranslator::translate($e->getMessage());
$this->updateStockResult = [];
}
}

View File

@@ -4,6 +4,7 @@ namespace App\Filament\Pages;
use App\Exceptions\LogisticsApiException;
use App\Services\LogisticsService;
use App\Support\ApiErrorTranslator;
use Filament\Pages\Page;
use Filament\Support\Icons\Heroicon;
@@ -117,8 +118,30 @@ class Documents extends Page
public ?string $errorMessage = null;
// Tracking
public bool $hasSearchedDocs = false;
public bool $hasDetail = false;
public bool $hasStatus = false;
public bool $hasPrice = false;
public bool $hasDueDate = false;
public bool $hasAttach = false;
public bool $hasPdf = false;
public bool $hasAdded = false;
public bool $hasModified = false;
public function searchDocuments(): void
{
$this->errorMessage = null;
$this->hasSearchedDocs = true;
try {
$service = app(LogisticsService::class);
@@ -131,64 +154,82 @@ class Documents extends Page
$this->data = $response['data'] ?? [];
$this->metadata = $response['metadata'] ?? null;
$this->errorMessage = $response['error'] ?? null;
$this->errorMessage = ApiErrorTranslator::translate($response['error'] ?? null);
} catch (LogisticsApiException $e) {
$this->errorMessage = $e->getMessage();
$this->errorMessage = ApiErrorTranslator::translate($e->getMessage());
$this->data = [];
} catch (\Throwable $e) {
$this->errorMessage = "Erreur inattendue : {$e->getMessage()}";
$this->errorMessage = ApiErrorTranslator::translate($e->getMessage());
$this->data = [];
}
}
public function getDocumentDetail(): void
{
$this->errorMessage = null;
if (blank($this->detailJnl) || blank($this->detailNumber)) {
$this->errorMessage = 'Les champs code journal (jnl) et numero de document (number) sont obligatoires.';
return;
}
$this->hasDetail = true;
try {
$service = app(LogisticsService::class);
$response = $service->documentDetail($this->detailJnl, $this->detailNumber);
$this->detailData = $response['data'] ?? [];
$this->errorMessage = $response['error'] ?? null;
$this->errorMessage = ApiErrorTranslator::translate($response['error'] ?? null);
} catch (LogisticsApiException $e) {
$this->errorMessage = $e->getMessage();
$this->errorMessage = ApiErrorTranslator::translate($e->getMessage());
$this->detailData = [];
} catch (\Throwable $e) {
$this->errorMessage = "Erreur inattendue : {$e->getMessage()}";
$this->errorMessage = ApiErrorTranslator::translate($e->getMessage());
$this->detailData = [];
}
}
public function getStatusList(): void
{
$this->errorMessage = null;
if (blank($this->statusJnl)) {
$this->errorMessage = 'Le champ code journal (jnl) est obligatoire.';
return;
}
$this->hasStatus = true;
try {
$service = app(LogisticsService::class);
$response = $service->documentGetStatusList($this->statusJnl);
$this->statusData = $response['data'] ?? [];
$this->errorMessage = $response['error'] ?? null;
$this->errorMessage = ApiErrorTranslator::translate($response['error'] ?? null);
} catch (LogisticsApiException $e) {
$this->errorMessage = $e->getMessage();
$this->errorMessage = ApiErrorTranslator::translate($e->getMessage());
$this->statusData = [];
} catch (\Throwable $e) {
$this->errorMessage = "Erreur inattendue : {$e->getMessage()}";
$this->errorMessage = ApiErrorTranslator::translate($e->getMessage());
$this->statusData = [];
}
}
public function getUnitPriceAndVat(): void
{
$this->errorMessage = null;
if (blank($this->priceArtId) || blank($this->priceQty) || blank($this->priceJnl) || blank($this->priceThirdId) || blank($this->priceDate)) {
$this->errorMessage = 'Tous les champs sont obligatoires : ARTID, QTY (format string), JNL, THIRDID et DATE.';
return;
}
$this->hasPrice = true;
try {
$service = app(LogisticsService::class);
$response = $service->documentGetUnitPriceAndVat([
@@ -200,85 +241,109 @@ class Documents extends Page
]);
$this->priceData = $response['data'] ?? [];
$this->errorMessage = $response['error'] ?? null;
$this->errorMessage = ApiErrorTranslator::translate($response['error'] ?? null);
} catch (LogisticsApiException $e) {
$this->errorMessage = $e->getMessage();
$this->errorMessage = ApiErrorTranslator::translate($e->getMessage());
$this->priceData = [];
} catch (\Throwable $e) {
$this->errorMessage = "Erreur inattendue : {$e->getMessage()}";
$this->errorMessage = ApiErrorTranslator::translate($e->getMessage());
$this->priceData = [];
}
}
public function getDueDate(): void
{
$this->errorMessage = null;
if (blank($this->payDelay) || blank($this->dueDateInput)) {
$this->errorMessage = 'Les champs delai de paiement (paydelay) et date de depart (date) sont obligatoires.';
return;
}
$this->hasDueDate = true;
try {
$service = app(LogisticsService::class);
$response = $service->documentGetDueDate($this->payDelay, $this->dueDateInput);
$this->dueDateData = $response['data'] ?? [];
$this->errorMessage = $response['error'] ?? null;
$this->errorMessage = ApiErrorTranslator::translate($response['error'] ?? null);
} catch (LogisticsApiException $e) {
$this->errorMessage = $e->getMessage();
$this->errorMessage = ApiErrorTranslator::translate($e->getMessage());
$this->dueDateData = [];
} catch (\Throwable $e) {
$this->errorMessage = "Erreur inattendue : {$e->getMessage()}";
$this->errorMessage = ApiErrorTranslator::translate($e->getMessage());
$this->dueDateData = [];
}
}
public function getAttachListThumbnail(): void
{
$this->errorMessage = null;
if (blank($this->attachJnl) || blank($this->attachNumber)) {
$this->errorMessage = 'Les champs code journal (JNL) et numero de document (NUMBER) sont obligatoires.';
return;
}
$this->hasAttach = true;
try {
$service = app(LogisticsService::class);
$response = $service->documentGetAttachListThumbnail($this->attachJnl, $this->attachNumber);
$this->attachData = $response['data'] ?? [];
$this->errorMessage = $response['error'] ?? null;
$this->errorMessage = ApiErrorTranslator::translate($response['error'] ?? null);
} catch (LogisticsApiException $e) {
$this->errorMessage = $e->getMessage();
$this->errorMessage = ApiErrorTranslator::translate($e->getMessage());
$this->attachData = [];
} catch (\Throwable $e) {
$this->errorMessage = "Erreur inattendue : {$e->getMessage()}";
$this->errorMessage = ApiErrorTranslator::translate($e->getMessage());
$this->attachData = [];
}
}
public function getPdf(): void
{
$this->errorMessage = null;
if (blank($this->pdfJnl) || blank($this->pdfNumber) || blank($this->pdfLayout)) {
$this->errorMessage = 'Les champs code journal (JNL), numero de document (NUMBER) et mise en page (LAYOUT) sont obligatoires.';
return;
}
$this->hasPdf = true;
try {
$service = app(LogisticsService::class);
$response = $service->documentGetPdf($this->pdfJnl, $this->pdfNumber, $this->pdfLayout);
$this->pdfData = $response['data'] ?? [];
$this->errorMessage = $response['error'] ?? null;
$this->errorMessage = ApiErrorTranslator::translate($response['error'] ?? null);
} catch (LogisticsApiException $e) {
$this->errorMessage = $e->getMessage();
$this->errorMessage = ApiErrorTranslator::translate($e->getMessage());
$this->pdfData = [];
} catch (\Throwable $e) {
$this->errorMessage = "Erreur inattendue : {$e->getMessage()}";
$this->errorMessage = ApiErrorTranslator::translate($e->getMessage());
$this->pdfData = [];
}
}
public function addDocument(): void
{
$this->errorMessage = null;
if (blank($this->addThirdId) || blank($this->addDate) || blank($this->addArtIds) || blank($this->addQty) || blank($this->addSalePrice) || blank($this->addJnl)) {
$this->errorMessage = 'Les champs ThirdId, Date, Artid, Qty, Saleprice et JNL sont obligatoires.';
return;
}
$this->hasAdded = true;
try {
$service = app(LogisticsService::class);
@@ -304,22 +369,28 @@ class Documents extends Page
$response = $service->documentAdd($params);
$this->addResult = $response['data'] ?? [];
$this->errorMessage = $response['error'] ?? null;
$this->errorMessage = ApiErrorTranslator::translate($response['error'] ?? null);
} catch (LogisticsApiException $e) {
$this->errorMessage = $e->getMessage();
$this->errorMessage = ApiErrorTranslator::translate($e->getMessage());
$this->addResult = [];
} catch (\Throwable $e) {
$this->errorMessage = "Erreur inattendue : {$e->getMessage()}";
$this->errorMessage = ApiErrorTranslator::translate($e->getMessage());
$this->addResult = [];
}
}
public function modDocument(): void
{
$this->errorMessage = null;
if (blank($this->modNumber) || blank($this->modJnl)) {
$this->errorMessage = 'Les champs numero de document (number) et code journal (JNL) sont obligatoires.';
return;
}
$this->hasModified = true;
try {
$service = app(LogisticsService::class);
@@ -344,12 +415,12 @@ class Documents extends Page
$response = $service->documentMod($params);
$this->modResult = $response['data'] ?? [];
$this->errorMessage = $response['error'] ?? null;
$this->errorMessage = ApiErrorTranslator::translate($response['error'] ?? null);
} catch (LogisticsApiException $e) {
$this->errorMessage = $e->getMessage();
$this->errorMessage = ApiErrorTranslator::translate($e->getMessage());
$this->modResult = [];
} catch (\Throwable $e) {
$this->errorMessage = "Erreur inattendue : {$e->getMessage()}";
$this->errorMessage = ApiErrorTranslator::translate($e->getMessage());
$this->modResult = [];
}
}

View File

@@ -4,6 +4,7 @@ namespace App\Filament\Pages;
use App\Exceptions\LogisticsApiException;
use App\Services\LogisticsService;
use App\Support\ApiErrorTranslator;
use Filament\Pages\Page;
use Filament\Support\Icons\Heroicon;
@@ -33,8 +34,20 @@ class Journaux extends Page
public ?string $errorMessage = null;
public bool $hasSearched = false;
public function searchJournaux(): void
{
$this->errorMessage = null;
if (blank($this->type)) {
$this->errorMessage = 'Le champ type de journal (TYPE) est obligatoire.';
return;
}
$this->hasSearched = true;
try {
$service = app(LogisticsService::class);
@@ -48,12 +61,12 @@ class Journaux extends Page
$this->data = $response['data'] ?? [];
$this->metadata = $response['metadata'] ?? null;
$this->errorMessage = $response['error'] ?? null;
$this->errorMessage = ApiErrorTranslator::translate($response['error'] ?? null);
} catch (LogisticsApiException $e) {
$this->errorMessage = $e->getMessage();
$this->errorMessage = ApiErrorTranslator::translate($e->getMessage());
$this->data = [];
} catch (\Throwable $e) {
$this->errorMessage = "Erreur inattendue : {$e->getMessage()}";
$this->errorMessage = ApiErrorTranslator::translate($e->getMessage());
$this->data = [];
}
}

View File

@@ -4,6 +4,7 @@ namespace App\Filament\Pages;
use App\Exceptions\LogisticsApiException;
use App\Services\LogisticsService;
use App\Support\ApiErrorTranslator;
use Filament\Pages\Page;
use Filament\Support\Icons\Heroicon;
use Livewire\Attributes\Url;
@@ -92,11 +93,11 @@ class TablesExplorer extends Page
$this->tables = $response['data'] ?? [];
$this->tablesMetadata = $response['metadata'] ?? null;
$this->errorMessage = $response['error'] ?? null;
$this->errorMessage = ApiErrorTranslator::translate($response['error'] ?? null);
} catch (LogisticsApiException $e) {
$this->errorMessage = $e->getMessage();
$this->errorMessage = ApiErrorTranslator::translate($e->getMessage());
} catch (\Throwable $e) {
$this->errorMessage = "Erreur inattendue : {$e->getMessage()}";
$this->errorMessage = ApiErrorTranslator::translate($e->getMessage());
}
}
@@ -116,12 +117,12 @@ class TablesExplorer extends Page
$rawColumns = $response['data'] ?? [];
$this->columns = $this->deduplicateColumns($rawColumns);
$this->columnsMetadata = $response['metadata'] ?? null;
$this->errorMessage = $response['error'] ?? null;
$this->errorMessage = ApiErrorTranslator::translate($response['error'] ?? null);
} catch (LogisticsApiException $e) {
$this->errorMessage = $e->getMessage();
$this->errorMessage = ApiErrorTranslator::translate($e->getMessage());
$this->columns = [];
} catch (\Throwable $e) {
$this->errorMessage = "Erreur inattendue : {$e->getMessage()}";
$this->errorMessage = ApiErrorTranslator::translate($e->getMessage());
$this->columns = [];
}
}

View File

@@ -4,6 +4,7 @@ namespace App\Filament\Pages;
use App\Exceptions\LogisticsApiException;
use App\Services\LogisticsService;
use App\Support\ApiErrorTranslator;
use Filament\Pages\Page;
use Filament\Support\Icons\Heroicon;
@@ -37,8 +38,22 @@ class Tiers extends Page
public ?string $errorMessage = null;
public bool $hasSearched = false;
public bool $hasHistory = false;
public function searchTiers(): void
{
$this->errorMessage = null;
if (blank($this->search)) {
$this->errorMessage = 'Le champ de recherche (search) est obligatoire.';
return;
}
$this->hasSearched = true;
try {
$service = app(LogisticsService::class);
@@ -52,33 +67,39 @@ class Tiers extends Page
$this->data = $response['data'] ?? [];
$this->metadata = $response['metadata'] ?? null;
$this->errorMessage = $response['error'] ?? null;
$this->errorMessage = ApiErrorTranslator::translate($response['error'] ?? null);
} catch (LogisticsApiException $e) {
$this->errorMessage = $e->getMessage();
$this->errorMessage = ApiErrorTranslator::translate($e->getMessage());
$this->data = [];
} catch (\Throwable $e) {
$this->errorMessage = "Erreur inattendue : {$e->getMessage()}";
$this->errorMessage = ApiErrorTranslator::translate($e->getMessage());
$this->data = [];
}
}
public function getArtHistory(): void
{
$this->errorMessage = null;
if (blank($this->historyThirdId)) {
$this->errorMessage = 'Le champ identifiant tiers (thirdid) est obligatoire.';
return;
}
$this->hasHistory = true;
try {
$service = app(LogisticsService::class);
$response = $service->thirdGetArtHistory($this->historyThirdId);
$this->historyData = $response['data'] ?? [];
$this->errorMessage = $response['error'] ?? null;
$this->errorMessage = ApiErrorTranslator::translate($response['error'] ?? null);
} catch (LogisticsApiException $e) {
$this->errorMessage = $e->getMessage();
$this->errorMessage = ApiErrorTranslator::translate($e->getMessage());
$this->historyData = [];
} catch (\Throwable $e) {
$this->errorMessage = "Erreur inattendue : {$e->getMessage()}";
$this->errorMessage = ApiErrorTranslator::translate($e->getMessage());
$this->historyData = [];
}
}

View File

@@ -0,0 +1,74 @@
<?php
namespace App\Support;
class ApiErrorTranslator
{
/**
* @var array<string, string>
*/
private static array $explanations = [
'search terms are required' => 'Le champ de recherche est obligatoire.',
'invalid api key' => 'La cle API configuree est invalide ou absente.',
'api key is required' => 'La cle API est requise dans les en-tetes de la requete.',
'not found' => "L'element demande n'a pas ete trouve dans la base de donnees.",
'layout not found' => 'La valeur du parametre LAYOUT est inconnue ou invalide.',
'unknown stkid' => "L'identifiant de stock (STKID) fourni est inconnu.",
'invalid date' => 'Le format de date fourni est invalide.',
'thirdid is required' => "L'identifiant du tiers est obligatoire.",
'jnl is required' => 'Le code journal est obligatoire.',
'number is required' => 'Le numero de document est obligatoire.',
'artid is required' => "L'identifiant de l'article est obligatoire.",
];
/**
* Normalise et traduit un message d'erreur API.
*
* Le champ `error` de l'API peut etre null, une chaine ou un tableau.
*/
public static function translate(mixed $error): ?string
{
if ($error === null) {
return null;
}
$message = self::normalize($error);
if ($message === '') {
return null;
}
$explanation = self::findExplanation($message);
if ($explanation !== null) {
return "{$message}\n\nExplication : {$explanation}";
}
return $message;
}
/**
* Normalise une valeur error (string ou array) en chaine lisible.
*/
public static function normalize(mixed $error): string
{
if (is_array($error)) {
return implode(' ', array_map('strval', $error));
}
return (string) $error;
}
private static function findExplanation(string $message): ?string
{
$lower = mb_strtolower($message);
foreach (self::$explanations as $pattern => $explanation) {
if (str_contains($lower, $pattern)) {
return $explanation;
}
}
return null;
}
}