added ledger as souec of funds for payouts
This commit is contained in:
@@ -1,42 +1,137 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'package:pshared/controllers/balance_mask/wallets.dart';
|
||||
import 'package:pshared/controllers/payment/source.dart';
|
||||
import 'package:pshared/models/ledger/account.dart';
|
||||
import 'package:pshared/models/payment/source_type.dart';
|
||||
import 'package:pshared/models/payment/wallet.dart';
|
||||
import 'package:pshared/utils/currency.dart';
|
||||
import 'package:pshared/utils/money.dart';
|
||||
|
||||
import 'package:pweb/generated/i18n/app_localizations.dart';
|
||||
|
||||
|
||||
typedef _SourceOptionKey = ({PaymentSourceType type, String ref});
|
||||
|
||||
class SourceWalletSelector extends StatelessWidget {
|
||||
const SourceWalletSelector({
|
||||
super.key,
|
||||
required this.walletsController,
|
||||
this.walletsController,
|
||||
this.sourceController,
|
||||
this.isBusy = false,
|
||||
this.onChanged,
|
||||
});
|
||||
}) : assert(
|
||||
(walletsController != null) != (sourceController != null),
|
||||
'Provide either walletsController or sourceController',
|
||||
);
|
||||
|
||||
final WalletsController walletsController;
|
||||
final WalletsController? walletsController;
|
||||
final PaymentSourceController? sourceController;
|
||||
final bool isBusy;
|
||||
final ValueChanged<Wallet>? onChanged;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final wallets = walletsController.wallets;
|
||||
final selectedWalletRef = walletsController.selectedWalletRef;
|
||||
final source = sourceController;
|
||||
if (source != null) {
|
||||
final selectedWallet = source.selectedWallet;
|
||||
final selectedLedger = source.selectedLedgerAccount;
|
||||
final selectedValue = switch (source.selectedType) {
|
||||
PaymentSourceType.wallet =>
|
||||
selectedWallet == null ? null : _walletKey(selectedWallet.id),
|
||||
PaymentSourceType.ledger =>
|
||||
selectedLedger == null
|
||||
? null
|
||||
: _ledgerKey(selectedLedger.ledgerAccountRef),
|
||||
null => null,
|
||||
};
|
||||
|
||||
return _buildSourceSelector(
|
||||
context: context,
|
||||
wallets: source.wallets,
|
||||
ledgerAccounts: source.ledgerAccounts,
|
||||
selectedValue: selectedValue,
|
||||
onChanged: (value) {
|
||||
if (value.type == PaymentSourceType.wallet) {
|
||||
source.selectWalletByRef(value.ref);
|
||||
final selected = source.selectedWallet;
|
||||
if (selected != null) {
|
||||
onChanged?.call(selected);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (value.type == PaymentSourceType.ledger) {
|
||||
source.selectLedgerByRef(value.ref);
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
final wallets = walletsController!;
|
||||
return _buildSourceSelector(
|
||||
context: context,
|
||||
wallets: wallets.wallets,
|
||||
ledgerAccounts: const <LedgerAccount>[],
|
||||
selectedValue: wallets.selectedWalletRef == null
|
||||
? null
|
||||
: _walletKey(wallets.selectedWalletRef!),
|
||||
onChanged: (value) {
|
||||
if (value.type != PaymentSourceType.wallet) return;
|
||||
wallets.selectWalletByRef(value.ref);
|
||||
final selected = wallets.selectedWallet;
|
||||
if (selected != null) {
|
||||
onChanged?.call(selected);
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildSourceSelector({
|
||||
required BuildContext context,
|
||||
required List<Wallet> wallets,
|
||||
required List<LedgerAccount> ledgerAccounts,
|
||||
required _SourceOptionKey? selectedValue,
|
||||
required ValueChanged<_SourceOptionKey> onChanged,
|
||||
}) {
|
||||
final theme = Theme.of(context);
|
||||
final l10n = AppLocalizations.of(context)!;
|
||||
|
||||
if (wallets.isEmpty) {
|
||||
if (wallets.isEmpty && ledgerAccounts.isEmpty) {
|
||||
return Text(l10n.noWalletsAvailable, style: theme.textTheme.bodySmall);
|
||||
}
|
||||
|
||||
final effectiveSelectedWalletRef = selectedWalletRef != null &&
|
||||
wallets.any((wallet) => wallet.id == selectedWalletRef)
|
||||
? selectedWalletRef
|
||||
final items = <DropdownMenuItem<_SourceOptionKey>>[
|
||||
...wallets.map((wallet) {
|
||||
return DropdownMenuItem<_SourceOptionKey>(
|
||||
value: _walletKey(wallet.id),
|
||||
child: Text(
|
||||
'${wallet.name} - ${_walletBalance(wallet)}',
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
);
|
||||
}),
|
||||
...ledgerAccounts.map((ledger) {
|
||||
return DropdownMenuItem<_SourceOptionKey>(
|
||||
value: _ledgerKey(ledger.ledgerAccountRef),
|
||||
child: Text(
|
||||
'${ledger.name} - ${_ledgerBalance(ledger)}',
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
);
|
||||
}),
|
||||
];
|
||||
|
||||
final knownValues = items
|
||||
.map((item) => item.value)
|
||||
.whereType<_SourceOptionKey>()
|
||||
.toSet();
|
||||
final effectiveValue = knownValues.contains(selectedValue)
|
||||
? selectedValue
|
||||
: null;
|
||||
|
||||
return DropdownButtonFormField<String>(
|
||||
initialValue: effectiveSelectedWalletRef,
|
||||
return DropdownButtonFormField<_SourceOptionKey>(
|
||||
initialValue: effectiveValue,
|
||||
isExpanded: true,
|
||||
decoration: InputDecoration(
|
||||
labelText: l10n.whereGetMoney,
|
||||
@@ -46,48 +141,45 @@ class SourceWalletSelector extends StatelessWidget {
|
||||
vertical: 10,
|
||||
),
|
||||
),
|
||||
items: wallets
|
||||
.map(
|
||||
(wallet) => DropdownMenuItem<String>(
|
||||
value: wallet.id,
|
||||
child: Text(
|
||||
'${_walletLabel(wallet)} - ${currencyCodeToSymbol(wallet.currency)} ${amountToString(wallet.balance)}',
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
),
|
||||
)
|
||||
.toList(growable: false),
|
||||
items: items,
|
||||
onChanged: isBusy
|
||||
? null
|
||||
: (value) {
|
||||
if (value == null) return;
|
||||
walletsController.selectWalletByRef(value);
|
||||
final selected = walletsController.selectedWallet;
|
||||
if (selected != null) {
|
||||
onChanged?.call(selected);
|
||||
}
|
||||
onChanged(value);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
String _walletLabel(Wallet wallet) {
|
||||
final description = wallet.description?.trim();
|
||||
if (description != null && description.isNotEmpty) {
|
||||
return description;
|
||||
}
|
||||
final name = wallet.name.trim();
|
||||
if (name.isNotEmpty && !_looksLikeId(name)) {
|
||||
return name;
|
||||
}
|
||||
final token = wallet.tokenSymbol?.trim();
|
||||
if (token != null && token.isNotEmpty) {
|
||||
return '$token wallet';
|
||||
}
|
||||
return '${currencyCodeToString(wallet.currency)} wallet';
|
||||
_SourceOptionKey _walletKey(String walletRef) =>
|
||||
(type: PaymentSourceType.wallet, ref: walletRef);
|
||||
|
||||
_SourceOptionKey _ledgerKey(String ledgerAccountRef) =>
|
||||
(type: PaymentSourceType.ledger, ref: ledgerAccountRef);
|
||||
|
||||
String _walletBalance(Wallet wallet) {
|
||||
final symbol = currencyCodeToSymbol(wallet.currency);
|
||||
return '$symbol ${amountToString(wallet.balance)}';
|
||||
}
|
||||
|
||||
bool _looksLikeId(String value) {
|
||||
return RegExp(r'^[a-f0-9]{12,}$', caseSensitive: false)
|
||||
.hasMatch(value);
|
||||
String _ledgerBalance(LedgerAccount account) {
|
||||
final money = account.balance?.balance;
|
||||
final rawAmount = money?.amount.trim();
|
||||
final amount = parseMoneyAmount(rawAmount, fallback: double.nan);
|
||||
final amountText = amount.isNaN
|
||||
? (rawAmount == null || rawAmount.isEmpty ? '--' : rawAmount)
|
||||
: amountToString(amount);
|
||||
|
||||
final currencyCode = (money?.currency ?? account.currency)
|
||||
.trim()
|
||||
.toUpperCase();
|
||||
final symbol = currencySymbolFromCode(currencyCode);
|
||||
if (symbol != null && symbol.trim().isNotEmpty) {
|
||||
return '$symbol $amountText';
|
||||
}
|
||||
if (currencyCode.isNotEmpty) {
|
||||
return '$amountText $currencyCode';
|
||||
}
|
||||
return amountText;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user