155 lines
6.7 KiB
PHP
155 lines
6.7 KiB
PHP
<?php
|
||
|
||
namespace App\Support\CourierBills;
|
||
|
||
use Generator;
|
||
|
||
/**
|
||
* 从快递账单文件里找表头、取出每一行的单号 / 运费 / 重量,原始列整行保留。
|
||
*
|
||
* 表头可能不在第一行(上面常有标题行),所以在每个工作表前 15 行里找含单号列的那一行;
|
||
* 没有单号列的工作表(快递自己做的汇总页等)整表跳过。
|
||
*/
|
||
final class BillParser
|
||
{
|
||
public const WAYBILL_HEADERS = ['运单号', '运单编号', '快递单号', '运单号码', '面单号', '单号'];
|
||
public const AMOUNT_HEADERS = ['运费', '快递运费', '结算运费', '结算金额', '应付金额', '总运费', '金额', '费用'];
|
||
public const WEIGHT_HEADERS = ['计费重量', '结算重量', '重量', '实际重量'];
|
||
|
||
private const HEADER_SCAN_ROWS = 15;
|
||
private const SKIP_WORDS = ['合计', '总计', '小计', '汇总', '总额'];
|
||
|
||
public function __construct(private readonly BillWorkbookReader $reader) {}
|
||
|
||
/**
|
||
* @param array{waybill_header?:?string,amount_header?:?string,weight_header?:?string} $overrides
|
||
* @return Generator<int, array{type:string}&array<string,mixed>>
|
||
* type=sheet: {sheet, header_row, headers, waybill, amount, weight, skipped?:string}
|
||
* type=line: {sheet, row, waybill, amount, weight, raw}
|
||
* type=reject: {sheet, row, value, reason}
|
||
*/
|
||
public function parse(string $path, string $extension, array $overrides = []): Generator
|
||
{
|
||
foreach ($this->reader->sheets($path, $extension) as $sheet => $rows) {
|
||
$header = null;
|
||
$columns = null;
|
||
$scanned = 0;
|
||
foreach ($rows as $rowNumber => $row) {
|
||
if ($columns === null) {
|
||
$scanned++;
|
||
$found = self::findColumns($row, $overrides);
|
||
if ($found !== null) {
|
||
$header = $row;
|
||
$columns = $found;
|
||
yield ['type' => 'sheet', 'sheet' => (string) $sheet, 'header_row' => $rowNumber,
|
||
'headers' => array_map('strval', $row), ...$columns];
|
||
if ($columns['amount'] === null) {
|
||
yield ['type' => 'sheet', 'sheet' => (string) $sheet, 'skipped' => '找到单号列但没有运费列,整表跳过'];
|
||
break;
|
||
}
|
||
continue;
|
||
}
|
||
if ($scanned >= self::HEADER_SCAN_ROWS) {
|
||
yield ['type' => 'sheet', 'sheet' => (string) $sheet, 'skipped' => '前 15 行没有找到单号列,整表跳过'];
|
||
break;
|
||
}
|
||
continue;
|
||
}
|
||
|
||
$rawWaybill = (string) ($row[$columns['waybill']] ?? '');
|
||
$amount = self::number($row[$columns['amount']] ?? null);
|
||
$weight = $columns['weight'] === null ? null : self::number($row[$columns['weight']] ?? null);
|
||
$trimmed = trim($rawWaybill);
|
||
// 空单号、合计行、公式错误值(#N/A 等,手工表里 VLOOKUP 没找到时常见)都不算账单行。
|
||
if ($trimmed === '' || self::isTotalRow($row) || preg_match('/^#(N\/A|REF!|VALUE!|DIV\/0!|NAME\?|NULL!|NUM!)$/i', $trimmed)) {
|
||
continue;
|
||
}
|
||
$result = Waybill::parse($trimmed);
|
||
if ($result['waybill'] === null) {
|
||
yield ['type' => 'reject', 'sheet' => (string) $sheet, 'row' => $rowNumber, 'value' => $trimmed, 'reason' => $result['reason']];
|
||
continue;
|
||
}
|
||
$raw = [];
|
||
foreach ($header as $index => $_) {
|
||
$raw[] = (string) ($row[$index] ?? '');
|
||
}
|
||
yield ['type' => 'line', 'sheet' => (string) $sheet, 'row' => $rowNumber, 'waybill' => $result['waybill'],
|
||
'amount' => $amount, 'weight' => $weight, 'raw' => $raw];
|
||
}
|
||
if ($columns === null && $scanned < self::HEADER_SCAN_ROWS) {
|
||
yield ['type' => 'sheet', 'sheet' => (string) $sheet, 'skipped' => '没有找到单号列,整表跳过'];
|
||
}
|
||
}
|
||
}
|
||
|
||
/**
|
||
* @param array<int,string> $row
|
||
* @return array{waybill:int,amount:?int,weight:?int}|null
|
||
*/
|
||
public static function findColumns(array $row, array $overrides = []): ?array
|
||
{
|
||
$headers = array_map(static fn ($v): string => preg_replace('/\s+/u', '', (string) $v) ?? '', $row);
|
||
$waybill = self::locate($headers, array_filter([$overrides['waybill_header'] ?? null]) ?: self::WAYBILL_HEADERS, true);
|
||
if ($waybill === null) {
|
||
return null;
|
||
}
|
||
$amount = self::locate($headers, array_filter([$overrides['amount_header'] ?? null]) ?: self::AMOUNT_HEADERS, false);
|
||
$weight = self::locate($headers, array_filter([$overrides['weight_header'] ?? null]) ?: self::WEIGHT_HEADERS, false);
|
||
|
||
return ['waybill' => $waybill, 'amount' => $amount, 'weight' => $weight];
|
||
}
|
||
|
||
/** 先按候选顺序全等匹配,再退到包含匹配(单号列不认「订单号」「交易单号」)。 */
|
||
private static function locate(array $headers, array $candidates, bool $waybill): ?int
|
||
{
|
||
foreach ($candidates as $candidate) {
|
||
$index = array_search(preg_replace('/\s+/u', '', $candidate), $headers, true);
|
||
if ($index !== false) {
|
||
return (int) $index;
|
||
}
|
||
}
|
||
foreach ($candidates as $candidate) {
|
||
foreach ($headers as $index => $header) {
|
||
if ($header === '' || ! str_contains($header, $candidate)) {
|
||
continue;
|
||
}
|
||
if ($waybill && preg_match('/订单|交易|退回|原单|换单/u', $header)) {
|
||
continue;
|
||
}
|
||
if (! $waybill && preg_match('/差|率|单价|首重|续重|附加|保价|面单/u', $header)) {
|
||
continue;
|
||
}
|
||
|
||
return (int) $index;
|
||
}
|
||
}
|
||
|
||
return null;
|
||
}
|
||
|
||
private static function isTotalRow(array $row): bool
|
||
{
|
||
foreach (array_slice($row, 0, 3) as $cell) {
|
||
$cell = preg_replace('/\s+/u', '', (string) $cell) ?? '';
|
||
if (in_array($cell, self::SKIP_WORDS, true)) {
|
||
return true;
|
||
}
|
||
}
|
||
|
||
return false;
|
||
}
|
||
|
||
public static function number(mixed $value): ?float
|
||
{
|
||
if ($value === null) {
|
||
return null;
|
||
}
|
||
$text = str_replace([',', ',', '¥', '¥', '元', ' '], '', trim((string) $value));
|
||
if ($text === '' || ! is_numeric($text)) {
|
||
return null;
|
||
}
|
||
|
||
return (float) $text;
|
||
}
|
||
}
|