CaiWuXiTong/app/Console/Commands/PlantingReimbursementDailyDigest.php
limeng b5244362a1 feat(种菜报销): 佣金人员可维护、自动放行检查、自动发通知、一键提交钉钉流程
- 报销佣金人员改为工作台维护(新增 / 移除 / 追加导入 / 导出),首次读取时自动导入以前上传的历史名单
- 去掉「去聚工单人工核对、勾选已核对」:改为放行检查(聚工单 20 分钟内同步过、额度已校验、
  规则不为空、自检全过、有可报销单),不通过不许导出 / 发通知 / 提交,并在页面列出原因
- 每天 11:20 汇总任务放行检查通过后自动发不予报销通知(@ 发起人,同一批不重发)
- 一键提交钉钉「种菜报销流程」(PROC-C5159B8D…):发起人李斯(按钉钉花名册查 userId),
  按钉钉表单实际结构自动填(收款费用明细 / 收款合计 / 收款人·开户行·账号=账号详见附件 /
  付款明细 / 报销类型=种菜核销),打款明细 xlsx 上传审批钉盘作附件;先可预览,
  缺必填 / 选项不匹配不许提交;同一账单日只提交一次,结果不确定记待核查
- 新配置 config/planting_reimbursement.php(凭据默认沿用快递入驻的财务系统钉钉应用,
  需补 PLANTING_DINGTALK_AGENT_ID)

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

122 lines
6.5 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 App\Console\Commands;
use App\Models\Common\Company;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Cache;
use Modules\EcommerceFinance\Repositories\PlantingReimbursementRepository as Repository;
use Modules\EcommerceFinance\Services\PlantingReimbursementAuditor as Auditor;
use Modules\EcommerceFinance\Services\PlantingReimbursementGate as Gate;
use Modules\EcommerceFinance\Services\PlantingReimbursementRobot as Robot;
use Modules\EcommerceFinance\Services\PlantingReimbursementWorkbench as Workbench;
use Throwable;
/** 每天 11:20 生成当天种菜报销批次:把可报销合计、要取消的单和放行检查结果推到钉钉群并 @ 报销人,并自动发不予报销通知(@ 发起人)。不改聚工单。 */
class PlantingReimbursementDailyDigest extends Command
{
protected $signature = 'planting-reimbursement:daily-digest
{--date= : 报销日期 Y-m-d,默认今天}
{--dry-run : 只打印消息,不发送}
{--force : 忽略当天已推送标记}';
protected $description = '推送当天种菜报销批次汇总到钉钉群';
public function handle(Workbench $workbench, Robot $robot, Repository $repository): int
{
$date = trim((string) $this->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)];
}
$result = $workbench->build($companyId, $date);
$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 ($gate['ok'] && Gate::check($result)['ok']) {
$lines[] = '- 确认无误后在工作台点「提交钉钉流程」';
}
$lines[] = '- [打开工作台](' . $url . ')';
return [$title, implode("\n", $lines)];
}
}