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 0 : 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 0 : Future<InterfaceAppData> parseFileLine(List<dynamic> line, Map<String, dynamic> def) async {
68 0 : final defaultAccount = def[attrAccountName];
69 0 : final defaultBudget = def[attrCategoryName];
70 0 : final dateFormat = def[defDateFormat];
71 0 : _defaultCurrency = def[attrBillCurrency];
72 :
73 0 : dynamic amount = _get(line, attrBillAmount);
74 0 : if (amount is String) {
75 0 : amount = double.tryParse(amount);
76 : }
77 : dynamic newItem;
78 0 : dynamic account = await _find(AppDataType.accounts, line, _get(line, attrAccountName), defaultAccount);
79 0 : String? uuid = _get(line, attrBillUuid);
80 0 : String? dateValue = _get(line, attrBillDate);
81 : DateTime? date;
82 : if (dateValue != null) {
83 : try {
84 0 : date = DateFormat(dateFormat).parse(dateValue);
85 : } catch (e) {
86 0 : date = DateTime.parse(dateValue);
87 : }
88 : }
89 0 : 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 0 : final categoryFromFile = _get(line, attrCategoryName);
102 0 : newItem = BillAppData(
103 : uuid: uuid,
104 : account: account,
105 0 : category: await _find(AppDataType.budgets, line, categoryFromFile, defaultBudget),
106 0 : title: _get(line, attrBillComment).toString(),
107 0 : details: 0.0 + amount,
108 : createdAt: date,
109 : updatedAt: date,
110 0 : currency: _getCurrency(line, _defaultCurrency),
111 : hidden: false,
112 : );
113 : if (categoryFromFile == null) {
114 0 : final categories = prediction?.predict(newItem) ?? const [];
115 0 : if (categories.isNotEmpty) {
116 0 : newItem.category = categories.first;
117 : }
118 : }
119 : }
120 : return newItem;
121 : }
122 :
123 0 : 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 0 : if (_cache[type] == null) {
129 0 : _cache[type] = <String, String?>{};
130 : }
131 0 : if (_cache[type]?[value] != null) {
132 0 : uuid = _cache[type]?[value];
133 : } else {
134 0 : final item = search(type, value);
135 0 : uuid = item?.uuid;
136 0 : _cache[type]![value] = uuid ?? '';
137 : }
138 0 : return uuid ?? await _new(type, line, value);
139 : }
140 :
141 0 : Future<String> _new(AppDataType type, List<dynamic> line, dynamic value) async {
142 : InterfaceAppData newItem;
143 0 : 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 0 : newItem = BudgetAppData(
153 : title: value,
154 0 : currency: _getCurrency(line, _defaultCurrency),
155 : hidden: false,
156 : );
157 : }
158 0 : newItem = add(newItem);
159 0 : _cache[type]![value] = newItem.uuid;
160 0 : return newItem.uuid!;
161 : }
162 :
163 0 : dynamic _get(List<dynamic> line, String type) {
164 0 : final index = columnMap.indexOf(type);
165 0 : return index >= 0 ? line[index] : null;
166 : }
167 :
168 0 : Currency? _getCurrency(List<dynamic> line, String? def) {
169 0 : return CurrencyProvider.find(_get(line, attrBillCurrency) ?? def);
170 : }
171 : }
|