93 lines
4.0 KiB
PHP
93 lines
4.0 KiB
PHP
<?php
|
|
|
|
namespace Modules\EcommerceFinance\Services;
|
|
|
|
use Illuminate\Contracts\Filesystem\Factory;
|
|
use Modules\EcommerceFinance\Repositories\PlantingReimbursementRepository;
|
|
use RuntimeException;
|
|
|
|
/** 聚工单导出确认的完成状态补录;只在上游仍返回已核对的旧版本时生效。 */
|
|
class PlantingWorkOrderCompletions
|
|
{
|
|
public const PATH = 'planting-work-order-completions/jiansun.json';
|
|
|
|
public function __construct(private readonly Factory $filesystems) {}
|
|
|
|
/** @param list<array<string,mixed>> $rows @return list<array<string,mixed>> */
|
|
public function apply(array $rows): array
|
|
{
|
|
$disk = $this->filesystems->disk('local');
|
|
if (! $disk->exists(self::PATH)) {
|
|
return $rows;
|
|
}
|
|
$document = json_decode($disk->get(self::PATH), true, 64, JSON_THROW_ON_ERROR);
|
|
if (! is_array($document['rows'] ?? null)) {
|
|
throw new RuntimeException('种菜工单完成补录文档无效。');
|
|
}
|
|
$completions = [];
|
|
foreach ($document['rows'] as $completion) {
|
|
$id = (string) ($completion['Id'] ?? '');
|
|
if (! ctype_digit($id) || (int) $id < 1 || isset($completions[$id])
|
|
|| ! is_string($completion['woNo'] ?? null) || $completion['woNo'] === ''
|
|
|| ! is_string($completion['online_order_no'] ?? null) || $completion['online_order_no'] === ''
|
|
|| ! in_array($completion['expected_status'] ?? '', PlantingReimbursementRepository::OPEN_STATUSES, true)
|
|
|| ! $this->validTime($completion['expected_updated_at'] ?? null)
|
|
|| ! $this->validTime($completion['completed_at'] ?? null)
|
|
|| $completion['completed_at'] < $completion['expected_updated_at']) {
|
|
throw new RuntimeException('种菜工单完成补录缺少有效标识、时间或存在重复。');
|
|
}
|
|
$completions[$id] = $completion;
|
|
}
|
|
|
|
foreach ($rows as &$row) {
|
|
$completion = $completions[(string) ($row['Id'] ?? '')] ?? null;
|
|
if ($completion === null
|
|
|| ($row['woTypeId'] ?? '') !== PlantingReimbursementRepository::WORK_TYPE_ID
|
|
|| ($row['woNo'] ?? '') !== $completion['woNo']
|
|
|| ($row['status'] ?? '') !== $completion['expected_status']
|
|
|| trim((string) ($row['completeTime'] ?? '')) !== ''
|
|
|| $this->time($row['updated'] ?? '') !== $completion['expected_updated_at']
|
|
|| $this->orderNo($row['businessFieldList'] ?? '') !== $completion['online_order_no']) {
|
|
continue;
|
|
}
|
|
// 金额、原始更新时间及表单保留上游值,完成状态和时间有独立导出证据。
|
|
// 上游状态/更新时间改变后不再覆盖,可正常接收后续作废或重新处理。
|
|
$row['status'] = PlantingReimbursementRepository::STATUS_CONFIRMED;
|
|
$row['completeTime'] = $completion['completed_at'];
|
|
}
|
|
unset($row);
|
|
|
|
return $rows;
|
|
}
|
|
|
|
private function validTime(mixed $value): bool
|
|
{
|
|
if (! is_string($value)) return false;
|
|
$date = \DateTimeImmutable::createFromFormat('!Y-m-d H:i:s', $value);
|
|
|
|
return $date !== false && $date->format('Y-m-d H:i:s') === $value;
|
|
}
|
|
|
|
private function time(mixed $value): string
|
|
{
|
|
return substr(str_replace('T', ' ', trim((string) $value)), 0, 19);
|
|
}
|
|
|
|
private function orderNo(mixed $json): string
|
|
{
|
|
$fields = json_decode((string) $json, true);
|
|
if (! is_array($fields)) return '';
|
|
foreach ($fields['businessField'] ?? $fields as $field) {
|
|
if (is_array($field) && ($field['fieldId'] ?? '') === 'orderField') {
|
|
foreach ([$field['valueLabel'] ?? null, $field['value'] ?? null] as $value) {
|
|
if (is_scalar($value) && trim((string) $value) !== '') {
|
|
return trim((string) $value);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return '';
|
|
}
|
|
}
|