$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 ?? '')); $occurredAt = $this->rowDate($row); [$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 ? null : ($map['plans'][$this->normalize($companyName)] ?? null); $row->company_name = $companyName; $row->taxpayer_type = $plan['taxpayer_type'] ?? null; $row->company_status = $plan['company_status'] ?? null; $row->company_plan = $plan['plan'] ?? null; $row->subject_effective_at = $effectiveAt; $row->subject_expires_at = $expiresAt; $row->subject_match_source = $companyName === null ? '未匹配' : ($plan === null ? $source . '(公司盘点未匹配)' : $source); return $row; } /** * @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, 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, '公司盘点']; } private function rowDate(object $row): string { $date = trim((string) ($row->occurred_date ?? '')); if ($date !== '') { return substr($date, 0, 10); } return substr(trim((string) ($row->report_month ?? '')), 0, 10); } 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); } }