feat: 展示账户月度期初并修复期初锁定 (#1694)

This commit is contained in:
shanshi 2026-12-03 14:47:07 +08:00
parent 7318eb6dea
commit a7bb3514b7
9 changed files with 150 additions and 8 deletions

View File

@ -117,7 +117,7 @@ class LockFinanceLedgerMonthlyOpening extends Command
->orderBy('balance_date')
->orderBy('balance_at')
->orderBy('id')
->get(['account_no', 'account_balance', 'available_balance']) as $row) {
->get(['account_no', 'balance_date', 'account_balance', 'available_balance']) as $row) {
$accountNo = $this->accountNo($row->account_no);
if ($accountNo !== '') {
$snapshotBefore[$accountNo] = $row;

View File

@ -253,6 +253,8 @@ CRON_TZ=Asia/Shanghai
30 2 * * * root /usr/bin/flock -w 1800 /run/caiwu-upstream-sync.lock /usr/bin/docker exec -u www-data caiwu_app php artisan finance-ledger:sync-rpt --days=7 --merge --sources=store,withdrawal --quiet >/dev/null 2>&1
5 * * * * root /usr/bin/flock -w 600 /run/caiwu-upstream-sync.lock /usr/bin/docker exec -u www-data caiwu_app php artisan finance-ledger:sync-rpt --days=2 --merge --sources=dingtalk,payable --quiet >/dev/null 2>&1
30 10 * * * root /usr/bin/flock -w 1800 /run/caiwu-upstream-sync.lock /usr/bin/docker exec -u www-data caiwu_app php artisan finance-ledger:sync-shop-data --balance-only --quiet >/dev/null 2>&1
# 每月 1 日按上月末最后一笔真实余额锁定月度期初,供账户总览和月度资金分析直接读取。
20 8 1 * * root /usr/bin/docker exec -u www-data caiwu_app php artisan finance-ledger:lock-monthly-opening --apply --quiet >/dev/null 2>&1
# 拼多多期初:账单导入后即时尝试,每日 11:30 再扫一次未就绪店铺;只在余额恒等式通过后写入。
30 11 * * * root /bin/sh /opt/caiwuxitong/run-finance-batch.sh pinduoduo-opening-balances:import --quiet >/dev/null 2>&1
# 财务流程明细每周完整校准一次,修正超过 48 小时重叠窗口才出现的上游改单或晚到付款。

View File

@ -257,6 +257,8 @@ CRON_TZ=Asia/Shanghai
30 2 * * * root /usr/bin/flock -w 1800 /run/caiwu-upstream-sync.lock /usr/bin/docker exec -u www-data caiwu_app php artisan finance-ledger:sync-rpt --days=7 --merge --sources=store,withdrawal --quiet >/dev/null 2>&1
5 * * * * root /usr/bin/flock -w 600 /run/caiwu-upstream-sync.lock /usr/bin/docker exec -u www-data caiwu_app php artisan finance-ledger:sync-rpt --days=2 --merge --sources=dingtalk,payable --quiet >/dev/null 2>&1
30 10 * * * root /usr/bin/flock -w 1800 /run/caiwu-upstream-sync.lock /usr/bin/docker exec -u www-data caiwu_app php artisan finance-ledger:sync-shop-data --balance-only --quiet >/dev/null 2>&1
# 每月 1 日按上月末最后一笔真实余额锁定月度期初,供账户总览和月度资金分析直接读取。
20 8 1 * * root /usr/bin/docker exec -u www-data caiwu_app php artisan finance-ledger:lock-monthly-opening --apply --quiet >/dev/null 2>&1
# 拼多多期初:账单导入后即时尝试,每日 11:30 再扫一次未就绪店铺;只在余额恒等式通过后写入。
30 11 * * * root /bin/sh /opt/caiwuxitong/run-finance-batch.sh pinduoduo-opening-balances:import --quiet >/dev/null 2>&1
# 财务流程明细每周完整校准一次,修正超过 48 小时重叠窗口才出现的上游改单或晚到付款。

View File

@ -125,6 +125,9 @@ final class NonStoreAccountMonitorPageData
$unretainedProgress = $this->progressSummary($unretained);
$fundOverview = [
'total_accounts' => $retainedSummary['count'],
'opening_balance' => $retainedSummary['opening_balance'],
'opening_accounts' => $retainedSummary['opening_accounts'],
'opening_missing_accounts' => $retainedSummary['opening_missing_accounts'],
'current_balance' => $retainedSummary['current_balance'],
'available_balance' => $retainedSummary['available_balance'],
'blocked_balance' => $retainedSummary['blocked_balance'],
@ -888,8 +891,18 @@ final class NonStoreAccountMonitorPageData
private function summary(Collection $rows): array
{
$openingRows = $rows->filter(
static fn (array $row): bool => array_key_exists('opening_balance', $row)
&& $row['opening_balance'] !== null,
);
return [
'count' => $rows->count(),
'opening_balance' => $openingRows->isEmpty()
? null
: round((float) $openingRows->sum('opening_balance'), 2),
'opening_accounts' => $openingRows->count(),
'opening_missing_accounts' => max(0, $rows->count() - $openingRows->count()),
'current_balance' => round((float) $rows->sum('current_balance'), 2),
'available_balance' => round((float) $rows->sum('available_balance'), 2),
'blocked_balance' => round((float) $rows->sum('blocked_balance'), 2),

View File

@ -3,6 +3,9 @@
? '—'
: number_format((float) $value, 2);
$moneyWan = static fn ($value): string => number_format((float) $value / 10000, 2, '.', ',');
$moneyWanOptional = static fn ($value): string => $value === null || $value === ''
? '—'
: $moneyWan($value);
$currentUrl = $currentUrl ?? route('business.pages.show', [
'section' => 'financial-operations',
'page' => 'non-store-accounts',
@ -185,7 +188,7 @@
.nsm-fund-overview__grid {
display: grid;
grid-template-columns: repeat(4, minmax(0, 1fr));
grid-template-columns: repeat(5, minmax(0, 1fr));
}
.nsm-fund-kpi {
@ -224,6 +227,14 @@
color: var(--nsm-warning);
}
.nsm-table-note {
display: block;
margin-top: .25rem;
color: var(--nsm-muted);
font-size: .625rem;
font-weight: 400;
}
.nsm-distribution-summary {
display: grid;
grid-template-columns: repeat(4, minmax(0, 1fr));
@ -1775,6 +1786,10 @@
.nsm-fund-kpi:nth-child(4) {
border-top: 1px solid #eeeae2;
}
.nsm-fund-kpi:nth-child(5) {
border-left: 0;
border-top: 1px solid #eeeae2;
}
.nsm-page-head {
align-items: flex-start;
flex-direction: column;
@ -1803,7 +1818,8 @@
}
.nsm-fund-kpi + .nsm-fund-kpi,
.nsm-fund-kpi:nth-child(3),
.nsm-fund-kpi:nth-child(4) {
.nsm-fund-kpi:nth-child(4),
.nsm-fund-kpi:nth-child(5) {
border-top: 1px solid #eeeae2;
border-left: 0;
}
@ -1823,6 +1839,9 @@
data-open-retained-on-load="{{ request()->boolean('retained') ? '1' : '0' }}"
data-retained-period-from="{{ $period['from'] }}"
data-retained-period-to="{{ $period['to'] }}"
data-retained-period-opening="{{ $fundOverview['opening_balance'] === null ? '' : (float) $fundOverview['opening_balance'] }}"
data-retained-period-opening-accounts="{{ (int) $fundOverview['opening_accounts'] }}"
data-retained-period-opening-missing="{{ (int) $fundOverview['opening_missing_accounts'] }}"
data-retained-period-total="{{ (float) $fundOverview['current_balance'] }}"
data-retained-period-available="{{ (float) $fundOverview['available_balance'] }}"
data-retained-period-blocked="{{ (float) $fundOverview['blocked_balance'] }}"
@ -1948,6 +1967,14 @@
<span>{{ number_format((int) $fundOverview['total_accounts']) }} 个账户 · 金额单位:万元</span>
</header>
<div class="nsm-fund-overview__grid">
<div class="nsm-fund-kpi @if(($fundOverview['opening_missing_accounts'] ?? 0) > 0) nsm-fund-kpi--warning @endif">
<span>期初余额</span>
<strong>{{ $moneyWanOptional($fundOverview['opening_balance']) }}</strong>
<small>
{{ number_format((int) $fundOverview['opening_accounts']) }} /
{{ number_format((int) $fundOverview['total_accounts']) }} 户已锁定月度期初
</small>
</div>
<div class="nsm-fund-kpi">
<span>期末总余额</span>
<strong>{{ $moneyWan($fundOverview['current_balance']) }}</strong>
@ -1984,6 +2011,7 @@
<tr>
<th scope="col">账户平台</th>
<th scope="col" class="num">账户数</th>
<th scope="col" class="num">期初余额</th>
<th scope="col" class="num">期末余额</th>
<th scope="col" class="num">可用金额</th>
<th scope="col" class="num">不可用金额</th>
@ -2022,6 +2050,14 @@
</a>
@endif
</td>
<td class="num">
{{ $moneyWanOptional($distribution['opening_balance']) }}
@if ($distribution['opening_balance'] !== null) 万 @endif
<small class="nsm-table-note">
{{ number_format((int) $distribution['opening_accounts']) }} /
{{ number_format((int) $distribution['count']) }} 户
</small>
</td>
<td class="num">{{ $moneyWan($distribution['current_balance']) }} 万</td>
<td class="num">{{ $moneyWan($distribution['available_balance']) }} 万</td>
<td class="num">{{ $moneyWan($distribution['blocked_balance']) }} 万</td>
@ -2046,6 +2082,14 @@
data-account-archive-open
>{{ number_format((int) $fundOverview['total_accounts']) }}</a>
</td>
<td class="num">
{{ $moneyWanOptional($fundOverview['opening_balance']) }}
@if ($fundOverview['opening_balance'] !== null) 万 @endif
<small class="nsm-table-note">
{{ number_format((int) $fundOverview['opening_accounts']) }} /
{{ number_format((int) $fundOverview['total_accounts']) }} 户
</small>
</td>
<td class="num">{{ $moneyWan($fundOverview['current_balance']) }} 万</td>
<td class="num">{{ $moneyWan($fundOverview['available_balance']) }} 万</td>
<td class="num">{{ $moneyWan($fundOverview['blocked_balance']) }} 万</td>
@ -2422,7 +2466,7 @@
</div>
<div class="nsm-bank-detail__actions">
@include('business.pages.financial-operations.partials.detail-column-settings', [
'storageKey' => 'caiwu.non-store-accounts.detail.bank-inline.columns.v2',
'storageKey' => 'caiwu.non-store-accounts.detail.bank-inline.columns.v3',
'columns' => [
'公共信息' => [
['key' => 'company_name', 'label' => '公司名称'],
@ -2436,6 +2480,7 @@
['key' => 'account_no', 'label' => '登录账号/账号'],
],
'资金信息' => [
['key' => 'opening_balance', 'label' => '期初余额'],
['key' => 'current_balance', 'label' => '期末余额'],
['key' => 'available_balance', 'label' => '可用金额'],
['key' => 'blocked_balance', 'label' => '不可用金额'],
@ -2463,6 +2508,7 @@
<th scope="col" data-detail-column="connection_label">连接方式</th>
<th scope="col" data-detail-column="account_status_label">账户状态</th>
<th scope="col" data-detail-column="account_no">登录账号/账号</th>
<th scope="col" class="num" data-detail-column="opening_balance">期初余额</th>
<th scope="col" class="num" data-detail-column="current_balance">期末余额</th>
<th scope="col" class="num" data-detail-column="available_balance">可用金额</th>
<th scope="col" class="num" data-detail-column="blocked_balance">不可用金额</th>
@ -2484,6 +2530,7 @@
<td data-detail-column="connection_label">{{ $account['is_direct'] ? '银企直连' : '非银企直连' }}</td>
<td data-detail-column="account_status_label">{{ $account['account_status_label'] ?: '—' }}</td>
<td data-detail-column="account_no">{{ in_array($account['account_no'], ['', '/'], true) ? '—' : $account['account_no'] }}</td>
<td class="num" data-detail-column="opening_balance">{{ $money($account['opening_balance'] ?? null) }}</td>
<td class="num" data-detail-column="current_balance">
@php
$inlineBillUrl = $bankBillInlineUrl($account);

View File

@ -19,6 +19,7 @@
['key' => 'account_no', 'label' => '登录账号/账号'],
],
'资金信息' => [
['key' => 'opening_balance', 'label' => '期初余额'],
['key' => 'current_balance', 'label' => '当前余额'],
['key' => 'available_balance', 'label' => '可用金额'],
['key' => 'blocked_balance', 'label' => '不可用金额'],
@ -39,7 +40,7 @@
<span>{{ $description }}</span>
</div>
@include('business.pages.financial-operations.partials.detail-column-settings', [
'storageKey' => 'caiwu.non-store-accounts.detail.' . $detailId . '.columns.v2',
'storageKey' => 'caiwu.non-store-accounts.detail.' . $detailId . '.columns.v3',
'columns' => $detailColumns,
])
</header>
@ -53,6 +54,7 @@
<th scope="col" data-detail-column="platform_label">所属银行/平台</th>
<th scope="col" data-detail-column="account_status_label">账户状态</th>
<th scope="col" data-detail-column="account_no">登录账号/账号</th>
<th scope="col" class="num" data-detail-column="opening_balance">期初余额</th>
<th scope="col" class="num" data-detail-column="current_balance">当前余额</th>
<th scope="col" class="num" data-detail-column="available_balance">可用金额</th>
<th scope="col" class="num" data-detail-column="blocked_balance">不可用金额</th>
@ -94,6 +96,7 @@
<td data-detail-column="platform_label">{{ $account['platform_label'] ?: '其他账户' }}</td>
<td data-detail-column="account_status_label">{{ $account['account_status_label'] ?: '—' }}</td>
<td data-detail-column="account_no">{{ in_array($account['account_no'], ['', '/'], true) ? '—' : $account['account_no'] }}</td>
<td class="num" data-detail-column="opening_balance">{{ $money($account['opening_balance'] ?? null) }}</td>
<td class="num" data-detail-column="current_balance">{{ number_format((float) $account['current_balance'], 2) }}</td>
<td class="num" data-detail-column="available_balance">{{ number_format((float) $account['available_balance'], 2) }}</td>
<td class="num" data-detail-column="blocked_balance">{{ number_format((float) $account['blocked_balance'], 2) }}</td>
@ -101,7 +104,7 @@
</tr>
@empty
<tr>
<td colspan="9">{{ $emptyMessage }}</td>
<td colspan="10">{{ $emptyMessage }}</td>
</tr>
@endforelse
</tbody>

View File

@ -161,13 +161,15 @@ class NonStoreAccountMonitorTest extends FeatureTestCase
$this->assertGreaterThanOrEqual(8, substr_count($html, 'data-detail-column-picker-toggle'));
$this->assertStringContainsString('data-detail-column-table', $html);
$this->assertStringContainsString(
'caiwu.non-store-accounts.detail.bank-inline.columns.v2',
'caiwu.non-store-accounts.detail.bank-inline.columns.v3',
$html,
);
$this->assertStringContainsString(
'caiwu.non-store-accounts.detail.nsm-alipay-drilldown.columns.v2',
'caiwu.non-store-accounts.detail.nsm-alipay-drilldown.columns.v3',
$html,
);
$this->assertStringContainsString('data-detail-column="opening_balance"', $html);
$this->assertStringContainsString('value="opening_balance"', $html);
$this->assertStringContainsString('data-detail-column="current_balance"', $html);
$this->assertStringContainsString('data-detail-column="balance_updated_at">余额更新时间</th>', $html);
$this->assertStringContainsString('const initializeDetailColumnPickers', $html);
@ -211,6 +213,45 @@ class NonStoreAccountMonitorTest extends FeatureTestCase
$this->assertStringContainsString('<span>保留账户数</span>', $html);
}
public function test_monthly_opening_is_visible_in_summary_and_bank_details(): void
{
$connection = DB::connection('business_mirror');
$connection->table('fin_account_opening')->insert([
[
'account_short' => '保留直连工行',
'opening_date' => '2026-11-01',
'opening_balance' => 90000,
],
[
'account_short' => '保留手动支付宝',
'opening_date' => '2026-11-01',
'opening_balance' => 50000,
],
]);
$response = $this->loginAs()->get($this->pageUrl([
'retained' => 1,
'period_from' => '2026-11-01',
'period_to' => '2026-11-30',
]))->assertOk();
$page = app(NonStoreAccountMonitorPageData::class)->page();
$fundOverview = $page['fundOverview'];
$bankAccount = collect($fundOverview['bank_accounts'])
->firstWhere('account_short', '保留直连工行');
$this->assertSame(140000.0, $fundOverview['opening_balance']);
$this->assertSame(2, $fundOverview['opening_accounts']);
$this->assertSame(0, $fundOverview['opening_missing_accounts']);
$this->assertNotNull($bankAccount);
$this->assertSame(90000.0, $bankAccount['opening_balance']);
$response->assertSee('data-retained-period-opening="140000"', false);
$response->assertSee('data-retained-period-opening-accounts="2"', false);
$response->assertSee('data-retained-period-opening-missing="0"', false);
$response->assertSee('data-detail-column="opening_balance">期初余额</th>', false);
$response->assertSee('>90,000.00</td>', false);
}
public function test_retained_dialog_amounts_follow_the_selected_period(): void
{
DB::connection('business_mirror')->table('fin_flow_summary_account_sources')

View File

@ -155,6 +155,12 @@ class BusinessMirrorScheduleTest extends TestCase
$contents,
$script,
);
$this->assertStringContainsString(
'20 8 1 * * root /usr/bin/docker exec -u www-data caiwu_app php artisan finance-ledger:lock-monthly-opening --apply --quiet >/dev/null 2>&1',
$contents,
$script,
);
$this->assertSame(1, substr_count($contents, 'finance-ledger:lock-monthly-opening'), $script);
$this->assertStringContainsString(
'30 11 * * * root /bin/sh /opt/caiwuxitong/run-finance-batch.sh pinduoduo-opening-balances:import --quiet',
$contents,

View File

@ -172,6 +172,34 @@ class LockFinanceLedgerMonthlyOpeningTest extends TestCase
], 'business_mirror');
}
public function test_it_uses_a_bank_direct_balance_snapshot_as_the_basis(): void
{
$db = DB::connection('business_mirror');
$db->table('fin_accounts')->insert([
'account_short' => '快照余额账户',
'account_no' => '6200000000000005',
]);
$db->table('fin_bank_direct_balance_snapshots')->insert([
'account_no' => '6200000000000005',
'balance_date' => '2026-10-31',
'balance_at' => '2026-10-31 18:00:00',
'account_balance' => 765.43,
'available_balance' => 700,
]);
$this->artisan('finance-ledger:lock-monthly-opening', [
'date' => '2026-11-01',
'--apply' => true,
])->assertSuccessful();
$this->assertDatabaseHas('fin_account_opening', [
'account_short' => '快照余额账户',
'opening_date' => '2026-11-01',
'opening_balance' => 765.43,
'remark' => '系统锁定:2026-10-31 最后一笔实际余额',
], 'business_mirror');
}
public function test_it_does_not_carry_forward_past_unpriced_transactions(): void
{
$db = DB::connection('business_mirror');