194 lines
6.8 KiB
PHP
194 lines
6.8 KiB
PHP
<?php
|
||
|
||
namespace Modules\EcommerceFinance\Services;
|
||
|
||
use App\Business\CompanyPlan\CompanyPlanRepository;
|
||
use App\Business\CompanyPlan\CompanyPlanShopRepository;
|
||
|
||
/**
|
||
* 按业绩发生日期匹配店铺维度报表中的主体期间,再用公司盘点补充主体资料。
|
||
*
|
||
* 同一店铺主体变更后,老主体期间仍用于匹配变更前的历史业绩;新主体从变更日生效。
|
||
*/
|
||
final class PerformanceDetailSubjectMatcher
|
||
{
|
||
public function __construct(
|
||
private readonly StoreReportPeriodBuilder $periods,
|
||
private readonly CompanyPlanRepository $companyPlans,
|
||
private readonly CompanyPlanShopRepository $companyPlanShops,
|
||
) {}
|
||
|
||
/**
|
||
* @param list<string> $storeNames
|
||
* @return array{
|
||
* periods:array<string,list<array{company_name:?string,effective_at:string,expires_at:?string,source:string}>>,
|
||
* plans:array<string,array<string,mixed>>,
|
||
* company_plan_shops:array<string,array<string,mixed>>
|
||
* }
|
||
*/
|
||
public function map(array $storeNames, int $companyId): array
|
||
{
|
||
$names = [];
|
||
foreach ($storeNames as $storeName) {
|
||
$name = trim((string) $storeName);
|
||
if ($name !== '') {
|
||
$names[$name] = $name;
|
||
}
|
||
}
|
||
if ($names === []) {
|
||
return ['periods' => [], 'plans' => [], 'company_plan_shops' => []];
|
||
}
|
||
|
||
$plans = [];
|
||
foreach ($this->companyPlans->allSubjectReferences($companyId) as $plan) {
|
||
$key = $this->normalize($plan['company_name'] ?? null);
|
||
if ($key !== '') {
|
||
$plans[$key] = $plan;
|
||
}
|
||
}
|
||
|
||
$companyPlanShops = [];
|
||
foreach ($this->companyPlanShops->allShopReferences($companyId) as $shop) {
|
||
$key = $this->normalize($shop['shop_name'] ?? null);
|
||
if ($key !== '') {
|
||
$companyPlanShops[$key] = $shop;
|
||
}
|
||
}
|
||
|
||
return [
|
||
'periods' => $this->periods->build(array_map(
|
||
static fn (string $name): array => ['erp_store_name' => $name],
|
||
array_values($names),
|
||
)),
|
||
'plans' => $plans,
|
||
'company_plan_shops' => $companyPlanShops,
|
||
];
|
||
}
|
||
|
||
/**
|
||
* @param array{
|
||
* periods:array<string,list<array{company_name:?string,effective_at:string,expires_at:?string,source:string}>>,
|
||
* plans:array<string,array<string,mixed>>,
|
||
* company_plan_shops:array<string,array<string,mixed>>
|
||
* } $map
|
||
*/
|
||
public function apply(object $row, array $map): object
|
||
{
|
||
$storeName = trim((string) ($row->store_name ?? ''));
|
||
$occurredAt = $this->rowDate($row);
|
||
[$companyName, $effectiveAt, $expiresAt, $source] = $this->periodCompany(
|
||
$storeName,
|
||
$occurredAt,
|
||
$map['periods'] ?? [],
|
||
);
|
||
|
||
if ($companyName === null) {
|
||
[$companyName, $source] = $this->companyPlanFallback(
|
||
$storeName,
|
||
$occurredAt,
|
||
$map['company_plan_shops'] ?? [],
|
||
$map['plans'] ?? [],
|
||
);
|
||
}
|
||
|
||
$plan = $companyName === null
|
||
? null
|
||
: ($map['plans'][$this->normalize($companyName)] ?? null);
|
||
|
||
$row->company_name = $companyName;
|
||
$row->taxpayer_type = $plan['taxpayer_type'] ?? null;
|
||
$row->company_status = $plan['company_status'] ?? null;
|
||
$row->company_plan = $plan['plan'] ?? null;
|
||
$row->subject_effective_at = $effectiveAt;
|
||
$row->subject_expires_at = $expiresAt;
|
||
$row->subject_match_source = $companyName === null
|
||
? '未匹配'
|
||
: ($plan === null ? $source . '(公司盘点未匹配)' : $source);
|
||
|
||
return $row;
|
||
}
|
||
|
||
/**
|
||
* @param array<string,list<array{company_name:?string,effective_at:string,expires_at:?string,source:string}>> $periods
|
||
* @return array{0:?string,1:?string,2:?string,3:?string}
|
||
*/
|
||
private function periodCompany(string $storeName, string $occurredAt, array $periods): array
|
||
{
|
||
foreach ($periods[$storeName] ?? [] as $period) {
|
||
$from = trim((string) ($period['effective_at'] ?? ''));
|
||
$to = trim((string) ($period['expires_at'] ?? ''));
|
||
if ($from !== '' && $occurredAt < $from) {
|
||
continue;
|
||
}
|
||
if ($to !== '' && $occurredAt > $to) {
|
||
continue;
|
||
}
|
||
|
||
$companyName = trim((string) ($period['company_name'] ?? ''));
|
||
|
||
return [
|
||
$companyName !== '' ? $companyName : null,
|
||
$from !== '' ? $from : null,
|
||
$to !== '' ? $to : null,
|
||
$companyName !== '' ? '店铺维度报表' : null,
|
||
];
|
||
}
|
||
|
||
return [null, null, null, null];
|
||
}
|
||
|
||
/**
|
||
* 店铺维度报表未命中时,用公司盘点的“接替主体”兜底。
|
||
*
|
||
* @param array<string,array<string,mixed>> $shops
|
||
* @param array<string,array<string,mixed>> $plans
|
||
* @return array{0:?string,1:?string}
|
||
*/
|
||
private function companyPlanFallback(
|
||
string $storeName,
|
||
string $occurredAt,
|
||
array $shops,
|
||
array $plans,
|
||
): array {
|
||
$shop = $shops[$this->normalize($storeName)] ?? null;
|
||
if ($shop === null) {
|
||
return [null, null];
|
||
}
|
||
|
||
$companyName = trim((string) ($shop['company_name'] ?? ''));
|
||
$successor = trim((string) ($shop['successor'] ?? ''));
|
||
$currentPlan = $plans[$this->normalize($companyName)] ?? null;
|
||
$successorPlan = $successor !== ''
|
||
? ($plans[$this->normalize($successor)] ?? null)
|
||
: null;
|
||
$currentRetired = in_array(trim((string) ($currentPlan['plan'] ?? '')), ['注销', '已注销'], true)
|
||
|| in_array(trim((string) ($currentPlan['company_status'] ?? '')), ['注销中', '已注销'], true);
|
||
$successorRegisteredAt = trim((string) ($successorPlan['registered_at'] ?? ''));
|
||
$successorStarted = $successorRegisteredAt === '' || $occurredAt >= $successorRegisteredAt;
|
||
|
||
if ($successor !== '' && $currentRetired && $successorStarted) {
|
||
return [$successor, '公司盘点接替主体'];
|
||
}
|
||
|
||
return [$companyName !== '' ? $companyName : null, '公司盘点'];
|
||
}
|
||
|
||
private function rowDate(object $row): string
|
||
{
|
||
$date = trim((string) ($row->occurred_date ?? ''));
|
||
if ($date !== '') {
|
||
return substr($date, 0, 10);
|
||
}
|
||
|
||
return substr(trim((string) ($row->report_month ?? '')), 0, 10);
|
||
}
|
||
|
||
private function normalize(mixed $value): string
|
||
{
|
||
$text = str_replace(['(', ')'], ['(', ')'], trim((string) $value));
|
||
$text = preg_replace('/[\s\x{00A0}\x{3000}]+/u', '', $text) ?? $text;
|
||
|
||
return mb_strtolower($text);
|
||
}
|
||
}
|