CaiWuXiTong/modules/EcommerceFinance/Services/PlantingCommissionPeople.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

135 lines
6.0 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\Facades\Storage;
use InvalidArgumentException;
use RuntimeException;
/**
* 种菜报销「报销佣金人员」名单:发起人在名单里才能报佣金。
*
* 2026-12-01 起在工作台维护(新增 / 删除 / 导入),不再每次上传整表。第一次读取时,用以前上传过的历史名单
* (planting-reimbursement-references/<公司>/commission.json)初始化。
* 存为 storage/app/planting-reimbursement-references/<公司>/commission-people.json(持久卷),不建表。
*/
class PlantingCommissionPeople
{
public const FILE = 'commission-people.json';
/** @var array<int,array<string,mixed>> */
private array $loaded = [];
public function __construct(private readonly PlantingReimbursementReferences $references) {}
/** @return array{people:list<array{name:string,platform:string,remark:string,added_at:?string}>,source:string,updated_at:?string} */
public function document(int $companyId): array
{
if (isset($this->loaded[$companyId])) {
return $this->loaded[$companyId];
}
$disk = Storage::disk('local');
$document = $disk->exists($this->path($companyId)) ? json_decode((string) $disk->get($this->path($companyId)), true) : null;
if (! is_array($document) || ! is_array($document['people'] ?? null)) {
// 首次:用历史上传的名单初始化。
$people = [];
foreach (array_keys($this->references->nameSet($companyId, PlantingReimbursementReferences::KIND_COMMISSION)) as $name) {
$people[] = ['name' => (string) $name, 'platform' => '', 'remark' => '历史上传名单导入', 'added_at' => null];
}
$document = ['people' => $people, 'source' => $people === [] ? 'empty' : 'history', 'updated_at' => null];
}
return $this->loaded[$companyId] = $document;
}
/** @return array<string,bool> 姓名集合(给校验用) */
public function nameSet(int $companyId): array
{
$set = [];
foreach ($this->document($companyId)['people'] as $person) {
$set[PlantingReimbursementAuditor::normName($person['name'])] = true;
}
return $set;
}
public function add(int $companyId, string $name, string $platform, string $remark, ?int $userId): string
{
$name = PlantingReimbursementAuditor::normName($name);
if ($name === '' || mb_strlen($name) > 50 || preg_match('/^\d+$/', $name)) {
throw new InvalidArgumentException('请填写发起人花名(不超过 50 个字)。');
}
$people = $this->document($companyId)['people'];
foreach ($people as $person) {
if ($person['name'] === $name) {
throw new InvalidArgumentException($name . ' 已在名单里。');
}
}
$people[] = ['name' => $name, 'platform' => mb_substr(trim($platform), 0, 50), 'remark' => mb_substr(trim($remark), 0, 200), 'added_at' => now()->format('Y-m-d H:i:s')];
$this->store($companyId, $people, $userId);
return $name;
}
public function remove(int $companyId, string $name, ?int $userId): void
{
$name = PlantingReimbursementAuditor::normName($name);
$people = array_values(array_filter($this->document($companyId)['people'], static fn (array $person): bool => $person['name'] !== $name));
if (count($people) === count($this->document($companyId)['people'])) {
throw new InvalidArgumentException($name . ' 不在名单里,请刷新页面后重试。');
}
$this->store($companyId, $people, $userId);
}
/**
* 用表格追加名单(格式同以前上传的「报销佣金人员」表,取右侧「人员 / 平台」)。已有的不重复加。
*
* @return array{added:int,skipped:int}
*/
public function import(int $companyId, string $path, string $fileName, ?int $userId): array
{
$rows = $this->references->parse(PlantingReimbursementReferences::KIND_COMMISSION, $path, $fileName);
$people = $this->document($companyId)['people'];
$existing = array_flip(array_column($people, 'name'));
$added = 0;
foreach ($rows as $row) {
$name = PlantingReimbursementAuditor::normName((string) $row['key']);
if ($name === '' || isset($existing[$name])) {
continue;
}
$people[] = ['name' => $name, 'platform' => '', 'remark' => '导入:' . mb_substr($fileName, 0, 80), 'added_at' => now()->format('Y-m-d H:i:s')];
$existing[$name] = true;
$added++;
}
$this->store($companyId, $people, $userId);
return ['added' => $added, 'skipped' => count($rows) - $added];
}
public function exportCsv(int $companyId): string
{
$lines = ["\xEF\xBB\xBF人员,平台,备注,加入时间"];
foreach ($this->document($companyId)['people'] as $person) {
$lines[] = implode(',', array_map(static fn (?string $value): string => '"' . str_replace('"', '""', (string) $value) . '"', [$person['name'], $person['platform'], $person['remark'], $person['added_at']]));
}
return implode("\n", $lines) . "\n";
}
/** @param list<array<string,mixed>> $people */
private function store(int $companyId, array $people, ?int $userId): void
{
usort($people, static fn (array $a, array $b): int => strcmp($a['name'], $b['name']));
$document = ['people' => array_values($people), 'source' => 'edited', 'updated_at' => now()->format('Y-m-d H:i:s'), 'updated_by' => $userId];
if (! Storage::disk('local')->put($this->path($companyId), json_encode($document, JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR))) {
throw new RuntimeException('报销佣金人员名单保存失败。');
}
$this->loaded[$companyId] = $document;
}
private function path(int $companyId): string
{
return PlantingReimbursementReferences::DIRECTORY . '/' . $companyId . '/' . self::FILE;
}
}