LCOV - code coverage report
Current view: top level - _classes/storage - file_parser.dart Coverage Total Hit
Test: lcov.info Lines: 62.7 % 67 42
Test Date: 2026-12-05 10:48:47 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 'dart:async';
       5              : 
       6              : import 'package:app_finance/_classes/herald/app_locale.dart';
       7              : import 'package:app_finance/_classes/math/budget_prediction.dart';
       8              : import 'package:app_finance/_classes/storage/app_data.dart';
       9              : import 'package:app_finance/_classes/structure/account_app_data.dart';
      10              : import 'package:app_finance/_classes/structure/bill_app_data.dart';
      11              : import 'package:app_finance/_classes/structure/budget_app_data.dart';
      12              : import 'package:app_finance/design/form/list_selector_item.dart';
      13              : import 'package:app_finance/_classes/structure/interface_app_data.dart';
      14              : import 'package:app_finance/_classes/structure/invoice_app_data.dart';
      15              : import 'package:app_finance/_configs/account_type.dart';
      16              : import 'package:flutter_currency_picker/flutter_currency_picker.dart';
      17              : import 'package:intl/intl.dart';
      18              : 
      19              : typedef SearchFunction = Function(AppDataType type, String value);
      20              : typedef AddFunction = InterfaceAppData Function(InterfaceAppData item);
      21              : 
      22              : class FileParser {
      23              :   static const attrBillUuid = 'billUuid';
      24              :   static const attrAccountName = 'accountName';
      25              :   static const attrCategoryName = 'categoryName';
      26              :   static const attrBillAmount = 'billAmount';
      27              :   static const attrBillType = 'billType';
      28              :   static const attrBillDate = 'billDate';
      29              :   static const attrBillCurrency = 'billCurrency';
      30              :   static const attrBillComment = 'billComment';
      31              :   static const defDateFormat = 'dateFormat';
      32              : 
      33              :   List<String> columnMap;
      34              :   SearchFunction search;
      35              :   AddFunction add;
      36              :   BudgetPrediction? prediction;
      37              : 
      38              :   String? _defaultCurrency;
      39              :   final _cache = <AppDataType, Map<String, String?>>{};
      40              : 
      41            1 :   FileParser({
      42              :     required this.search,
      43              :     required this.add,
      44              :     this.prediction,
      45              :     this.columnMap = const [],
      46              :   });
      47              : 
      48            0 :   static List<ListSelectorItem> getMappingTypes() {
      49            0 :     final account = AppLocale.labels.account;
      50            0 :     final budget = AppLocale.labels.budget;
      51            0 :     final bill = AppLocale.labels.bill;
      52            0 :     final mapping = [
      53            0 :       ListSelectorItem(id: '', name: '-- ${AppLocale.labels.ignoreTooltip} --'),
      54            0 :       ListSelectorItem(id: attrAccountName, name: '$account: ${AppLocale.labels.title}'),
      55            0 :       ListSelectorItem(id: attrCategoryName, name: '$budget: ${AppLocale.labels.title}'),
      56            0 :       ListSelectorItem(id: attrBillAmount, name: '$bill: ${AppLocale.labels.expense}'),
      57            0 :       ListSelectorItem(id: attrBillType, name: '$bill: ${AppLocale.labels.flowTypeTooltip}'),
      58            0 :       ListSelectorItem(id: attrBillDate, name: '$bill: ${AppLocale.labels.expenseDateTime}'),
      59            0 :       ListSelectorItem(id: attrBillCurrency, name: '$bill: ${AppLocale.labels.currency}'),
      60            0 :       ListSelectorItem(id: attrBillComment, name: '$bill: ${AppLocale.labels.description}'),
      61            0 :       ListSelectorItem(id: attrBillUuid, name: '$bill: ${AppLocale.labels.uuid}'),
      62              :     ];
      63            0 :     mapping.sort((a, b) => a.name.compareTo(b.name));
      64              :     return mapping;
      65              :   }
      66              : 
      67            1 :   Future<InterfaceAppData> parseFileLine(List<dynamic> line, Map<String, dynamic> def) async {
      68            1 :     final defaultAccount = def[attrAccountName];
      69            1 :     final defaultBudget = def[attrCategoryName];
      70            1 :     final dateFormat = def[defDateFormat];
      71            2 :     _defaultCurrency = def[attrBillCurrency];
      72              : 
      73            1 :     dynamic amount = _get(line, attrBillAmount);
      74            1 :     if (amount is String) {
      75            1 :       amount = double.tryParse(amount);
      76              :     }
      77              :     dynamic newItem;
      78            2 :     dynamic account = await _find(AppDataType.accounts, line, _get(line, attrAccountName), defaultAccount);
      79            1 :     String? uuid = _get(line, attrBillUuid);
      80            1 :     String? dateValue = _get(line, attrBillDate);
      81              :     DateTime? date;
      82              :     if (dateValue != null) {
      83              :       try {
      84            2 :         date = DateFormat(dateFormat).parse(dateValue);
      85              :       } catch (e) {
      86            0 :         date = DateTime.parse(dateValue);
      87              :       }
      88              :     }
      89            4 :     if (_get(line, attrBillType) == AppLocale.labels.flowTypeInvoice) {
      90            0 :       newItem = InvoiceAppData(
      91              :         uuid: uuid,
      92              :         account: account,
      93            0 :         title: _get(line, attrBillComment).toString(),
      94            0 :         details: 0.0 + amount,
      95              :         createdAt: date,
      96              :         updatedAt: date,
      97            0 :         currency: _getCurrency(line, _defaultCurrency),
      98              :         hidden: false,
      99              :       );
     100              :     } else {
     101            1 :       final categoryFromFile = _get(line, attrCategoryName);
     102            1 :       newItem = BillAppData(
     103              :         uuid: uuid,
     104              :         account: account,
     105            1 :         category: await _find(AppDataType.budgets, line, categoryFromFile, defaultBudget),
     106            2 :         title: _get(line, attrBillComment).toString(),
     107            1 :         details: 0.0 + amount,
     108              :         createdAt: date,
     109              :         updatedAt: date,
     110            2 :         currency: _getCurrency(line, _defaultCurrency),
     111              :         hidden: false,
     112              :       );
     113              :       if (categoryFromFile == null) {
     114            2 :         final categories = prediction?.predict(newItem) ?? const [];
     115            1 :         if (categories.isNotEmpty) {
     116            2 :           newItem.category = categories.first;
     117              :         }
     118              :       }
     119              :     }
     120              :     return newItem;
     121              :   }
     122              : 
     123            1 :   Future<String> _find(AppDataType type, List<dynamic> line, dynamic value, String? def) async {
     124              :     if (value == null) {
     125              :       return def ?? '';
     126              :     }
     127              :     String? uuid;
     128            2 :     if (_cache[type] == null) {
     129            3 :       _cache[type] = <String, String?>{};
     130              :     }
     131            3 :     if (_cache[type]?[value] != null) {
     132            0 :       uuid = _cache[type]?[value];
     133              :     } else {
     134            2 :       final item = search(type, value);
     135            0 :       uuid = item?.uuid;
     136            3 :       _cache[type]![value] = uuid ?? '';
     137              :     }
     138            1 :     return uuid ?? await _new(type, line, value);
     139              :   }
     140              : 
     141            1 :   Future<String> _new(AppDataType type, List<dynamic> line, dynamic value) async {
     142              :     InterfaceAppData newItem;
     143            1 :     if (type == AppDataType.accounts) {
     144            0 :       newItem = AccountAppData(
     145              :         title: value,
     146            0 :         type: AppAccountType.account.toString(),
     147              :         details: 0.0,
     148            0 :         currency: _getCurrency(line, _defaultCurrency),
     149              :         hidden: false,
     150              :       );
     151              :     } else {
     152            1 :       newItem = BudgetAppData(
     153              :         title: value,
     154            2 :         currency: _getCurrency(line, _defaultCurrency),
     155              :         hidden: false,
     156              :       );
     157              :     }
     158            2 :     newItem = add(newItem);
     159            4 :     _cache[type]![value] = newItem.uuid;
     160            1 :     return newItem.uuid!;
     161              :   }
     162              : 
     163            1 :   dynamic _get(List<dynamic> line, String type) {
     164            2 :     final index = columnMap.indexOf(type);
     165            2 :     return index >= 0 ? line[index] : null;
     166              :   }
     167              : 
     168            1 :   Currency? _getCurrency(List<dynamic> line, String? def) {
     169            2 :     return CurrencyProvider.find(_get(line, attrBillCurrency) ?? def);
     170              :   }
     171              : }
        

Generated by: LCOV version 2.0-1