fix: actually make recurring entries work correctly

This commit is contained in:
Matyáš Caras 2024-01-08 23:22:35 +01:00
parent b72e811339
commit e40f0491fc
Signed by untrusted user who does not match committer: hernik
GPG key ID: 2A3175F98820C5C6
2 changed files with 44 additions and 16 deletions

View file

@ -1,6 +1,7 @@
import 'package:currency_picker/currency_picker.dart'; import 'package:currency_picker/currency_picker.dart';
import 'package:intl/intl.dart'; import 'package:intl/intl.dart';
import 'package:json_annotation/json_annotation.dart'; import 'package:json_annotation/json_annotation.dart';
import 'package:logger/logger.dart';
import 'package:prasule/api/category.dart'; import 'package:prasule/api/category.dart';
import 'package:prasule/api/recurring_entry.dart'; import 'package:prasule/api/recurring_entry.dart';
import 'package:prasule/api/wallet_entry.dart'; import 'package:prasule/api/wallet_entry.dart';
@ -56,9 +57,11 @@ class Wallet {
/// Getter for the next unused unique number ID in the wallet's **entry** list /// Getter for the next unused unique number ID in the wallet's **entry** list
int get nextId { int get nextId {
var id = 1; var id = 1;
logger.d("Making ID after ${entries.length} entries");
while (entries.where((element) => element.id == id).isNotEmpty) { while (entries.where((element) => element.id == id).isNotEmpty) {
id++; // create unique ID id++; // create unique ID
} }
logger.d("New ID: $id\nLast entry ID: ${entries.lastOrNull?.id}");
return id; return id;
} }
@ -91,33 +94,57 @@ class Wallet {
while (n.isAfter( while (n.isAfter(
m, m,
)) { )) {
logger.i("Adding recurring entry ${ent.data.name}"); logger
..i("Adding recurring entry ${ent.data.name}")
..d("Current entry count: ${entries.length}");
recurringEntries[recurringEntries.indexOf(ent)].lastRunDate = recurringEntries[recurringEntries.indexOf(ent)].lastRunDate =
m; // update date on recurring entry m; // update date on recurring entry
logger.i(recurringEntries[recurringEntries.indexOf(ent)].lastRunDate); logger.i(recurringEntries[recurringEntries.indexOf(ent)].lastRunDate);
final addedEntry = (recurringEntries[recurringEntries.indexOf(ent)]
as WalletSingleEntry) var id = 1;
..date = DateTime.now() logger.d("Making ID after ${entries.length} entries");
..id = nextId; // copy entry with today's date and unique ID while (entries.where((element) => element.id == id).isNotEmpty) {
entries.add( id++; // create unique ID
addedEntry, }
); // add it to entries logger.d("New ID: $id\nLast entry ID: ${entries.lastOrNull?.id}");
final addedEntry = WalletSingleEntry(
data: recurringEntries[recurringEntries.indexOf(ent)].data,
type: recurringEntries[recurringEntries.indexOf(ent)].type,
date: m,
category: recurringEntries[recurringEntries.indexOf(ent)].category,
id: id,
);
entries.add(addedEntry);
m = DateTime( m = DateTime(
(ent.recurType == RecurType.year) (ent.recurType == RecurType.year)
? ent.lastRunDate.year + ent.repeatAfter ? recurringEntries[recurringEntries.indexOf(ent)]
: ent.lastRunDate.year, .lastRunDate
.year +
ent.repeatAfter
: recurringEntries[recurringEntries.indexOf(ent)]
.lastRunDate
.year,
(ent.recurType == RecurType.month) (ent.recurType == RecurType.month)
? ent.lastRunDate.month + ent.repeatAfter ? recurringEntries[recurringEntries.indexOf(ent)]
: ent.lastRunDate.month, .lastRunDate
.month +
ent.repeatAfter
: recurringEntries[recurringEntries.indexOf(ent)]
.lastRunDate
.month,
(ent.recurType == RecurType.day) (ent.recurType == RecurType.day)
? ent.lastRunDate.day + ent.repeatAfter ? recurringEntries[recurringEntries.indexOf(ent)]
: ent.lastRunDate.day, .lastRunDate
); // add tne variable again to check if we aren't missing any entries .day +
ent.repeatAfter
: recurringEntries[recurringEntries.indexOf(ent)].lastRunDate.day,
); // add the variable again to check if we aren't missing any entries
logger.i( logger.i(
"Last recurred date is now on ${DateFormat.yMMMMd().format(m)} (${n.isAfter(m)})"); "Last recurred date is now on ${DateFormat.yMMMMd().format(m)} (${n.isAfter(m)})");
} }
WalletManager.saveWallet(this); // save wallet WalletManager.saveWallet(this); // save wallet
} }
} }

View file

@ -374,6 +374,7 @@ class _HomeViewState extends State<HomeView> {
selectedWallet?.entries.removeWhere( selectedWallet?.entries.removeWhere(
(e) => e.id == element.id, (e) => e.id == element.id,
); );
WalletManager.saveWallet( WalletManager.saveWallet(
selectedWallet!, selectedWallet!,
); );