fixed ledger account listing + quotation listing

This commit is contained in:
Stephan D
2026-01-23 02:48:10 +01:00
parent 218c4e20b3
commit 41f653775f
60 changed files with 469 additions and 283 deletions

View File

@@ -0,0 +1,46 @@
import 'package:flutter/foundation.dart';
import 'package:pshared/provider/ledger.dart';
class LedgerBalanceMaskController with ChangeNotifier {
String? _orgRef;
final Set<String> _maskedBalanceRefs = <String>{};
Set<String> _knownAccountRefs = <String>{};
void update(LedgerAccountsProvider accountsProvider) {
final nextOrgRef = accountsProvider.organizationRef;
final orgChanged = nextOrgRef != _orgRef;
if (orgChanged) {
_orgRef = nextOrgRef;
_maskedBalanceRefs.clear();
_knownAccountRefs = <String>{};
}
final refs = accountsProvider.accounts.map((a) => a.ledgerAccountRef).toSet();
_maskedBalanceRefs.removeWhere((id) => !refs.contains(id));
final newRefs = refs.difference(_knownAccountRefs);
if (newRefs.isNotEmpty) {
_maskedBalanceRefs.addAll(newRefs);
}
_knownAccountRefs = refs;
notifyListeners();
}
bool isBalanceMasked(String ledgerAccountRef) => _maskedBalanceRefs.contains(ledgerAccountRef);
bool isBalanceVisible(String ledgerAccountRef) => !isBalanceMasked(ledgerAccountRef);
void toggleBalanceMask(String ledgerAccountRef) {
final existed = _maskedBalanceRefs.remove(ledgerAccountRef);
if (!existed) _maskedBalanceRefs.add(ledgerAccountRef);
notifyListeners();
}
void unmaskAllBalances() {
_maskedBalanceRefs.clear();
notifyListeners();
}
}

View File

@@ -0,0 +1,106 @@
import 'package:flutter/foundation.dart';
import 'package:collection/collection.dart';
import 'package:pshared/models/payment/wallet.dart';
import 'package:pshared/provider/payment/wallets.dart';
class WalletsController with ChangeNotifier {
late WalletsProvider _wallets;
String? _orgRef;
/// UI-only: wallet refs with masked balances
final Set<String> _maskedBalanceWalletRefs = <String>{};
Set<String> _knownWalletRefs = <String>{};
String? _selectedWalletRef;
bool get isLoading => _wallets.isLoading;
Exception? get error => _wallets.error;
void update(WalletsProvider wallets) {
_wallets = wallets;
final nextOrgRef = wallets.organizationRef;
final orgChanged = nextOrgRef != _orgRef;
if (orgChanged) {
_orgRef = nextOrgRef;
_maskedBalanceWalletRefs.clear();
_knownWalletRefs = <String>{};
_selectedWalletRef = null;
}
// Remove ids that no longer exist
final ids = wallets.wallets.map((w) => w.id).toSet();
_maskedBalanceWalletRefs.removeWhere((id) => !ids.contains(id));
final newIds = ids.difference(_knownWalletRefs);
if (newIds.isNotEmpty) {
_maskedBalanceWalletRefs.addAll(newIds);
}
_knownWalletRefs = ids;
_selectedWalletRef = _resolveSelectedId(
currentRef: _selectedWalletRef,
wallets: wallets.wallets,
);
notifyListeners();
}
List<Wallet> get wallets => _wallets.wallets;
bool isBalanceMasked(String walletRef) => _maskedBalanceWalletRefs.contains(walletRef);
bool isBalanceVisible(String walletRef) => !isBalanceMasked(walletRef);
List<Wallet> get unmaskedWallets =>
wallets.where((w) => !_maskedBalanceWalletRefs.contains(w.id)).toList(growable: false);
Wallet? get selectedWallet {
final id = _selectedWalletRef;
if (id == null) return null;
return wallets.firstWhereOrNull((w) => w.id == id);
}
String? get selectedWalletRef => _selectedWalletRef;
void selectWallet(Wallet wallet) => selectWalletByRef(wallet.id);
void selectWalletByRef(String walletRef) {
if (_selectedWalletRef == walletRef) return;
_selectedWalletRef = walletRef;
notifyListeners();
}
/// Toggle wallet balance masking
void toggleBalanceMask(String walletRef) {
final existed = _maskedBalanceWalletRefs.remove(walletRef);
if (!existed) _maskedBalanceWalletRefs.add(walletRef);
notifyListeners();
}
/// Unmask balances for all wallets (bulk action)
void unmaskAllBalances() {
_maskedBalanceWalletRefs.clear();
notifyListeners();
}
String? _resolveSelectedId({
required String? currentRef,
required List<Wallet> wallets,
}) {
if (wallets.isEmpty) return null;
// Keep current selection if it still exists
if (currentRef != null && wallets.any((w) => w.id == currentRef)) {
return currentRef;
}
// Fallback to the first wallet
return wallets.first.id;
}
}

