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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,48 +0,0 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
import 'package:pweb/models/wallet/wallet_transaction.dart';
|
||||
import 'package:pweb/services/wallet_transactions.dart';
|
||||
|
||||
|
||||
class WalletTransactionsProvider extends ChangeNotifier {
|
||||
final WalletTransactionsService _service;
|
||||
|
||||
WalletTransactionsProvider(this._service);
|
||||
|
||||
List<WalletTransaction> _transactions = const [];
|
||||
bool _isLoading = false;
|
||||
String? _error;
|
||||
String? _walletId;
|
||||
int _loadSeq = 0;
|
||||
|
||||
List<WalletTransaction> get transactions => List.unmodifiable(_transactions);
|
||||
bool get isLoading => _isLoading;
|
||||
String? get error => _error;
|
||||
String? get walletId => _walletId;
|
||||
|
||||
Future<void> load({String? walletId}) async {
|
||||
final targetWalletId = walletId ?? _walletId;
|
||||
final requestSeq = ++_loadSeq;
|
||||
_walletId = targetWalletId;
|
||||
_isLoading = true;
|
||||
_error = null;
|
||||
notifyListeners();
|
||||
|
||||
try {
|
||||
final fetched = await _service.fetchHistory(walletId: targetWalletId);
|
||||
if (requestSeq != _loadSeq) return;
|
||||
|
||||
_transactions = targetWalletId == null
|
||||
? fetched
|
||||
: fetched.where((tx) => tx.walletId == targetWalletId).toList();
|
||||
} catch (e) {
|
||||
if (requestSeq != _loadSeq) return;
|
||||
_error = e.toString();
|
||||
} finally {
|
||||
if (requestSeq == _loadSeq) {
|
||||
_isLoading = false;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user