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/invoice_app_data.dart'; 8 : 9 : class InvoiceRecalculation extends AbstractRecalculation { 10 : InvoiceAppData change; 11 : InvoiceAppData? initial; 12 : 13 1 : InvoiceRecalculation(this.change, [this.initial]); 14 : 15 1 : @override 16 1 : double getDelta() => throw UnimplementedError(); 17 : 18 6 : double getPrevDelta() => initial?.hidden == true ? 0.0 : initial?.details; 19 : 20 1 : double getStateDelta(dynamic prev, dynamic curr) { 21 4 : double delta = change.hidden ? 0.0 : change.details; 22 4 : if (initial != null && prev?.uuid == curr?.uuid) { 23 : delta = 24 14 : change.hidden ? -initial?.details : (initial!.hidden ? change.details : change.details - initial?.details); 25 : } 26 : return delta; 27 : } 28 : 29 1 : void updateAccount(AccountAppData accountChange, AccountAppData? accountInitial, [bool reverse = false]) { 30 : double plex = reverse ? -1 : 1; 31 : double? diffDelta; 32 4 : if (accountInitial != null && initial != null && accountChange.uuid != accountInitial.uuid) { 33 2 : diffDelta = plex * getPrevDelta(); 34 6 : HistoryData.addLog(accountInitial.uuid!, initial, 0.0, -diffDelta, initial!.uuid); 35 : } 36 1 : double delta = getStateDelta(accountInitial, accountChange); 37 6 : HistoryData.addLog(accountChange.uuid!, change, 0.0, delta * plex, change.uuid); 38 5 : if (diffDelta != null && accountInitial?.createdAt.isBefore(initial?.createdAt ?? DateTime.now()) == true) { 39 8 : accountInitial!.details -= plex * super.exchange.reform(diffDelta, initial?.currency, accountInitial.currency); 40 : } 41 4 : if (accountChange.createdAt.isBefore(change.createdAt)) { 42 8 : accountChange.details += plex * super.exchange.reform(delta, change.currency, accountChange.currency); 43 : } 44 : } 45 : }