CaiWuXiTong/app/Support/DropshipFreight/FactoryBillParser.php

187 lines
8.0 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Support\DropshipFreight;
use App\Support\CourierBills\BillParser;
use App\Support\CourierBills\BillWorkbookReader;
use App\Support\CourierBills\Waybill;
/**
* 从代发厂家的账单里取「快递单号 => 厂家开的运费」。
*
* 厂家账单格式各不相同(运费+货款、送货单、账单、Sheet1……),逐表在前 15 行找表头:
* 要同时有单号列和厂家运费列才读;货款、记账、退货、拦截、附加费、我方对账等表不读。
* 一个单号在一张表里有几行(一单多件),运费相加;几张表都有同一单号时,只取第一张表的。
* 运费为空或 0 的单号不算厂家开了运费(包邮、京东快递等)。
*/
final class FactoryBillParser
{
/** 单号列:按顺序先全等、再包含(不认订单号、交易单号、条码拼接列)。 */
public const WAYBILL_HEADERS = ['快递单号', '运单号', '运单号码', '运单编号', '物流单号', '母单号', '面单号', '单号'];
/** 厂家运费列:按顺序全等匹配;「快递」只在全等时当金额(卿德客账单的运费列就叫「快递」)。 */
public const AMOUNT_HEADERS = ['快递运费', '运费', '快递费', '运费金额', '快递费用', '邮费', '快递'];
/** 偏远地区单独一列的运费(浩浦等),和运费列相加。 */
public const EXTRA_AMOUNT_HEADERS = ['偏远', '偏远费', '偏远运费', '偏远加收'];
/** 表名含这些字的不读:货款/记账/退货/拦截/附加费,以及我方自己做的对账表。 */
public const SKIP_SHEET_WORDS = ['记账', '退货', '退回', '拦截', '附加', '违约', '汇总', '入库', '入仓', '报价', '对账',
'我代', '我入', '我退', '少算', '未发出', '补款', '补件', '赔'];
private const HEADER_SCAN_ROWS = 15;
private const TOTAL_WORDS = ['合计', '总计', '小计', '汇总', '总额'];
public function __construct(private readonly BillWorkbookReader $reader) {}
/**
* @return array{lines:array<string,array{amount:float,sheet:string,row:int}>,sheets:list<array{sheet:string,read:bool,note:string,rows?:int,amount?:float}>}
*/
public function parse(string $path, string $extension): array
{
$lines = [];
$sheets = [];
foreach ($this->reader->sheets($path, $extension) as $sheet => $rows) {
$sheet = (string) $sheet;
$word = self::skipWord($sheet);
if ($word !== null) {
$sheets[] = ['sheet' => $sheet, 'read' => false, 'note' => '表名含「' . $word . '」,不是运费表'];
continue;
}
$columns = null;
$scanned = 0;
$mine = [];
foreach ($rows as $rowNumber => $row) {
if ($columns === null) {
if (++$scanned > self::HEADER_SCAN_ROWS) {
break;
}
$columns = self::findColumns($row);
continue;
}
$raw = trim((string) ($row[$columns['waybill']] ?? ''));
if ($raw === '' || self::isTotalRow($row)) {
continue;
}
$waybill = Waybill::parse($raw)['waybill'];
$amount = BillParser::number($row[$columns['amount']] ?? null);
foreach ($columns['extra'] as $extra) {
$value = BillParser::number($row[$extra] ?? null);
if ($value !== null) {
$amount = ($amount ?? 0.0) + $value;
}
}
if ($waybill === null || $amount === null) {
continue;
}
$mine[$waybill] ??= ['amount' => 0.0, 'sheet' => $sheet, 'row' => (int) $rowNumber];
$mine[$waybill]['amount'] += $amount;
}
if ($columns === null) {
$sheets[] = ['sheet' => $sheet, 'read' => false, 'note' => '没有同时找到单号列和运费列'];
continue;
}
$count = 0;
$total = 0.0;
$repeated = 0;
foreach ($mine as $waybill => $line) {
$line['amount'] = round($line['amount'], 2);
if (abs($line['amount']) < 0.005) {
continue;
}
if (isset($lines[$waybill])) {
$repeated++;
continue;
}
$lines[$waybill] = $line;
$count++;
$total += $line['amount'];
}
$sheets[] = ['sheet' => $sheet, 'read' => true, 'rows' => $count, 'amount' => round($total, 2),
'note' => '单号列「' . $columns['waybill_header'] . '」、运费列「' . $columns['amount_header'] . '」'
. ($columns['extra'] !== [] ? '(加上「' . implode('」「', array_map(static fn (int $i): string => $columns['headers'][$i], $columns['extra'])) . '」)' : '')
. ($repeated > 0 ? ';' . $repeated . ' 个单号前面的表已有,没重复算' : '')];
}
return ['lines' => $lines, 'sheets' => $sheets];
}
public static function skipWord(string $sheet): ?string
{
foreach (self::SKIP_SHEET_WORDS as $word) {
if (str_contains($sheet, $word)) {
return $word;
}
}
return null;
}
/**
* @param array<int,string> $row
* @return array{waybill:int,amount:int,extra:list<int>,headers:list<string>,waybill_header:string,amount_header:string}|null
*/
public static function findColumns(array $row): ?array
{
$headers = array_map(static fn ($v): string => preg_replace('/\s+/u', '', (string) $v) ?? '', $row);
$waybill = null;
foreach (self::WAYBILL_HEADERS as $candidate) {
$index = array_search($candidate, $headers, true);
if ($index !== false) {
$waybill = (int) $index;
break;
}
}
if ($waybill === null) {
foreach (self::WAYBILL_HEADERS as $candidate) {
foreach ($headers as $index => $header) {
if ($header !== '' && str_contains($header, $candidate) && ! preg_match('/订单|交易|条码|退回|原单|换单|入库|业务|采购/u', $header)) {
$waybill = (int) $index;
break 2;
}
}
}
}
if ($waybill === null) {
return null;
}
$amount = null;
foreach (self::AMOUNT_HEADERS as $candidate) {
$index = array_search($candidate, $headers, true);
if ($index !== false && $index !== $waybill) {
$amount = (int) $index;
break;
}
}
if ($amount === null) {
foreach ($headers as $index => $header) {
if ($header !== '' && str_contains($header, '运费') && ! preg_match('/结算|提供|成本|差|率|首重|续重|面单|附加|退回|拦截|合计|货款|发货数据/u', $header)) {
$amount = (int) $index;
break;
}
}
}
if ($amount === null) {
return null;
}
$extra = [];
foreach ($headers as $index => $header) {
if ($index !== $amount && in_array($header, self::EXTRA_AMOUNT_HEADERS, true)) {
$extra[] = (int) $index;
}
}
return ['waybill' => $waybill, 'amount' => $amount, 'extra' => $extra, 'headers' => array_values($headers),
'waybill_header' => $headers[$waybill], 'amount_header' => $headers[$amount]];
}
private static function isTotalRow(array $row): bool
{
foreach (array_slice($row, 0, 3) as $cell) {
if (in_array(preg_replace('/\s+/u', '', (string) $cell) ?? '', self::TOTAL_WORDS, true)) {
return true;
}
}
return false;
}
}