CaiWuXiTong/app/Support/CourierQuoteNotes.php

63 lines
2.7 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?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;
}
}