From 3adc35019475e2158696c8d459a8b3eca0147b06 Mon Sep 17 00:00:00 2001 From: limeng <1131177064@qq.com> Date: Sun, 6 Dec 2026 16:37:30 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E8=BF=90=E8=B4=B9=E5=AF=B9=E8=B4=A6):=20?= =?UTF-8?q?=E6=8C=89=E6=9C=80=E7=BB=88=E7=BB=93=E7=AE=97=E5=80=BC=E8=AE=A1?= =?UTF-8?q?=E7=AE=97=E8=BF=90=E8=B4=B9=E5=B7=AE=E5=92=8C=E9=87=8D=E9=87=8F?= =?UTF-8?q?=E5=B7=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Business/CourierBillController.php | 8 ++- .../Business/DropshipFreightController.php | 4 +- .../CourierBills/SettlementComparison.php | 39 ++++++++++ .../CourierBills/StatementExporter.php | 15 ++-- .../DropshipFreight/DropshipExporter.php | 5 +- .../DropshipFreight/DropshipReconciler.php | 8 ++- .../courier-bills/details.blade.php | 14 ++-- .../courier-bills/statement.blade.php | 12 ++-- .../dropship-freight/factory.blade.php | 6 +- .../CourierBillReconciliationTest.php | 3 +- .../Business/CourierSettlementRulesTest.php | 71 +++++++++++++++++++ tests/Feature/Business/DropshipWeightTest.php | 31 ++++++++ 12 files changed, 186 insertions(+), 30 deletions(-) create mode 100644 app/Support/CourierBills/SettlementComparison.php diff --git a/app/Http/Controllers/Business/CourierBillController.php b/app/Http/Controllers/Business/CourierBillController.php index 93e4f4932..fcff3393a 100644 --- a/app/Http/Controllers/Business/CourierBillController.php +++ b/app/Http/Controllers/Business/CourierBillController.php @@ -562,13 +562,15 @@ final class CourierBillController extends Controller return response()->streamDownload(function () use ($query): void { $book = new StreamingWorkbook(); $book->row('对账明细', ['账单月份', '对账分组', '负责人', '快递单号', '快递提供运费', '快递提供重量', '分类', '发货时间', '发货数据快递名称1', - '核算运费', '计费重量', '自算金额', '差异', '未发出来源', '处理结果', '处理人'], true); + '核算运费', '结算运费', '运费差', '计费重量', '结算重量', '重量差', '未发出来源', '处理结果', '处理人'], true); foreach ($query->orderBy('l.id')->select(['l.*', 's.bill_month', 'g.name as group_name', 'g.owner'])->lazyById(1000, 'l.id', 'id') as $line) { + $comparison = \App\Support\CourierBills\SettlementComparison::courier($line); $book->row('对账明细', [$line->bill_month, $line->group_name, $line->owner, (string) $line->waybill_no, $line->bill_amount === null ? null : (float) $line->bill_amount, $line->bill_weight === null ? null : (float) $line->bill_weight, Category::label((string) $line->category, $line->category_month), $line->ship_time, $line->express_name, - $line->calc_freight === null ? null : (float) $line->calc_freight, $line->charge_weight === null ? null : (float) $line->charge_weight, - $line->self_amount === null ? null : (float) $line->self_amount, round((float) $line->bill_amount - (float) $line->self_amount, 2), + $line->calc_freight === null ? null : (float) $line->calc_freight, + $line->self_amount === null ? null : (float) $line->self_amount, $comparison['freight_diff'], + $line->charge_weight === null ? null : (float) $line->charge_weight, $comparison['settlement_weight'], $comparison['weight_diff'], $line->unshipped_sources, Category::RESOLUTIONS[$line->resolution] ?? null, $line->resolved_by]); } $book->writeTo(fopen('php://output', 'wb')); diff --git a/app/Http/Controllers/Business/DropshipFreightController.php b/app/Http/Controllers/Business/DropshipFreightController.php index 44ca4b7fd..32a388a9e 100644 --- a/app/Http/Controllers/Business/DropshipFreightController.php +++ b/app/Http/Controllers/Business/DropshipFreightController.php @@ -428,10 +428,10 @@ final class DropshipFreightController extends Controller } foreach ($details as [$factory, $rows]) { $sheet = $factories->count() === 1 ? '明细' : mb_substr((string) $factory->name, 0, 28); - $book->row($sheet, ['快递单号', '发货时间', '快递名称1', '仓库', '厂家运费', '我方核算运费', '结算运费', '最终结算运费', '差额(厂家−我方)', '状态', '说明', '核算公式'], true); + $book->row($sheet, ['快递单号', '发货时间', '快递名称1', '仓库', '厂家运费', '我方核算运费', '结算运费', '最终结算运费', '运费差', '厂家重量', '我方计费重量', '结算重量', '重量差', '状态', '说明', '核算公式'], true); foreach ($rows as $r) { $book->row($sheet, [$r['waybill'], $r['ship_time'], $r['express'], $r['warehouse'], $r['bill'], $r['calc'], $r['settle'], - $r['final'] ?? $r['settle'], $r['diff'], $r['label'], $r['note'], $r['formula']]); + $r['final'] ?? $r['settle'], $r['freight_diff'], $r['bill_weight'], $r['charge_weight'], $r['settlement_weight'], $r['weight_diff'], $r['label'], $r['note'], $r['formula']]); } } $book->writeTo(fopen('php://output', 'wb')); diff --git a/app/Support/CourierBills/SettlementComparison.php b/app/Support/CourierBills/SettlementComparison.php new file mode 100644 index 000000000..fe174eb00 --- /dev/null +++ b/app/Support/CourierBills/SettlementComparison.php @@ -0,0 +1,39 @@ +resolution) { + 'agree' => $line->bill_weight === null ? null : (float) $line->bill_weight, + 'exclude', 'defer' => 0.0, + default => in_array($line->category, [...Category::PAYABLE, Category::PRIOR], true) + ? (in_array(trim((string) ($line->express_name ?? '')), CourierBillMatcher::ACTUAL_FREIGHT_EXPRESSES, true) + ? ($line->self_amount === null || $line->charge_weight === null ? null : (float) $line->charge_weight) + : self::weight($line->bill_amount, $line->calc_freight, $line->bill_weight, $line->charge_weight, $line->self_amount)) : null, + }; + + return ['settlement_weight' => $weight, 'weight_diff' => self::difference($line->bill_weight, $weight, 4), + 'freight_diff' => self::difference($line->bill_amount, $line->self_amount)]; + } +} diff --git a/app/Support/CourierBills/StatementExporter.php b/app/Support/CourierBills/StatementExporter.php index b32dcbb8f..4324af84f 100644 --- a/app/Support/CourierBills/StatementExporter.php +++ b/app/Support/CourierBills/StatementExporter.php @@ -11,15 +11,15 @@ use Modules\EcommerceFinance\Services\CourierBookkeepingRegistry; * 导出文件以快递发来的原始账单为底:原文件的每张表一个字节不改,后面追加汇总、对账、前月补提供-X月、跨月订单-X月、快递不符、重复、 * 匹配不到、未发出、未提供、说明;没有数据的工作表不生成。 * - * 对账类子表的列顺序:快递原始列 → 财务指定的 25 列 → 发货数据其余字段 → 处理结果。 + * 对账类子表的列顺序:快递原始列 → 核算及结算对比列 → 发货数据其余字段 → 处理结果。 */ final class StatementExporter { - /** 财务指定的 25 列 => 取值来源(发货数据字段,或 _ 开头的计算项)。 */ + /** 对账列 => 取值来源(发货数据字段,或 _ 开头的计算项)。 */ public const FRONT = [ '称重重量' => '_measured', '首重' => 'qc_first_price', '面单' => 'qc_miandan', '快递单号' => 'express_no', '交易单号' => 'trade_no', '商品ID' => 'item_id', '条形码' => 'barcode', '商品名称' => 'product_name', '数量' => 'qty', - '计费重量' => 'qc_charge_weight', '续重/费率' => 'qc_rate', '核算运费' => 'qc_freight', '重量差' => '_weight_diff', + '计费重量' => 'qc_charge_weight', '结算重量' => '_settlement_weight', '续重/费率' => 'qc_rate', '核算运费' => 'qc_freight', '重量差' => '_weight_diff', '运费差' => '_freight_diff', '省' => 'receiver_state', '买家昵称' => 'buyer_nick', '收货姓名' => 'receiver_name', '店铺名称' => 'shop_name', '快递名称1' => 'express_name1', '仓库' => 'warehouse_name', '发货时间' => 'ship_time', '未发出' => '_unshipped', '运费' => 'freight', '线上重量' => 'online_weight', '实称重量' => 'actual_weight', @@ -398,7 +398,7 @@ final class StatementExporter } $rows = [ ...$rows, - [$book->nameOf('对账'), '本月发货、发货数据里找得到、属于这家快递的单号(运费一致或有差)。前面是快递原始列,后面接我方核算的 25 列(称重重量、首重、面单、计费重量、续重/费率、核算运费、重量差、运费差……)和发货数据其余字段。', $count(Category::PAYABLE)], + [$book->nameOf('对账'), '本月发货、发货数据里找得到、属于这家快递的单号(运费一致或有差)。前面是快递原始列,后面接核算与结算对比列;运费差为快递运费减结算运费,重量差为快递重量减结算重量。', $count(Category::PAYABLE)], ['前月补提供-X月', '发货时间在账单月之前、结算台账里没结算过的单号:快递之前漏报、这次补上的。', $count([Category::PRIOR])], ['跨月订单-X月', '发货时间在账单月之后的单号,本月不结算。', $count([Category::LATER])], [$book->nameOf('快递不符'), '发货数据里有这个单号,但快递名称1 不是这家(也不在同一张合并账单里)。', $count([Category::MISMATCH])], @@ -488,10 +488,9 @@ final class StatementExporter return match ($field) { '_measured' => $ship['qc_measured_weight'] ?? null, '_unshipped' => $line->unshipped_sources, - '_weight_diff' => $line->bill_weight !== null && ($ship['qc_charge_weight'] ?? null) !== null - ? round((float) $line->bill_weight - (float) $ship['qc_charge_weight'], 4) : null, - '_freight_diff' => $line->bill_amount !== null && ($ship['qc_freight'] ?? null) !== null - ? round((float) $line->bill_amount - (float) $ship['qc_freight'], 2) : null, + '_settlement_weight' => SettlementComparison::courier($line)['settlement_weight'], + '_weight_diff' => SettlementComparison::courier($line)['weight_diff'], + '_freight_diff' => SettlementComparison::courier($line)['freight_diff'], default => self::value($field, $ship[$field] ?? null), }; } diff --git a/app/Support/DropshipFreight/DropshipExporter.php b/app/Support/DropshipFreight/DropshipExporter.php index 0f43acf02..5fbec5c5c 100644 --- a/app/Support/DropshipFreight/DropshipExporter.php +++ b/app/Support/DropshipFreight/DropshipExporter.php @@ -17,7 +17,7 @@ use RuntimeException; * 代发运费对账单导出(内部版),和快递账单对账的导出一个样子: * 和手工版一样:第一张是「汇总」(照这家上月手工汇总的样子),然后是厂家发来的原始账单(表名改叫「厂家账单」,内容原样不动),后面接 对账、前月补提供-X月、跨月订单-X月、快递不符、重复、匹配不到、未发出、 * 厂家未提供、说明;没有数据的表不生成。 - * 对账类子表的列顺序:厂家账单原始列(一单多行取第一行)→ 财务指定的 25 列 → 发货数据其余字段 → 厂家运费合计、结算运费、分类、说明。 + * 对账类子表的列顺序:厂家账单原始列(一单多行取第一行)→ 核算及结算对比列 → 发货数据其余字段 → 厂家运费合计、结算运费、分类、说明。 */ final class DropshipExporter { @@ -258,7 +258,7 @@ final class DropshipExporter . ';' . ($read ? '读了这张表的运费。' : '这张表没读运费(货款、记账、退货等)。'), $lines]); } foreach ([ - ['对账', '本月发货、是这家代发单的单号(一致、运费有差、系统未核算),按我方核算运费结算。前面是厂家账单原始列,后面接我方核算的 25 列和发货数据其余字段。', [Category::MATCH, Category::DIFF, Category::UNCALCULATED]], + ['对账', '本月发货、是这家代发单的单号(一致、运费有差、系统未核算)。运费差为厂家运费减最终结算运费,重量差为厂家重量减结算重量。前面是厂家账单原始列,后面接核算及结算对比列和发货数据其余字段。', [Category::MATCH, Category::DIFF, Category::UNCALCULATED]], ['前月补提供-X月', '上月及以前发货、厂家这次才开的单,按我方核算运费结算。', [Category::PRIOR]], ['跨月订单-X月', '下个月才发货的单,本月不付,留到发货那个月。', [Category::LATER]], ['快递不符', '发货数据查得到,但不是这家代发单(不是这家代发仓发的、快递名称1 不含关键字,或是自有快递),不付。', [Category::MISMATCH]], @@ -337,6 +337,7 @@ final class DropshipExporter '_unshipped' => null, '_bill_weight' => $r['bill_weight'], 'qc_charge_weight' => $r['charge_weight'], + '_settlement_weight' => $r['settlement_weight'], '_weight_diff' => $r['weight_diff'], '_final' => $r['final'] ?? $r['settle'] ?? null, // 运费差 = 厂家运费 − 最终结算运费 diff --git a/app/Support/DropshipFreight/DropshipReconciler.php b/app/Support/DropshipFreight/DropshipReconciler.php index b949138b4..3d45e1374 100644 --- a/app/Support/DropshipFreight/DropshipReconciler.php +++ b/app/Support/DropshipFreight/DropshipReconciler.php @@ -5,6 +5,7 @@ namespace App\Support\DropshipFreight; use App\Support\CourierBills\Category; use App\Support\CourierBills\CourierBillMatcher; use App\Support\CourierBills\ShippingLookup; +use App\Support\CourierBills\SettlementComparison; use App\Support\UnshippedWaybills; /** @@ -141,12 +142,15 @@ final class DropshipReconciler $settle = in_array($status, self::PAYABLE, true) ? $calc : null; $chargeWeight = $i['charge_weight'] ?? null; $weightDiff = $billWeight !== null && $chargeWeight !== null ? round($billWeight - $chargeWeight, 4) : null; + $final = self::final($billed, $settle); + $settlementWeight = SettlementComparison::weight($billed, $calc, $billWeight, $chargeWeight, $final); return ['waybill' => $waybill, 'ship_time' => $i['ship_time'] ?? null, 'express' => $i['express'] ?? null, 'warehouse' => $i['warehouse'] ?? null, 'bill' => $billed, 'calc' => $calc, - 'bill_weight' => $billWeight, 'charge_weight' => $chargeWeight, 'weight_diff' => $weightDiff, + 'bill_weight' => $billWeight, 'charge_weight' => $chargeWeight, 'settlement_weight' => $settlementWeight, + 'weight_diff' => SettlementComparison::difference($billWeight, $settlementWeight, 4), 'diff_kind' => $status !== Category::DIFF ? null : ($weightDiff === null ? 'missing_weight' : (abs($weightDiff) > 0.005 ? 'weight_diff' : 'price_diff')), - 'settle' => $settle, 'final' => self::final($billed, $settle), + 'settle' => $settle, 'final' => $final, 'freight_diff' => SettlementComparison::difference($billed, $final), 'diff' => $billed !== null && $calc !== null ? round($billed - $calc, 2) : null, 'status' => $status, 'status_month' => $statusMonth, 'label' => self::label($status, $statusMonth), 'note' => $note, 'formula' => $i['formula'] ?? null]; diff --git a/resources/views/business/pages/courier-payables/courier-bills/details.blade.php b/resources/views/business/pages/courier-payables/courier-bills/details.blade.php index 47b78121b..c6b94309b 100644 --- a/resources/views/business/pages/courier-payables/courier-bills/details.blade.php +++ b/resources/views/business/pages/courier-payables/courier-bills/details.blade.php @@ -29,23 +29,29 @@ 账单月份对账分组负责人快递单号分类 - 快递运费核算运费结算运费差异 + 快递运费核算运费结算运费运费差 + 快递重量计费重量结算重量重量差 发货时间发货数据快递名称1处理结果 @forelse ($cbLines as $line) + @php($comparison = \App\Support\CourierBills\SettlementComparison::courier($line)) @if ($line->statement_status !== 'confirmed')@endif {{ $line->bill_month }}{{ $line->group_name }}{{ $line->owner }}{{ $line->waybill_no }} {{ \App\Support\CourierBills\Category::label((string) $line->category, $line->category_month) }} {{ $money($line->bill_amount) }}{{ $money($line->calc_freight) }} - {{ $money($line->self_amount) }}{{ $money((float) $line->bill_amount - (float) $line->self_amount) }} + {{ $money($line->self_amount) }}{{ $money($comparison['freight_diff']) }} + {{ $line->bill_weight === null ? '' : round((float) $line->bill_weight, 4) }} + {{ $line->charge_weight === null ? '' : round((float) $line->charge_weight, 4) }} + {{ $comparison['settlement_weight'] === null ? '' : round($comparison['settlement_weight'], 4) }} + {{ $comparison['weight_diff'] ?? '' }} {{ $line->ship_time ? substr((string) $line->ship_time, 0, 16) : '' }}{{ $line->express_name }} {{ \App\Support\CourierBills\Category::RESOLUTIONS[$line->resolution] ?? '' }}{{ $line->statement_status === 'confirmed' ? '(已确认结算)' : '' }} 对账单 @empty - 当前条件下没有明细。 + 当前条件下没有明细。 @endforelse @@ -63,4 +69,4 @@ 共 {{ number_format($cbLines->total()) }} 条,每页 50 条 {{ $cbLines->links('business.partials.previous-next-pagination') }} -

正常应付运费取快递提供与我方核算的较低金额;中铁物流、武进安能按我方实际核算运费结算。处理结果:同意付款 = 自算金额取快递金额;已与快递确认剔除 / 转下月 = 本月自算金额为 0。武进韵达重量多报超过1公斤的正向运费差额,合计后补50%,在小汇总单列。改完对应对账单的小汇总和快递应付汇总自动重算。

+

正常应付运费取快递提供与我方核算的较低金额;按快递较低运费结算时,结算重量也取快递重量,缺失则留空。运费差 = 快递运费 − 结算运费;重量差 = 快递重量 − 结算重量。中铁物流、武进安能按我方实际核算运费结算。处理结果:同意付款 = 结算金额和重量取快递提供值;已与快递确认剔除 / 转下月 = 本月结算金额和重量为 0。武进韵达重量多报超过1公斤的正向运费差额,合计后补50%,在小汇总单列。改完对应对账单的小汇总和快递应付汇总自动重算。

diff --git a/resources/views/business/pages/courier-payables/courier-bills/statement.blade.php b/resources/views/business/pages/courier-payables/courier-bills/statement.blade.php index d906614f2..7fa993818 100644 --- a/resources/views/business/pages/courier-payables/courier-bills/statement.blade.php +++ b/resources/views/business/pages/courier-payables/courier-bills/statement.blade.php @@ -47,7 +47,7 @@

小汇总

-

正常应付运费与已核价附加费取快递提供和我方核算的较低金额;中铁物流、武进安能的运费按我方实际核算值结算,已有手工处理优先。武进韵达:快递重量超过我方计费重量1公斤的单据,快递多报的运费差额合计的50%单列补给快递。

+

正常应付运费与已核价附加费取快递提供和我方核算的较低金额;按快递较低运费结算时,结算重量也取快递重量,缺失则留空。运费差 = 快递运费 − 结算运费;重量差 = 快递重量 − 结算重量。中铁物流、武进安能的运费按我方实际核算值结算,已有手工处理优先。武进韵达:快递重量超过我方计费重量1公斤的单据,快递多报的运费差额合计的50%单列补给快递。

@@ -111,7 +111,7 @@ @else @if ($open)@endif - + @endif @@ -120,6 +120,7 @@ @if ($isUnprovided) @else + @php($comparison = \App\Support\CourierBills\SettlementComparison::courier($line)) @if ($open)@endif @@ -130,14 +131,15 @@ - - + + + @endif @empty - + @endforelse
项目快递提供金额自算金额差异归到汇总列
快递单号快递运费快递重量发货时间发货数据快递名称1核算运费结算运费计费重量发货时间发货数据快递名称1核算运费结算运费计费重量结算重量 运费差重量差未发出来源处理结果
{{ $line->waybill_no }}{{ $line->ship_time }}{{ $line->express_name }}{{ $money($line->calc_freight) }}
{{ $line->waybill_no }}{{ $money($line->calc_freight) }} {{ $money($line->self_amount) }} {{ $line->charge_weight === null ? '' : rtrim(rtrim(number_format((float) $line->charge_weight, 3), '0'), '.') }}{{ $line->bill_amount !== null && $line->calc_freight !== null ? $money((float) $line->bill_amount - (float) $line->calc_freight) : '' }}{{ $line->bill_weight !== null && $line->charge_weight !== null ? round((float) $line->bill_weight - (float) $line->charge_weight, 3) : '' }}{{ $comparison['settlement_weight'] === null ? '' : round($comparison['settlement_weight'], 4) }}{{ $money($comparison['freight_diff']) }}{{ $comparison['weight_diff'] ?? '' }} {{ $line->unshipped_sources }} {{ \App\Support\CourierBills\Category::RESOLUTIONS[$line->resolution] ?? '' }}{{ $line->resolved_by ? ' · ' . $line->resolved_by : '' }}
这一类没有明细。
这一类没有明细。
diff --git a/resources/views/business/pages/courier-payables/dropship-freight/factory.blade.php b/resources/views/business/pages/courier-payables/dropship-freight/factory.blade.php index 96b3b292b..d9a9d3091 100644 --- a/resources/views/business/pages/courier-payables/dropship-freight/factory.blade.php +++ b/resources/views/business/pages/courier-payables/dropship-freight/factory.blade.php @@ -68,10 +68,10 @@ $feedbackPageFor = function (string $waybill) use ($dfCurrentDisputes): int { -

重量差 = 厂家账单重量 − 我方计费重量(公斤)。运费有差时,重量相同归价格差;任一重量缺失归重量待补充。差异分类不改变结算金额。

+

运费差 = 厂家运费 − 最终结算运费;重量差 = 厂家重量 − 结算重量(公斤)。按厂家较低运费结算时,结算重量也取厂家重量;厂家未提供重量则留空。上方差异原因仍按原核算值识别。

逐单核算明细

-
-@forelse($dfLines as $r)@foreach(['bill_weight','charge_weight','weight_diff'] as $weightKey)@endforeach@empty@endforelse +
快递单号发货 / 快递厂家重量(kg)我方计费重量(kg)重量差(kg)厂家运费我方核算最终结算差额分类 / 说明厂家反馈
{{ $r['waybill'] }}{{ $r['ship_time'] }}
{{ $r['express'] }}
{{ $r[$weightKey]===null?'—':rtrim(rtrim(number_format($r[$weightKey],4,'.',''),'0'),'.') }}{{ $money($r['bill']) }}{{ $money($r['calc']) }}{{ $money($r['final']??$r['settle']) }}{{ $r['bill']===null?'':$money($r['bill']-(float)($r['final']??$r['settle'])) }}{{ $r['label'] }}@if($r['diff_kind'])
{{ ['weight_diff'=>'重量差','price_diff'=>'价格差','missing_weight'=>'重量待补充'][$r['diff_kind']] }}
@endif
{{ $r['note'] }}
@if(isset($dfDisputeMap[$r['waybill']])){{ isset($dfDisputeMap[$r['waybill']]['decision'])?'查看处理':'待处理异议' }}@else—@endif
当前筛选没有明细。
+@forelse($dfLines as $r)@foreach(['bill_weight','charge_weight','settlement_weight','weight_diff'] as $weightKey)@endforeach@empty@endforelse
快递单号发货 / 快递厂家重量(kg)我方计费重量(kg)结算重量(kg)重量差(kg)厂家运费我方核算最终结算运费差分类 / 说明厂家反馈
{{ $r['waybill'] }}{{ $r['ship_time'] }}
{{ $r['express'] }}
{{ $r[$weightKey]===null?'—':rtrim(rtrim(number_format($r[$weightKey],4,'.',''),'0'),'.') }}{{ $money($r['bill']) }}{{ $money($r['calc']) }}{{ $money($r['final']??$r['settle']) }}{{ $money($r['freight_diff']) }}{{ $r['label'] }}@if($r['diff_kind'])
{{ ['weight_diff'=>'重量差','price_diff'=>'价格差','missing_weight'=>'重量待补充'][$r['diff_kind']] }}
@endif
{{ $r['note'] }}
@if(isset($dfDisputeMap[$r['waybill']])){{ isset($dfDisputeMap[$r['waybill']]['decision'])?'查看处理':'待处理异议' }}@else—@endif
当前筛选没有明细。
{{ $dfLines->links() }}
@if(($dfSurchargeSummary??collect())->isNotEmpty())

附加费核对

@foreach($dfSurchargeSummary as $fee)@endforeach
项目厂家金额自算金额
{{ $fee->item }}{{ $money($fee->bill) }}{{ $money($fee->self) }}
@endif @include('business.pages.courier-payables.partials.surcharge-matches',['surchargeRows'=>$dfSurchargeRows??null]) diff --git a/tests/Feature/Business/CourierBillReconciliationTest.php b/tests/Feature/Business/CourierBillReconciliationTest.php index 570780425..7206e0e05 100644 --- a/tests/Feature/Business/CourierBillReconciliationTest.php +++ b/tests/Feature/Business/CourierBillReconciliationTest.php @@ -317,7 +317,8 @@ final class CourierBillReconciliationTest extends FeatureTestCase $this->assertSame('13800000000', (string) $recon->getCell('C2')->getValue()); $this->assertNotSame(DataType::TYPE_NUMERIC, $recon->getCell('C2')->getDataType()); $this->assertSame('ZT001', (string) $recon->getCell('G2')->getValue()); // 快递单号(25 列里的第 4 列) - $this->assertEquals(0, $recon->getCell('R2')->getValue()); // 运费差 = 10 − 核算 10,核算后新增结算列 + $freightDiffColumn = array_search('运费差', $header, true) + 1; + $this->assertEquals(0, $recon->getCell([$freightDiffColumn, 2])->getValue()); // 运费差 = 快递金额 − 结算金额 $book->disconnectWorksheets(); } diff --git a/tests/Feature/Business/CourierSettlementRulesTest.php b/tests/Feature/Business/CourierSettlementRulesTest.php index cc7eb383f..1776cecfc 100644 --- a/tests/Feature/Business/CourierSettlementRulesTest.php +++ b/tests/Feature/Business/CourierSettlementRulesTest.php @@ -9,6 +9,77 @@ use Tests\Feature\FeatureTestCase; class CourierSettlementRulesTest extends FeatureTestCase { + public function test_final_settlement_weight_and_differences_are_used_by_pages_and_exports(): void + { + Storage::fake('local'); + $this->user->attachPermission(\App\Models\Auth\Permission::where('name', 'read-admin-panel')->firstOrFail()); + $this->loginAs()->withExceptionHandling(); + $db = CourierBillMatcher::db(); + $store = app(CourierBillStore::class); + $group = $store->saveGroup(null, ['name' => '武进极兔', 'express_names' => ['武进极兔']]); + $shipping = app(\App\Support\CourierBills\ShippingLookup::class)->connection(); + $cases = [['WTLOW0001', 8, 2, 10, 5, 2, 0, 0], ['WTHIGH0001', 12, 7, 10, 5, 5, 2, 2], + ['WTEQUAL0001', 10, 4, 10, 5, 5, -1, 0], ['WTMISSING0001', 8, null, 10, 5, null, null, 0], + ['WTZERO0001', 0, 0, 10, 5, 0, 0, 0]]; + $csv = "单号,运费,重量\n"; + foreach ($cases as $i => [$waybill, $bill, $weight, $calc, $charge]) { + $shipping->table('jst_oms_shipping')->insert(['source_id' => $i + 1, 'express_no' => $waybill, + 'express_name1' => '武进极兔', 'ship_time' => '2026-11-03 09:00:00']); + $shipping->table('jst_oms_shipping_freight')->insert(['source_id' => $i + 1, 'status' => 'OK', 'freight' => $calc, 'charge_weight' => $charge]); + $csv .= "$waybill,$bill,$weight\n"; + } + $file = $store->upload([\Illuminate\Http\UploadedFile::fake()->createWithContent('武进极兔.csv', $csv)], '2026-11', [], '测试')[0]; + $id = (int) $db->table('courier_bill_files')->where('id', $file)->value('statement_id'); + $before = $db->table('courier_bill_lines')->where('statement_id', $id)->orderBy('id')->get()->toJson(); + $path = tempnam(sys_get_temp_dir(), 'settlement-weight'); + $out = fopen($path, 'wb'); + try { app(StatementExporter::class)->export($id, $out); } finally { fclose($out); } + try { + $book = IOFactory::load($path); + $grid = $book->getSheetByName('对账')->toArray(null, false, false); + $headers = array_shift($grid); + $rows = collect($grid)->keyBy(fn ($row) => $row[array_search('快递单号', $headers, true)]); + foreach ($cases as [$waybill, $bill, $weight, $calc, $charge, $expectedWeight, $expectedWeightDiff, $expectedFreightDiff]) { + $row = $rows[$waybill]; + $expectedWeight === null ? $this->assertNull($row[array_search('结算重量', $headers, true)]) + : $this->assertEquals($expectedWeight, $row[array_search('结算重量', $headers, true)]); + $expectedWeightDiff === null ? $this->assertNull($row[array_search('重量差', $headers, true)]) + : $this->assertEquals($expectedWeightDiff, $row[array_search('重量差', $headers, true)]); + $this->assertEquals($expectedFreightDiff, $row[array_search('运费差', $headers, true)]); + $this->assertEquals($charge, $row[array_search('计费重量', $headers, true)]); + $this->assertEquals($calc, $row[array_search('核算运费', $headers, true)]); + } + $book->disconnectWorksheets(); + } finally { @unlink($path); } + foreach (['courier-payables', 'courier-payables-final'] as $section) { + $this->get(route('business.pages.show', ['section' => $section, 'page' => 'courier-bill-reconciliation', + 'cb_view' => 'statement', 'statement' => $id, 'month' => '2026-11']))->assertOk()->assertSeeText('结算重量'); + $query = ['cb_view' => 'details', 'month' => '2026-11', 'group_id' => $group, 'resolution' => 'all']; + $this->get(route('business.pages.show', ['section' => $section, 'page' => 'courier-bill-reconciliation', ...$query])) + ->assertOk()->assertSeeText('结算重量')->assertSeeText('重量差'); + $response = $this->get(route("business.$section.courier-bills.export-details", $query))->assertOk(); + file_put_contents($path, $response->streamedContent()); + try { + $book = IOFactory::load($path);$grid = $book->getActiveSheet()->toArray(null, false, false);$headers = array_shift($grid); + $low = collect($grid)->first(fn ($row) => $row[array_search('快递单号', $headers, true)] === 'WTLOW0001'); + $this->assertEquals(2, $low[array_search('结算重量', $headers, true)]); + $this->assertEquals(0, $low[array_search('重量差', $headers, true)]); + $this->assertEquals(0, $low[array_search('运费差', $headers, true)]); + $book->disconnectWorksheets(); + } finally { @unlink($path); } + } + $this->assertSame($before, $db->table('courier_bill_lines')->where('statement_id', $id)->orderBy('id')->get()->toJson()); + foreach (['中铁物流', '武进安能'] as $name) { + $line = (object) ['bill_amount'=>8, 'bill_weight'=>2, 'calc_freight'=>10, 'charge_weight'=>5, + 'self_amount'=>CourierBillMatcher::selfAmount(Category::DIFF,10,8,null,$name), 'category'=>Category::DIFF, 'resolution'=>null, 'express_name'=>$name]; + $this->assertSame(['settlement_weight'=>5.0,'weight_diff'=>-3.0,'freight_diff'=>-2.0], \App\Support\CourierBills\SettlementComparison::courier($line)); + } + $line->resolution = 'agree'; $line->self_amount = 8; + $this->assertSame(['settlement_weight'=>2.0,'weight_diff'=>0.0,'freight_diff'=>0.0], \App\Support\CourierBills\SettlementComparison::courier($line)); + $line->resolution = 'exclude'; $line->self_amount = 0; + $this->assertSame(['settlement_weight'=>0.0,'weight_diff'=>2.0,'freight_diff'=>8.0], \App\Support\CourierBills\SettlementComparison::courier($line)); + } + public function test_freight_settlement_caps_at_billed_amount_but_keeps_missing_values_and_manual_decisions(): void { foreach ([Category::MATCH, Category::DIFF, Category::UNCALCULATED, Category::PRIOR] as $category) { diff --git a/tests/Feature/Business/DropshipWeightTest.php b/tests/Feature/Business/DropshipWeightTest.php index 72aadad7f..78c71d9db 100644 --- a/tests/Feature/Business/DropshipWeightTest.php +++ b/tests/Feature/Business/DropshipWeightTest.php @@ -12,6 +12,37 @@ use Tests\Feature\FeatureTestCase; class DropshipWeightTest extends FeatureTestCase { + public function test_lower_freight_also_uses_billed_weight_without_overwriting_original_calculation(): void + { + Storage::fake('local');$store=app(DropshipFreightStore::class);$shipping=app(ShippingLookup::class)->connection(); + $id=$store->saveFactory(null,['name'=>'重量测试','warehouse'=>'测试代发仓','keywords'=>'测试']); + $file=$store->upload([UploadedFile::fake()->createWithContent('重量测试.csv',"单号,运费,重量\nWTLOW0001,8,2\nWTMISSING0001,8,\nWTHIGH0001,12,7\n")], '2026-11', '测试')[0]; + foreach(['WTLOW0001','WTMISSING0001','WTHIGH0001'] as $i=>$no){ + $shipping->table('jst_oms_shipping')->insert(['source_id'=>$i+1,'express_no'=>$no,'express_name1'=>'测试快递','warehouse_name'=>'测试代发仓','ship_time'=>'2026-11-03 09:00:00']); + $shipping->table('jst_oms_shipping_freight')->insert(['source_id'=>$i+1,'status'=>'OK','freight'=>10,'charge_weight'=>5]); + } + $factory=collect($store->factories())->firstWhere('id',$id);$result=app(DropshipReconciler::class)->factoryMonth($factory,'2026-11'); + $rows=collect($result['rows'])->keyBy('waybill'); + $this->assertEquals([10,5,8,2,0,0],array_map(fn($key)=>$rows['WTLOW0001'][$key],['calc','charge_weight','final','settlement_weight','weight_diff','freight_diff'])); + $this->assertNull($rows['WTMISSING0001']['settlement_weight']);$this->assertNull($rows['WTMISSING0001']['weight_diff']); + $this->assertEquals([10,5,2,2],array_map(fn($key)=>$rows['WTHIGH0001'][$key],['final','settlement_weight','weight_diff','freight_diff'])); + $this->assertEquals(26,$result['summary']['settle']); + $path=tempnam(sys_get_temp_dir(),'drop-weight');$out=fopen($path,'wb'); + try{app(DropshipExporter::class)->export($factory,'2026-11',$out);}finally{fclose($out);} + try{ + $book=IOFactory::load($path);$grid=$book->getSheetByName('对账')->toArray(null,false,false);$headers=array_shift($grid); + $low=collect($grid)->first(fn($row)=>$row[array_search('快递单号',$headers,true)]==='WTLOW0001'); + $this->assertEquals(2,$low[array_search('结算重量',$headers,true)]); + $this->assertEquals(0,$low[array_search('重量差',$headers,true)]); + $this->assertEquals(0,$low[array_search('运费差',$headers,true)]); + $book->disconnectWorksheets(); + }finally{@unlink($path);} + $this->user->attachPermission(\App\Models\Auth\Permission::where('name','read-admin-panel')->firstOrFail());$this->loginAs(); + $this->get(route('business.pages.show',['section'=>'courier-payables','page'=>DropshipFreightController::PAGE,'df_view'=>'factory','factory'=>$id,'month'=>'2026-11'])) + ->assertOk()->assertSeeText('结算重量(kg)')->assertSeeText('运费差'); + $this->assertEquals(28,$store->db()->table('dropship_bill_lines')->where('file_id',$file)->sum('amount')); + } + public function test_parser_sums_weight_without_treating_partial_missing_as_zero_or_reading_system_weight(): void { $path = tempnam(sys_get_temp_dir(), 'weight') . '.csv'; -- 2.47.2