补丁13和14:精简代发汇总及同名运费列取最右 (#1595)

This commit is contained in:
limeng 2026-12-01 13:23:58 +08:00
parent 9de1010c6f
commit fdc2ea8e3f
4 changed files with 29 additions and 36 deletions

View File

@ -237,13 +237,14 @@ final class BillParser
return ['waybill' => $waybill, 'amount' => $amount, 'weight' => $weight, 'item' => $item];
}
/** 先按候选顺序全等匹配,再退到包含匹配(单号列不认「订单号」「交易单号」)。 */
/** 先按候选顺序全等匹配(金额、重量同名多列取最右边),再退到包含匹配(单号列不认「订单号」「交易单号」)。 */
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;
$indexes = array_keys($headers, preg_replace('/\s+/u', '', $candidate), true);
if ($indexes !== []) {
// 金额、重量列同名出现多次(如手工合并的账单:运费、中转费、拦截费、运费),取最右边那列——手工习惯把最终金额放在最后
return (int) ($waybill ? min($indexes) : max($indexes));
}
}
foreach ($candidates as $candidate) {

View File

@ -140,7 +140,7 @@ final class DropshipExporter
/**
* 汇总:照这家上月手工汇总的样子(标题、表头、类目顺序),月份顺延。运费行系统填(厂家开的运费合计 / 按我方核算的结算运费),
* 货款、记账等先空着手工填;差异和合计行是公式,手工填了会跟着算。下面再附运费按分类的明细,说明运费差在哪。
* 货款、记账等先空着手工填;差异和合计行是公式,手工填了会跟着算。下面不再附别的(分类明细看各分类子表)。
*/
private function writeSummary(StreamingWorkbook $book, object $factory, string $month, array $rows): void
{
@ -172,33 +172,6 @@ final class DropshipExporter
$n = $last + 1;
$book->row('汇总', [$template['total'], new XlsxFormula('SUM(B2:B' . $last . ')', round($sums[0], 2)),
new XlsxFormula('SUM(C2:C' . $last . ')', round($sums[1], 2)), new XlsxFormula('N(B' . $n . ')-N(C' . $n . ')', round($sums[0] - $sums[1], 2))], true);
// 运费按分类:厂家开的每一单落在哪一类、结算多少
$groups = [];
foreach ($rows as $r) {
$label = in_array($r['status'], [Category::MATCH, Category::DIFF, Category::UNCALCULATED], true)
? (int) substr($month, 5, 2) . '月运费' : $r['label'];
$groups[$label] ??= ['bill' => null, 'settle' => null, 'n' => 0];
$groups[$label]['n']++;
if ($r['bill'] !== null) {
$groups[$label]['bill'] = (float) $groups[$label]['bill'] + (float) $r['bill'];
}
if ($r['settle'] !== null) {
$groups[$label]['settle'] = (float) $groups[$label]['settle'] + (float) $r['settle'];
}
}
$book->row('汇总', []);
$book->row('汇总', []);
$book->row('汇总', ['运费按分类', '厂家运费', '结算运费(我方核算)', '差异', '单数'], true);
foreach ($groups as $label => $g) {
$book->row('汇总', [$label, $g['bill'] === null ? null : round($g['bill'], 2), $g['settle'] === null ? null : round($g['settle'], 2),
round((float) $g['bill'] - (float) $g['settle'], 2), $g['n']]);
}
$book->row('汇总', ['合计', round((float) $billTotal, 2), round((float) $settleTotal, 2), round((float) $billTotal - (float) $settleTotal, 2), count($rows)], true);
$book->row('汇总', []);
$book->row('汇总', ['代发仓:' . $factory->warehouse . ';代发快递关键字:' . $factory->keywords
. '。一致类和前月补提供按我方核算运费结算;厂家没开的、快递不符、重复、匹配不到、未发出、跨月订单本月不付。'
. ($template['from'] === null ? '这家还没导入过手工汇总,汇总表用的默认样子。' : '汇总表照 ' . $template['from'] . ' 手工汇总的样子排。')]);
}
/** 差异 = 原始 − 简尚,两格都空就空着(公式,手工填了会跟着算)。 */

View File

@ -181,10 +181,7 @@ final class DropshipFreightTest extends FeatureTestCase
$sheet->setCellValue('C4', -100);
$this->assertEquals(100, $sheet->getCell('D4')->getCalculatedValue());
$this->assertEquals(-71, $sheet->getCell('C5')->getCalculatedValue());
$this->assertContains('10月运费', array_column($summary, 0)); // 下面附运费按分类
$total = collect($summary)->first(fn ($r) => $r[0] === '合计');
$this->assertEquals(63, $total[1]);
$this->assertEquals(29, $total[2]);
$this->assertSame(5, $sheet->getHighestDataRow()); // 汇总块下面不再附别的
$this->assertSame([3, 1], [count($book->getSheetByName('快递不符')->toArray()) - 1, count($book->getSheetByName('匹配不到')->toArray()) - 1]);
$book->disconnectWorksheets();
@unlink($out);

View File

@ -0,0 +1,22 @@
<?php
namespace Tests\Unit;
use App\Support\CourierBills\BillParser;
use PHPUnit\Framework\TestCase;
final class BillParserColumnsTest extends TestCase
{
public function test_duplicate_amount_header_uses_the_last_column(): void
{
// 手工合并的账单:前面「运费」只有部分快递填了,最后一列「运费」才是含中转费、拦截费的最终金额
$columns = BillParser::findColumns(['快递单号', '发货时间', '快递名称1', '顺丰计费重量', '首重', '续重', '运费', '中转费', '拦截费', '运费']);
$this->assertSame(['waybill' => 0, 'amount' => 9, 'weight' => 3], array_intersect_key($columns, array_flip(['waybill', 'amount', 'weight'])));
}
public function test_single_amount_header_is_unchanged(): void
{
$this->assertSame(2, BillParser::findColumns(['运单号', '计费重量', '运费', '备注'])['amount']);
$this->assertSame(0, BillParser::findColumns(['运单号', '运费', '运单号'])['waybill']);
}
}