mirror of
https://github.com/R0m1k3/CollectFlow.git
synced 2026-10-13 02:07:37 +02:00
feat: Add FF Nancy API client and status endpoint, implement grid product fetching and draft change saving, and introduce AI Copilot core logic.
This commit is contained in:
1 parent
22aa6f101a
commit
1035489db1
13 files changed
+706
-265
No files matched your search
@@ -1,14 +1,18 @@
|
||||
"use server";
|
||||
|
||||
import { db } from "@/db";
|
||||
import { ventesProduits } from "@/db/schema";
|
||||
import { eq, and, ne, like, sql } from "drizzle-orm";
|
||||
import type { ProductRow, GammeCode, GridFilters } from "@/types/grid";
|
||||
import { computeProductScores } from "@/lib/score-engine";
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
|
||||
let isSchemaInitialized = false;
|
||||
import {
|
||||
getArticlesByFournisseur,
|
||||
getMouvementsByFournisseur,
|
||||
getStockByFournisseur,
|
||||
getCommandesByFournisseur,
|
||||
buildLast12MonthsRange,
|
||||
dateToYYYYMM,
|
||||
} from "@/lib/api-ff-client";
|
||||
import { db } from "@/db";
|
||||
import { sessionSnapshots } from "@/db/schema";
|
||||
import { eq, desc } from "drizzle-orm";
|
||||
|
||||
interface GetProductRowsInput {
|
||||
codeFournisseur: string;
|
||||
@@ -17,104 +21,60 @@ interface GetProductRowsInput {
|
||||
}
|
||||
|
||||
export async function getProductRows(input: GetProductRowsInput): Promise<ProductRow[]> {
|
||||
const { codeFournisseur, magasin = "TOTAL", filters } = input;
|
||||
const { codeFournisseur, magasin = "TOTAL" } = input;
|
||||
console.log(`\n>>> [getProductRows] RECEIVED CALL for supplier: ${codeFournisseur}, magasin: ${magasin}`);
|
||||
|
||||
try {
|
||||
// Self-healing: Ensure codeGammeInit column exists and is populated
|
||||
if (!isSchemaInitialized) {
|
||||
try {
|
||||
await db.execute(sql`
|
||||
ALTER TABLE ventes_produits
|
||||
ADD COLUMN IF NOT EXISTS code_gamme_init VARCHAR(20)
|
||||
`);
|
||||
await db.execute(sql`
|
||||
UPDATE ventes_produits
|
||||
SET code_gamme_init = code_gamme
|
||||
WHERE code_gamme_init IS NULL
|
||||
`);
|
||||
isSchemaInitialized = true;
|
||||
console.log("[Schema] codeGammeInit initialized successfully.");
|
||||
} catch (err) {
|
||||
console.error("[Schema] Failed to initialize codeGammeInit:", err);
|
||||
}
|
||||
}
|
||||
// ─── Phase 1 : Articles du fournisseur ───────────────────────────────
|
||||
const articles = await getArticlesByFournisseur(codeFournisseur);
|
||||
console.log(`[getProductRows] ${articles.length} articles found for ${codeFournisseur}`);
|
||||
|
||||
// ─── Phase 2 : Mouvements (ventes + stock12m) ────────────────────────
|
||||
const { dateDebut, dateFin } = buildLast12MonthsRange();
|
||||
const mouvements = await getMouvementsByFournisseur(codeFournisseur, dateDebut, dateFin);
|
||||
console.log(`[getProductRows] ${mouvements.length} mouvements found`);
|
||||
|
||||
// ─── Phase 3 : Stock & Commandes (en parallèle) ──────────────────────
|
||||
const [stockMap, commandesMap] = await Promise.all([
|
||||
getStockByFournisseur(articles),
|
||||
getCommandesByFournisseur(codeFournisseur),
|
||||
]);
|
||||
|
||||
// ─── Phase 4 : Fenêtre temporelle (12 mois complets) ─────────────────
|
||||
// En mars 2026 → allowedPeriods = {"202503"..."202602"} (12 mois)
|
||||
const now = new Date();
|
||||
const allowedPeriods = new Set<string>();
|
||||
for (let i = 12; i >= 0; i--) {
|
||||
for (let i = 12; i >= 1; i--) {
|
||||
const d = new Date(now.getFullYear(), now.getMonth() - i, 1);
|
||||
allowedPeriods.add(`${d.getFullYear()}${String(d.getMonth() + 1).padStart(2, "0")}`);
|
||||
}
|
||||
|
||||
const conditions = [
|
||||
eq(ventesProduits.codeFournisseur, codeFournisseur),
|
||||
ne(ventesProduits.periode, "TOTAL"),
|
||||
magasin !== "TOTAL" ? eq(ventesProduits.magasin, magasin) : ne(ventesProduits.magasin, "TOTAL"),
|
||||
];
|
||||
|
||||
if (filters?.code3) {
|
||||
conditions.push(eq(ventesProduits.code3, filters.code3));
|
||||
} else if (filters?.code2) {
|
||||
conditions.push(like(ventesProduits.code3, `${filters.code2}%`));
|
||||
} else if (filters?.code1) {
|
||||
conditions.push(like(ventesProduits.code3, `${filters.code1}%`));
|
||||
}
|
||||
|
||||
const rows = await db
|
||||
.select()
|
||||
.from(ventesProduits)
|
||||
.where(and(...conditions));
|
||||
|
||||
console.log(`[getProductRows] Fetching for ${codeFournisseur} (${magasin}). Found ${rows.length} rows.`);
|
||||
|
||||
// ─── Phase 5 : Seed productMap depuis articles ────────────────────────
|
||||
const productMap = new Map<string, ProductRow>();
|
||||
const storeParticipationMap = new Map<string, Map<string, Set<string>>>(); // codein -> magasin -> Set of periods
|
||||
const storeParticipationMap = new Map<string, Map<string, Set<string>>>();
|
||||
// stockMaxByPeriod[codein][YYYYMM] = dernier qtestock du mois
|
||||
const stockLastByPeriod = new Map<string, Map<string, { qtestock: number; datemvt: string }>>();
|
||||
|
||||
for (const row of rows) {
|
||||
const { codein, magasin, periode } = row;
|
||||
const isInPeriod = allowedPeriods.has(periode);
|
||||
|
||||
if (isInPeriod && magasin !== "TOTAL") {
|
||||
if (!storeParticipationMap.has(codein)) storeParticipationMap.set(codein, new Map());
|
||||
const productStores = storeParticipationMap.get(codein)!;
|
||||
if (!productStores.has(magasin)) productStores.set(magasin, new Set());
|
||||
productStores.get(magasin)!.add(periode);
|
||||
}
|
||||
|
||||
let existing = productMap.get(codein);
|
||||
|
||||
if (!existing) {
|
||||
const c3 = row.code3 || "";
|
||||
const c1 = c3.slice(0, 2);
|
||||
const c2 = c3.slice(0, 4);
|
||||
|
||||
const NOMENCLATURE_LABELS: Record<string, string> = {
|
||||
"20": "LIQUIDES", "30": "ÉPICERIE", "40": "DPH", "50": "BAZAR", "60": "TEXTILE", "70": "FRAIS",
|
||||
"2001": "EAUX", "2002": "JUS DE FRUITS", "2003": "SODAS / BOUILLONS", "2004": "BIERES", "2005": "VINS", "2006": "CIDRES", "2007": "APERITIFS", "2008": "ALCOOLS",
|
||||
"3001": "ÉPICERIE SALÉE", "3002": "ÉPICERIE SUCRÉE", "3003": "PETIT DÉJEUNER", "3004": "CONSERVES", "3005": "PLAT CUISINES",
|
||||
"4001": "HYGIÈNE", "4002": "ENTRETIEN", "4003": "PARFUMERIE",
|
||||
"5001": "BRICOLAGE", "5002": "MAISON", "5003": "LOISIRS", "5004": "JARDIN",
|
||||
"7001": "FRUITS ET LEGUMES", "7002": "CREMERIE", "7003": "VOLAILLE", "7004": "CHARCUTERIE", "7005": "TRAITEUR", "7006": "BOULANGERIE",
|
||||
};
|
||||
|
||||
existing = {
|
||||
codein,
|
||||
codeFournisseur: row.codeFournisseur ?? "",
|
||||
nomFournisseur: row.nomFournisseur ?? "",
|
||||
libelle1: row.libelle1 ?? "",
|
||||
gtin: row.gtin ?? "",
|
||||
reference: row.reference ?? "",
|
||||
code1: c1,
|
||||
libelleNiveau1: NOMENCLATURE_LABELS[c1] || `Secteur ${c1}`,
|
||||
code2: c2,
|
||||
libelleNiveau2: NOMENCLATURE_LABELS[c2] || `Rayon ${c2}`,
|
||||
code3: c3,
|
||||
libelle3: row.libelle3 ?? "",
|
||||
codeGamme: (row.codeGamme as GammeCode | null),
|
||||
codeGammeInit: (row.codeGammeInit as GammeCode | null) ?? (row.codeGamme as GammeCode | null),
|
||||
for (const art of articles) {
|
||||
if (!productMap.has(art.codein)) {
|
||||
productMap.set(art.codein, {
|
||||
codein: art.codein,
|
||||
codeFournisseur: art.codefou ?? codeFournisseur,
|
||||
nomFournisseur: art.nomfou ?? "",
|
||||
libelle1: art.libelle1 ?? "",
|
||||
gtin: "",
|
||||
reference: "",
|
||||
code1: "",
|
||||
libelleNiveau1: "",
|
||||
code2: "",
|
||||
libelleNiveau2: "",
|
||||
code3: "",
|
||||
libelle3: "",
|
||||
codeGamme: null,
|
||||
codeGammeInit: null,
|
||||
codeGammeDraft: null,
|
||||
sales12m: {},
|
||||
stock12m: {},
|
||||
totalQuantite: 0,
|
||||
totalCa: 0,
|
||||
totalMarge: 0,
|
||||
@@ -122,39 +82,136 @@ export async function getProductRows(input: GetProductRowsInput): Promise<Produc
|
||||
score: 0,
|
||||
workingStores: [],
|
||||
aiRecommendation: null,
|
||||
};
|
||||
productMap.set(codein, existing);
|
||||
}
|
||||
|
||||
if (isInPeriod) {
|
||||
const qty = Math.abs(parseFloat(row.quantite?.toString() ?? "0"));
|
||||
const ca = Math.abs(parseFloat(row.montantMvt?.toString() ?? "0"));
|
||||
const marge = Math.abs(parseFloat(row.margeMvt?.toString() ?? "0"));
|
||||
|
||||
existing.sales12m[periode] = (existing.sales12m[periode] ?? 0) + qty;
|
||||
existing.totalQuantite += qty;
|
||||
existing.totalCa += ca;
|
||||
existing.totalMarge += marge;
|
||||
existing.tauxMarge = existing.totalCa > 0 ? (existing.totalMarge / existing.totalCa) * 100 : 0;
|
||||
pcb: art.pcb,
|
||||
prixVente: art.pv_central,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// ─── Phase 6 : Agréger mouvements → sales12m + stock12m ─────────────
|
||||
for (const mvt of mouvements) {
|
||||
const { codein, genremvt, datemvt, qte, montant, marge, qtestock, site } = mvt;
|
||||
|
||||
// Filtre magasin
|
||||
if (magasin !== "TOTAL" && site !== magasin) continue;
|
||||
|
||||
const periode = dateToYYYYMM(datemvt);
|
||||
const isInPeriod = allowedPeriods.has(periode);
|
||||
|
||||
const product = productMap.get(codein);
|
||||
if (!product) continue;
|
||||
|
||||
// Participation magasin (pour workingStores)
|
||||
if (isInPeriod && site && site !== "TOTAL") {
|
||||
if (!storeParticipationMap.has(codein)) storeParticipationMap.set(codein, new Map());
|
||||
const stores = storeParticipationMap.get(codein)!;
|
||||
if (!stores.has(site)) stores.set(site, new Set());
|
||||
stores.get(site)!.add(periode);
|
||||
}
|
||||
|
||||
// Ventes (genremvt = 3)
|
||||
if (isInPeriod && genremvt === 3) {
|
||||
const qty = Math.abs(qte ?? 0);
|
||||
const ca = Math.abs(montant ?? 0);
|
||||
const mg = Math.abs(marge ?? 0);
|
||||
product.sales12m[periode] = (product.sales12m[periode] ?? 0) + qty;
|
||||
product.totalQuantite += qty;
|
||||
product.totalCa += ca;
|
||||
product.totalMarge += mg;
|
||||
}
|
||||
|
||||
// Stock12m : conserver le dernier mouvement par mois (via qtestock)
|
||||
// On inclut tous les mouvements (pas que les ventes) pour avoir le stock fin de mois
|
||||
if (isInPeriod) {
|
||||
if (!stockLastByPeriod.has(codein)) stockLastByPeriod.set(codein, new Map());
|
||||
const byPeriod = stockLastByPeriod.get(codein)!;
|
||||
const existing = byPeriod.get(periode);
|
||||
if (!existing || datemvt >= existing.datemvt) {
|
||||
byPeriod.set(periode, { qtestock, datemvt });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Calcul tauxMarge + reconstruction stock12m avec carry-forward
|
||||
for (const [codein, product] of productMap.entries()) {
|
||||
const productStores = storeParticipationMap.get(codein);
|
||||
if (productStores) {
|
||||
const workingStores: string[] = [];
|
||||
for (const [magasin, periods] of productStores.entries()) {
|
||||
if (magasin === "TOTAL") continue;
|
||||
if (periods.size >= 3) {
|
||||
workingStores.push(magasin);
|
||||
product.tauxMarge = product.totalCa > 0
|
||||
? (product.totalMarge / product.totalCa) * 100
|
||||
: 0;
|
||||
|
||||
// Reconstruction stock12m avec carry-forward
|
||||
const byPeriod = stockLastByPeriod.get(codein);
|
||||
if (byPeriod) {
|
||||
const sortedPeriods = [...allowedPeriods].sort();
|
||||
let lastKnownStock = 0;
|
||||
for (const p of sortedPeriods) {
|
||||
const entry = byPeriod.get(p);
|
||||
if (entry !== undefined) {
|
||||
lastKnownStock = entry.qtestock;
|
||||
}
|
||||
product.stock12m[p] = lastKnownStock;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ─── Phase 7 : workingStores ─────────────────────────────────────────
|
||||
for (const [codein, product] of productMap.entries()) {
|
||||
const stores = storeParticipationMap.get(codein);
|
||||
if (stores) {
|
||||
const working: string[] = [];
|
||||
for (const [site, periods] of stores.entries()) {
|
||||
if (site !== "TOTAL" && periods.size >= 3) working.push(site);
|
||||
}
|
||||
product.workingStores = working.sort();
|
||||
}
|
||||
}
|
||||
|
||||
// ─── Phase 8 : Enrichir avec stockMap ────────────────────────────────
|
||||
for (const [codein, product] of productMap.entries()) {
|
||||
const stock = stockMap.get(codein);
|
||||
if (stock) {
|
||||
product.stockActuel = stock.stockActuel;
|
||||
product.stockTotal = stock.stockTotal;
|
||||
product.stockValeur = stock.stockValeur;
|
||||
product.pa = stock.pa;
|
||||
product.prixAchat = stock.pa;
|
||||
product.derniereVente = stock.derniereVente || undefined;
|
||||
product.derniereLivraison = stock.derniereLivraison || undefined;
|
||||
product.nbJoursDerniereVente = stock.nbJoursDerniereVente;
|
||||
}
|
||||
const cmdQty = commandesMap.get(codein);
|
||||
if (cmdQty) product.commandesEnCours = cmdQty;
|
||||
}
|
||||
|
||||
// ─── Phase 8b : Restaurer gammes depuis dernier snapshot ─────────────
|
||||
try {
|
||||
const snapshots = await db
|
||||
.select()
|
||||
.from(sessionSnapshots)
|
||||
.where(eq(sessionSnapshots.codeFournisseur, codeFournisseur))
|
||||
.orderBy(desc(sessionSnapshots.createdAt))
|
||||
.limit(1);
|
||||
|
||||
if (snapshots.length > 0) {
|
||||
const snap = snapshots[0];
|
||||
const changes = snap.changes as Record<string, { before: string | null; after: string }>;
|
||||
for (const [codein, change] of Object.entries(changes)) {
|
||||
const product = productMap.get(codein);
|
||||
if (product && change.after) {
|
||||
product.codeGamme = change.after as GammeCode;
|
||||
product.codeGammeInit = change.before as GammeCode | null;
|
||||
}
|
||||
}
|
||||
product.workingStores = workingStores.sort();
|
||||
console.log(`[getProductRows] Restored ${Object.keys(changes).length} gammes from snapshot ${snap.id}`);
|
||||
}
|
||||
} catch (snapErr) {
|
||||
console.error("[getProductRows] Failed to restore gammes from snapshot:", snapErr);
|
||||
}
|
||||
|
||||
console.log(`- Final ProductMap Size: ${productMap.size}`);
|
||||
return computeProductScores(Array.from(productMap.values()));
|
||||
// ─── Phase 9 : Score composite ────────────────────────────────────────
|
||||
const rows = Array.from(productMap.values());
|
||||
console.log(`[getProductRows] Final: ${rows.length} products`);
|
||||
return computeProductScores(rows);
|
||||
|
||||
} catch (error) {
|
||||
console.error(`[getProductRows] Error for supplier ${codeFournisseur}:`, error);
|
||||
return [];
|
||||
|
||||
@@ -1,18 +1,20 @@
|
||||
"use server";
|
||||
|
||||
import { db } from "@/db";
|
||||
import { ventesProduits } from "@/db/schema";
|
||||
import { eq, and, sql } from "drizzle-orm";
|
||||
import { sessionSnapshots } from "@/db/schema";
|
||||
import { eq, desc } from "drizzle-orm";
|
||||
import { z } from "zod";
|
||||
|
||||
import { revalidatePath } from "next/cache";
|
||||
import { auth } from "@/lib/auth";
|
||||
|
||||
const SaveDraftsSchema = z.object({
|
||||
codeFournisseur: z.string(),
|
||||
nomFournisseur: z.string().optional(),
|
||||
magasin: z.string(),
|
||||
changes: z.array(
|
||||
z.object({
|
||||
codein: z.string(),
|
||||
magasin: z.string(),
|
||||
codeGamme: z.enum(["A", "B", "C", "Z"]),
|
||||
codeGammeBefore: z.string().nullable(),
|
||||
codeGamme: z.string(),
|
||||
})
|
||||
),
|
||||
});
|
||||
@@ -25,29 +27,48 @@ export async function saveDraftChanges(
|
||||
return { success: false, saved: 0, error: "Validation failed: " + parsed.error.message };
|
||||
}
|
||||
|
||||
const { changes } = parsed.data;
|
||||
const { codeFournisseur, nomFournisseur, magasin, changes } = parsed.data;
|
||||
|
||||
const session = await auth();
|
||||
const rawUserId = (session?.user as { id?: string | number })?.id;
|
||||
const userId = rawUserId ? parseInt(String(rawUserId), 10) : null;
|
||||
const finalUserId = userId && !isNaN(userId) ? userId : null;
|
||||
|
||||
try {
|
||||
// Batch update all rows for given codein.
|
||||
// If magasin is "TOTAL", update ALL stores for that product.
|
||||
await Promise.all(
|
||||
changes.map(({ codein, magasin, codeGamme }) => {
|
||||
const whereClause = [eq(ventesProduits.codein, codein)];
|
||||
if (magasin !== "TOTAL") {
|
||||
whereClause.push(eq(ventesProduits.magasin, magasin));
|
||||
}
|
||||
// Charger le dernier snapshot existant pour merger les changements
|
||||
const existing = await db
|
||||
.select()
|
||||
.from(sessionSnapshots)
|
||||
.where(eq(sessionSnapshots.codeFournisseur, codeFournisseur))
|
||||
.orderBy(desc(sessionSnapshots.createdAt))
|
||||
.limit(1);
|
||||
|
||||
return db
|
||||
.update(ventesProduits)
|
||||
.set({
|
||||
codeGamme,
|
||||
codeGammeInit: sql`COALESCE(ventes_produits.code_gamme_init, ventes_produits.code_gamme)`
|
||||
})
|
||||
.where(and(...whereClause));
|
||||
})
|
||||
);
|
||||
const prevChanges: Record<string, { before: string | null; after: string }> =
|
||||
existing.length > 0
|
||||
? (existing[0].changes as Record<string, { before: string | null; after: string }>)
|
||||
: {};
|
||||
|
||||
// Merger les nouveaux changements par-dessus l'existant
|
||||
const mergedChanges = { ...prevChanges };
|
||||
for (const c of changes) {
|
||||
mergedChanges[c.codein] = {
|
||||
before: c.codeGammeBefore,
|
||||
after: c.codeGamme,
|
||||
};
|
||||
}
|
||||
|
||||
// Upsert : insérer un nouveau snapshot "draft" pour ce fournisseur
|
||||
await db.insert(sessionSnapshots).values({
|
||||
userId: finalUserId,
|
||||
codeFournisseur,
|
||||
nomFournisseur: nomFournisseur ?? null,
|
||||
magasin,
|
||||
label: `Draft — ${new Date().toLocaleDateString("fr-FR")}`,
|
||||
changes: mergedChanges,
|
||||
summaryJson: null,
|
||||
type: "snapshot",
|
||||
});
|
||||
|
||||
revalidatePath("/grid");
|
||||
return { success: true, saved: changes.length };
|
||||
} catch (err) {
|
||||
const msg = err instanceof Error ? err.message : "Unknown error";
|
||||
|
||||
Reference in new issue
Block a user