diff --git a/app/Http/Controllers/Business/FinanceLedger.php b/app/Http/Controllers/Business/FinanceLedger.php index 46abc8f6..9008fa42 100644 --- a/app/Http/Controllers/Business/FinanceLedger.php +++ b/app/Http/Controllers/Business/FinanceLedger.php @@ -8,6 +8,7 @@ use App\Utilities\FinanceLedgerAccountType; use App\Utilities\FinanceLedgerAccess; use App\Utilities\FinanceLedgerIndependentAccounts; use Illuminate\Http\JsonResponse; +use Illuminate\Pagination\LengthAwarePaginator; use Illuminate\Http\Request; use Illuminate\Routing\Controller; use Illuminate\Support\Carbon; @@ -609,6 +610,9 @@ class FinanceLedger extends Controller ->reorder() ->select('accounts.company_name') ->pluck('accounts.company_name'); + $legalRepresentativeCounts = $legalRepresentativeCountSource->countBy( + static fn ($companyName): string => trim((string) ($legalRepresentatives[trim((string) $companyName)] ?? '')), + ); $summaryQuery = clone $query; $summary = [ 'count' => (clone $query)->count(), @@ -624,19 +628,8 @@ class FinanceLedger extends Controller $sortField = in_array($sortField, $headerFilterColumns, true) ? $sortField : ''; $sortDirection = strtolower((string) $request->query('keep_direction', 'asc')); $sortDirection = $sortDirection === 'desc' ? 'desc' : 'asc'; - if ($sortField !== '') { - if ($sortField === 'legal_representative') { - if ($connection->getSchemaBuilder()->hasTable('ops_fys_company_info')) { - $legalRepresentativeOrder = $connection->table('ops_fys_company_info') - ->select('legalRepresentative') - ->whereColumn('businessLicenseName', 'accounts.company_name') - ->orderByDesc('Id') - ->limit(1); - $query->orderBy($legalRepresentativeOrder, $sortDirection); - } else { - $query->orderBy('accounts.company_name', $sortDirection); - } - } elseif ($sortField === 'account_type') { + if ($sortField !== '' && $sortField !== 'legal_representative') { + if ($sortField === 'account_type') { $query->orderByRaw( 'CASE WHEN type_override_id IS NOT NULL THEN effective_account_type' . ' ELSE sort_account_type END ' . $sortDirection, @@ -674,27 +667,75 @@ class FinanceLedger extends Controller } } - $rows = $query + $hydrateRows = static function ($rows) use ($legalRepresentatives, $legalRepresentativeCounts): void { + $rows->transform(static function ($row) use ($legalRepresentatives, $legalRepresentativeCounts) { + $row->account_status = FinanceLedgerAccountStatus::normalize($row->effective_account_status ?? null); + if ($row->type_override_id !== null) { + $row->account_type = (string) ($row->effective_account_type ?? ''); + } + $row->todo_current_progress = $row->todo_current_progress ?: 'not_started'; + $row->legal_representative = $legalRepresentatives[trim((string) $row->company_name)] ?? null; + $row->legal_representative_count = (int) ($legalRepresentativeCounts[ + trim((string) $row->legal_representative) + ] ?? 0); + + return $row; + }); + }; + + $orderedQuery = $query ->orderBy('accounts.company_name') - ->orderBy('accounts.account_short') - ->paginate(50) - ->appends($filters) - ->appends(array_filter([ + ->orderBy('accounts.account_short'); + if ($sortField === 'legal_representative') { + $allRows = $orderedQuery->get(); + $hydrateRows($allRows); + $allRows = $allRows->sort(static function ($left, $right) use ($sortDirection): int { + $countOrder = (int) $left->legal_representative_count <=> (int) $right->legal_representative_count; + if ($sortDirection === 'desc') { + $countOrder *= -1; + } + if ($countOrder !== 0) { + return $countOrder; + } + + $nameOrder = strnatcasecmp( + trim((string) $left->legal_representative), + trim((string) $right->legal_representative), + ); + if ($nameOrder !== 0) { + return $nameOrder; + } + + $companyOrder = strnatcasecmp((string) $left->company_name, (string) $right->company_name); + + return $companyOrder !== 0 + ? $companyOrder + : strnatcasecmp((string) $left->account_short, (string) $right->account_short); + })->values(); + $lastPage = max(1, (int) ceil($allRows->count() / 50)); + $page = min($lastPage, max(1, (int) $request->query('page', 1))); + $rows = new LengthAwarePaginator( + $allRows->forPage($page, 50)->values(), + $allRows->count(), + 50, + $page, + ['path' => $request->url(), 'query' => $request->query()], + ); + $rows->appends($filters)->appends([ 'keep_sort' => $sortField, 'keep_direction' => $sortField !== '' ? $sortDirection : '', - ])) - ->withQueryString(); - - $rows->getCollection()->transform(static function ($row) use ($legalRepresentatives) { - $row->account_status = FinanceLedgerAccountStatus::normalize($row->effective_account_status ?? null); - if ($row->type_override_id !== null) { - $row->account_type = (string) ($row->effective_account_type ?? ''); - } - $row->todo_current_progress = $row->todo_current_progress ?: 'not_started'; - $row->legal_representative = $legalRepresentatives[trim((string) $row->company_name)] ?? null; - - return $row; - }); + ])->withQueryString(); + } else { + $rows = $orderedQuery + ->paginate(50) + ->appends($filters) + ->appends(array_filter([ + 'keep_sort' => $sortField, + 'keep_direction' => $sortField !== '' ? $sortDirection : '', + ])) + ->withQueryString(); + $hydrateRows($rows->getCollection()); + } $ownerOptions = self::UNRETAINED_OWNER_OPTIONS; $headerOptionValues = static function (iterable $values, bool $includeEmpty = false): array { @@ -714,9 +755,6 @@ class FinanceLedger extends Controller }; $hasEmpty = static fn (string $column): bool => $headerOptionRows ->contains(static fn ($row): bool => trim((string) ($row->{$column} ?? '')) === ''); - $legalRepresentativeCounts = $legalRepresentativeCountSource->countBy( - static fn ($companyName): string => trim((string) ($legalRepresentatives[trim((string) $companyName)] ?? '')), - ); $legalRepresentativeOptions = []; if (($legalRepresentativeCounts[''] ?? 0) > 0) { $legalRepresentativeOptions[] = '未填写(' . $legalRepresentativeCounts[''] . ')'; diff --git a/resources/views/business/pages/financial-ledger/unretained-account-tasks.blade.php b/resources/views/business/pages/financial-ledger/unretained-account-tasks.blade.php index f4209e91..0ea0f0df 100644 --- a/resources/views/business/pages/financial-ledger/unretained-account-tasks.blade.php +++ b/resources/views/business/pages/financial-ledger/unretained-account-tasks.blade.php @@ -5,7 +5,7 @@ $keepHeaderColumns = [ ['column' => 'company_name', 'label' => '公司名称', 'filterType' => 'searchable-enum'], ['column' => 'account_short', 'label' => '账户简称', 'filterType' => 'searchable-enum'], - ['column' => 'legal_representative', 'label' => '法人名称', 'filterType' => 'searchable-enum'], + ['column' => 'legal_representative', 'label' => '法人名称 / 数量', 'filterType' => 'searchable-enum'], ['column' => 'account_type', 'label' => '账户类型', 'filterType' => 'searchable-enum'], ['column' => 'account_status', 'label' => '账户状态', 'filterType' => 'searchable-enum'], ['column' => 'account_no', 'label' => '账号', 'filterType' => 'searchable-enum'], @@ -333,7 +333,7 @@ {{ $row->company_name ?: '未填写' }}