CaiWuXiTong/modules/EcommerceFinance/Services/PayablesFieldNormalizer.php
renshen 813e956df5 应付款终版目录接入独立按表数据
两个「终版」目录(供应商应付(终版)、快递应付(终版))改为独立数据:
保留原有导入、导出、查询、重置、编辑按钮和导入模板,数据初始为空、按表独立存放,
不再借用基础目录的页面与数据。

- 终版目录拥有自己的同名子页面与横向页签
- 新增终版按表数据的独立导入/导出/模板/编辑接口(数据集键 final:<目录>:<页面>)
- 供应商终版 13 个页面、快递终版 4 个页面已接入
- 终版导入模板统一增加“月份”列,记录标识列改为选填
2026-09-29 11:06:41 +08:00

58 lines
2.2 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 Modules\EcommerceFinance\Services;
use Illuminate\Validation\ValidationException;
/** 按字段定义校验并整理按表维护/导入的字段值。 */
class PayablesFieldNormalizer
{
/** @param array<string,array{label:string,type:string}> $fields */
public function normalize(array $fields, array $input): array
{
$values = [];
foreach ($fields as $field => $definition) {
if (! array_key_exists($field, $input)) {
continue;
}
$value = $input[$field];
if ($value === null || trim((string) $value) === '') {
$values[$field] = null;
continue;
}
if ($definition['type'] === 'number') {
$number = str_replace([',', ',', '¥', '¥'], '', trim((string) $value));
if (preg_match('/^-?(?:\d+(?:\.\d{1,4})?|\.\d{1,4})$/', $number) !== 1
|| bccomp(ltrim($number, '-'), '9999999999999999.9999', 4) === 1) {
throw ValidationException::withMessages([
$field => $definition['label'] . '必须是最多 4 位小数的有效数字。',
]);
}
$values[$field] = $number;
continue;
}
$text = trim((string) $value);
if (mb_strlen($text) > 5000) {
throw ValidationException::withMessages([$field => $definition['label'] . '内容过长。']);
}
if ($definition['type'] === 'date' && preg_match('/^\d{4}-\d{2}-\d{2}$/', $text) !== 1) {
throw ValidationException::withMessages([$field => $definition['label'] . '日期格式无效。']);
}
if ($definition['type'] === 'month' && preg_match('/^\d{4}-\d{2}$/', $text) !== 1) {
throw ValidationException::withMessages([$field => $definition['label'] . '月份格式无效。']);
}
$values[$field] = $text;
}
if ($values === []) {
throw ValidationException::withMessages(['values' => '没有可保存的字段。']);
}
return $values;
}
}