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>
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:sqflite/sqflite.dart';
|
||||
|
||||
import '../models/produit.dart';
|
||||
import 'local_db.dart';
|
||||
|
||||
/// Source de vérité des produits, partagée par tous les écrans.
|
||||
/// Persistée localement dans une base SQLite (aucun serveur, fonctionne hors-ligne).
|
||||
class ProduitRepository extends ChangeNotifier {
|
||||
ProduitRepository._();
|
||||
static final ProduitRepository instance = ProduitRepository._();
|
||||
|
||||
final List<Produit> _produits = [];
|
||||
bool _charge = false;
|
||||
bool _enCours = false;
|
||||
String? _erreur;
|
||||
|
||||
List<Produit> get produits => List.unmodifiable(_produits);
|
||||
bool get charge => _charge;
|
||||
bool get enCours => _enCours;
|
||||
String? get erreur => _erreur;
|
||||
|
||||
Future<Database> get _db async => LocalDb.instance.database;
|
||||
|
||||
Future<void> charger() async {
|
||||
_enCours = true;
|
||||
_erreur = null;
|
||||
notifyListeners();
|
||||
|
||||
try {
|
||||
final db = await _db;
|
||||
final rows = await db.query('produits', orderBy: 'nom COLLATE NOCASE ASC');
|
||||
_produits
|
||||
..clear()
|
||||
..addAll(rows.map(Produit.fromMap));
|
||||
_charge = true;
|
||||
} catch (e) {
|
||||
_erreur = e.toString();
|
||||
} finally {
|
||||
_enCours = false;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
Produit? parCodeBarres(String code) {
|
||||
for (final p in _produits) {
|
||||
if (p.codeBarres == code) return p;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
Future<Produit> ajouter(Produit p) async {
|
||||
final db = await _db;
|
||||
final ts = DateTime.now().millisecondsSinceEpoch;
|
||||
final data = p.toInsertMap()..['cree_le'] = ts;
|
||||
final id = await db.insert(
|
||||
'produits',
|
||||
data,
|
||||
conflictAlgorithm: ConflictAlgorithm.replace,
|
||||
);
|
||||
final cree = p.copyWith(id: id.toString(), creeLe: ts);
|
||||
_produits.add(cree);
|
||||
_trier();
|
||||
notifyListeners();
|
||||
return cree;
|
||||
}
|
||||
|
||||
Future<void> modifier(Produit p) async {
|
||||
final db = await _db;
|
||||
await db.update(
|
||||
'produits',
|
||||
p.toInsertMap(),
|
||||
where: 'id = ?',
|
||||
whereArgs: [int.tryParse(p.id) ?? p.id],
|
||||
);
|
||||
final i = _produits.indexWhere((e) => e.id == p.id);
|
||||
if (i != -1) _produits[i] = p;
|
||||
_trier();
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Future<void> supprimer(String id) async {
|
||||
final db = await _db;
|
||||
await db.delete('produits', where: 'id = ?', whereArgs: [int.tryParse(id) ?? id]);
|
||||
_produits.removeWhere((e) => e.id == id);
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void _trier() => _produits
|
||||
.sort((a, b) => a.nom.toLowerCase().compareTo(b.nom.toLowerCase()));
|
||||
}
|
||||
Reference in New Issue
Block a user