fix: 报价执行日期统一、签署价格两位小数及变价备注编辑 #1189

Merged
limeng merged 1 commits from fix/limeng-quote-effective-notes into main 2026-11-16 10:22:31 +08:00
15 changed files with 276 additions and 14 deletions

View File

@ -364,6 +364,8 @@ class CourierQuoteChanges extends Controller
'change_type' => ['required', 'in:费率调整,面单调整,规则备注,执行日期,其他'],
'effective_date' => ['required', 'date_format:Y-m-d'],
'summary' => ['required', 'string', 'min:2', 'max:2000'],
'quote_notes' => ['sometimes', 'array', 'max:500'],
'quote_notes.*' => ['nullable', 'string', 'max:10000'],
'items' => ['required', 'array', 'min:1', 'max:500'],
'items.*.source_cell' => ['required', 'regex:/^[A-Z]+[1-9][0-9]*$/'],
'items.*.source_row' => ['required', 'integer', 'min:0'],
@ -393,6 +395,7 @@ class CourierQuoteChanges extends Controller
'quotes' => $quotes,
'source' => $source,
'editableCells' => $this->editableCells($source, $existingItems),
'editableNotes' => \App\Support\CourierQuoteNotes::cells($source, $change?->quote_payload['quote_notes'] ?? []),
];
}

View File

