Line data Source code
1 : // Copyright 2023 The terCAD team. All rights reserved.
2 : // Use of this source code is governed by a CC BY-NC-ND 4.0 license that can be found in the LICENSE file.
3 :
4 : import 'package:app_finance/_classes/math/abstract_recalculation.dart';
5 : import 'package:app_finance/_classes/storage/history_data.dart';
6 : import 'package:app_finance/_classes/structure/account_app_data.dart';
7 : import 'package:app_finance/_classes/structure/bill_app_data.dart';
8 : import 'package:app_finance/_classes/structure/budget_app_data.dart';
9 :
10 : class BillRecalculation extends AbstractRecalculation {
11 : BillAppData change;
12 : BillAppData? initial;
13 :
14 2 : BillRecalculation({
15 : required this.change,
16 : this.initial,
17 : });
18 :
19 0 : @override
20 0 : double getDelta() => throw UnimplementedError();
21 :
22 0 : double getPrevDelta() => initial?.hidden == true ? 0.0 : initial?.details;
23 :
24 2 : double getStateDelta(dynamic prev, dynamic curr, [bool isBudget = true]) {
25 2 : double initialDetails = initial?.details ?? 0.0;
26 8 : double delta = change.hidden ? 0.0 : change.details;
27 : if (isBudget) {
28 4 : initialDetails *= initial?.exchangeCategory ?? 1.0;
29 6 : delta *= change.exchangeCategory;
30 : } else {
31 4 : initialDetails *= initial?.exchangeAccount ?? 1.0;
32 6 : delta *= change.exchangeAccount;
33 : }
34 2 : if (initial != null && prev?.uuid == curr?.uuid) {
35 0 : delta = initial?.hidden == true ? delta : delta - initialDetails;
36 : }
37 : return delta;
38 : }
39 :
40 2 : BillRecalculation updateAccount(AccountAppData accountChange, AccountAppData? accountInitial) {
41 : double? diffDelta;
42 0 : if (accountInitial != null && initial != null && accountChange.uuid != accountInitial.uuid) {
43 0 : diffDelta = getPrevDelta();
44 0 : HistoryData.addLog(accountInitial.uuid!, initial, 0.0, diffDelta, initial!.uuid);
45 : }
46 2 : double delta = getStateDelta(accountInitial, accountChange, false);
47 12 : HistoryData.addLog(accountChange.uuid!, change, 0.0, -delta, change.uuid);
48 0 : if (diffDelta != null && accountInitial?.createdAt.isBefore(initial?.createdAt ?? DateTime.now()) == true) {
49 0 : accountInitial!.details += exchange.reform(diffDelta, initial?.currency, accountInitial.currency);
50 : }
51 8 : if (accountChange.createdAt.isBefore(change.createdAt)) {
52 0 : accountChange.details -= delta;
53 : }
54 : return this;
55 : }
56 :
57 2 : BillRecalculation updateBudget(BudgetAppData budgetChange, BudgetAppData? budgetInitial) {
58 : if (budgetInitial != null &&
59 0 : budgetChange.uuid != budgetInitial.uuid &&
60 0 : !budgetInitial.getDateBoundary().isAfter(change.createdAt)) {
61 0 : double prevDelta = exchange.reform(getPrevDelta(), initial?.currency, budgetInitial.currency);
62 0 : budgetInitial.progress = getProgress(budgetInitial.amountLimit, budgetInitial.progress, -prevDelta);
63 0 : budgetInitial.amount -= prevDelta;
64 : }
65 8 : if (!budgetChange.getDateBoundary().isAfter(change.createdAt)) {
66 2 : double delta = getStateDelta(budgetInitial, budgetChange, true);
67 8 : budgetChange.progress = getProgress(budgetChange.amountLimit, budgetChange.progress, delta);
68 4 : budgetChange.amount += delta;
69 : }
70 : return this;
71 : }
72 : }
|