Skip to content

Commit 70d842d

Browse files
committed
fix(account): count net worth misused
- Fix #312 - Also fix resetDatabase() to use the last db version correctly
1 parent 748156b commit 70d842d

9 files changed

Lines changed: 211 additions & 69 deletions

File tree

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import 'package:sqflite/sqflite.dart';
2+
import '../migration_base.dart';
3+
4+
// Models
5+
import '/model/bank_account.dart';
6+
7+
class AccountNetWorth extends Migration {
8+
AccountNetWorth() : super(
9+
version: 2,
10+
description: 'Add account net worth column'
11+
);
12+
13+
@override
14+
Future<void> up(Database db) async {
15+
const integerNotNull = 'INTEGER NOT NULL';
16+
17+
// Bank accounts Table
18+
await db.execute('''
19+
ALTER TABLE `$bankAccountTable` ADD COLUMN `${BankAccountFields.countNetWorth}` $integerNotNull CHECK (${BankAccountFields.countNetWorth} IN (0, 1)) DEFAULT 1;
20+
''');
21+
}
22+
}

lib/database/migrations/migration_registry.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ library;
1313

1414

1515
import '0001_initial_schema.dart';
16+
import '0002_account_net_worth.dart';
1617
import '../migration_base.dart';
1718

1819

@@ -24,7 +25,8 @@ import '../migration_base.dart';
2425
/// determines which runs first.
2526
List<Migration> getMigrations() {
2627
return [
27-
InitialSchema()
28+
InitialSchema(),
29+
AccountNetWorth(),
2830
// Add future migrations here
2931
];
3032
}

lib/database/sossoldi_database.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ class SossoldiDatabase {
351351
} catch (error) {
352352
throw Exception('DbBase.resetDatabase: $error');
353353
}
354-
await _createDB(_database!, 1);
354+
await _createDB(_database!, _migrationManager.latestVersion);
355355
}
356356

