$storeNames * @return array{ * periods:array>, * plans:array>, * company_plan_shops:array> * } */ 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>, * plans:array>, * company_plan_shops:array> * } $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 $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 $row * @param array $map * @return array */ public function applyArray(array $row, array $map): array { return (array) $this->apply((object) $row, $map); } /** * @param array $row * @param array $map * @return array */ public function applyArrayCurrent(array $row, array $map): array { return (array) $this->applyCurrent((object) $row, $map); } /** * 返回指定日期对应的主体名称。 * * @param array $map */ public function companyForStoreDate(string $storeName, string $occurredDate, array $map): string { return (string) ($this->subjectForStoreDate($storeName, $occurredDate, $map)['company_name'] ?? ''); } /** * 返回店铺当前存续主体,供小额打款等非期间明细使用。 * * @param array{ * periods:array>, * plans:array>, * company_plan_shops:array> * } $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 $map * @return array{ * company_name:?string, * taxpayer_type:?string, * effective_at:?string, * expires_at:?string, * source:string, * plan:?array * } */ 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> $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> $shops * @param array> $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> $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); } }