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 1 : BillRecalculation({ 15 : required this.change, 16 : this.initial, 17 : }); 18 : 19 1 : @override 20 1 : double getDelta() => throw UnimplementedError(); 21 : 22 6 : double getPrevDelta() => initial?.hidden == true ? 0.0 : initial?.details; 23 : 24 1 : double getStateDelta(dynamic prev, dynamic curr) { 25 8 : double initialDetails = exchange.reform(initial?.details ?? 0.0, initial?.currency, change.currency); 26 4 : double delta = change.hidden ? 0.0 : change.details; 27 4 : if (initial != null && prev?.uuid == curr?.uuid) { 28 2 : delta = change.hidden 29 1 : ? -initialDetails 30 8 : : (initial?.hidden == true ? change.details : change.details - initialDetails); 31 : } 32 : return delta; 33 : } 34 : 35 1 : BillRecalculation updateAccount(AccountAppData accountChange, AccountAppData? accountInitial) { 36 : double? diffDelta; 37 4 : if (accountInitial != null && initial != null && accountChange.uuid != accountInitial.uuid) { 38 1 : diffDelta = getPrevDelta(); 39 5 : HistoryData.addLog(accountInitial.uuid!, initial, 0.0, diffDelta, initial!.uuid); 40 : } 41 1 : double delta = getStateDelta(accountInitial, accountChange); 42 6 : HistoryData.addLog(accountChange.uuid!, change, 0.0, -delta, change.uuid); 43 5 : if (diffDelta != null && accountInitial?.createdAt.isBefore(initial?.createdAt ?? DateTime.now()) == true) { 44 7 : accountInitial!.details += exchange.reform(diffDelta, initial?.currency, accountInitial.currency); 45 : } 46 4 : if (accountChange.createdAt.isBefore(change.createdAt)) { 47 7 : accountChange.details -= exchange.reform(delta, change.currency, accountChange.currency); 48 : } 49 : return this; 50 : } 51 : 52 1 : BillRecalculation updateBudget(BudgetAppData budgetChange, BudgetAppData? budgetInitial) { 53 1 : DateTime now = DateTime.now(); 54 6 : if (DateTime(now.year, now.month).isAfter(change.createdAt)) { 55 : return this; 56 : } 57 3 : if (budgetInitial != null && budgetChange.uuid != budgetInitial.uuid) { 58 6 : double prevDelta = exchange.reform(getPrevDelta(), initial?.currency, budgetInitial.currency); 59 5 : budgetInitial.progress = getProgress(budgetInitial.amountLimit, budgetInitial.progress, -prevDelta); 60 2 : budgetInitial.amount -= prevDelta; 61 : } 62 6 : double delta = exchange.reform(getStateDelta(budgetInitial, budgetChange), change.currency, budgetChange.currency); 63 4 : budgetChange.progress = getProgress(budgetChange.amountLimit, budgetChange.progress, delta); 64 2 : budgetChange.amount += delta; 65 : return this; 66 : } 67 : }