option('date')) ?: Workbench::defaultDate(); try { Workbench::parseDate($date); } catch (Throwable $exception) { $this->error($exception->getMessage()); return self::FAILURE; } $failed = false; foreach (Company::enabled()->orderBy('id')->get() as $company) { $company->makeCurrent(); if (! $robot->enabled()) { $this->line("#{$company->id}: 未配置种菜报销群机器人,跳过"); continue; } $lock = "planting-reimbursement:digest:{$company->id}:{$date}"; if (! $this->option('dry-run') && ! $this->option('force') && ! Cache::add($lock, now()->toDateTimeString(), now()->addDay())) { $this->line("#{$company->id}: {$date} 已推送过"); continue; } try { [$title, $text] = $this->message($workbench, $repository, $robot, (int) $company->id, $date, ! $this->option('dry-run')); if ($this->option('dry-run')) { $this->line($title . "\n" . $text); continue; } $robot->sendDigest($title, $text); $this->info("#{$company->id}: 已推送 {$date} 汇总"); } catch (Throwable $exception) { Cache::forget($lock); report($exception); $this->error("#{$company->id}: 推送失败:" . $exception->getMessage()); $failed = true; } } return $failed ? self::FAILURE : self::SUCCESS; } /** @return array{0:string,1:string} */ private function message(Workbench $workbench, Repository $repository, Robot $robot, int $companyId, string $date, bool $sendNotices): array { $sync = $repository->syncStatus(); $url = route('business.pages.show', ['company_id' => $companyId, 'section' => 'planting-data', 'page' => 'planting-reimbursement-workbench', 'date' => $date]); $title = '种菜报销 ' . $date . ' 批次'; $lines = ['#### ' . $title]; $stale = $sync['code'] !== 'ready' || $sync['last_succeeded_at'] === null || now()->diffInMinutes(\Illuminate\Support\Carbon::parse($sync['last_succeeded_at']), true) > 20; if ($stale) { $lines[] = '> **聚工单数据不是最新的**(最后同步成功:' . ($sync['last_succeeded_at'] ? substr($sync['last_succeeded_at'], 0, 16) : '无') . '),先别按下面结果报销。'; } if (! in_array($sync['code'], ['ready', 'failed'], true)) { $lines[] = '- 聚工单镜像未就绪,暂时无法生成批次。'; $lines[] = '- [打开工作台](' . $url . ')'; return [$title, implode("\n", $lines)]; } // 已确认的批次直接用;没确认的现取现算,放行检查通过就自动确认本批(之后锁单、取消、标记打款都按这份来)。 $batches = app(Batches::class); $result = $batches->get($companyId, $date); $confirmedNow = false; if ($result === null) { $result = $workbench->build($companyId, $date); if ($sendNotices && Gate::check($result)['ok']) { $result = $batches->confirm($companyId, $date, $result, ['paid_from' => Workbench::defaultPaidFrom($date), 'scope' => Workbench::DEFAULT_SCOPE], null, 'auto'); $confirmedNow = true; } } $s = $result['summary']; $lines[] = '- 待处理(付款 ' . substr($result['window']['from'], 0, 10) . ' ~ ' . substr($result['window']['to'], 0, 10) . '):**' . $s['source']['count'] . ' 条 / ' . number_format((float) $s['source']['amount'], 2) . ' 元**'; $lines[] = '- 可报销:' . $s['counts'][Auditor::DECISION_PASS] . ' 条 / **' . number_format((float) $s['total'], 2) . ' 元**(三大天猫 ' . number_format((float) $s['group_amounts'][Auditor::GROUP_BIG_TMALL], 2) . ',其他 ' . number_format((float) $s['group_amounts'][Auditor::GROUP_OTHER], 2) . ')'; $lines[] = '- 取消工单:' . $s['counts'][Auditor::DECISION_CANCEL] . ' 条;待复查:' . $s['counts'][Auditor::DECISION_RECHECK] . ' 条'; $bad = array_column(array_filter($s['checks'], static fn (array $check): bool => ! $check['ok']), 'label'); $lines[] = '- 自检:' . ($bad === [] ? '全部通过' : '未通过 ' . implode('、', $bad)); if (($result['quota_error'] ?? '') !== '') { $lines[] = '- **种菜额度没有校验**(' . $result['quota_error'] . '),相关工单都在待复查,先别报销'; } if (($result['shop_source'] ?? '') === 'mirror') { $lines[] = '- 工作平台店铺信息暂时读不到,利润中心用的是前一天同步的数据'; } if (($result['roster_source'] ?? '') === 'none') { $lines[] = '- 取不到在职名单,收款人在职未校验'; } // 自动发取消 / 待复查通知(@ 发起人;同一批已发过的不重发),放行检查不过就不发,等人看。 $gate = Gate::check($result, false); if (! $gate['ok']) { $lines[] = '- **暂不能报销**:' . implode(';', $gate['reasons']); } elseif ($s['notices'] === []) { $lines[] = '- 没有要取消或复查的工单'; } elseif ($sendNotices) { $sent = $robot->sendNotices($companyId, $date, $s['notices']); $lines[] = sprintf('- 已自动发不予报销通知:%d 人(之前已发 %d 人%s)', $sent['sent'], $sent['skipped'], $sent['failed'] > 0 ? ',失败 ' . $sent['failed'] . ' 人' : ''); } if (isset($result['batch'])) { $lines[] = '- 本批' . ($confirmedNow ? '已自动确认' : '已于 ' . substr((string) $result['batch']['confirmed_at'], 11, 5) . ' 确认') . ',聚工单待处理:① 整批锁单 ' . $s['source']['count'] . ' 单 ② 取消 ' . $s['counts'][Auditor::DECISION_CANCEL] . ' 单 ③ 打款后标记打款 ' . $s['counts'][Auditor::DECISION_PASS] . ' 单'; $lines[] = '- 标记打款后在工作台点「提交钉钉流程」'; } $lines[] = '- [打开工作台](' . $url . ')'; return [$title, implode("\n", $lines)]; } }