diff --git a/app/Support/DropshipFreight/DropshipExporter.php b/app/Support/DropshipFreight/DropshipExporter.php index 4d109007a..496894398 100644 --- a/app/Support/DropshipFreight/DropshipExporter.php +++ b/app/Support/DropshipFreight/DropshipExporter.php @@ -79,7 +79,8 @@ final class DropshipExporter [$originals, $sources, $original] = $this->writeOriginal($book, $files, $baseFileId, $index, $baseCount); $lookup = $index->prepare('SELECT raw FROM originals WHERE file = ? AND sheet = ? AND row = ?'); $rest = array_diff_key(ShippingLookup::COLUMNS, array_flip(array_filter(StatementExporter::FRONT, static fn ($f) => $f[0] !== '_'))); - $tail = ['厂家运费(这一单合计)', '结算运费', '最终结算运费', '分类', '说明']; + $tail = ['厂家运费(这一单合计)', '结算运费', '分类', '说明']; + $front = self::frontColumns(); // 对账类:本月一致 / 运费有差 / 系统未核算,和前月补提供、跨月订单(按月份各一张) $sheets = ['对账' => static fn (array $r): bool => in_array($r['status'], [Category::MATCH, Category::DIFF, Category::UNCALCULATED], true)]; @@ -93,19 +94,19 @@ final class DropshipExporter if ($picked === []) { continue; } - $book->row($name, [...$original, ...array_keys(StatementExporter::FRONT), ...array_values($rest), ...$tail], true); + $book->row($name, [...$original, ...array_keys($front), ...array_values($rest), ...$tail], true); foreach (array_chunk($picked, self::CHUNK) as $chunk) { $details = $this->shipping->details(array_map(static fn (array $r): string => (string) $r['ship_waybill'], $chunk)); foreach ($chunk as $r) { $ship = $details[strtoupper((string) $r['ship_waybill'])] ?? []; $cells = $this->originalCells($r, $sources, $original, $lookup); - foreach (StatementExporter::FRONT as $field) { + foreach ($front as $field) { $cells[] = $this->front($field, $r, $ship); } foreach (array_keys($rest) as $field) { $cells[] = self::value($field, $ship[$field] ?? null); } - $book->row($name, [...$cells, $r['bill'], $r['settle'], $r['final'] ?? $r['settle'], $r['label'], $r['note']]); + $book->row($name, [...$cells, $r['bill'], $r['settle'], $r['label'], $r['note']]); } } } @@ -375,13 +376,30 @@ final class DropshipExporter }, $original); } + /** 对账表前面几列:照快递对账单的,「核算运费」后面加「最终结算运费」。 @return array */ + private static function frontColumns(): array + { + $result = []; + foreach (StatementExporter::FRONT as $label => $field) { + $result[$label] = $field; + if ($field === 'qc_freight') { + $result['最终结算运费'] = '_final'; + } + } + + return $result; + } + private function front(string $field, array $r, array $ship): string|float|null { return match ($field) { '_measured' => $ship['qc_measured_weight'] ?? null, '_unshipped' => null, '_weight_diff' => null, - '_freight_diff' => $r['bill'] !== null && ($ship['qc_freight'] ?? null) !== null ? round((float) $r['bill'] - (float) $ship['qc_freight'], 2) : null, + '_final' => $r['final'] ?? $r['settle'] ?? null, + // 运费差 = 厂家运费 − 最终结算运费 + '_freight_diff' => $r['bill'] !== null && ($r['final'] ?? $r['settle'] ?? null) !== null + ? round((float) $r['bill'] - (float) ($r['final'] ?? $r['settle']), 2) : null, default => self::value($field, $ship[$field] ?? null), }; } diff --git a/tests/Feature/Business/DropshipFreightTest.php b/tests/Feature/Business/DropshipFreightTest.php index cb407aa45..bbc0a40f0 100644 --- a/tests/Feature/Business/DropshipFreightTest.php +++ b/tests/Feature/Business/DropshipFreightTest.php @@ -180,6 +180,14 @@ final class DropshipFreightTest extends FeatureTestCase $this->assertSame('卿德客申通', $row[1]); $this->assertEquals(6, $row[2]); $this->assertSame('一致但运费有差', $row[count($row) - 2]); + // 核算运费后面是最终结算运费;运费差 = 厂家运费 − 最终结算运费 + $head = $recon[0]; + $at = array_search('核算运费', $head, true); + $this->assertSame('最终结算运费', $head[$at + 1]); + $under = collect($recon)->first(fn ($r) => $r[0] === '773444000003'); // 厂家 5、核算 6 → 按 5 结,运费差 0 + $this->assertEquals([6, 5], [$under[$at], $under[$at + 1]]); + $this->assertEquals(0, $under[array_search('运费差', $head, true)]); + $this->assertEquals([5, 5, 1], [$row[$at], $row[$at + 1], $row[array_search('运费差', $head, true)]]); // 厂家 6、核算 5:多开 1 $swapRow = collect($recon)->first(fn ($r) => $r[0] === 'OLD0000001'); $this->assertSame('773444000020', $swapRow[6]); $sheet = $book->getSheetByName('汇总'); @@ -305,8 +313,8 @@ final class DropshipFreightTest extends FeatureTestCase $data = $book->getSheetByName('对账')->toArray(null, false, false); $this->assertSame(['快递单号', '运费', '备注', '备注(2)'], array_slice($data[0], 0, 4)); $this->assertSame(['00012345678901234567', 4, ' 保留空格 ', '=1+1'], array_slice($data[1], 0, 4)); - $this->assertEquals(11, $data[1][count($data[0]) - 5]); - $this->assertSame(['厂家运费(这一单合计)', '结算运费', '最终结算运费'], array_slice($data[0], -5, 3)); + $this->assertEquals(11, $data[1][count($data[0]) - 4]); + $this->assertSame(['厂家运费(这一单合计)', '结算运费', '分类', '说明'], array_slice($data[0], -4)); $this->assertContains($book->getSheetByName('对账')->getCell('D2')->getDataType(), [ \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING, \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_INLINE, ]);