CaiWuXiTong/modules/EcommerceFinance/Services/PerformanceDetailSubjectMatcher.php

319 lines
11 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?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 ?? ''));
$subject = $this->subjectForStoreDate($storeName, $this->rowDate($row), $map);
$row->company_name = $subject['company_name'];
$row->taxpayer_type = $subject['taxpayer_type'];
$row->company_effective_at = $subject['effective_at'];
$row->company_expires_at = $subject['expires_at'];
$row->company_plan_match = $subject['company_name'] !== null && $subject['plan'] !== null;
$row->company_status = $subject['plan']['company_status'] ?? null;
$row->company_plan = $subject['plan']['plan'] ?? null;
$row->subject_match_source = $subject['source'];
return $row;
}
/**
* 小额打款等非业绩明细沿用“店铺当前存续主体”口径。
*
* @param array<string,mixed> $map
*/
public function applyCurrent(object $row, array $map): object
{
$storeName = trim((string) ($row->store_name ?? $row->shop_name ?? ''));
$companyName = $this->companyForStore($storeName, $map);
$plan = $companyName !== '' ? ($map['plans'][$this->normalize($companyName)] ?? null) : null;
$row->company_name = $companyName !== '' ? $companyName : null;
$row->taxpayer_type = $plan['taxpayer_type'] ?? null;
return $row;
}
/**
* @param array<string,mixed> $row
* @param array<string,mixed> $map
* @return array<string,mixed>
*/
public function applyArray(array $row, array $map): array
{
return (array) $this->apply((object) $row, $map);
}
/**
* @param array<string,mixed> $row
* @param array<string,mixed> $map
* @return array<string,mixed>
*/
public function applyArrayCurrent(array $row, array $map): array
{
return (array) $this->applyCurrent((object) $row, $map);
}
/**
* 返回指定日期对应的主体名称。
*
* @param array<string,mixed> $map
*/
public function companyForStoreDate(string $storeName, string $occurredDate, array $map): string
{
return (string) ($this->subjectForStoreDate($storeName, $occurredDate, $map)['company_name'] ?? '');
}
/**
* 返回店铺当前存续主体,供小额打款等非期间明细使用。
*
* @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 companyForStore(string $storeName, array $map): string
{
$active = null;
foreach ($map['periods'][$storeName] ?? [] as $period) {
if (($period['expires_at'] ?? null) !== null) {
continue;
}
$companyName = trim((string) ($period['company_name'] ?? ''));
if ($companyName === '') {
continue;
}
$effectiveAt = trim((string) ($period['effective_at'] ?? ''));
if ($active === null || strcmp($effectiveAt, (string) $active['effective_at']) > 0) {
$active = [
'company_name' => $companyName,
'effective_at' => $effectiveAt,
];
}
}
if ($active !== null) {
return (string) $active['company_name'];
}
return $this->companyPlanCompany($storeName, $map['company_plan_shops'] ?? []);
}
/**
* @param array<string,mixed> $map
* @return array{
* company_name:?string,
* taxpayer_type:?string,
* effective_at:?string,
* expires_at:?string,
* source:string,
* plan:?array<string,mixed>
* }
*/
private function subjectForStoreDate(string $storeName, string $occurredAt, array $map): array
{
[$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
? ($map['plans'][$this->normalize($companyName)] ?? null)
: null;
return [
'company_name' => $companyName,
'taxpayer_type' => $plan['taxpayer_type'] ?? null,
'effective_at' => $effectiveAt,
'expires_at' => $expiresAt,
'source' => $companyName === null
? '未匹配'
: ($plan === null ? $source . '(公司盘点未匹配)' : $source),
'plan' => is_array($plan) ? $plan : null,
];
}
/**
* @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, '未匹配'];
}
$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, '公司盘点'];
}
/** @param array<string,array<string,mixed>> $shops */
private function companyPlanCompany(string $storeName, array $shops): string
{
$shop = $shops[$this->normalize($storeName)] ?? null;
if ($shop === null) {
return '';
}
$successor = trim((string) ($shop['successor'] ?? ''));
if ($successor !== '') {
return $successor;
}
return trim((string) ($shop['company_name'] ?? ''));
}
private function rowDate(object $row): string
{
foreach (['occurred_date', 'occurred_at', 'report_month'] as $property) {
$date = trim((string) ($row->{$property} ?? ''));
if ($date !== '') {
return substr($date, 0, 10);
}
}
return '';
}
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);
}
}