@extends('layouts.finance') @section('title', $mode === 'create' ? 'Add Bank Account' : 'Edit Bank Account') @section('subtitle', 'Create and maintain database-backed bank accounts') @push('styles') @endpush @section('content') @php $isCreate = $mode === 'create'; $action = $isCreate ? route('finance.accounts.store') : route('finance.accounts.update', $account); $countryFor = fn($currency) => $currency === 'PKR' ? 'Pakistan' : ($currency === 'AUD' ? 'Australia' : 'United States'); $logoColorFor = fn($currency) => $currency === 'PKR' ? '#1e3a8a' : ($currency === 'AUD' ? '#0d9488' : '#dc2626'); $shortFor = fn($bank) => strtoupper(substr(preg_replace('/[^A-Za-z0-9]/', '', $bank->bank_name ?: $bank->name), 0, 4)) ?: 'BANK'; $maskedAccount = fn($bank) => $bank->account_number ? '****'.substr($bank->account_number, -4) : 'No #'; $cardShortFor = fn($card) => strtoupper(substr(preg_replace('/[^A-Za-z0-9]/', '', $card->short_name ?: $card->issuer ?: $card->card_name), 0, 4)) ?: 'CARD'; $cardMaskedAccount = fn($card) => $card->card_name ? $card->card_name : 'Credit Card'; $linkGroups = $linkableAccounts->groupBy(fn($bank) => $countryFor($bank->currency)); $cardLinkGroups = $linkableCreditCards->groupBy(fn($card) => $countryFor($card->currency)); $creditLinkIdsByAccount = $account->exists ? $account->creditSources->mapWithKeys(fn($bank) => [$bank->id => $bank->pivot->id]) : collect(); $debitLinkIdsByAccount = $account->exists ? $account->debitDestinations->mapWithKeys(fn($bank) => [$bank->id => $bank->pivot->id]) : collect(); $creditCardLinkIdsByCard = $account->exists ? $account->creditCardSources->mapWithKeys(fn($card) => [$card->id => $card->pivot->id]) : collect(); $debitCardLinkIdsByCard = $account->exists ? $account->debitCardDestinations->mapWithKeys(fn($card) => [$card->id => $card->pivot->id]) : collect(); $transactionCategories = \App\Models\BankAccount::query() ->whereNotNull('transaction_category') ->distinct() ->orderBy('transaction_category') ->pluck('transaction_category') ->merge(['Pak Banks', 'Wallets', 'US Banks', 'AUS Banks']) ->filter() ->unique() ->values(); // Currency implied by each category (built-in mapping + currencies of // existing accounts), so selecting a category can auto-pick its currency. $categoryCurrencies = $transactionCategories ->mapWithKeys(fn($category) => [$category => \App\Models\BankAccount::defaultCurrencyForCategory($category)]) ->filter(); @endphp