diff --git a/app/Http/Controllers/Business/CourierQuoteChanges.php b/app/Http/Controllers/Business/CourierQuoteChanges.php index 63040157a..967ce31e7 100644 --- a/app/Http/Controllers/Business/CourierQuoteChanges.php +++ b/app/Http/Controllers/Business/CourierQuoteChanges.php @@ -381,6 +381,12 @@ class CourierQuoteChanges extends Controller 'summary' => ['required', 'string', 'min:2', 'max:2000'], 'quote_notes' => ['sometimes', 'array', 'max:500'], 'quote_notes.*' => ['nullable', 'string', 'max:10000'], + 'province_additions' => ['sometimes', 'array', 'max:34'], + 'province_additions.*' => ['array:region,prices,note'], + 'province_additions.*.region' => ['required', 'string', 'max:16', 'regex:/^[\p{Han}]+$/u'], + 'province_additions.*.prices' => ['required', 'array'], + 'province_additions.*.prices.*' => ['required', 'numeric', 'min:0', 'decimal:0,4'], + 'province_additions.*.note' => ['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'], diff --git a/app/Models/Business/CourierQuoteChange.php b/app/Models/Business/CourierQuoteChange.php index 6fb7d8a5f..49a8d3489 100644 --- a/app/Models/Business/CourierQuoteChange.php +++ b/app/Models/Business/CourierQuoteChange.php @@ -72,7 +72,7 @@ class CourierQuoteChange extends Model public function hasQuotePayload(): bool { - return is_array($this->quote_payload) && array_diff_key($this->quote_payload, ['execution_periods' => true, 'quote_notes' => true]) !== []; + return is_array($this->quote_payload) && array_diff_key($this->quote_payload, ['execution_periods' => true, 'quote_notes' => true, 'province_additions' => true]) !== []; } public function isStockQuote(): bool diff --git a/app/Support/CourierQuoteChangeExport.php b/app/Support/CourierQuoteChangeExport.php index e55ec91c3..805a683ea 100644 --- a/app/Support/CourierQuoteChangeExport.php +++ b/app/Support/CourierQuoteChangeExport.php @@ -95,7 +95,10 @@ final class CourierQuoteChangeExport $values[$item->source_row][$item->source_column] = $item->new_amount; } - return [$values, $source['billing_rules'] ?? null]; + $source['values'] = $values; + $source = CourierQuoteProvinceAdditions::apply($source, $change->quote_payload['province_additions'] ?? []); + + return [$source['values'], $source['billing_rules'] ?? null]; } private function text(Worksheet $sheet, string $address, mixed $value): void diff --git a/app/Support/CourierQuoteChangeService.php b/app/Support/CourierQuoteChangeService.php index c28f04ea3..1e17c4d2c 100644 --- a/app/Support/CourierQuoteChangeService.php +++ b/app/Support/CourierQuoteChangeService.php @@ -29,6 +29,11 @@ final class CourierQuoteChangeService $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); + unset($data['quote_payload']['province_additions']); + if (!empty($data['province_additions'])) { + CourierQuoteProvinceAdditions::apply($source, $data['province_additions']); + $data['quote_payload']['province_additions'] = $data['province_additions']; + } } return DB::transaction(function () use ($data, $userId, $source): CourierQuoteChange { @@ -72,6 +77,11 @@ final class CourierQuoteChangeService $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); + unset($data['quote_payload']['province_additions']); + if (!empty($data['province_additions'])) { + CourierQuoteProvinceAdditions::apply($source, $data['province_additions']); + $data['quote_payload']['province_additions'] = $data['province_additions']; + } } return DB::transaction(function () use ($change, $data, $userId, $source): CourierQuoteChange { @@ -103,7 +113,7 @@ final class CourierQuoteChangeService { $this->ensureEditableBy($change, $userId); - if (! ($change->isStockQuote() && filled($change->proposed_values_json)) && $change->items()->count() === 0 && empty($change->quote_payload['quote_notes'])) { + if (! ($change->isStockQuote() && filled($change->proposed_values_json)) && $change->items()->count() === 0 && empty($change->quote_payload['quote_notes']) && empty($change->quote_payload['province_additions'])) { throw ValidationException::withMessages(['items' => '至少需要一条发生变化的报价明细。']); } @@ -231,6 +241,13 @@ final class CourierQuoteChangeService $values[$item->source_row][$item->source_column] = $item->new_amount; } + $expanded = CourierQuoteProvinceAdditions::apply( + ['values' => $values, 'formulas' => $formulas], + $change->quote_payload['province_additions'] ?? [] + ); + $values = $expanded['values']; + $formulas = $expanded['formulas']; + $importedVersionCount = collect($this->snapshot->load()['quotes'] ?? []) ->whereNull('source_change_id') ->count(); @@ -351,7 +368,7 @@ final class CourierQuoteChangeService ]); } - if ($change->items()->count() === 0 && empty($change->quote_payload['quote_notes'])) { + if ($change->items()->count() === 0 && empty($change->quote_payload['quote_notes']) && empty($change->quote_payload['province_additions'])) { throw ValidationException::withMessages(['items' => '至少需要一条原价格与新价格不同的变价明细。']); } } @@ -399,6 +416,7 @@ final class CourierQuoteChangeService 'end_date' => $change->end_date?->toDateString(), 'execution_periods' => $change->executionPeriods(), 'quote_notes' => $change->quote_payload['quote_notes'] ?? [], + 'province_additions' => $change->quote_payload['province_additions'] ?? [], 'is_retrospective' => $change->isRetrospective(), 'recalculation_status' => $change->recalculation_status, 'recalculation_note' => $change->recalculation_note, diff --git a/app/Support/CourierQuoteProvinceAdditions.php b/app/Support/CourierQuoteProvinceAdditions.php new file mode 100644 index 000000000..8f981276b --- /dev/null +++ b/app/Support/CourierQuoteProvinceAdditions.php @@ -0,0 +1,64 @@ + $label) { + if ($column === 0 || blank($label)) continue; + if (preg_match('/^(报价备注|备注|说明)[::]?$/u', trim($label))) { + $remarks = $column; + } elseif (collect($rows)->contains(fn ($row) => is_numeric($row[$column] ?? null))) { + $columns[$column] = (string) $label; + } + } + + return [$columns, $remarks, array_map(fn ($row) => (string) ($row[0] ?? ''), $rows), $layout['row_keys']]; + } + + public static function apply(array $source, array $additions): array + { + if ($additions === []) return $source; + + [$columns, $remarks, $regions, $rowKeys] = self::fields($source); + $normalize = static fn ($name) => preg_replace('/(?:壮族自治区|回族自治区|维吾尔自治区|自治区|特别行政区|省|市)$/u', '', trim($name)); + $existing = array_map($normalize, preg_split('/[、,,\/\s;;++]+/u', implode('、', $regions), -1, PREG_SPLIT_NO_EMPTY)); + $rows = []; + foreach ($additions as $index => $addition) { + $region = $normalize($addition['region']); + if ($region === '' || in_array($region, $existing, true)) { + throw ValidationException::withMessages(["province_additions.$index.region" => '省份已存在,请直接编辑原报价行。']); + } + if ($columns === [] || array_keys($columns) !== array_keys(array_intersect_key($columns, $addition['prices'])) || array_diff_key($addition['prices'], $columns)) { + throw ValidationException::withMessages(["province_additions.$index.prices" => '请填写该省份的全部价格列。']); + } + $existing[] = $region; + $row = array_fill(0, max(array_keys($columns)) + 1, null); + $row[0] = $addition['region']; + foreach ($columns as $column => $label) { + $row[$column] = (float) $addition['prices'][$column]; + } + if ($remarks !== null) $row[$remarks] = $addition['note'] ?? null; + ksort($row); + $rows[] = array_replace(array_fill(0, max(array_keys($row)) + 1, null), $row); + } + + $offset = (int) end($rowKeys) + 1; + $formulas = array_replace(array_fill(0, count($source['values']), []), $source['formulas']); + array_splice($source['values'], $offset, 0, $rows); + array_splice($formulas, $offset, 0, array_fill(0, count($rows), [])); + $source['formulas'] = $formulas; + + return $source; + } +} diff --git a/resources/views/business/pages/courier-payables/quote-change-editor.blade.php b/resources/views/business/pages/courier-payables/quote-change-editor.blade.php index ce5816050..5f12c95be 100644 --- a/resources/views/business/pages/courier-payables/quote-change-editor.blade.php +++ b/resources/views/business/pages/courier-payables/quote-change-editor.blade.php @@ -202,6 +202,21 @@ + @php([$provinceColumns, $provinceRemarks] = \App\Support\CourierQuoteProvinceAdditions::fields($source)) + @if($provinceColumns) +
+

