fix(运费对账): 结算重量紧跟核算运费 (#1790)

This commit is contained in:
limeng 2026-12-06 17:01:19 +08:00
parent aa7ec7aece
commit d26532dee4
9 changed files with 23 additions and 21 deletions

View File

@ -562,15 +562,16 @@ 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,
$comparison['settlement_weight'],
$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->charge_weight === null ? null : (float) $line->charge_weight, $comparison['weight_diff'],
$line->unshipped_sources, Category::RESOLUTIONS[$line->resolution] ?? null, $line->resolved_by]);
}
$book->writeTo(fopen('php://output', 'wb'));

View File

@ -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['freight_diff'], $r['bill_weight'], $r['charge_weight'], $r['settlement_weight'], $r['weight_diff'], $r['label'], $r['note'], $r['formula']]);
$book->row($sheet, [$r['waybill'], $r['ship_time'], $r['express'], $r['warehouse'], $r['bill'], $r['calc'], $r['settlement_weight'], $r['settle'],
$r['final'] ?? $r['settle'], $r['freight_diff'], $r['bill_weight'], $r['charge_weight'], $r['weight_diff'], $r['label'], $r['note'], $r['formula']]);
}
}
$book->writeTo(fopen('php://output', 'wb'));

View File

@ -19,7 +19,7 @@ final class StatementExporter
public const FRONT = [
'称重重量' => '_measured', '首重' => 'qc_first_price', '面单' => 'qc_miandan', '快递单号' => 'express_no',
'交易单号' => 'trade_no', '商品ID' => 'item_id', '条形码' => 'barcode', '商品名称' => 'product_name', '数量' => 'qty',
'计费重量' => 'qc_charge_weight', '结算重量' => '_settlement_weight', '续重/费率' => 'qc_rate', '核算运费' => 'qc_freight', '重量差' => '_weight_diff',
'计费重量' => 'qc_charge_weight', '续重/费率' => 'qc_rate', '核算运费' => 'qc_freight', '结算重量' => '_settlement_weight', '重量差' => '_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',
@ -111,7 +111,7 @@ final class StatementExporter
$front = [];
foreach (self::FRONT as $label => $field) {
$front[$label] = $field;
if ($field === 'qc_freight') $front['结算运费'] = '_settlement';
if ($field === '_settlement_weight') $front['结算运费'] = '_settlement';
}
$rest = array_diff_key(ShippingLookup::COLUMNS, array_flip(array_filter(self::FRONT, static fn ($f) => $f[0] !== '_')));
$rest['settle_freight'] = '发货数据结算运费';

View File

@ -315,14 +315,14 @@ final class DropshipExporter
}, $original);
}
/** 对账表前面几列:照快递对账单的,「核算运费」后面加「最终结算运费」。 @return array<string,string> */
/** 对账表前面几列:核算运费、结算重量、最终结算运费依次相邻。 @return array<string,string> */
private static function frontColumns(): array
{
$result = [];
foreach (StatementExporter::FRONT as $label => $field) {
if ($field === 'qc_charge_weight') $result['厂家重量(kg)'] = '_bill_weight';
$result[$label] = $field;
if ($field === 'qc_freight') {
if ($field === '_settlement_weight') {
$result['最终结算运费'] = '_final';
}
}

View File

@ -29,8 +29,8 @@
<thead><tr>
<th scope="col"><input type="checkbox" data-cbr-check-all aria-label="全选本页"></th>
<th scope="col">账单月份</th><th scope="col">对账分组</th><th scope="col">负责人</th><th scope="col">快递单号</th><th scope="col">分类</th>
<th scope="col" class="num">快递运费</th><th scope="col" class="num">核算运费</th><th scope="col" class="num">结算运费</th><th scope="col" class="num">运费差</th>
<th scope="col" class="num">快递重量</th><th scope="col" class="num">计费重量</th><th scope="col" class="num">结算重量</th><th scope="col" class="num">重量差</th>
<th scope="col" class="num">快递运费</th><th scope="col" class="num">核算运费</th><th scope="col" class="num">结算重量</th><th scope="col" class="num">结算运费</th><th scope="col" class="num">运费差</th>
<th scope="col" class="num">快递重量</th><th scope="col" class="num">计费重量</th><th scope="col" class="num">重量差</th>
<th scope="col">发货时间</th><th scope="col">发货数据快递名称1</th><th scope="col">处理结果</th><th scope="col"></th>
</tr></thead>
<tbody>
@ -41,10 +41,10 @@
<td>{{ $line->bill_month }}</td><td>{{ $line->group_name }}</td><td>{{ $line->owner }}</td><td>{{ $line->waybill_no }}</td>
<td>{{ \App\Support\CourierBills\Category::label((string) $line->category, $line->category_month) }}</td>
<td class="num">{{ $money($line->bill_amount) }}</td><td class="num">{{ $money($line->calc_freight) }}</td>
<td class="num">{{ $comparison['settlement_weight'] === null ? '' : round($comparison['settlement_weight'], 4) }}</td>
<td class="num">{{ $money($line->self_amount) }}</td><td class="num cbr-diff">{{ $money($comparison['freight_diff']) }}</td>
<td class="num">{{ $line->bill_weight === null ? '' : round((float) $line->bill_weight, 4) }}</td>
<td class="num">{{ $line->charge_weight === null ? '' : round((float) $line->charge_weight, 4) }}</td>
<td class="num">{{ $comparison['settlement_weight'] === null ? '' : round($comparison['settlement_weight'], 4) }}</td>
<td class="num">{{ $comparison['weight_diff'] ?? '' }}</td>
<td>{{ $line->ship_time ? substr((string) $line->ship_time, 0, 16) : '' }}</td><td>{{ $line->express_name }}</td>
<td>{{ \App\Support\CourierBills\Category::RESOLUTIONS[$line->resolution] ?? '' }}{{ $line->statement_status === 'confirmed' ? '(已确认结算)' : '' }}</td>

View File

@ -111,7 +111,7 @@
@else
@if ($open)<th scope="col"><input type="checkbox" data-cbr-check-all aria-label="全选本页"></th>@endif
<th scope="col">快递单号</th><th scope="col" class="num">快递运费</th><th scope="col" class="num">快递重量</th>
<th scope="col">发货时间</th><th scope="col">发货数据快递名称1</th><th scope="col" class="num">核算运费</th><th scope="col" class="num">结算运费</th><th scope="col" class="num">计费重量</th><th scope="col" class="num">结算重量</th>
<th scope="col">发货时间</th><th scope="col">发货数据快递名称1</th><th scope="col" class="num">核算运费</th><th scope="col" class="num">结算重量</th><th scope="col" class="num">结算运费</th><th scope="col" class="num">计费重量</th>
<th scope="col" class="num">运费差</th><th scope="col" class="num">重量差</th><th scope="col">未发出来源</th><th scope="col">处理结果</th>
@endif
</tr></thead>
@ -129,9 +129,9 @@
<td>{{ $line->ship_time ? substr((string) $line->ship_time, 0, 16) : '' }}</td>
<td>{{ $line->express_name }}</td>
<td class="num">{{ $money($line->calc_freight) }}</td>
<td class="num">{{ $comparison['settlement_weight'] === null ? '' : round($comparison['settlement_weight'], 4) }}</td>
<td class="num">{{ $money($line->self_amount) }}</td>
<td class="num">{{ $line->charge_weight === null ? '' : rtrim(rtrim(number_format((float) $line->charge_weight, 3), '0'), '.') }}</td>
<td class="num">{{ $comparison['settlement_weight'] === null ? '' : round($comparison['settlement_weight'], 4) }}</td>
<td class="num cbr-diff">{{ $money($comparison['freight_diff']) }}</td>
<td class="num">{{ $comparison['weight_diff'] ?? '' }}</td>
<td>{{ $line->unshipped_sources }}</td>

View File

@ -70,8 +70,8 @@ $feedbackPageFor = function (string $waybill) use ($dfCurrentDisputes): int {
<nav class="cbr-tabs" aria-label="运费差异原因">@foreach(['weight_diff'=>'重量差','price_diff'=>'价格差','missing_weight'=>'重量待补充'] as $key=>$label)<a data-cbr-tab href="{{ $pageUrl($base+['status'=>$key]) }}" class="{{ $dfStatus===$key?'is-active':'' }}">{{ $label }} {{ $s['diff_counts'][$key] }}</a>@endforeach</nav>
<p class="cbr-muted">运费差 = 厂家运费 − 最终结算运费;重量差 = 厂家重量 − 结算重量(公斤)。按厂家较低运费结算时,结算重量也取厂家重量;厂家未提供重量则留空。上方差异原因仍按原核算值识别。</p>
<section class="cbr-panel"><h3>逐单核算明细</h3><form method="GET" action="{{ $pageUrl() }}" class="cbr-form cbr-panel-body" data-cbr-filter><input type="hidden" name="df_view" value="factory"><input type="hidden" name="factory" value="{{ $dfFactory->id }}"><input type="hidden" name="month" value="{{ $month }}"><input type="hidden" name="status" value="{{ $dfStatus }}"><input name="q" value="{{ $dfSearch }}" placeholder="搜索快递单号" aria-label="搜索快递单号"><button>搜索</button></form>
<div class="cbr-scroll"><table><thead><tr><th>快递单号</th><th>发货 / 快递</th><th class="num">厂家重量(kg)</th><th class="num">我方计费重量(kg)</th><th class="num">结算重量(kg)</th><th class="num">重量差(kg)</th><th class="num">厂家运费</th><th class="num">我方核算</th><th class="num">最终结算</th><th class="num">运费差</th><th>分类 / 说明</th><th>厂家反馈</th></tr></thead><tbody>
@forelse($dfLines as $r)<tr><td>{{ $r['waybill'] }}</td><td>{{ $r['ship_time'] }}<div class="cbr-muted">{{ $r['express'] }}</div></td>@foreach(['bill_weight','charge_weight','settlement_weight','weight_diff'] as $weightKey)<td class="num">{{ $r[$weightKey]===null?'—':rtrim(rtrim(number_format($r[$weightKey],4,'.',''),'0'),'.') }}</td>@endforeach<td class="num">{{ $money($r['bill']) }}</td><td class="num">{{ $money($r['calc']) }}</td><td class="num">{{ $money($r['final']??$r['settle']) }}</td><td class="num cbr-diff">{{ $money($r['freight_diff']) }}</td><td class="wrap">{{ $r['label'] }}@if($r['diff_kind'])<div class="cbr-muted">{{ ['weight_diff'=>'重量差','price_diff'=>'价格差','missing_weight'=>'重量待补充'][$r['diff_kind']] }}</div>@endif<div class="cbr-muted">{{ $r['note'] }}</div></td><td>@if(isset($dfDisputeMap[$r['waybill']]))<a data-cbr-tab href="{{ $pageUrl(array_merge($base, request()->query(), ['feedback_page' => $feedbackPageFor((string)$r['waybill'])])) }}#feedback-{{ md5($r['waybill']) }}">{{ isset($dfDisputeMap[$r['waybill']]['decision'])?'查看处理':'待处理异议' }}</a>@else<span class="cbr-muted">—</span>@endif</td></tr>@empty<tr><td colspan="12">当前筛选没有明细。</td></tr>@endforelse
<div class="cbr-scroll"><table><thead><tr><th>快递单号</th><th>发货 / 快递</th><th class="num">厂家重量(kg)</th><th class="num">我方计费重量(kg)</th><th class="num">重量差(kg)</th><th class="num">厂家运费</th><th class="num">我方核算</th><th class="num">结算重量(kg)</th><th class="num">最终结算</th><th class="num">运费差</th><th>分类 / 说明</th><th>厂家反馈</th></tr></thead><tbody>
@forelse($dfLines as $r)<tr><td>{{ $r['waybill'] }}</td><td>{{ $r['ship_time'] }}<div class="cbr-muted">{{ $r['express'] }}</div></td>@foreach(['bill_weight','charge_weight','weight_diff'] as $weightKey)<td class="num">{{ $r[$weightKey]===null?'—':rtrim(rtrim(number_format($r[$weightKey],4,'.',''),'0'),'.') }}</td>@endforeach<td class="num">{{ $money($r['bill']) }}</td><td class="num">{{ $money($r['calc']) }}</td><td class="num">{{ $r['settlement_weight']===null?'—':rtrim(rtrim(number_format($r['settlement_weight'],4,'.',''),'0'),'.') }}</td><td class="num">{{ $money($r['final']??$r['settle']) }}</td><td class="num cbr-diff">{{ $money($r['freight_diff']) }}</td><td class="wrap">{{ $r['label'] }}@if($r['diff_kind'])<div class="cbr-muted">{{ ['weight_diff'=>'重量差','price_diff'=>'价格差','missing_weight'=>'重量待补充'][$r['diff_kind']] }}</div>@endif<div class="cbr-muted">{{ $r['note'] }}</div></td><td>@if(isset($dfDisputeMap[$r['waybill']]))<a data-cbr-tab href="{{ $pageUrl(array_merge($base, request()->query(), ['feedback_page' => $feedbackPageFor((string)$r['waybill'])])) }}#feedback-{{ md5($r['waybill']) }}">{{ isset($dfDisputeMap[$r['waybill']]['decision'])?'查看处理':'待处理异议' }}</a>@else<span class="cbr-muted">—</span>@endif</td></tr>@empty<tr><td colspan="12">当前筛选没有明细。</td></tr>@endforelse
</tbody></table></div><div class="cbr-pager">{{ $dfLines->links() }}</div></section>
@if(($dfSurchargeSummary??collect())->isNotEmpty())<section class="cbr-panel"><h3>附加费核对</h3><div class="cbr-scroll"><table><thead><tr><th>项目</th><th>厂家金额</th><th>自算金额</th></tr></thead><tbody>@foreach($dfSurchargeSummary as $fee)<tr><td>{{ $fee->item }}</td><td>{{ $money($fee->bill) }}</td><td>{{ $money($fee->self) }}</td></tr>@endforeach</tbody></table></div></section>@endif
@include('business.pages.courier-payables.partials.surcharge-matches',['surchargeRows'=>$dfSurchargeRows??null])

View File

@ -141,7 +141,8 @@ class CourierSettlementRulesTest extends FeatureTestCase
try {
$book=IOFactory::load($path);$grid=$book->getSheetByName('对账')->toArray(null,false,false);$headers=$grid[0];
$calcCol=array_search('核算运费',$headers,true);$settleCol=array_search('结算运费',$headers,true);
$this->assertSame($calcCol+1,$settleCol);$this->assertEquals(10,$grid[1][$settleCol]);
$this->assertSame('结算重量',$headers[$calcCol+1]);
$this->assertSame($calcCol+2,$settleCol);$this->assertEquals(10,$grid[1][$settleCol]);
$this->assertSame(1,count(array_filter($headers,fn($h)=>$h==='结算运费')));$book->disconnectWorksheets();
} finally {@unlink($path);}
}

View File

@ -180,14 +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]);
$this->assertSame(['结算重量', '最终结算运费'], array_slice($head, $at + 1, 2));
$under = collect($recon)->first(fn ($r) => $r[0] === '773444000003'); // 厂家 5、核算 6 → 按 5 结,运费差 0
$this->assertEquals([6, 5], [$under[$at], $under[$at + 1]]);
$this->assertEquals([6, 5], [$under[$at], $under[$at + 2]]);
$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
$this->assertEquals([5, 5, 1], [$row[$at], $row[$at + 2], $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('汇总');