Feat: Generiert alle Transaktionen, die benötigt werden
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import 'package:drift/drift.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:logger/logger.dart';
|
||||
|
||||
import '../Entities/drift_database.dart';
|
||||
@@ -24,7 +25,7 @@ class GenerateTransactionsTask extends Task {
|
||||
await _recurringTransactionRepository.findBy();
|
||||
|
||||
for (final recurringTransaction in recurringTransactions) {
|
||||
_logger.i('Generating Transactions of $recurringTransaction...');
|
||||
_logger.i('Generating Transactions of ${recurringTransaction.name}...');
|
||||
|
||||
final List<Transaction> transactions = await _transactionRepository
|
||||
.findBy(
|
||||
@@ -33,27 +34,55 @@ class GenerateTransactionsTask extends Task {
|
||||
);
|
||||
final Transaction? transaction = transactions.firstOrNull;
|
||||
|
||||
final DateTime date = (transaction != null)
|
||||
? transaction.date!
|
||||
: recurringTransaction.startDate!;
|
||||
final DateTime nextTransactionDate;
|
||||
|
||||
switch (recurringTransaction.timeFrame) {
|
||||
case TimeFrameEnum.daily:
|
||||
nextTransactionDate = date.add(const Duration(days: 1));
|
||||
case TimeFrameEnum.weekly:
|
||||
nextTransactionDate = date.add(const Duration(days: 7));
|
||||
case TimeFrameEnum.monthly:
|
||||
nextTransactionDate = DateTime(date.year, date.month + 1, date.day);
|
||||
case TimeFrameEnum.yearly:
|
||||
nextTransactionDate = DateTime(date.year + 1, date.month, date.day);
|
||||
await _generateTransactions(recurringTransaction, transaction?.date);
|
||||
}
|
||||
|
||||
if (DateTime.now().compareTo(nextTransactionDate) <= 0) {
|
||||
// TODO: Nicht mit NOW, sondern Ende dieses Monats Vergleichen
|
||||
_logger.i('Generating transactions completed.');
|
||||
return true;
|
||||
}
|
||||
|
||||
Future<bool> _generateTransactions(
|
||||
final RecurringTransaction recurringTransaction,
|
||||
final DateTime? lastTransactionDate, {
|
||||
final int number = 0,
|
||||
}) async {
|
||||
final DateTime newTransactionDate;
|
||||
|
||||
if (lastTransactionDate != null) {
|
||||
switch (recurringTransaction.timeFrame) {
|
||||
case TimeFrameEnum.daily:
|
||||
newTransactionDate = lastTransactionDate.add(const Duration(days: 1));
|
||||
case TimeFrameEnum.weekly:
|
||||
newTransactionDate = lastTransactionDate.add(const Duration(days: 7));
|
||||
case TimeFrameEnum.monthly:
|
||||
newTransactionDate = DateUtils.addMonthsToMonthDate(
|
||||
lastTransactionDate,
|
||||
1,
|
||||
);
|
||||
case TimeFrameEnum.yearly:
|
||||
newTransactionDate = DateUtils.addMonthsToMonthDate(
|
||||
lastTransactionDate,
|
||||
12,
|
||||
);
|
||||
}
|
||||
} else {
|
||||
newTransactionDate = recurringTransaction.startDate!;
|
||||
}
|
||||
|
||||
_logger.i('New transaction-date is $newTransactionDate');
|
||||
|
||||
final DateTime endOfMonth = DateTime(
|
||||
DateTime.now().year,
|
||||
DateTime.now().month + 1,
|
||||
0,
|
||||
);
|
||||
|
||||
if (endOfMonth.compareTo(newTransactionDate) >= 0) {
|
||||
_logger.i('$newTransactionDate is before $endOfMonth');
|
||||
|
||||
final TransactionsCompanion transaction = TransactionsCompanion(
|
||||
name: Value(recurringTransaction.name),
|
||||
date: Value(nextTransactionDate),
|
||||
name: Value('${recurringTransaction.name} #$number'),
|
||||
date: Value(newTransactionDate),
|
||||
amount: Value(recurringTransaction.amount),
|
||||
checked: const Value(false),
|
||||
accountId: Value(recurringTransaction.accountId),
|
||||
@@ -61,14 +90,20 @@ class GenerateTransactionsTask extends Task {
|
||||
);
|
||||
|
||||
_logger.i(
|
||||
'Adding transaction ${transaction.name} on ${transaction.date}',
|
||||
'Adding transaction ${transaction.name.value}'
|
||||
' on ${transaction.date.value}',
|
||||
);
|
||||
|
||||
await _transactionRepository.add(transaction);
|
||||
}
|
||||
}
|
||||
|
||||
_logger.i('Generating transactions completed.');
|
||||
return _generateTransactions(
|
||||
recurringTransaction,
|
||||
newTransactionDate,
|
||||
number: number + 1,
|
||||
);
|
||||
} else {
|
||||
_logger.i('$newTransactionDate is after $endOfMonth');
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user