Files
gestion_prix_produit/lib/services/local_db.dart
T
Mathew 4d936d2ee3 Version initiale de Gestion Prix
App Flutter de gestion de stock/prix pour tablette Android (100% local) :
scan code-barres (OpenFoodFacts), prix au kilo/litre, packs, inventaire,
prix d'achat + marge, filtres de tri, étiquettes PDF, design noir & blanc.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-17 21:28:33 +02:00

70 lines
2.1 KiB
Dart

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<Database> get database async {
return _db ??= await _ouvrir();
}
Future<Database> _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<void> _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
)
''');
}
}