feat(财务台账): 重做网商银行基本信息页并支持表格导入 (#1163)

This commit is contained in:
shanshi 2026-11-13 18:20:47 +08:00
parent c21a622a16
commit 4ca8685324
10 changed files with 654 additions and 6 deletions

View File

@ -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)],

View File

@ -0,0 +1,51 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
private function connection(): string
{
return (string) config('upstream_mirrors.connection', 'business_mirror');
}
public function up(): void
{
$schema = Schema::connection($this->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');
});
}
}
};

View File

@ -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,

View File

@ -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),

View File

@ -0,0 +1,341 @@
<?php
namespace Modules\EcommerceFinance\Services;
use App\Business\XlsxSheetReader;
use App\Utilities\FinanceLedgerAccountShouldKeep;
use App\Utilities\FinanceLedgerAccountType;
use Generator;
use Illuminate\Database\ConnectionInterface;
use Illuminate\Database\DatabaseManager;
use RuntimeException;
/**
* 合并导入网商银行基本信息。
*
* 文件中的账户会按网商账号、其次按公司名称匹配现有记录。匹配后只更新本表提供的
* 字段;表中没有的现有账户会保留,避免基础资料调整误删历史流水关联。
*/
class FinanceLedgerWangshangBasicImporter
{
private const MAX_ROWS = 1000;
private const REQUIRED_HEADERS = [
'公司名称',
'账户简称',
'法人',
'账户状态',
'网商账号',
'开户行',
'绑定支付宝',
'是否保留',
'后4位',
'备注',
];
private readonly ConnectionInterface $connection;
public function __construct(
DatabaseManager $databases,
private readonly XlsxSheetReader $reader,
) {
$this->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<int,array{
* company_name:string,account_short:string,legal_representative:?string,
* account_no:string,bank_name:?string,bound_alipay:?string,
* company_plan:?string,account_should_keep:?string,
* account_last4:?string,remark:?string
* }>
*/
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<string,mixed> $row
* @return array<string,mixed>
*/
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;
}
}

View File

@ -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'],

View File

