feat: add monthly closing store summary #1044

Merged
qiuxiang merged 1 commits from feature/qiuxiang-monthly-closing-store-summary into main 2026-11-10 15:30:11 +08:00
2 changed files with 288 additions and 7 deletions

View File

@ -132,11 +132,30 @@
<h3 id="monthly-closing-title">月度期末余额汇总</h3>
<span class="monthly-closing-badge">新增</span>
</div>
<p>2026 年 7-10 月各平台期末余额 · 金额单位:万元</p>
<p>
2026 年 7-10 月{{ monthlyClosingView === 'store' ? '店铺' : '各平台' }}期末余额
· 金额单位:万元
</p>
</div>
<div v-if="!monthlyClosingLoading && !monthlyClosingError" class="monthly-closing-head-actions">
<span class="monthly-closing-count">{{ monthlyClosingCountText }}</span>
<div class="monthly-closing-view-switch" role="tablist" aria-label="月度期末余额汇总方式">
<button
type="button"
role="tab"
:aria-selected="monthlyClosingView === 'platform' ? 'true' : 'false'"
:class="{ 'is-active': monthlyClosingView === 'platform' }"
@click="selectMonthlyClosingView('platform')"
>平台账户</button>
<button
type="button"
role="tab"
:aria-selected="monthlyClosingView === 'store' ? 'true' : 'false'"
:class="{ 'is-active': monthlyClosingView === 'store' }"
@click="selectMonthlyClosingView('store')"
>店铺汇总</button>
</div>
</div>
<span v-if="!monthlyClosingLoading && !monthlyClosingError" class="monthly-closing-count">
{{ monthlyClosing.summary.available_platform_count }} / {{ monthlyClosing.summary.platform_count }} 个平台
</span>
</header>
<div v-if="monthlyClosingLoading" class="monthly-closing-loading" role="status">
@ -147,7 +166,15 @@
<span>{{ monthlyClosingError }}</span>
<button type="button" @click="fetchMonthlyClosing">重新加载</button>
</div>
<div v-else class="monthly-closing-scroll">
<div v-else-if="monthlyClosingView === 'store' && monthlyClosingStoreLoading" class="monthly-closing-loading" role="status">
<span v-for="index in 4" :key="index"></span>
<p>正在汇总 7-10 月店铺期末余额…</p>
</div>
<div v-else-if="monthlyClosingView === 'store' && monthlyClosingStoreError" class="monthly-closing-error" role="alert">
<span>{{ monthlyClosingStoreError }}</span>
<button type="button" @click="fetchMonthlyClosingStores">重新加载</button>
</div>
<div v-else-if="monthlyClosingView === 'platform'" class="monthly-closing-scroll">
<table class="monthly-closing-table">
<thead>
<tr>
@ -179,9 +206,46 @@
</tbody>
</table>
</div>
<div v-else class="monthly-closing-scroll is-store">
<table class="monthly-closing-table monthly-closing-store-table">
<thead>
<tr>
<th scope="col">平台账户</th>
<th scope="col">店铺名称</th>
<th v-for="month in monthlyClosing.months" :key="month.key" scope="col" class="is-number">
{{ month.label }}
</th>
<th scope="col" class="is-number monthly-closing-compare-head">环比</th>
</tr>
</thead>
<tbody>
<tr
v-for="row in monthlyStoreRows"
:key="row.key"
:class="{ 'is-total': row.isTotal, 'is-unavailable': !row.available }"
>
<th scope="row">{{ row.platform }}</th>
<td class="monthly-store-name">{{ row.store_name }}</td>
<td v-for="month in monthlyClosing.months" :key="month.key" class="is-number">
{{ monthlyBalanceWan(row, month.key) }}
</td>
<td class="is-number monthly-closing-compare">
<span :class="changeClass(row.change.amount)">{{ signedMoneyWan(row.change.amount) }}</span>
<small v-if="row.change.rate !== null" :class="changeClass(row.change.amount)">
{{ signedPercent(row.change.rate) }}
</small>
</td>
</tr>
</tbody>
</table>
</div>
<footer class="monthly-closing-foot">
<span>环比口径:2026年10月期末余额 − 2026年9月期末余额,金额单位为万元,百分比以上月余额为基数。</span>
<span v-if="!monthlyClosingLoading && !monthlyClosingError">8 个平台沿用各自账单页的期末余额口径。</span>
<span v-if="!monthlyClosingLoading && !monthlyClosingError">
{{ monthlyClosingView === 'store'
? '店铺期末余额 = 期初余额 + 收入 - 支出;缺少期初时显示“—”。'
: '8 个平台沿用各自账单页的期末余额口径。' }}
</span>
</footer>
</section>
@ -342,6 +406,19 @@ const dateConfig = {
onReady: (_, value, picker) => picker.calendarContainer.classList.add('overview-calendar'),
};
const closingChange = (balances, currentMonth, previousMonth) => {
const current = balances[currentMonth];
const previous = balances[previousMonth];
const amount = Number.isFinite(current) && Number.isFinite(previous)
? Math.round((current - previous) * 100) / 100
: null;
const rate = amount !== null && Math.abs(previous) >= 0.005
? Math.round((amount / Math.abs(previous)) * 10000) / 100
: (amount === 0 ? 0 : null);
return { amount, rate };
};
export default {
name: 'StoreReceivingBalanceOverview',
components: { flatPicker },
@ -375,6 +452,12 @@ export default {
monthlyClosingLoading: true,
monthlyClosingError: '',
monthlyClosingRequestSerial: 0,
monthlyClosingView: 'platform',
monthlyClosingStoreLoading: false,
monthlyClosingStoreError: '',
monthlyClosingStoreLoaded: false,
monthlyClosingStoreRequestSerial: 0,
monthlyStoreMonthDetails: [],
monthlyClosing: {
months: [],
summary: {
@ -605,6 +688,81 @@ export default {
...(Array.isArray(this.monthlyClosing.platforms) ? this.monthlyClosing.platforms : []),
];
},
monthlyClosingCountText() {
if (this.monthlyClosingView === 'platform') {
return `${this.monthlyClosing.summary.available_platform_count} / ${this.monthlyClosing.summary.platform_count} 个平台`;
}
if (this.monthlyClosingStoreLoading) return '正在读取店铺…';
if (this.monthlyClosingStoreError) return '店铺数据暂不可用';
const total = this.monthlyStoreRows.filter(row => !row.isTotal).length;
const incomplete = this.monthlyStoreRows.filter(row => !row.isTotal && !row.available).length;
return incomplete > 0
? `${total} 个店铺 · ${incomplete} 个期初待补`
: `${total} 个店铺`;
},
monthlyStoreRows() {
const months = Array.isArray(this.monthlyClosing.months)
? this.monthlyClosing.months.map(month => month.key)
: [];
const platformOrder = new Map(
this.monthlyClosing.platforms.map((platform, index) => [platform.slug, index]),
);
const grouped = new Map();
this.monthlyStoreMonthDetails.forEach((result) => {
(Array.isArray(result.details) ? result.details : []).forEach((detail) => {
const key = [detail.platform_slug, detail.store_name].join('|');
const row = grouped.get(key) || {
key,
platform_slug: detail.platform_slug,
platform: detail.platform,
store_name: detail.store_name,
balances: Object.fromEntries(months.map(month => [month, null])),
};
row.balances[result.month] = detail.amount === null || detail.amount === undefined
? null
: (Number.isFinite(Number(detail.amount))
? Math.round(Number(detail.amount) * 100) / 100
: null);
grouped.set(key, row);
});
});
const currentMonth = this.monthlyClosing.comparison.current;
const previousMonth = this.monthlyClosing.comparison.previous;
const rows = Array.from(grouped.values()).map(row => ({
...row,
available: months.some(month => row.balances[month] !== null),
change: closingChange(row.balances, currentMonth, previousMonth),
}));
const latestMonth = months[months.length - 1];
rows.sort((left, right) => {
const leftBalance = left.balances[latestMonth];
const rightBalance = right.balances[latestMonth];
return (
(leftBalance === null ? 1 : 0) - (rightBalance === null ? 1 : 0)
|| (leftBalance === null ? 0 : rightBalance)
- (leftBalance === null ? 0 : leftBalance)
|| (platformOrder.get(left.platform_slug) ?? 99)
- (platformOrder.get(right.platform_slug) ?? 99)
|| left.store_name.localeCompare(right.store_name, 'zh-CN')
);
});
if (!this.monthlyClosing.total) return rows;
return [{
...this.monthlyClosing.total,
key: 'monthly-store-total',
platform: '合计',
store_name: `合计 · ${rows.length} 个店铺`,
isTotal: true,
available: true,
}, ...rows];
},
},
mounted() {
this.fetchOverview();
@ -652,6 +810,9 @@ export default {
const requestId = ++this.monthlyClosingRequestSerial;
this.monthlyClosingLoading = true;
this.monthlyClosingError = '';
++this.monthlyClosingStoreRequestSerial;
this.monthlyClosingStoreLoaded = false;
this.monthlyStoreMonthDetails = [];
try {
const response = await window.axios.get(`${this.apiBase}/monthly-closing`);
@ -668,6 +829,9 @@ export default {
total: body.total || null,
platforms: Array.isArray(body.platforms) ? body.platforms : [],
};
if (this.monthlyClosingView === 'store') {
this.fetchMonthlyClosingStores();
}
} catch (error) {
if (requestId !== this.monthlyClosingRequestSerial) return;
this.monthlyClosingError = (error.response && error.response.data && error.response.data.message)
@ -678,6 +842,54 @@ export default {
}
}
},
selectMonthlyClosingView(view) {
if (!['platform', 'store'].includes(view) || this.monthlyClosingView === view) return;
this.monthlyClosingView = view;
if (view === 'store' && !this.monthlyClosingStoreLoaded && !this.monthlyClosingStoreLoading) {
this.fetchMonthlyClosingStores();
}
},
async fetchMonthlyClosingStores() {
const months = Array.isArray(this.monthlyClosing.months) ? this.monthlyClosing.months : [];
if (!months.length) return;
const requestId = ++this.monthlyClosingStoreRequestSerial;
this.monthlyClosingStoreLoading = true;
this.monthlyClosingStoreError = '';
try {
const responses = await Promise.all(months.map((month) => {
const [year, monthNumber] = month.key.split('-').map(Number);
const lastDay = new Date(year, monthNumber, 0).getDate();
return window.axios.get(`${this.apiBase}/balance-store-detail`, {
params: {
start: `${month.key}-01`,
end: `${month.key}-${pad(lastDay)}`,
},
});
}));
if (requestId !== this.monthlyClosingStoreRequestSerial) return;
this.monthlyStoreMonthDetails = responses.map((response, index) => ({
month: months[index].key,
details: Array.isArray(response.data && response.data.details)
? response.data.details
: [],
}));
this.monthlyClosingStoreLoaded = true;
} catch (error) {
if (requestId !== this.monthlyClosingStoreRequestSerial) return;
this.monthlyStoreMonthDetails = [];
this.monthlyClosingStoreLoaded = false;
this.monthlyClosingStoreError = (error.response && error.response.data && error.response.data.message)
|| '店铺月度期末余额暂时无法读取,请稍后重试。';
} finally {
if (requestId === this.monthlyClosingStoreRequestSerial) {
this.monthlyClosingStoreLoading = false;
}
}
},
changeDate(field, value) {
const date = value || '';
if (this.dateRange[field] === date) return;
@ -1255,10 +1467,55 @@ export default {
font-size: .6875rem;
font-weight: 600;
}
.monthly-closing-head-actions {
display: flex;
flex: 0 0 auto;
align-items: center;
gap: .75rem;
}
.monthly-closing-view-switch {
display: inline-flex;
padding: 2px;
border: 1px solid #d8cdbc;
border-radius: .5rem;
background: #f7f1e5;
}
.monthly-closing-view-switch button {
height: 1.9rem;
padding: 0 .75rem;
border: 0;
border-radius: .38rem;
background: transparent;
color: var(--overview-muted);
font: 600 .6875rem/1.2 inherit;
white-space: nowrap;
cursor: pointer;
}
.monthly-closing-view-switch button:hover {
color: var(--overview-gold);
}
.monthly-closing-view-switch button.is-active {
background: #fffdf8;
color: var(--overview-gold);
box-shadow: 0 1px 2px rgba(63, 50, 29, .10);
}
.monthly-closing-view-switch button:focus-visible {
box-shadow: 0 0 0 3px rgba(138, 104, 47, .18);
outline: none;
}
.monthly-closing-scroll {
overflow-x: auto;
overscroll-behavior-inline: contain;
}
.monthly-closing-scroll.is-store {
max-height: 30rem;
overflow: auto;
}
.monthly-closing-scroll.is-store .monthly-closing-table thead th {
position: sticky;
z-index: 2;
top: 0;
}
.monthly-closing-table {
width: 100%;
min-width: 44rem;
@ -1267,6 +1524,9 @@ export default {
font-size: .75rem;
font-variant-numeric: tabular-nums;
}
.monthly-closing-store-table {
min-width: 60rem;
}
.monthly-closing-table th,
.monthly-closing-table td {
padding: .65rem .8rem;
@ -1316,6 +1576,9 @@ export default {
.monthly-closing-table tbody tr.is-unavailable td {
color: #937b57;
}
.monthly-closing-store-table .monthly-store-name {
min-width: 15rem;
}
.monthly-closing-compare span,
.monthly-closing-compare small {
display: block;
@ -1345,6 +1608,10 @@ export default {
font-size: .625rem;
}
.monthly-closing-loading,
.monthly-closing-error {
min-height: 6rem;
}
.monthly-closing-loading,
.monthly-closing-error {
padding: 1rem;
}
@ -1678,6 +1945,11 @@ export default {
align-items: flex-start;
flex-direction: column;
}
.monthly-closing-head-actions {
width: 100%;
flex-wrap: wrap;
justify-content: space-between;
}
.monthly-closing-foot {
align-items: flex-start;
flex-direction: column;

View File

@ -74,7 +74,8 @@ class StoreReceivingBalanceOverviewTest extends TestCase
$this->assertStringContainsString('月度期末余额汇总', $source);
$this->assertStringContainsString('平台账户', $source);
$this->assertStringContainsString('monthly-closing-table', $source);
$this->assertStringContainsString('2026 年 7-10 月各平台期末余额', $source);
$this->assertStringContainsString('2026 年 7-10 月', $source);
$this->assertStringContainsString("'各平台'", $source);
$this->assertStringContainsString('金额单位:万元', $source);
$this->assertStringContainsString('monthlyBalanceWan', $source);
$this->assertStringContainsString('signedMoneyWan', $source);
@ -83,5 +84,13 @@ class StoreReceivingBalanceOverviewTest extends TestCase
$this->assertStringContainsString('环比口径:2026年10月期末余额 − 2026年9月期末余额', $source);
$this->assertStringContainsString('monthlyClosingRows', $source);
$this->assertStringContainsString('fetchMonthlyClosing', $source);
$this->assertStringContainsString('店铺汇总', $source);
$this->assertStringContainsString('monthly-closing-view-switch', $source);
$this->assertStringContainsString('selectMonthlyClosingView', $source);
$this->assertStringContainsString('fetchMonthlyClosingStores', $source);
$this->assertStringContainsString('monthlyStoreRows', $source);
$this->assertStringContainsString('monthly-closing-store-table', $source);
$this->assertStringContainsString('店铺期末余额 = 期初余额 + 收入 - 支出', $source);
$this->assertStringContainsString('缺少期初时显示“—”', $source);
}
}