$template 模板类目(已顺延月份) * @param array $auto 系统生成的行,kind 见 CourierBillMatcher::refreshSummary * @return list 模板行在前(顺序不变),模板里没有的系统行排在后面 */ public static function apply(array $template, array $auto, int $month): array { $rows = []; $index = []; foreach ($template as $label) { $key = self::normalize($label); if ($key === '') { continue; } // 手工小汇总里同名的行可以有两行(如一正一负的车费),都留着;系统金额只填第一行 $index[$key] ??= count($rows); $rows[] = ['label' => $label, 'bill' => null, 'self' => null, 'fee' => false, 'blank' => true]; } $extras = []; foreach ($auto as $label => $item) { $slot = self::slotFor((string) $label, $item['kind'], $index, $month); if ($slot === null) { $extras[] = ['label' => (string) $label, 'bill' => $item['bill'], 'self' => $item['self'], 'fee' => $item['kind'] === 'fee', 'blank' => false]; continue; } $row = &$rows[$slot]; $row['bill'] = $item['bill'] === null && $row['bill'] === null ? null : (float) $row['bill'] + (float) $item['bill']; $row['self'] = $item['self'] === null && $row['self'] === null ? null : (float) $row['self'] + (float) $item['self']; $row['fee'] = $row['fee'] || $item['kind'] === 'fee'; $row['blank'] = false; unset($row); } return [...$rows, ...$extras]; } /** 模板里属于附加费的那几行(自算先放 0,可回填)。 @param list $feeLabels @return list */ public static function feeSlots(array $template, array $feeLabels, int $month): array { $auto = []; foreach ($feeLabels as $label) { $auto[$label] = ['bill' => 0.0, 'self' => 0.0, 'kind' => 'fee']; } return array_values(array_map(static fn (array $r): string => self::normalize($r['label']), array_filter(self::apply($template, $auto, $month), static fn (array $r): bool => $r['fee']))); } /** 上月小汇总的类目:去掉当月临时行。 @param iterable $labels @return list */ public static function fromLabels(iterable $labels, int $delta): array { $result = []; foreach ($labels as $label) { $label = trim((string) $label); if ($label !== '' && ! preg_match(self::TRANSIENT, $label)) { $result[] = self::roll($label, $delta); } } return $result; } /** * 从手工发送版账单的「汇总」类表里找小汇总:表头是 标题(如「樵舍中通-上楼10月」)| 快递金额 | 自算金额 | 差异 四格, * 下面一列是类目,读到「总计 / 合计」或空行为止。一张表里可以有几块(左右并排也行)。只读类目名,不读金额。 * * @param iterable>> $sheets * @return list}> */ public static function blocks(iterable $sheets): array { $blocks = []; foreach ($sheets as $rows) { // 小汇总都在表的前面,只看前 400 行(运费明细表动辄几万行,不整表读进内存) $grid = []; foreach ($rows as $row) { $grid[] = array_values($row); if (count($grid) >= self::SCAN_ROWS) { break; } } foreach ($grid as $r => $row) { foreach ($row as $d => $cell) { // 表头:标题 | 快递金额(快递提供金额/原始/快递…)| 自算(自算金额/简尚/简尚核对…)| 差异 if ($d < 3 || ! in_array(self::normalize((string) $cell), ['差异', '差额'], true) || trim((string) ($row[$d - 1] ?? '')) === '' || trim((string) ($row[$d - 2] ?? '')) === '' || is_numeric($row[$d - 2]) || is_numeric($row[$d - 1])) { continue; } $c = $d - 2; $title = trim((string) ($row[$c - 1] ?? '')); $labels = []; for ($i = $r + 1; $i < count($grid); $i++) { $label = trim((string) ($grid[$i][$c - 1] ?? '')); if ($label === '' || in_array(self::normalize($label), self::STOP_LABELS, true) || in_array(self::normalize((string) ($grid[$i][$d] ?? '')), ['差异', '差额'], true)) { break; } $labels[] = $label; } // 首重、体积这类数字表不是小汇总 $labels = array_values(array_filter($labels, static fn (string $l): bool => ! is_numeric($l))); if ($title !== '' && ! is_numeric($title) && $labels !== []) { $blocks[] = ['title' => $title, 'labels' => $labels]; } } } } return $blocks; } public static function normalize(string $label): string { return preg_replace('/\s+/u', '', $label) ?? ''; } /** @param array $index */ private static function slotFor(string $label, string $kind, array $index, int $month): ?int { $key = self::normalize($label); if (isset($index[$key])) { return $index[$key]; } $find = static function (callable $test) use ($index): ?int { foreach ($index as $name => $slot) { if ($test((string) $name)) { return $slot; } } return null; }; return match (true) { // 本月运费(含重量差、价格差)合成手工的运费那一行:「运费-重量差」>「运费10月 / 10月运费」>「运费」 $kind === 'freight' => $find(static fn (string $n): bool => $n === '运费-重量差') ?? $find(static fn (string $n): bool => (bool) preg_match('/^(' . $month . '月)?(快递)?运费(' . $month . '月)?$/u', $n) && str_contains($n, $month . '月')) ?? $find(static fn (string $n): bool => (bool) preg_match('/^(快递)?运费$/u', $n)) ?? $find(static fn (string $n): bool => str_contains($n, '运费') && ! preg_match('/\d+月|退|补|不结算|调拨|面单|抵扣|逆向|附加/u', $n)), str_starts_with($kind, 'prior:') => $find(static fn (string $n): bool => str_contains($n, '运费') && (bool) preg_match('/(^|[^\d])' . substr($kind, 6) . '月/u', $n) && ! str_contains($n, '记账')), $kind === 'duplicate' => $find(static fn (string $n): bool => (bool) preg_match('/重复|查重/u', $n)), $kind === 'unshipped' => $find(static fn (string $n): bool => str_contains($n, '未发出') && ! str_contains($n, '实际已发出')), $kind === 'unmatched' => $find(static fn (string $n): bool => (bool) preg_match('/匹配不到|查不到|未匹配|无发货数据|匹配失败/u', $n)), $kind === 'mismatch' => $find(static fn (string $n): bool => str_contains($n, '不符')), $kind === 'accessory' => $find(static fn (string $n): bool => SummaryMapper::classify($n) === 'AL'), $kind === 'fee' => self::feeSlot($key, $find), default => null, }; } private static function feeSlot(string $label, callable $find): ?int { foreach (self::FEE_WORDS as $word) { if (str_contains($label, $word)) { $slot = $find(static fn (string $n): bool => str_contains($n, $word)); if ($slot !== null) { return $slot; } } } return $find(static fn (string $n): bool => str_contains($n, '附加费')); } /** @return array{0:int,1:int} */ private static function shift(?int $year, int $month, int $delta): array { $index = ($year ?? 2000) * 12 + ($month - 1) + $delta; return [intdiv($index, 12), $index % 12 + 1]; } }