CaiWuXiTong/modules/EcommerceFinance/Services/PerformanceDetailSubjectMatcher.php
yueyang c639b351d8 fix: 修复小额打款日期翻月并补充主体税务类型
修复小额打款日历翻月,新增当前主体与税务类型列。

31 项测试、367 个断言通过。
2026-11-16 19:23:19 +08:00

124 lines
4.0 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,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,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);
}
}