feat: Add User and UserBalance models with serialization methods
feat: Implement BalanceRepository for group balance calculations feat: Create ExpenseRepository for managing expenses feat: Add services for handling expenses and storage operations fix: Update import paths for models in repositories and services refactor: Rename CountContent to AccountContent in HomePage chore: Add StorageService for image upload and management
This commit is contained in:
98
lib/models/user_balance.dart
Normal file
98
lib/models/user_balance.dart
Normal file
@@ -0,0 +1,98 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
|
||||
class UserBalance extends Equatable {
|
||||
final String userId;
|
||||
final String userName;
|
||||
final double totalPaid; // Total payé par cet utilisateur
|
||||
final double totalOwed; // Total dû par cet utilisateur
|
||||
final double balance; // Différence (positif = à recevoir, négatif = à payer)
|
||||
|
||||
const UserBalance({
|
||||
required this.userId,
|
||||
required this.userName,
|
||||
required this.totalPaid,
|
||||
required this.totalOwed,
|
||||
required this.balance,
|
||||
});
|
||||
|
||||
// Constructeur factory pour créer depuis une Map
|
||||
factory UserBalance.fromMap(Map<String, dynamic> map) {
|
||||
return UserBalance(
|
||||
userId: map['userId'] ?? '',
|
||||
userName: map['userName'] ?? '',
|
||||
totalPaid: (map['totalPaid'] as num?)?.toDouble() ?? 0.0,
|
||||
totalOwed: (map['totalOwed'] as num?)?.toDouble() ?? 0.0,
|
||||
balance: (map['balance'] as num?)?.toDouble() ?? 0.0,
|
||||
);
|
||||
}
|
||||
|
||||
// Convertir en Map pour la sérialisation
|
||||
Map<String, dynamic> toMap() {
|
||||
return {
|
||||
'userId': userId,
|
||||
'userName': userName,
|
||||
'totalPaid': totalPaid,
|
||||
'totalOwed': totalOwed,
|
||||
'balance': balance,
|
||||
};
|
||||
}
|
||||
|
||||
// Méthode copyWith pour créer une copie modifiée
|
||||
UserBalance copyWith({
|
||||
String? userId,
|
||||
String? userName,
|
||||
double? totalPaid,
|
||||
double? totalOwed,
|
||||
double? balance,
|
||||
}) {
|
||||
return UserBalance(
|
||||
userId: userId ?? this.userId,
|
||||
userName: userName ?? this.userName,
|
||||
totalPaid: totalPaid ?? this.totalPaid,
|
||||
totalOwed: totalOwed ?? this.totalOwed,
|
||||
balance: balance ?? this.balance,
|
||||
);
|
||||
}
|
||||
|
||||
// Constructeur factory pour recalculer automatiquement la balance
|
||||
factory UserBalance.calculated({
|
||||
required String userId,
|
||||
required String userName,
|
||||
required double totalPaid,
|
||||
required double totalOwed,
|
||||
}) {
|
||||
return UserBalance(
|
||||
userId: userId,
|
||||
userName: userName,
|
||||
totalPaid: totalPaid,
|
||||
totalOwed: totalOwed,
|
||||
balance: totalPaid - totalOwed,
|
||||
);
|
||||
}
|
||||
|
||||
// Getters pour la logique métier
|
||||
bool get shouldReceive => balance > 0;
|
||||
bool get shouldPay => balance < 0;
|
||||
bool get isBalanced => balance.abs() < 0.01; // Tolérance pour les arrondis
|
||||
|
||||
// Montants absolus pour l'affichage
|
||||
double get absoluteBalance => balance.abs();
|
||||
String get balanceStatus {
|
||||
if (isBalanced) return 'Équilibré';
|
||||
if (shouldReceive) return 'À recevoir';
|
||||
return 'À payer';
|
||||
}
|
||||
|
||||
// Formatage pour l'affichage
|
||||
String get formattedBalance => '${absoluteBalance.toStringAsFixed(2)} €';
|
||||
String get formattedTotalPaid => '${totalPaid.toStringAsFixed(2)} €';
|
||||
String get formattedTotalOwed => '${totalOwed.toStringAsFixed(2)} €';
|
||||
|
||||
@override
|
||||
List<Object?> get props => [userId, userName, balance];
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'UserBalance(userId: $userId, userName: $userName, balance: ${balance.toStringAsFixed(2)}€)';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user