refactor of money utils with new money2 package
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
import 'package:pshared/models/money.dart';
|
||||
import 'package:money2/money2.dart';
|
||||
|
||||
import 'package:pshared/models/payment/payment.dart';
|
||||
import 'package:pshared/models/payment/quote/quote.dart';
|
||||
import 'package:pshared/models/payment/quote/status_type.dart';
|
||||
@@ -89,14 +90,16 @@ class MultiplePayoutsProvider extends ChangeNotifier {
|
||||
Money? get requestedSentAmount {
|
||||
if (_rows.isEmpty) return null;
|
||||
const currency = 'RUB';
|
||||
final rubCurrency = money2CurrencyFromCode(currency);
|
||||
if (rubCurrency == null) return null;
|
||||
|
||||
double total = 0;
|
||||
Money? total;
|
||||
for (final row in _rows) {
|
||||
final value = parseMoneyAmount(row.amount, fallback: double.nan);
|
||||
if (value.isNaN) return null;
|
||||
total += value;
|
||||
final value = parseMoneyWithCurrency(row.amount, rubCurrency);
|
||||
if (value == null) return null;
|
||||
total = total == null ? value : total + value;
|
||||
}
|
||||
return Money(amount: amountToString(total), currency: currency);
|
||||
return total;
|
||||
}
|
||||
|
||||
Money? aggregateSettlementAmountForCurrency(String? sourceCurrencyCode) {
|
||||
@@ -118,11 +121,11 @@ class MultiplePayoutsProvider extends ChangeNotifier {
|
||||
final fee = aggregateFeeAmountForCurrency(sourceCurrencyCode);
|
||||
if (debit == null || fee == null) return null;
|
||||
|
||||
final debitValue = parseMoneyAmount(debit.amount, fallback: double.nan);
|
||||
final feeValue = parseMoneyAmount(fee.amount, fallback: double.nan);
|
||||
if (debit.currency.toUpperCase() != fee.currency.toUpperCase()) return null;
|
||||
if (debitValue.isNaN || feeValue.isNaN || debitValue <= 0) return null;
|
||||
return (feeValue / debitValue) * 100;
|
||||
final debitValue = debit;
|
||||
final feeValue = fee;
|
||||
if (debitValue.currency.isoCode != feeValue.currency.isoCode) return null;
|
||||
if (!debitValue.isPositive) return null;
|
||||
return (feeValue.toDecimal() / debitValue.toDecimal()).toDouble() * 100;
|
||||
}
|
||||
|
||||
Future<void> quoteFromCsv({
|
||||
@@ -279,8 +282,8 @@ class MultiplePayoutsProvider extends ChangeNotifier {
|
||||
final sentAmount = requestedSentAmount;
|
||||
if (sentAmount == null) return null;
|
||||
return <String, String>{
|
||||
UploadMetadataKeys.amount: sentAmount.amount,
|
||||
UploadMetadataKeys.currency: sentAmount.currency,
|
||||
UploadMetadataKeys.amount: sentAmount.toDecimal().toString(),
|
||||
UploadMetadataKeys.currency: sentAmount.currency.isoCode,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -290,10 +293,12 @@ class MultiplePayoutsProvider extends ChangeNotifier {
|
||||
) {
|
||||
if (values == null || values.isEmpty) return null;
|
||||
|
||||
if (sourceCurrencyCode != null && sourceCurrencyCode.isNotEmpty) {
|
||||
final sourceCurrency = sourceCurrencyCode.trim().toUpperCase();
|
||||
final sourceCurrency =
|
||||
money2CurrencyFromCode(sourceCurrencyCode)?.isoCode ??
|
||||
sourceCurrencyCode;
|
||||
if (sourceCurrency != null) {
|
||||
for (final value in values) {
|
||||
if (value.currency.toUpperCase() == sourceCurrency) {
|
||||
if (value.currency.isoCode == sourceCurrency) {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user