Files
fin65-mobile/lib/models/account.dart
T

91 lines
2.3 KiB
Dart

class Account {
final int id;
final String name;
final String type;
final String currency;
final double initialBalance;
final double appBalance;
final bool isActive;
Account({
required this.id,
required this.name,
required this.type,
required this.currency,
required this.initialBalance,
required this.appBalance,
required this.isActive,
});
factory Account.fromJson(Map<String, dynamic> json) {
return Account(
id: json['id'] as int,
name: json['name'] as String,
type: json['type'] as String,
currency: json['currency'] as String? ?? 'MAD',
initialBalance: (json['initial_balance'] as num? ?? 0).toDouble(),
appBalance: (json['app_balance'] as num? ?? 0).toDouble(),
isActive: json['is_active'] is int
? (json['is_active'] as int) == 1
: json['is_active'] as bool? ?? true,
);
}
Map<String, dynamic> toJson() {
return {
'id': id,
'name': name,
'type': type,
'currency': currency,
'initial_balance': initialBalance,
'app_balance': appBalance,
'is_active': isActive,
};
}
Map<String, dynamic> toDbMap() {
return {
'id': id,
'name': name,
'type': type,
'currency': currency,
'initial_balance': initialBalance,
'app_balance': appBalance,
'is_active': isActive ? 1 : 0,
'updated_at': DateTime.now().toIso8601String(),
};
}
factory Account.fromDbMap(Map<String, dynamic> map) {
return Account(
id: map['id'] as int,
name: map['name'] as String,
type: map['type'] as String,
currency: map['currency'] as String? ?? 'MAD',
initialBalance: (map['initial_balance'] as num? ?? 0).toDouble(),
appBalance: (map['app_balance'] as num? ?? 0).toDouble(),
isActive: (map['is_active'] as int? ?? 1) == 1,
);
}
Account copyWith({
int? id,
String? name,
String? type,
String? currency,
double? initialBalance,
double? appBalance,
bool? isActive,
}) {
return Account(
id: id ?? this.id,
name: name ?? this.name,
type: type ?? this.type,
currency: currency ?? this.currency,
initialBalance: initialBalance ?? this.initialBalance,
appBalance: appBalance ?? this.appBalance,
isActive: isActive ?? this.isActive,
);
}
}