- 报销金额公式:空包费改为加(任务付款+快递费+佣金+京喜差价−扣款+空包费) - 去掉「以旧换新超买家实付」「查不到标签待复查」两项校验 - 种菜额度不再上传看板:按《种菜明细报表生成逻辑》(业务交付包 2026-11-27)自算, 商品ID→ID负责人、职级、店铺信息直接只读查经营分析副本库(JSReportReplica.rpt), 金额取实付金额字段(同看板);单笔/每日/月度超额、未匹配负责人、职级为空、职级未配置、 额度规则未配置全部取消;额度数据取不到时整批待复查 - 额度规则 309 条(脉象导出)随代码初始化,工作台可新增/修改/删除/整体导入/导出 - 利润中心直接读工作平台店铺信息(rpt.fys_shopInfo),查不到改为待复查;去掉店铺信息、看板上传 - 种菜任务工单导入模板执行人统一填李斯 - 每日群汇总推送改到 11:20(部署脚本 cron) - 用看板 11 月 1–27 日 13,941 单对照:规则来源、日/月累计、风险类型逐单一致 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UNTa9ohkCsVFACqjz3KMcG
225 lines
8.3 KiB
PHP
225 lines
8.3 KiB
PHP
<?php
|
||
|
||
namespace Modules\EcommerceFinance\Services;
|
||
|
||
use Illuminate\Support\Facades\Storage;
|
||
use InvalidArgumentException;
|
||
use PhpOffice\PhpSpreadsheet\IOFactory;
|
||
use RuntimeException;
|
||
|
||
/**
|
||
* 种菜报销工作台的参考名单:财务上传的报销佣金人员、花名册在职姓名。
|
||
* (种菜监控看板、店铺信息 2026-11-27 起不再上传:额度由财务系统自算,店铺信息直接读工作平台。)
|
||
* 每次上传整类替换,存为 storage/app/planting-reimbursement-references/<公司>/<类型>.json;
|
||
* 花名册只取「在职」表的姓名列,不保存证件号、手机号等其他列。
|
||
*/
|
||
class PlantingReimbursementReferences
|
||
{
|
||
public const KIND_COMMISSION = 'commission';
|
||
|
||
public const KIND_ROSTER = 'roster';
|
||
|
||
public const KINDS = [
|
||
self::KIND_COMMISSION => '报销佣金人员',
|
||
self::KIND_ROSTER => '花名册(在职)',
|
||
];
|
||
|
||
public const DIRECTORY = 'planting-reimbursement-references';
|
||
|
||
/** @var array<string,array<string,mixed>|null> 本次请求内已读过的名单 */
|
||
private array $loaded = [];
|
||
|
||
/**
|
||
* 解析上传文件。
|
||
*
|
||
* @return list<array{key:string,payload:?array<string,string>}>
|
||
*/
|
||
public function parse(string $kind, string $path, string $fileName): array
|
||
{
|
||
$table = $this->table($kind, $path, $fileName);
|
||
|
||
return match ($kind) {
|
||
self::KIND_COMMISSION => $this->names($table, '人员', '平台', '报销佣金人员表里没找到「人员」列。'),
|
||
self::KIND_ROSTER => $this->names($table, '姓名', null, '花名册「在职」表里没找到「姓名」列。'),
|
||
default => throw new InvalidArgumentException('未知的名单类型。'),
|
||
};
|
||
}
|
||
|
||
/**
|
||
* 整类替换。存成 storage/app 下的 JSON(持久卷),不建表、不写业务库。
|
||
*
|
||
* @param list<array{key:string,payload:?array<string,string>}> $rows
|
||
*/
|
||
public function replace(int $companyId, string $kind, array $rows, string $fileName, ?int $userId): void
|
||
{
|
||
$document = [
|
||
'file_name' => mb_substr($fileName, 0, 191),
|
||
'row_count' => count($rows),
|
||
'uploaded_at' => now()->format('Y-m-d H:i:s'),
|
||
'uploaded_by' => $userId,
|
||
'rows' => $rows,
|
||
];
|
||
$json = json_encode($document, JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR);
|
||
if (! Storage::disk('local')->put($this->path($companyId, $kind), $json)) {
|
||
throw new RuntimeException('名单保存失败。');
|
||
}
|
||
unset($this->loaded[$companyId . ':' . $kind]);
|
||
}
|
||
|
||
/** @return array<string,bool> 姓名集合 */
|
||
public function nameSet(int $companyId, string $kind): array
|
||
{
|
||
$set = [];
|
||
foreach ($this->load($companyId, $kind)['rows'] ?? [] as $row) {
|
||
$set[PlantingReimbursementAuditor::normName((string) $row['key'])] = true;
|
||
}
|
||
|
||
return $set;
|
||
}
|
||
|
||
/** @return array<string,array{file_name:string,row_count:int,uploaded_at:string}|null> */
|
||
public function uploads(int $companyId): array
|
||
{
|
||
$uploads = [];
|
||
foreach (array_keys(self::KINDS) as $kind) {
|
||
$document = $this->load($companyId, $kind);
|
||
$uploads[$kind] = $document === null ? null : [
|
||
'file_name' => (string) ($document['file_name'] ?? ''),
|
||
'row_count' => (int) ($document['row_count'] ?? 0),
|
||
'uploaded_at' => (string) ($document['uploaded_at'] ?? ''),
|
||
];
|
||
}
|
||
|
||
return $uploads;
|
||
}
|
||
|
||
private function path(int $companyId, string $kind): string
|
||
{
|
||
if (! isset(self::KINDS[$kind])) {
|
||
throw new InvalidArgumentException('未知的名单类型。');
|
||
}
|
||
|
||
return self::DIRECTORY . '/' . $companyId . '/' . $kind . '.json';
|
||
}
|
||
|
||
/** @return array<string,mixed>|null */
|
||
private function load(int $companyId, string $kind): ?array
|
||
{
|
||
$cacheKey = $companyId . ':' . $kind;
|
||
if (! array_key_exists($cacheKey, $this->loaded)) {
|
||
$disk = Storage::disk('local');
|
||
$path = $this->path($companyId, $kind);
|
||
$document = $disk->exists($path) ? json_decode((string) $disk->get($path), true) : null;
|
||
$this->loaded[$cacheKey] = is_array($document) ? $document : null;
|
||
}
|
||
|
||
return $this->loaded[$cacheKey];
|
||
}
|
||
|
||
/** @return list<list<string>> 表格(字符串单元格) */
|
||
private function table(string $kind, string $path, string $fileName): array
|
||
{
|
||
$extension = strtolower(pathinfo($fileName, PATHINFO_EXTENSION));
|
||
if ($extension === 'csv') {
|
||
return $this->csv($path);
|
||
}
|
||
if (! in_array($extension, ['xlsx', 'xls'], true)) {
|
||
throw new InvalidArgumentException('只支持 xlsx / xls / csv 文件。');
|
||
}
|
||
|
||
$reader = IOFactory::createReaderForFile($path);
|
||
$reader->setReadDataOnly(true);
|
||
if ($kind === self::KIND_ROSTER && method_exists($reader, 'listWorksheetNames')) {
|
||
$names = $reader->listWorksheetNames($path);
|
||
if (in_array('在职', $names, true)) {
|
||
$reader->setLoadSheetsOnly(['在职']);
|
||
}
|
||
}
|
||
$sheet = $reader->load($path)->getActiveSheet();
|
||
if ($kind === self::KIND_ROSTER && $sheet->getParent()?->getSheetByName('在职') !== null) {
|
||
$sheet = $sheet->getParent()->getSheetByName('在职');
|
||
}
|
||
|
||
$rows = [];
|
||
foreach ($sheet->toArray(null, true, false, false) as $row) {
|
||
$rows[] = array_map(static fn (mixed $v): string => is_scalar($v) ? trim((string) $v) : '', $row);
|
||
}
|
||
|
||
return $rows;
|
||
}
|
||
|
||
/** @return list<list<string>> */
|
||
private function csv(string $path): array
|
||
{
|
||
$content = (string) file_get_contents($path);
|
||
if (str_starts_with($content, "\xEF\xBB\xBF")) {
|
||
$content = substr($content, 3);
|
||
} elseif (! mb_check_encoding($content, 'UTF-8')) {
|
||
// 兼容 GBK 编码的 CSV。
|
||
$content = (string) mb_convert_encoding($content, 'UTF-8', 'GB18030');
|
||
}
|
||
$stream = fopen('php://temp', 'r+');
|
||
fwrite($stream, $content);
|
||
rewind($stream);
|
||
$rows = [];
|
||
while (($row = fgetcsv($stream, 0, ',', '"', '')) !== false) {
|
||
$rows[] = array_map(static fn (mixed $v): string => trim((string) $v), $row);
|
||
}
|
||
fclose($stream);
|
||
|
||
return $rows;
|
||
}
|
||
|
||
/**
|
||
* 在前 10 行里找表头行,返回 [表头行号, 列名 → 列号]。
|
||
*
|
||
* @param list<list<string>> $table
|
||
* @param list<string> $required
|
||
* @return array{0:int,1:array<string,list<int>>}|null
|
||
*/
|
||
private function header(array $table, array $required): ?array
|
||
{
|
||
foreach (array_slice($table, 0, 10, true) as $index => $row) {
|
||
$columns = [];
|
||
foreach ($row as $col => $value) {
|
||
$columns[$value][] = $col;
|
||
}
|
||
if (array_diff($required, array_keys($columns)) === []) {
|
||
return [$index, $columns];
|
||
}
|
||
}
|
||
|
||
return null;
|
||
}
|
||
|
||
/** @return list<array{key:string,payload:null}> */
|
||
private function names(array $table, string $column, ?string $preferNeighbor, string $missing): array
|
||
{
|
||
$header = $this->header($table, [$column]);
|
||
if ($header === null) {
|
||
throw new InvalidArgumentException($missing);
|
||
}
|
||
[$headerRow, $columns] = $header;
|
||
$col = $columns[$column][0];
|
||
// 报销佣金人员表左右各有一组「人员」,以右侧「人员 / 平台」那组为准。
|
||
foreach ($columns[$column] as $candidate) {
|
||
if ($preferNeighbor !== null && ($table[$headerRow][$candidate + 1] ?? '') === $preferNeighbor) {
|
||
$col = $candidate;
|
||
}
|
||
}
|
||
|
||
$names = [];
|
||
foreach (array_slice($table, $headerRow + 1) as $row) {
|
||
$name = PlantingReimbursementAuditor::normName($row[$col] ?? '');
|
||
if ($name !== '' && ! preg_match('/^\d+$/', $name)) {
|
||
$names[$name] = ['key' => $name, 'payload' => null];
|
||
}
|
||
}
|
||
if ($names === []) {
|
||
throw new InvalidArgumentException('文件里没有读到姓名。');
|
||
}
|
||
|
||
return array_values($names);
|
||
}
|
||
}
|