658 lines
25 KiB
PHP
658 lines
25 KiB
PHP
<?php
|
||
|
||
namespace Modules\EcommerceFinance\View;
|
||
|
||
use Illuminate\Database\Query\Builder;
|
||
use Illuminate\Pagination\LengthAwarePaginator;
|
||
use Illuminate\Support\Facades\DB;
|
||
use Modules\EcommerceFinance\Services\BankBillTemplateExporter;
|
||
|
||
final class BankBillPageData
|
||
{
|
||
private const TABLE = 'fin_bank_direct_bills';
|
||
|
||
private const COLUMNS_TABLE = 'fin_bank_direct_bill_columns';
|
||
|
||
private const PER_PAGE = 100;
|
||
|
||
private const GENERIC_COLUMNS = [
|
||
['key' => 'txn_at', 'label' => '账务时间', 'data_type' => 'text', 'canonical' => 'txn_at', 'visible_default' => true],
|
||
['key' => 'bank_name', 'label' => '银行', 'data_type' => 'text', 'canonical' => 'bank_name', 'visible_default' => true],
|
||
['key' => 'company_name', 'label' => '公司名称', 'data_type' => 'text', 'canonical' => 'company_name', 'visible_default' => true],
|
||
['key' => 'account_short', 'label' => '账户简称', 'data_type' => 'text', 'canonical' => 'account_short', 'visible_default' => true],
|
||
['key' => 'account_no', 'label' => '银行账号', 'data_type' => 'text', 'canonical' => 'account_no', 'visible_default' => true],
|
||
['key' => 'record_code', 'label' => '流水号', 'data_type' => 'text', 'canonical' => 'record_code', 'visible_default' => true],
|
||
['key' => 'income', 'label' => '收入', 'data_type' => 'number', 'canonical' => 'income', 'visible_default' => true],
|
||
['key' => 'expense', 'label' => '支出', 'data_type' => 'number', 'canonical' => 'expense', 'visible_default' => true],
|
||
['key' => 'balance', 'label' => '交易后余额', 'data_type' => 'number', 'canonical' => 'balance', 'visible_default' => true],
|
||
['key' => 'counterparty_name', 'label' => '对方户名', 'data_type' => 'text', 'canonical' => 'counterparty_name', 'visible_default' => true],
|
||
['key' => 'counterparty_account', 'label' => '对方账号', 'data_type' => 'text', 'canonical' => 'counterparty_account', 'visible_default' => true],
|
||
['key' => 'counterparty_bank', 'label' => '对方机构', 'data_type' => 'text', 'canonical' => 'counterparty_bank', 'visible_default' => true],
|
||
['key' => 'detail', 'label' => '摘要', 'data_type' => 'text', 'canonical' => 'detail', 'visible_default' => true],
|
||
['key' => 'payment_remark', 'label' => '备注', 'data_type' => 'text', 'canonical' => 'payment_remark', 'visible_default' => true],
|
||
['key' => 'imported_at', 'label' => '下载时间', 'data_type' => 'text', 'canonical' => 'imported_at', 'visible_default' => true],
|
||
];
|
||
|
||
public function page(): array
|
||
{
|
||
$filters = $this->filters();
|
||
$columns = $this->columns($filters['bank']);
|
||
$query = $this->baseQuery($filters);
|
||
$this->applyHeaderFilter($query, $columns, $filters);
|
||
$this->applyHeaderSort($query, $columns, $filters);
|
||
|
||
$summaryQuery = clone $query;
|
||
$summary = [
|
||
'count' => (int) (clone $summaryQuery)->count(),
|
||
'income' => round((float) (clone $summaryQuery)->sum('income'), 2),
|
||
'expense' => round((float) (clone $summaryQuery)->sum('expense'), 2),
|
||
'latest_imported_at' => (clone $summaryQuery)->max('imported_at'),
|
||
];
|
||
|
||
$page = min(
|
||
max(1, (int) request('bills_page', 1)),
|
||
max(1, (int) ceil($summary['count'] / self::PER_PAGE)),
|
||
);
|
||
$rows = (clone $query)
|
||
->orderByDesc('txn_date')
|
||
->orderByDesc('txn_at')
|
||
->orderByDesc('id')
|
||
->forPage($page, self::PER_PAGE)
|
||
->get();
|
||
$rows->transform(function (object $row): object {
|
||
$row->raw_data_values = $this->rawData($row->raw_data ?? null);
|
||
|
||
return $row;
|
||
});
|
||
$paginator = new LengthAwarePaginator(
|
||
$rows->values()->all(),
|
||
$summary['count'],
|
||
self::PER_PAGE,
|
||
$page,
|
||
['path' => request()->url(), 'query' => request()->query()],
|
||
);
|
||
$paginator->setPageName('bills_page');
|
||
|
||
return [
|
||
'filters' => $filters,
|
||
'period' => ['from' => $filters['from'], 'to' => $filters['to']],
|
||
'summary' => $summary,
|
||
'page' => $paginator,
|
||
'bankOptions' => $this->bankOptions(),
|
||
'accountOptions' => $this->accountOptions(),
|
||
'columns' => $columns,
|
||
'importTemplates' => BankBillTemplateExporter::templates(),
|
||
];
|
||
}
|
||
|
||
public function filterOptions(string $columnKey): array
|
||
{
|
||
$filters = $this->filters();
|
||
$column = collect($this->columns($filters['bank']))
|
||
->firstWhere('key', $columnKey);
|
||
if (! is_array($column)) {
|
||
throw new \InvalidArgumentException('请选择有效的银行账单列。');
|
||
}
|
||
|
||
if (($column['canonical'] ?? null) === 'account_no') {
|
||
$options = collect($this->directAccountOptions())
|
||
->map(static fn (array $account): array => [
|
||
'value' => (string) $account['account_no'],
|
||
'label' => (string) $account['label'],
|
||
'bank' => (string) $account['bank_name'],
|
||
])
|
||
->values()
|
||
->all();
|
||
|
||
return [
|
||
'column' => $columnKey,
|
||
'label' => (string) $column['label'],
|
||
'options' => $options,
|
||
];
|
||
}
|
||
|
||
$query = $this->baseQuery($filters);
|
||
$values = [];
|
||
if (($column['canonical'] ?? null) === 'txn_at') {
|
||
$dateExpression = match ($query->getConnection()->getDriverName()) {
|
||
'mysql', 'mariadb' => 'DATE(txn_at)',
|
||
'sqlite' => 'date(txn_at)',
|
||
default => null,
|
||
};
|
||
if ($dateExpression !== null) {
|
||
$values = (clone $query)
|
||
->selectRaw($dateExpression . ' AS option_value')
|
||
->whereNotNull('txn_at')
|
||
->distinct()
|
||
->orderBy('option_value')
|
||
->limit(500)
|
||
->pluck('option_value')
|
||
->all();
|
||
}
|
||
} elseif (str_starts_with($columnKey, 'source_')) {
|
||
$expression = $this->rawValueExpression($query, $columnKey);
|
||
if ($expression !== null) {
|
||
[$sql, $path] = $expression;
|
||
$values = (clone $query)
|
||
->selectRaw($sql . ' AS option_value', [$path])
|
||
->whereNotNull('raw_data')
|
||
->distinct()
|
||
->orderBy('option_value')
|
||
->limit(500)
|
||
->pluck('option_value')
|
||
->all();
|
||
}
|
||
} elseif (is_string($column['canonical'] ?? null) && $column['canonical'] !== '') {
|
||
$canonical = (string) $column['canonical'];
|
||
$values = (clone $query)
|
||
->whereNotNull($canonical)
|
||
->where($canonical, '<>', '')
|
||
->distinct()
|
||
->orderBy($canonical)
|
||
->limit(500)
|
||
->pluck($canonical)
|
||
->all();
|
||
}
|
||
|
||
$options = collect($values)
|
||
->map(static fn (mixed $value): string => trim((string) $value))
|
||
->filter(static fn (string $value): bool => $value !== '' && $value !== '-')
|
||
->unique()
|
||
->values()
|
||
->map(static fn (string $value): array => [
|
||
'value' => $value,
|
||
'label' => $value,
|
||
'bank' => '',
|
||
])
|
||
->all();
|
||
|
||
return [
|
||
'column' => $columnKey,
|
||
'label' => (string) $column['label'],
|
||
'options' => $options,
|
||
];
|
||
}
|
||
|
||
public function accountStatement(
|
||
string $bank,
|
||
string $accountNo,
|
||
?string $from = null,
|
||
?string $to = null,
|
||
): array {
|
||
$bank = trim($bank);
|
||
$accountNo = preg_replace('/\D+/', '', $accountNo) ?? '';
|
||
if ($bank === '' || $accountNo === '') {
|
||
throw new \InvalidArgumentException('银行和账号不能为空。');
|
||
}
|
||
|
||
$connection = DB::connection((string) config('upstream_mirrors.connection', 'business_mirror'));
|
||
$query = $connection->table(self::TABLE)
|
||
->where('bank_name', $bank)
|
||
->where('account_no', $accountNo);
|
||
$from = $from !== null ? $this->date($from) : null;
|
||
$to = $to !== null ? $this->date($to) : null;
|
||
if ($from !== null) {
|
||
$query->where('txn_date', '>=', $from);
|
||
}
|
||
if ($to !== null) {
|
||
$query->where('txn_date', '<=', $to);
|
||
}
|
||
|
||
$count = (int) (clone $query)->count();
|
||
$income = round((float) (clone $query)->sum('income'), 2);
|
||
$expense = round((float) (clone $query)->sum('expense'), 2);
|
||
$rows = (clone $query)
|
||
->orderByDesc('txn_date')
|
||
->orderByDesc('txn_at')
|
||
->orderByDesc('id')
|
||
->limit(200)
|
||
->get()
|
||
->map(function (object $row): object {
|
||
$row->raw_data_values = $this->rawData($row->raw_data ?? null);
|
||
|
||
return $row;
|
||
});
|
||
$account = $connection->table('fin_accounts')
|
||
->where('account_no', $accountNo)
|
||
->first();
|
||
$accountLabel = trim((string) ($account->account_short ?? ''));
|
||
if ($accountLabel === '' && $rows->isNotEmpty()) {
|
||
$accountLabel = trim((string) ($rows->first()->account_short ?? ''));
|
||
}
|
||
|
||
$columns = array_values(array_filter(
|
||
$this->columns($bank),
|
||
static fn (array $column): bool => (bool) ($column['visible_default'] ?? true),
|
||
));
|
||
if ($columns === []) {
|
||
$columns = $this->columns($bank);
|
||
}
|
||
|
||
return [
|
||
'title' => trim($bank . ' · ' . ($accountLabel !== '' ? $accountLabel : $accountNo)),
|
||
'bank' => $bank,
|
||
'account_no' => $accountNo,
|
||
'period' => ['from' => $from, 'to' => $to],
|
||
'count' => $count,
|
||
'displayed' => $rows->count(),
|
||
'income' => $income,
|
||
'expense' => $expense,
|
||
'columns' => array_map(
|
||
static fn (array $column): array => [
|
||
'key' => (string) $column['key'],
|
||
'label' => (string) $column['label'],
|
||
'type' => (string) ($column['data_type'] ?? 'text'),
|
||
],
|
||
$columns,
|
||
),
|
||
'rows' => $rows->map(function (object $row) use ($columns): array {
|
||
$values = [];
|
||
foreach ($columns as $column) {
|
||
$values[(string) $column['key']] = $this->cellValue($row, $column);
|
||
}
|
||
|
||
return ['values' => $values];
|
||
})->values()->all(),
|
||
];
|
||
}
|
||
|
||
private function filters(): array
|
||
{
|
||
$defaultFrom = now()->startOfMonth()->toDateString();
|
||
$defaultTo = now()->endOfMonth()->toDateString();
|
||
$from = $this->date((string) request('bills_from', '')) ?? $defaultFrom;
|
||
$to = $this->date((string) request('bills_to', '')) ?? $defaultTo;
|
||
if ($from > $to) {
|
||
[$from, $to] = [$to, $from];
|
||
}
|
||
|
||
return [
|
||
'from' => $from,
|
||
'to' => $to,
|
||
'bank' => trim((string) request('bills_bank', 'all')) ?: 'all',
|
||
'account_no' => preg_replace('/\D+/', '', (string) request('bills_account_no', '')) ?? '',
|
||
'search' => trim((string) request('bills_search', '')),
|
||
'filter_column' => trim((string) request('bills_filter_column', '')),
|
||
'filter_values' => $this->filterValues(),
|
||
'filter_active' => request()->query->has('bills_filter_values'),
|
||
'sort_column' => trim((string) request('bills_sort_column', '')),
|
||
'sort_direction' => strtolower(trim((string) request('bills_sort_direction', ''))),
|
||
];
|
||
}
|
||
|
||
private function filterValues(): array
|
||
{
|
||
$raw = request('bills_filter_values');
|
||
if (is_array($raw)) {
|
||
$values = $raw;
|
||
} else {
|
||
$decoded = json_decode((string) $raw, true);
|
||
$values = is_array($decoded) ? $decoded : [];
|
||
}
|
||
|
||
return collect($values)
|
||
->map(static fn (mixed $value): string => trim((string) $value))
|
||
->filter(static fn (string $value): bool => $value !== '')
|
||
->unique()
|
||
->values()
|
||
->all();
|
||
}
|
||
|
||
private function date(string $value): ?string
|
||
{
|
||
if ($value === '') {
|
||
return null;
|
||
}
|
||
$date = \DateTimeImmutable::createFromFormat('!Y-m-d', $value);
|
||
|
||
return $date !== false && $date->format('Y-m-d') === $value ? $value : null;
|
||
}
|
||
|
||
private function baseQuery(array $filters): Builder
|
||
{
|
||
$query = DB::connection((string) config('upstream_mirrors.connection', 'business_mirror'))
|
||
->table(self::TABLE);
|
||
|
||
if ($filters['from'] !== '') {
|
||
$query->where('txn_date', '>=', $filters['from']);
|
||
}
|
||
if ($filters['to'] !== '') {
|
||
$query->where('txn_date', '<=', $filters['to']);
|
||
}
|
||
if ($filters['bank'] !== 'all') {
|
||
$query->where('bank_name', $filters['bank']);
|
||
}
|
||
if ($filters['account_no'] !== '') {
|
||
$query->where('account_no', $filters['account_no']);
|
||
}
|
||
if ($filters['search'] !== '') {
|
||
$search = $filters['search'];
|
||
$query->where(function (Builder $builder) use ($search): void {
|
||
foreach ([
|
||
'company_name',
|
||
'account_short',
|
||
'account_no',
|
||
'counterparty_name',
|
||
'counterparty_account',
|
||
'counterparty_bank',
|
||
'detail',
|
||
'payment_remark',
|
||
'record_code',
|
||
] as $column) {
|
||
$builder->orWhere($column, 'like', '%' . $search . '%');
|
||
}
|
||
});
|
||
}
|
||
|
||
return $query;
|
||
}
|
||
|
||
private function applyHeaderFilter(Builder $query, array $columns, array $filters): void
|
||
{
|
||
$columnKey = (string) ($filters['filter_column'] ?? '');
|
||
$values = (array) ($filters['filter_values'] ?? []);
|
||
if ($columnKey === '') {
|
||
return;
|
||
}
|
||
|
||
$column = collect($columns)->firstWhere('key', $columnKey);
|
||
if (! is_array($column)) {
|
||
return;
|
||
}
|
||
|
||
if ($values === []) {
|
||
if ((bool) ($filters['filter_active'] ?? false)) {
|
||
$query->whereRaw('1 = 0');
|
||
}
|
||
|
||
return;
|
||
}
|
||
|
||
if (($column['canonical'] ?? null) === 'account_no') {
|
||
$query->whereIn('account_no', $values);
|
||
|
||
return;
|
||
}
|
||
|
||
if (str_starts_with($columnKey, 'source_')) {
|
||
$expression = $this->rawValueExpression($query, $columnKey);
|
||
if ($expression === null) {
|
||
return;
|
||
}
|
||
[$sql, $path] = $expression;
|
||
$query->whereRaw(
|
||
$sql . ' IN (' . implode(',', array_fill(0, count($values), '?')) . ')',
|
||
[$path, ...$values],
|
||
);
|
||
|
||
return;
|
||
}
|
||
|
||
if (is_string($column['canonical'] ?? null) && $column['canonical'] !== '') {
|
||
$canonical = (string) $column['canonical'];
|
||
if ($canonical === 'txn_at') {
|
||
$query->where(function (Builder $dateQuery) use ($values): void {
|
||
foreach ($values as $value) {
|
||
$dateQuery->orWhereDate('txn_at', $value);
|
||
}
|
||
});
|
||
} else {
|
||
$query->whereIn($canonical, $values);
|
||
}
|
||
}
|
||
}
|
||
|
||
private function applyHeaderSort(Builder $query, array $columns, array $filters): void
|
||
{
|
||
$columnKey = (string) ($filters['sort_column'] ?? '');
|
||
$direction = (string) ($filters['sort_direction'] ?? '');
|
||
if ($columnKey === '' || ! in_array($direction, ['asc', 'desc'], true)) {
|
||
return;
|
||
}
|
||
|
||
$column = collect($columns)->firstWhere('key', $columnKey);
|
||
if (! is_array($column)) {
|
||
return;
|
||
}
|
||
|
||
if (str_starts_with($columnKey, 'source_')) {
|
||
$expression = $this->rawValueExpression($query, $columnKey);
|
||
if ($expression !== null) {
|
||
[$sql, $path] = $expression;
|
||
$query->orderByRaw($sql . ' ' . strtoupper($direction), [$path]);
|
||
}
|
||
|
||
return;
|
||
}
|
||
|
||
if (is_string($column['canonical'] ?? null) && $column['canonical'] !== '') {
|
||
$query->orderBy((string) $column['canonical'], $direction);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* @return array{0:string,1:string}|null
|
||
*/
|
||
private function rawValueExpression(Builder $query, string $columnKey): ?array
|
||
{
|
||
if (preg_match('/^source_c\d{3}$/', $columnKey) !== 1) {
|
||
return null;
|
||
}
|
||
|
||
$path = '$."' . $columnKey . '"';
|
||
|
||
return match ($query->getConnection()->getDriverName()) {
|
||
'mysql', 'mariadb' => ['JSON_UNQUOTE(JSON_EXTRACT(raw_data, ?))', $path],
|
||
'sqlite' => ['json_extract(raw_data, ?)', $path],
|
||
default => null,
|
||
};
|
||
}
|
||
|
||
private function bankOptions(): array
|
||
{
|
||
$connection = DB::connection((string) config('upstream_mirrors.connection', 'business_mirror'));
|
||
$billBanks = $connection
|
||
->table(self::TABLE)
|
||
->whereNotNull('bank_name')
|
||
->where('bank_name', '<>', '')
|
||
->distinct()
|
||
->pluck('bank_name')
|
||
->all();
|
||
$configuredBanks = $connection
|
||
->table(self::COLUMNS_TABLE)
|
||
->whereNotNull('bank_name')
|
||
->where('bank_name', '<>', '')
|
||
->distinct()
|
||
->pluck('bank_name')
|
||
->all();
|
||
|
||
return collect(array_merge($billBanks, $configuredBanks))
|
||
->map(static fn (mixed $value): string => trim((string) $value))
|
||
->filter()
|
||
->unique()
|
||
->sort()
|
||
->values()
|
||
->all();
|
||
}
|
||
|
||
private function columns(string $bank): array
|
||
{
|
||
if ($bank === 'all') {
|
||
return self::GENERIC_COLUMNS;
|
||
}
|
||
|
||
$configured = DB::connection((string) config('upstream_mirrors.connection', 'business_mirror'))
|
||
->table(self::COLUMNS_TABLE)
|
||
->where('bank_name', $bank)
|
||
->orderBy('sort_order')
|
||
->orderBy('id')
|
||
->get();
|
||
if ($configured->isEmpty()) {
|
||
return self::GENERIC_COLUMNS;
|
||
}
|
||
|
||
return $configured
|
||
->map(fn (object $column): array => [
|
||
'key' => (string) $column->column_key,
|
||
'label' => (string) $column->label,
|
||
'data_type' => (string) $column->data_type,
|
||
'canonical' => $this->canonicalField((string) ($column->source_header ?? $column->label)),
|
||
'visible_default' => (bool) $column->visible_default,
|
||
])
|
||
->values()
|
||
->all();
|
||
}
|
||
|
||
private function rawData(mixed $value): array
|
||
{
|
||
if (is_array($value)) {
|
||
return $value;
|
||
}
|
||
if (! is_string($value) || trim($value) === '') {
|
||
return [];
|
||
}
|
||
|
||
$decoded = json_decode($value, true);
|
||
|
||
return is_array($decoded) ? $decoded : [];
|
||
}
|
||
|
||
private function canonicalField(string $header): ?string
|
||
{
|
||
return match ($header) {
|
||
'账号', '银行账号' => 'account_no',
|
||
'账号名称', '账户名称', '账户别名' => 'account_short',
|
||
'单位名称', '公司名称' => 'company_name',
|
||
'交易日' => 'txn_date',
|
||
'交易时间' => 'txn_at',
|
||
'收入金额', '贷方金额' => 'income',
|
||
'支出金额', '借方金额' => 'expense',
|
||
'账户余额', '余额' => 'balance',
|
||
'对方户名', '收(付)方名称' => 'counterparty_name',
|
||
'对方账号', '收(付)方账号' => 'counterparty_account',
|
||
'对方开户行', '收(付)方开户行名' => 'counterparty_bank',
|
||
'附言', '用途' => 'payment_remark',
|
||
'摘要', '交易类型', '交易描述' => 'detail',
|
||
'流水号', '凭证号' => 'record_code',
|
||
default => null,
|
||
};
|
||
}
|
||
|
||
private function cellValue(object $row, array $column): string
|
||
{
|
||
$key = (string) ($column['key'] ?? '');
|
||
$rawValues = is_array($row->raw_data_values ?? null) ? $row->raw_data_values : [];
|
||
$value = array_key_exists($key, $rawValues) ? $rawValues[$key] : null;
|
||
if ($value === null || $value === '') {
|
||
$canonical = $column['canonical'] ?? null;
|
||
$value = $canonical ? ($row->{$canonical} ?? null) : null;
|
||
}
|
||
if ($value === null || trim((string) $value) === '' || trim((string) $value) === '-') {
|
||
return '—';
|
||
}
|
||
if (($column['data_type'] ?? 'text') === 'number') {
|
||
$normalised = str_replace([',', ',', ' '], '', (string) $value);
|
||
if (is_numeric($normalised)) {
|
||
return number_format((float) $normalised, 2, '.', ',');
|
||
}
|
||
}
|
||
|
||
return (string) $value;
|
||
}
|
||
|
||
private function accountOptions(): array
|
||
{
|
||
return collect($this->directAccountOptions())
|
||
->values()
|
||
->all();
|
||
}
|
||
|
||
private function directAccountOptions(): array
|
||
{
|
||
$connection = DB::connection((string) config('upstream_mirrors.connection', 'business_mirror'));
|
||
$query = $connection->table('fin_accounts as a')
|
||
->leftJoin(
|
||
'fin_flow_summary_account_sources as s',
|
||
's.account_short',
|
||
'=',
|
||
'a.account_short',
|
||
)
|
||
->select([
|
||
'a.company_name',
|
||
'a.account_short',
|
||
'a.account_no',
|
||
'a.bank_name',
|
||
])
|
||
->whereNotNull('a.account_no')
|
||
->where('a.account_no', '<>', '')
|
||
->where(function (Builder $builder): void {
|
||
$builder->where('s.data_source', '银企直连')
|
||
->orWhere(function (Builder $directBank): void {
|
||
$directBank
|
||
->where(function (Builder $accountType): void {
|
||
$accountType->whereNull('a.account_type')
|
||
->orWhere('a.account_type', '<>', '个人卡');
|
||
})
|
||
->where(function (Builder $bank): void {
|
||
$bank->where('a.bank_name', 'like', '%招商银行%')
|
||
->orWhere('a.account_short', 'like', '%招行%');
|
||
});
|
||
})
|
||
->orWhere(function (Builder $wangshang): void {
|
||
$wangshang
|
||
->where(function (Builder $accountType): void {
|
||
$accountType->where('a.account_type', '网商银行')
|
||
->orWhere('a.bank_name', 'like', '%网商银行%')
|
||
->orWhere('a.account_short', 'like', '%网商%');
|
||
});
|
||
});
|
||
})
|
||
->distinct()
|
||
->orderBy('a.account_short')
|
||
->orderBy('a.account_no')
|
||
->get();
|
||
|
||
return $query
|
||
->map(function (object $row): array {
|
||
$bank = $this->directAccountBank(
|
||
(string) ($row->account_short ?? ''),
|
||
(string) ($row->bank_name ?? ''),
|
||
);
|
||
|
||
return [
|
||
'bank_name' => $bank,
|
||
'company_name' => (string) ($row->company_name ?? ''),
|
||
'account_short' => (string) $row->account_short,
|
||
'account_no' => preg_replace('/\D+/', '', (string) $row->account_no) ?? '',
|
||
'label' => trim(implode(' · ', array_filter([
|
||
$bank,
|
||
(string) $row->account_short,
|
||
(string) $row->account_no,
|
||
(string) ($row->company_name ?? ''),
|
||
]))),
|
||
];
|
||
})
|
||
->filter(static fn (array $row): bool => $row['account_no'] !== '')
|
||
->unique(static fn (array $row): string => $row['bank_name'] . '|' . $row['account_no'])
|
||
->sortBy(static fn (array $row): string => $row['label'])
|
||
->values()
|
||
->all();
|
||
}
|
||
|
||
private function directAccountBank(string $accountShort, string $bankName): string
|
||
{
|
||
if (str_contains($accountShort, '招行') || str_contains($bankName, '招商银行')) {
|
||
return '招商银行';
|
||
}
|
||
if (str_contains($accountShort, '华夏') || str_contains($bankName, '华夏银行')) {
|
||
return '华夏银行';
|
||
}
|
||
if (str_contains($accountShort, '网商') || str_contains($bankName, '网商银行')) {
|
||
return '网商银行';
|
||
}
|
||
|
||
return trim($bankName);
|
||
}
|
||
|
||
}
|