仅新增原报价没有的省份。新增报价随本次变价审核生效。

+
+ @foreach(old('province_additions', $change?->quote_payload['province_additions'] ?? []) as $provinceIndex => $province) + @include('business.pages.courier-payables.quote-change-province-fields') + @endforeach +
+ + +
+ @endif @@ -227,6 +242,25 @@ var count = root.querySelector('[data-changed-count]'); var effectiveDate = root.querySelector('[data-effective-date]'); var retrospectiveAlert = root.querySelector('[data-retrospective-alert]'); + const provinceEditor = root.querySelector('[data-province-editor]'); + if (provinceEditor) { + const provinceRows = provinceEditor.querySelector('[data-province-rows]'); + const provinceTemplate = provinceEditor.querySelector('[data-province-template]'); + let nextProvinceIndex = Math.max(-1, ...Array.from(provinceRows.children, row => Number(row.dataset.provinceIndex))) + 1; + provinceEditor.querySelector('[data-add-province]').addEventListener('click', function () { + provinceRows.insertAdjacentHTML('beforeend', provinceTemplate.innerHTML.replaceAll('__INDEX__', String(nextProvinceIndex++))); + const newRow = provinceRows.lastElementChild; + newRow.querySelectorAll('[data-field-name]').forEach(field => { + field.name = field.dataset.fieldName; + }); + newRow.querySelector('input').focus(); + }); + provinceRows.addEventListener('click', function (event) { + const removeButton = event.target.closest('[data-remove-province]'); + if (removeButton) removeButton.closest('fieldset').remove(); + }); + } + function refreshRetrospective() { if (!effectiveDate || !retrospectiveAlert) return; var isRetrospective = effectiveDate.value !== '' && effectiveDate.value < root.dataset.today; diff --git a/resources/views/business/pages/courier-payables/quote-change-province-fields.blade.php b/resources/views/business/pages/courier-payables/quote-change-province-fields.blade.php new file mode 100644 index 000000000..d1e5fd2f7 --- /dev/null +++ b/resources/views/business/pages/courier-payables/quote-change-province-fields.blade.php @@ -0,0 +1,17 @@ +
+ 新增省份 · 原报价无 + + @foreach($provinceColumns as $column => $label) + + @endforeach + @if($provinceRemarks !== null) + + @endif + +
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 08beb1f1c..759c8b043 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 @@ -106,6 +106,16 @@
签署状态
{{ $change->version?->signatureLabel() ?? '未进入签署' }}
变更依据与说明
{{ $change->summary }}
+ @if($change->quote_payload['province_additions'] ?? []) +
+ 新增省份 + +
+ @endif @if($change->quote_payload['quote_notes'] ?? [])
报价备注修改 @foreach($change->quote_payload['quote_notes'] as $note) diff --git a/tests/Feature/Business/CourierQuoteChangeWorkflowTest.php b/tests/Feature/Business/CourierQuoteChangeWorkflowTest.php index 8d1346709..2061aaaf5 100644 --- a/tests/Feature/Business/CourierQuoteChangeWorkflowTest.php +++ b/tests/Feature/Business/CourierQuoteChangeWorkflowTest.php @@ -255,6 +255,32 @@ class CourierQuoteChangeWorkflowTest extends FeatureTestCase $this->assertSame('QB-002', app(QuoteManagementSnapshot::class)->load()['quotes'][1]['id']); } + public function test_a_province_can_be_added_without_changing_existing_prices(): void + { + $payload = $this->validPayload(); + $payload['items'][0]['new_amount'] = $payload['items'][0]['original_amount']; + $payload['province_additions'] = [['region' => '浙江', 'prices' => [1 => '0', 2 => '1.2'], 'note' => '新增线路']]; + + $duplicate = $payload; + $duplicate['province_additions'][0]['region'] = '江苏'; + $this->withExceptionHandling()->loginAs()->post(route('business.courier-quote-changes.store'), $duplicate) + ->assertSessionHasErrors('province_additions.0.region'); + $this->withoutExceptionHandling(); + + $this->post(route('business.courier-quote-changes.store'), $payload)->assertSessionHasNoErrors(); + $change = CourierQuoteChange::firstOrFail(); + $this->assertCount(0, $change->items); + $this->get(route('business.courier-quote-changes.edit', $change))->assertOk()->assertSee('浙江'); + $this->post(route('business.courier-quote-changes.submit', $change))->assertSessionHasNoErrors(); + + $this->loginAs($this->createReviewer(), $this->company) + ->post(route('business.courier-quote-changes.approve', $change)) + ->assertSessionHasNoErrors(); + $values = json_decode(CourierQuoteVersion::firstOrFail()->values_json, true, 512, JSON_THROW_ON_ERROR); + $this->assertEquals(['浙江', 0, 1.2, '新增线路'], $values[3]); + $this->assertCount(3, app(QuoteManagementSnapshot::class)->load()['quotes'][0]['values']); + } + public function test_approving_historical_change_blocks_automatic_compensation_and_records_reason(): void { $payload = $this->validPayload();