View File

@@ -1,124 +0,0 @@
import 'package:flutter/foundation.dart';
import 'package:collection/collection.dart';
import 'package:pshared/models/payment/wallet.dart';
import 'package:pshared/provider/payment/wallets.dart';
class WalletsController with ChangeNotifier {
late WalletsProvider _wallets;
String? _orgRef;
/// UI-only: which wallets are allowed to be visible
final Set<String> _visibleWalletRefs = <String>{};
String? _selectedWalletRef;
bool get isLoading => _wallets.isLoading;
Exception? get error => _wallets.error;
void update(WalletsProvider wallets) {
_wallets = wallets;
final nextOrgRef = wallets.organizationRef;
final orgChanged = nextOrgRef != _orgRef;
if (orgChanged) {
_orgRef = nextOrgRef;
_visibleWalletRefs.clear(); // All wallets hidden on org change
_selectedWalletRef = null;
}
// Remove ids that no longer exist
final ids = wallets.wallets.map((w) => w.id).toSet();
_visibleWalletRefs.removeWhere((id) => !ids.contains(id));
_selectedWalletRef = _resolveSelectedId(
currentRef: _selectedWalletRef,
wallets: wallets.wallets,
visibleRefs: _visibleWalletRefs,
);
notifyListeners();
}
List<Wallet> get wallets => _wallets.wallets;
bool isVisible(String walletRef) => _visibleWalletRefs.contains(walletRef);
bool isHidden(String walletRef) => !isVisible(walletRef);
List<Wallet> get visibleWallets =>
wallets.where((w) => _visibleWalletRefs.contains(w.id)).toList(growable: false);
Wallet? get selectedWallet {
final id = _selectedWalletRef;
if (id == null) return null;
return wallets.firstWhereOrNull((w) => w.id == id);
}
String? get selectedWalletRef => _selectedWalletRef;
void selectWallet(Wallet wallet, {bool allowHidden = false}) =>
selectWalletByRef(wallet.id, allowHidden: allowHidden);
void selectWalletByRef(String walletRef, {bool allowHidden = false}) {
if (_selectedWalletRef == walletRef) return;
// Prevent selecting a hidden wallet
if (!allowHidden && !_visibleWalletRefs.contains(walletRef)) return;
_selectedWalletRef = walletRef;
notifyListeners();
}
/// Toggle wallet visibility
void toggleVisibility(String accountRef) {
final existed = _visibleWalletRefs.remove(accountRef);
if (!existed) _visibleWalletRefs.add(accountRef);
_selectedWalletRef = _resolveSelectedId(
currentRef: _selectedWalletRef,
wallets: wallets,
visibleRefs: _visibleWalletRefs,
);
notifyListeners();
}
/// Show all wallets (bulk action)
void showAll() {
final allRefs = wallets.map((w) => w.id);
_visibleWalletRefs
..clear()
..addAll(allRefs);
_selectedWalletRef = _resolveSelectedId(
currentRef: _selectedWalletRef,
wallets: wallets,
visibleRefs: _visibleWalletRefs,
);
notifyListeners();
}
String? _resolveSelectedId({
required String? currentRef,
required List<Wallet> wallets,
required Set<String> visibleRefs,
}) {
if (wallets.isEmpty) return null;
// Keep current selection if it still exists and is visible
if (currentRef != null &&
visibleRefs.contains(currentRef) &&
wallets.any((w) => w.id == currentRef)) {
return currentRef;
}
// Select the first visible wallet
final firstVisible = wallets.firstWhereOrNull((w) => visibleRefs.contains(w.id));
return firstVisible?.id;
}
}

View File

