修复:保留店铺余额快照中的真实店铺 #526

Merged
shanshi merged 2 commits from fix/shanshi-ledger-add-confirmed-shop-balances into main 2026-10-24 20:46:56 +08:00
2 changed files with 42 additions and 12 deletions

View File

@ -156,26 +156,28 @@ class FinanceLedgerShopDataSynchronizer
continue;
}
$receivingMatches = $receivingByName[$normalized] ?? [];
if (count($receivingMatches) !== 1) {
continue;
}
$receiving = $receivingMatches[0];
$bankLastFour = trim((string) ($receiving->bank_card_last_four ?? ''));
$payout = trim((string) ($receiving->withdrawal_account_name ?? ''));
if ($bankLastFour !== '') {
$payout = trim($payout . ' ' . $bankLastFour);
$receiving = count($receivingMatches) === 1 ? $receivingMatches[0] : null;
$bankLastFour = $receiving === null ? '' : trim((string) ($receiving->bank_card_last_four ?? ''));
$payout = $receiving === null ? '' : trim((string) ($receiving->withdrawal_account_name ?? ''));
if ($payout !== '' && $bankLastFour !== '') {
$payout .= ' ' . $bankLastFour;
}
$insert[] = [
'shop_name' => mb_substr($name, 0, 200),
'platform' => $this->financialPlatform((string) ($source->source_platform ?? ''), $name),
'company_name' => trim((string) ($receiving->store_entity_name ?? '')) ?: null,
'company_name' => $receiving === null
? null
: (trim((string) ($receiving->store_entity_name ?? '')) ?: null),
'account_short' => null,
'status' => trim((string) ($receiving->store_status ?? '')) ?: '在营',
'status' => $receiving === null
? '待确认'
: (trim((string) ($receiving->store_status ?? '')) ?: '在营'),
'payout_account' => $payout !== '' ? mb_substr($payout, 0, 100) : null,
'shop_balance' => 0,
'shop_balance_date' => null,
'remark' => '由店铺收款账户和余额快照补充,待确认回款账户',
'remark' => $receiving === null
? '由聚水潭店铺余额快照补充,待确认回款账户'
: '由店铺收款账户和余额快照补充,待确认回款账户',
'created_at' => $now,
'updated_at' => $now,
];

View File

@ -87,6 +87,34 @@ class FinanceLedgerShopDataSynchronizerTest extends FeatureTestCase
$this->assertSame('2026-10-23', substr((string) $shop->shop_balance_date, 0, 10));
}
public function test_balance_sync_adds_missing_store_even_when_receiving_account_is_unknown(): void
{
Carbon::setTestNow('2026-10-24 09:00:00');
$this->insertBalance(
DB::connection('business_mirror'),
'2026-10-23',
'京喜-待确认店铺',
432.10,
'京喜',
);
$result = app(FinanceLedgerShopDataSynchronizer::class)->syncShopBalances('2026-10-23');
$this->assertSame(1, $result['added']);
$shop = DB::connection('business_mirror')->table('fin_shops')
->where('shop_name', '京喜-待确认店铺')
->first();
$this->assertNotNull($shop);
$this->assertSame('京喜企业店', $shop->platform);
$this->assertSame('待确认', $shop->status);
$this->assertNull($shop->company_name);
$this->assertNull($shop->account_short);
$this->assertNull($shop->payout_account);
$this->assertSame(432.10, (float) $shop->shop_balance);
$this->assertSame('2026-10-23', substr((string) $shop->shop_balance_date, 0, 10));
$this->assertStringContainsString('待确认回款账户', (string) $shop->remark);
}
public function test_withdrawal_rows_are_mapped_to_shop_account_income_and_are_idempotent(): void
{
Carbon::setTestNow('2026-10-24 09:00:00');