From 4ca86853243dfbe7994bccd114e7947c3bc4f236 Mon Sep 17 00:00:00 2001 From: shanshi <3316401569@qq.com> Date: Fri, 13 Nov 2026 18:20:47 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E8=B4=A2=E5=8A=A1=E5=8F=B0=E8=B4=A6):=20?= =?UTF-8?q?=E9=87=8D=E5=81=9A=E7=BD=91=E5=95=86=E9=93=B6=E8=A1=8C=E5=9F=BA?= =?UTF-8?q?=E6=9C=AC=E4=BF=A1=E6=81=AF=E9=A1=B5=E5=B9=B6=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E8=A1=A8=E6=A0=BC=E5=AF=BC=E5=85=A5=20(#1163)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/Business/FinanceLedger.php | 41 +++ ...wangshang_basic_fields_to_fin_accounts.php | 51 +++ .../Services/FinanceLedgerAccountCreator.php | 2 + .../Services/FinanceLedgerAccountEditor.php | 2 + .../FinanceLedgerWangshangBasicImporter.php | 341 ++++++++++++++++++ .../View/FinanceLedgerPageData.php | 2 + .../pages/financial-ledger/ledger.blade.php | 81 ++++- routes/admin.php | 4 + .../Business/FinanceLedgerManualEditTest.php | 4 + .../FinanceLedgerWangshangBasicImportTest.php | 132 +++++++ 10 files changed, 654 insertions(+), 6 deletions(-) create mode 100644 database/migrations/2026_11_13_180000_add_wangshang_basic_fields_to_fin_accounts.php create mode 100644 modules/EcommerceFinance/Services/FinanceLedgerWangshangBasicImporter.php create mode 100644 tests/Feature/Business/FinanceLedgerWangshangBasicImportTest.php diff --git a/app/Http/Controllers/Business/FinanceLedger.php b/app/Http/Controllers/Business/FinanceLedger.php index 8d4cc078..49ad1ead 100644 --- a/app/Http/Controllers/Business/FinanceLedger.php +++ b/app/Http/Controllers/Business/FinanceLedger.php @@ -10,6 +10,7 @@ use App\Utilities\FinanceLedgerFlowSummaryDataSource; use App\Utilities\FinanceLedgerIndependentAccounts; use Illuminate\Http\JsonResponse; use Illuminate\Pagination\LengthAwarePaginator; +use Illuminate\Http\RedirectResponse; use Illuminate\Http\Request; use Illuminate\Routing\Controller; use Illuminate\Support\Carbon; @@ -31,6 +32,7 @@ use Modules\EcommerceFinance\Services\FinanceLedgerPayableMappingBulk; use Modules\EcommerceFinance\Services\FinanceLedgerShopDataSynchronizer; use Modules\EcommerceFinance\Services\FinanceLedgerShopMappingBulk; use Modules\EcommerceFinance\Services\FinanceLedgerTransactionWriter; +use Modules\EcommerceFinance\Services\FinanceLedgerWangshangBasicImporter; use Modules\EcommerceFinance\View\FinanceLedgerPageData; use Symfony\Component\HttpFoundation\StreamedResponse; use Throwable; @@ -67,6 +69,41 @@ class FinanceLedger extends Controller ]); } + public function importWangshangBasic( + Request $request, + FinanceLedgerWangshangBasicImporter $importer, + ): RedirectResponse { + abort_unless( + FinanceLedgerAccess::canEditAccountStatus(user()), + 403, + '只有财务台账管理员可以导入网商银行基本信息。', + ); + + $data = $request->validate([ + 'file' => ['required', 'file', 'mimes:xlsx', 'max:5120'], + ]); + + try { + $result = $importer->import((string) $data['file']->getRealPath()); + } catch (Throwable $exception) { + throw ValidationException::withMessages([ + 'file' => $exception->getMessage(), + ]); + } + + return redirect()->back()->with( + 'success', + sprintf( + '网商银行基本信息导入完成:共 %d 条,新增 %d 条,更新 %d 条,未变化 %d 条,保留表格外现有账户 %d 条。', + $result['total'], + $result['inserted'], + $result['updated'], + $result['unchanged'], + $result['preserved'], + ), + ); + } + public function storeAccount(Request $request, FinanceLedgerAccountCreator $creator): JsonResponse { abort_unless( @@ -87,10 +124,12 @@ class FinanceLedger extends Controller 'company_remark' => ['nullable', 'string', 'max:500'], 'account_short' => ['required', 'string', 'max:100'], 'account_type' => ['nullable', 'string', 'max:50'], + 'legal_representative' => ['nullable', 'string', 'max:100'], 'status' => ['required', 'string', Rule::in(array_keys(FinanceLedgerAccountStatus::OPTIONS))], 'region' => ['nullable', 'string', 'max:50'], 'account_no' => ['nullable', 'string', 'max:50'], 'bank_name' => ['nullable', 'string', 'max:200'], + 'bound_alipay' => ['nullable', 'string', 'max:200'], 'bank_city_branch' => ['nullable', 'string', 'max:100'], 'company_plan' => ['nullable', 'string', 'max:50'], 'account_should_keep' => [ @@ -1360,10 +1399,12 @@ class FinanceLedger extends Controller 'company_name' => ['required', 'string', 'max:100'], 'account_short' => ['required', 'string', 'max:100'], 'account_type' => ['nullable', 'string', 'max:50'], + 'legal_representative' => ['nullable', 'string', 'max:100'], 'status' => ['required', 'string', Rule::in(array_keys(FinanceLedgerAccountStatus::OPTIONS))], 'region' => ['nullable', 'string', 'max:50'], 'account_no' => ['nullable', 'string', 'max:50'], 'bank_name' => ['nullable', 'string', 'max:200'], + 'bound_alipay' => ['nullable', 'string', 'max:200'], 'bank_city_branch' => ['nullable', 'string', 'max:100'], 'company_plan' => ['nullable', 'string', 'max:50'], 'account_should_keep' => ['nullable', 'string', Rule::in(FinanceLedgerAccountShouldKeep::OPTIONS)], diff --git a/database/migrations/2026_11_13_180000_add_wangshang_basic_fields_to_fin_accounts.php b/database/migrations/2026_11_13_180000_add_wangshang_basic_fields_to_fin_accounts.php new file mode 100644 index 00000000..d2995d4b --- /dev/null +++ b/database/migrations/2026_11_13_180000_add_wangshang_basic_fields_to_fin_accounts.php @@ -0,0 +1,51 @@ +connection()); + if (! $schema->hasTable('fin_accounts')) { + return; + } + + if (! $schema->hasColumn('fin_accounts', 'legal_representative')) { + $schema->table('fin_accounts', function (Blueprint $table): void { + $table->string('legal_representative', 100)->nullable()->after('account_short'); + }); + } + if (! $schema->hasColumn('fin_accounts', 'bound_alipay')) { + $schema->table('fin_accounts', function (Blueprint $table): void { + $table->string('bound_alipay', 200)->nullable()->after('bank_name'); + }); + } + } + + public function down(): void + { + $schema = Schema::connection($this->connection()); + if (! $schema->hasTable('fin_accounts')) { + return; + } + + if ($schema->hasColumn('fin_accounts', 'bound_alipay')) { + $schema->table('fin_accounts', function (Blueprint $table): void { + $table->dropColumn('bound_alipay'); + }); + } + if ($schema->hasColumn('fin_accounts', 'legal_representative')) { + $schema->table('fin_accounts', function (Blueprint $table): void { + $table->dropColumn('legal_representative'); + }); + } + } +}; diff --git a/modules/EcommerceFinance/Services/FinanceLedgerAccountCreator.php b/modules/EcommerceFinance/Services/FinanceLedgerAccountCreator.php index 84b0c368..6362e385 100644 --- a/modules/EcommerceFinance/Services/FinanceLedgerAccountCreator.php +++ b/modules/EcommerceFinance/Services/FinanceLedgerAccountCreator.php @@ -92,9 +92,11 @@ class FinanceLedgerAccountCreator $accountId = $connection->table('fin_accounts')->insertGetId([ 'company_name' => $companyName, 'account_short' => $accountShort, + 'legal_representative' => $this->nullableText($data['legal_representative'] ?? null), 'region' => $this->nullableText($data['region'] ?? null), 'account_no' => $accountNo !== '' ? $accountNo : null, 'bank_name' => $this->nullableText($data['bank_name'] ?? null), + 'bound_alipay' => $this->nullableText($data['bound_alipay'] ?? null), 'bank_city_branch' => $this->nullableText($data['bank_city_branch'] ?? null), 'account_type' => $accountType !== '' ? $accountType : null, 'company_plan' => $companyPlan !== '' ? $companyPlan : null, diff --git a/modules/EcommerceFinance/Services/FinanceLedgerAccountEditor.php b/modules/EcommerceFinance/Services/FinanceLedgerAccountEditor.php index 8c446e5d..3e5ae1ec 100644 --- a/modules/EcommerceFinance/Services/FinanceLedgerAccountEditor.php +++ b/modules/EcommerceFinance/Services/FinanceLedgerAccountEditor.php @@ -64,9 +64,11 @@ class FinanceLedgerAccountEditor $values = [ 'company_name' => $companyName, 'account_short' => $accountShort, + 'legal_representative' => $this->nullableText($data['legal_representative'] ?? null), 'region' => $this->nullableText($data['region'] ?? null), 'account_no' => $accountNo, 'bank_name' => $this->nullableText($data['bank_name'] ?? null), + 'bound_alipay' => $this->nullableText($data['bound_alipay'] ?? null), 'bank_city_branch' => $this->nullableText($data['bank_city_branch'] ?? null), 'account_type' => $accountType, 'company_plan' => $this->nullableText($data['company_plan'] ?? null), diff --git a/modules/EcommerceFinance/Services/FinanceLedgerWangshangBasicImporter.php b/modules/EcommerceFinance/Services/FinanceLedgerWangshangBasicImporter.php new file mode 100644 index 00000000..402fa024 --- /dev/null +++ b/modules/EcommerceFinance/Services/FinanceLedgerWangshangBasicImporter.php @@ -0,0 +1,341 @@ +connection = $databases->connection( + (string) config('upstream_mirrors.connection', 'business_mirror'), + ); + } + + /** + * @return array{total:int,inserted:int,updated:int,unchanged:int,preserved:int} + */ + public function import(string $path): array + { + if (! is_file($path)) { + throw new RuntimeException('导入文件不存在。'); + } + + return $this->connection->transaction(function () use ($path): array { + $current = $this->connection->table('fin_accounts') + ->where(function ($scope): void { + $scope->where('account_short', 'like', '%网商%') + ->orWhere('account_type', 'like', '%网商%'); + }) + ->orderBy('id') + ->lockForUpdate() + ->get() + ->all(); + + $byAccountNo = []; + $byCompany = []; + foreach ($current as $account) { + $accountNo = $this->normalizeAccountNo($account->account_no ?? null); + if ($accountNo !== '') { + $byAccountNo[$accountNo] = $account; + } + $byCompany[(string) $account->company_name][] = $account; + } + + $used = []; + $inserted = 0; + $updated = 0; + $unchanged = 0; + $now = now(); + + foreach ($this->rows($path) as $row) { + $account = $byAccountNo[$row['account_no']] ?? null; + if ($account !== null && isset($used[(int) $account->id])) { + $account = null; + } + if ($account === null) { + foreach ($byCompany[$row['company_name']] ?? [] as $candidate) { + if (! isset($used[(int) $candidate->id])) { + $account = $candidate; + break; + } + } + } + + if ($account !== null) { + $used[(int) $account->id] = true; + $values = $this->updateValues($row, $account); + if ($values === []) { + $unchanged++; + continue; + } + $this->connection->table('fin_accounts')->where('id', $account->id)->update([ + ...$values, + 'updated_at' => $now, + ]); + $updated++; + continue; + } + + $accountShort = $row['account_short'] !== '' + ? $this->uniqueAccountShort($row['account_short']) + : $this->uniqueAccountShort($this->deriveAccountShort($row['company_name'])); + $this->ensureCompany($row['company_name'], $now); + $this->connection->table('fin_accounts')->insert([ + 'company_name' => $row['company_name'], + 'account_short' => $accountShort, + 'legal_representative' => $row['legal_representative'], + 'region' => null, + 'account_no' => $row['account_no'], + 'bank_name' => $row['bank_name'], + 'bound_alipay' => $row['bound_alipay'], + 'bank_city_branch' => null, + 'account_type' => '网商银行', + 'company_plan' => $row['company_plan'], + 'account_should_keep' => $row['account_should_keep'], + 'account_last4' => $row['account_last4'], + 'current_balance' => 0, + 'blocked_amount' => 0, + 'daily_available' => 0, + 'remark' => $row['remark'], + 'created_at' => $now, + 'updated_at' => $now, + ]); + $inserted++; + } + + if ($inserted + $updated + $unchanged === 0) { + throw new RuntimeException('表格中没有可导入的网商银行账户。'); + } + + return [ + 'total' => $inserted + $updated + $unchanged, + 'inserted' => $inserted, + 'updated' => $updated, + 'unchanged' => $unchanged, + 'preserved' => count(array_filter( + $current, + static fn (object $account): bool => ! isset($used[(int) $account->id]), + )), + ]; + }); + } + + /** + * @return Generator + */ + private function rows(string $path): Generator + { + $seenAccountNos = []; + $line = 1; + foreach ($this->reader->rows($path, self::REQUIRED_HEADERS) as $row) { + $line++; + if ($line - 1 > self::MAX_ROWS) { + throw new RuntimeException('单次最多导入 ' . self::MAX_ROWS . ' 条网商银行账户。'); + } + $value = static fn (string $header): mixed => $row[$header] ?? null; + $company = $this->text($value('公司名称')); + $accountNo = $this->normalizeAccountNo($value('网商账号')); + if ($company === '' && $accountNo === '') { + continue; + } + if ($company === '') { + throw new RuntimeException("第 {$line} 行缺少公司名称。"); + } + if ($accountNo === '') { + throw new RuntimeException("第 {$line} 行缺少网商账号。"); + } + if (isset($seenAccountNos[$accountNo])) { + throw new RuntimeException("第 {$line} 行网商账号与第 {$seenAccountNos[$accountNo]} 行重复。"); + } + $seenAccountNos[$accountNo] = $line; + + $accountType = '网商银行'; + yield [ + 'company_name' => $company, + 'account_short' => $this->text($value('账户简称')), + 'legal_representative' => $this->nullableText($value('法人')), + 'account_no' => $accountNo, + 'bank_name' => $this->nullableText($value('开户行')), + 'bound_alipay' => $this->nullableText($value('绑定支付宝')), + 'company_plan' => $this->companyPlan($value('账户状态')), + 'account_should_keep' => $this->shouldKeep($value('是否保留')), + 'account_last4' => FinanceLedgerAccountType::last4( + $accountType, + $accountNo, + $this->text($value('后4位')), + ) ?: null, + 'remark' => $this->nullableText($value('备注')), + ]; + } + } + + /** + * @param array $row + * @return array + */ + private function updateValues(array $row, object $account): array + { + $values = [ + 'company_name' => $row['company_name'], + 'account_no' => $row['account_no'], + 'account_type' => trim((string) ($account->account_type ?? '')) !== '' + ? (string) $account->account_type + : '网商银行', + 'legal_representative' => $row['legal_representative'] ?? $account->legal_representative, + 'bank_name' => $row['bank_name'] ?? $account->bank_name, + 'bound_alipay' => $row['bound_alipay'] ?? $account->bound_alipay, + 'company_plan' => $row['company_plan'] ?? $account->company_plan, + 'account_should_keep' => $row['account_should_keep'] ?? $account->account_should_keep, + 'account_last4' => $row['account_last4'] ?? $account->account_last4, + 'remark' => $row['remark'] ?? $account->remark, + ]; + + $changed = []; + foreach ($values as $field => $value) { + $current = $account->{$field} ?? null; + if ((string) ($value ?? '') !== (string) ($current ?? '')) { + $changed[$field] = $value; + } + } + + return $changed; + } + + private function ensureCompany(string $companyName, mixed $now): void + { + if ($this->connection->table('fin_companies')->where('name', $companyName)->exists()) { + return; + } + + $this->connection->table('fin_companies')->insert([ + 'name' => $companyName, + 'created_at' => $now, + 'updated_at' => $now, + ]); + } + + private function uniqueAccountShort(string $base): string + { + $base = mb_substr($this->text($base), 0, 90); + $candidate = $base; + $suffix = 1; + while ($this->connection->table('fin_accounts')->where('account_short', $candidate)->exists()) { + $suffix++; + $candidate = $base . $suffix; + } + + return $candidate; + } + + private function deriveAccountShort(string $companyName): string + { + $name = preg_replace( + '/^(?:常州市?|天津市?|上海市?|深圳市?|杭州市?|义乌市?|南召县?|佛山市?|苏州市?|南京市?|青岛市?|株洲市?|广州市?|永康市?|武进区)/u', + '', + $companyName, + ) ?? $companyName; + $name = preg_replace( + '/(?:有限责任公司|股份有限公司|有限公司|个人独资|商贸行|家具有限公司|家具店|公司)$/u', + '', + $name, + ) ?? $name; + $name = mb_substr(trim($name) !== '' ? trim($name) : $companyName, 0, 18); + + return $name . '网商'; + } + + private function companyPlan(mixed $value): ?string + { + $text = $this->text($value); + if ($text === '') { + return null; + } + if (str_contains($text, '注销') || str_contains($text, '不保留')) { + return '待注销'; + } + if (str_contains($text, '保留')) { + return '保留'; + } + + return $text; + } + + private function shouldKeep(mixed $value): ?string + { + $text = $this->text($value); + if ($text === '') { + return null; + } + if (in_array($text, ['注销', '不保留', '不需要', '否'], true)) { + return FinanceLedgerAccountShouldKeep::NO; + } + $normalized = FinanceLedgerAccountShouldKeep::normalize($text); + + return in_array($normalized, FinanceLedgerAccountShouldKeep::OPTIONS, true) + ? $normalized + : null; + } + + private function normalizeAccountNo(mixed $value): string + { + return preg_replace('/[\s-]+/u', '', $this->text($value)) ?? ''; + } + + private function text(mixed $value): string + { + if ($value === null) { + return ''; + } + $text = trim((string) $value); + if (in_array($text, ['#N/A', '#DIV/0!', '#VALUE!', '#REF!', 'None'], true)) { + return ''; + } + + return preg_replace('/\s+/u', ' ', $text) ?? $text; + } + + private function nullableText(mixed $value): ?string + { + $text = $this->text($value); + + return $text !== '' ? $text : null; + } +} diff --git a/modules/EcommerceFinance/View/FinanceLedgerPageData.php b/modules/EcommerceFinance/View/FinanceLedgerPageData.php index f8008671..5098025a 100644 --- a/modules/EcommerceFinance/View/FinanceLedgerPageData.php +++ b/modules/EcommerceFinance/View/FinanceLedgerPageData.php @@ -2114,11 +2114,13 @@ class FinanceLedgerPageData return [ 'company_name' => ['sort' => $a . '.company_name', 'type' => 'text'], 'account_short' => ['sort' => $a . '.account_short', 'type' => 'text'], + 'legal_representative' => ['sort' => $a . '.legal_representative', 'type' => 'text'], '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'], 'bank_name' => ['sort' => $a . '.bank_name', 'type' => 'text'], + 'bound_alipay' => ['sort' => $a . '.bound_alipay', 'type' => 'text'], 'bank_city_branch' => ['sort' => $a . '.bank_city_branch', 'type' => 'text'], 'company_plan' => ['sort' => $a . '.company_plan', 'type' => 'enum'], 'account_should_keep' => ['sort' => $keepExpression, 'type' => 'enum'], diff --git a/resources/views/business/pages/financial-ledger/ledger.blade.php b/resources/views/business/pages/financial-ledger/ledger.blade.php index 3d1b224c..19ca7c5c 100644 --- a/resources/views/business/pages/financial-ledger/ledger.blade.php +++ b/resources/views/business/pages/financial-ledger/ledger.blade.php @@ -279,6 +279,22 @@ ['date_range', '日期范围', 'text'], ], ]; + if ($standaloneAccountView === 'wangshang') { + array_splice($ledgerTableHeaders['accounts'], 2, 0, [ + ['legal_representative', '法人', 'text'], + ]); + $bankNameIndex = array_search( + 'bank_name', + array_column($ledgerTableHeaders['accounts'], 0), + true, + ); + if ($bankNameIndex !== false) { + array_splice($ledgerTableHeaders['accounts'], $bankNameIndex + 1, 0, [ + ['bound_alipay', '绑定支付宝', 'text'], + ]); + } + } + $accountTableColumnCount = count($ledgerTableHeaders['accounts']); @endphp @@ -524,6 +540,7 @@ .ledger-bank-category-note { margin:.75rem 0 0; color:var(--vip-content-muted, #595959); font-size:.68rem; line-height:1.55; } .ledger-flow-source-select { width:100%; min-width:10rem; height:2rem; padding:0 .45rem; border:1px solid var(--vip-border, #D8D2C7); border-radius:.35rem; background:#fff; color:var(--vip-content-ink, #171717); font:inherit; } .ledger-note { padding:.65rem .9rem; margin-bottom:.85rem; border:1px solid var(--vip-border, #D8D2C7); border-radius:.55rem; background:#fff; color:var(--vip-content-muted, #595959); font-size:.72rem; line-height:1.6; } + .ledger-note.is-warning { border-color:#E2C88E; background:#fff8eb; color:var(--vip-warning, #935F07); } .ledger-table-wrap { overflow:auto; border:1px solid var(--vip-border, #D8D2C7); border-radius:.65rem; background:#fff; } .ledger-table { width:100%; min-width:82rem; border-collapse:separate; border-spacing:0; font-size:.72rem; } .ledger-table th, .ledger-table td { padding:.52rem .6rem; border-right:1px solid #e8e3da; border-bottom:1px solid #e8e3da; vertical-align:top; white-space:nowrap; } @@ -775,6 +792,12 @@ {{-- 账户总表 --}}