Skip to content

Commit

Permalink
improved recurring transactions logic
Browse files Browse the repository at this point in the history
  • Loading branch information
mikev-cw committed Apr 25, 2024
1 parent 08d06e1 commit b146b83
Show file tree
Hide file tree
Showing 2 changed files with 193 additions and 11 deletions.
19 changes: 8 additions & 11 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,29 +30,26 @@ void main() async {
getPref == null ? await preferences.setBool('is_first_login', false) : null;
_isFirstLogin = getPref;



// TODO PENDING FIXME remove this line
preferences.remove('last_recurring_transactions_check');



// perform recurring transactions checks
DateTime? lastCheckGetPref = preferences.getString('last_recurring_transactions_check') != null ? DateTime.parse(preferences.getString('last_recurring_transactions_check')!) : null;
DateTime? lastRecurringTransactionsCheck = lastCheckGetPref;

if(lastRecurringTransactionsCheck == null || DateTime.now().difference(lastRecurringTransactionsCheck).inDays >= 1){
checkRecurringTransactions();
RecurringTransactionMethods().checkRecurringTransactions();
// update last recurring transactions runtime
await preferences.setString('last_recurring_transactions_check', DateTime.now().toIso8601String());
}

initializeDateFormatting('it_IT', null).then((_) => runApp(const ProviderScope(child: Launcher())));
}

// put here the function to check recurring transactions
void checkRecurringTransactions() async {
// get all recurring transactions
final accounts = await RecurringTransactionMethods().selectAll();
// check if the recurring transaction is due
// if it is due, insert a new transaction
// update the last insertion date
// update the recurring transaction
}

class Launcher extends ConsumerWidget {
const Launcher({super.key});

Expand Down
185 changes: 185 additions & 0 deletions lib/model/recurring_transaction.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import '../database/sossoldi_database.dart';
import 'transaction.dart';
import 'base_entity.dart';

const String recurringTransactionTable = 'recurringTransaction';
Expand Down Expand Up @@ -29,6 +30,44 @@ class RecurringTransactionFields extends BaseEntityFields {
];
}

Map<String, dynamic> recurrenciesMap = {
'DAILY': {
'label': 'Daily',
'entity': 'days',
'amount': 1
},
'WEEKLY': {
'label': 'Weekly',
'entity': 'days',
'amount': 7
},
'MONTHLY': {
'label': 'Monthly',
'entity': 'months',
'amount': 1
},
'BIMONTHLY': {
'label': 'Bimonthly',
'entity': 'months',
'amount': 2
},
'QUARTERLY': {
'label': 'Quarterly',
'entity': 'months',
'amount': 3
},
'SEMESTER': {
'label': 'Semester',
'entity': 'months',
'amount': 6
},
'YEARLY': {
'label': 'Yearly',
'entity': 'months',
'amount': 12
},
};

class RecurringTransaction extends BaseEntity {
final DateTime fromDate;
final DateTime? toDate;
Expand Down Expand Up @@ -142,6 +181,21 @@ class RecurringTransactionMethods extends SossoldiDatabase {
return result.map((json) => RecurringTransaction.fromJson(json)).toList();
}

Future<List<RecurringTransaction>> selectAllActive() async {
final db = await database;

final orderByASC = '${RecurringTransactionFields.createdAt} ASC';

final result = await db.query(
recurringTransactionTable,
orderBy: orderByASC,
where: '${RecurringTransactionFields.toDate} IS NULL OR ${RecurringTransactionFields.toDate} > ?',
whereArgs: [DateTime.now().toIso8601String()],
);

return result.map((json) => RecurringTransaction.fromJson(json)).toList();
}

Future<int> updateItem(RecurringTransaction item) async {
final db = await database;

Expand All @@ -164,4 +218,135 @@ class RecurringTransactionMethods extends SossoldiDatabase {
whereArgs: [id]);
}


Future<void> checkRecurringTransactions() async {
// get all recurring transactions active
final transactions = await selectAllActive();

if (transactions.isEmpty) {
return;
}

for (var transaction in transactions) {
print("-------- transaction: ${transaction.note} -----------");

DateTime lastTransactionDate;

try {
lastTransactionDate = await _getLastRecurringTransactionInsertion(transaction.id ?? 0);
} catch (e) {
lastTransactionDate = transaction.fromDate;
}

String entity = recurrenciesMap[transaction.recurrency]?['entity'] ?? 'UNMAPPED';
int entityAmt = recurrenciesMap[transaction.recurrency]?['amount'] ?? 0;

print(recurrenciesMap[transaction.recurrency]);

try {
if (entityAmt == 0) {
throw Exception('No amount provided for entity "$entity"');
}

populateRecurringTransaction(entity, lastTransactionDate, transaction, entityAmt);

} catch (e) {
// TODO show an error to the user?
print("ERROR TRYING TO POPULATE RECURRING TRANSACTIONS: ${e.toString()}");
}
}
}

Future<DateTime> _getLastRecurringTransactionInsertion(int tid) async {
if (tid == 0) {
throw Exception('No transaction ID provided');
}

final db = await database;

final orderByASC = '${TransactionFields.date} ASC';

final result = await db.query(
transactionTable,
orderBy: orderByASC,
where: '${TransactionFields.idRecurringTransaction} = ?',
whereArgs: [tid],
limit: 1,
);

if (result.isEmpty) {
throw Exception('No transaction found for ID $tid');
}

return Transaction.fromJson(result.first).date;
}

void populateRecurringTransaction(String scope, DateTime lastTransactionDate, RecurringTransaction transaction, int amount) {

if (amount == 0) {
throw Exception('No amount provided for entity "$scope"');
}

DateTime now = DateTime.now();

// create a list to store the months
List<DateTime> transactions2Add = [];

// calculate the number of periods between the current date and the last transaction insertion
int periods;

switch (scope) {
case 'days':
periods = now.difference(lastTransactionDate).inDays;
break;
case 'months':
periods = (((now.year - lastTransactionDate.year) * 12 + now.month - lastTransactionDate.month)/amount).floor();
break;
default:
throw Exception('No scope provided');
}

print("periods: $periods");

// for each period passed, insert a new transaction
for (int i = 0; i < periods; i++) {
switch (scope) {
case 'days':
lastTransactionDate = DateTime(lastTransactionDate.year, lastTransactionDate.month, lastTransactionDate.day + amount);
break;
case 'months':
// get the last day of the next period
int lastDayOfNextPeriod = DateTime(lastTransactionDate.year, (lastTransactionDate.month + amount + 1), 0).day;
int dayOfInsertion = transaction.fromDate.day;

// if the next period's month has fewer days than the day of the last transaction insertion, adjust the day
if (transaction.fromDate.day > lastDayOfNextPeriod) {
dayOfInsertion = lastDayOfNextPeriod;
}

lastTransactionDate = DateTime(lastTransactionDate.year, lastTransactionDate.month + amount, dayOfInsertion);

break;
default:
//nothing to do
return;
}

if (transaction.toDate?.isAfter(lastTransactionDate) ?? true) {
transactions2Add.add(lastTransactionDate);
}

}

for (var tr in transactions2Add) {
// insert a new transaction
print(tr);

}

// update the last insertion date
// update the recurring transaction

}

}

0 comments on commit b146b83

Please sign in to comment.