CaiWuXiTong/modules/EcommerceFinance/Services/PerformanceDetailSubjectMatcher.php
yueyang a03405e932 fix: 业绩明细仅展示当前主体和税务类型
业绩明细仅展示当前存续主体和税务类型,移除历史期间及公司状态等多余列。

34 项测试、369 个断言通过。
2026-11-16 17:38:14 +08:00

114 lines
3.7 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{
* current_companies:array<string,string>,
* taxpayer_types:array<string,string>,
* 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 ['current_companies' => [], 'taxpayer_types' => [], 'company_plan_shops' => []];
}
$taxpayerTypes = [];
foreach ($this->companyPlans->allSubjectReferences($companyId) as $plan) {
$key = $this->normalize($plan['company_name'] ?? null);
$taxpayerType = trim((string) ($plan['taxpayer_type'] ?? ''));
if ($key !== '' && $taxpayerType !== '') {
$taxpayerTypes[$key] = $taxpayerType;
}
}
$companyPlanShops = [];
foreach ($this->companyPlanShops->allShopReferences($companyId) as $shop) {
$key = $this->normalize($shop['shop_name'] ?? null);
if ($key !== '') {
$companyPlanShops[$key] = $shop;
}
}
return [
'current_companies' => $this->periods->currentCompanies(array_map(
static fn (string $name): array => ['erp_store_name' => $name],
array_values($names),
)),
'taxpayer_types' => $taxpayerTypes,
'company_plan_shops' => $companyPlanShops,
];
}
/**
* @param array{
* current_companies:array<string,string>,
* taxpayer_types:array<string,string>,
* company_plan_shops:array<string,array<string,mixed>>
* } $map
*/
public function apply(object $row, array $map): object
{
$storeName = trim((string) ($row->store_name ?? ''));
$companyName = trim((string) ($map['current_companies'][$storeName] ?? ''));
if ($companyName === '') {
$companyName = $this->companyPlanCompany($storeName, $map['company_plan_shops'] ?? []);
}
$row->company_name = $companyName !== '' ? $companyName : null;
$row->taxpayer_type = $companyName !== ''
? ($map['taxpayer_types'][$this->normalize($companyName)] ?? null)
: null;
return $row;
}
/** @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 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);
}
}