CaiWuXiTong/modules/EcommerceFinance/Services/PerformanceDetailSubjectMatcher.php

125 lines
4.1 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
{
// 业绩明细使用 store_name,小额打款使用 shop_name;两者共用同一套当前主体口径。
$storeName = trim((string) ($row->store_name ?? $row->shop_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);
}
}