front dev update
This commit is contained in:
@@ -2,11 +2,15 @@ import 'package:flutter/material.dart';
|
||||
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
import 'package:pshared/models/payment/operation.dart';
|
||||
import 'package:pshared/models/payment/payment.dart';
|
||||
import 'package:pshared/models/payment/status.dart';
|
||||
import 'package:pshared/provider/payment/payments.dart';
|
||||
|
||||
import 'package:pweb/pages/report/charts/distribution.dart';
|
||||
import 'package:pweb/pages/report/charts/status.dart';
|
||||
import 'package:pweb/pages/report/table/filters.dart';
|
||||
import 'package:pweb/pages/report/table/widget.dart';
|
||||
import 'package:pweb/providers/operatioins.dart';
|
||||
|
||||
import 'package:pweb/generated/i18n/app_localizations.dart';
|
||||
|
||||
@@ -19,18 +23,25 @@ class OperationHistoryPage extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _OperationHistoryPageState extends State<OperationHistoryPage> {
|
||||
DateTimeRange? _pendingRange;
|
||||
DateTimeRange? _appliedRange;
|
||||
final Set<OperationStatus> _pendingStatuses = {};
|
||||
Set<OperationStatus> _appliedStatuses = {};
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
context.read<OperationProvider>().loadOperations();
|
||||
final provider = context.read<PaymentsProvider>();
|
||||
if (!provider.isReady && !provider.isLoading) {
|
||||
provider.refresh();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> _pickRange() async {
|
||||
final provider = context.read<OperationProvider>();
|
||||
final now = DateTime.now();
|
||||
final initial = provider.dateRange ??
|
||||
final initial = _pendingRange ??
|
||||
DateTimeRange(
|
||||
start: now.subtract(const Duration(days: 30)),
|
||||
end: now,
|
||||
@@ -44,33 +55,157 @@ class _OperationHistoryPageState extends State<OperationHistoryPage> {
|
||||
);
|
||||
|
||||
if (picked != null) {
|
||||
provider.setDateRange(picked);
|
||||
setState(() {
|
||||
_pendingRange = picked;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void _toggleStatus(OperationStatus status) {
|
||||
setState(() {
|
||||
if (_pendingStatuses.contains(status)) {
|
||||
_pendingStatuses.remove(status);
|
||||
} else {
|
||||
_pendingStatuses.add(status);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void _applyFilters() {
|
||||
setState(() {
|
||||
_appliedRange = _pendingRange;
|
||||
_appliedStatuses = {..._pendingStatuses};
|
||||
});
|
||||
}
|
||||
|
||||
List<OperationItem> _mapPayments(List<Payment> payments) {
|
||||
return payments.map(_mapPayment).toList();
|
||||
}
|
||||
|
||||
OperationItem _mapPayment(Payment payment) {
|
||||
final debit = payment.lastQuote?.debitAmount;
|
||||
final settlement = payment.lastQuote?.expectedSettlementAmount;
|
||||
final amountMoney = debit ?? settlement;
|
||||
|
||||
final amount = _parseAmount(amountMoney?.amount);
|
||||
final currency = amountMoney?.currency ?? '';
|
||||
final toAmount = settlement == null ? amount : _parseAmount(settlement.amount);
|
||||
final toCurrency = settlement?.currency ?? currency;
|
||||
|
||||
final payId = _firstNonEmpty([payment.paymentRef, payment.idempotencyKey]) ?? '-';
|
||||
final name = _firstNonEmpty([payment.lastQuote?.quoteRef, payment.paymentRef, payment.idempotencyKey]) ?? '-';
|
||||
final comment = _firstNonEmpty([payment.failureReason, payment.failureCode, payment.state]) ?? '';
|
||||
|
||||
return OperationItem(
|
||||
status: _statusFromPaymentState(payment.state),
|
||||
fileName: null,
|
||||
amount: amount,
|
||||
currency: currency,
|
||||
toAmount: toAmount,
|
||||
toCurrency: toCurrency,
|
||||
payId: payId,
|
||||
cardNumber: null,
|
||||
name: name,
|
||||
date: _resolvePaymentDate(payment),
|
||||
comment: comment,
|
||||
);
|
||||
}
|
||||
|
||||
List<OperationItem> _filterOperations(List<OperationItem> operations) {
|
||||
if (_appliedRange == null && _appliedStatuses.isEmpty) {
|
||||
return operations;
|
||||
}
|
||||
|
||||
return operations.where((op) {
|
||||
final statusMatch =
|
||||
_appliedStatuses.isEmpty || _appliedStatuses.contains(op.status);
|
||||
|
||||
final dateMatch = _appliedRange == null ||
|
||||
_isUnknownDate(op.date) ||
|
||||
(op.date.isAfter(_appliedRange!.start.subtract(const Duration(seconds: 1))) &&
|
||||
op.date.isBefore(_appliedRange!.end.add(const Duration(seconds: 1))));
|
||||
|
||||
return statusMatch && dateMatch;
|
||||
}).toList();
|
||||
}
|
||||
|
||||
OperationStatus _statusFromPaymentState(String? raw) {
|
||||
final state = raw?.trim().toLowerCase();
|
||||
|
||||
switch (state) {
|
||||
case 'accepted':
|
||||
case 'funds_reserved':
|
||||
case 'submitted':
|
||||
case 'unspecified':
|
||||
case null:
|
||||
return OperationStatus.processing;
|
||||
|
||||
case 'settled':
|
||||
return OperationStatus.success;
|
||||
|
||||
case 'failed':
|
||||
case 'cancelled':
|
||||
return OperationStatus.error;
|
||||
|
||||
default:
|
||||
// Future-proof: any new backend state is treated as processing
|
||||
return OperationStatus.processing;
|
||||
}
|
||||
}
|
||||
|
||||
DateTime _resolvePaymentDate(Payment payment) {
|
||||
final expiresAt = payment.lastQuote?.fxQuote?.expiresAtUnixMs;
|
||||
if (expiresAt != null && expiresAt > 0) {
|
||||
return DateTime.fromMillisecondsSinceEpoch(expiresAt, isUtc: true);
|
||||
}
|
||||
return DateTime.fromMillisecondsSinceEpoch(0, isUtc: true);
|
||||
}
|
||||
|
||||
double _parseAmount(String? amount) {
|
||||
if (amount == null || amount.trim().isEmpty) return 0;
|
||||
return double.tryParse(amount) ?? 0;
|
||||
}
|
||||
|
||||
String? _firstNonEmpty(List<String?> values) {
|
||||
for (final value in values) {
|
||||
final trimmed = value?.trim();
|
||||
if (trimmed != null && trimmed.isNotEmpty) return trimmed;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
bool _isUnknownDate(DateTime date) => date.millisecondsSinceEpoch == 0;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final loc = AppLocalizations.of(context)!;
|
||||
return Consumer<OperationProvider>(
|
||||
return Consumer<PaymentsProvider>(
|
||||
builder: (context, provider, child) {
|
||||
if (provider.isLoading) {
|
||||
return const Center(child: CircularProgressIndicator());
|
||||
}
|
||||
|
||||
if (provider.error != null) {
|
||||
final message = provider.error?.toString() ?? loc.noErrorInformation;
|
||||
return Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Text(loc.notificationError(provider.error ?? loc.noErrorInformation)),
|
||||
Text(loc.notificationError(message)),
|
||||
ElevatedButton(
|
||||
onPressed: () => provider.loadOperations(),
|
||||
onPressed: () => provider.refresh(),
|
||||
child: Text(loc.retry),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
final operations = _mapPayments(provider.payments);
|
||||
final filteredOperations = _filterOperations(operations);
|
||||
final hasFileName = operations.any(
|
||||
(operation) => (operation.fileName ?? '').trim().isNotEmpty,
|
||||
);
|
||||
|
||||
return Padding(
|
||||
padding: const EdgeInsets.all(16.0),
|
||||
@@ -84,26 +219,26 @@ class _OperationHistoryPageState extends State<OperationHistoryPage> {
|
||||
spacing: 16,
|
||||
children: [
|
||||
Expanded(
|
||||
child: StatusChart(operations: provider.allOperations),
|
||||
child: StatusChart(operations: operations),
|
||||
),
|
||||
Expanded(
|
||||
child: PayoutDistributionChart(
|
||||
operations: provider.allOperations,
|
||||
operations: operations,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
OperationFilters(
|
||||
selectedRange: provider.dateRange,
|
||||
selectedStatuses: provider.selectedStatuses,
|
||||
selectedRange: _pendingRange,
|
||||
selectedStatuses: _pendingStatuses,
|
||||
onPickRange: _pickRange,
|
||||
onToggleStatus: provider.toggleStatus,
|
||||
onApply: () => provider.applyFilters(context),
|
||||
onToggleStatus: _toggleStatus,
|
||||
onApply: _applyFilters,
|
||||
),
|
||||
OperationsTable(
|
||||
operations: provider.filteredOperations,
|
||||
showFileNameColumn: provider.hasFileName,
|
||||
operations: filteredOperations,
|
||||
showFileNameColumn: hasFileName,
|
||||
),
|
||||
],
|
||||
),
|
||||
|
||||
@@ -6,10 +6,10 @@ import 'package:pweb/generated/i18n/app_localizations.dart';
|
||||
|
||||
class OperationFilters extends StatelessWidget {
|
||||
final DateTimeRange? selectedRange;
|
||||
final Set<String> selectedStatuses;
|
||||
final Set<OperationStatus> selectedStatuses;
|
||||
final VoidCallback onPickRange;
|
||||
final VoidCallback onApply;
|
||||
final ValueChanged<String> onToggleStatus;
|
||||
final ValueChanged<OperationStatus> onToggleStatus;
|
||||
|
||||
const OperationFilters({
|
||||
super.key,
|
||||
@@ -66,11 +66,12 @@ class OperationFilters extends StatelessWidget {
|
||||
Wrap(
|
||||
spacing: 12,
|
||||
runSpacing: 8,
|
||||
children: [
|
||||
OperationStatus.success.localized(context),
|
||||
OperationStatus.processing.localized(context),
|
||||
OperationStatus.error.localized(context),
|
||||
children: const [
|
||||
OperationStatus.success,
|
||||
OperationStatus.processing,
|
||||
OperationStatus.error,
|
||||
].map((status) {
|
||||
final label = status.localized(context);
|
||||
final isSelected = selectedStatuses.contains(status);
|
||||
return GestureDetector(
|
||||
onTap: () => onToggleStatus(status),
|
||||
@@ -89,7 +90,7 @@ class OperationFilters extends StatelessWidget {
|
||||
vertical: 4,
|
||||
),
|
||||
child: Text(
|
||||
l10n.status(status),
|
||||
l10n.status(label),
|
||||
style: TextStyle(
|
||||
color: isSelected ? Colors.white : Colors.black87,
|
||||
fontSize: 14,
|
||||
|
||||
@@ -8,6 +8,13 @@ import 'package:pweb/pages/report/table/badge.dart';
|
||||
|
||||
class OperationRow {
|
||||
static DataRow build(OperationItem op, BuildContext context) {
|
||||
final isUnknownDate = op.date.millisecondsSinceEpoch == 0;
|
||||
final localDate = op.date.toLocal();
|
||||
final dateLabel = isUnknownDate
|
||||
? '-'
|
||||
: '${TimeOfDay.fromDateTime(localDate).format(context)}\n'
|
||||
'${localDate.toIso8601String().split("T").first}';
|
||||
|
||||
return DataRow(cells: [
|
||||
DataCell(OperationStatusBadge(status: op.status)),
|
||||
DataCell(Text(op.fileName ?? '')),
|
||||
@@ -16,10 +23,7 @@ class OperationRow {
|
||||
DataCell(Text(op.payId)),
|
||||
DataCell(Text(op.cardNumber ?? '-')),
|
||||
DataCell(Text(op.name)),
|
||||
DataCell(Text(
|
||||
'${TimeOfDay.fromDateTime(op.date).format(context)}\n'
|
||||
'${op.date.toLocal().toIso8601String().split("T").first}',
|
||||
)),
|
||||
DataCell(Text(dateLabel)),
|
||||
DataCell(Text(op.comment)),
|
||||
]);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user