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