Skip to content

[vibe-code-with-gemini-in-aistudio] #2398

Closed

Description

@kouabroumarie39-ctrl

function onEdit(e) {
const vente = e.range.getSheet();

// Travailler uniquement dans l'onglet VENTES
if (vente.getName() !== "VENTES") return;

const ligne = e.range.getRow();
const colonne = e.range.getColumn();

// Déclencher uniquement quand la quantité est saisie
if (ligne < 2 || colonne !== 4) return;

const classeur = e.source;
const produits = classeur.getSheetByName("PRODUITS");

if (!produits) {
classeur.toast("Onglet PRODUITS introuvable.");
return;
}

// Vérifier si cette vente a déjà été traitée
if (vente.getRange(ligne, 8).getValue() === "OUI") return;

const nomProduit = String(vente.getRange(ligne, 3).getValue())
.trim().toLowerCase();

const quantite = Number(vente.getRange(ligne, 4).getValue());

if (!nomProduit || !Number.isInteger(quantite) || quantite <= 0) {
vente.getRange(ligne, 4).clearContent();
classeur.toast("Saisis un produit et une quantité valide.");
return;
}

const derniereLigne = produits.getLastRow();

if (derniereLigne < 2) {
vente.getRange(ligne, 4).clearContent();
classeur.toast("Aucun produit enregistré.");
return;
}

// Rechercher le produit dans la colonne B
const noms = produits.getRange(2, 2, derniereLigne - 1, 1)
.getValues();

const index = noms.findIndex(
p => String(p[0]).trim().toLowerCase() === nomProduit
);

if (index === -1) {
vente.getRange(ligne, 4).clearContent();
classeur.toast("Produit introuvable.");
return;
}

const ligneProduit = index + 2;

// Lire la quantité disponible dans la colonne E
const celluleStock = produits.getRange(ligneProduit, 5);
const stock = Number(celluleStock.getValue());

if (!Number.isFinite(stock) || stock < quantite) {
vente.getRange(ligne, 4).clearContent();
classeur.toast("Stock insuffisant !");
return;
}

// Déduire la quantité vendue
celluleStock.setValue(stock - quantite);

// Marquer la vente comme traitée
vente.getRange(ligne, 8).setValue("OUI");

classeur.toast("Vente enregistrée : stock mis à jour !");
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions