财务台账:增加账户是否保留列 #420

Merged
shanshi merged 1 commits from feature/shanshi-account-should-keep-column into main 2026-10-21 11:59:04 +08:00
9 changed files with 338 additions and 38 deletions

View File

@ -0,0 +1,45 @@
<?php
namespace App\Utilities;
class FinanceLedgerAccountShouldKeep
{
public const YES = '是';
public const NO = '否';
public const OPTIONS = [self::YES, self::NO];
public static function normalize(?string $value): string
{
$value = trim((string) $value);
if ($value === '') {
return '';
}
$compact = mb_strtoupper((string) preg_replace('/\s+/u', '', $value));
if (in_array($compact, ['是', '保留', '需要保留', '需要', 'Y', 'YES', 'TRUE', '1'], true)) {
return self::YES;
}
if (in_array($compact, ['否', '不保留', '不需要保留', '不需要', 'N', 'NO', 'FALSE', '0'], true)) {
return self::NO;
}
return $value;
}
public static function display(?string $explicitValue, ?string $companyPlan): string
{
$normalized = self::normalize($explicitValue);
if ($normalized !== '') {
return $normalized;
}
$plan = trim((string) $companyPlan);
if ($plan === '') {
return '';
}
return str_contains($plan, '保留') ? self::YES : self::NO;
}
}

View File

@ -0,0 +1,35 @@
<?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') || $schema->hasColumn('fin_accounts', 'account_should_keep')) {
return;
}
$schema->table('fin_accounts', function (Blueprint $table): void {
$table->string('account_should_keep', 20)->nullable()->after('company_plan');
});
}
public function down(): void
{
$schema = Schema::connection($this->connection());
if ($schema->hasTable('fin_accounts') && $schema->hasColumn('fin_accounts', 'account_should_keep')) {
$schema->table('fin_accounts', function (Blueprint $table): void {
$table->dropColumn('account_should_keep');
});
}
}
};

View File

@ -3,6 +3,7 @@
namespace Modules\EcommerceFinance\Services;
use App\Utilities\FinanceLedgerAccountStatus;
use App\Utilities\FinanceLedgerAccountShouldKeep;
use App\Utilities\FinanceLedgerAccountType;
use Illuminate\Support\Facades\DB;
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
@ -33,6 +34,7 @@ class FinanceLedgerAccountTemplate
'开户行',
'开户城市/网点',
'公司规划',
'账户是否保留',
'后4位',
'单笔限额',
'日限额',
@ -98,29 +100,35 @@ class FinanceLedgerAccountTemplate
$sheet->getStyle("A1:{$lastColumn}{$lastRow}")->getBorders()->getAllBorders()
->setBorderStyle(Border::BORDER_THIN)->getColor()->setARGB('FFD8D2C7');
$sheet->getStyle("A2:N{$lastRow}")->getNumberFormat()->setFormatCode(NumberFormat::FORMAT_TEXT);
$sheet->getStyle("O2:Q{$lastRow}")->getNumberFormat()->setFormatCode('#,##0.00');
$sheet->getStyle("R2:R{$lastRow}")->getNumberFormat()->setFormatCode('0.00');
$sheet->getStyle("S2:Y{$lastRow}")->getNumberFormat()->setFormatCode(NumberFormat::FORMAT_TEXT);
$sheet->getStyle("A2:O{$lastRow}")->getNumberFormat()->setFormatCode(NumberFormat::FORMAT_TEXT);
$sheet->getStyle("P2:R{$lastRow}")->getNumberFormat()->setFormatCode('#,##0.00');
$sheet->getStyle("S2:S{$lastRow}")->getNumberFormat()->setFormatCode('0.00');
$sheet->getStyle("T2:Z{$lastRow}")->getNumberFormat()->setFormatCode(NumberFormat::FORMAT_TEXT);
$options = $spreadsheet->createSheet();
$options->setTitle('选项');
$options->setCellValue('A1', '账户状态');
$options->setCellValue('B1', '账户类型');
$options->setCellValue('C1', '账户是否保留');
$statuses = array_values(FinanceLedgerAccountStatus::OPTIONS);
$types = $this->accountTypes();
$keepOptions = FinanceLedgerAccountShouldKeep::OPTIONS;
foreach ($statuses as $index => $status) {
$options->setCellValue('A' . ($index + 2), $status);
}
foreach ($types as $index => $type) {
$options->setCellValue('B' . ($index + 2), $type);
}
foreach ($keepOptions as $index => $option) {
$options->setCellValue('C' . ($index + 2), $option);
}
$options->setSheetState(Worksheet::SHEETSTATE_HIDDEN);
$this->applyListValidation($sheet, 'D', '\'选项\'!$A$2:$A$' . (count($statuses) + 1), '请选择账户状态');
if ($types !== []) {
$this->applyListValidation($sheet, 'C', '\'选项\'!$B$2:$B$' . (count($types) + 1), '请选择账户类型');
}
$this->applyListValidation($sheet, 'J', '\'选项\'!$C$2:$C$' . (count($keepOptions) + 1), '请选择账户是否保留');
$notes = $spreadsheet->createSheet();
$notes->setTitle('填写说明');
@ -130,6 +138,7 @@ class FinanceLedgerAccountTemplate
['必填', '公司名称、账户简称。'],
['账户状态', implode(' / ', $statuses)],
['账户类型', $types !== [] ? implode(' / ', $types) : '留空或按实际填写'],
['账户是否保留', implode(' / ', $keepOptions)],
['金额', '可用金额、不可用金额、每日可出按数字填写;限额列保留原有组合写法。'],
['提交前', '请核对账户数、余额合计和期初合计,再导入系统。'],
], null, 'A1');
@ -139,8 +148,8 @@ class FinanceLedgerAccountTemplate
$notes->getStyle('A1:B1')->getFill()
->setFillType(Fill::FILL_SOLID)
->getStartColor()->setARGB('FF856632');
$notes->getStyle('A1:B7')->getAlignment()->setVertical(Alignment::VERTICAL_TOP)->setWrapText(true);
$notes->getStyle('A1:B7')->getBorders()->getAllBorders()
$notes->getStyle('A1:B8')->getAlignment()->setVertical(Alignment::VERTICAL_TOP)->setWrapText(true);
$notes->getStyle('A1:B8')->getBorders()->getAllBorders()
->setBorderStyle(Border::BORDER_THIN)->getColor()->setARGB('FFD8D2C7');
$notes->getRowDimension(1)->setRowHeight(26);
@ -189,9 +198,9 @@ class FinanceLedgerAccountTemplate
{
return match ($index) {
0, 6 => 30,
1, 5, 7, 23 => 22,
2, 3, 4, 8, 17 => 13,
14, 15, 16 => 16,
1, 5, 7, 24 => 22,
2, 3, 4, 8, 9, 18 => 13,
15, 16, 17 => 16,
default => 18,
};
}

View File

