按账户总表匹配店铺账户简称 #658

Merged
yutu merged 1 commits from feature/92451-ledger-account-short-match into main 2026-10-26 21:38:52 +08:00
2 changed files with 142 additions and 17 deletions

View File

@ -71,6 +71,7 @@ class FinanceLedgerShopDirectory
}
$financeShops = $this->financeShops();
$accountMaster = $this->accountMaster();
$rows = [];
foreach ($openStores as $store) {
$erpStoreName = trim((string) ($store['erp_store_name'] ?? ''));
@ -87,15 +88,22 @@ class FinanceLedgerShopDirectory
$normalized = JushuitanShopBalanceRepository::normalizeStoreName($erpStoreName);
$financeShop = $financeShops['normalized'][$normalized] ?? null;
}
$storePayoutAccount = $this->nullableString($store['withdrawal_account'] ?? null);
$financePayoutAccount = $this->nullableString($financeShop->payout_account ?? null);
$accountShort = $this->matchAccountShort(
$storePayoutAccount ?? $financePayoutAccount,
$this->nullableString($financeShop->account_short ?? null),
$accountMaster,
);
$rows[] = [
'id' => (int) ($store['id'] ?? 0),
'platform' => $this->nullableString($store['platform'] ?? null),
'shop_name' => $erpStoreName,
'company_name' => $companyName,
'account_short' => $this->nullableString($financeShop->account_short ?? null),
'account_short' => $accountShort,
'status' => '在营',
'payout_account' => $this->nullableString(
$financeShop->payout_account ?? ($store['withdrawal_account'] ?? null),
$financePayoutAccount ?? $storePayoutAccount,
),
'shop_balance' => round((float) ($financeShop->shop_balance ?? 0), 2),
'shop_balance_date' => $this->nullableString($financeShop->shop_balance_date ?? null),
@ -138,6 +146,78 @@ class FinanceLedgerShopDirectory
];
}
/**
* @return array{by_account_no:array<string,string>,by_short:array<string,true>}
*/
private function accountMaster(): array
{
$connection = $this->databases->connection(
(string) config('upstream_mirrors.connection', 'business_mirror'),
);
if (! Schema::connection($connection->getName())->hasTable('fin_accounts')) {
return ['by_account_no' => [], 'by_short' => []];
}
$accounts = $connection->table('fin_accounts')
->get(['account_short', 'account_no', 'bank_name', 'account_type'])
->filter(function (object $account): bool {
$label = (string) ($account->account_type ?? '') . ' ' . (string) ($account->bank_name ?? '');
return mb_stripos($label, '网商银行') === false
&& mb_stripos($label, '支付宝') === false
&& trim((string) ($account->account_short ?? '')) !== '';
});
$byAccountNo = [];
foreach ($accounts->groupBy(
fn (object $account): string => $this->normalizeAccountValue($account->account_no ?? null),
) as $key => $group) {
$shorts = $group
->pluck('account_short')
->map(static fn (mixed $value): string => trim((string) $value))
->filter()
->unique();
if ($key !== '' && $shorts->count() === 1) {
$byAccountNo[$key] = $shorts->first();
}
}
return [
'by_account_no' => $byAccountNo,
'by_short' => $accounts
->pluck('account_short')
->map(static fn (mixed $value): string => trim((string) $value))
->filter()
->unique()
->mapWithKeys(static fn (string $short): array => [$short => true])
->all(),
];
}
/**
* @param array{by_account_no:array<string,string>,by_short:array<string,true>} $accountMaster
*/
private function matchAccountShort(
?string $payoutAccount,
?string $existingShort,
array $accountMaster,
): ?string {
$accountNo = $this->normalizeAccountValue($payoutAccount);
if ($accountNo !== '' && isset($accountMaster['by_account_no'][$accountNo])) {
return $accountMaster['by_account_no'][$accountNo];
}
$existingShort = trim((string) $existingShort);
return $existingShort !== '' && isset($accountMaster['by_short'][$existingShort])
? $existingShort
: null;
}
private function normalizeAccountValue(mixed $value): string
{
return preg_replace('/[^a-z0-9]+/i', '', mb_strtolower(trim((string) $value))) ?? '';
}
private function nullableString(mixed $value): ?string
{
$value = trim((string) $value);

View File

@ -21,6 +21,35 @@ class FinanceLedgerShopDirectoryTest extends FeatureTestCase
public function test_shop_ledger_uses_store_report_current_subject_and_platform(): void
{
DB::connection('business_mirror')->table('fin_accounts')->insert([
[
'company_name' => '测试公司',
'account_short' => '公户账户',
'account_no' => 'ABC-1234',
'account_type' => '公户',
'bank_name' => null,
'created_at' => now(),
'updated_at' => now(),
],
[
'company_name' => '测试公司',
'account_short' => '支付宝账户',
'account_no' => 'PAY-8888',
'account_type' => '支付宝',
'bank_name' => null,
'created_at' => now(),
'updated_at' => now(),
],
[
'company_name' => '测试公司',
'account_short' => '网商银行账户',
'account_no' => 'NET-7777',
'bank_name' => '网商银行',
'account_type' => '公户',
'created_at' => now(),
'updated_at' => now(),
],
]);
DB::connection('business_mirror')->table('fin_shops')->insert([
'shop_name' => '报表测试店',
'platform' => '天猫',
@ -36,7 +65,7 @@ class FinanceLedgerShopDirectoryTest extends FeatureTestCase
'erp_store_name' => '天猫-报表测试店',
'company_name' => '历史主体',
'store_status' => '0',
'withdrawal_account' => '回款账户甲',
'withdrawal_account' => 'ABC-1234',
];
$closedStore = [
'id' => 902,
@ -52,17 +81,25 @@ class FinanceLedgerShopDirectoryTest extends FeatureTestCase
'erp_store_name' => '淘宝-无主体店',
'company_name' => null,
'store_status' => '0',
'withdrawal_account' => null,
'withdrawal_account' => 'PAY-8888',
];
$openStoreWithExcludedBank = [
'id' => 904,
'platform' => '拼多多',
'erp_store_name' => '拼多多-网商店',
'company_name' => null,
'store_status' => '0',
'withdrawal_account' => 'NET-7777',
];
$this->mock(BusinessInformationMirrorRepository::class)
->shouldReceive('allStores')
->once()
->andReturn([$openStore, $closedStore, $openStoreWithoutSubject]);
->andReturn([$openStore, $closedStore, $openStoreWithoutSubject, $openStoreWithExcludedBank]);
$this->mock(StoreReportPeriodBuilder::class)
->shouldReceive('build')
->once()
->with([$openStore, $openStoreWithoutSubject])
->with([$openStore, $openStoreWithoutSubject, $openStoreWithExcludedBank])
->andReturn([
'天猫-报表测试店' => [
[
@ -80,17 +117,25 @@ class FinanceLedgerShopDirectoryTest extends FeatureTestCase
],
]);
$this->loginAs()->get($this->pageUrl())
->assertOk()
->assertSeeText('天猫')
->assertSeeText('天猫-报表测试店')
->assertSeeText('当前存续主体')
->assertSeeText('淘宝-无主体店')
->assertSeeText('在营')
->assertSeeText('回款账户甲')
->assertSeeText('123.45')
->assertDontSeeText('京东-已关店')
->assertDontSeeText('历史主体');
$html = $this->loginAs()->get($this->pageUrl())->assertOk()->getContent();
$panelStart = strpos($html, 'id="ledger-panel-shops"');
$panelEnd = strpos($html, 'id="ledger-panel-flows"', $panelStart);
$this->assertNotFalse($panelStart);
$this->assertNotFalse($panelEnd);
$shopHtml = substr($html, $panelStart, $panelEnd - $panelStart);
$this->assertStringContainsString('天猫', $shopHtml);
$this->assertStringContainsString('天猫-报表测试店', $shopHtml);
$this->assertStringContainsString('当前存续主体', $shopHtml);
$this->assertStringContainsString('淘宝-无主体店', $shopHtml);
$this->assertStringContainsString('拼多多-网商店', $shopHtml);
$this->assertStringContainsString('在营', $shopHtml);
$this->assertStringContainsString('公户账户', $shopHtml);
$this->assertStringContainsString('123.45', $shopHtml);
$this->assertStringNotContainsString('京东-已关店', $shopHtml);
$this->assertStringNotContainsString('历史主体', $shopHtml);
$this->assertStringNotContainsString('支付宝账户', $shopHtml);
$this->assertStringNotContainsString('网商银行账户', $shopHtml);
}
private function pageUrl(): string