/batches/<报销日>.json;钉钉流程提交后不能再撤销确认。 */ class PlantingReimbursementBatches { /** @return array|null 已确认的批次结果(结构同 PlantingReimbursementWorkbench::build(),另带 batch) */ public function get(int $companyId, string $date): ?array { $disk = Storage::disk('local'); $path = $this->path($companyId, $date); $batch = $disk->exists($path) ? json_decode((string) $disk->get($path), true) : null; return is_array($batch) && isset($batch['summary'], $batch['rows']) ? $batch : null; } /** * @param array $result 刚生成、放行检查已通过的批次结果 * @return array */ public function confirm(int $companyId, string $date, array $result, array $filters, ?int $userId, string $by = 'manual'): array { if ($this->get($companyId, $date) !== null) { throw new RuntimeException($date . ' 这一批已经确认过了。'); } $gate = PlantingReimbursementGate::check($result); if (! $gate['ok']) { throw new RuntimeException('放行检查没通过,不能确认:' . implode(';', $gate['reasons']) . '。'); } $batch = $result + ['batch' => [ 'confirmed_at' => now()->format('Y-m-d H:i:s'), 'confirmed_by' => $userId, 'confirmed_via' => $by, 'paid_from' => $filters['paid_from'] ?? null, 'scope' => $filters['scope'] ?? null, ]]; unset($batch['uploads']); if (! Storage::disk('local')->put($this->path($companyId, $date), json_encode($batch, JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR))) { throw new RuntimeException('批次保存失败。'); } return $batch; } /** 撤销确认(重新取单)。钉钉流程已提交的批次不能撤销。 */ public function discard(int $companyId, string $date, PlantingDingTalkSubmission $dingTalk): void { $batch = $this->get($companyId, $date); if ($batch === null) { throw new RuntimeException('这一天还没有确认批次。'); } $record = $dingTalk->record($companyId, (string) $batch['summary']['bill_date']); if ($record !== null) { throw new RuntimeException('这一批已经提交过钉钉流程(' . ($record['status'] === 'submitted' ? '审批编号 ' . $record['instance_id'] : '结果待核查') . '),不能撤销确认。'); } Storage::disk('local')->delete($this->path($companyId, $date)); } /** * 聚工单操作清单(按手工报销顺序,2026-12-01 财务确认): * 1. 锁单:本批全部工单「待处理 → 处理中」; * 2. 取消:处理中里判为取消的; * 3. 标记打款:可报销的,出纳打款后标记,工单变为已完成。 * 待复查的单锁上后留在处理中,下次批次(默认取待处理 + 处理中)会再带进来。 * * @param array $batch * @param array $statuses 工单号 → 当前状态编码 * @return array> */ public static function operations(array $batch, array $statuses): array { $groups = [ 'lock' => ['label' => '① 锁单(标处理中)', 'action' => '更多操作 → 标处理中', 'decision' => null, 'done' => ['CONFIRMING', 'CONFIRMED', 'DISABLED']], 'cancel' => ['label' => '② 取消工单', 'action' => '取消工单', 'decision' => PlantingReimbursementAuditor::DECISION_CANCEL, 'done' => ['DISABLED']], 'paid' => ['label' => '③ 标记打款(变为已完成)', 'action' => '标记打款', 'decision' => PlantingReimbursementAuditor::DECISION_PASS, 'done' => ['CONFIRMED']], ]; $result = []; foreach ($groups as $key => $group) { $items = []; foreach ($batch['rows'] as $row) { if ($group['decision'] !== null && $row['decision'] !== $group['decision']) { continue; } $status = $statuses[(string) $row['work_order_no']] ?? ''; $items[] = [ 'work_order_no' => (string) $row['work_order_no'], 'online_order_no' => (string) $row['online_order_no'], 'status' => \Modules\EcommerceFinance\Repositories\PlantingReimbursementRepository::STATUS_LABELS[$status] ?? ($status === '' ? '未同步' : $status), 'done' => in_array($status, $group['done'], true), ]; } $done = count(array_filter($items, static fn (array $item): bool => $item['done'])); $result[$key] = $group + [ 'items' => $items, 'done_count' => $done, 'pending' => array_values(array_filter($items, static fn (array $item): bool => ! $item['done'])), 'online_orders' => implode(',', array_values(array_unique(array_column(array_filter($items, static fn (array $item): bool => ! $item['done']), 'online_order_no')))), ]; } return $result; } private function path(int $companyId, string $date): string { return PlantingReimbursementReferences::DIRECTORY . '/' . $companyId . '/batches/' . preg_replace('/[^0-9-]/', '', $date) . '.json'; } }