@ -2,7 +2,9 @@
namespace Modules\EcommerceFinance\Services;
use App\Utilities\FinanceLedgerAccountShouldKeep;
use Illuminate\Support\Facades\DB;
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use RuntimeException;
@ -90,45 +92,63 @@ class FinanceLedgerBaseImporter
];
}
private function accounts(?Worksheet $sheet): array
protected function accounts(?Worksheet $sheet): array
{
if (! $sheet) {
throw new RuntimeException('缺少「账户总表」工作表。');
}
$lastColumn = Coordinate::columnIndexFromString($sheet->getHighestDataColumn());
$headers = $sheet->rangeToArray('A1:' . Coordinate::stringFromColumnIndex($lastColumn) . '1')[0] ?? [];
$headerIndexes = [];
foreach ($headers as $index => $header) {
$key = (string) preg_replace('/\s+/u', '', trim((string) $header));
if ($key !== '') {
$headerIndexes[$key] = $index;
}
}
$rows = [];
foreach ($this->rows($sheet, 2, 23) as $row) {
$company = $this->text($row[0] ?? null);
$short = $this->text($row[1] ?? null);
foreach ($this->rows($sheet, 2, $lastColumn) as $row) {
$value = static function (string $header, int $legacyIndex) use ($row, $headerIndexes): mixed {
return $row[$headerIndexes[$header] ?? $legacyIndex] ?? null;
};
$company = $this->text($value('公司名称', 0));
$short = $this->text($value('账户简称', 1));
if ($company === '' || $short === '') {
continue;
}
$keepIndex = $headerIndexes['账户是否保留'] ?? null;
$rows[] = [
'company_name' => $company,
'account_short' => $short,
'region' => null,
'account_no' => $this->text($row[2] ?? null),
'bank_name' => $this->text($row[3] ?? null),
'bank_city_branch' => $this->text($row[4] ?? null),
'account_type' => $this->text($row[5] ?? null),
'company_plan' => $this->text($row[6] ?? null),
'account_last4' => $this->text($row[7] ?? null),
'single_limit' => $this->text($row[8] ?? null),
'daily_limit' => $this->text($row[9] ?? null),
'monthly_limit' => $this->text($row[10] ?? null),
'yearly_limit' => $this->text($row[11] ?? null),
'current_balance' => $this->number($row[12] ?? null),
'blocked_amount' => $this->number($row[13] ?? null),
'daily_available' => $this->number($row[14] ?? null),
'clearing_days' => $this->nullableNumber($row[15] ?? null),
'raise_target' => $this->text($row[16] ?? null),
'raise_status' => $this->text($row[17] ?? null),
'raise_actions' => $this->text($row[18] ?? null),
'done_actions' => $this->text($row[19] ?? null),
'next_action' => $this->text($row[20] ?? null),
'remark' => $this->text($row[21] ?? null),
'owner' => $this->text($row[22] ?? null),
'account_no' => $this->text($value('账号', 2)),
'bank_name' => $this->text($value('开户行', 3)),
'bank_city_branch' => $this->text($value('开户城市/网点', 4)),
'account_type' => $this->text($value('账户类型', 5)),
'company_plan' => $this->text($value('公司规划', 6)),
'account_should_keep' => FinanceLedgerAccountShouldKeep::normalize(
$keepIndex === null ? null : $this->text($row[$keepIndex] ?? null),
),
'account_last4' => $this->text($value('后4位', 7)),
'single_limit' => $this->text($value('单笔限额', 8)),
'daily_limit' => $this->text($value('日限额', 9)),
'monthly_limit' => $this->text($value('月限额', 10)),
'yearly_limit' => $this->text($value('年限额', 11)),
'current_balance' => $this->number($value('可用金额', 12)),
'blocked_amount' => $this->number($value('不可用金额', 13)),
'daily_available' => $this->number($value('每日可出', 14)),
'clearing_days' => $this->nullableNumber($value('预计出清天数', 15)),
'raise_target' => $this->text($value('提额目标', 16)),
'raise_status' => $this->text($value('提额状态', 17)),
'raise_actions' => $this->text($value('提额动作', 18)),
'done_actions' => $this->text($value('已做动作', 19)),
'next_action' => $this->text($value('下一步', 20)),
'remark' => $this->text($value('备注', 21)),
'owner' => $this->text($value('责任人', 22)),
];
}

View File

@ -3,6 +3,7 @@
namespace Modules\EcommerceFinance\View;
use App\Utilities\FinanceLedgerAccountStatus;
use App\Utilities\FinanceLedgerAccountShouldKeep;
use App\Utilities\FinanceLedgerAccountType;
use App\Utilities\FinanceLedgerAccess;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
@ -173,6 +174,10 @@ class FinanceLedgerPageData
if ($typeOverrides->has($row->account_short)) {
$row->account_type = (string) ($typeOverrides->get($row->account_short) ?? '');
}
$row->account_should_keep = FinanceLedgerAccountShouldKeep::display(
$row->account_should_keep ?? null,
$row->company_plan ?? null,
);
return $row;
});

