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;
@@ -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 = [];
}
}