loadMissing(['change']); $values = $this->decodeMatrix($version->values_json); $nextVersion = CourierQuoteVersion::query() ->where('carrier_name', $version->carrier_name) ->where('quote_type', $version->quote_type) ->where('courier_expcode', $version->courier_expcode) ->where('status', 'approved') ->where('effective_date', '>', $version->effective_date) ->whereNull('end_date') ->orderBy('effective_date') ->first(); // Show each gap explicitly rather than implying continuous execution between the bounds. $others = $version->isTemporary() ? CourierQuoteVersion::query()->with(['signature', 'change'])->where('company_name', $version->company_name) ->where('courier_expcode', $version->courier_expcode) ->when(filled($version->courier_expcode), fn ($query) => $query->where('quote_type', $version->quote_type)) ->where('status', 'approved')->where('id', '!=', $version->id)->get() : collect(); $restorations = []; if ($version->isTemporary()) { foreach ($version->executionPeriods() as $period) { $day = \Illuminate\Support\Carbon::parse($period['end_date'])->addDay(); $restore = CourierRequoteService::resolveAt($others, $day, false); $restorations[] = $day->toDateString() . ' 起(下一临时区间除外)' . ($restore ? '自动恢复原报价 ' . $restore->version_code . '(' . $restore->effective_date->toDateString() . ' 起执行)' : '按当日有效的原长期报价执行;目前未找到可恢复的已签署长期报价'); } } return [ 'version_id' => $version->id, 'version_code' => $version->version_code, 'party_a' => $partyA ?: (company((int) $version->company_id)?->name ?: '当前公司'), 'party_b' => $version->carrier_name, 'carrier_name' => $version->carrier_name, 'quote_type' => $version->quote_type, 'effective_date' => $version->effective_date?->toDateString(), 'period_label' => $version->periodLabel(), 'expiry_date' => $version->isTemporary() ? $version->end_date->toDateString() : ($nextVersion?->effective_date?->copy()->subDay()->toDateString() ?: '长期'), 'temporary_note' => $version->isTemporary() ? '本报价为短期(临时)报价,仅在以下区间执行(均含开始和截止当天):' . $version->periodLabel() . '。区间外沿用原长期报价。' . implode(';', $restorations) . ',无需另行签署。' : null, 'face_fee' => $this->matrixMetric($values, ['面单费', '面单费用', '面单']), 'bubble_coefficient' => $this->matrixMetric($values, ['计泡系数', '泡系数', '计泡']), 'billing_rules' => CourierQuoteDocument::executionText($version->billing_rules, $version->periodLabel()), 'values' => CourierQuoteDocument::values($values, $version->periodLabel()), 'formulas' => $this->decodeMatrix($version->formulas_json), ]; } private function decodeMatrix(?string $json): array { if (blank($json)) { return []; } $decoded = json_decode($json, true); return is_array($decoded) ? $decoded : []; } private function matrixMetric(array $values, array $labels): string { foreach ($values as $rowIndex => $row) { if (! is_array($row)) { continue; } foreach (array_values($row) as $columnIndex => $cell) { $text = trim((string) $cell); foreach ($labels as $label) { if ($text !== $label && ! str_contains($text, $label)) { continue; } $inline = trim((string) preg_replace('/^.*?' . preg_quote($label, '/') . '\\s*[::=]?\\s*/u', '', $text)); if ($inline !== '' && $inline !== $text && $inline !== $label) { return $inline; } $columnValues = []; foreach (array_slice($values, $rowIndex + 1) as $nextRow) { $value = is_array($nextRow) ? ($nextRow[$columnIndex] ?? null) : null; if ($value !== null && trim((string) $value) !== '') { $columnValues[] = trim((string) $value); } } $columnValues = array_values(array_unique($columnValues)); if (count($columnValues) === 1) { return $columnValues[0]; } if (count($columnValues) > 1) { return '详见报价明细'; } $adjacent = $row[$columnIndex + 1] ?? null; if ($adjacent !== null && trim((string) $adjacent) !== '') { return trim((string) $adjacent); } } } } return '按报价明细'; } }