LCOV - code coverage report
Current view: top level - _classes/math - bill_recalculation.dart Coverage Total Hit
Test: lcov.info Lines: 93.9 % 33 31
Test Date: 2026-11-14 15:03:01 Functions: - 0 0

            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, [bool isBudget = true]) {
      25            2 :     double initialDetails = initial?.details ?? 0.0;
      26            4 :     double delta = change.hidden ? 0.0 : change.details;
      27              :     if (isBudget) {
      28            3 :       initialDetails *= initial?.exchangeCategory ?? 1.0;
      29            3 :       delta *= change.exchangeCategory;
      30              :     } else {
      31            0 :       initialDetails *= initial?.exchangeAccount ?? 1.0;
      32            0 :       delta *= change.exchangeAccount;
      33              :     }
      34            4 :     if (initial != null && prev?.uuid == curr?.uuid) {
      35            4 :       delta = initial?.hidden == true ? delta : delta - initialDetails;
      36              :     }
      37              :     return delta;
      38              :   }
      39              : 
      40            1 :   BillRecalculation updateAccount(AccountAppData accountChange, AccountAppData? accountInitial) {
      41              :     double? diffDelta;
      42            4 :     if (accountInitial != null && initial != null && accountChange.uuid != accountInitial.uuid) {
      43            1 :       diffDelta = getPrevDelta();
      44            5 :       HistoryData.addLog(accountInitial.uuid!, initial, 0.0, diffDelta, initial!.uuid);
      45              :     }
      46            1 :     double delta = getStateDelta(accountInitial, accountChange, false);
      47            6 :     HistoryData.addLog(accountChange.uuid!, change, 0.0, -delta, change.uuid);
      48            5 :     if (diffDelta != null && accountInitial?.createdAt.isBefore(initial?.createdAt ?? DateTime.now()) == true) {
      49            7 :       accountInitial!.details += exchange.reform(diffDelta, initial?.currency, accountInitial.currency);
      50              :     }
      51            4 :     if (accountChange.createdAt.isBefore(change.createdAt)) {
      52            2 :       accountChange.details -= delta;
      53              :     }
      54              :     return this;
      55              :   }
      56              : 
      57            1 :   BillRecalculation updateBudget(BudgetAppData budgetChange, BudgetAppData? budgetInitial) {
      58              :     if (budgetInitial != null &&
      59            3 :         budgetChange.uuid != budgetInitial.uuid &&
      60            4 :         !budgetInitial.getDateBoundary().isAfter(change.createdAt)) {
      61            6 :       double prevDelta = exchange.reform(getPrevDelta(), initial?.currency, budgetInitial.currency);
      62            5 :       budgetInitial.progress = getProgress(budgetInitial.amountLimit, budgetInitial.progress, -prevDelta);
      63            2 :       budgetInitial.amount -= prevDelta;
      64              :     }
      65            4 :     if (!budgetChange.getDateBoundary().isAfter(change.createdAt)) {
      66            1 :       double delta = getStateDelta(budgetInitial, budgetChange, true);
      67            4 :       budgetChange.progress = getProgress(budgetChange.amountLimit, budgetChange.progress, delta);
      68            2 :       budgetChange.amount += delta;
      69              :     }
      70              :     return this;
      71              :   }
      72              : }
        

Generated by: LCOV version 2.0-1