refactor of money utils with new money2 package
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
import 'package:money2/money2.dart';
|
||||
|
||||
import 'package:pshared/models/payment/currency_pair.dart';
|
||||
import 'package:pshared/models/payment/fx/intent.dart';
|
||||
import 'package:pshared/models/payment/fx/side.dart';
|
||||
import 'package:pshared/models/money.dart';
|
||||
|
||||
|
||||
class FxIntentHelper {
|
||||
@@ -37,11 +38,15 @@ class FxIntentHelper {
|
||||
case FxSide.unspecified:
|
||||
break;
|
||||
}
|
||||
if (amount.currency == pair.base && pair.quote.isNotEmpty) return pair.quote;
|
||||
if (amount.currency == pair.quote && pair.base.isNotEmpty) return pair.base;
|
||||
if (amount.currency.isoCode == pair.base && pair.quote.isNotEmpty) {
|
||||
return pair.quote;
|
||||
}
|
||||
if (amount.currency.isoCode == pair.quote && pair.base.isNotEmpty) {
|
||||
return pair.base;
|
||||
}
|
||||
if (pair.quote.isNotEmpty) return pair.quote;
|
||||
if (pair.base.isNotEmpty) return pair.base;
|
||||
}
|
||||
return amount.currency;
|
||||
return amount.currency.isoCode;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ class PaymentQuotationCurrencyResolver {
|
||||
PaymentMethodData? paymentData,
|
||||
}) {
|
||||
final quoteCurrency = _normalizeCurrency(
|
||||
quote?.amounts?.destinationSettlement?.currency,
|
||||
quote?.amounts?.destinationSettlement?.currency.isoCode,
|
||||
);
|
||||
if (quoteCurrency != null) return quoteCurrency;
|
||||
|
||||
|
||||
@@ -1,17 +1,16 @@
|
||||
import 'package:pshared/models/money.dart';
|
||||
import 'package:money2/money2.dart';
|
||||
|
||||
import 'package:pshared/models/payment/fees/line.dart';
|
||||
import 'package:pshared/models/payment/quote/quote.dart';
|
||||
import 'package:pshared/utils/currency.dart';
|
||||
import 'package:pshared/utils/money.dart';
|
||||
|
||||
|
||||
Money? quoteFeeTotal(PaymentQuote? quote) {
|
||||
final preferredCurrency =
|
||||
quote?.amounts?.sourcePrincipal?.currency ??
|
||||
quote?.amounts?.sourceDebitTotal?.currency;
|
||||
return quoteFeeTotalFromLines(
|
||||
quote?.fees?.lines,
|
||||
preferredCurrency: preferredCurrency,
|
||||
preferredCurrency:
|
||||
quote?.amounts?.sourcePrincipal?.currency.isoCode ??
|
||||
quote?.amounts?.sourceDebitTotal?.currency.isoCode,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -21,7 +20,8 @@ Money? quoteSourceDebitTotal(
|
||||
}) {
|
||||
final sourceDebitTotal = quote?.amounts?.sourceDebitTotal;
|
||||
final preferredCurrency = _normalizeCurrency(
|
||||
preferredSourceCurrency ?? quote?.amounts?.sourcePrincipal?.currency,
|
||||
preferredSourceCurrency ??
|
||||
quote?.amounts?.sourcePrincipal?.currency.isoCode,
|
||||
);
|
||||
|
||||
if (sourceDebitTotal == null) {
|
||||
@@ -31,10 +31,9 @@ Money? quoteSourceDebitTotal(
|
||||
);
|
||||
}
|
||||
|
||||
final debitCurrency = _normalizeCurrency(sourceDebitTotal.currency);
|
||||
if (preferredCurrency == null ||
|
||||
debitCurrency == null ||
|
||||
debitCurrency == preferredCurrency) {
|
||||
_normalizeCurrency(sourceDebitTotal.currency.isoCode) ==
|
||||
preferredCurrency) {
|
||||
return sourceDebitTotal;
|
||||
}
|
||||
|
||||
@@ -52,22 +51,18 @@ Money? quoteFeeTotalFromLines(
|
||||
if (lines == null || lines.isEmpty) return null;
|
||||
|
||||
final normalizedPreferred = _normalizeCurrency(preferredCurrency);
|
||||
final totalsByCurrency = <String, double>{};
|
||||
final totalsByCurrency = <String, Money>{};
|
||||
|
||||
for (final line in lines) {
|
||||
final money = line.amount;
|
||||
if (money == null) continue;
|
||||
final parsedAmount = line.amount;
|
||||
if (parsedAmount == null) continue;
|
||||
|
||||
final currency = _normalizeCurrency(money.currency);
|
||||
if (currency == null) continue;
|
||||
|
||||
final amount = parseMoneyAmount(money.amount, fallback: double.nan);
|
||||
if (amount.isNaN) continue;
|
||||
|
||||
final sign = _lineSign(line.side);
|
||||
final signedAmount = sign * amount.abs();
|
||||
totalsByCurrency[currency] =
|
||||
(totalsByCurrency[currency] ?? 0) + signedAmount;
|
||||
final currencyCode = parsedAmount.currency.isoCode;
|
||||
final signedAmount = _isCreditLine(line.side) ? -parsedAmount : parsedAmount;
|
||||
final current = totalsByCurrency[currencyCode];
|
||||
totalsByCurrency[currencyCode] = current == null
|
||||
? signedAmount
|
||||
: current + signedAmount;
|
||||
}
|
||||
|
||||
if (totalsByCurrency.isEmpty) return null;
|
||||
@@ -77,85 +72,59 @@ Money? quoteFeeTotalFromLines(
|
||||
totalsByCurrency.containsKey(normalizedPreferred)
|
||||
? normalizedPreferred
|
||||
: totalsByCurrency.keys.first;
|
||||
final total = totalsByCurrency[selectedCurrency];
|
||||
if (total == null) return null;
|
||||
|
||||
return Money(amount: amountToString(total), currency: selectedCurrency);
|
||||
return totalsByCurrency[selectedCurrency];
|
||||
}
|
||||
|
||||
List<Money> aggregateMoneyByCurrency(Iterable<Money?> values) {
|
||||
final totals = <String, double>{};
|
||||
final totals = <String, Money>{};
|
||||
for (final value in values) {
|
||||
if (value == null) continue;
|
||||
|
||||
final currency = _normalizeCurrency(value.currency);
|
||||
if (currency == null) continue;
|
||||
|
||||
final amount = parseMoneyAmount(value.amount, fallback: double.nan);
|
||||
if (amount.isNaN) continue;
|
||||
|
||||
totals[currency] = (totals[currency] ?? 0) + amount;
|
||||
final currency = value.currency.isoCode;
|
||||
final current = totals[currency];
|
||||
totals[currency] = current == null ? value : current + value;
|
||||
}
|
||||
|
||||
return totals.entries
|
||||
.map(
|
||||
(entry) =>
|
||||
Money(amount: amountToString(entry.value), currency: entry.key),
|
||||
)
|
||||
.toList();
|
||||
return totals.values.toList();
|
||||
}
|
||||
|
||||
Money? _rebuildSourceDebitTotal(
|
||||
PaymentQuote? quote, {
|
||||
String? preferredSourceCurrency,
|
||||
}) {
|
||||
final sourcePrincipal = quote?.amounts?.sourcePrincipal;
|
||||
if (sourcePrincipal == null) return null;
|
||||
final principal = quote?.amounts?.sourcePrincipal;
|
||||
if (principal == null) return null;
|
||||
|
||||
final principalCurrency = _normalizeCurrency(sourcePrincipal.currency);
|
||||
if (principalCurrency == null) return null;
|
||||
final principalCurrency = principal.currency.isoCode;
|
||||
if (preferredSourceCurrency != null &&
|
||||
principalCurrency != preferredSourceCurrency) {
|
||||
return null;
|
||||
}
|
||||
|
||||
final principalAmount = parseMoneyAmount(
|
||||
sourcePrincipal.amount,
|
||||
fallback: double.nan,
|
||||
);
|
||||
if (principalAmount.isNaN) return null;
|
||||
|
||||
double totalAmount = principalAmount;
|
||||
var totalAmount = principal;
|
||||
final fee = quoteFeeTotalFromLines(
|
||||
quote?.fees?.lines,
|
||||
preferredCurrency: principalCurrency,
|
||||
);
|
||||
if (fee != null && _normalizeCurrency(fee.currency) == principalCurrency) {
|
||||
final feeAmount = parseMoneyAmount(fee.amount, fallback: double.nan);
|
||||
if (!feeAmount.isNaN) {
|
||||
totalAmount += feeAmount;
|
||||
}
|
||||
if (fee != null && fee.currency.isoCode == principalCurrency) {
|
||||
totalAmount += fee;
|
||||
}
|
||||
|
||||
return Money(
|
||||
amount: amountToString(totalAmount),
|
||||
currency: principalCurrency,
|
||||
);
|
||||
return totalAmount;
|
||||
}
|
||||
|
||||
double _lineSign(String? side) {
|
||||
bool _isCreditLine(String? side) {
|
||||
final normalized = side?.trim().toLowerCase() ?? '';
|
||||
switch (normalized) {
|
||||
case 'entry_side_credit':
|
||||
case 'credit':
|
||||
return -1;
|
||||
return true;
|
||||
default:
|
||||
return 1;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
String? _normalizeCurrency(String? currency) {
|
||||
final normalized = currency?.trim().toUpperCase();
|
||||
final normalized = currency?.trim();
|
||||
if (normalized == null || normalized.isEmpty) return null;
|
||||
return normalized;
|
||||
return money2CurrencyFromCode(normalized)?.isoCode ?? normalized.toUpperCase();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user