@ -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
<meta name="csrf-token" content="{{ csrf_token() }}">
@ -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 @@
{{-- 账户总表 --}}
<section class="ledger-panel" id="ledger-panel-accounts" role="tabpanel" data-ledger-panel="accounts" @if ($activeTab !== 'accounts') hidden @endif>
@if ($standaloneAccountView === 'wangshang' && session('success'))
<div class="ledger-note" role="status">{{ session('success') }}</div>
@endif
@if ($standaloneAccountView === 'wangshang' && $errors->any())
<div class="ledger-note is-warning" role="alert">{{ $errors->first() }}</div>
@endif
<form class="ledger-toolbar" method="GET" action="{{ $accountFormUrl }}">
@if ($standaloneAccountView === null)
<input type="hidden" name="tab" value="accounts">
@ -800,7 +823,7 @@
新增公司/账户
</button>
@endif
<div class="ledger-column-picker" data-ledger-column-picker data-column-storage-key="caiwu.financial-ledger.accounts.preferences.v5.{{ auth()->id() }}.{{ company_id() }}">
<div class="ledger-column-picker" data-ledger-column-picker data-column-storage-key="caiwu.financial-ledger.accounts.preferences.v6.{{ auth()->id() }}.{{ company_id() }}.{{ $standaloneAccountView ?? 'all' }}">
<button class="ledger-btn" type="button" data-ledger-column-toggle aria-expanded="false"><span class="material-icons-outlined" aria-hidden="true">view_column</span>列设置</button>
<div class="ledger-column-panel" data-ledger-column-panel role="group" aria-label="选择{{ $standaloneAccountView === null ? '账户总表' : $standaloneAccountTitle }}展示列" hidden>
<p class="ledger-column-help">拖动字段可调整列顺序;拖动表头右侧边界可调整列宽,双击边界恢复默认。</p>
@ -808,9 +831,25 @@
<div class="ledger-column-actions"><span data-ledger-column-count></span><div class="ledger-column-action-group"><button type="button" data-ledger-column-all>显示全部</button><button type="button" data-ledger-column-reset>恢复默认</button></div></div>
</div>
</div>
<div class="ledger-actions"><button class="ledger-btn primary" type="submit"><span class="material-icons-outlined">search</span>查询</button><a class="ledger-btn" href="{{ $accountViewResetUrl }}"><span class="material-icons-outlined">refresh</span>重置</a><a class="ledger-btn" href="{{ route('business.financial-ledger.accounts.template') }}" download data-account-template-download><span class="material-icons-outlined">download</span>导出录入模板</a></div>
<div class="ledger-actions">
<button class="ledger-btn primary" type="submit"><span class="material-icons-outlined">search</span>查询</button>
<a class="ledger-btn" href="{{ $accountViewResetUrl }}"><span class="material-icons-outlined">refresh</span>重置</a>
@if ($standaloneAccountView === 'wangshang' && $canEditAccountStatus)
<button class="ledger-btn" type="button" data-wangshang-import-trigger>
<span class="material-icons-outlined" aria-hidden="true">upload_file</span>
导入网商基本信息
</button>
@endif
<a class="ledger-btn" href="{{ route('business.financial-ledger.accounts.template') }}" download data-account-template-download><span class="material-icons-outlined">download</span>导出录入模板</a>
</div>
<div class="ledger-download-status" data-account-template-download-status role="status" aria-live="polite" hidden>下载已开始,请查看浏览器下载目录。</div>
</form>
@if ($standaloneAccountView === 'wangshang' && $canEditAccountStatus)
<form method="POST" action="{{ route('business.financial-ledger.accounts.wangshang.import') }}" enctype="multipart/form-data" data-wangshang-import-form hidden>
@csrf
<input type="file" name="file" accept=".xlsx" data-wangshang-import-file required>
</form>
@endif
@if ($standaloneAccountView === null)
<nav class="ledger-subtabbar" aria-label="账户总表账户分类">
<a class="ledger-subtab {{ $accountView === 'bank' ? 'is-active' : '' }}" href="{{ $accountViewUrl('bank') }}" @if ($accountView === 'bank') aria-current="page" @endif>银行账户</a>
@ -879,6 +918,10 @@
<label for="ledger-company-account-short">账户简称 <span aria-hidden="true">*</span></label>
<input id="ledger-company-account-short" name="account_short" type="text" maxlength="100" required autocomplete="off" placeholder="如 上海甲公司农行">
</div>
<div class="ledger-company-field">
<label for="ledger-company-legal-representative">法人</label>
<input id="ledger-company-legal-representative" name="legal_representative" type="text" maxlength="100" autocomplete="off" placeholder="法人姓名">
</div>
<div class="ledger-company-field">
<label for="ledger-company-account-type">账户类型</label>
<select id="ledger-company-account-type" name="account_type">
@ -908,6 +951,10 @@
<label for="ledger-company-bank-name">开户行</label>
<input id="ledger-company-bank-name" name="bank_name" type="text" maxlength="200" autocomplete="off" placeholder="开户银行">
</div>
<div class="ledger-company-field">
<label for="ledger-company-bound-alipay">绑定支付宝</label>
<input id="ledger-company-bound-alipay" name="bound_alipay" type="text" maxlength="200" autocomplete="off" placeholder="绑定支付宝账号">
</div>
<div class="ledger-company-field">
<label for="ledger-company-bank-branch">开户城市/网点</label>
<input id="ledger-company-bank-branch" name="bank_city_branch" type="text" maxlength="100" autocomplete="off" placeholder="城市及网点">
@ -970,11 +1017,13 @@
<div class="ledger-company-form__section"><span>基础资料</span><span>账户简称修改后,期初、动账、流水和映射会同步更新。</span></div>
<div class="ledger-company-field"><label for="ledger-account-company-name">公司名称 <span aria-hidden="true">*</span></label><input id="ledger-account-company-name" name="company_name" type="text" list="ledger-company-options" maxlength="100" required></div>
<div class="ledger-company-field"><label for="ledger-account-short-edit">账户简称 <span aria-hidden="true">*</span></label><input id="ledger-account-short-edit" name="account_short" type="text" maxlength="100" required></div>
<div class="ledger-company-field"><label for="ledger-account-legal-representative-edit">法人</label><input id="ledger-account-legal-representative-edit" name="legal_representative" type="text" maxlength="100"></div>
<div class="ledger-company-field"><label for="ledger-account-type-edit">账户类型</label><select id="ledger-account-type-edit" name="account_type"><option value="">未填写</option>@foreach ($accountTypeOptions as $accountType)<option value="{{ $accountType }}">{{ $accountType }}</option>@endforeach</select></div>
<div class="ledger-company-field"><label for="ledger-account-status-edit">账户状态</label><select id="ledger-account-status-edit" name="status" required>@foreach ($accountStatusOptions as $statusCode => $statusLabel)<option value="{{ $statusCode }}">{{ $statusLabel }}</option>@endforeach</select></div>
<div class="ledger-company-field"><label for="ledger-account-region-edit">地区</label><input id="ledger-account-region-edit" name="region" type="text" maxlength="50"></div>
<div class="ledger-company-field ledger-edit-field--wide"><label for="ledger-account-no-edit">账号</label><input id="ledger-account-no-edit" name="account_no" type="text" maxlength="50"></div>
<div class="ledger-company-field ledger-edit-field--wide"><label for="ledger-account-bank-name-edit">开户行</label><input id="ledger-account-bank-name-edit" name="bank_name" type="text" maxlength="200"></div>
<div class="ledger-company-field ledger-edit-field--wide"><label for="ledger-account-bound-alipay-edit">绑定支付宝</label><input id="ledger-account-bound-alipay-edit" name="bound_alipay" type="text" maxlength="200"></div>
<div class="ledger-company-field ledger-edit-field--wide"><label for="ledger-account-bank-branch-edit">开户城市/网点</label><input id="ledger-account-bank-branch-edit" name="bank_city_branch" type="text" maxlength="100"></div>
<div class="ledger-company-field"><label for="ledger-account-plan-edit">公司规划</label><input id="ledger-account-plan-edit" name="company_plan" type="text" list="ledger-company-plan-options" maxlength="50"></div>
<div class="ledger-company-field"><label for="ledger-account-keep-edit">账户是否保留</label><select id="ledger-account-keep-edit" name="account_should_keep"><option value="">未填写</option>@foreach ($accountKeepOptions as $keepOption)<option value="{{ $keepOption }}">{{ $keepOption }}</option>@endforeach</select></div>
@ -1102,6 +1151,9 @@
<button class="ledger-row-edit" type="button" title="编辑账户 {{ $accountDisplayShort }}" aria-label="编辑账户 {{ $accountDisplayShort }}" data-ledger-account-edit data-edit-url="{{ route('business.financial-ledger.accounts.edit', ['id' => $row->id]) }}" data-save-url="{{ route('business.financial-ledger.accounts.update', ['id' => $row->id]) }}"><span class="material-icons-outlined" aria-hidden="true">edit</span></button>
@endif
</td>
@if ($standaloneAccountView === 'wangshang')
<td data-account-legal>{{ $row->legal_representative }}</td>
@endif
<td>{{ $row->bank_affiliation ?: '未填写' }}</td>
<td class="ledger-status-column">
@if ($canEditAccountStatus)
@ -1115,7 +1167,13 @@
<span class="ledger-status-badge ledger-account-readonly" data-status="{{ $row->account_status }}">{{ $accountStatusOptions[$row->account_status] ?? '正常' }}</span>
@endif
</td>
<td>{{ $row->region }}</td><td>{{ $row->account_no }}</td><td>{{ $row->bank_name }}</td><td>{{ $row->bank_city_branch }}</td>
<td>{{ $row->region }}</td>
<td>{{ $row->account_no }}</td>
<td>{{ $row->bank_name }}</td>
@if ($standaloneAccountView === 'wangshang')
<td data-account-bound-alipay>{{ $row->bound_alipay }}</td>
@endif
<td>{{ $row->bank_city_branch }}</td>
<td class="ledger-plan-column">
<span class="ledger-account-readonly" data-account-edit-display data-account-plan-display>{{ $row->company_plan ?: '未填写' }}</span>
@if ($canEditAccountStatus)
@ -1141,7 +1199,7 @@
<td class="center" data-account-last4>{{ $row->account_last4 }}</td><td class="center" title="{{ $moneyYuanTitle($row->single_limit) }}">{{ $moneyWan($row->single_limit) }}</td><td class="center" title="{{ $moneyYuanTitle($row->daily_limit) }}">{{ $moneyWan($row->daily_limit) }}</td><td class="center" title="{{ $moneyYuanTitle($row->monthly_limit) }}">{{ $moneyWan($row->monthly_limit) }}</td><td class="center" title="{{ $moneyYuanTitle($row->yearly_limit) }}">{{ $moneyWan($row->yearly_limit) }}</td><td class="num" data-account-current-balance="{{ (float) $row->current_balance }}" title="{{ $moneyYuanTitle($row->current_balance) }}">{{ $moneyWan($row->current_balance) }}</td><td class="num" data-account-available-amount title="{{ $moneyYuanTitle($row->daily_available) }}">{{ $moneyWan($row->daily_available) }}</td><td class="num" data-account-blocked-amount title="{{ $moneyYuanTitle($row->blocked_amount) }}">{{ $moneyWan($row->blocked_amount) }}</td><td class="num" data-account-daily-available-amount title="{{ $moneyYuanTitle($row->daily_available) }}">{{ $moneyWan($row->daily_available) }}</td><td>{{ $row->remark }}</td><td>{{ $row->owner }}</td>
</tr>
@empty
<tr><td colspan="21"><div class="ledger-empty">没有匹配的账户数据。</div></td></tr>
<tr><td colspan="{{ $accountTableColumnCount }}"><div class="ledger-empty">没有匹配的账户数据。</div></td></tr>
@endforelse
</tbody>
</table>
@ -3396,6 +3454,16 @@
status.textContent = '下载已开始,请查看浏览器下载目录:财务台账-账户总表录入模板.xlsx';
}
});
const wangshangImportTrigger = page.querySelector('[data-wangshang-import-trigger]');
const wangshangImportForm = page.querySelector('[data-wangshang-import-form]');
const wangshangImportFile = page.querySelector('[data-wangshang-import-file]');
wangshangImportTrigger?.addEventListener('click', () => wangshangImportFile?.click());
wangshangImportFile?.addEventListener('change', () => {
if (!wangshangImportFile.files?.length || !wangshangImportForm) return;
wangshangImportTrigger.disabled = true;
wangshangImportTrigger.textContent = '正在导入…';
wangshangImportForm.requestSubmit();
});
const availableTrigger = page.querySelector('[data-ledger-available-trigger]');
const availableDialog = page.querySelector('[data-ledger-available-dialog]');
@ -3610,8 +3678,9 @@
accountEditForm.action = button.dataset.saveUrl;
accountEditForm.reset();
[
'company_name', 'account_short', 'account_type', 'status', 'region',
'account_no', 'bank_name', 'bank_city_branch', 'company_plan',
'company_name', 'account_short', 'legal_representative', 'account_type',
'status', 'region', 'account_no', 'bank_name', 'bound_alipay',
'bank_city_branch', 'company_plan',
'account_should_keep', 'single_limit', 'daily_limit', 'monthly_limit',
'yearly_limit', 'clearing_days', 'raise_target', 'raise_status',
'raise_actions', 'done_actions', 'next_action', 'remark', 'owner',

View File

@ -1016,6 +1016,10 @@ Route::get('workspace/financial-operations/ledger/account-basic/{view}', [\App\H
->middleware('business.page:financial-operations,ledger')
->name('business.financial-ledger.account-basic');
Route::post('workspace/financial-ledger/accounts/wangshang/import', [\App\Http\Controllers\Business\FinanceLedger::class, 'importWangshangBasic'])
->middleware('business.page:financial-operations,ledger')
->name('business.financial-ledger.accounts.wangshang.import');
Route::post('workspace/financial-ledger/accounts/todo', [\App\Http\Controllers\Business\FinanceLedger::class, 'updateAccountTodo'])
->middleware('business.page:financial-operations,ledger')
->name('business.financial-ledger.accounts.todo');

View File

@ -93,11 +93,13 @@ class FinanceLedgerManualEditTest extends FeatureTestCase
$this->loginAs()->postJson($this->accountUpdateUrl(), [
'company_name' => '手动编辑测试公司',
'account_short' => '手动编辑测试账户新',
'legal_representative' => '测试法人',
'account_type' => '公户',
'status' => 'limited',
'region' => '苏州',
'account_no' => '6222000000008899',
'bank_name' => '测试银行',
'bound_alipay' => 'bound@example.com',
'bank_city_branch' => '苏州分行',
'company_plan' => '待注销',
'account_should_keep' => '否',
@ -119,8 +121,10 @@ class FinanceLedgerManualEditTest extends FeatureTestCase
$this->assertDatabaseHas('fin_accounts', [
'id' => $this->accountId,
'account_short' => '手动编辑测试账户新',
'legal_representative' => '测试法人',
'region' => '苏州',
'bank_name' => '测试银行',
'bound_alipay' => 'bound@example.com',
'owner' => '财务负责人',
], 'business_mirror');
foreach (['fin_account_opening', 'fin_daily_flows', 'fin_transactions', 'fin_shops'] as $table) {

View File

@ -0,0 +1,132 @@
<?php
namespace Tests\Feature\Business;
use App\Models\Auth\Permission;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use Tests\Feature\FeatureTestCase;
class FinanceLedgerWangshangBasicImportTest extends FeatureTestCase
{
protected function setUp(): void
{
parent::setUp();
Carbon::setTestNow('2026-11-13 18:00:00');
$this->user->attachPermission(
Permission::where('name', 'read-admin-panel')->firstOrFail(),
);
DB::connection('business_mirror')->table('fin_accounts')->insert([
[
'company_name' => '导入测试甲公司',
'account_short' => '测试甲网商',
'account_no' => '888800000001',
'bank_name' => '浙江网商银行',
'account_type' => '网商银行',
'account_should_keep' => '否',
'current_balance' => 100,
'blocked_amount' => 0,
'daily_available' => 100,
],
[
'company_name' => '导入测试乙公司',
'account_short' => '测试乙网商',
'account_no' => '888800000002',
'bank_name' => '浙江网商银行',
'account_type' => '网商银行',
'account_should_keep' => '否',
'current_balance' => 50,
'blocked_amount' => 0,
'daily_available' => 50,
],
[
'company_name' => '导入测试丙公司',
'account_short' => '测试丙网商',
'account_no' => '888800000003',
'bank_name' => '浙江网商银行',
'account_type' => '网商银行',
'account_should_keep' => '否',
'current_balance' => 25,
'blocked_amount' => 0,
'daily_available' => 25,
],
]);
}
public function test_wangshang_page_exposes_import_and_new_columns(): void
{
$this->loginAs()
->get(route('business.financial-ledger.account-basic', ['view' => 'wangshang']))
->assertOk()
->assertSee('导入网商基本信息')
->assertSee('法人')
->assertSee('绑定支付宝')
->assertSee('data-wangshang-import-form', false);
}
public function test_import_updates_matches_adds_new_rows_and_preserves_other_accounts(): void
{
$response = $this->loginAs()->post(
route('business.financial-ledger.accounts.wangshang.import'),
['file' => $this->workbook()],
);
$response->assertRedirect();
$response->assertSessionHas('success');
$connection = DB::connection('business_mirror');
$first = $connection->table('fin_accounts')->where('account_short', '测试甲网商')->first();
$this->assertSame('法人甲', $first->legal_representative);
$this->assertSame('jia@example.com', $first->bound_alipay);
$this->assertSame('保留', $first->company_plan);
$this->assertSame('是', $first->account_should_keep);
$this->assertSame('0001', $first->account_last4);
$third = $connection->table('fin_accounts')->where('account_short', '测试丙网商')->first();
$this->assertSame('888800009999', $third->account_no);
$this->assertSame('待注销', $third->company_plan);
$this->assertSame('否', $third->account_should_keep);
$this->assertDatabaseHas('fin_accounts', [
'account_short' => '测试乙网商',
], 'business_mirror');
$new = $connection->table('fin_accounts')->where('account_no', '888800000004')->first();
$this->assertNotNull($new);
$this->assertSame('导入测试丁公司', $new->company_name);
$this->assertSame('网商银行', $new->account_type);
$this->assertSame('导入测试丁网商', $new->account_short);
$this->assertSame('法人丁', $new->legal_representative);
$this->assertSame('ding@example.com', $new->bound_alipay);
$this->assertSame('0004', $new->account_last4);
}
private function workbook(): UploadedFile
{
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->fromArray([
['公司名称', '账户简称', '法人', '账户状态', '网商账号', '开户行', '绑定支付宝', '是否保留', '后4位', '备注'],
['导入测试甲公司', null, '法人甲', '保留', '888800000001', '浙江网商银行', 'jia@example.com', '保留', null, null],
['导入测试丙公司', null, '法人丙', '不保留', '888800009999', '浙江网商银行', 'bing@example.com', '注销', null, '账号更新'],
['导入测试丁公司', null, '法人丁', '不保留', '888800000004', '浙江网商银行', 'ding@example.com', '注销', null, null],
], null, 'A1');
$path = tempnam(sys_get_temp_dir(), 'wangshang-basic-');
(new Xlsx($spreadsheet))->save($path);
$spreadsheet->disconnectWorksheets();
return new UploadedFile(
$path,
'网商银行基本信息.xlsx',
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
null,
true,
);
}
}