From 998aae27307b44ad30cfca70fd5b3cfac79475f0 Mon Sep 17 00:00:00 2001 From: limeng <1131177064@qq.com> Date: Wed, 18 Nov 2026 17:03:59 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=AF=BC=E5=87=BA=E5=AE=8C=E6=95=B4?= =?UTF-8?q?=E5=8F=98=E4=BB=B7=E5=90=8E=E6=8A=A5=E4=BB=B7=E5=B9=B6=E7=BB=9F?= =?UTF-8?q?=E4=B8=80=E5=A4=87=E6=B3=A8=E6=89=A7=E8=A1=8C=E6=97=A5=E6=9C=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Business/CourierQuoteChanges.php | 2 +- app/Support/CourierQuoteChangeExport.php | 213 ++++++------------ .../quote-change-show.blade.php | 12 +- .../CourierQuoteChangeWorkflowTest.php | 119 +++++++--- .../CourierQuoteWorkflowPermissionsTest.php | 11 +- 5 files changed, 173 insertions(+), 184 deletions(-) diff --git a/app/Http/Controllers/Business/CourierQuoteChanges.php b/app/Http/Controllers/Business/CourierQuoteChanges.php index 6af3f69c..7aaebcf5 100644 --- a/app/Http/Controllers/Business/CourierQuoteChanges.php +++ b/app/Http/Controllers/Business/CourierQuoteChanges.php @@ -232,7 +232,7 @@ class CourierQuoteChanges extends Controller } finally { $book->disconnectWorksheets(); } - }, $quoteChange->number . '-变价明细.xlsx', [ + }, $quoteChange->number . '-变价后报价.xlsx', [ 'Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'Cache-Control' => 'private, no-store', ]); diff --git a/app/Support/CourierQuoteChangeExport.php b/app/Support/CourierQuoteChangeExport.php index 9927be3d..0f7f5b5b 100644 --- a/app/Support/CourierQuoteChangeExport.php +++ b/app/Support/CourierQuoteChangeExport.php @@ -2,157 +2,87 @@ namespace App\Support; +use App\Http\Controllers\Business\CourierQuoteSignatures; use App\Models\Business\CourierQuoteChange; use PhpOffice\PhpSpreadsheet\Cell\Coordinate; use PhpOffice\PhpSpreadsheet\Cell\DataType; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; -/** 单张变价单导出;以保存的明细为准,不重新读取可变的报价底稿。 */ +/** Complete quotation after this change, using the approved snapshot when available. */ final class CourierQuoteChangeExport { public function build(CourierQuoteChange $change): Spreadsheet { abort_unless((int) $change->company_id === (int) company_id(), 403); abort_if($change->isStockQuote(), 422, '存量报价请在详情页查看原报价资料。'); + [$values, $rules] = $this->finalQuote($change); + $values = CourierQuoteDocument::values($values, $change->periodLabel()); + $rules = CourierQuoteDocument::executionText($rules, $change->periodLabel()); + $layout = CourierQuoteSignatures::quoteLayout($values); + $text = implode("\n", array_map(static fn ($row) => implode("\n", $row), $values)); + if (filled($rules) && ! str_contains($text, trim($rules))) { + $layout['notes'][] = $rules; + } + $table = CourierQuoteSignatures::quoteTableWithNotes($layout)['rows']; + abort_if($table === [], 422, '这份变价单没有可导出的完整报价。'); + $book = new Spreadsheet(); - $book->removeSheetByIndex(0); - $cells = []; - if (! $change->hasQuotePayload()) { - foreach ($change->items()->reorder('source_row')->orderBy('source_column')->cursor() as $item) { - $cells[] = ['row' => $item->source_row, 'column' => $item->source_column, 'region' => $item->region, - 'label' => $item->weight_band ?: $item->charge_item ?: '价格', 'old' => $item->original_amount, - 'new' => $item->new_amount, 'cell' => $item->source_cell, 'unit' => $item->unit, 'note' => $item->note]; - } - } else { - $requotes = app(CourierRequoteService::class); - $onboarding = $requotes->onboardingForCourier($change->carrier_name); - abort_if($onboarding === null, 422, '未找到原入驻报价,无法导出新旧对比。'); - $old = $requotes->basePayload($onboarding, $change); - $diff = $requotes->diff($onboarding, $old, $change->quote_payload); - foreach ($diff['cells'] as $cell) { - $cells[] = ['row' => $cell['region'], 'column' => $cell['label'], 'region' => $cell['region'], - 'label' => $cell['label'], 'old' => $cell['old'], 'new' => $cell['new'], 'cell' => '', 'unit' => '元', 'note' => '']; - } + $sheet = $book->getActiveSheet()->setTitle('变价后报价'); + foreach ([ + $change->carrier_name . ' · 变价后报价表', + '变价单:' . $change->number . ';报价类型:' . $change->quote_type, + '状态:' . $change->statusLabel() . ';执行期间:' . $change->periodLabel(), + '变更说明:' . $change->summary, + '本表包含变价后的完整地区、价格和报价备注。', + $change->end_date !== null ? '各区间均含开始和截止当天;区间外沿用原长期报价。' : '自生效日期起长期执行。', + ] as $index => $value) { + $this->text($sheet, 'A' . ($index + 1), $value); } - $sheet = $this->sheet($book, $change, '横向变价对比'); - $this->text($sheet, 'A5', '仅列本单已保存的变价项;—表示本单未变更或无对应价格,不代表零价。备注见逐项明细。'); - if (isset($diff)) { - $this->text($sheet, 'A5', $diff['same_structure'] - ? '价格变动见下表;新增或移除地区、规则变更见结构与规则变更及原、新报价全表。' - : '计费结构已变化,请对照结构与规则变更、原报价全表与新报价全表,不计算不同区间之间的差额。'); - } - $this->text($sheet, 'A7', '地区 / 规则'); - $sheet->mergeCells('A7:A8'); - $columns = collect($cells)->unique('column')->values(); - foreach ($columns as $index => $column) { - $start = 2 + $index * 3; - $this->text($sheet, Coordinate::stringFromColumnIndex($start) . '7', $column['label']); - $sheet->mergeCells(Coordinate::stringFromColumnIndex($start) . '7:' . Coordinate::stringFromColumnIndex($start + 2) . '7'); - foreach (['原价', '新价', '差额'] as $offset => $label) { - $this->text($sheet, Coordinate::stringFromColumnIndex($start + $offset) . '8', $label); - } - } - $row = 9; - foreach (collect($cells)->groupBy('row') as $items) { - $this->text($sheet, 'A' . $row, $items->first()['region']); - $byColumn = $items->keyBy('column'); - foreach ($columns as $index => $column) { - $cell = $byColumn->get($column['column']); - foreach ([$cell['old'] ?? null, $cell['new'] ?? null, $cell === null ? null : $this->delta($cell)] as $offset => $value) { - $address = Coordinate::stringFromColumnIndex(2 + $index * 3 + $offset) . $row; - $this->amount($sheet, $address, $value); + $columns = count($table[0]); + foreach ($table as $index => $row) { + $line = $index + 7; + $height = 1; + foreach ($row as $column => $value) { + $address = Coordinate::stringFromColumnIndex($column + 1) . $line; + if ($index > 0 && $column > 0 && $column < $columns - 1 && is_numeric($value)) { + $sheet->setCellValueExplicit($address, round((float) $value, 4), DataType::TYPE_NUMERIC); + $sheet->getStyle($address)->getNumberFormat()->setFormatCode('0.0000'); + } else { + $this->text($sheet, $address, $value); } + $width = $column === $columns - 1 ? 60 : ($column === 0 ? 22 : 16); + $height = max($height, array_sum(array_map(static fn ($text) => max(1, (int) ceil(mb_strwidth($text) / ($width - 2))), explode("\n", (string) $value)))); } - $row++; + $sheet->getRowDimension($line)->setRowHeight(min(409, max(26, $height * 16 + 8))); } - $this->style($sheet, max(1, 1 + $columns->count() * 3), max(8, $row - 1), 8); - $sheet->freezePane('B9'); - - $details = $this->sheet($book, $change, '逐项明细'); - foreach (['地区 / 规则', '重量区间 / 计费项', '原价', '新价', '差额', '单位', '备注', '原单元格'] as $index => $label) { - $this->text($details, Coordinate::stringFromColumnIndex($index + 1) . '7', $label); - } - foreach ($cells as $index => $cell) { - $line = $index + 8; - foreach (['A' => $cell['region'], 'B' => $cell['label'], 'F' => $cell['unit'], 'G' => $cell['note'], 'H' => $cell['cell']] as $column => $value) { - $this->text($details, $column . $line, $value); - } - foreach (['C' => $cell['old'], 'D' => $cell['new'], 'E' => $this->delta($cell)] as $column => $value) { - $this->amount($details, $column . $line, $value); - } - } - $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); - - if (isset($diff)) { - $rules = $this->sheet($book, $change, '结构与规则变更'); - $rows = [['原计费方式', $diff['old_mode']], ['新计费方式', $diff['new_mode']], - ['新增地区', implode('、', $diff['added']) ?: '无'], ['移除地区', implode('、', $diff['removed']) ?: '无']]; - foreach ($diff['rules'] as $rule) { - $rows[] = ['原规则', $rule['old']]; - $rows[] = ['新规则', $rule['new']]; - } - if (! $diff['same_structure']) { - $rows[] = ['核对说明', '计费结构已变化,请对照原报价全表与新报价全表,不按相同区间计算差额。']; - } - foreach ($rows as $index => $values) { - $this->text($rules, 'A' . ($index + 7), $values[0]); - $this->text($rules, 'B' . ($index + 7), $values[1]); - } - $this->style($rules, 2, count($rows) + 6, 7); - $rules->getColumnDimension('B')->setWidth(100); - foreach (['原报价全表' => $old, '新报价全表' => $change->quote_payload] as $title => $payload) { - $model = $requotes->draftModel($onboarding, $payload); - $full = $this->sheet($book, $change, $title); - $headers = ['地区 / 规则', ...array_column($model->quoteColumns(), 'label')]; - foreach ($headers as $index => $label) { - $this->text($full, Coordinate::stringFromColumnIndex($index + 1) . '7', $label); - } - foreach ($model->quoteRowList() as $index => $item) { - $this->text($full, 'A' . ($index + 8), $item['region']); - foreach (CourierQuoteStructure::rowValues($item, $model->quoteColumns()) as $column => $value) { - $this->amount($full, Coordinate::stringFromColumnIndex($column + 2) . ($index + 8), $value); - } - } - $this->style($full, count($headers), max(7, count($model->quoteRowList()) + 7), 7); - } - } - $book->setActiveSheetIndex(0); + $this->style($sheet, $columns, count($table) + 6); return $book; } - private function sheet(Spreadsheet $book, CourierQuoteChange $change, string $title): Worksheet + private function finalQuote(CourierQuoteChange $change): array { - $sheet = $book->createSheet()->setTitle($title); - foreach (['报价变价核对 · ' . $change->number, $change->carrier_name . ' · 原报价 ' . $change->source_quote_id, - '状态:' . $change->statusLabel() . ';执行期间:' . $change->periodLabel(), '变更说明:' . $change->summary] as $index => $value) { - $this->text($sheet, 'A' . ($index + 1), $value); + if ($change->version && filled($change->version->values_json)) { + return [json_decode($change->version->values_json, true, 512, JSON_THROW_ON_ERROR), $change->version->billing_rules]; } - if (!$change->isStockQuote() && $change->end_date !== null) { - $this->text($sheet, 'A6', '各区间均含开始和截止当天;区间外沿用原长期报价。原报价:' . $change->source_quote_id); - } - return $sheet; - } + if ($change->hasQuotePayload()) { + abort_if(blank($change->proposed_values_json), 422, '变价后的完整报价尚未生成,请重新载入变价单。'); - private function delta(array $cell): ?float - { - return $cell['old'] === null || $cell['new'] === null ? null : round((float) $cell['new'] - (float) $cell['old'], 4); + return [json_decode($change->proposed_values_json, true, 512, JSON_THROW_ON_ERROR), $change->quote_payload['billing_rules'] ?? null]; + } + + $source = app(CourierQuoteChangeService::class)->findSourceQuote($change->source_quote_id); + $source = CourierQuoteNotes::apply($source, $change->quote_payload['quote_notes'] ?? []); + $values = $source['values']; + foreach ($change->items as $item) { + abort_unless(array_key_exists($item->source_row, $values) && array_key_exists($item->source_column, $values[$item->source_row]), + 422, '原报价位置已变化,请重新载入变价单。'); + $values[$item->source_row][$item->source_column] = $item->new_amount; + } + + return [$values, $source['billing_rules'] ?? null]; } private function text(Worksheet $sheet, string $address, mixed $value): void @@ -160,38 +90,27 @@ final class CourierQuoteChangeExport $sheet->setCellValueExplicit($address, (string) ($value ?? ''), DataType::TYPE_STRING); } - private function amount(Worksheet $sheet, string $address, mixed $value): void - { - if ($value === null) { - $this->text($sheet, $address, '—'); - return; - } - $sheet->setCellValueExplicit($address, round((float) $value, 4), DataType::TYPE_NUMERIC); - $sheet->getStyle($address)->getNumberFormat()->setFormatCode('0.0000'); - } - - private function style(Worksheet $sheet, int $columns, int $lastRow, int $headerRow): void + private function style(Worksheet $sheet, int $columns, int $lastRow): void { $last = Coordinate::stringFromColumnIndex($columns); $sheet->getStyle('A1:' . $last . $lastRow)->getFont()->setName('Microsoft YaHei')->setSize(10); - $sheet->getStyle('A7:' . $last . $headerRow)->applyFromArray([ + $sheet->getStyle('A7:' . $last . $lastRow)->getAlignment()->setWrapText(true)->setVertical('center'); + $sheet->getStyle('A7:' . $last . '7')->applyFromArray([ 'font' => ['bold' => true, 'color' => ['rgb' => '513E21']], 'fill' => ['fillType' => 'solid', 'startColor' => ['rgb' => 'F7F0DE']], - 'alignment' => ['wrapText' => true, 'vertical' => 'center'], ]); foreach (range(1, $columns) as $column) { - $sheet->getColumnDimension(Coordinate::stringFromColumnIndex($column))->setWidth($column === 1 ? 22 : 16); + $sheet->getColumnDimension(Coordinate::stringFromColumnIndex($column))->setWidth($column === $columns ? 60 : ($column === 1 ? 22 : 16)); } $sheet->getStyle('A1')->getFont()->setBold(true)->setSize(14); - foreach ([3, 6] as $line) { - if (blank($sheet->getCell('A' . $line)->getValue())) continue; + foreach (range(1, 6) as $line) { if ($columns > 1) $sheet->mergeCells('A' . $line . ':' . $last . $line); $sheet->getStyle('A' . $line)->getAlignment()->setWrapText(true); - $lines = (int) ceil(mb_strwidth((string) $sheet->getCell('A' . $line)->getValue()) / max(22, 22 + ($columns - 1) * 16)); - $sheet->getRowDimension($line)->setRowHeight(min(409, max(22, $lines * 16 + 6))); + $lines = (int) ceil(mb_strwidth((string) $sheet->getCell('A' . $line)->getValue()) / (22 + max(0, $columns - 2) * 16 + 60)); + $sheet->getRowDimension($line)->setRowHeight(max(24, $lines * 16 + 8)); } - $sheet->freezePane('B' . ($headerRow + 1)); - $sheet->setAutoFilter('A' . $headerRow . ':' . $last . $lastRow); - $sheet->getPageSetup()->setOrientation('landscape')->setFitToWidth(1)->setFitToHeight(0); + $sheet->freezePane('B8'); + $sheet->setAutoFilter('A7:' . $last . $lastRow); + $sheet->getPageSetup()->setOrientation('landscape')->setFitToWidth(1)->setFitToHeight(0)->setRowsToRepeatAtTopByStartAndEnd(7, 7); } } diff --git a/resources/views/business/pages/courier-payables/quote-change-show.blade.php b/resources/views/business/pages/courier-payables/quote-change-show.blade.php index d5416456..89cac8ae 100644 --- a/resources/views/business/pages/courier-payables/quote-change-show.blade.php +++ b/resources/views/business/pages/courier-payables/quote-change-show.blade.php @@ -21,6 +21,9 @@ .quote-review__meta dt { color:var(--qr-muted); font-size:11px; } .quote-review__meta dd { margin:4px 0 0; font-size:12.5px; font-variant-numeric:tabular-nums; } .quote-review__summary { margin-top:16px; border-top:1px solid var(--qr-border); padding-top:14px; line-height:1.65; white-space:pre-wrap; } + .quote-review__notes { display:grid; grid-template-columns:repeat(2,minmax(0,1fr)); gap:16px; margin-top:8px; white-space:normal; } + .quote-review__note { min-width:0; border:1px solid var(--qr-border); border-radius:8px; padding:12px; } + .quote-review__note-text { margin-top:8px; white-space:pre-wrap; overflow-wrap:anywhere; } .quote-review__alert { display:flex; align-items:flex-start; gap:8px; margin-bottom:16px; border:1px solid #D9BD88; border-radius:8px; background:#FFF8E8; padding:10px 12px; color:#704800; font-size:12px; line-height:1.6; } .quote-review__alert .material-icons-outlined { flex:0 0 auto; font-size:18px; } .quote-review__table-wrap { overflow-x:auto; } @@ -56,14 +59,14 @@ .quote-review__choice label { display:inline-flex; align-items:center; gap:5px; font-size:12px; } .quote-review__hint { color:var(--qr-muted); font-size:11px; line-height:1.55; } @media(max-width:1000px){.quote-review__form-grid{grid-template-columns:1fr 1fr}.quote-review__signature-meta{grid-template-columns:1fr 1fr}} - @media(max-width:800px){.quote-review{margin:16px}.quote-review__toolbar{align-items:stretch;flex-direction:column}.quote-review__meta,.quote-review__signature-meta,.quote-review__form-grid{grid-template-columns:1fr}.quote-review__event{grid-template-columns:1fr}.quote-review__review-form,.quote-review__form-actions{align-items:stretch;flex-direction:column}} + @media(max-width:800px){.quote-review{margin:16px}.quote-review__toolbar{align-items:stretch;flex-direction:column}.quote-review__meta,.quote-review__signature-meta,.quote-review__form-grid,.quote-review__notes{grid-template-columns:1fr}.quote-review__event{grid-template-columns:1fr}.quote-review__review-form,.quote-review__form-actions{align-items:stretch;flex-direction:column}}
返回报价历史
@unless($change->isStockQuote()) - 导出变价明细 + 导出变价后报价 @endunless @if($canWithdraw ?? false)
@csrf
@@ -103,7 +106,10 @@ @if($change->quote_payload['quote_notes'] ?? [])
报价备注修改 @foreach($change->quote_payload['quote_notes'] as $note) -
{{ $note['region'] }}
原备注:{{ $note['original'] ?: '空白' }}
新备注:{{ $note['text'] ?: '已清空' }}
+
{{ $note['region'] }}
+
原备注(原报价留档):
{{ $note['original'] ?: '空白' }}
+
变价后备注:
{{ \App\Support\CourierQuoteDocument::executionText($note['text'], $change->periodLabel()) ?: '已清空' }}
+
@endforeach
@endif diff --git a/tests/Feature/Business/CourierQuoteChangeWorkflowTest.php b/tests/Feature/Business/CourierQuoteChangeWorkflowTest.php index cdf92234..0afaee6c 100644 --- a/tests/Feature/Business/CourierQuoteChangeWorkflowTest.php +++ b/tests/Feature/Business/CourierQuoteChangeWorkflowTest.php @@ -326,15 +326,15 @@ class CourierQuoteChangeWorkflowTest extends FeatureTestCase $this->withExceptionHandling()->loginAs()->post(route('business.courier-quote-changes.store'), $this->validPayload())->assertForbidden(); } - public function test_export_preserves_horizontal_coordinates_and_all_500_saved_items_without_mutating_pending_change(): void + public function test_export_contains_the_complete_55_by_17_quote_without_mutating_pending_change(): void { - $values = [['地区', ...array_map(fn ($column) => ($column - 1) . '-' . $column . 'kg', range(1, 10))]]; + $values = [['地区', ...array_map(fn ($column) => ($column - 1) . '-' . $column . 'kg', range(1, 16))]]; $payload = $this->validPayload(); $payload['items'] = []; - foreach (range(1, 50) as $row) { + foreach (range(1, 55) as $row) { $region = $row <= 2 ? '江苏' : '地区' . $row; - $values[] = [$region, ...array_fill(0, 10, 5.1234)]; - foreach (range(1, 10) as $column) { + $values[] = [$region, ...array_fill(0, 16, 5.1234)]; + foreach ($row <= 50 ? range(1, 10) : [] as $column) { $payload['items'][] = ['source_cell' => \PhpOffice\PhpSpreadsheet\Cell\Coordinate::stringFromColumnIndex($column + 1) . ($row + 1), 'source_row' => $row, 'source_column' => $column, 'region' => $region, 'weight_band' => $values[0][$column], 'charge_item' => $values[0][$column], @@ -342,43 +342,44 @@ class CourierQuoteChangeWorkflowTest extends FeatureTestCase 'unit' => '元', 'note' => '=1+1']; } } - file_put_contents($this->fixturePath, json_encode(['sourcePath' => 'private/quote.xlsx', 'sheets' => [[ - 'index' => 0, 'name' => '测试快递报价', 'address' => 'A1:K51', 'values' => $values, 'formulas' => [], - ]]], JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR)); + $sheets = array_map(fn ($index) => [ + 'index' => $index, 'name' => '测试快递' . $index . '报价', 'address' => 'A1:Q56', 'values' => $values, 'formulas' => [], + ], range(0, 211)); + file_put_contents($this->fixturePath, json_encode(['sourcePath' => 'private/quote.xlsx', 'sheets' => $sheets], JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR)); $this->loginAs()->post(route('business.courier-quote-changes.store'), $payload)->assertRedirect(); $change = CourierQuoteChange::firstOrFail(); $this->post(route('business.courier-quote-changes.submit', $change))->assertRedirect(); $before = $change->fresh()->getAttributes(); $eventsBefore = $change->events()->count(); + \Illuminate\Support\Facades\DB::flushQueryLog(); + \Illuminate\Support\Facades\DB::enableQueryLog(); $start = microtime(true); $response = $this->get(route('business.courier-quote-changes.export', $change))->assertOk(); $response->assertHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); $bytes = $response->streamedContent(); + $queries = \Illuminate\Support\Facades\DB::getQueryLog(); + \Illuminate\Support\Facades\DB::disableQueryLog(); $file = tempnam(sys_get_temp_dir(), 'quote-export-'); try { file_put_contents($file, $bytes); $book = \PhpOffice\PhpSpreadsheet\IOFactory::load($file); - $sheet = $book->getSheetByName('横向变价对比'); - $details = $book->getSheetByName('逐项明细'); - $this->assertSame(58, $sheet->getHighestRow()); - $this->assertSame('AE', $sheet->getHighestColumn()); + $this->assertSame(['变价后报价'], $book->getSheetNames()); + $sheet = $book->getSheet(0); + $this->assertSame(62, $sheet->getHighestRow()); + $this->assertSame('R', $sheet->getHighestColumn()); + $this->assertSame('江苏', $sheet->getCell('A8')->getValue()); $this->assertSame('江苏', $sheet->getCell('A9')->getValue()); - $this->assertSame('江苏', $sheet->getCell('A10')->getValue()); $this->assertSame('0-1kg', $sheet->getCell('B7')->getValue()); - $this->assertSame('9-10kg', $sheet->getCell('AC7')->getValue()); - $this->assertEquals(5.1234, $sheet->getCell('B9')->getValue()); - $this->assertEquals(4.5678, $sheet->getCell('C9')->getValue()); - $this->assertEquals(-0.5556, $sheet->getCell('D9')->getValue()); - $this->assertEquals(0, $sheet->getCell('AD58')->getValue()); - $this->assertEquals(-5.1234, $sheet->getCell('AE58')->getValue()); - $this->assertSame('0.0000', $sheet->getStyle('AD58')->getNumberFormat()->getFormatCode()); - $this->assertSame('B9', $sheet->getFreezePane()); - $this->assertSame(507, $details->getHighestRow()); - $this->assertSame('=1+1', $details->getCell('G8')->getValue()); - $this->assertSame('s', $details->getCell('G8')->getDataType()); - $this->assertSame('K51', $details->getCell('H507')->getValue()); + $this->assertSame('15-16kg', $sheet->getCell('Q7')->getValue()); + $this->assertEquals(4.5678, $sheet->getCell('B8')->getValue()); + $this->assertEquals(0, $sheet->getCell('K57')->getValue()); + $this->assertEquals(5.1234, $sheet->getCell('L57')->getValue()); + $this->assertEquals(5.1234, $sheet->getCell('B62')->getValue()); + $this->assertEquals(5.1234, $sheet->getCell('Q62')->getValue()); + $this->assertSame('0.0000', $sheet->getStyle('K57')->getNumberFormat()->getFormatCode()); + $this->assertSame('B8', $sheet->getFreezePane()); $book->disconnectWorksheets(); - fwrite(STDERR, sprintf("\nQUOTE_EXPORT_500: %.3fs, %d bytes, process peak %.1f MiB\n", microtime(true) - $start, strlen($bytes), memory_get_peak_usage(true) / 1048576)); + fwrite(STDERR, sprintf("\nQUOTE_EXPORT_212_QUOTES_55x17_500_CHANGES: %.3fs, %d bytes, process peak %.1f MiB, %d queries / %.1fms\n", microtime(true) - $start, strlen($bytes), memory_get_peak_usage(true) / 1048576, count($queries), array_sum(array_column($queries, 'time')))); } finally { unlink($file); } @@ -407,6 +408,70 @@ class CourierQuoteChangeWorkflowTest extends FeatureTestCase $this->post(route('business.courier-quote-changes.withdraw', $change))->assertForbidden(); } + public function test_final_quote_export_and_new_remarks_use_change_dates_and_preserve_the_approved_snapshot(): void + { + \Illuminate\Support\Facades\Storage::fake('uploads'); + \Illuminate\Support\Facades\Http::preventStrayRequests(); + config(['courier_onboarding.allow_same_reviewer' => true]); + $original = "1.计泡:长*宽*高/6000\n2.限重60kg\n5.执行时间:2026年2月1日起\n协议签署日期:2026-02-01"; + $values = [['省份', '面单', '续重', '备注'], ['广东省', 5, 1.2, $original], ['江苏省', 4, 0, '=1+1']]; + file_put_contents($this->fixturePath, json_encode(['sheets' => [[ + 'index' => 0, 'name' => '新北中通报价', 'address' => 'A1:D3', 'values' => $values, 'formulas' => [], + ]]], JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR)); + $edited = str_replace('限重60kg', '限重50kg', $original); + $payload = [...$this->validPayload(), 'execution_mode' => 'temporary', + 'periods' => [['effective_date' => '2026-11-01', 'end_date' => '2026-11-20']], + 'quote_notes' => [1 => $edited]]; + $this->loginAs()->post(route('business.courier-quote-changes.store'), $payload)->assertSessionHasNoErrors(); + $change = CourierQuoteChange::firstOrFail(); + $this->get(route('business.courier-quote-changes.show', $change))->assertOk() + ->assertSeeText('原备注(原报价留档)')->assertSeeText('变价后备注') + ->assertSeeText('本报价执行期间:2026-11-01 至 2026-11-20(临时)'); + $expected = str_replace('执行时间:2026年2月1日起', '本报价执行期间:2026-11-01 至 2026-11-20(临时)。', $edited); + $before = $change->getAttributes(); + $book = app(\App\Support\CourierQuoteChangeExport::class)->build($change); + $sheet = $book->getSheet(0); + $this->assertSame(['变价后报价'], $book->getSheetNames()); + $this->assertSame($expected, $sheet->getCell('D8')->getValue()); + $this->assertEquals(6, $sheet->getCell('B8')->getValue()); + $this->assertEquals(4, $sheet->getCell('B9')->getValue()); + $this->assertEquals(0, $sheet->getCell('C9')->getValue()); + $this->assertSame('=1+1', $sheet->getCell('D9')->getValue()); + $this->assertSame('s', $sheet->getCell('D9')->getDataType()); + $book->disconnectWorksheets(); + $this->assertSame($before, $change->fresh()->getAttributes()); + $this->assertSame($original, app(\App\Support\CourierQuoteChangeService::class)->findSourceQuote('QB-001')['values'][1][3]); + $this->post(route('business.courier-quote-changes.submit', $change))->assertSessionHasNoErrors(); + $this->post(route('business.courier-quote-changes.approve', $change))->assertSessionHasNoErrors(); + $change->refresh()->unsetRelation('version'); + $approved = $change->version->getAttributes(); + file_put_contents($this->fixturePath, '{"sheets":[]}'); + $book = app(\App\Support\CourierQuoteChangeExport::class)->build($change); + $this->assertSame($expected, $book->getSheet(0)->getCell('D8')->getValue()); + $this->assertEquals(4, $book->getSheet(0)->getCell('B9')->getValue()); + $book->disconnectWorksheets(); + $this->assertSame($approved, $change->version->fresh()->getAttributes()); + } + + public function test_full_payload_export_uses_the_final_matrix_including_new_regions_and_columns(): void + { + $this->loginAs()->post(route('business.courier-quote-changes.store'), $this->validPayload())->assertRedirect(); + $change = CourierQuoteChange::firstOrFail(); + $matrix = [['新报价'], ['计费方式:首重+续重'], ['省份', '首重1kg', '续重每kg'], ['江苏', 3.5, 0.8], ['浙江', 0, null]]; + $change->update(['quote_payload' => ['quote_mode' => 'first_continue'], + 'proposed_values_json' => json_encode($matrix, JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR)]); + $book = app(\App\Support\CourierQuoteChangeExport::class)->build($change); + $sheet = $book->getSheet(0); + $this->assertSame('首重1kg', $sheet->getCell('B7')->getValue()); + $this->assertSame('续重每kg', $sheet->getCell('C7')->getValue()); + $this->assertSame('江苏', $sheet->getCell('A8')->getValue()); + $this->assertSame('浙江', $sheet->getCell('A9')->getValue()); + $this->assertEquals(0, $sheet->getCell('B9')->getValue()); + $this->assertSame('', $sheet->getCell('C9')->getValue()); + $this->assertSame('计费方式:首重+续重', $sheet->getCell('D8')->getValue()); + $book->disconnectWorksheets(); + } + public function test_multiple_periods_survive_draft_edit_approval_and_restore_on_every_boundary(): void { \Illuminate\Support\Facades\Storage::fake('uploads'); @@ -636,7 +701,7 @@ class CourierQuoteChangeWorkflowTest extends FeatureTestCase $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()); + $this->assertSame($payload['quote_notes'][1], $book->getSheetByName('变价后报价')->getCell('D8')->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元/单'); diff --git a/tests/Feature/Business/CourierQuoteWorkflowPermissionsTest.php b/tests/Feature/Business/CourierQuoteWorkflowPermissionsTest.php index 85352d52..835a1106 100644 --- a/tests/Feature/Business/CourierQuoteWorkflowPermissionsTest.php +++ b/tests/Feature/Business/CourierQuoteWorkflowPermissionsTest.php @@ -146,7 +146,7 @@ class CourierQuoteWorkflowPermissionsTest extends FeatureTestCase $permissions->save($groups, 'explicit authorization', $this->user->id); $change = $this->pending(); $this->get(route('business.courier-quote-changes.show', $change))->assertOk() - ->assertSeeText('可审核本人发起的变价')->assertSeeText('审核通过')->assertSeeText('撤回修改')->assertSeeText('导出变价明细'); + ->assertSeeText('可审核本人发起的变价')->assertSeeText('审核通过')->assertSeeText('撤回修改')->assertSeeText('导出变价后报价'); $this->get(route('business.courier-quote-changes.index'))->assertOk()->assertSee('name="ids[]"', false); $this->withExceptionHandling()->loginAs($this->people['review-one'], $this->company) ->post(route('business.courier-quote-changes.approve', $change))->assertForbidden(); @@ -229,11 +229,10 @@ class CourierQuoteWorkflowPermissionsTest extends FeatureTestCase $this->assertSame($this->people['init-two']->id, $change->created_by); $this->get(route('business.courier-quote-changes.export', $change))->assertOk()->assertHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); $book = app(\App\Support\CourierQuoteChangeExport::class)->build($change); - $this->assertSame('江苏', $book->getSheetByName('横向变价对比')->getCell('A9')->getValue()); - $this->assertEquals(3.2, $book->getSheetByName('横向变价对比')->getCell('B9')->getValue()); - $this->assertEquals(3.5, $book->getSheetByName('横向变价对比')->getCell('C9')->getValue()); - $this->assertNotNull($book->getSheetByName('原报价全表')); - $this->assertNotNull($book->getSheetByName('新报价全表')); + $this->assertSame(['变价后报价'], $book->getSheetNames()); + $this->assertSame('江苏', $book->getSheet(0)->getCell('A8')->getValue()); + $this->assertEquals(3.5, $book->getSheet(0)->getCell('B8')->getValue()); + $this->assertEquals(0.8, $book->getSheet(0)->getCell('C8')->getValue()); $book->disconnectWorksheets(); $this->post(route('business.courier-quote-changes.withdraw', $change))->assertRedirect(); $this->get(route('business.courier-quote-changes.edit', $change))->assertRedirect(route('business.courier-onboardings.requote', $onboarding)); -- 2.47.2