Browse Source

database

28062025_02
Stephane 5 months ago
parent
commit
6b83a1ab5e
  1. 11
      lib/Components/colors.dart
  2. 844
      lib/Services/stock_managementDatabase.dart
  3. 86
      lib/config/DatabaseConfig.dart
  4. 24
      lib/controller/userController.dart

11
lib/Components/colors.dart

@ -0,0 +1,11 @@
import 'package:flutter/material.dart';
class AppColors {
static const primaryBlue =
Color(0xFF04365F); // Replace with your exact logo tone
static const secondaryBlue =
Color(0xFF1976D2); // Replace if your logo has a secondary tone
static const neutralGrey = Color(0xFF8A8A8A);
static const accent =
Color(0xFFF7941D); // Example: if your logo has an orange or accent
}

844
lib/Services/stock_managementDatabase.dart

File diff suppressed because it is too large

86
lib/config/DatabaseConfig.dart

@ -1,18 +1,22 @@
// Config/database_config.dart - Version améliorée // Config/database_config.dart
import 'dart:io';
import 'dart:async';
class DatabaseConfig { class DatabaseConfig {
// static const String host = '10.0.2.2'; // Local MySQL settings
//static const String host = '172.20.10.5'; static const String localHost = '192.168.88.73';
static const String host = 'localhost'; static const String localUsername = 'guycom';
static const int port = 3306; static const String? localPassword = '3iV59wjRdbuXAPR';
static const String username = 'root'; static const String localDatabase = 'guycom';
static const String? password = null;
static const String database = 'gico';
// Production (public) MySQL settings
static const String prodHost = '185.70.105.157'; static const String prodHost = '185.70.105.157';
static const String prodUsername = 'guycom'; static const String prodUsername = 'guycom';
static const String prodPassword = '3iV59wjRdbuXAPR'; static const String prodPassword = '3iV59wjRdbuXAPR';
static const String prodDatabase = 'guycom'; static const String prodDatabase = 'guycom';
static const int port = 3306;
static const Duration connectionTimeout = Duration(seconds: 30); static const Duration connectionTimeout = Duration(seconds: 30);
static const Duration queryTimeout = Duration(seconds: 15); static const Duration queryTimeout = Duration(seconds: 15);
@ -21,32 +25,57 @@ class DatabaseConfig {
static bool get isDevelopment => false; static bool get isDevelopment => false;
static Map<String, dynamic> getConfig() { /// Build config map for connection
if (isDevelopment) { static Map<String, dynamic> _buildConfig({
required String host,
required String user,
required String? password,
required String database,
}) {
return { return {
'host': host, 'host': host,
'port': port, 'port': port,
'user': username, 'user': user,
'password': password, 'password': password,
'database': database, 'database': database,
'timeout': connectionTimeout.inSeconds, 'timeout': connectionTimeout.inSeconds,
}; };
}
/// TCP check if MySQL server is reachable
static Future<bool> isServerReachable(String host, {int port = 3306}) async {
try {
final socket =
await Socket.connect(host, port, timeout: Duration(seconds: 2));
socket.destroy();
return true;
} catch (_) {
return false;
}
}
/// Get smart config (local if reachable, otherwise public)
static Future<Map<String, dynamic>> getSmartConfig() async {
if (await isServerReachable(localHost, port: port)) {
return _buildConfig(
host: localHost,
user: localUsername,
password: localPassword,
database: localDatabase,
);
} else { } else {
return { return _buildConfig(
'host': prodHost, host: prodHost,
'port': port, user: prodUsername,
'user': prodUsername, password: prodPassword,
'password': prodPassword, database: prodDatabase,
'database': prodDatabase, );
'timeout': connectionTimeout.inSeconds,
};
} }
} }
// Validation de la configuration /// Validate any config
static bool validateConfig() { static bool validateConfig(Map<String, dynamic> config) {
try { try {
final config = getConfig();
return config['host']?.toString().isNotEmpty == true && return config['host']?.toString().isNotEmpty == true &&
config['database']?.toString().isNotEmpty == true && config['database']?.toString().isNotEmpty == true &&
config['user'] != null; config['user'] != null;
@ -56,11 +85,12 @@ class DatabaseConfig {
} }
} }
// Configuration avec retry automatique /// Add retry config
static Map<String, dynamic> getConfigWithRetry() { static Map<String, dynamic> addRetry(Map<String, dynamic> config) {
final config = getConfig(); return {
config['retryCount'] = 3; ...config,
config['retryDelay'] = 5000; // ms 'retryCount': 3,
return config; 'retryDelay': 5000, // ms
};
} }
} }

24
lib/controller/userController.dart

@ -44,7 +44,8 @@ class UserController extends GetxController {
final storedRole = prefs.getString('role') ?? ''; final storedRole = prefs.getString('role') ?? '';
final storedUserId = prefs.getInt('user_id') ?? 0; final storedUserId = prefs.getInt('user_id') ?? 0;
final storedPointDeVenteId = prefs.getInt('point_de_vente_id') ?? 0; final storedPointDeVenteId = prefs.getInt('point_de_vente_id') ?? 0;
final storedPointDeVenteDesignation = prefs.getString('point_de_vente_designation') ?? ''; final storedPointDeVenteDesignation =
prefs.getString('point_de_vente_designation') ?? '';
if (storedUsername.isNotEmpty) { if (storedUsername.isNotEmpty) {
try { try {
@ -60,13 +61,13 @@ class UserController extends GetxController {
_pointDeVenteId.value = storedPointDeVenteId; _pointDeVenteId.value = storedPointDeVenteId;
_pointDeVenteDesignation.value = storedPointDeVenteDesignation; _pointDeVenteDesignation.value = storedPointDeVenteDesignation;
if (_pointDeVenteDesignation.value.isEmpty && _pointDeVenteId.value > 0) { if (_pointDeVenteDesignation.value.isEmpty &&
_pointDeVenteId.value > 0) {
await loadPointDeVenteDesignation(); await loadPointDeVenteDesignation();
} }
// Précharger les permissions en arrière-plan (non bloquant) // Précharger les permissions en arrière-plan (non bloquant)
_preloadPermissionsInBackground(); _preloadPermissionsInBackground();
} catch (dbError) { } catch (dbError) {
print("❌ Erreur BDD, utilisation du fallback: $dbError"); print("❌ Erreur BDD, utilisation du fallback: $dbError");
_username.value = storedUsername; _username.value = storedUsername;
@ -105,13 +106,15 @@ class UserController extends GetxController {
if (_pointDeVenteId.value <= 0) return; if (_pointDeVenteId.value <= 0) return;
try { try {
final pointDeVente = await AppDatabase.instance.getPointDeVenteById(_pointDeVenteId.value); final pointDeVente =
await AppDatabase.instance.getPointDeVenteById(_pointDeVenteId.value);
if (pointDeVente != null) { if (pointDeVente != null) {
_pointDeVenteDesignation.value = pointDeVente['nom'] as String; _pointDeVenteDesignation.value = pointDeVente['nom'] as String;
await saveUserData(); await saveUserData();
} }
} catch (e) { } catch (e) {
print('❌ Erreur lors du chargement de la désignation du point de vente: $e'); print(
'❌ Erreur lors du chargement de la désignation du point de vente: $e');
} }
} }
@ -160,7 +163,8 @@ class UserController extends GetxController {
await prefs.setString('lastname', _lastname.value); await prefs.setString('lastname', _lastname.value);
await prefs.setInt('user_id', _userId.value); await prefs.setInt('user_id', _userId.value);
await prefs.setInt('point_de_vente_id', _pointDeVenteId.value); await prefs.setInt('point_de_vente_id', _pointDeVenteId.value);
await prefs.setString('point_de_vente_designation', _pointDeVenteDesignation.value); await prefs.setString(
'point_de_vente_designation', _pointDeVenteDesignation.value);
print("✅ Données sauvegardées avec succès"); print("✅ Données sauvegardées avec succès");
} catch (e) { } catch (e) {
@ -198,7 +202,6 @@ class UserController extends GetxController {
_pointDeVenteDesignation.value = ''; _pointDeVenteDesignation.value = '';
print("✅ Données utilisateur et cache de session vidés"); print("✅ Données utilisateur et cache de session vidés");
} catch (e) { } catch (e) {
print('❌ Erreur lors de l\'effacement: $e'); print('❌ Erreur lors de l\'effacement: $e');
} }
@ -231,12 +234,12 @@ class UserController extends GetxController {
await _cacheService.loadUserPermissions(_username.value); await _cacheService.loadUserPermissions(_username.value);
return _cacheService.hasPermission(_username.value, permission, route); return _cacheService.hasPermission(_username.value, permission, route);
} catch (e) { } catch (e) {
print('❌ Erreur vérification permission: $e'); print('❌ Erreur vérification permission: $e');
// Fallback vers la méthode originale en cas d'erreur // Fallback vers la méthode originale en cas d'erreur
try { try {
return await AppDatabase.instance.hasPermission(_username.value, permission, route); return await AppDatabase.instance
.hasPermission(_username.value, permission, route);
} catch (fallbackError) { } catch (fallbackError) {
print('❌ Erreur fallback permission: $fallbackError'); print('❌ Erreur fallback permission: $fallbackError');
return false; return false;
@ -245,7 +248,8 @@ class UserController extends GetxController {
} }
/// Vérification de permissions multiples /// Vérification de permissions multiples
Future<bool> hasAnyPermission(List<String> permissionNames, String menuRoute) async { Future<bool> hasAnyPermission(
List<String> permissionNames, String menuRoute) async {
for (String permissionName in permissionNames) { for (String permissionName in permissionNames) {
if (await hasPermission(permissionName, menuRoute)) { if (await hasPermission(permissionName, menuRoute)) {
return true; return true;

Loading…
Cancel
Save