Feat: Macht die Dashboard-Seite funktional
This commit is contained in:
@@ -13,6 +13,10 @@ class TransactionRepository {
|
||||
return find(id);
|
||||
}
|
||||
|
||||
/// Aktualisiert eine Transaktion in der Datenbank
|
||||
Future<bool> update(final TransactionsCompanion transaction) =>
|
||||
_db.update(_db.transactions).replace(transaction);
|
||||
|
||||
/// Entfernt eine Transaktion aus der Datenbank
|
||||
Future<int> remove(final Transaction transaction) => (_db.delete(
|
||||
_db.transactions,
|
||||
@@ -28,11 +32,14 @@ class TransactionRepository {
|
||||
final int? id,
|
||||
final String? name,
|
||||
final DateTime? date,
|
||||
final DateTime? dateBefore,
|
||||
final DateTime? dateAfter,
|
||||
final DateTime? dateFrom,
|
||||
final DateTime? dateTo,
|
||||
final double? amount,
|
||||
final double? amountMin,
|
||||
final double? amountMax,
|
||||
final Account? account,
|
||||
final int? limit,
|
||||
final int? offset,
|
||||
final String? orderBy,
|
||||
}) {
|
||||
final SimpleSelectStatement<$TransactionsTable, Transaction> query = _db
|
||||
@@ -50,12 +57,12 @@ class TransactionRepository {
|
||||
query.where((final t) => t.date.equals(date));
|
||||
}
|
||||
|
||||
if (dateBefore != null) {
|
||||
query.where((final t) => t.date.isBiggerThanValue(dateBefore));
|
||||
if (dateFrom != null) {
|
||||
query.where((final t) => t.date.isBiggerThanValue(dateFrom));
|
||||
}
|
||||
|
||||
if (dateAfter != null) {
|
||||
query.where((final t) => t.date.isSmallerThanValue(dateAfter));
|
||||
if (dateTo != null) {
|
||||
query.where((final t) => t.date.isSmallerThanValue(dateTo));
|
||||
}
|
||||
|
||||
if (amount != null) {
|
||||
@@ -70,6 +77,14 @@ class TransactionRepository {
|
||||
query.where((final t) => t.amount.isSmallerThanValue(amountMax));
|
||||
}
|
||||
|
||||
if (account != null) {
|
||||
query.where((final t) => t.accountId.equals(account.id));
|
||||
}
|
||||
|
||||
if (limit != null) {
|
||||
query.limit(limit, offset: offset);
|
||||
}
|
||||
|
||||
if (orderBy != null) {
|
||||
switch (orderBy) {
|
||||
case 'nameAsc':
|
||||
@@ -89,4 +104,124 @@ class TransactionRepository {
|
||||
|
||||
return query.get();
|
||||
}
|
||||
|
||||
/// Gibt den Kontostand zurück
|
||||
Future<double> balance({
|
||||
final Account? account,
|
||||
final DateTime? until,
|
||||
}) async {
|
||||
final JoinedSelectStatement<$TransactionsTable, Transaction> query =
|
||||
_db.selectOnly(_db.transactions)
|
||||
..addColumns([_db.transactions.amount.sum()]);
|
||||
|
||||
if (account != null) {
|
||||
query.where(_db.transactions.accountId.equals(account.id));
|
||||
}
|
||||
|
||||
if (until != null) {
|
||||
query.where(_db.transactions.date.isSmallerOrEqualValue(until));
|
||||
}
|
||||
|
||||
return (await query
|
||||
.map((final row) => row.read(_db.transactions.amount.sum()) ?? 0)
|
||||
.getSingle()) *
|
||||
-1;
|
||||
}
|
||||
|
||||
/// Gibt den Kontostand der letzten 12 Monate zurück
|
||||
Future<List<Map<String, dynamic>>> monthlyBalances({
|
||||
final Account? account,
|
||||
final String? name,
|
||||
final double? amountMin,
|
||||
final double? amountMax,
|
||||
DateTime? dateFrom,
|
||||
DateTime? dateTo,
|
||||
}) async {
|
||||
final now = DateTime.now();
|
||||
|
||||
final monthStart = DateTime(now.year, now.month - 12);
|
||||
final monthEnd = DateTime(now.year, now.month + 1, 0);
|
||||
|
||||
if (dateFrom == null || dateFrom.compareTo(monthStart) < 0) {
|
||||
dateFrom = monthStart;
|
||||
}
|
||||
|
||||
if (dateTo == null || dateTo.compareTo(monthEnd) > 0) {
|
||||
dateTo = monthEnd;
|
||||
}
|
||||
|
||||
final Expression<int> yearExpr = _db.transactions.date.year;
|
||||
final Expression<int> monthExpr = _db.transactions.date.month;
|
||||
final Expression<double> sumExpr = _db.transactions.amount.sum();
|
||||
|
||||
final JoinedSelectStatement<$TransactionsTable, Transaction> query =
|
||||
_db.selectOnly(_db.transactions)
|
||||
..addColumns([yearExpr, monthExpr, sumExpr])
|
||||
..groupBy([yearExpr, monthExpr])
|
||||
..orderBy([OrderingTerm.asc(yearExpr), OrderingTerm.asc(monthExpr)])
|
||||
..where(_db.transactions.date.isBiggerOrEqualValue(dateFrom))
|
||||
..where(_db.transactions.date.isSmallerOrEqualValue(dateTo));
|
||||
|
||||
if (account != null) {
|
||||
query.where(_db.transactions.accountId.equals(account.id));
|
||||
}
|
||||
|
||||
if (name != null && name.isNotEmpty) {
|
||||
query.where(_db.transactions.name.like('%$name%'));
|
||||
}
|
||||
|
||||
if (amountMin != null) {
|
||||
query.where(_db.transactions.amount.isBiggerOrEqualValue(amountMin));
|
||||
}
|
||||
|
||||
if (amountMax != null) {
|
||||
query.where(_db.transactions.amount.isSmallerOrEqualValue(amountMax));
|
||||
}
|
||||
|
||||
final List<Map<String, Object>> rows = (await query.get()).map((final row) {
|
||||
final int year = row.read(yearExpr)!;
|
||||
final int month = row.read(monthExpr)!;
|
||||
|
||||
return {
|
||||
'date': DateTime(year, month),
|
||||
'balance': (row.read(sumExpr) ?? 0) * -1,
|
||||
};
|
||||
}).toList();
|
||||
|
||||
double amount = await balance(account: account, until: dateFrom);
|
||||
|
||||
DateTime dateTimeLoop = dateFrom;
|
||||
int loop = 0;
|
||||
|
||||
final List<Map<String, Object>> result = [];
|
||||
|
||||
while (dateTimeLoop.compareTo(monthEnd) < 0) {
|
||||
Map<String, Object>? row;
|
||||
for (final value in rows) {
|
||||
final Object? rowDate = value['date'];
|
||||
|
||||
if (rowDate is DateTime) {
|
||||
if (dateTimeLoop.compareTo(rowDate) == 0) {
|
||||
row = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (row == null) {
|
||||
result.add({'date': dateTimeLoop, 'balance': 0.0});
|
||||
} else {
|
||||
result.add(row);
|
||||
}
|
||||
|
||||
final double balance = double.parse(result[loop]['balance'].toString());
|
||||
amount = balance + amount;
|
||||
|
||||
result[loop]['balance'] = amount;
|
||||
|
||||
loop = loop + 1;
|
||||
dateTimeLoop = DateTime(dateTimeLoop.year, dateTimeLoop.month + 1);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user