fix: 报价签署原件仅展示横向新价格并修复缺失字形 (#1184)

This commit is contained in:
limeng 2026-11-16 09:31:03 +08:00
parent e12c9c20ec
commit 167211490b
5 changed files with 71 additions and 37 deletions

View File

@ -274,7 +274,7 @@ class CourierQuoteSignatures extends Controller
}
/**
* 价格表右侧加一列「备注」,每条计费条件占一行;条件比省份多时在表尾补行。
* 复用价格表已有的「备注」列,缺少时再添加;条件比省份多时在表尾补行。
*
* @param array{table:list<list<mixed>>,notes:list<string>,row_keys:list<int|string>} $layout
* @return array{rows:list<list<mixed>>,row_keys:list<int|string|null>}
@ -287,12 +287,19 @@ class CourierQuoteSignatures extends Controller
}
$notes = $layout['notes'];
$width = count($table[0]);
$hasNoteColumn = preg_match('/^(备注|说明)[::]?$/u', trim((string) $table[0][$width - 1])) === 1;
$rows = [];
$keys = [];
$total = max(count($table), count($notes) + 1);
for ($i = 0; $i < $total; $i++) {
$row = $table[$i] ?? array_fill(0, $width, null);
$row[] = $i === 0 ? '备注' : ($notes[$i - 1] ?? null);
if ($hasNoteColumn) {
if ($i > 0 && filled($notes[$i - 1] ?? null)) {
$row[$width - 1] = implode("\n", array_filter([$row[$width - 1], $notes[$i - 1]], static fn ($value) => $value !== null && $value !== ''));
}
} else {
$row[] = $i === 0 ? '备注' : ($notes[$i - 1] ?? null);
}
$rows[] = $row;
$keys[] = $layout['row_keys'][$i] ?? null;
}

View File

@ -9,7 +9,7 @@ final class CourierQuoteConfirmation
public function data(CourierQuoteVersion $version, ?string $signingSubject = null): array
{
$partyA = trim((string) $signingSubject) ?: trim((string) config('services.esign.default_initiator'));
$version->loadMissing(['change.items']);
$version->loadMissing(['change']);
$values = $this->decodeMatrix($version->values_json);
$nextVersion = CourierQuoteVersion::query()
->where('carrier_name', $version->carrier_name)

View File

@ -29,11 +29,12 @@
th { background: #eee8db; font-weight: 400; }
.number { text-align: right; }
.matrix { font-size: 7.5px; table-layout: auto; }
.matrix thead { display: table-header-group; }
.matrix tr { page-break-inside: avoid; }
.matrix th, .matrix td { max-width: 90px; padding: 3px 4px; }
.matrix .matrix__note { width: 30%; max-width: none; background: #faf6ea; }
.matrix th.matrix__note { background: #eee8db; }
.formula { color: #735c31; font-size: 7px; }
.summary { white-space: pre-wrap; }
.signature { page-break-before: always; }
.signature h2 { margin-top: 0; }
.signature__meta { margin-bottom: 18px; color: #555; }
@ -47,15 +48,19 @@
$mixedText = static function ($value): string {
// Spreadsheet-pasted whitespace has no glyph in the CJK font.
$text = str_replace(["\r\n", "\r", "\t", "\u{00A0}"], ["\n", "\n", ' ', ' '], (string) $value);
$parts = preg_split('/([\x20-\x7E]+)/', $text, -1, PREG_SPLIT_DELIM_CAPTURE) ?: [];
// Keep real line breaks without reproducing spreadsheet alignment padding.
$text = preg_replace('/[ \x{3000}]+/u', ' ', $text) ?? $text;
// Droid Sans Fallback also lacks common dash and currency glyphs.
$latinCharacters = '\x20-\x7E\x{00A5}\x{00B0}\x{00B7}\x{00D7}\x{00F7}\x{2010}-\x{2015}\x{2212}';
$parts = preg_split('/([' . $latinCharacters . ']+)/u', $text, -1, PREG_SPLIT_DELIM_CAPTURE) ?: [];
return implode('', array_map(static function (string $part): string {
return nl2br(implode('', array_map(static function (string $part) use ($latinCharacters): string {
$escaped = e($part);
return preg_match('/^[\x20-\x7E]+$/', $part) === 1
return preg_match('/^[' . $latinCharacters . ']+$/u', $part) === 1
? '<span class="latin">' . $escaped . '</span>'
: $escaped;
}, $parts));
}, $parts)), false);
};
@endphp
<header class="heading">
@ -82,7 +87,7 @@
</tr>
<tr>
<th>执行期间</th><td>{!! $mixedText($version->periodLabel()) !!}</td>
<th>区间截止 / 有效期</th><td>{!! $mixedText($version->isTemporary() ? '以左侧各区间为准,区间外恢复原报价' : $expiryDate) !!}</td>
<th>{!! $mixedText('区间截止 / 有效期') !!}</th><td>{!! $mixedText($version->isTemporary() ? '以左侧各区间为准,区间外恢复原报价' : $expiryDate) !!}</td>
<th>报价编号</th><td>{!! $mixedText($version->version_code) !!}</td>
</tr>
<tr>
@ -98,40 +103,19 @@
@endif
<div class="notice">本文件由财务系统根据已审核报价版本自动生成。乙方签章后文件与完成证书将按本版本单独归档;后续变价须生成新版本并重新签章,旧确认单不会被覆盖。</div>
@if($change)
<h2>变价说明</h2>
<p class="summary">{!! $mixedText($change->summary) !!}</p>
<h2>新旧价格差异</h2>
<table>
<thead><tr><th>单元格</th><th>{!! $mixedText('地区/规则') !!}</th><th>{!! $mixedText('重量段/计费项') !!}</th><th class="number">原价格</th><th class="number">新价格</th><th>单位</th><th>备注</th></tr></thead>
<tbody>
@foreach($change->items as $item)
<tr>
<td>{!! $mixedText($item->source_cell) !!}</td>
<td>{!! $mixedText($item->region ?: '—') !!}</td>
<td>{!! $mixedText($item->weight_band ?: ($item->charge_item ?: '—')) !!}</td>
<td class="number">{!! $mixedText(rtrim(rtrim($item->original_amount, '0'), '.')) !!}</td>
<td class="number">{!! $mixedText(rtrim(rtrim($item->new_amount, '0'), '.')) !!}</td>
<td>{!! $mixedText($item->unit ?: '—') !!}</td>
<td>{!! $mixedText($item->note ?: '—') !!}</td>
</tr>
@endforeach
</tbody>
</table>
@endif
<h2>完整报价规则</h2>
<h2>报价明细(执行价格)</h2>
@if(! empty($billingRules))<p class="summary">{!! $mixedText($billingRules) !!}</p>@endif
@php
// 价格表右侧加「备注」列:价格表以外的计费条件(执行时间、计泡、起算重量、报价备注等)逐行列出,随确认单一起盖章。
// 价格表保留原有横向公斤段;其他计费条件合入备注,随确认单一起盖章。
$quoteLayout = \App\Http\Controllers\Business\CourierQuoteSignatures::quoteTableWithNotes(\App\Http\Controllers\Business\CourierQuoteSignatures::quoteLayout($values));
@endphp
<table class="matrix">
<tbody>
@if($quoteLayout['rows'] !== [])
@foreach($quoteLayout['rows'] as $rowIndex => $row)
@php($rowKey = $quoteLayout['row_keys'][$rowIndex] ?? null)
@php($lastColumn = count($row) - 1)
@if($rowIndex === 0)<thead>@endif
@if($rowIndex === 1)<tbody>@endif
<tr>
@foreach($row as $columnIndex => $cell)
@php($formula = $rowKey !== null && $columnIndex < $lastColumn ? ($formulas[$rowKey][$columnIndex] ?? null) : null)
@ -141,8 +125,11 @@
</{{ $rowIndex === 0 ? 'th' : 'td' }}>
@endforeach
</tr>
@if($rowIndex === 0)</thead>@endif
@endforeach
@if(count($quoteLayout['rows']) > 1)</tbody>@endif
@else
<tbody>
@forelse($values as $rowIndex => $row)
<tr>
@foreach($row as $columnIndex => $cell)
@ -156,8 +143,8 @@
@empty
<tr><td>该版本未保存可展示的报价矩阵。</td></tr>
@endforelse
</tbody>
@endif
</tbody>
</table>
<section class="signature">

View File

@ -92,6 +92,46 @@ class CourierQuoteSignatureWorkflowTest extends FeatureTestCase
$this->assertStringNotContainsString($this->company->name, $rendered);
}
public function test_signature_pdf_contains_only_approved_prices_in_horizontal_weight_columns(): void
{
$version = $this->createVersion();
$version->update([
'values_json' => json_encode([
['示例快递报价'],
['省份', '0-3公斤', '3-35公斤', '单位', '备注'],
['江苏', '0.5000', '0.5300', '元/公斤', '按实际重量计费'],
['安徽', 0, '0.6300', '元/公斤', null],
['计泡系数:1.2'],
], JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR),
'formulas_json' => '[]',
]);
$version->change->update(['summary' => '内部审核备注,不发送给快递']);
$version->change->items()->update(['original_amount' => '987.6543', 'note' => '内部差额核对']);
$text = [];
$pdf = app('dompdf.wrapper');
$pdf->getDomPDF()->setCallbacks([['event' => 'end_frame', 'f' => static function ($frame) use (&$text): void {
if ($frame instanceof \Dompdf\FrameDecorator\Text) $text[] = $frame->get_text();
}]]);
app()->instance('dompdf.wrapper', $pdf);
$signature = app(CourierQuoteSignatureService::class)->prepareVersion($version, $this->user->id);
$rendered = implode('', $text);
foreach (['0-3公斤', '3-35公斤', '江苏', '安徽', '0.5000', '0.5300', '0.6300', '元/公斤', '按实际重量计费', '计泡系数:1.2'] as $expected) {
$this->assertStringContainsString($expected, $rendered);
}
foreach (['新旧价格差异', '原价格', '差额', '单元格', '987.6543', '内部审核备注', '内部差额核对'] as $internal) {
$this->assertStringNotContainsString($internal, $rendered);
}
$this->assertStringStartsWith('%PDF-', Storage::disk('uploads')->get($signature->source_pdf_path));
$table = \App\Http\Controllers\Business\CourierQuoteSignatures::quoteTableWithNotes(
\App\Http\Controllers\Business\CourierQuoteSignatures::quoteLayout(json_decode($version->values_json, true))
);
$this->assertSame(['省份', '0-3公斤', '3-35公斤', '单位', '备注'], $table['rows'][0]);
$this->assertSame(['江苏', '0.5000', '0.5300', '元/公斤', "按实际重量计费\n计泡系数:1.2"], $table['rows'][1]);
$this->assertSame(0, $table['rows'][2][1]);
}
public function test_admin_can_push_the_exact_pdf_to_esign_sandbox(): void
{
$signature = $this->createPreparedSignature();

View File

@ -21,11 +21,11 @@ class CourierQuotePdfWhitespaceTest extends TestCase
'quote_type' => '普通报价', 'effective_date' => '2026-11-01',
]),
'change' => (object) ['summary' => '按原报价执行。', 'approved_at' => now(), 'items' => []],
'values' => [['地区', '费用'], ['江苏', '6.50']],
'values' => [['地区', '费用', '单位', '备注'], ['江苏', '6.50', '元/单', '—'], ['安徽', '0.00', null, null]],
'formulas' => [],
'partyA' => '示例甲方', 'expiryDate' => '长期',
'faceFee' => '0.50', 'bubbleCoefficient' => '1.2',
'billingRules' => "1、\u{00A0}计费重 > 0.5kg,费用6.50元。\t\t\t\r\n2、\u{00A0}轻抛比1:10000。\t\t\t\r3、保留末行。",
'billingRules' => "1、\u{00A0}计费重 > 0.5kg,费用6.50元。\t\t\t\r\n2、\u{00A0}轻抛比1:10000。\t\t\t\r3、保留末行。0–3kg — 费用 ¥6.50。",
'signatureDate' => '2026年11月7日', 'generatedAt' => now(),
'fontPath' => str_replace('\\', '/', resource_path('fonts/DroidSansFallback.ttf')),
])->render();