@@ -14,8 +14,10 @@ class LedgerAccountDTO {
final String organizationRef;
final String? ownerRef;
final String accountCode;
@JsonKey(fromJson: ledgerAccountTypeFromJson, toJson: ledgerAccountTypeToJson)
final LedgerAccountTypeDTO accountType;
final String currency;
@JsonKey(fromJson: ledgerAccountStatusFromJson, toJson: ledgerAccountStatusToJson)
final LedgerAccountStatusDTO status;
final bool allowNegative;
final bool isSettlement;
@@ -23,7 +25,7 @@ class LedgerAccountDTO {
final DateTime? createdAt;
final DateTime? updatedAt;
final DescribableDTO describable;
final DescribableDTO? describable;
final LedgerBalanceDTO? balance;
@@ -40,7 +42,7 @@ class LedgerAccountDTO {
this.metadata,
this.createdAt,
this.updatedAt,
required this.describable,
this.describable,
this.balance,
});

View File

@@ -11,3 +11,34 @@ enum LedgerAccountStatusDTO {
@JsonValue('frozen')
frozen,
}
LedgerAccountStatusDTO ledgerAccountStatusFromJson(Object? value) {
final raw = value?.toString() ?? '';
var normalized = raw.trim().toLowerCase();
const prefix = 'account_status_';
if (normalized.startsWith(prefix)) {
normalized = normalized.substring(prefix.length);
}
switch (normalized) {
case 'active':
return LedgerAccountStatusDTO.active;
case 'frozen':
return LedgerAccountStatusDTO.frozen;
case 'unspecified':
case '':
return LedgerAccountStatusDTO.unspecified;
default:
return LedgerAccountStatusDTO.unspecified;
}
}
String ledgerAccountStatusToJson(LedgerAccountStatusDTO value) {
switch (value) {
case LedgerAccountStatusDTO.active:
return 'active';
case LedgerAccountStatusDTO.frozen:
return 'frozen';
case LedgerAccountStatusDTO.unspecified:
return 'unspecified';
}
}

View File

@@ -17,3 +17,42 @@ enum LedgerAccountTypeDTO {
@JsonValue('expense')
expense,
}
LedgerAccountTypeDTO ledgerAccountTypeFromJson(Object? value) {
final raw = value?.toString() ?? '';
var normalized = raw.trim().toLowerCase();
const prefix = 'account_type_';
if (normalized.startsWith(prefix)) {
normalized = normalized.substring(prefix.length);
}
switch (normalized) {
case 'asset':
return LedgerAccountTypeDTO.asset;
case 'liability':
return LedgerAccountTypeDTO.liability;
case 'revenue':
return LedgerAccountTypeDTO.revenue;
case 'expense':
return LedgerAccountTypeDTO.expense;
case 'unspecified':
case '':
return LedgerAccountTypeDTO.unspecified;
default:
return LedgerAccountTypeDTO.unspecified;
}
}
String ledgerAccountTypeToJson(LedgerAccountTypeDTO value) {
switch (value) {
case LedgerAccountTypeDTO.asset:
return 'asset';
case LedgerAccountTypeDTO.liability:
return 'liability';
case LedgerAccountTypeDTO.revenue:
return 'revenue';
case LedgerAccountTypeDTO.expense:
return 'expense';
case LedgerAccountTypeDTO.unspecified:
return 'unspecified';
}
}

View File

@@ -3,6 +3,7 @@ import 'package:pshared/data/mapper/describable.dart';
import 'package:pshared/data/mapper/ledger/balance.dart';
import 'package:pshared/data/mapper/ledger/status.dart';
import 'package:pshared/data/mapper/ledger/type.dart';
import 'package:pshared/models/describable.dart';
import 'package:pshared/models/ledger/account.dart';
@@ -20,7 +21,7 @@ extension LedgerAccountDTOMapper on LedgerAccountDTO {
metadata: metadata,
createdAt: createdAt,
updatedAt: updatedAt,
describable: describable.toDomain(),
describable: describable?.toDomain() ?? newDescribable(name: '', description: null),
balance: balance?.toDomain(),
);
}

View File

