Line data Source code
1 : // Copyright 2026 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/integration/integration_record.dart';
5 : import 'package:app_finance/_classes/integration/wise/wise_client.dart';
6 : import 'package:app_finance/_classes/math/budget_prediction.dart';
7 : import 'package:app_finance/_classes/structure/currency_app_data.dart';
8 : import 'package:app_finance/_classes/structure/interface_app_data.dart';
9 : import 'package:app_finance/_classes/structure/invoice_app_data.dart';
10 : import 'package:flutter_currency_picker/flutter_currency_picker.dart';
11 : import 'package:uuid/uuid.dart';
12 :
13 : // Another balance of the same profile linked to an application's account
14 : typedef WiseCounterpart = ({String balanceId, String account});
15 :
16 : class WiseImporter {
17 : final bool Function(String uuid) exists;
18 : final void Function(InterfaceAppData item, String uuid) add;
19 : final void Function(CurrencyAppData rate) setRate;
20 :
21 0 : const WiseImporter({required this.exists, required this.add, required this.setRate});
22 :
23 0 : static String _uuid(String value) => const Uuid().v5(Namespace.url.value, value);
24 :
25 0 : static String recordUuid(String balanceId, String type, String reference) =>
26 0 : _uuid('wise:$balanceId:$type:$reference');
27 :
28 0 : static String conversionUuid(int profileId, String reference) => _uuid('wise:$profileId:conversion:$reference');
29 :
30 0 : IntegrationImportResult run({
31 : required int profileId,
32 : required WiseBalance balance,
33 : required List<WiseTransaction> transactions,
34 : required String account,
35 : required String category,
36 : BudgetPrediction? prediction,
37 : Map<String, WiseCounterpart> counterparts = const {},
38 : }) {
39 : var added = 0;
40 : var skipped = 0;
41 0 : for (final transaction in transactions) {
42 0 : final own = recordUuid('${balance.id}', transaction.type, transaction.referenceNumber);
43 0 : final counterpart = _counterpart(transaction, balance, counterparts);
44 : if (counterpart != null) {
45 0 : final other = recordUuid(
46 0 : counterpart.balanceId, transaction.type == 'DEBIT' ? 'CREDIT' : 'DEBIT', transaction.referenceNumber);
47 0 : final uuid = conversionUuid(profileId, transaction.referenceNumber);
48 0 : if (exists(uuid) || exists(own) || exists(other)) {
49 0 : skipped++;
50 : continue;
51 : }
52 0 : _addTransfer(transaction, balance, account, counterpart.account, uuid);
53 0 : added++;
54 : continue;
55 : }
56 0 : if (exists(own) || exists(conversionUuid(profileId, transaction.referenceNumber))) {
57 0 : skipped++;
58 : continue;
59 : }
60 0 : add(
61 0 : IntegrationRecord.toAppData(
62 0 : amount: transaction.amount.value,
63 0 : currency: transaction.amount.currency.isNotEmpty ? transaction.amount.currency : balance.currency,
64 0 : date: transaction.date,
65 0 : title: transaction.description,
66 : account: account,
67 : category: category,
68 : prediction: prediction,
69 : ),
70 : own,
71 : );
72 0 : added++;
73 : }
74 : return (added: added, skipped: skipped, pending: 0);
75 : }
76 :
77 0 : WiseCounterpart? _counterpart(
78 : WiseTransaction transaction, WiseBalance balance, Map<String, WiseCounterpart> counterparts) {
79 0 : if (!transaction.isConversion) {
80 : return null;
81 : }
82 0 : final source = transaction.sourceAmount!.currency;
83 0 : final target = transaction.targetAmount!.currency;
84 0 : if (source == target || (source != balance.currency && target != balance.currency)) {
85 : return null;
86 : }
87 0 : final result = counterparts[source == balance.currency ? target : source];
88 0 : return result != null && result.balanceId != '${balance.id}' ? result : null;
89 : }
90 :
91 0 : void _addTransfer(WiseTransaction transaction, WiseBalance balance, String account, String other, String uuid) {
92 0 : final source = transaction.sourceAmount!;
93 0 : final target = transaction.targetAmount!;
94 0 : final isOutgoing = source.currency == balance.currency;
95 0 : final from = CurrencyProvider.find(source.currency);
96 0 : final to = CurrencyProvider.find(target.currency);
97 0 : if (from != null && to != null && target.value != 0) {
98 : // A transfer is kept in the target currency, the source account is charged by the rate
99 0 : setRate(CurrencyAppData(currencyFrom: to, currency: from, details: source.value.abs() / target.value.abs()));
100 : }
101 0 : add(
102 0 : InvoiceAppData(
103 0 : title: transaction.description,
104 : account: isOutgoing ? other : account,
105 : accountFrom: isOutgoing ? account : other,
106 0 : details: target.value.abs(),
107 : currency: to,
108 0 : createdAt: transaction.date,
109 0 : updatedAt: transaction.date,
110 : hidden: false,
111 : ),
112 : uuid,
113 : );
114 : }
115 : }
|