View File

@ -88,6 +88,7 @@
.ledger-accounts-table thead th.num { text-align:center; }
.ledger-accounts-table { min-width:max-content; }
.ledger-accounts-table .is-column-hidden { display:none !important; }
.ledger-keep-column { width:6.5rem; min-width:6.5rem; text-align:center; }
.ledger-table td.wrap { white-space:normal; min-width:14rem; max-width:22rem; line-height:1.5; }
.ledger-table .muted { color:var(--vip-content-muted, #595959); }
.ledger-table .danger { color:var(--vip-danger, #B80000); }
@ -196,7 +197,7 @@
</div>
<div class="ledger-table-wrap ledger-accounts-table-wrap">
<table class="ledger-table ledger-accounts-table">
<thead><tr><th>公司名称</th><th>账户简称</th><th>账户类型</th><th class="ledger-status-column">账户状态</th><th>地区</th><th>账号</th><th>开户行</th><th>开户城市/网点</th><th>公司规划</th><th>后4位</th><th>单笔限额</th><th>日限额</th><th>月限额</th><th>年限额</th><th class="num">可用金额</th><th class="num">不可用金额</th><th class="num">每日可出</th><th class="num">预计出清天数</th><th>提额目标</th><th>提额状态</th><th>提额动作</th><th>已做动作</th><th>下一步</th><th>备注</th><th>责任人</th></tr></thead>
<thead><tr><th>公司名称</th><th>账户简称</th><th>账户类型</th><th class="ledger-status-column">账户状态</th><th>地区</th><th>账号</th><th>开户行</th><th>开户城市/网点</th><th>公司规划</th><th class="ledger-keep-column">账户是否保留</th><th>后4位</th><th>单笔限额</th><th>日限额</th><th>月限额</th><th>年限额</th><th class="num">可用金额</th><th class="num">不可用金额</th><th class="num">每日可出</th><th class="num">预计出清天数</th><th>提额目标</th><th>提额状态</th><th>提额动作</th><th>已做动作</th><th>下一步</th><th>备注</th><th>责任人</th></tr></thead>
<tbody>
@forelse ($ledgerAccounts as $row)
<tr>
@ -226,10 +227,10 @@
@endif
</td>
<td>{{ $row->region }}</td><td>{{ $row->account_no }}</td><td>{{ $row->bank_name }}</td><td>{{ $row->bank_city_branch }}</td>
<td>{{ $row->company_plan }}</td><td>{{ $row->account_last4 }}</td><td>{{ $row->single_limit }}</td><td>{{ $row->daily_limit }}</td><td>{{ $row->monthly_limit }}</td><td>{{ $row->yearly_limit }}</td><td class="num">{{ $money($row->current_balance) }}</td><td class="num">{{ $money($row->blocked_amount) }}</td><td class="num">{{ $money($row->daily_available) }}</td><td class="num">{{ $row->clearing_days !== null ? number_format((float) $row->clearing_days, 2) : '' }}</td><td>{{ $row->raise_target }}</td><td>{{ $row->raise_status }}</td><td>{{ $row->raise_actions }}</td><td>{{ $row->done_actions }}</td><td>{{ $row->next_action }}</td><td>{{ $row->remark }}</td><td>{{ $row->owner }}</td>
<td>{{ $row->company_plan }}</td><td class="ledger-keep-column" data-account-short="{{ $row->account_short }}" data-account-should-keep="{{ $row->account_should_keep }}">{{ $row->account_should_keep }}</td><td>{{ $row->account_last4 }}</td><td>{{ $row->single_limit }}</td><td>{{ $row->daily_limit }}</td><td>{{ $row->monthly_limit }}</td><td>{{ $row->yearly_limit }}</td><td class="num">{{ $money($row->current_balance) }}</td><td class="num">{{ $money($row->blocked_amount) }}</td><td class="num">{{ $money($row->daily_available) }}</td><td class="num">{{ $row->clearing_days !== null ? number_format((float) $row->clearing_days, 2) : '' }}</td><td>{{ $row->raise_target }}</td><td>{{ $row->raise_status }}</td><td>{{ $row->raise_actions }}</td><td>{{ $row->done_actions }}</td><td>{{ $row->next_action }}</td><td>{{ $row->remark }}</td><td>{{ $row->owner }}</td>
</tr>
@empty
<tr><td colspan="25"><div class="ledger-empty">没有匹配的账户数据。</div></td></tr>
<tr><td colspan="26"><div class="ledger-empty">没有匹配的账户数据。</div></td></tr>
@endforelse
</tbody>
</table>

View File

@ -21,6 +21,7 @@ class FinanceLedgerAccountStatusTest extends FeatureTestCase
'account_short' => '测试账户',
'account_no' => '6222000000000001',
'bank_name' => '测试银行',
'company_plan' => '保留',
]);
}
@ -68,6 +69,8 @@ class FinanceLedgerAccountStatusTest extends FeatureTestCase
->assertSee('data-account-template-download', false)
->assertSee('data-account-template-download-status', false)
->assertSee('<th>账户简称</th><th>账户类型</th><th class="ledger-status-column">账户状态</th>', false)
->assertSee('<th>公司规划</th><th class="ledger-keep-column">账户是否保留</th><th>后4位</th>', false)
->assertSee('data-account-short="测试账户" data-account-should-keep="是"', false)
->assertSee('网商银行')
->assertSee('ledger-accounts-table-wrap', false)
->assertSee('ledger-pagination__controls', false)
@ -97,6 +100,20 @@ class FinanceLedgerAccountStatusTest extends FeatureTestCase
}
}
public function test_explicit_account_keep_value_takes_precedence_over_company_plan(): void
{
DB::connection('business_mirror')->table('fin_accounts')->insert([
'company_name' => '保留判断测试公司',
'account_short' => '显式不保留账户',
'company_plan' => '保留',
'account_should_keep' => '否',
]);
$this->loginAs()->get($this->pageUrl())
->assertOk()
->assertSee('data-account-short="显式不保留账户" data-account-should-keep="否"', false);
}
public function test_account_pagination_lists_page_urls_with_current_filter(): void
{
$rows = [];

View File

@ -53,6 +53,7 @@ class FinanceLedgerAccountTemplateTest extends FeatureTestCase
'开户行',
'开户城市/网点',
'公司规划',
'账户是否保留',
'后4位',
'单笔限额',
'日限额',
@ -69,16 +70,24 @@ class FinanceLedgerAccountTemplateTest extends FeatureTestCase
'下一步',
'备注',
'责任人',
], $sheet->rangeToArray('A1:Y1')[0]);
], $sheet->rangeToArray('A1:Z1')[0]);
$this->assertNull($sheet->getCell('A2')->getValue());
$this->assertSame(DataValidation::TYPE_LIST, $sheet->getCell('C2')->getDataValidation()->getType());
$this->assertSame(DataValidation::TYPE_LIST, $sheet->getCell('D2')->getDataValidation()->getType());
$this->assertSame(DataValidation::TYPE_LIST, $sheet->getCell('J2')->getDataValidation()->getType());
$this->assertSame(Worksheet::SHEETSTATE_HIDDEN, $spreadsheet->getSheetByName('选项')->getSheetState());
$typeOptions = array_map(
static fn (array $row): string => (string) ($row[0] ?? ''),
$spreadsheet->getSheetByName('选项')->rangeToArray('B2:B20'),
);
$this->assertContains('网商银行', $typeOptions);
$this->assertSame(
['是', '否'],
array_map(
static fn (array $row): string => (string) ($row[0] ?? ''),
$spreadsheet->getSheetByName('选项')->rangeToArray('C2:C3'),
),
);
$this->assertNotNull($spreadsheet->getSheetByName('填写说明'));
$spreadsheet->disconnectWorksheets();
} finally {

View File

@ -0,0 +1,159 @@
<?php
namespace Tests\Unit\EcommerceFinance;
use Modules\EcommerceFinance\Services\FinanceLedgerBaseImporter;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use Tests\TestCase;
class FinanceLedgerBaseImporterTest extends TestCase
{
public function test_account_headers_import_new_keep_column_without_shifting_other_fields(): void
{
$sheet = $this->sheet([
'公司名称',
'账户简称',
'账号',
'开户行',
'开户城市/网点',
'账户类型',
'公司规划',
'账户是否保留',
'后4位',
'单笔限额',
'日限额',
'月限额',
'年限额',
'可用金额',
'不可用金额',
'每日可出',
'预计出清天数',
'提额目标',
'提额状态',
'提额动作',
'已做动作',
'下一步',
'备注',
'责任人',
], [
'新格式公司',
'新格式账户',
'62220001',
'测试银行',
'上海 / 测试支行',
'公户',
'保留',
'是',
'0001',
'10000',
'20000',
'30000',
'40000',
123.45,
6.78,
116.67,
2.5,
'目标',
'进行中',
'动作',
'已做',
'下一步',
'备注',
'责任人',
]);
$rows = $this->importer()->parseAccounts($sheet);
$this->assertCount(1, $rows);
$this->assertSame('是', $rows[0]['account_should_keep']);
$this->assertSame('0001', $rows[0]['account_last4']);
$this->assertSame('10000', $rows[0]['single_limit']);
$this->assertEqualsWithDelta(123.45, $rows[0]['current_balance'], 0.001);
$this->assertSame('责任人', $rows[0]['owner']);
}
public function test_account_headers_keep_legacy_layout_compatible(): void
{
$sheet = $this->sheet([
'公司名称',
'账户简称',
'账号',
'开户行',
'开户城市/网点',
'账户类型',
'公司规划',
'后4位',
'单笔限额',
'日限额',
'月限额',
'年限额',
'可用金额',
'不可用金额',
'每日可出',
'预计出清天数',
'提额目标',
'提额状态',
'提额动作',
'已做动作',
'下一步',
'备注',
'责任人',
], [
'旧格式公司',
'旧格式账户',
'62220002',
'测试银行',
'杭州 / 测试支行',
'个人卡',
'待注销',
'0002',
'1000',
'2000',
'3000',
'4000',
12.34,
0,
12.34,
null,
'',
'',
'',
'',
'',
'',
'旧格式责任人',
]);
$rows = $this->importer()->parseAccounts($sheet);
$this->assertCount(1, $rows);
$this->assertSame('', $rows[0]['account_should_keep']);
$this->assertSame('0002', $rows[0]['account_last4']);
$this->assertSame('1000', $rows[0]['single_limit']);
$this->assertEqualsWithDelta(12.34, $rows[0]['current_balance'], 0.001);
$this->assertSame('旧格式责任人', $rows[0]['owner']);
}
private function importer(): FinanceLedgerBaseImporterForTest
{
return new FinanceLedgerBaseImporterForTest();
}
private function sheet(array $headers, array $values): Worksheet
{
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->fromArray([$headers, $values], null, 'A1');
return $sheet;
}
}
final class FinanceLedgerBaseImporterForTest extends FinanceLedgerBaseImporter
{
public function parseAccounts(Worksheet $sheet): array
{
return $this->accounts($sheet);
}
}