diff --git a/modules/EcommerceFinance/View/FinanceLedgerPageData.php b/modules/EcommerceFinance/View/FinanceLedgerPageData.php index 244452a1..e52693fb 100644 --- a/modules/EcommerceFinance/View/FinanceLedgerPageData.php +++ b/modules/EcommerceFinance/View/FinanceLedgerPageData.php @@ -215,33 +215,9 @@ class FinanceLedgerPageData } } - $accountType = trim((string) request('accounts_type', '')); - if ($accountType !== '') { - $query->where(function (Builder $effectiveType) use ($accountType): void { - $effectiveType->where(function (Builder $baseType) use ($accountType): void { - $baseType->whereNotIn( - 'a.account_short', - $this->db()->table('fin_account_type_overrides')->select('account_short'), - ); - if ($accountType === self::ACCOUNT_FILTER_EMPTY) { - $baseType->where(function (Builder $blank): void { - $blank->whereNull('a.account_type')->orWhere('a.account_type', ''); - }); - } else { - $baseType->where('a.account_type', $accountType); - } - }); - - $overrideType = $this->db()->table('fin_account_type_overrides')->select('account_short'); - if ($accountType === self::ACCOUNT_FILTER_EMPTY) { - $overrideType->where(function (Builder $blank): void { - $blank->whereNull('account_type')->orWhere('account_type', ''); - }); - } else { - $overrideType->where('account_type', $accountType); - } - $effectiveType->orWhereIn('a.account_short', $overrideType); - }); + $bankAffiliation = trim((string) request('accounts_bank', '')); + if ($bankAffiliation !== '') { + $this->applyBankAffiliationFilter($query, 'a.account_short', $bankAffiliation); } $balanceRange = trim((string) request('accounts_balance', '')); @@ -287,11 +263,12 @@ class FinanceLedgerPageData : $this->db()->table('fin_account_type_overrides') ->whereIn('account_short', $accountShorts) ->pluck('account_type', 'account_short'); - $rows->getCollection()->transform(static function ($row) use ($statuses, $typeOverrides) { + $rows->getCollection()->transform(function ($row) use ($statuses, $typeOverrides) { $row->account_status = FinanceLedgerAccountStatus::normalize($statuses[$row->account_short] ?? null); if ($typeOverrides->has($row->account_short)) { $row->account_type = (string) ($typeOverrides->get($row->account_short) ?? ''); } + $row->bank_affiliation = $this->bankAffiliation((string) $row->account_short); $row->account_should_keep = FinanceLedgerAccountShouldKeep::display( $row->account_should_keep ?? null, $row->company_plan ?? null, @@ -857,6 +834,56 @@ class FinanceLedgerPageData return ''; } + private function bankAffiliationSqlExpression(string $column): string + { + $suffixesByLabel = []; + foreach (self::BANK_AFFILIATION_SUFFIXES as $suffix => $label) { + $suffixesByLabel[$label][] = $suffix; + } + + $cases = []; + foreach ($suffixesByLabel as $label => $suffixes) { + $conditions = array_map( + fn (string $suffix): string => $column . " LIKE " . $this->db()->getPdo()->quote('%' . $suffix), + $suffixes, + ); + $cases[] = 'WHEN (' . implode(' OR ', $conditions) . ') THEN ' + . $this->db()->getPdo()->quote($label); + } + + return 'CASE ' . implode(' ', $cases) . " ELSE '' END"; + } + + private function applyBankAffiliationFilter(Builder $query, string $column, string $bankAffiliation): void + { + if ($bankAffiliation === self::ACCOUNT_FILTER_EMPTY) { + $query->where(function (Builder $scope) use ($column): void { + foreach (array_keys(self::BANK_AFFILIATION_SUFFIXES) as $suffix) { + $scope->where($column, 'not like', '%' . $suffix); + } + $scope->orWhereNull($column); + }); + + return; + } + + $suffixes = array_keys(array_filter( + self::BANK_AFFILIATION_SUFFIXES, + static fn (string $label): bool => $label === $bankAffiliation, + )); + if ($suffixes === []) { + $query->whereRaw('1 = 0'); + + return; + } + + $query->where(function (Builder $scope) use ($column, $suffixes): void { + foreach ($suffixes as $suffix) { + $scope->orWhere($column, 'like', '%' . $suffix); + } + }); + } + /** * 期初取筛选开始日前每个账户的最近期末;开始日后才起账的账户取首个流水期初。 * 期末取筛选结束日前每个账户的最近期末,避免对期间内每天余额重复求和。 @@ -1575,6 +1602,7 @@ class FinanceLedgerPageData ->whereNotNull('region')->where('region', '<>', '') ->distinct()->orderBy('region')->pluck('region')->all(), 'types' => $types, + 'banks' => array_values(array_unique(array_values(self::BANK_AFFILIATION_SUFFIXES))), ]; } @@ -1708,15 +1736,13 @@ class FinanceLedgerPageData { $a = $this->queryAlias('a'); $statusTable = $this->tableName('fin_account_statuses'); - $typeTable = $this->tableName('fin_account_type_overrides'); $statusExpression = "COALESCE((SELECT NULLIF(s.status, '') FROM {$statusTable} s WHERE s.account_short = {$a}.account_short LIMIT 1), 'normal')"; - $typeExpression = "COALESCE((SELECT NULLIF(o.account_type, '') FROM {$typeTable} o WHERE o.account_short = {$a}.account_short LIMIT 1), NULLIF({$a}.account_type, ''), '')"; $keepExpression = "COALESCE(NULLIF({$a}.account_should_keep, ''), CASE WHEN COALESCE({$a}.company_plan, '') = '' THEN '' WHEN {$a}.company_plan LIKE '%保留%' THEN '是' ELSE '否' END)"; return [ 'company_name' => ['sort' => $a . '.company_name', 'type' => 'text'], 'account_short' => ['sort' => $a . '.account_short', 'type' => 'text'], - 'account_type' => ['sort' => $typeExpression, 'type' => 'enum'], + 'bank_affiliation' => ['sort' => $this->bankAffiliationSqlExpression($a . '.account_short'), 'type' => 'enum'], 'account_status' => ['sort' => $statusExpression, 'type' => 'enum'], 'region' => ['sort' => $a . '.region', 'type' => 'text'], 'account_no' => ['sort' => $a . '.account_no', 'type' => 'text'], diff --git a/resources/views/business/pages/financial-ledger/ledger.blade.php b/resources/views/business/pages/financial-ledger/ledger.blade.php index 0f9e93d1..8b55a3cf 100644 --- a/resources/views/business/pages/financial-ledger/ledger.blade.php +++ b/resources/views/business/pages/financial-ledger/ledger.blade.php @@ -10,6 +10,7 @@ $accountStatusOptions = $ledgerAccountStatusOptions ?? \App\Utilities\FinanceLedgerAccountStatus::OPTIONS; $accountFilterOptions = $ledgerAccountFilterOptions ?? ['regions' => [], 'types' => []]; $accountTypeOptions = $accountFilterOptions['types'] ?? []; + $accountBankOptions = $accountFilterOptions['banks'] ?? []; $accountPlanOptions = $ledgerAccountPlanOptions ?? []; $companyOptions = $ledgerCompanyOptions ?? []; $accountKeepOptions = $ledgerAccountKeepOptions ?? \App\Utilities\FinanceLedgerAccountShouldKeep::OPTIONS; @@ -24,7 +25,7 @@ request('accounts_short'), request('accounts_status'), request('accounts_region'), - request('accounts_type'), + request('accounts_bank'), request('accounts_balance'), ], static fn ($value): bool => trim((string) $value) !== '')); $money = static fn ($value): string => $value === null || $value === '' ? '' : number_format((float) $value, 2); @@ -147,7 +148,7 @@ 'accounts' => [ ['company_name', '公司名称', 'text'], ['account_short', '账户简称', 'text'], - ['account_type', '账户类型', 'enum', ['options' => $optionMap($accountTypeOptions)]], + ['bank_affiliation', '所属银行', 'enum', ['options' => $optionMap($accountBankOptions)]], ['account_status', '账户状态', 'enum', ['options' => $accountStatusOptions, 'headerClass' => 'ledger-status-column']], ['region', '地区', 'text'], ['account_no', '账号', 'text'], @@ -679,7 +680,7 @@
- + @if ($activeAccountFilterCount > 0) 筛选中{{ $activeAccountFilterCount }} @@ -695,7 +696,7 @@ 新增公司/账户 @endif -拖动字段可调整列顺序;拖动表头右侧边界可调整列宽,双击边界恢复默认。
@@ -972,19 +973,7 @@ @endif -| assertSame(1, preg_match('/ |
| 网商银行 | ', $bankAccountRow[1]); $this->assertStringNotContainsString('>预计出清天数', $allHtml); $this->assertStringNotContainsString('>提额目标(万元)', $allHtml); $this->assertStringNotContainsString('>提额状态', $allHtml); $this->assertStringNotContainsString('>提额动作', $allHtml); $this->assertStringNotContainsString('>已做动作', $allHtml); $this->assertStringNotContainsString('>下一步', $allHtml); - $this->assertStringContainsString('caiwu.financial-ledger.accounts.preferences.v4.', $allHtml); + $this->assertStringContainsString('caiwu.financial-ledger.accounts.preferences.v5.', $allHtml); $bankHtml = $this->loginAs()->get($this->pageUrl([ 'accounts_view' => 'bank', @@ -555,6 +561,37 @@ class FinanceLedgerHeaderControlsTest extends FeatureTestCase $this->assertStringNotContainsString('data-ledger-account-row="账户丁网商"', $nonStoreHtml); } + public function test_account_directory_bank_affiliation_filter_uses_account_short_suffix(): void + { + DB::connection('business_mirror')->table('fin_accounts')->insert([ + [ + 'company_name' => '丙公司', + 'account_short' => '账户丙支付宝', + 'account_type' => '公户', + 'current_balance' => 0, + 'daily_available' => 0, + 'blocked_amount' => 0, + ], + [ + 'company_name' => '丁公司', + 'account_short' => '账户丁网商', + 'account_type' => '公户', + 'current_balance' => 0, + 'daily_available' => 0, + 'blocked_amount' => 0, + ], + ]); + + $html = $this->loginAs()->get($this->pageUrl([ + 'accounts_bank' => '支付宝', + ]))->assertOk()->getContent(); + + $this->assertStringContainsString('value="支付宝" selected', $html); + $this->assertStringContainsString('data-ledger-account-row="账户丙支付宝"', $html); + $this->assertStringNotContainsString('data-ledger-account-row="账户乙"', $html); + $this->assertStringNotContainsString('data-ledger-account-row="账户甲"', $html); + } + public function test_account_bank_affiliation_uses_only_the_account_short_suffix(): void { $affiliations = [