@@ -24,7 +24,7 @@ class LedgerAccountsProvider with ChangeNotifier {
Resource<List<LedgerAccount>> _resource = Resource(data: []);
Resource<List<LedgerAccount>> get resource => _resource;
List<LedgerAccount> get accounts => _resource.data ?? [];
List<LedgerAccount> get accounts => (_resource.data ?? []).whereNot((la)=> la.isSettlement).toList();
bool get isLoading => _resource.isLoading;
Exception? get error => _resource.error;

View File

@@ -1,12 +1,14 @@
import 'package:collection/collection.dart';
import 'package:pshared/controllers/wallets.dart';
import 'package:pshared/controllers/balance_mask/wallets.dart';
import 'package:pshared/models/payment/currency_pair.dart';
import 'package:pshared/models/payment/customer.dart';
import 'package:pshared/models/payment/fx/intent.dart';
import 'package:pshared/models/payment/fx/side.dart';
import 'package:pshared/models/payment/kind.dart';
import 'package:pshared/models/payment/methods/card.dart';
import 'package:pshared/models/payment/methods/data.dart';
import 'package:pshared/models/payment/methods/iban.dart';
import 'package:pshared/models/payment/methods/managed_wallet.dart';
import 'package:pshared/models/payment/methods/russian_bank.dart';
import 'package:pshared/models/payment/methods/type.dart';
import 'package:pshared/models/money.dart';
import 'package:pshared/models/payment/settlement_mode.dart';
@@ -15,7 +17,6 @@ import 'package:pshared/models/recipient/recipient.dart';
import 'package:pshared/provider/payment/amount.dart';
import 'package:pshared/provider/payment/flow.dart';
import 'package:pshared/provider/recipient/provider.dart';
import 'package:pshared/provider/recipient/pmethods.dart';
import 'package:pshared/utils/currency.dart';
@@ -25,15 +26,16 @@ class QuotationIntentBuilder {
required WalletsController wallets,
required PaymentFlowProvider flow,
required RecipientsProvider recipients,
required PaymentMethodsProvider methods,
}) {
final selectedWallet = wallets.selectedWallet;
final method = methods.methods.firstWhereOrNull((m) => m.type == flow.selectedType);
if (selectedWallet == null || method == null) return null;
final paymentData = flow.selectedPaymentData;
final selectedMethod = flow.selectedMethod;
if (selectedWallet == null || paymentData == null) return null;
final customer = _buildCustomer(
recipient: recipients.currentObject,
method: method,
method: selectedMethod,
data: paymentData,
);
final amount = Money(
amount: payment.amount.toString(),
@@ -50,7 +52,7 @@ class QuotationIntentBuilder {
return PaymentIntent(
kind: PaymentKind.payout,
amount: amount,
destination: method.data,
destination: paymentData,
source: ManagedWalletPaymentMethod(
managedWalletRef: selectedWallet.id,
),
@@ -85,11 +87,19 @@ class QuotationIntentBuilder {
return amount.currency;
}
Customer _buildCustomer({
Customer? _buildCustomer({
required Recipient? recipient,
required PaymentMethod method,
required PaymentMethod? method,
required PaymentMethodData? data,
}) {
final name = _resolveCustomerName(method, recipient);
final id = recipient?.id ?? method?.recipientRef;
if (id == null || id.isEmpty) return null;
final name = _resolveCustomerName(
method: method,
data: data,
recipient: recipient,
);
final parts = name == null || name.trim().isEmpty
? const <String>[]
: name.trim().split(RegExp(r'\s+'));
@@ -99,26 +109,31 @@ class QuotationIntentBuilder {
parts.length > 2 ? parts.sublist(1, parts.length - 1).join(' ') : null;
return Customer(
id: recipient?.id ?? method.recipientRef,
id: id,
firstName: firstName,
middleName: middleName,
lastName: lastName,
country: method.cardData?.country,
country: _resolveCustomerCountry(method: method, data: data),
);
}
String? _resolveCustomerName(PaymentMethod method, Recipient? recipient) {
final card = method.cardData;
String? _resolveCustomerName({
required PaymentMethod? method,
required PaymentMethodData? data,
required Recipient? recipient,
}) {
final card = method?.cardData ?? (data is CardPaymentMethod ? data : null);
if (card != null) {
return '${card.firstName} ${card.lastName}'.trim();
final fullName = '${card.firstName} ${card.lastName}'.trim();
if (fullName.isNotEmpty) return fullName;
}
final iban = method.ibanData;
final iban = method?.ibanData ?? (data is IbanPaymentMethod ? data : null);
if (iban != null && iban.accountHolder.trim().isNotEmpty) {
return iban.accountHolder.trim();
}
final bank = method.bankAccountData;
final bank = method?.bankAccountData ?? (data is RussianBankAccountPaymentMethod ? data : null);
if (bank != null && bank.recipientName.trim().isNotEmpty) {
return bank.recipientName.trim();
}
@@ -126,4 +141,12 @@ class QuotationIntentBuilder {
final recipientName = recipient?.name.trim();
return recipientName?.isNotEmpty == true ? recipientName : null;
}
String? _resolveCustomerCountry({
required PaymentMethod? method,
required PaymentMethodData? data,
}) {
final card = method?.cardData ?? (data is CardPaymentMethod ? data : null);
return card?.country;
}
}

View File

@@ -7,7 +7,7 @@ import 'package:logging/logging.dart';
import 'package:uuid/uuid.dart';
import 'package:pshared/api/requests/payment/quote.dart';
import 'package:pshared/controllers/wallets.dart';
import 'package:pshared/controllers/balance_mask/wallets.dart';
import 'package:pshared/data/mapper/payment/intent/payment.dart';
import 'package:pshared/models/asset.dart';
import 'package:pshared/models/payment/intent.dart';
@@ -38,7 +38,7 @@ class QuotationProvider extends ChangeNotifier {
WalletsController wallets,
PaymentFlowProvider flow,
RecipientsProvider recipients,
PaymentMethodsProvider methods,
PaymentMethodsProvider _methods,
) {
_organizations = venue;
final intent = _intentBuilder.build(
@@ -46,7 +46,6 @@ class QuotationProvider extends ChangeNotifier {
wallets: wallets,
flow: flow,
recipients: recipients,
methods: methods,
);
if (intent == null) return;
final intentKey = _buildIntentKey(intent);