357357
Future clearDatabase() async {

lib/model/bank_account.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class BankAccountFields extends BaseEntityFields {
1414
static String color = 'color';
1515
static String startingValue = 'startingValue';
1616
static String active = 'active';
17+
static String countNetWorth = 'countNetWorth';
1718
static String mainAccount = 'mainAccount';
1819
static String total = 'total';
1920
static String createdAt = BaseEntityFields.getCreatedAt;
@@ -26,6 +27,7 @@ class BankAccountFields extends BaseEntityFields {
2627
color,
2728
startingValue,
2829
active,
30+
countNetWorth,
2931
mainAccount,
3032
BaseEntityFields.createdAt,
3133
BaseEntityFields.updatedAt
@@ -38,6 +40,7 @@ class BankAccount extends BaseEntity {
3840
final int color;
3941
final num startingValue;
4042
final bool active;
43+
final bool countNetWorth;
4144
final bool mainAccount;
4245
final num? total;
4346

@@ -48,6 +51,7 @@ class BankAccount extends BaseEntity {
4851
required this.color,
4952
required this.startingValue,
5053
required this.active,
54+
required this.countNetWorth,
5155
required this.mainAccount,
5256
this.total,
5357
super.createdAt,
@@ -61,6 +65,7 @@ class BankAccount extends BaseEntity {
6165
int? color,
6266
num? startingValue,
6367
bool? active,
68+
bool? countNetWorth,
6469
bool? mainAccount,
6570
DateTime? createdAt,
6671
DateTime? updatedAt,
@@ -72,6 +77,7 @@ class BankAccount extends BaseEntity {
7277
color: color ?? this.color,
7378
startingValue: startingValue ?? this.startingValue,
7479
active: active ?? this.active,
80+
countNetWorth: countNetWorth ?? this.countNetWorth,
7581
mainAccount: mainAccount ?? this.mainAccount,
7682
createdAt: createdAt ?? this.createdAt,
7783
updatedAt: updatedAt ?? this.updatedAt,
@@ -85,6 +91,7 @@ class BankAccount extends BaseEntity {
8591
color: json[BankAccountFields.color] as int,
8692
startingValue: json[BankAccountFields.startingValue] as num,
8793
active: json[BankAccountFields.active] == 1 ? true : false,
94+
countNetWorth: json[BankAccountFields.countNetWorth] == 1 ? true : false,
8895
mainAccount: json[BankAccountFields.mainAccount] == 1 ? true : false,
8996
total: json[BankAccountFields.total] as num?,
9097
createdAt: DateTime.parse(json[BaseEntityFields.createdAt] as String),
@@ -98,6 +105,7 @@ class BankAccount extends BaseEntity {
98105
BankAccountFields.color: color,
99106
BankAccountFields.startingValue: startingValue,
100107
BankAccountFields.active: active ? 1 : 0,
108+
BankAccountFields.countNetWorth: countNetWorth ? 1 : 0,
101109
BankAccountFields.mainAccount: mainAccount ? 1 : 0,
102110
BaseEntityFields.createdAt: update
103111
? createdAt?.toIso8601String()

lib/model/transaction.dart

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -396,11 +396,12 @@ class TransactionMethods extends SossoldiDatabase {
396396

397397
final result = await db.rawQuery('''
398398
SELECT
399-
strftime('$frequencyDateParser', ${TransactionFields.date}) as $freqencyString,
400-
SUM(CASE WHEN ${TransactionFields.type} = 'IN' THEN ${TransactionFields.amount} ELSE 0 END) as income,
401-
SUM(CASE WHEN ${TransactionFields.type} = 'OUT' THEN ${TransactionFields.amount} ELSE 0 END) as expense
402-
FROM "$transactionTable"
403-
WHERE $sqlFilters
399+
strftime('$frequencyDateParser', t.${TransactionFields.date}) as $freqencyString,
400+
SUM(CASE WHEN t.${TransactionFields.type} = 'IN' THEN t.${TransactionFields.amount} ELSE 0 END) as income,
401+
SUM(CASE WHEN t.${TransactionFields.type} = 'OUT' THEN t.${TransactionFields.amount} ELSE 0 END) as expense
402+
FROM "$transactionTable" t
403+
JOIN $bankAccountTable b ON t.${TransactionFields.idBankAccount} = b.${BankAccountFields.id}
404+
WHERE $sqlFilters AND b.${BankAccountFields.countNetWorth} = 1 AND b.${BankAccountFields.active} = 1
404405
GROUP BY $freqencyString
405406
''');
406407

lib/pages/accounts/add_account.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class _AddAccountState extends ConsumerState<AddAccount> with Functions {
3434
balanceController.text = numToCurrency(selectedAccount.total);
3535
accountIcon = selectedAccount.symbol;
3636
accountColor = selectedAccount.color;
37-
countNetWorth = selectedAccount.active;
37+
countNetWorth = selectedAccount.countNetWorth;
3838
mainAccount = selectedAccount.mainAccount;
3939
}
4040
super.initState();
@@ -431,15 +431,15 @@ class _AddAccountState extends ConsumerState<AddAccount> with Functions {
431431
icon: accountIcon,
432432
color: accountColor,
433433
balance: currencyToNum(balanceController.text),
434-
active: countNetWorth,
434+
countNetWorth: countNetWorth,
435435
mainAccount: mainAccount,
436436
);
437437
} else {
438438
await ref.read(accountsProvider.notifier).addAccount(
439439
name: nameController.text,
440440
icon: accountIcon,
441441
color: accountColor,
442-
active: countNetWorth,
442+
countNetWorth: countNetWorth,
443443
mainAccount: mainAccount,
444444
startingValue: currencyToNum(balanceController.text),
445445
);

lib/providers/accounts_provider.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import 'package:fl_chart/fl_chart.dart';
33

44
import '../model/bank_account.dart';
55
import '../model/transaction.dart';
6+
import 'dashboard_provider.dart';
67
import 'transactions_provider.dart';
78

89
final mainAccountProvider = StateProvider<BankAccount?>((ref) => null);
@@ -42,6 +43,7 @@ class AsyncAccountsNotifier extends AsyncNotifier<List<BankAccount>> {
4243
required String icon,
4344
required int color,
4445
bool active = true,
46+
bool countNetWorth = true,
4547
bool mainAccount = false,
4648
num startingValue = 0,
4749
}) async {
@@ -51,6 +53,7 @@ class AsyncAccountsNotifier extends AsyncNotifier<List<BankAccount>> {
5153
color: color,
5254
startingValue: startingValue,
5355
active: active,
56+
countNetWorth: countNetWorth,
5457
mainAccount: mainAccount,
5558
);
5659

@@ -67,13 +70,15 @@ class AsyncAccountsNotifier extends AsyncNotifier<List<BankAccount>> {
6770
int? color,
6871
num? balance,
6972
bool? mainAccount,
73+
bool? countNetWorth,
7074
bool active = true,
7175
}) async {
7276
BankAccount account = ref.read(selectedAccountProvider)!.copy(
7377
name: name,
7478
symbol: icon,
7579
color: color,
7680
active: active,
81+
countNetWorth: countNetWorth,
7782
mainAccount: mainAccount,
7883
);
7984
state = const AsyncValue.loading();
@@ -86,6 +91,8 @@ class AsyncAccountsNotifier extends AsyncNotifier<List<BankAccount>> {
8691
if (account.mainAccount) {
8792
ref.read(mainAccountProvider.notifier).state = account;
8893
}
94+
ref.invalidate(dashboardProvider);
95+
8996
return _getAccounts();
9097
});
9198
}

0 commit comments

Comments
 (0)