CaiWuXiTong/tests/Feature/Business/FundBalanceMonitorPageTest.php
qiuxiang 4dfed91c92 feat(资金管理): 新增月度资金余额监控并统一支付宝账户口径
合并月度资金余额监控页、店铺每日余额本页小计标识及全部/店铺支付宝账户名称区分。
2026-11-27 14:33:32 +08:00

252 lines
9.6 KiB
PHP

<?php
namespace Tests\Feature\Business;
use App\Models\Auth\Permission;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
use Mockery;
use Modules\EcommerceFinance\Services\PinduoduoOpeningBalanceService;
use Modules\EcommerceFinance\Services\StoreReceivingBalanceOverviewService;
use Tests\Feature\FeatureTestCase;
class FundBalanceMonitorPageTest extends FeatureTestCase
{
protected function setUp(): void
{
parent::setUp();
Carbon::setTestNow('2026-11-27 10:30:00');
$this->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<string,mixed> $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<string,mixed> */
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,
];
}
}