diff --git a/app/Support/CourierBills/BillParser.php b/app/Support/CourierBills/BillParser.php index a36f2a7a..1bae9e32 100644 --- a/app/Support/CourierBills/BillParser.php +++ b/app/Support/CourierBills/BillParser.php @@ -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) { diff --git a/app/Support/DropshipFreight/DropshipExporter.php b/app/Support/DropshipFreight/DropshipExporter.php index c4ff0c83..5beb43cf 100644 --- a/app/Support/DropshipFreight/DropshipExporter.php +++ b/app/Support/DropshipFreight/DropshipExporter.php @@ -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'] . ' 手工汇总的样子排。')]); } /** 差异 = 原始 − 简尚,两格都空就空着(公式,手工填了会跟着算)。 */ diff --git a/tests/Feature/Business/DropshipFreightTest.php b/tests/Feature/Business/DropshipFreightTest.php index 980f79cd..35f31831 100644 --- a/tests/Feature/Business/DropshipFreightTest.php +++ b/tests/Feature/Business/DropshipFreightTest.php @@ -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); diff --git a/tests/Unit/BillParserColumnsTest.php b/tests/Unit/BillParserColumnsTest.php new file mode 100644 index 00000000..fdba2cab --- /dev/null +++ b/tests/Unit/BillParserColumnsTest.php @@ -0,0 +1,22 @@ +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']); + } +}