54 lines
2.6 KiB
PHP
54 lines
2.6 KiB
PHP
<?php
|
||
|
||
namespace Modules\EcommerceFinance\Services;
|
||
|
||
use Illuminate\Support\Carbon;
|
||
|
||
/**
|
||
* 种菜报销批次放行检查(2026-12-01 起替代「去聚工单人工核对条数和金额、勾选已核对」):
|
||
* 导出、发群通知、提交钉钉流程前,以下全部满足才放行;不满足时给出原因。
|
||
* - 聚工单镜像最近 20 分钟内同步成功(数据是最新的);
|
||
* - 种菜额度已校验(经营分析副本库可用);
|
||
* - 额度规则不为空;
|
||
* - 金额与条数自检全部通过;
|
||
* - 本批有可报销的工单(导出 / 提交时)。
|
||
* 纯计算类。
|
||
*/
|
||
final class PlantingReimbursementGate
|
||
{
|
||
public const STALE_MINUTES = 20;
|
||
|
||
/**
|
||
* @param array<string,mixed> $result PlantingReimbursementWorkbench::build() 的结果
|
||
* @return array{ok:bool,reasons:list<string>}
|
||
*/
|
||
public static function check(array $result, bool $requirePass = true, ?Carbon $now = null): array
|
||
{
|
||
// 已确认的批次:按确认那一刻的同步状态判断(确认后数据就固定了)。
|
||
$now ??= isset($result['batch']['confirmed_at']) ? Carbon::parse((string) $result['batch']['confirmed_at']) : now();
|
||
$reasons = [];
|
||
$sync = $result['sync'] ?? ['code' => 'unavailable', 'last_succeeded_at' => null];
|
||
$last = $sync['last_succeeded_at'] ?? null;
|
||
if (($sync['code'] ?? '') !== 'ready' || $last === null) {
|
||
$reasons[] = '聚工单数据同步异常(最后成功:' . ($last ? substr((string) $last, 0, 16) : '无') . ')';
|
||
} elseif ($now->diffInMinutes(Carbon::parse((string) $last), true) > self::STALE_MINUTES) {
|
||
$reasons[] = '聚工单数据超过 ' . self::STALE_MINUTES . ' 分钟没同步(最后成功:' . substr((string) $last, 0, 16) . ')';
|
||
}
|
||
if (trim((string) ($result['quota_error'] ?? '')) !== '') {
|
||
$reasons[] = '种菜额度没有校验(' . $result['quota_error'] . ')';
|
||
}
|
||
if (($result['quota_rules']['count'] ?? 1) === 0) {
|
||
$reasons[] = '没有种菜额度规则';
|
||
}
|
||
$failed = array_column(array_filter($result['summary']['checks'] ?? [], static fn (array $check): bool => ! $check['ok']), 'label');
|
||
if ($failed !== []) {
|
||
$reasons[] = '自检未通过:' . implode('、', $failed);
|
||
}
|
||
if ($requirePass && (int) ($result['summary']['counts'][PlantingReimbursementAuditor::DECISION_PASS] ?? 0) === 0) {
|
||
$reasons[] = '本批没有可报销的工单';
|
||
}
|
||
|
||
return ['ok' => $reasons === [], 'reasons' => $reasons];
|
||
}
|
||
}
|