95 lines
2.6 KiB
Dart
Executable File
95 lines
2.6 KiB
Dart
Executable File
import 'package:cloud_firestore/cloud_firestore.dart';
|
|
import 'package:hive/hive.dart';
|
|
import 'transaction.dart';
|
|
|
|
part 'budget.g.dart';
|
|
|
|
@HiveType(typeId: 3)
|
|
enum BudgetPeriod {
|
|
@HiveField(0)
|
|
week,
|
|
@HiveField(1)
|
|
month,
|
|
@HiveField(2)
|
|
quarter,
|
|
@HiveField(3)
|
|
year,
|
|
}
|
|
|
|
@HiveType(typeId: 4)
|
|
class Budget {
|
|
@HiveField(0)
|
|
String? id;
|
|
@HiveField(1)
|
|
TransactionCategory category;
|
|
@HiveField(2)
|
|
double amount;
|
|
@HiveField(3)
|
|
BudgetPeriod period;
|
|
@HiveField(4)
|
|
DateTime startDate;
|
|
@HiveField(5)
|
|
DateTime endDate;
|
|
@HiveField(6)
|
|
bool repeatBudget;
|
|
@HiveField(7)
|
|
String? note;
|
|
|
|
Budget({
|
|
this.id,
|
|
required this.category,
|
|
required this.amount,
|
|
required this.period,
|
|
required this.startDate,
|
|
required this.endDate,
|
|
this.repeatBudget = false,
|
|
this.note,
|
|
});
|
|
|
|
// Método toMap para o Firestore
|
|
Map<String, dynamic> toMap() {
|
|
return {
|
|
// O ID não é armazenado aqui, é o ID do documento
|
|
'category': category.index,
|
|
'amount': amount,
|
|
'period': period.index, // Armazena o índice para ser compatível com Firestore
|
|
'startDate': Timestamp.fromDate(startDate),
|
|
'endDate': Timestamp.fromDate(endDate),
|
|
'repeatBudget': repeatBudget,
|
|
'note': note,
|
|
};
|
|
}
|
|
|
|
// Construtor de fábrica para desserializar a partir de um documento do Firestore
|
|
factory Budget.fromFirestore(DocumentSnapshot doc) {
|
|
final data = doc.data() as Map<String, dynamic>;
|
|
return Budget(
|
|
id: doc.id,
|
|
category: TransactionCategory.values[data['category'] as int],
|
|
amount: data['amount'] as double,
|
|
period: BudgetPeriod.values[data['period'] as int],
|
|
startDate: (data['startDate'] as Timestamp).toDate(),
|
|
endDate: (data['endDate'] as Timestamp).toDate(),
|
|
repeatBudget: data['repeatBudget'] as bool,
|
|
note: data['note'],
|
|
);
|
|
}
|
|
|
|
// Construtor de fábrica original para o sqflite (se ainda for necessário)
|
|
// O seu código atual não precisa mais deste construtor `fromMap`,
|
|
// pois o `DatabaseService` não interage mais com orçamentos.
|
|
// Pode ser removido, mas foi mantido aqui caso precise de um exemplo.
|
|
factory Budget.fromMap(Map<String, dynamic> map) {
|
|
return Budget(
|
|
id: map['id'],
|
|
category: TransactionCategory.values[map['category'] as int],
|
|
amount: map['amount'] as double,
|
|
period: BudgetPeriod.values.firstWhere((e) => e.name == map['period']),
|
|
startDate: DateTime.parse(map['startDate'] as String),
|
|
endDate: DateTime.parse(map['endDate'] as String),
|
|
repeatBudget: (map['repeatBudget'] as int) == 1,
|
|
note: map['note'] as String?,
|
|
);
|
|
}
|
|
}
|