diff --git a/app/Business/StoreReceivingAccountSynchronizer.php b/app/Business/StoreReceivingAccountSynchronizer.php index f3cbb3e2..35edd08f 100644 --- a/app/Business/StoreReceivingAccountSynchronizer.php +++ b/app/Business/StoreReceivingAccountSynchronizer.php @@ -4,11 +4,13 @@ namespace App\Business; use App\Models\Business\StoreReceivingAccount; use Modules\EcommerceFinance\Repositories\BusinessInformationMirrorRepository; +use Modules\EcommerceFinance\Services\StoreReportPeriodBuilder; class StoreReceivingAccountSynchronizer { public function __construct( private readonly BusinessInformationMirrorRepository $mirror, + private readonly StoreReportPeriodBuilder $storeReportPeriods, ) { } @@ -35,6 +37,15 @@ class StoreReceivingAccountSynchronizer $byWallet = []; $byPlatformAndName = []; $byName = []; + $sourceStores = $this->mirror->receivingAccountStores(); + // 店铺主体跟随店铺维度报表的“存续中”期间,不再直接取店铺档案当前主体。 + $currentCompanies = $this->storeReportPeriods->currentCompanies(array_map( + static fn (array $row): array => [ + 'erp_store_name' => trim((string) ($row['erp_store_name'] ?? '')), + 'system_company_name' => trim((string) ($row['company_name'] ?? '')), + ], + $sourceStores, + )); foreach ($existing as $account) { $platform = (string) $account->store_platform; @@ -49,7 +60,7 @@ class StoreReceivingAccountSynchronizer } } - foreach ($this->mirror->receivingAccountStores() as $row) { + foreach ($sourceStores as $row) { $erpStoreName = trim((string) ($row['erp_store_name'] ?? '')); $platform = $this->classify($erpStoreName, (string) ($row['source_platform'] ?? '')); @@ -75,7 +86,8 @@ class StoreReceivingAccountSynchronizer 'company_id' => $companyId, 'store_platform' => $platform, 'store_name' => $erpStoreName, - 'store_entity_name' => trim((string) ($row['company_name'] ?? '')), + 'store_entity_name' => $currentCompanies[$erpStoreName] + ?? trim((string) ($row['company_name'] ?? '')), 'shop_name' => trim((string) ($row['shop_name'] ?? '')) ?: $erpStoreName, 'store_status' => $this->status($row['store_status'] ?? null), 'wallet_number' => $storeId, diff --git a/modules/EcommerceFinance/Services/StoreReportPeriodBuilder.php b/modules/EcommerceFinance/Services/StoreReportPeriodBuilder.php index 091fa92a..fe8c8944 100644 --- a/modules/EcommerceFinance/Services/StoreReportPeriodBuilder.php +++ b/modules/EcommerceFinance/Services/StoreReportPeriodBuilder.php @@ -67,6 +67,42 @@ class StoreReportPeriodBuilder return $this->assemble(array_values($names), $fallbackCompanies, $knownEntities); } + /** + * 店铺维度报表当前仍存续的主体:失效时间为空,且生效时间最新。 + * + * @param array> $stores + * @return array + */ + public function currentCompanies(array $stores): array + { + $result = []; + + foreach ($this->build($stores) as $storeName => $periods) { + $active = null; + foreach ($periods as $period) { + if (($period['expires_at'] ?? null) !== null) { + continue; + } + $company = $this->cleanCompany($period['company_name'] ?? null); + if ($company === null) { + continue; + } + $effectiveAt = $this->date($period['effective_at'] ?? null) ?? ''; + if ($active === null || strcmp($effectiveAt, $active['effective_at']) > 0) { + $active = [ + 'company_name' => $company, + 'effective_at' => $effectiveAt, + ]; + } + } + if ($active !== null) { + $result[$storeName] = $active['company_name']; + } + } + + return $result; + } + /** * @param list $names * @param array $fallbackCompanies diff --git a/tests/Feature/Business/StoreReceivingAccountSynchronizerTest.php b/tests/Feature/Business/StoreReceivingAccountSynchronizerTest.php new file mode 100644 index 00000000..51ce24c9 --- /dev/null +++ b/tests/Feature/Business/StoreReceivingAccountSynchronizerTest.php @@ -0,0 +1,83 @@ +create([ + 'company_id' => $this->company->id, + 'store_platform' => '拼多多', + 'erp_store_name' => $storeName, + 'shop_name' => $storeName, + 'store_entity_name' => '历史旧主体', + ]); + $source = [[ + 'id' => 701, + 'store_id' => 'PDD-701', + 'shop_name' => $storeName, + 'erp_store_name' => $storeName, + 'company_name' => '店铺基础信息旧主体', + 'source_platform' => '拼多多', + 'store_status' => '0', + 'store_type' => 'POP', + ]]; + $mirror = Mockery::mock(BusinessInformationMirrorRepository::class); + $mirror->shouldReceive('receivingAccountStores')->once()->andReturn($source); + $periods = Mockery::mock(StoreReportPeriodBuilder::class); + $periods->shouldReceive('currentCompanies') + ->once() + ->with(Mockery::on( + static fn (array $stores): bool => $stores === [[ + 'erp_store_name' => $storeName, + 'system_company_name' => '店铺基础信息旧主体', + ]], + )) + ->andReturn([$storeName => '报表存续中主体']); + + $result = (new StoreReceivingAccountSynchronizer($mirror, $periods)) + ->sync((int) $this->company->id); + + $this->assertSame(1, $result['updated']); + $this->assertSame('报表存续中主体', $account->fresh()->store_entity_name); + } + + public function test_sync_falls_back_to_store_information_company_when_no_current_period_exists(): void + { + $storeName = '天猫-无存续期主体店'; + $source = [[ + 'id' => 702, + 'store_id' => 'TM-702', + 'shop_name' => $storeName, + 'erp_store_name' => $storeName, + 'company_name' => '店铺基础信息主体', + 'source_platform' => '天猫', + 'store_status' => '0', + 'store_type' => '旗舰店', + ]]; + $mirror = Mockery::mock(BusinessInformationMirrorRepository::class); + $mirror->shouldReceive('receivingAccountStores')->once()->andReturn($source); + $periods = Mockery::mock(StoreReportPeriodBuilder::class); + $periods->shouldReceive('currentCompanies')->once()->andReturn([]); + + (new StoreReceivingAccountSynchronizer($mirror, $periods)) + ->sync((int) $this->company->id); + + $this->assertSame( + '店铺基础信息主体', + StoreReceivingAccount::withoutGlobalScopes() + ->where('company_id', $this->company->id) + ->where('store_name', $storeName) + ->value('store_entity_name'), + ); + } +} diff --git a/tests/Unit/EcommerceFinance/StoreReportCurrentCompanyTest.php b/tests/Unit/EcommerceFinance/StoreReportCurrentCompanyTest.php new file mode 100644 index 00000000..8f2178bd --- /dev/null +++ b/tests/Unit/EcommerceFinance/StoreReportCurrentCompanyTest.php @@ -0,0 +1,35 @@ + '店铺A'], + ['erp_store_name' => '店铺B'], + ['erp_store_name' => '店铺C'], + ]; + $periods = Mockery::mock(StoreReportPeriodBuilder::class)->makePartial(); + $periods->shouldReceive('build')->once()->with($stores)->andReturn([ + '店铺A' => [ + ['company_name' => '旧主体', 'effective_at' => '2026-07-01', 'expires_at' => '2026-09-13'], + ['company_name' => '过渡主体', 'effective_at' => '2026-09-14', 'expires_at' => null], + ['company_name' => '最新主体', 'effective_at' => '2026-10-01', 'expires_at' => null], + ], + '店铺B' => [ + ['company_name' => '已失效主体', 'effective_at' => '2026-07-01', 'expires_at' => '2026-09-30'], + ], + '店铺C' => [ + ['company_name' => null, 'effective_at' => '2026-07-01', 'expires_at' => null], + ], + ]); + + $this->assertSame(['店铺A' => '最新主体'], $periods->currentCompanies($stores)); + } +}