From 4dfed91c92d499030b7fcf3962fd267dc049b601 Mon Sep 17 00:00:00 2001 From: qiuxiang <394405259@qq.com> Date: Fri, 27 Nov 2026 14:33:32 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E8=B5=84=E9=87=91=E7=AE=A1=E7=90=86):=20?= =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=9C=88=E5=BA=A6=E8=B5=84=E9=87=91=E4=BD=99?= =?UTF-8?q?=E9=A2=9D=E7=9B=91=E6=8E=A7=E5=B9=B6=E7=BB=9F=E4=B8=80=E6=94=AF?= =?UTF-8?q?=E4=BB=98=E5=AE=9D=E8=B4=A6=E6=88=B7=E5=8F=A3=E5=BE=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 合并月度资金余额监控页、店铺每日余额本页小计标识及全部/店铺支付宝账户名称区分。 --- app/Http/Controllers/Business/Pages.php | 1 + config/business_navigation.php | 3 +- .../StoreReceivingBalanceOverviewService.php | 2 +- .../View/FundBalanceMonitorPageData.php | 438 ++++++++++++ .../View/NonStoreAccountMonitorPageData.php | 5 +- .../StoreReceivingBalanceOverview.vue | 23 +- .../components/business/TaobaoAlipayBills.vue | 2 +- .../fund-management/balance-monitor.blade.php | 630 ++++++++++++++++++ .../Business/FinanceLedgerNavigationTest.php | 1 + .../Business/FundBalanceMonitorPageTest.php | 251 +++++++ ...oreReceivingBalanceOverviewServiceTest.php | 8 +- .../StoreReceivingBalanceOverviewTest.php | 9 +- 12 files changed, 1358 insertions(+), 15 deletions(-) create mode 100644 modules/EcommerceFinance/View/FundBalanceMonitorPageData.php create mode 100644 resources/views/business/pages/fund-management/balance-monitor.blade.php create mode 100644 tests/Feature/Business/FundBalanceMonitorPageTest.php diff --git a/app/Http/Controllers/Business/Pages.php b/app/Http/Controllers/Business/Pages.php index 121524ad..94c5fb01 100644 --- a/app/Http/Controllers/Business/Pages.php +++ b/app/Http/Controllers/Business/Pages.php @@ -5676,6 +5676,7 @@ class Pages extends Controller 'shipping-data' => $this->contentForShippingData(), 'return-pickup-management' => $this->contentForReturnPickupManagement(), 'withdrawal-management' => $this->contentForWithdrawalManagement(), + 'fund-balance-monitor' => app(\Modules\EcommerceFinance\View\FundBalanceMonitorPageData::class)->page(), 'systemization-todos' => app(\App\Http\Controllers\Business\SystemizationTodos::class)->page(), 'log-statistics' => app(\Modules\EcommerceFinance\View\DingWorklogStatisticsPageData::class)->page(request()), 'order-profit-list' => app(\Modules\EcommerceFinance\View\OrderProfitPageData::class)->page(), diff --git a/config/business_navigation.php b/config/business_navigation.php index 91d939c7..d081af75 100644 --- a/config/business_navigation.php +++ b/config/business_navigation.php @@ -563,6 +563,7 @@ $navigation = [ 'route_page' => 'fund-ledger', 'permission_key' => 'financial-operations/fund-ledger', ], + ['slug' => 'fund-balance-monitor', 'title' => '资金余额监控'], [ 'slug' => 'store-receiving-accounts', 'title' => '店铺收款账户', @@ -591,7 +592,7 @@ $navigation = [ 'title' => '店铺收款账户账单', 'tabs' => [ ['slug' => 'balance-overview', 'title' => '账户余额总览'], - ['slug' => 'taobao-alipay', 'title' => '淘系支付宝账户'], + ['slug' => 'taobao-alipay', 'title' => '全部支付宝账户'], ['slug' => 'taobao-wechat', 'title' => '淘系微信账户'], ['slug' => 'pinduoduo', 'title' => '拼多多账户'], ['slug' => 'jd-wallet', 'title' => '京东网银钱包账户'], diff --git a/modules/EcommerceFinance/Services/StoreReceivingBalanceOverviewService.php b/modules/EcommerceFinance/Services/StoreReceivingBalanceOverviewService.php index fae3ce76..0538b8c1 100644 --- a/modules/EcommerceFinance/Services/StoreReceivingBalanceOverviewService.php +++ b/modules/EcommerceFinance/Services/StoreReceivingBalanceOverviewService.php @@ -48,7 +48,7 @@ class StoreReceivingBalanceOverviewService /** @var array */ public const PLATFORMS = [ - ['slug' => 'taobao-alipay', 'name' => '淘系支付宝账户'], + ['slug' => 'taobao-alipay', 'name' => '店铺支付宝账户'], ['slug' => 'taobao-wechat', 'name' => '淘系微信账户'], ['slug' => 'pinduoduo', 'name' => '拼多多账户'], ['slug' => 'jd-wallet', 'name' => '京东网银钱包账户'], diff --git a/modules/EcommerceFinance/View/FundBalanceMonitorPageData.php b/modules/EcommerceFinance/View/FundBalanceMonitorPageData.php new file mode 100644 index 00000000..abe0d22b --- /dev/null +++ b/modules/EcommerceFinance/View/FundBalanceMonitorPageData.php @@ -0,0 +1,438 @@ + */ + public function page(): array + { + $month = $this->selectedMonth(); + $periodStart = $month['start']; + $periodEnd = $month['end']; + $accountError = null; + + try { + $accountData = $this->accounts->page([ + 'from' => $periodStart, + 'to' => $periodEnd, + ]); + } catch (Throwable $exception) { + report($exception); + $accountData = []; + $accountError = '非店铺账户余额暂时无法读取。'; + } + + $storeError = null; + try { + $storeDaily = $this->cachedStoreDaily($periodStart, $periodEnd); + } catch (Throwable $exception) { + report($exception); + $storeDaily = []; + $storeError = '店铺每日余额暂时无法读取。'; + } + + return $this->build( + $month, + $accountData, + $storeDaily, + $accountError, + $storeError, + ); + } + + /** + * 月度资金页默认查看最近已结束月份;本次进入页面即为 2026 年 10 月。 + * + * @return array{ + * value:string,label:string,start:string,end:string, + * options:array + * } + */ + private function selectedMonth(): array + { + $now = CarbonImmutable::now('Asia/Shanghai')->startOfDay(); + $default = $now->subMonthNoOverflow()->startOfMonth(); + $requested = trim((string) request('month', '')); + $selected = $default; + if (preg_match('/^\d{4}-\d{2}$/', $requested) === 1) { + try { + $candidate = CarbonImmutable::createFromFormat( + '!Y-m', + $requested, + 'Asia/Shanghai', + ); + if ($candidate instanceof CarbonImmutable + && $candidate->format('Y-m') === $requested) { + $selected = $candidate; + } + } catch (Throwable) { + // 非法月份回落到最近已结束月份。 + } + } + + $periodStart = $selected->startOfMonth(); + $periodEnd = $selected->endOfMonth(); + if ($selected->isSameMonth($now)) { + $periodEnd = $now; + } + + $options = []; + for ($offset = 0; $offset < 12; $offset++) { + $month = $default->subMonthsNoOverflow($offset); + $options[] = [ + 'value' => $month->format('Y-m'), + 'label' => $month->format('Y年n月'), + ]; + } + + return [ + 'value' => $selected->format('Y-m'), + 'label' => $selected->format('Y年n月'), + 'start' => $periodStart->format('Y-m-d'), + 'end' => $periodEnd->format('Y-m-d'), + 'options' => $options, + ]; + } + + /** @return array */ + private function cachedStoreDaily(string $start, string $end): array + { + $cacheKey = implode(':', [ + 'ecommerce-finance', + 'fund-balance-monitor-month', + 'v2', + (string) company_id(), + $start, + $end, + $this->pinduoduoOpeningBalances->cacheVersion(), + ]); + + return Cache::remember( + $cacheKey, + now()->addMinutes(self::STORE_CACHE_MINUTES), + fn (): array => $this->stores->dailyBalances($start, $end), + ); + } + + /** + * @param array $month + * @param array $accountData + * @param array $storeDaily + * @return array + */ + private function build( + array $month, + array $accountData, + array $storeDaily, + ?string $accountError, + ?string $storeError, + ): array { + $accountReady = $accountError === null && $accountData !== []; + $storeReady = $storeError === null && $storeDaily !== []; + $fundOverview = (array) ($accountData['fundOverview'] ?? []); + $distribution = collect((array) ($fundOverview['distribution'] ?? [])) + ->keyBy(static fn (array $row): string => (string) ($row['category'] ?? '')); + $retainedCard = (array) ($accountData['retainedCard'] ?? []); + $unretainedCard = (array) ($accountData['unretainedCard'] ?? []); + $storePlatforms = $this->storePlatformRows( + (array) ($storeDaily['platforms'] ?? []), + $month['end'], + ); + $storeReadyPlatforms = count(array_filter( + $storePlatforms, + static fn (array $row): bool => $row['available'], + )); + $storePlatformTotal = count(StoreReceivingBalanceOverviewService::PLATFORMS); + $storeComplete = $storeReady + && $storeReadyPlatforms === $storePlatformTotal + && $storePlatformTotal > 0; + $storeBalance = round(array_sum(array_column($storePlatforms, 'balance')), 2); + $accountLinks = $this->accountLinks($month); + $storeLink = $this->storeLink($month); + + $categories = [ + $this->accountCategory( + 'bank', + '银行账户余额', + '保留银行账户', + (array) $distribution->get('bank', []), + $accountLinks['bank'], + '查看银行余额明细', + ), + $this->accountCategory( + 'alipay', + '非店铺支付宝余额', + '保留非店铺支付宝账户', + (array) $distribution->get('alipay', []), + $accountLinks['alipay'], + '查看支付宝账户明细', + ), + $this->accountCategory( + 'wangshang', + '网商银行余额', + '保留网商银行账户', + (array) $distribution->get('wangshang', []), + $accountLinks['wangshang'], + '查看网商银行明细', + ), + $this->accountCategory( + 'other', + '其他账户余额', + '其他支付宝、微信等保留账户', + (array) $distribution->get('other', []), + $accountLinks['other'], + '查看其他账户明细', + ), + [ + 'key' => 'store', + 'label' => '店铺余额', + 'description' => '8 个店铺平台每日余额汇总', + 'count' => $storeReadyPlatforms, + 'count_label' => $storeReadyPlatforms . '/' . $storePlatformTotal . ' 个平台', + 'balance' => $storeBalance, + 'available' => $storeBalance, + 'blocked' => 0.0, + 'share' => 0.0, + 'status' => $storeComplete ? '完整' : ($storeReady ? '部分平台未就绪' : '暂不可用'), + 'status_level' => $storeComplete ? 'complete' : 'warning', + 'link' => $storeLink, + 'link_label' => '查看店铺每日余额明细', + ], + ]; + + $totalBalance = round(array_sum(array_column($categories, 'balance')), 2); + $availableBalance = round(array_sum(array_column($categories, 'available')), 2); + $blockedBalance = round(array_sum(array_column($categories, 'blocked')), 2); + foreach ($categories as &$category) { + $category['share'] = $totalBalance > 0 + ? round(max(0.0, (float) $category['balance']) / $totalBalance * 100, 1) + : 0.0; + } + unset($category); + + $complete = $accountReady && $storeComplete; + $status = $this->status( + $complete, + $accountReady, + $storeReady, + $storeReadyPlatforms, + $storePlatformTotal, + $accountError, + $storeError, + ); + + return [ + 'view' => 'business.pages.fund-management.balance-monitor', + 'month' => $month, + 'periodLabel' => $month['start'] . ' 至 ' . $month['end'], + 'summary' => [ + 'total' => $totalBalance, + 'available' => $availableBalance, + 'blocked' => $blockedBalance, + 'available_percent' => $totalBalance > 0 + ? round($availableBalance / $totalBalance * 100, 1) + : 0.0, + 'blocked_percent' => $totalBalance > 0 + ? round($blockedBalance / $totalBalance * 100, 1) + : 0.0, + 'retained_account_count' => (int) ($retainedCard['count'] ?? 0), + 'unretained_account_count' => (int) ($unretainedCard['count'] ?? 0), + 'unretained_balance' => $this->number($unretainedCard['current_balance'] ?? null), + 'store_platform_ready' => $storeReadyPlatforms, + 'store_platform_total' => $storePlatformTotal, + 'complete' => $complete, + ], + 'categories' => $categories, + 'status' => $status, + 'links' => [ + 'refresh' => route('business.pages.show', [ + 'section' => 'fund-management', + 'page' => 'fund-balance-monitor', + 'month' => $month['value'], + ]), + ], + ]; + } + + /** + * @param array $rowData + * @return array + */ + private function accountCategory( + string $key, + string $label, + string $description, + array $rowData, + string $link, + string $linkLabel, + ): array { + $balance = $this->number($rowData['current_balance'] ?? null); + $available = $this->number($rowData['available_balance'] ?? null); + $blocked = $this->number($rowData['blocked_balance'] ?? null); + $count = (int) ($rowData['count'] ?? 0); + $missing = array_key_exists('current_balance', $rowData) + && $rowData['current_balance'] === null; + + return [ + 'key' => $key, + 'label' => $label, + 'description' => $description, + 'count' => $count, + 'count_label' => number_format($count) . ' 户', + 'balance' => $balance, + 'available' => $available, + 'blocked' => $blocked, + 'share' => 0.0, + 'status' => $missing ? '余额待核对' : ($count > 0 ? '正常' : '无账户'), + 'status_level' => $missing ? 'warning' : 'complete', + 'link' => $link, + 'link_label' => $linkLabel, + ]; + } + + /** + * @param array> $platforms + * @return array> + */ + private function storePlatformRows(array $platforms, string $asOf): array + { + $bySlug = collect($platforms)->keyBy( + static fn (array $platform): string => (string) ($platform['slug'] ?? ''), + ); + $rows = []; + foreach (StoreReceivingBalanceOverviewService::PLATFORMS as $definition) { + $platform = (array) $bySlug->get((string) $definition['slug'], []); + $daily = (array) ($platform['daily'][$asOf] ?? []); + $closing = $daily['closing'] ?? null; + $available = is_numeric($closing); + $rows[] = [ + 'slug' => (string) $definition['slug'], + 'name' => (string) $definition['name'], + 'available' => $available, + 'balance' => $available ? round((float) $closing, 2) : 0.0, + 'date' => $asOf, + 'error' => $available + ? '' + : trim((string) ( + $platform['daily_error'] + ?? $platform['error'] + ?? '月末余额未就绪' + )), + ]; + } + + return $rows; + } + + /** + * @param array $month + * @return array + */ + private function accountLinks(array $month): array + { + $query = [ + 'section' => 'financial-operations', + 'page' => 'ledger', + 'tab' => 'overview', + 'view' => 'accounts', + 'keep' => 'retained', + 'period_from' => $month['start'], + 'period_to' => $month['end'], + ]; + + return [ + 'bank' => route('business.pages.show', [ + 'section' => 'financial-operations', + 'page' => 'fund-ledger', + 'tab' => 'bank_balance', + 'period_from' => $month['start'], + 'period_to' => $month['end'], + ]), + 'alipay' => route('business.pages.show', [ + ...$query, + 'account_category' => 'alipay', + ]), + 'wangshang' => route('business.pages.show', [ + ...$query, + 'account_category' => 'wangshang', + ]), + 'other' => route('business.pages.show', [ + ...$query, + 'account_category' => 'other', + ]), + ]; + } + + /** @param array $month */ + private function storeLink(array $month): string + { + $url = route('business.pages.show', [ + 'section' => 'billing-transactions', + 'page' => 'store-receiving-bills', + 'tab' => 'balance-overview', + 'month' => $month['value'], + ]); + + return $url . '#trend-table-title'; + } + + /** @return array{level:string,title:string,message:string} */ + private function status( + bool $complete, + bool $accountReady, + bool $storeReady, + int $readyPlatforms, + int $platformTotal, + ?string $accountError, + ?string $storeError, + ): array { + if (! $accountReady) { + return [ + 'level' => 'warning', + 'title' => '账户余额待补齐', + 'message' => $accountError ?: '非店铺账户余额暂时无法读取。', + ]; + } + if (! $storeReady) { + return [ + 'level' => 'warning', + 'title' => '店铺余额待补齐', + 'message' => $storeError ?: '店铺每日余额暂时无法读取,当前总资金为已接入数据小计。', + ]; + } + if (! $complete) { + return [ + 'level' => 'warning', + 'title' => '部分店铺平台未就绪', + 'message' => $readyPlatforms . '/' . $platformTotal + . ' 个平台已返回月末余额,当前总资金为已接入数据小计。', + ]; + } + + return [ + 'level' => 'complete', + 'title' => '本月数据完整', + 'message' => '五类资金均已按月末口径汇总,可直接用于资金审阅。', + ]; + } + + private function number(mixed $value): float + { + return is_numeric($value) ? round((float) $value, 2) : 0.0; + } +} diff --git a/modules/EcommerceFinance/View/NonStoreAccountMonitorPageData.php b/modules/EcommerceFinance/View/NonStoreAccountMonitorPageData.php index e13b33d1..b00b7043 100644 --- a/modules/EcommerceFinance/View/NonStoreAccountMonitorPageData.php +++ b/modules/EcommerceFinance/View/NonStoreAccountMonitorPageData.php @@ -96,9 +96,10 @@ final class NonStoreAccountMonitorPageData ) { } - public function page(): array + /** @param array{from:string,to:string}|null $periodOverride */ + public function page(?array $periodOverride = null): array { - $period = $this->period(); + $period = $periodOverride ?? $this->period(); $accountRows = $this->accountRows()->all(); $this->attachFlowMetrics($accountRows, $period, true); $this->attachBankBillMetrics($accountRows, $period, true); diff --git a/resources/assets/js/components/business/StoreReceivingBalanceOverview.vue b/resources/assets/js/components/business/StoreReceivingBalanceOverview.vue index d38cd206..b040b5ac 100644 --- a/resources/assets/js/components/business/StoreReceivingBalanceOverview.vue +++ b/resources/assets/js/components/business/StoreReceivingBalanceOverview.vue @@ -6,7 +6,7 @@

账户余额总览

8 个平台账户 · {{ queryPeriodLabel }} · 单位:万元

- 聚合 8 个收款平台(淘系支付宝账户、淘系微信账户、拼多多账户、京东网银钱包账户、抖音账户、唯品会MP账户、唯品会JX账户、小红书账户)在选定发生时间范围内的期初、收入与支出(单位:万元) + 聚合 8 个收款平台(店铺支付宝账户、淘系微信账户、拼多多账户、京东网银钱包账户、抖音账户、唯品会MP账户、唯品会JX账户、小红书账户)在选定发生时间范围内的期初、收入与支出(单位:万元)

@@ -406,14 +406,14 @@ @click="toggleStoreGroup(group.slug)" > {{ isStoreGroupExpanded(group.slug) ? '▾' : '▸' }} - {{ group.stores.length }} 家店铺 + 本页小计 · {{ group.stores.length }} 家店铺 @@ -495,6 +495,21 @@ const lastCompleteMonthRange = () => { }; }; +const requestedMonthRange = () => { + const month = new URLSearchParams(window.location.search).get('month') || ''; + const match = /^(\d{4})-(\d{2})$/.exec(month); + if (!match) return null; + const year = Number(match[1]); + const monthNumber = Number(match[2]); + if (monthNumber < 1 || monthNumber > 12) return null; + const lastDay = new Date(year, monthNumber, 0).getDate(); + + return { + start: `${year}-${pad(monthNumber)}-01`, + end: `${year}-${pad(monthNumber)}-${pad(lastDay)}`, + }; +}; + const emptySummary = () => ({ opening_balance: null, income: null, @@ -519,7 +534,7 @@ export default { apiBase: { type: String, required: true }, }, data() { - const range = lastCompleteMonthRange(); + const range = requestedMonthRange() || lastCompleteMonthRange(); return { dateRange: { ...range }, diff --git a/resources/assets/js/components/business/TaobaoAlipayBills.vue b/resources/assets/js/components/business/TaobaoAlipayBills.vue index 5f305bcc..fe232911 100644 --- a/resources/assets/js/components/business/TaobaoAlipayBills.vue +++ b/resources/assets/js/components/business/TaobaoAlipayBills.vue @@ -49,7 +49,7 @@ -
+
账单明细{{ total }} 条
diff --git a/resources/views/business/pages/fund-management/balance-monitor.blade.php b/resources/views/business/pages/fund-management/balance-monitor.blade.php new file mode 100644 index 00000000..21eb02cd --- /dev/null +++ b/resources/views/business/pages/fund-management/balance-monitor.blade.php @@ -0,0 +1,630 @@ +@php + $money = static fn (mixed $value): string => number_format((float) $value / 10000, 2, '.', ','); + $summary = $summary ?? []; + $categories = $categories ?? []; + $status = $status ?? []; + $month = $month ?? []; +@endphp + + + +
+
+
+

+ + {{ $month['label'] }}资金余额 +

+

+ {{ $periodLabel ?? '' }} · 月末余额口径,统一查看总资金、可用资金和不可用资金。 +

+
+
+
+ + +
+ + + + 刷新 + +
+
+ +
($status['level'] ?? '') !== 'complete', + ])> + + {{ $status['title'] ?? '' }} · {{ $status['message'] ?? '' }} +
+ +
+
+ + 本月资金总额 + + + + {{ $money($summary['total'] ?? 0) }} + 万元 + + 五类月末余额合计 +
+
+ + 本月可用资金 + + + + {{ $money($summary['available'] ?? 0) }} + 万元 + + + 占资金总额 {{ number_format((float) ($summary['available_percent'] ?? 0), 1) }}% + +
+
+ + 本月不可用资金 + + + + {{ $money($summary['blocked'] ?? 0) }} + 万元 + + + 占资金总额 {{ number_format((float) ($summary['blocked_percent'] ?? 0), 1) }}% + +
+
+ +
+
+

+ + 资金类别汇总 +

+ + 月末余额 · {{ $month['end'] }} · 单位:万元 + +
+ +
+ + + + + + + + + + + + + + + @foreach ($categories as $category) + + + + + + + + + + + @endforeach + + + + + + + + + + + +
资金类别账户范围月末余额资金占比可用金额不可用金额状态明细入口
+ + true, + ]) aria-hidden="true"> + + {{ $category['label'] }} + {{ $category['count_label'] }} + + + {{ $category['description'] }}{{ $money($category['balance']) }} + + {{ $money($category['available']) }}{{ $money($category['blocked']) }} + $category['status_level'] !== 'complete', + ])> + {{ $category['status'] }} + + + + {{ $category['link_label'] }} + + +
资金合计{{ $money($summary['total'] ?? 0) }}100.0%{{ $money($summary['available'] ?? 0) }}{{ $money($summary['blocked'] ?? 0) }} + {{ ($summary['complete'] ?? false) ? '本月数据完整' : '按已接入数据汇总' }} +
+
+
+ +
+ 口径说明 + + 非店铺资金按保留账户的月末余额计算,包含银行账户、非店铺支付宝、网商银行和其他账户; + 其他账户包含其他支付宝、微信等。 + + + 店铺资金取 {{ $summary['store_platform_ready'] ?? 0 }}/{{ $summary['store_platform_total'] ?? 0 }} + 个平台的月末每日余额;未就绪平台不计为 0,页面会提示按已接入数据汇总。 + @if (($summary['unretained_account_count'] ?? 0) > 0) + 另有不保留账户 {{ $summary['unretained_account_count'] }} 户、余额 + {{ $money($summary['unretained_balance'] ?? 0) }} 万元,未计入总资金。 + @endif + +
+
diff --git a/tests/Feature/Business/FinanceLedgerNavigationTest.php b/tests/Feature/Business/FinanceLedgerNavigationTest.php index ff5d7733..f92f79e5 100644 --- a/tests/Feature/Business/FinanceLedgerNavigationTest.php +++ b/tests/Feature/Business/FinanceLedgerNavigationTest.php @@ -43,6 +43,7 @@ class FinanceLedgerNavigationTest extends FeatureTestCase $this->assertSame([ '账户资金管理', '资金台账', + '资金余额监控', ], array_column($visiblePages, 'title')); $this->assertSame('financial-operations', $visiblePages[0]['route_section']); $this->assertSame('ledger', $visiblePages[0]['route_page']); diff --git a/tests/Feature/Business/FundBalanceMonitorPageTest.php b/tests/Feature/Business/FundBalanceMonitorPageTest.php new file mode 100644 index 00000000..4b54356e --- /dev/null +++ b/tests/Feature/Business/FundBalanceMonitorPageTest.php @@ -0,0 +1,251 @@ +user->attachPermission( + Permission::where('name', 'read-admin-panel')->firstOrFail(), + ); + + $connection = DB::connection('business_mirror'); + foreach ([ + [ + 'company_name' => '监控测试有限公司', + 'account_short' => '监控工商银行', + 'account_type' => '公户', + 'bank_name' => '中国工商银行常州分行', + 'account_no' => '100200300400500', + 'company_plan' => '保留', + 'account_should_keep' => '是', + 'current_balance' => 100000, + 'daily_available' => 80000, + 'blocked_amount' => 20000, + ], + [ + 'company_name' => '监控测试有限公司', + 'account_short' => '非店铺支付宝', + 'account_type' => '支付宝', + 'account_no' => 'monitor@example.com', + 'company_plan' => '保留', + 'account_should_keep' => '是', + 'current_balance' => 50000, + 'daily_available' => 50000, + 'blocked_amount' => 0, + ], + [ + 'company_name' => '监控测试有限公司', + 'account_short' => '非店铺网商', + 'account_type' => '网商银行', + 'account_no' => 'wangshang-monitor', + 'company_plan' => '保留', + 'account_should_keep' => '是', + 'current_balance' => 25000, + 'daily_available' => 25000, + 'blocked_amount' => 0, + ], + [ + 'company_name' => '朱俊祥', + 'account_short' => '朱俊祥微信', + 'account_type' => '微信', + 'account_no' => 'wx-monitor', + 'company_plan' => '保留', + 'account_should_keep' => '是', + 'current_balance' => 10000, + 'daily_available' => 10000, + 'blocked_amount' => 0, + ], + [ + 'company_name' => '不保留测试有限公司', + 'account_short' => '待注销华夏银行', + 'account_type' => '公户', + 'bank_name' => '华夏银行常州分行', + 'account_no' => '131500000001', + 'company_plan' => '不保留', + 'account_should_keep' => '否', + 'current_balance' => 30000, + 'daily_available' => 30000, + 'blocked_amount' => 0, + ], + ] as $account) { + $connection->table('fin_accounts')->insert($account); + } + + $connection->table('fin_flow_summary_account_sources')->insert([ + ['account_short' => '监控工商银行', 'data_source' => '银企直连', 'updated_by' => '测试'], + ['account_short' => '非店铺支付宝', 'data_source' => '手动', 'updated_by' => '测试'], + ['account_short' => '非店铺网商', 'data_source' => '手动', 'updated_by' => '测试'], + ['account_short' => '朱俊祥微信', 'data_source' => '手动', 'updated_by' => '测试'], + ['account_short' => '待注销华夏银行', 'data_source' => '手动', 'updated_by' => '测试'], + ]); + $connection->table('fin_account_opening')->insert([ + ['account_short' => '监控工商银行', 'opening_date' => '2026-10-01', 'opening_balance' => 90000], + ['account_short' => '非店铺支付宝', 'opening_date' => '2026-10-01', 'opening_balance' => 40000], + ['account_short' => '朱俊祥微信', 'opening_date' => '2026-10-01', 'opening_balance' => 5000], + ]); + $connection->table('fin_daily_flows')->insert([ + [ + 'flow_date' => '2026-10-31', + 'account_short' => '非店铺支付宝', + 'opening_balance' => 40000, + 'inflow' => 10000, + 'outflow' => 0, + 'closing_balance' => 50000, + ], + [ + 'flow_date' => '2026-10-31', + 'account_short' => '朱俊祥微信', + 'opening_balance' => 5000, + 'inflow' => 5000, + 'outflow' => 0, + 'closing_balance' => 10000, + ], + ]); + } + + protected function tearDown(): void + { + Carbon::setTestNow(); + parent::tearDown(); + } + + public function test_page_combines_retained_accounts_and_complete_store_balances(): void + { + $this->bindStoreBalances($this->storeDailyPayload(8)); + + $response = $this->loginAs()->get($this->pageUrl()); + + $response->assertOk() + ->assertSeeText('2026年10月资金余额') + ->assertSeeText('月末余额口径') + ->assertSeeText('97.50') + ->assertSeeText('80.00') + ->assertSeeText('95.50') + ->assertSeeText('2.00') + ->assertSeeText('银行账户余额') + ->assertSeeText('非店铺支付宝余额') + ->assertSeeText('网商银行余额') + ->assertSeeText('其他账户余额') + ->assertSeeText('店铺余额') + ->assertSeeText('查看银行余额明细') + ->assertSeeText('查看店铺每日余额明细') + ->assertSee('bank_balance', false) + ->assertSee('period_from=2026-10-01', false) + ->assertSee('month=2026-10', false) + ->assertDontSeeText('银行资金集中度') + ->assertDontSeeText('近 30 天') + ->assertDontSeeText('100.50'); + } + + public function test_page_uses_an_explicit_partial_total_when_a_store_platform_is_missing(): void + { + $this->bindStoreBalances($this->storeDailyPayload(7)); + + $response = $this->loginAs()->get($this->pageUrl()); + + $response->assertOk() + ->assertSeeText('部分店铺平台未就绪') + ->assertSeeText('按已接入数据汇总') + ->assertSeeText('87.50') + ->assertSeeText('70.00') + ->assertSeeText('7/8 个平台已返回月末余额'); + } + + public function test_page_uses_the_selected_month(): void + { + $this->bindStoreBalances( + $this->storeDailyPayload(8, ['2026-09-28', '2026-09-29', '2026-09-30']), + '2026-09-01', + '2026-09-30', + ); + + $response = $this->loginAs()->get($this->pageUrl() . '?month=2026-09'); + + $response->assertOk() + ->assertSeeText('2026年9月资金余额') + ->assertSeeText('2026-09-01 至 2026-09-30') + ->assertSee('month=2026-09', false); + } + + private function pageUrl(): string + { + return route('business.pages.show', [ + 'section' => 'fund-management', + 'page' => 'fund-balance-monitor', + ]); + } + + /** @param array $payload */ + private function bindStoreBalances( + array $payload, + string $start = '2026-10-01', + string $end = '2026-10-31', + ): void + { + $stores = Mockery::mock(StoreReceivingBalanceOverviewService::class); + $stores->shouldReceive('dailyBalances') + ->once() + ->with($start, $end) + ->andReturn($payload); + $this->app->instance(StoreReceivingBalanceOverviewService::class, $stores); + + $pinduoduo = Mockery::mock(PinduoduoOpeningBalanceService::class); + $pinduoduo->shouldReceive('cacheVersion')->once()->andReturn('fund-monitor-test-v1'); + $this->app->instance(PinduoduoOpeningBalanceService::class, $pinduoduo); + } + + /** @return array */ + private function storeDailyPayload( + int $availablePlatforms, + array $dates = ['2026-10-29', '2026-10-30', '2026-10-31'], + ): array + { + $platforms = []; + foreach (StoreReceivingBalanceOverviewService::PLATFORMS as $index => $definition) { + $available = $index < $availablePlatforms; + $daily = []; + foreach ($dates as $dateIndex => $date) { + $currentDateAvailable = $available; + $daily[$date] = [ + 'opening' => $currentDateAvailable ? 90000 : null, + 'income' => $currentDateAvailable ? 10000 : null, + 'expense' => 0, + 'closing' => $currentDateAvailable && $dateIndex === 2 + ? 100000 + : ($available ? 90000 : null), + 'available' => $currentDateAvailable, + ]; + } + $platforms[] = [ + 'slug' => $definition['slug'], + 'name' => $definition['name'], + 'available' => $available, + 'opening_balance' => $available ? 90000 : null, + 'income' => $available ? 10000 : null, + 'expense' => 0, + 'balance' => $available ? 100000 : null, + 'error' => $available ? null : '平台期初余额未就绪', + 'daily_error' => null, + 'daily' => $daily, + ]; + } + + return [ + 'dates' => $dates, + 'platforms' => $platforms, + ]; + } +} diff --git a/tests/Unit/EcommerceFinance/StoreReceivingBalanceOverviewServiceTest.php b/tests/Unit/EcommerceFinance/StoreReceivingBalanceOverviewServiceTest.php index 3f269748..84bafcbc 100644 --- a/tests/Unit/EcommerceFinance/StoreReceivingBalanceOverviewServiceTest.php +++ b/tests/Unit/EcommerceFinance/StoreReceivingBalanceOverviewServiceTest.php @@ -120,7 +120,7 @@ class StoreReceivingBalanceOverviewServiceTest extends TestCase $this->assertCount(8, $result['details']); $this->assertSame([ 'platform_slug' => 'taobao-alipay', - 'platform' => '淘系支付宝账户', + 'platform' => '店铺支付宝账户', 'store_name' => '店铺-1', 'bill_item' => '货款收入', 'amount' => 100.30, @@ -167,7 +167,7 @@ class StoreReceivingBalanceOverviewServiceTest extends TestCase $this->assertCount(8, $result['details']); $this->assertSame([ 'platform_slug' => 'taobao-alipay', - 'platform' => '淘系支付宝账户', + 'platform' => '店铺支付宝账户', 'store_name' => '店铺-1', 'bill_item' => '平台扣费', 'amount' => 20.30, @@ -198,7 +198,7 @@ class StoreReceivingBalanceOverviewServiceTest extends TestCase $this->assertCount(8, $result['details']); $this->assertSame([ 'platform_slug' => 'taobao-alipay', - 'platform' => '淘系支付宝账户', + 'platform' => '店铺支付宝账户', 'store_name' => '', 'bill_item' => '', 'amount' => 109.05, @@ -237,7 +237,7 @@ class StoreReceivingBalanceOverviewServiceTest extends TestCase $this->assertCount(8, $result['details']); $this->assertSame([ 'platform_slug' => 'taobao-alipay', - 'platform' => '淘系支付宝账户', + 'platform' => '店铺支付宝账户', 'store_name' => '店铺-1', 'bill_item' => '', 'opening_balance' => 100.10, diff --git a/tests/Unit/View/StoreReceivingBalanceOverviewTest.php b/tests/Unit/View/StoreReceivingBalanceOverviewTest.php index d8fea1c5..1c898e3e 100644 --- a/tests/Unit/View/StoreReceivingBalanceOverviewTest.php +++ b/tests/Unit/View/StoreReceivingBalanceOverviewTest.php @@ -35,7 +35,7 @@ class StoreReceivingBalanceOverviewTest extends TestCase $source = $this->overviewSource(); $this->assertStringContainsString( - '聚合 8 个收款平台(淘系支付宝账户、淘系微信账户、拼多多账户、京东网银钱包账户、抖音账户、唯品会MP账户、唯品会JX账户、小红书账户)', + '聚合 8 个收款平台(店铺支付宝账户、淘系微信账户、拼多多账户、京东网银钱包账户、抖音账户、唯品会MP账户、唯品会JX账户、小红书账户)', $source, ); $this->assertStringContainsString('期初余额合计', $source); @@ -54,6 +54,9 @@ class StoreReceivingBalanceOverviewTest extends TestCase { $source = $this->overviewSource(); + $this->assertStringContainsString('const requestedMonthRange = () => {', $source); + $this->assertStringContainsString("new URLSearchParams(window.location.search).get('month')", $source); + $this->assertStringContainsString('requestedMonthRange() || lastCompleteMonthRange()', $source); $this->assertStringContainsString('${this.apiBase}/daily-balance', $source); $this->assertStringContainsString('${this.apiBase}/daily-balance-stores', $source); $this->assertStringContainsString('this.dailySummary = body.daily_summary || {}', $source); @@ -88,6 +91,8 @@ class StoreReceivingBalanceOverviewTest extends TestCase $this->assertStringContainsString('v-model.trim="storeKeywordDraft"', $source); $this->assertStringContainsString('@submit.prevent="applyStoreFilters"', $source); $this->assertStringContainsString('@click="openDailyDetail(\'store\', store, date)"', $source); + $this->assertStringContainsString('本页小计 · {{ group.stores.length }} 家店铺', $source); + $this->assertStringContainsString('${group.platform} ${date} 本页小计', $source); $this->assertStringContainsString('v-if="selectedDailyDetail"', $source); $this->assertStringContainsString('dailyDetailMoney(\'opening\')', $source); $this->assertStringContainsString('dailyDetailSigned(\'income\')', $source); @@ -124,7 +129,7 @@ class StoreReceivingBalanceOverviewTest extends TestCase $navigation = file_get_contents(__DIR__ . '/../../../config/business_navigation.php'); $this->assertLessThan( - strpos($navigation, "['slug' => 'taobao-alipay', 'title' => '淘系支付宝账户']"), + strpos($navigation, "['slug' => 'taobao-alipay', 'title' => '全部支付宝账户']"), strpos($navigation, "['slug' => 'balance-overview', 'title' => '账户余额总览']"), ); }