kz_educa/lib/models/financial_transaction.dart

67 lines
1.5 KiB
Dart

enum TransactionType { expense, income }
enum TransactionCategory {
food,
transport,
social,
education,
medical,
shopping,
salary,
invest,
business,
others,
}
class FinancialTransaction {
final int? id;
final String title;
final double amount;
final TransactionType type;
final TransactionCategory category;
final DateTime date;
final String? note;
FinancialTransaction({
this.id,
required this.title,
required this.amount,
required this.type,
required this.category,
required this.date,
this.note,
});
Map<String, dynamic> toMap() {
return {
'id': id,
'title': title,
'amount': amount,
'type': type.name,
'category': category.name,
'date': date.toIso8601String(),
'note': note,
};
}
factory FinancialTransaction.fromMap(Map<String, dynamic> map) {
return FinancialTransaction(
id: map['id'] is int
? map['id'] as int
: int.tryParse(map['id']?.toString() ?? ''),
title: map['title']?.toString() ?? 'Sem título',
amount: (map['amount'] as num?)?.toDouble() ?? 0.0,
type: TransactionType.values.firstWhere(
(e) => e.name == map['type'],
orElse: () => TransactionType.expense,
),
category: TransactionCategory.values.firstWhere(
(e) => e.name == map['category'],
orElse: () => TransactionCategory.others,
),
date: DateTime.tryParse(map['date']?.toString() ?? '') ?? DateTime.now(),
note: map['note']?.toString(),
);
}
}