CaiWuXiTong/modules/EcommerceFinance/Services/PlantingReimbursementGate.php
limeng 8e1d9ab778 feat(种菜报销): 确认本批 + 聚工单操作清单,提速商品负责人查询
- 确认本批:把当天校验结果固定下来(每天 11:20 放行检查通过自动确认,也可手动;钉钉已提交前可撤销),
  之后页面、导出、通知、提交钉钉都按确认时的结果,聚工单锁单 / 标记打款后批次不再消失
- 聚工单操作清单(同手工顺序):① 整批锁单(标处理中)② 取消判为取消的 ③ 打款后标记打款(变为已完成),
  给出未完成工单的线上订单号(逗号分隔可直接粘贴查询)和工单编号,进度按同步回来的工单状态核对
- 取单默认改为「待处理 + 处理中」:整批锁单后待复查的单留在处理中,下次批次再带进来
- 商品负责人查询:商品ID 校验后内联为 varchar 字面量(参数按 nvarchar 绑定用不上索引,本机实测 2,271 个商品 181 秒),
  并按商品缓存一天,只查新出现的商品
- 每日群汇总写明本批确认情况和聚工单三步待办数

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UNTa9ohkCsVFACqjz3KMcG
2026-12-01 06:38:03 +00:00

54 lines
2.6 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\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];
}
}