Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 512c96d

Browse files
authoredFeb 25, 2024··
Merge pull request #126 from enrique-lozano/fix/healthy-value-bug
Fix and simplify the calculation of the number of months without income
2 parents d83c3a2 + 397145d commit 512c96d

File tree

1 file changed

+27
-26
lines changed

1 file changed

+27
-26
lines changed
 

‎lib/core/services/finance_health_service.dart

+27-26
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import 'dart:math';
22

33
import 'package:async/async.dart';
4-
import 'package:collection/collection.dart';
54
import 'package:flutter/material.dart';
65
import 'package:monekin/core/database/services/account/account_service.dart';
76
import 'package:monekin/core/database/services/transaction/transaction_service.dart';
87
import 'package:monekin/core/models/transaction/transaction.dart';
98
import 'package:monekin/core/presentation/widgets/transaction_filter/transaction_filters.dart';
9+
import 'package:monekin/core/utils/date_time_picker.dart';
1010
import 'package:monekin/i18n/translations.g.dart';
1111
import 'package:rxdart/rxdart.dart';
1212

@@ -87,7 +87,7 @@ class FinanceHealthData {
8787
score: monthsWithoutIncome == null
8888
? null
8989
: min(monthsWithoutIncome! * 10, 100),
90-
weight: 50);
90+
weight: monthsWithoutIncomeWeight);
9191
}
9292

9393
FinanceHealthAttrScore get savingPercentageScore {
@@ -101,7 +101,8 @@ class FinanceHealthData {
101101
// To desmos: \frac{100}{1+e^{-1.25+-0.2\left(x-\ 15\right)}}
102102
toReturn = 100 / (1 + exp(-1.25 - 0.2 * (savingsPercentage - 15))) - 2;
103103

104-
return FinanceHealthAttrScore(score: toReturn, weight: 50);
104+
return FinanceHealthAttrScore(
105+
score: toReturn, weight: savingPercentageWeight);
105106
}
106107

107108
final int savingPercentageWeight = 50;
@@ -168,37 +169,37 @@ class FinanceHealthService {
168169
Stream<double?> getMonthsWithoutIncome({
169170
required TransactionFilters filters,
170171
}) {
171-
return Rx.combineLatest([
172-
TransactionService.instance
173-
.countTransactions(predicate: filters)
174-
.map((event) => event.numberOfRes),
175-
AccountService.instance
176-
.getAccountsMoney(date: filters.maxDate, trFilters: filters),
177-
for (var i = 1; i <= 6; i++)
172+
final minDate = filters.minDate ?? kDefaultFirstSelectableDate;
173+
final maxDate = filters.maxDate ?? DateTime.now();
174+
175+
return Rx.combineLatest4(
176+
TransactionService.instance
177+
.countTransactions(predicate: filters)
178+
.map((event) => event.numberOfRes),
179+
AccountService.instance.getAccountsBalance(
180+
filters: filters,
181+
),
178182
AccountService.instance
179183
.getAccountsBalance(
180184
filters: filters.copyWith(
181-
minDate: (filters.maxDate ?? DateTime.now())
182-
.subtract(Duration(days: i * 30)),
183-
maxDate: (filters.maxDate ?? DateTime.now())
184-
.subtract(Duration(days: (i - 1) * 30)),
185-
transactionTypes: [TransactionType.expense]),
185+
transactionTypes: [TransactionType.expense],
186+
),
186187
)
187-
.map((event) => event.abs() * (1.1 - (i / 6)))
188-
], (values) {
189-
if (values[0] < 6) {
188+
.map((e) => e.abs()),
189+
AccountService.instance
190+
.getAccountsMoney(
191+
date: minDate,
192+
trFilters: filters.copyWith(minDate: null),
193+
)
194+
.map((event) => max(event, 0)),
195+
(numberOfTransactions, balance, expense, initialMoney) {
196+
if (numberOfTransactions < 4) {
190197
return null;
191198
}
192199

193-
final balance = values[1];
194-
final expensesInLastPeriod =
195-
values.whereIndexed((index, element) => index >= 2);
196-
197-
if (balance < 0) return 0;
200+
final dateDiff = maxDate.difference(minDate).inDays;
198201

199-
return balance /
200-
(expensesInLastPeriod.sum /
201-
[for (var i = 1; i <= 6; i++) (1.1 - (i / 6))].sum);
202+
return (initialMoney + balance) / expense / dateDiff * 30;
202203
});
203204
}
204205

0 commit comments

Comments
 (0)
Please sign in to comment.