From 566f7a28a4cc1cac9588b725442e825aa0b70f72 Mon Sep 17 00:00:00 2001 From: 92451 <924518135@qq.com> Date: Mon, 26 Oct 2026 21:38:30 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8C=89=E8=B4=A6=E6=88=B7=E6=80=BB=E8=A1=A8?= =?UTF-8?q?=E5=8C=B9=E9=85=8D=E5=BA=97=E9=93=BA=E8=B4=A6=E6=88=B7=E7=AE=80?= =?UTF-8?q?=E7=A7=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Services/FinanceLedgerShopDirectory.php | 84 ++++++++++++++++++- .../FinanceLedgerShopDirectoryTest.php | 75 +++++++++++++---- 2 files changed, 142 insertions(+), 17 deletions(-) diff --git a/modules/EcommerceFinance/Services/FinanceLedgerShopDirectory.php b/modules/EcommerceFinance/Services/FinanceLedgerShopDirectory.php index 058c6562..d7c3a496 100644 --- a/modules/EcommerceFinance/Services/FinanceLedgerShopDirectory.php +++ b/modules/EcommerceFinance/Services/FinanceLedgerShopDirectory.php @@ -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,by_short:array} + */ + 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,by_short:array} $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); diff --git a/tests/Feature/Business/FinanceLedgerShopDirectoryTest.php b/tests/Feature/Business/FinanceLedgerShopDirectoryTest.php index 88326eb3..49abbe0e 100644 --- a/tests/Feature/Business/FinanceLedgerShopDirectoryTest.php +++ b/tests/Feature/Business/FinanceLedgerShopDirectoryTest.php @@ -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 -- 2.47.2