LCOV - code coverage report
Current view: top level - lib/_classes/math - bill_recalculation.dart (source / functions) Hit Total Coverage
Test: lcov.info Lines: 16 31 51.6 %
Date: 2024-10-04 11:12:13 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           0 :   @override
      20           0 :   double getDelta() => throw UnimplementedError();
      21             : 
      22           0 :   double getPrevDelta() => initial?.hidden == true ? 0.0 : initial?.details;
      23             : 
      24           1 :   double getStateDelta(dynamic prev, dynamic curr) {
      25           6 :     double initialDetails = exchange.reform(initial?.details ?? 0.0, initial?.currency, change.currency);
      26           4 :     double delta = change.hidden ? 0.0 : change.details;
      27           1 :     if (initial != null && prev?.uuid == curr?.uuid) {
      28           0 :       delta = change.hidden
      29           0 :           ? -initialDetails
      30           0 :           : (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           0 :     if (accountInitial != null && initial != null && accountChange.uuid != accountInitial.uuid) {
      38           0 :       diffDelta = getPrevDelta();
      39           0 :       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           0 :     if (diffDelta != null && accountInitial?.createdAt.isBefore(initial?.createdAt ?? DateTime.now()) == true) {
      44           0 :       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           0 :     if (budgetInitial != null && budgetChange.uuid != budgetInitial.uuid) {
      58           0 :       double prevDelta = exchange.reform(getPrevDelta(), initial?.currency, budgetInitial.currency);
      59           0 :       budgetInitial.progress = getProgress(budgetInitial.amountLimit, budgetInitial.progress, -prevDelta);
      60           0 :       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             : }

Generated by: LCOV version 1.14