@ -140,10 +140,9 @@ class CourierQuoteSignatures extends Controller
*/
public function sheet(CourierQuoteVersion $quoteVersion): StreamedResponse
{
$values = json_decode((string) $quoteVersion->values_json, true);
$layout = self::quoteLayout(is_array($values) ? $values : []);
$confirmation = app(\App\Support\CourierQuoteConfirmation::class)->data($quoteVersion);
$layout = self::quoteLayout($confirmation['values']);
if ($quoteVersion->isTemporary()) {
$confirmation = app(\App\Support\CourierQuoteConfirmation::class)->data($quoteVersion);
array_unshift($layout['notes'], $confirmation['temporary_note']);
}
$table = self::quoteTableWithNotes($layout)['rows'];
@ -155,8 +154,9 @@ class CourierQuoteSignatures extends Controller
foreach ($table as $rowIndex => $row) {
foreach ($row as $columnIndex => $value) {
$cell = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::stringFromColumnIndex($columnIndex + 1) . ($rowIndex + 1);
if (is_int($value) || is_float($value)) {
$sheet->setCellValue($cell, $value);
if ($rowIndex > 0 && $columnIndex > 0 && $columnIndex < count($table[0]) - 1 && is_numeric($value)) {
$sheet->setCellValue($cell, (float) $value);
$sheet->getStyle($cell)->getNumberFormat()->setFormatCode('0.00');
} elseif ($value !== null && $value !== '') {
$sheet->setCellValueExplicit($cell, (string) $value, \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING);
}

View File

@ -71,7 +71,7 @@ class CourierQuoteChange extends Model
public function hasQuotePayload(): bool
{
return is_array($this->quote_payload) && array_diff_key($this->quote_payload, ['execution_periods' => true]) !== [];
return is_array($this->quote_payload) && array_diff_key($this->quote_payload, ['execution_periods' => true, 'quote_notes' => true]) !== [];
}
public function isStockQuote(): bool

View File

@ -84,6 +84,17 @@ final class CourierQuoteChangeExport
}
}
$this->style($details, 8, max(7, count($cells) + 7), 7);
if ($change->quote_payload['quote_notes'] ?? []) {
$notes = $this->sheet($book, $change, '报价备注变更');
foreach (['地区', '原备注', '新备注'] as $index => $label) $this->text($notes, Coordinate::stringFromColumnIndex($index + 1) . '7', $label);
foreach (array_values($change->quote_payload['quote_notes']) as $index => $note) {
foreach ([$note['region'], $note['original'], $note['text']] as $column => $text) $this->text($notes, Coordinate::stringFromColumnIndex($column + 1) . ($index + 8), $text);
}
$this->style($notes, 3, 7 + count($change->quote_payload['quote_notes']), 7);
$notes->getColumnDimension('B')->setWidth(60);
$notes->getColumnDimension('C')->setWidth(60);
$notes->getStyle('B8:C' . (7 + count($change->quote_payload['quote_notes'])))->getAlignment()->setWrapText(true);
}
$details->getColumnDimension('B')->setWidth(30);
$details->getColumnDimension('G')->setWidth(48);
$details->getStyle('G8:G' . max(8, count($cells) + 7))->getAlignment()->setWrapText(true);

View File

@ -28,6 +28,7 @@ final class CourierQuoteChangeService
if (($data['change_type'] ?? '') !== '存量报价') {
$data = CourierQuotePeriods::normalize($data);
$data['quote_payload'] = CourierQuotePeriods::payload($data['quote_payload'] ?? null, $data['execution_periods']);
$data['quote_payload'] = CourierQuoteNotes::payload($data['quote_payload'], $data, $source);
}
return DB::transaction(function () use ($data, $userId, $source): CourierQuoteChange {
@ -70,6 +71,7 @@ final class CourierQuoteChangeService
if (!$change->isStockQuote()) {
$data = CourierQuotePeriods::normalize($data + ['end_date' => $change->end_date?->toDateString()]);
$data['quote_payload'] = CourierQuotePeriods::payload($change->quote_payload, $data['execution_periods']);
$data['quote_payload'] = CourierQuoteNotes::payload($data['quote_payload'], $data, $source);
}
return DB::transaction(function () use ($change, $data, $userId, $source): CourierQuoteChange {
@ -101,7 +103,7 @@ final class CourierQuoteChangeService
{
$this->ensureEditableBy($change, $userId);
if (! ($change->isStockQuote() && filled($change->proposed_values_json)) && $change->items()->count() === 0) {
if (! ($change->isStockQuote() && filled($change->proposed_values_json)) && $change->items()->count() === 0 && empty($change->quote_payload['quote_notes'])) {
throw ValidationException::withMessages(['items' => '至少需要一条发生变化的报价明细。']);
}
@ -198,6 +200,7 @@ final class CourierQuoteChangeService
throw ValidationException::withMessages(['stock_signature_verified' => '尚未上传历史签署资料,请补充资料或取消历史资料核验;不影响存量报价审核生效。']);
}
$historicalSigned = $hasHistoricalFiles && $historicalDocumentsConfirmed;
$source = CourierQuoteNotes::apply($source, $change->quote_payload['quote_notes'] ?? []);
$values = $source['values'];
$formulas = $source['formulas'];
@ -335,7 +338,7 @@ final class CourierQuoteChangeService
]);
}
if ($change->items()->count() === 0) {
if ($change->items()->count() === 0 && empty($change->quote_payload['quote_notes'])) {
throw ValidationException::withMessages(['items' => '至少需要一条原价格与新价格不同的变价明细。']);
}
}
@ -382,6 +385,7 @@ final class CourierQuoteChangeService
'effective_date' => $change->effective_date->toDateString(),
'end_date' => $change->end_date?->toDateString(),
'execution_periods' => $change->executionPeriods(),
'quote_notes' => $change->quote_payload['quote_notes'] ?? [],
'is_retrospective' => $change->isRetrospective(),
'recalculation_status' => $change->recalculation_status,
'recalculation_note' => $change->recalculation_note,

View File

@ -56,7 +56,8 @@ final class CourierQuoteConfirmation
: null,
'face_fee' => $this->matrixMetric($values, ['面单费', '面单费用', '面单']),
'bubble_coefficient' => $this->matrixMetric($values, ['计泡系数', '泡系数', '计泡']),
'values' => $values,
'billing_rules' => CourierQuoteDocument::executionText($version->billing_rules, $version->periodLabel()),
'values' => CourierQuoteDocument::values($values, $version->periodLabel()),
'formulas' => $this->decodeMatrix($version->formulas_json),
];
}

View File

@ -0,0 +1,33 @@
<?php
namespace App\Support;
/** Display the current quotation's execution periods without rewriting the saved source. */
final class CourierQuoteDocument
{
public static function executionText(?string $text, string $periodLabel): string
{
$date = '\d{4}(?:\h*年\h*\d{1,2}\h*月\h*\d{1,2}\h*日?|[-./]\d{1,2}[-./]\d{1,2})';
$interval = $date . '(?:\h*(?:至|到|~|~|—|-)\h*' . $date . ')?';
$periods = $interval . '(?:\h*[;;]\h*' . $interval . ')*(?:\h*[((]临时[))])?';
$label = '(?:[本此该](?:次)?(?:报价|价格)\h*(?:的\h*)?(?:(?:执行|生效)(?:日期|时间|期间)?\h*[::]?\h*)?(?:[自从于]\h*)?'
. '|(?:报价)?(?:执行|生效)(?:日期|时间|期间)\h*[::]?\h*)';
return preg_replace('#' . $label . $periods . '(?:\h*(?:起(?:开始)?(?:执行|生效)?|(?:开始)?(?:执行|生效)))?。?#u',
'本报价执行期间:' . $periodLabel . '。', $text ?? '') ?? ($text ?? '');
}
public static function values(array $values, string $periodLabel): array
{
foreach ($values as &$row) {
if (! is_array($row)) continue;
foreach ($row as &$value) {
if (is_string($value) && ! is_numeric($value)) $value = self::executionText($value, $periodLabel);
}
unset($value);
}
unset($row);
return $values;
}
}

View File

@ -0,0 +1,62 @@
<?php
namespace App\Support;
use App\Http\Controllers\Business\CourierQuoteSignatures;
use Illuminate\Validation\ValidationException;
/** Editable cells in the actual quotation's remarks column, separate from price-item notes. */
final class CourierQuoteNotes
{
public static function cells(array $source, array $saved = []): array
{
$layout = CourierQuoteSignatures::quoteLayout($source['values']);
$header = $layout['table'][0] ?? [];
$column = count($header) - 1;
if ($column < 0 || preg_match('/^(备注|说明)[::]?$/u', trim((string) $header[$column])) !== 1) return [];
$cells = [];
foreach (array_slice($layout['row_keys'], 1) as $row) {
if (filled($source['formulas'][$row][$column] ?? null)) continue;
$original = (string) ($source['values'][$row][$column] ?? '');
$cells[$row] = ['source_row' => $row, 'source_column' => $column, 'original' => $original,
'text' => $saved[$row]['text'] ?? $original, 'region' => (string) ($source['values'][$row][0] ?? '')];
}
return $cells;
}
public static function payload(?array $payload, array $data, array $source): ?array
{
if (! array_key_exists('quote_notes', $data)) return $payload;
$cells = self::cells($source);
$changes = [];
foreach ($data['quote_notes'] as $row => $value) {
if (! isset($cells[$row])) throw ValidationException::withMessages(['quote_notes' => '报价备注位置已变化,请重新载入后修改。']);
$text = (string) ($value ?? '');
if ($text !== $cells[$row]['original']) $changes[$row] = [...$cells[$row], 'text' => $text];
}
$payload = $payload ?? [];
unset($payload['quote_notes']);
if ($changes !== []) $payload['quote_notes'] = $changes;
return $payload ?: null;
}
public static function apply(array $source, array $notes): array
{
$cells = self::cells($source);
foreach ($notes as $row => $note) {
if (! isset($cells[$row]) || $cells[$row]['original'] !== $note['original']) {
throw ValidationException::withMessages(['quote_notes' => '原报价备注已变化,请退回后重新核对。']);
}
$source['values'][$row][$cells[$row]['source_column']] = $note['text'];
// The legacy import also stores the same general remarks above the matrix.
if (filled($source['billing_rules'] ?? null) && trim($source['billing_rules']) === trim($note['original'])) {
$source['billing_rules'] = $note['text'];
}
}
return $source;
}
}

View File

@ -24,7 +24,7 @@ final class CourierQuotePdf
'partyA' => $data['party_a'],
'expiryDate' => $data['expiry_date'],
'temporaryNote' => $data['temporary_note'] ?? null,
'billingRules' => $version->billing_rules,
'billingRules' => $data['billing_rules'],
'faceFee' => $data['face_fee'],
'bubbleCoefficient' => $data['bubble_coefficient'],
'signatureDate' => $signatureDate,

View File

@ -74,6 +74,8 @@
.quote-change-cell-note summary { cursor: pointer; }
.quote-change-cell-note summary:focus-visible { outline: 2px solid var(--qc-gold); }
.quote-change-note-input { width: 100%; height: 30px; margin-top: 5px; border: 1px solid var(--qc-border); border-radius: 6px; padding: 0 8px; color: var(--qc-ink); font: inherit; }
.quote-change-table .quote-change-remarks { min-width: 360px; width: 360px; }
.quote-change-remarks textarea { min-height: 90px; font-size: 12px; line-height: 1.6; }
.quote-change-footer { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-top: 16px; padding: 12px 16px; border: 1px solid var(--qc-border); border-radius: 12px; background: #fff; }
.quote-change-footer__summary { color: var(--qc-muted); font-size: 12px; }
.quote-change-footer__summary strong { color: var(--qc-ink); font-variant-numeric: tabular-nums; }
@ -86,6 +88,9 @@
.quote-change-footer { align-items: stretch; flex-direction: column; }
.quote-change-footer__actions { justify-content: flex-end; }
}
@media (max-width: 600px) {
.quote-change-table .quote-change-remarks { min-width: 160px; width: 160px; }
}
</style>
<main class="quote-change-workspace" data-quote-change-editor data-today="{{ now()->toDateString() }}">
@ -139,7 +144,7 @@
</section>
<section class="quote-change-panel" aria-labelledby="quote-detail-edit-title">
<header class="quote-change-panel__header"><div><div class="quote-change-panel__title" id="quote-detail-edit-title"><span class="material-icons-outlined" aria-hidden="true">difference</span><span>{{ $change ? '变价明细' : '第三步:编辑新价格' }}</span></div><div class="quote-change-panel__hint">仅新价格可编辑;系统会再次核对原价格,防止过期数据覆盖。</div></div></header>
<header class="quote-change-panel__header"><div><div class="quote-change-panel__title" id="quote-detail-edit-title"><span class="material-icons-outlined" aria-hidden="true">difference</span><span>{{ $change ? '变价明细' : '第三步:编辑新价格' }}</span></div><div class="quote-change-panel__hint">可修改新价格和报价备注;系统会再次核对原价格和原备注。</div></div></header>
<div class="quote-change-panel__body">
<div class="quote-change-alert"><span class="material-icons-outlined" aria-hidden="true">verified_user</span><span>保存草稿不会让报价生效。提交后由指定审核人员审核并生成新版本;待审核单可在详情页撤回修改后重提。本人单据的审核权限以指定授权为准。</span></div>
<div class="quote-batch-tools" aria-label="批量调整">
@ -147,7 +152,7 @@
<div class="quote-change-field"><label for="batch_value">调整值</label><input class="quote-change-control" id="batch_value" type="number" step="0.0001" data-batch-value placeholder="例如 1 或 -0.5"></div>
<button class="quote-change-button" type="button" data-batch-apply><span class="material-icons-outlined" aria-hidden="true">calculate</span><span>应用到勾选项</span></button>
</div>
<p class="quote-change-panel__hint" style="margin-top:10px">每个地区一行,各重量区间横向填写新价;下方对照原价和差额。勾选地区、区间列或单个价格可批量调整。</p>
<p class="quote-change-panel__hint" style="margin-top:10px">每个地区一行,各重量区间横向填写新价;下方对照原价和差额。勾选地区、区间列或单个价格可批量调整。右侧报价备注可重新粘贴或清空;备注中的报价执行日期按本单执行区间显示。</p>
@php
$priceRows = collect($editableCells)->groupBy('source_row', true);
$priceColumns = collect($editableCells)->unique('source_column')->sortBy('source_column');
@ -159,6 +164,7 @@
@foreach ($priceColumns as $column)
<th scope="col"><label class="quote-change-column-label"><input type="checkbox" data-column-select="{{ $column['source_column'] }}" aria-label="选择整列 {{ $column['weight_band'] ?: $column['charge_item'] }}"><span>{{ $column['weight_band'] ?: ($column['charge_item'] ?: '价格') }}</span></label></th>
@endforeach
@if($editableNotes)<th scope="col" class="quote-change-remarks">报价备注</th>@endif
</tr></thead>
<tbody>
@foreach ($priceRows as $rowIndex => $rowCells)
@ -184,6 +190,13 @@
<td><span class="quote-change-panel__hint" aria-label="无可编辑价格">—</span></td>
@endif
@endforeach
@if($editableNotes)
<td class="quote-change-remarks">
@if(isset($editableNotes[$rowIndex]))
<textarea class="quote-change-control" name="quote_notes[{{ $rowIndex }}]" maxlength="10000" aria-label="{{ $editableNotes[$rowIndex]['region'] }} 报价备注" placeholder="可重新粘贴或清空报价备注">{{ old("quote_notes.$rowIndex", $editableNotes[$rowIndex]['text']) }}</textarea>
@endif
</td>
@endif
</tr>
@endforeach
</tbody>

View File

@ -100,6 +100,13 @@
<div><dt>签署状态</dt><dd>{{ $change->version?->signatureLabel() ?? '未进入签署' }}</dd></div>
</dl>
<div class="quote-review__summary"><strong>变更依据与说明</strong><br>{{ $change->summary }}</div>
@if($change->quote_payload['quote_notes'] ?? [])
<div class="quote-review__summary"><strong>报价备注修改</strong>
@foreach($change->quote_payload['quote_notes'] as $note)
<div><strong>{{ $note['region'] }}</strong><div style="white-space:pre-wrap">原备注:{{ $note['original'] ?: '空白' }}</div><div style="white-space:pre-wrap">新备注:{{ $note['text'] ?: '已清空' }}</div></div>
@endforeach
</div>
@endif
@if(!$stockEntry && $change->end_date)
<div class="quote-review__summary"><strong>临时执行区间(均含开始和截止当天)</strong>
@foreach($change->executionPeriods() as $period)<div>{{ $period['effective_date'] }} 至 {{ $period['end_date'] }}</div>@endforeach

View File

@ -120,7 +120,7 @@
@foreach($row as $columnIndex => $cell)
@php($formula = $rowKey !== null && $columnIndex < $lastColumn ? ($formulas[$rowKey][$columnIndex] ?? null) : null)
<{{ $rowIndex === 0 ? 'th' : 'td' }} @if($columnIndex === $lastColumn)class="matrix__note"@endif>
{!! $mixedText(is_bool($cell) ? ($cell ? '是' : '否') : ($cell ?? '')) !!}
{!! $mixedText($rowIndex > 0 && $columnIndex > 0 && $columnIndex < $lastColumn && is_numeric($cell) ? number_format((float) $cell, 2, '.', '') : (is_bool($cell) ? ($cell ? '是' : '否') : ($cell ?? ''))) !!}
@if(is_string($formula) && trim($formula) !== '')<div class="formula">公式:{!! $mixedText($formula) !!}</div>@endif
</{{ $rowIndex === 0 ? 'th' : 'td' }}>
@endforeach

View File

@ -616,6 +616,68 @@ class CourierQuoteChangeWorkflowTest extends FeatureTestCase
}
}
public function test_quote_remarks_can_be_replaced_cleared_reviewed_and_exported_without_price_changes(): void
{
\Illuminate\Support\Facades\Storage::fake('uploads');
\Illuminate\Support\Facades\Http::preventStrayRequests();
config(['courier_onboarding.allow_same_reviewer' => true]);
$this->loginAs()->get(route('business.courier-quote-changes.create', ['source_quote_id' => 'QB-001']))
->assertOk()->assertSee('name="quote_notes[1]"', false)->assertSeeText('报价备注');
$payload = $this->validPayload();
$payload['items'][0]['new_amount'] = $payload['items'][0]['original_amount'];
$payload['quote_notes'] = [1 => "包丢不包损\n面单预充6元/单", 2 => '江苏另行说明'];
$this->post(route('business.courier-quote-changes.store'), $payload)->assertSessionHasNoErrors();
$change = CourierQuoteChange::firstOrFail();
$this->assertFalse($change->hasQuotePayload());
$this->assertCount(0, $change->items);
$this->get(route('business.courier-quote-changes.edit', $change))->assertOk()->assertSeeText('面单预充6元/单');
$payload['quote_notes'][2] = '';
$this->patch(route('business.courier-quote-changes.update', $change), $payload)->assertSessionHasNoErrors();
$change->refresh();
$this->assertArrayNotHasKey(2, $change->quote_payload['quote_notes']);
$book = app(\App\Support\CourierQuoteChangeExport::class)->build($change);
$this->assertSame($payload['quote_notes'][1], $book->getSheetByName('报价备注变更')->getCell('C8')->getValue());
$book->disconnectWorksheets();
$this->post(route('business.courier-quote-changes.submit', $change))->assertSessionHasNoErrors();
$this->get(route('business.courier-quote-changes.show', $change))->assertOk()->assertSeeText('报价备注修改')->assertSeeText('面单预充6元/单');
$this->post(route('business.courier-quote-changes.approve', $change))->assertSessionHasNoErrors();
$values = json_decode($change->fresh()->version->values_json, true);
$this->assertSame($payload['quote_notes'][1], $values[1][3]);
$this->assertSame(5, $values[1][1]);
$this->assertSame(1.2, $values[1][2]);
$this->assertNull($values[2][3]);
$this->assertSame($payload['quote_notes'][1], json_decode($change->events()->where('event_type', 'approved')->sole()->details_json, true)['quote_notes'][1]['text']);
$this->assertNull(app(\App\Support\CourierQuoteChangeService::class)->findSourceQuote('QB-001')['values'][1][3]);
}
public function test_quote_remarks_reject_unknown_cells_and_pending_or_other_users_edits(): void
{
$payload = [...$this->validPayload(), 'quote_notes' => [999 => '不能写入任意单元格']];
$this->withExceptionHandling()->loginAs()->post(route('business.courier-quote-changes.store'), $payload)->assertSessionHasErrors('quote_notes');
$this->assertDatabaseCount('business_courier_quote_changes', 0);
$payload['quote_notes'] = [1 => '财务核对后确认'];
$this->post(route('business.courier-quote-changes.store'), $payload)->assertSessionHasNoErrors();
$change = CourierQuoteChange::firstOrFail();
$this->loginAs($this->createReviewer())->patch(route('business.courier-quote-changes.update', $change), $payload)->assertForbidden();
$this->loginAs()->post(route('business.courier-quote-changes.submit', $change))->assertSessionHasNoErrors();
$this->patch(route('business.courier-quote-changes.update', $change), $payload)->assertSessionHasErrors('status');
$this->assertSame('财务核对后确认', $change->fresh()->quote_payload['quote_notes'][1]['text']);
}
public function test_clearing_existing_quote_remarks_clears_matching_general_rules_and_stale_sources_are_rejected(): void
{
$source = ['values' => [['省份', '费用', '备注'], ['江苏', 1.2345, "第一行\n第二行"]],
'formulas' => [], 'billing_rules' => "第一行\n第二行"];
$payload = \App\Support\CourierQuoteNotes::payload(null, ['quote_notes' => [1 => null]], $source);
$updated = \App\Support\CourierQuoteNotes::apply($source, $payload['quote_notes']);
$this->assertSame('', $updated['values'][1][2]);
$this->assertSame('', $updated['billing_rules']);
$this->assertSame(1.2345, $updated['values'][1][1]);
$source['values'][1][2] = '已经变更的来源';
$this->expectException(\Illuminate\Validation\ValidationException::class);
\App\Support\CourierQuoteNotes::apply($source, $payload['quote_notes']);
}
private function validPayload(): array
{
return [

View File

@ -116,7 +116,7 @@ class CourierQuoteSignatureWorkflowTest extends FeatureTestCase
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) {
foreach (['0-3公斤', '3-35公斤', '江苏', '安徽', '0.50', '0.53', '0.63', '元/公斤', '按实际重量计费', '计泡系数:1.2'] as $expected) {
$this->assertStringContainsString($expected, $rendered);
}
foreach (['新旧价格差异', '原价格', '差额', '单元格', '987.6543', '内部审核备注', '内部差额核对'] as $internal) {
@ -132,6 +132,50 @@ class CourierQuoteSignatureWorkflowTest extends FeatureTestCase
$this->assertSame(0, $table['rows'][2][1]);
}
public function test_signature_and_sheet_use_actual_periods_in_rules_and_remarks_without_changing_saved_values(): void
{
$version = $this->createVersion();
$periods = [['effective_date' => '2026-10-20', 'end_date' => '2026-10-24'], ['effective_date' => '2026-11-11', 'end_date' => '2026-11-14']];
$version->change->update(['quote_payload' => ['execution_periods' => $periods]]);
$version->update(['effective_date' => '2026-10-20', 'end_date' => '2026-11-14',
'billing_rules' => '7、此价格于2026年9月20日起 8、以下城市主发',
'values_json' => json_encode([['省份', '0-3公斤', '备注'], ['江苏', '0.5350', "本价格2026.9.20开始执行\n包丢不包损"]], JSON_UNESCAPED_UNICODE)]);
$original = $version->values_json;
$data = app(\App\Support\CourierQuoteConfirmation::class)->data($version->fresh());
foreach ([$data['billing_rules'], $data['values'][1][2]] as $text) {
$this->assertStringContainsString('2026-10-20 至 2026-10-24;2026-11-11 至 2026-11-14', $text);
$this->assertStringNotContainsString('9月20', $text);
$this->assertStringNotContainsString('2026.9.20', $text);
}
$frames = [];
$pdf = app('dompdf.wrapper');
$pdf->getDomPDF()->setCallbacks([['event' => 'end_frame', 'f' => static function ($frame) use (&$frames): void {
if ($frame instanceof \Dompdf\FrameDecorator\Text) $frames[] = $frame->get_text();
}]]);
app()->instance('dompdf.wrapper', $pdf);
app(CourierQuoteSignatureService::class)->prepareVersion($version->fresh(), $this->user->id);
$rendered = implode('', $frames);
$this->assertStringContainsString('0.54', $rendered);
$this->assertStringNotContainsString('0.5350', $rendered);
$this->assertStringNotContainsString('2026年9月20', $rendered);
$this->assertStringContainsString('8、以下城市主发', $rendered);
$this->assertStringContainsString('包丢不包损', $rendered);
$response = $this->loginAs()->get(route('business.courier-quote-signatures.sheet', $version))->assertOk();
ob_start(); $response->baseResponse->sendContent(); $bytes = ob_get_clean();
$path = tempnam(sys_get_temp_dir(), 'quote-period-sheet'); file_put_contents($path, $bytes);
try {
$book = \PhpOffice\PhpSpreadsheet\IOFactory::load($path);
$sheet = $book->getActiveSheet();
$this->assertSame(0.535, $sheet->getCell('B2')->getValue());
$this->assertSame('0.54', $sheet->getCell('B2')->getFormattedValue());
$this->assertStringContainsString('2026-11-11 至 2026-11-14', $sheet->getCell('C2')->getValue());
$this->assertStringNotContainsString('2026.9.20', $sheet->getCell('C2')->getValue());
$book->disconnectWorksheets();
} finally { unlink($path); }
$this->assertSame($original, $version->fresh()->values_json);
}
public function test_admin_can_push_the_exact_pdf_to_esign_sandbox(): void
{
$signature = $this->createPreparedSignature();

View File

@ -0,0 +1,22 @@
<?php
namespace Tests\Unit;
use App\Support\CourierQuoteDocument;
use PHPUnit\Framework\TestCase;
class CourierQuoteDocumentTest extends TestCase
{
public function test_only_the_current_quote_execution_clause_is_replaced_and_other_dates_and_rules_survive(): void
{
$label = '2026-10-20 至 2026-10-24;2026-11-11 至 2026-11-14(临时)';
foreach (['此价格于2026年9月20日起', '本价格2026.9.20开始执行', '执行日期:2026-09-20',
'本报价执行期间:2026-10-20 至 2026-10-24;2026-11-11 至 2026-11-14(临时)。'] as $old) {
$text = "7、{$old} 8、以下城市主发\n协议签署日期:2026-09-20\n原报价 QB-108(2026-09-20 起执行)";
$actual = CourierQuoteDocument::executionText($text, $label);
$this->assertSame("7、本报价执行期间:{$label}。 8、以下城市主发\n协议签署日期:2026-09-20\n原报价 QB-108(2026-09-20 起执行)", $actual);
$this->assertSame($actual, CourierQuoteDocument::executionText($actual, $label));
}
$this->assertSame('本报价执行期间:2026-12-01 起。', CourierQuoteDocument::executionText('生效时间:2026/9/20起', '2026-12-01 起'));
}
}