CaiWuXiTong/modules/EcommerceFinance/Repositories/PlantingReimbursementRepository.php
limeng cc40d9dcfe feat(种菜报销): 额度改为财务系统自算,按 11-27 财务确认调整校验口径
- 报销金额公式:空包费改为加(任务付款+快递费+佣金+京喜差价−扣款+空包费)
- 去掉「以旧换新超买家实付」「查不到标签待复查」两项校验
- 种菜额度不再上传看板:按《种菜明细报表生成逻辑》(业务交付包 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
2026-12-01 05:49:55 +00:00

548 lines
22 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\Repositories;
use Carbon\CarbonImmutable;
use Illuminate\Database\ConnectionInterface;
use Illuminate\Database\Query\Builder;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Schema;
use InvalidArgumentException;
use Modules\EcommerceFinance\Contracts\BusinessMirrorDataset;
use Modules\EcommerceFinance\Services\BusinessMirrorDatasetRegistry;
use Modules\EcommerceFinance\Services\PlantingReimbursementCorrections;
use RuntimeException;
use Throwable;
/**
* 聚水潭聚工单「打款工单 → 种菜报销」:只读工单镜像 jiansun.dbo.jst_workOrderInfo。
*
* 口径(2026-10-19 财务确认):工单类型“种菜报销”、工单状态“已完成”,按完成时间筛选,
* 自 2026-09-02 00:00 起接入。种菜报销都是当天报销前一天的明细,所以 M 月种菜报销明细
* = 完成时间 [M 月 2 日 00:00, M+1 月 2 日 00:00)。同一工单号只取最新一版。
*/
class PlantingReimbursementRepository
{
public const DATASET = 'jiansun.dbo.jst_workOrderInfo';
public const WORK_TYPE_ID = '6F03128403F2442CABCF283B59FAD88D';
public const STATUS_CONFIRMED = 'CONFIRMED';
/** 聚工单「待处理」(锁单前)。 */
public const STATUS_PENDING = 'WAIT_CONFIRM';
/** 聚工单「处理中」(已锁单)。 */
public const STATUS_LOCKED = 'CONFIRMING';
public const OPEN_STATUSES = [self::STATUS_PENDING, self::STATUS_LOCKED];
/** 锁单时 ERP订单状态不勾的项:取消、删除、为空。 */
public const EXCLUDED_ERP_STATUSES = ['', '已取消', '取消', 'Cancelled', 'Delete', 'Deleted', '已删除', '删除'];
public const STATUS_LABELS = [
'CONFIRMED' => '已完成', 'DISABLED' => '已取消', 'WAIT_CONFIRM' => '待处理', 'CONFIRMING' => '处理中',
'WAIT_AUDIT' => '待审核', 'REJECT_AUDIT' => '审核驳回',
];
public const START_AT = '2026-09-02 00:00:00';
public const FIRST_MONTH = '2026-09';
/** 聚工单只返回 fieldId;对应关系由历史种菜明细逐列对撞及打款工单列表核对得出。 */
public const FIELDS = [
'online_order_no' => 'orderField',
'internal_order_no' => 'o_id',
'platform' => 'platform',
'shop_name' => 'shopField',
'actual_paid_amount' => 'actualPaidAmountField',
'buyer_paid_amount' => 'buyerPaidAmount',
'task_remark' => 'TEXTField1413856620251113150546',
'seller_remark' => 'businessRemarkField',
'task_payment' => 'NUMBERField1413856620250924131615',
'task_freight' => 'NUMBERField1413856620251113150537',
'task_commission' => 'NUMBERField1413856620251230154457',
'jingxi_price_diff' => 'NUMBERField1413856620250924131623',
'task_deduction' => 'NUMBERField1413856620251113150612',
'task_empty_package_fee' => 'NUMBERField1413856620251113145620',
'reimbursement_amount' => 'NUMBERField1413856620251117101250',
'payee_name' => 'TEXTField1413856620251117180931',
'payee_account' => 'TEXTField1413856620251117113451',
'payee_bank' => 'TEXTField1413856620251117112729',
'payee_mobile' => 'TEXTField1413856620260618171005',
'erp_order_label' => 'erpOrderLabel',
'erp_order_status' => 'erpOrderStatus',
'platform_order_status' => 'platformOrderStatus',
'paid_at' => 'payDateField',
'after_sale_no' => 'asid',
];
public const MONEY_FIELDS = [
'actual_paid_amount', 'buyer_paid_amount', 'task_payment', 'task_freight', 'task_commission', 'jingxi_price_diff',
'task_deduction', 'task_empty_package_fee', 'reimbursement_amount',
];
private const ERP_ORDER_STATUS = [
'Sent' => '已发货', 'Merged' => '被合并', 'Cancelled' => '已取消', 'Split' => '被拆分',
'WaitConfirm' => '待审核', 'WaitFConfirm' => '待财审', 'Delivering' => '发货中',
'Question' => '异常', 'WaitOuterSent' => '等供销商发货',
];
/** 额度计算只用到这些字段。 */
private const QUOTA_FIELDS = [
'work_order_no', 'status_code', 'paid_at', 'shop_name', 'platform', 'platform_product_id', 'actual_paid_amount', 'reimbursement_amount',
];
private const ROWS_CACHE_SECONDS = 600;
private const QUERY_TIMEOUT_SECONDS = 90;
private readonly ConnectionInterface $connection;
public function __construct(
private readonly BusinessMirrorDatasetRegistry $datasets,
private readonly GenericBusinessMirrorRepository $mirror,
private readonly PlantingReimbursementCorrections $corrections,
) {
$this->connection = $mirror->connection();
}
/**
* 报销月份 → 完成时间窗口 [M-02 00:00, M+1-02 00:00)。
*
* @return array{from:string,to:string}
*/
public static function window(string $month): array
{
if (preg_match('/^\d{4}-(0[1-9]|1[0-2])$/', $month) !== 1 || $month < self::FIRST_MONTH) {
throw new InvalidArgumentException('报销月份无效。');
}
$start = CarbonImmutable::createFromFormat('!Y-m-d', $month . '-02');
return ['from' => $start->format('Y-m-d H:i:s'), 'to' => $start->addMonthNoOverflow()->format('Y-m-d H:i:s')];
}
/** 默认月份:完成时间窗口已开始的最近一个报销月(每月 1 日仍算上月)。 */
public static function defaultMonth(?CarbonImmutable $now = null): string
{
$now ??= CarbonImmutable::now();
$month = $now->subDay()->format('Y-m');
return max($month, self::FIRST_MONTH);
}
/** @return list<string> 可选报销月份(新到旧) */
public static function months(?CarbonImmutable $now = null): array
{
$months = [];
$cursor = CarbonImmutable::createFromFormat('!Y-m', self::defaultMonth($now));
while ($cursor->format('Y-m') >= self::FIRST_MONTH) {
$months[] = $cursor->format('Y-m');
$cursor = $cursor->subMonthNoOverflow();
}
return $months;
}
/** @return array{code:string,last_succeeded_at:?string} */
public function syncStatus(): array
{
try {
$schema = Schema::connection($this->connection->getName());
if (! $schema->hasTable('business_mirror_table_states') || ! $schema->hasTable($this->dataset()->targetTable())) {
return ['code' => 'unavailable', 'last_succeeded_at' => null];
}
$state = $this->mirror->state($this->dataset());
} catch (Throwable) {
return ['code' => 'unavailable', 'last_succeeded_at' => null];
}
if (! is_array($state)) {
return ['code' => 'unavailable', 'last_succeeded_at' => null];
}
$last = isset($state['last_succeeded_at']) ? (string) $state['last_succeeded_at'] : null;
if (($state['initial_completed_at'] ?? null) === null) {
return ['code' => ($state['status'] ?? '') === 'failed' ? 'initial_failed' : 'initializing', 'last_succeeded_at' => $last];
}
return ['code' => ($state['status'] ?? '') === 'failed' ? 'failed' : 'ready', 'last_succeeded_at' => $last];
}
/**
* 某报销月份的全部种菜报销(已完成,每个工单号取最新一版),按完成时间倒序。
* 结果按月份缓存 10 分钟;单月约 1.5~2 万行。
*
* @return list<array<string,mixed>>
*/
public function rows(string $month): array
{
$window = self::window($month);
$status = $this->syncStatus();
if (! in_array($status['code'], ['ready', 'failed'], true)) {
throw new RuntimeException('business_mirror_dataset_not_ready');
}
// 镜像每几分钟增量一次,按版本做键会让缓存几乎每次失效;固定按月份缓存 10 分钟。
$key = 'planting-reimbursements:rows:v2:' . $month;
$cached = Cache::get($key);
if (is_array($cached)) {
return $this->corrections->apply($cached, (int) company_id(), $month);
}
$rows = $this->load($window['from'], $window['to']);
try {
Cache::put($key, $rows, self::ROWS_CACHE_SECONDS);
} catch (Throwable $exception) {
report($exception);
}
return $this->corrections->apply($rows, (int) company_id(), $month);
}
/**
* 种菜报销工作台:付款时间落在 [$paidFrom, $paidTo] 的种菜工单(每个工单号取最新一版,且最新一版状态在 $statuses 中),
* 并按锁单口径排除 ERP订单状态为取消 / 删除 / 空、以及有售后单号的单。
* $completedWindow 用于按完成时间回放某一批已报销的单做对照。
*
* @param list<string> $statuses
* @param array{from:string,to:string}|null $completedWindow
* @return list<array<string,mixed>>
*/
public function pendingRows(string $paidFrom, string $paidTo, array $statuses = self::OPEN_STATUSES, ?array $completedWindow = null): array
{
$status = $this->syncStatus();
if (! in_array($status['code'], ['ready', 'failed'], true)) {
throw new RuntimeException('business_mirror_dataset_not_ready');
}
$table = $this->dataset()->targetTable();
// 工单都在订单付款之后发起,用发起时间先收窄范围(走索引),再按表单里的付款时间精确过滤。
$query = $this->connection->table($table)
->where('woTypeId', self::WORK_TYPE_ID)
->where('created', '>=', $paidFrom)
->select(['Id', 'woNo', 'status', 'createby', 'executors', 'completer', 'completeTime', 'created', 'businessFieldList'])
->orderByDesc('Id');
if ($completedWindow !== null) {
$query->where('completeTime', '>=', $completedWindow['from'])->where('completeTime', '<', $completedWindow['to']);
}
$latest = [];
foreach ($this->boundedGet($query) as $record) {
$woNo = (string) $record->woNo;
if (! isset($latest[$woNo]) || (int) $record->Id > (int) $latest[$woNo]->Id) {
$latest[$woNo] = $record;
}
}
$newestIds = $this->newestIds(array_keys($latest));
$rows = [];
foreach ($latest as $woNo => $record) {
if (($newestIds[(string) $woNo] ?? (int) $record->Id) > (int) $record->Id
|| ! in_array((string) $record->status, $statuses, true)) {
continue;
}
$row = $this->decorate($record);
$paidAt = str_replace('/', '-', (string) $row['paid_at']);
if ($paidAt === '' || $paidAt < $paidFrom || $paidAt > $paidTo) {
continue;
}
// 与聚工单锁单筛选一致:ERP订单状态不为取消 / 删除 / 空,且售后单号为空。
if (in_array(trim((string) $row['erp_order_status']), self::EXCLUDED_ERP_STATUSES, true) || trim((string) $row['after_sale_no']) !== '') {
continue;
}
$rows[] = $row;
}
usort($rows, static fn (array $a, array $b): int => [$a['paid_at'], $a['work_order_no']] <=> [$b['paid_at'], $b['work_order_no']]);
return $rows;
}
/**
* 种菜额度计算用:付款时间 ≥ $paidFrom 的全部种菜工单(每个工单号取最新一版;排除审核驳回、作废),不论是否已报销。
* 累计要含之后付款的单(与看板一致),所以不设上限。结果缓存 5 分钟。
*
* @return list<array<string,mixed>>
*/
public function quotaRows(string $paidFrom): array
{
$status = $this->syncStatus();
if (! in_array($status['code'], ['ready', 'failed'], true)) {
throw new RuntimeException('business_mirror_dataset_not_ready');
}
return Cache::remember('planting-reimbursements:quota-rows:v2:' . $paidFrom, 300, function () use ($paidFrom): array {
// 工单在付款后才发起,按发起时间往前留 3 天收窄范围,再按表单付款时间精确过滤。
// 一个月可能上万单,分块读、只留额度计算要的字段,避免整批 JSON 常驻内存。
$latest = [];
$this->connection->table($this->dataset()->targetTable())
->where('woTypeId', self::WORK_TYPE_ID)
->where('created', '>=', CarbonImmutable::parse($paidFrom)->subDays(3)->format('Y-m-d H:i:s'))
->select(['Id', 'woNo', 'status', 'createby', 'executors', 'completer', 'completeTime', 'created', 'businessFieldList'])
->chunkById(2000, function ($records) use (&$latest): void {
foreach ($records as $record) {
$woNo = (string) $record->woNo;
if (isset($latest[$woNo]) && $latest[$woNo]['id'] > (int) $record->Id) {
continue;
}
$row = $this->decorate($record);
$latest[$woNo] = ['id' => (int) $record->Id, 'row' => array_intersect_key($row, array_flip(self::QUOTA_FIELDS))];
}
}, 'Id');
$newestIds = $this->newestIds(array_keys($latest));
$rows = [];
foreach ($latest as $woNo => $item) {
$row = $item['row'];
if (($newestIds[(string) $woNo] ?? $item['id']) > $item['id']
|| in_array($row['status_code'], ['REJECT_AUDIT', 'DISABLED'], true)) {
continue;
}
$paidAt = str_replace('/', '-', (string) $row['paid_at']);
if ($paidAt === '' || $paidAt < $paidFrom) {
continue;
}
$rows[] = $row;
}
return $rows;
});
}
/**
* 已报销过的线上订单号 → 报销日期(工单完成日期),取本报销月和上一个报销月的已完成种菜报销。
*
* @param list<string> $excludeWorkOrders 不计入的工单号(回放对照时排除本批自身)
* @return array<string,string>
*/
public function reimbursedOrders(string $reimburseDate, array $excludeWorkOrders = []): array
{
$month = self::defaultMonth(CarbonImmutable::createFromFormat('!Y-m-d', $reimburseDate));
$months = [$month];
$previous = CarbonImmutable::createFromFormat('!Y-m', $month)->subMonthNoOverflow()->format('Y-m');
if ($previous >= self::FIRST_MONTH) {
$months[] = $previous;
}
$exclude = array_fill_keys($excludeWorkOrders, true);
$orders = [];
foreach ($months as $item) {
foreach ($this->rows($item) as $row) {
$orderNo = trim((string) $row['online_order_no']);
if ($orderNo === '' || isset($exclude[$row['work_order_no']])) {
continue;
}
$date = substr((string) $row['completed_at'], 0, 10);
if (! isset($orders[$orderNo]) || $date > $orders[$orderNo]) {
$orders[$orderNo] = $date;
}
}
}
return $orders;
}
/**
* @param list<string|int> $workOrders
* @return array<string,int> 工单号 → 最新一版 Id
*/
private function newestIds(array $workOrders): array
{
$newestIds = [];
foreach (array_chunk(array_map('strval', $workOrders), 1000) as $chunk) {
$versions = $this->connection->table($this->dataset()->targetTable())
->where('woTypeId', self::WORK_TYPE_ID)
->whereIn('woNo', $chunk)
->groupBy('woNo')
->selectRaw('woNo, MAX(Id) AS newest_id')
->get();
foreach ($versions as $version) {
$newestIds[(string) $version->woNo] = (int) $version->newest_id;
}
}
return $newestIds;
}
/** @return list<array<string,mixed>> */
private function load(string $from, string $to): array
{
$table = $this->dataset()->targetTable();
$query = $this->connection->table($table)
->where('woTypeId', self::WORK_TYPE_ID)
->where('completeTime', '>=', $from)
->where('completeTime', '<', $to)
->select([
'Id', 'woNo', 'status', 'createby', 'executors', 'completer',
'completeTime', 'created', 'businessFieldList',
])
->orderByDesc('completeTime')
->orderByDesc('woNo');
/** @var array<string,object> $latest 窗口内每个工单号的最新一版 */
$latest = [];
foreach ($this->boundedGet($query) as $record) {
$woNo = (string) $record->woNo;
if (! isset($latest[$woNo]) || (int) $record->Id > (int) $latest[$woNo]->Id) {
$latest[$woNo] = $record;
}
}
// 同一工单若有更新的一版(可能已改完成时间或被作废)落在窗口外,以最新一版为准,这里剔除。
$newestIds = $this->newestIds(array_keys($latest));
$rows = [];
foreach ($latest as $woNo => $record) {
if (($newestIds[(string) $woNo] ?? (int) $record->Id) > (int) $record->Id) {
continue;
}
// 最新一版已不是“已完成”(如完成后被作废)的工单不计入。
if ((string) $record->status !== self::STATUS_CONFIRMED) {
continue;
}
$rows[] = $this->decorate($record);
}
return $rows;
}
/** @return array<string,mixed> */
private function decorate(object $record): array
{
$fields = $this->fields((string) ($record->businessFieldList ?? ''));
$row = [
'work_order_no' => (string) $record->woNo,
'started_at' => $this->time($record->created ?? null),
'completed_at' => $this->time($record->completeTime ?? null),
'status' => self::STATUS_LABELS[(string) ($record->status ?? '')] ?? (string) ($record->status ?? ''),
'status_code' => (string) ($record->status ?? ''),
'initiator' => (string) ($record->createby ?? ''),
'executor' => (string) ($record->executors ?? ''),
'completer' => (string) ($record->completer ?? ''),
];
foreach (self::FIELDS as $key => $fieldId) {
$row[$key] = $this->display($fields[$fieldId] ?? null);
}
foreach (self::MONEY_FIELDS as $key) {
$row[$key] = $this->money($row[$key]);
}
$row['erp_order_status'] = self::ERP_ORDER_STATUS[$row['erp_order_status']] ?? $row['erp_order_status'];
$items = $this->items($fields['itemInfo']['raw'] ?? null);
$row['product_code'] = implode("\n", array_filter(array_column($items, 'skuId')));
$row['product_name'] = implode("\n", array_filter(array_column($items, 'name')));
$row['product_quantity'] = implode("\n", array_filter(array_column($items, 'qty')));
$row['platform_product_id'] = implode("\n", array_filter(array_column($items, 'platformItemId')));
return $row;
}
/** @return array<string,array{value:mixed,label:mixed,raw:array<mixed>}> */
private function fields(string $json): array
{
if (trim($json) === '') {
return [];
}
try {
$decoded = json_decode($json, true, 64, JSON_THROW_ON_ERROR);
} catch (Throwable) {
return [];
}
if (isset($decoded['businessField']) && is_array($decoded['businessField'])) {
$decoded = $decoded['businessField'];
}
$fields = [];
foreach (is_array($decoded) ? $decoded : [] as $item) {
if (is_array($item) && isset($item['fieldId'])) {
$fields[(string) $item['fieldId']] = [
'value' => $item['value'] ?? ($item['valueList'] ?? null),
'label' => $item['valueLabel'] ?? null,
'raw' => $item,
];
}
}
return $fields;
}
/** @param array{value:mixed,label:mixed}|null $field */
private function display(?array $field): string
{
if ($field === null) {
return '';
}
foreach ([$field['label'], $field['value']] as $value) {
if (is_array($value)) {
$value = implode('、', array_filter(array_map(static fn (mixed $v): string => is_scalar($v) ? trim((string) $v) : '', $value)));
}
if (is_scalar($value) && trim((string) $value) !== '') {
return trim((string) $value);
}
}
return '';
}
/**
* @param array<mixed>|null $raw
* @return list<array<string,string>>
*/
private function items(?array $raw): array
{
$items = [];
foreach ((array) ($raw['childFieldList'] ?? []) as $childRow) {
$item = [];
foreach ((array) $childRow as $child) {
if (is_array($child) && isset($child['fieldId'])) {
$v = $child['valueLabel'] ?? $child['value'] ?? '';
$item[(string) $child['fieldId']] = is_scalar($v) ? trim((string) $v) : '';
}
}
if ($item !== []) {
$items[] = $item;
}
}
foreach ((array) ($raw['value'] ?? []) as $item) {
if (is_array($item) && ! array_is_list($item)) {
$items[] = array_map(static fn (mixed $v): string => is_scalar($v) ? trim((string) $v) : '', $item);
}
}
return $items;
}
private function money(string $value): ?string
{
$normalized = str_replace([',', ',', '¥', '¥', '元', ' '], '', trim($value));
if (! preg_match('/^-?\d+(\.\d+)?$/', $normalized)) {
return null;
}
return number_format(round((float) $normalized, 2), 2, '.', '');
}
private function time(mixed $value): string
{
return substr(trim((string) $value), 0, 19);
}
/** @return iterable<int,object> */
private function boundedGet(Builder $query): iterable
{
$connection = $query->getConnection();
if ($connection->getDriverName() !== 'mysql') {
return $query->get();
}
return $connection->select(
'SET STATEMENT max_statement_time=' . self::QUERY_TIMEOUT_SECONDS . ' FOR ' . $query->toSql(),
$query->getBindings(),
);
}
private function dataset(): BusinessMirrorDataset
{
return $this->datasets->get(self::DATASET);
}
}