diff --git a/src/features/grid/actions.ts b/src/features/grid/actions.ts index eb791d3..ddd53bb 100644 --- a/src/features/grid/actions.ts +++ b/src/features/grid/actions.ts @@ -1,14 +1,15 @@ "use server"; -import { getFournisseursFromApi, getSitesFromApi } from "@/lib/api-ff-client"; +import { getSitesFromApi } from "@/lib/api-ff-client"; +import { pgGetFournisseurs } from "@/lib/pg-ff-client"; import { getProductRows } from "./api/get-product-rows"; import type { ProductRow, GridFilters } from "@/types/grid"; /** - * Get the list of all suppliers from the FF Nancy API. + * Get the list of all suppliers from PostgreSQL (fouadr1). */ export async function getFournisseurs() { - return getFournisseursFromApi(); + return pgGetFournisseurs(); } /** diff --git a/src/features/grid/api/get-product-rows.ts b/src/features/grid/api/get-product-rows.ts index 6d7cea5..f7adc87 100644 --- a/src/features/grid/api/get-product-rows.ts +++ b/src/features/grid/api/get-product-rows.ts @@ -10,6 +10,7 @@ import { pgGetStockByFournisseur, pgGetRankingByFournisseur, pgGetCommandesByFournisseur, + type PgStockRow, } from "@/lib/pg-ff-client"; import { db } from "@/db"; import { sessionSnapshots } from "@/db/schema"; @@ -38,12 +39,12 @@ export async function getProductRows(input: GetProductRowsInput): Promise { console.error("[getProductRows] pgGetArticlesByFournisseur ERROR:", e); return []; }), + pgGetMensuelByFournisseur(codeFournisseur, dateDebut, dateFin).catch(e => { console.error("[getProductRows] pgGetMensuelByFournisseur ERROR:", e); return []; }), + pgGetGammesByFournisseur(codeFournisseur).catch(e => { console.error("[getProductRows] pgGetGammesByFournisseur ERROR:", e); return new Map(); }), + pgGetStockByFournisseur(codeFournisseur).catch(e => { console.error("[getProductRows] pgGetStockByFournisseur ERROR:", e); return new Map(); }), + pgGetRankingByFournisseur(codeFournisseur).catch(e => { console.error("[getProductRows] pgGetRankingByFournisseur ERROR:", e); return { rankings: new Map(), totalRankedProducts: 0 }; }), + pgGetCommandesByFournisseur(codeFournisseur).catch(e => { console.error("[getProductRows] pgGetCommandesByFournisseur ERROR:", e); return new Map(); }), ]); const { rankings: rankingMap, totalRankedProducts } = rankingResult; diff --git a/src/lib/pg-ff-client.ts b/src/lib/pg-ff-client.ts index c74da29..603473a 100644 --- a/src/lib/pg-ff-client.ts +++ b/src/lib/pg-ff-client.ts @@ -59,6 +59,31 @@ export interface PgRankingRow { ranking_mag_qte?: number; } +// --------------------------------------------------------------------------- +// 0. Liste des fournisseurs +// --------------------------------------------------------------------------- + +/** + * Retourne la liste de tous les fournisseurs depuis fouadr1. + * 1 requête SQL — remplace getFournisseursFromApi(). + */ +export async function pgGetFournisseurs(search?: string): Promise<{ code: string; nom: string }[]> { + const result = await db.execute(sql` + SELECT DISTINCT + fa.code, + fa.raisonsociale AS nom + FROM fouadr1 fa + WHERE fa.sit_code = '000' + AND fa.raisonsociale IS NOT NULL + AND fa.code IS NOT NULL + ${search ? sql`AND (fa.raisonsociale ILIKE ${'%' + search + '%'} OR fa.code ILIKE ${'%' + search + '%'})` : sql``} + ORDER BY fa.raisonsociale + `); + + return (result.rows as unknown as { code: string; nom: string }[]) + .filter(r => r.code && r.nom); +} + // --------------------------------------------------------------------------- // 1. Articles du fournisseur // ---------------------------------------------------------------------------