import 'package:sqflite/sqflite.dart'; /// Accès à la base SQLite locale (persistée sur la tablette). class LocalDb { LocalDb._(); static final LocalDb instance = LocalDb._(); Database? _db; Future get database async { return _db ??= await _ouvrir(); } Future _ouvrir() async { final dir = await getDatabasesPath(); final chemin = '$dir/gestion_prix.db'; return openDatabase( chemin, version: 6, onCreate: (db, version) async { await _creerTable(db); }, onUpgrade: (db, ancienne, nouvelle) async { if (ancienne < 2) { // Ancien schéma (prix HT/TTC) → refonte complète. await db.execute('DROP TABLE IF EXISTS produits'); await _creerTable(db); } if (ancienne < 3) { // Ajout quantité + unité pour le prix au kilo/litre. await db.execute('ALTER TABLE produits ADD COLUMN quantite REAL'); await db.execute('ALTER TABLE produits ADD COLUMN unite TEXT'); } if (ancienne < 4) { // Ajout du nombre de contenants (packs). await db.execute('ALTER TABLE produits ADD COLUMN nb_contenants INTEGER'); } if (ancienne < 5) { // Ajout du stock (inventaire). await db.execute( 'ALTER TABLE produits ADD COLUMN stock INTEGER NOT NULL DEFAULT 0'); } if (ancienne < 6) { // Ajout du prix d'achat (marge). await db.execute( 'ALTER TABLE produits ADD COLUMN prix_achat REAL NOT NULL DEFAULT 0'); } }, ); } Future _creerTable(Database db) async { await db.execute(''' CREATE TABLE produits( id INTEGER PRIMARY KEY AUTOINCREMENT, code_barres TEXT UNIQUE, nom TEXT NOT NULL, image_url TEXT, prix REAL NOT NULL DEFAULT 0, prix_achat REAL NOT NULL DEFAULT 0, quantite REAL, unite TEXT, nb_contenants INTEGER, stock INTEGER NOT NULL DEFAULT 0, cree_le INTEGER NOT NULL DEFAULT 0 ) '''); } }