Skip to content

Commit

Permalink
fix/account-exclusion-recurring-transactions
Browse files Browse the repository at this point in the history
- Corrected the SQL query to ensure that active bank accounts with only
  recurring transactions are no longer excluded from the results.
- Added a new `recurringFilter` variable to clearly define and encapsulate the
  logic for filtering out non-recurring transactions.
- Adjusted the `LEFT JOIN` clause to apply the recurring filter within the `ON`
  condition, preserving all active accounts while accurately calculating their
  balances.
- This fix resolves an issue where certain accounts disappeared from the query
  results, ensuring all active accounts are correctly handled in the balance
  calculation.
  • Loading branch information
gioisco committed Aug 15, 2024
1 parent 85cfb42 commit 76a795c
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions lib/model/bank_account.dart
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@ class BankAccountMethods extends SossoldiDatabase {
final db = await database;

final orderByASC = '${BankAccountFields.createdAt} ASC';
final where =
'${BankAccountFields.active} = 1 AND (${TransactionFields.recurring} = 0 OR ${TransactionFields.recurring} is NULL)';
final where = '${BankAccountFields.active} = 1 ';
final recurringFilter = '(t.${TransactionFields.recurring} = 0 OR t.${TransactionFields.recurring} IS NULL)';

final result = await db.rawQuery('''
SELECT b.*, (b.${BankAccountFields.startingValue} +
Expand All @@ -160,7 +160,10 @@ class BankAccountMethods extends SossoldiDatabase {
ELSE 0 END)
) as ${BankAccountFields.total}
FROM $bankAccountTable as b
LEFT JOIN "$transactionTable" as t ON t.${TransactionFields.idBankAccount} = b.${BankAccountFields.id} OR t.${TransactionFields.idBankAccountTransfer} = b.${BankAccountFields.id}
LEFT JOIN "$transactionTable" as t
ON (t.${TransactionFields.idBankAccount} = b.${BankAccountFields.id} OR
t.${TransactionFields.idBankAccountTransfer} = b.${BankAccountFields.id})
AND $recurringFilter
WHERE $where
GROUP BY b.${BankAccountFields.id}
ORDER BY $orderByASC
Expand Down

0 comments on commit 76a795c

Please sign in to comment.