- 报销佣金人员改为工作台维护(新增 / 移除 / 追加导入 / 导出),首次读取时自动导入以前上传的历史名单 - 去掉「去聚工单人工核对、勾选已核对」:改为放行检查(聚工单 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
166 lines
7.6 KiB
PHP
166 lines
7.6 KiB
PHP
<?php
|
||
|
||
namespace Modules\EcommerceFinance\Services;
|
||
|
||
use Illuminate\Support\Facades\Cache;
|
||
use Illuminate\Support\Facades\Storage;
|
||
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
|
||
use RuntimeException;
|
||
use Throwable;
|
||
|
||
/**
|
||
* 工作台一键提交钉钉「种菜报销流程」:发起人李斯,表单按钉钉实际结构自动填,附件 = 种菜报销明细M.D.xlsx。
|
||
*
|
||
* 防重复:同一账单日只提交一次,结果记在 storage/app/planting-reimbursement-references/<公司>/ding-submissions/<账单日>.json;
|
||
* 结果不确定(超时等)记为「待核查」,同样不允许再次提交,直到在钉钉核查后勾选「确认钉钉里没有」重提。
|
||
*/
|
||
class PlantingDingTalkSubmission
|
||
{
|
||
public function __construct(
|
||
private readonly PlantingDingTalkClient $client,
|
||
private readonly PlantingDingTalkForm $form,
|
||
private readonly PlantingReimbursementDirectory $directory,
|
||
private readonly PlantingReimbursementWorkbookExporter $exporter,
|
||
) {}
|
||
|
||
/** @return array<string,mixed>|null 已提交记录 */
|
||
public function record(int $companyId, string $billDate): ?array
|
||
{
|
||
$disk = Storage::disk('local');
|
||
$path = $this->path($companyId, $billDate);
|
||
$record = $disk->exists($path) ? json_decode((string) $disk->get($path), true) : null;
|
||
|
||
return is_array($record) ? $record : null;
|
||
}
|
||
|
||
/**
|
||
* 预览:读取钉钉表单结构,按本批数据填好,列出缺的 / 不匹配的字段。不上传、不发起。
|
||
*
|
||
* @param array<string,mixed> $result 工作台批次
|
||
* @return array<string,mixed>
|
||
*/
|
||
public function preview(array $result): array
|
||
{
|
||
$settings = $this->client->settings();
|
||
$form = $this->form->build($this->client->formSchema((string) $settings['process_code']), $result['summary'], null, $this->attachmentName($result));
|
||
$originator = $this->originator();
|
||
|
||
return $form + ['originator' => $originator, 'attachment_name' => $this->attachmentName($result), 'total' => $result['summary']['total']];
|
||
}
|
||
|
||
/**
|
||
* 提交。$confirmedNoDuplicate:之前结果「待核查」、已在钉钉确认没有生成时才允许再提交。
|
||
*
|
||
* @param array<string,mixed> $result
|
||
* @return array<string,mixed> 提交记录
|
||
*/
|
||
public function submit(int $companyId, array $result, ?int $userId, bool $confirmedNoDuplicate = false): array
|
||
{
|
||
$gate = PlantingReimbursementGate::check($result);
|
||
if (! $gate['ok']) {
|
||
throw new RuntimeException('暂不能提交:' . implode(';', $gate['reasons']) . '。');
|
||
}
|
||
$billDate = (string) $result['summary']['bill_date'];
|
||
$lock = Cache::lock('planting-dingtalk-submit:' . $companyId . ':' . $billDate, 120);
|
||
if (! $lock->get()) {
|
||
throw new RuntimeException('这一批正在提交,请稍候刷新页面查看结果,不要重复点。');
|
||
}
|
||
|
||
try {
|
||
$existing = $this->record($companyId, $billDate);
|
||
if ($existing !== null && ($existing['status'] === 'submitted' || ! $confirmedNoDuplicate)) {
|
||
throw new RuntimeException($existing['status'] === 'submitted'
|
||
? '这一批(' . $billDate . ')已经在 ' . $existing['submitted_at'] . ' 提交过钉钉流程,不能重复提交。'
|
||
: '上次提交结果不确定,请先到钉钉核查是否已生成审批;确认没有后勾选「已在钉钉确认没有生成」再提交。');
|
||
}
|
||
|
||
$settings = $this->client->settings();
|
||
$originator = $this->originator();
|
||
$schema = $this->client->formSchema((string) $settings['process_code']);
|
||
$check = $this->form->build($schema, $result['summary'], null, $this->attachmentName($result));
|
||
if ($check['missing'] !== [] || $check['invalid'] !== []) {
|
||
throw new RuntimeException('钉钉表单有字段填不上:' . implode('、', array_merge($check['missing'], $check['invalid'])) . '。请先点「预览钉钉表单」查看。');
|
||
}
|
||
|
||
$attachment = $this->client->uploadAttachment($originator['user_id'], $this->attachmentName($result), $this->attachmentContent($result));
|
||
$form = $this->form->build($schema, $result['summary'], $attachment, $this->attachmentName($result));
|
||
$payload = array_filter([
|
||
'originatorUserId' => $originator['user_id'],
|
||
'processCode' => (string) $settings['process_code'],
|
||
'deptId' => $originator['dept_id'],
|
||
'formComponentValues' => $form['values'],
|
||
], static fn ($value): bool => $value !== null && $value !== '');
|
||
|
||
$record = [
|
||
'bill_date' => $billDate, 'total' => $result['summary']['total'], 'lines' => count($result['summary']['ding_lines']),
|
||
'originator' => $originator['name'], 'by' => $userId, 'attachment' => $attachment['fileName'],
|
||
];
|
||
try {
|
||
$instanceId = $this->client->startInstance($payload);
|
||
} catch (PlantingDingTalkUncertainException $exception) {
|
||
$this->save($companyId, $billDate, $record + ['status' => 'uncertain', 'submitted_at' => now()->format('Y-m-d H:i:s'), 'message' => $exception->getMessage()]);
|
||
throw $exception;
|
||
}
|
||
|
||
$record += ['status' => 'submitted', 'instance_id' => $instanceId, 'submitted_at' => now()->format('Y-m-d H:i:s')];
|
||
$this->save($companyId, $billDate, $record);
|
||
|
||
return $record;
|
||
} finally {
|
||
$lock->release();
|
||
}
|
||
}
|
||
|
||
/** @return array{name:string,user_id:string,dept_id:?int} */
|
||
public function originator(): array
|
||
{
|
||
$settings = $this->client->settings();
|
||
$name = trim((string) ($settings['originator_flower_name'] ?? '李斯'));
|
||
$userId = trim((string) ($settings['originator_user_id'] ?? ''));
|
||
if ($userId === '') {
|
||
$userId = (string) ($this->directory->userIds([$name])[PlantingReimbursementAuditor::normName($name)] ?? '');
|
||
}
|
||
if ($userId === '') {
|
||
throw new RuntimeException('钉钉花名册里找不到发起人「' . $name . '」(或同名多人),请配置 PLANTING_DINGTALK_ORIGINATOR_USER_ID。');
|
||
}
|
||
$deptId = trim((string) ($settings['dept_id'] ?? ''));
|
||
if ($deptId === '') {
|
||
try {
|
||
$deptId = (string) ($this->client->deptIds($userId)[0] ?? '');
|
||
} catch (Throwable $exception) {
|
||
report($exception);
|
||
$deptId = '';
|
||
}
|
||
}
|
||
|
||
return ['name' => $name, 'user_id' => $userId, 'dept_id' => $deptId === '' ? null : (int) $deptId];
|
||
}
|
||
|
||
private function attachmentName(array $result): string
|
||
{
|
||
return $this->exporter->fileName('details', $result['summary']);
|
||
}
|
||
|
||
private function attachmentContent(array $result): string
|
||
{
|
||
$path = tempnam(sys_get_temp_dir(), 'planting-details-');
|
||
try {
|
||
(new Xlsx($this->exporter->build('details', $result)))->save($path);
|
||
|
||
return (string) file_get_contents($path);
|
||
} finally {
|
||
@unlink($path);
|
||
}
|
||
}
|
||
|
||
private function save(int $companyId, string $billDate, array $record): void
|
||
{
|
||
Storage::disk('local')->put($this->path($companyId, $billDate), json_encode($record, JSON_UNESCAPED_UNICODE));
|
||
}
|
||
|
||
private function path(int $companyId, string $billDate): string
|
||
{
|
||
return PlantingReimbursementReferences::DIRECTORY . '/' . $companyId . '/ding-submissions/' . preg_replace('/[^0-9-]/', '', $billDate) . '.json';
|
||
}
|
||
}
|