$storeNames * @return array{ * periods:array>, * plans:array>, * 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; } } $plans = []; if ($names !== []) { foreach ($this->companyPlans->allSubjectReferences($companyId) as $plan) { $key = $this->normalize($plan['company_name'] ?? null); if ($key !== '') { $plans[$key] = $plan; } } } $planShops = []; if ($names !== []) { foreach ($this->companyPlanShops->allShopReferences($companyId) as $shop) { $key = $this->normalize($shop['shop_name'] ?? null); if ($key !== '') { $planShops[$key] = $shop; } } } return [ 'periods' => $names === [] ? [] : $this->periods->build(array_map( static fn (string $name): array => ['erp_store_name' => $name], array_values($names), )), 'plans' => $plans, 'plan_shops' => $planShops, ]; } /** * @param array $map */ public function apply(object $row, array $map): object { $storeName = trim((string) ($row->store_name ?? $row->shop_name ?? '')); $occurredOn = $this->date($row->occurred_date ?? $row->report_month ?? null); $period = $this->periodFor($occurredOn, $map['periods'][$storeName] ?? []); $companyName = $this->normalizeCompany($period['company_name'] ?? null); if ($companyName === null) { $shop = $map['plan_shops'][$this->normalize($storeName)] ?? null; if (is_array($shop)) { $companyName = $this->normalizeCompany( ($shop['successor'] ?? null) ?: ($shop['company_name'] ?? null) ); } } $plan = $companyName === null ? null : ($map['plans'][$this->normalize($companyName)] ?? null); $row->company_name = $companyName; $row->company_effective_at = $companyName === null ? null : $this->date($period['effective_at'] ?? null); $row->company_expires_at = $companyName === null ? null : $this->date($period['expires_at'] ?? null); $row->company_plan_match = $companyName !== null && is_array($plan); $row->company_status = is_array($plan) ? $this->optionalText($plan['company_status'] ?? null) : null; $row->taxpayer_type = is_array($plan) ? $this->optionalText($plan['taxpayer_type'] ?? null) : null; $row->business_registration_type = is_array($plan) ? $this->optionalText($plan['business_registration_type'] ?? null) : null; return $row; } /** * @param list $periods * @return array{company_name:?string,effective_at:string,expires_at:?string,source:string}|null */ private function periodFor(?string $date, array $periods): ?array { $matched = null; foreach ($periods as $period) { $effectiveAt = $this->date($period['effective_at'] ?? null); if ($effectiveAt === null) { continue; } $expiresAt = $this->date($period['expires_at'] ?? null); if ($date !== null) { if ($effectiveAt > $date || ($expiresAt !== null && $expiresAt < $date)) { continue; } } if ($matched === null || $effectiveAt > (string) $matched['effective_at']) { $matched = $period; } } return $matched; } private function normalizeCompany(mixed $value): ?string { return $this->optionalText($value); } private function optionalText(mixed $value): ?string { $text = trim((string) ($value ?? '')); return $text === '' ? null : $text; } 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); } private function date(mixed $value): ?string { $text = trim((string) ($value ?? '')); if ($text === '') { return null; } try { return Carbon::parse($text)->format('Y-m-d'); } catch (\Throwable) { return null; } } }