fix: 提现管理按店铺报表取存续主体 #559
@ -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,
|
||||
|
||||
@ -67,6 +67,42 @@ class StoreReportPeriodBuilder
|
||||
return $this->assemble(array_values($names), $fallbackCompanies, $knownEntities);
|
||||
}
|
||||
|
||||
/**
|
||||
* 店铺维度报表当前仍存续的主体:失效时间为空,且生效时间最新。
|
||||
*
|
||||
* @param array<int,array<string,mixed>> $stores
|
||||
* @return array<string,string>
|
||||
*/
|
||||
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<string> $names
|
||||
* @param array<string,string> $fallbackCompanies
|
||||
|
||||
@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Business;
|
||||
|
||||
use App\Business\StoreReceivingAccountSynchronizer;
|
||||
use App\Models\Business\StoreReceivingAccount;
|
||||
use Mockery;
|
||||
use Modules\EcommerceFinance\Repositories\BusinessInformationMirrorRepository;
|
||||
use Modules\EcommerceFinance\Services\StoreReportPeriodBuilder;
|
||||
use Tests\Feature\FeatureTestCase;
|
||||
|
||||
class StoreReceivingAccountSynchronizerTest extends FeatureTestCase
|
||||
{
|
||||
public function test_sync_uses_current_store_report_company_when_updating_account(): void
|
||||
{
|
||||
$storeName = '拼多多-百里香家具旗舰店';
|
||||
$account = StoreReceivingAccount::query()->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'),
|
||||
);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\EcommerceFinance;
|
||||
|
||||
use Mockery;
|
||||
use Modules\EcommerceFinance\Services\StoreReportPeriodBuilder;
|
||||
use Tests\TestCase;
|
||||
|
||||
class StoreReportCurrentCompanyTest extends TestCase
|
||||
{
|
||||
public function test_it_selects_the_latest_non_expiring_period_company(): void
|
||||
{
|
||||
$stores = [
|
||||
['erp_store_name' => '店铺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));
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user