73 lines
2.1 KiB
Dart
73 lines
2.1 KiB
Dart
import 'package:flutter/material.dart';
|
|
|
|
import 'package:pshared/models/payment/payment.dart';
|
|
import 'package:pshared/models/payment/fx/quote.dart';
|
|
import 'package:pshared/utils/currency.dart';
|
|
import 'package:pshared/utils/money.dart';
|
|
|
|
import 'package:pweb/pages/report/details/section.dart';
|
|
import 'package:pweb/pages/report/details/sections/rows.dart';
|
|
|
|
import 'package:pweb/generated/i18n/app_localizations.dart';
|
|
|
|
|
|
class PaymentFxSection extends StatelessWidget {
|
|
final Payment payment;
|
|
|
|
const PaymentFxSection({super.key, required this.payment});
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final loc = AppLocalizations.of(context)!;
|
|
final fx = payment.lastQuote?.fxQuote;
|
|
final rows = buildDetailRows([
|
|
DetailValue(label: loc.fxRateLabel, value: _formatRate(fx)),
|
|
]);
|
|
|
|
return DetailsSection(title: loc.paymentDetailsFx, children: rows);
|
|
}
|
|
|
|
String? _formatRate(FxQuote? fx) {
|
|
if (fx == null) return null;
|
|
final price = fx.price?.trim();
|
|
if (price == null || price.isEmpty) return null;
|
|
|
|
final baseCurrency = _resolveCurrencyCode([
|
|
fx.baseCurrency,
|
|
fx.baseAmount?.currency.isoCode,
|
|
]);
|
|
final quoteCurrency = _resolveCurrencyCode([
|
|
fx.quoteCurrency,
|
|
fx.quoteAmount?.currency.isoCode,
|
|
]);
|
|
|
|
if (baseCurrency == null || quoteCurrency == null) return price;
|
|
|
|
final baseDisplay =
|
|
parseMoneyWithCurrencyCode('1', baseCurrency)?.toString() ??
|
|
'1 $baseCurrency';
|
|
final quoteDisplay =
|
|
parseMoneyWithCurrencyCode(
|
|
_normalizeAmount(price),
|
|
quoteCurrency,
|
|
)?.toString() ??
|
|
'$price $quoteCurrency';
|
|
|
|
return '$baseDisplay = $quoteDisplay';
|
|
}
|
|
|
|
String? _resolveCurrencyCode(List<String?> values) {
|
|
for (final value in values) {
|
|
if (value == null || value.isEmpty) continue;
|
|
final resolved = money2CurrencyFromCode(value);
|
|
if (resolved != null) return resolved.isoCode;
|
|
return value;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
String _normalizeAmount(String raw) {
|
|
return raw.replaceAll(RegExp(r'\s+'), '').replaceAll(',', '.');
|
|
}
|
|
}
|