diff --git a/docs/上游业务镜像清单.md b/docs/上游业务镜像清单.md index dc03e7d9..47129f1a 100644 --- a/docs/上游业务镜像清单.md +++ b/docs/上游业务镜像清单.md @@ -34,7 +34,7 @@ | 本次接入 | 普通商品资料的工作平台采购价格接入 `jiansun.dbo.pm_priceAdjustment`(镜像 `js_pm_price_adjustment`):按 `skuCode` 关联 `jst_sku.sku_id`,取 `effectTime` 不晚于当前时间的最新 `purchasePrice`,并在页面和导出中标记“工作平台价格”;源表没有可靠更新时间,按 `Id` 稳定键追加,首次全量完成前只显示普通商品自身采购价。 | | 待有效凭据后首次同步 | 普通商品当前成本价、商品类型和更新时间继续读取 `operations_replica.rpt.jst_sku`;`rpt.fys_skuCostPriceUpdate` 是一 SKU 一行的旧快照,不能充当变价历史。完整成本价变更仍读取聚水潭经典开放平台“商品操作日志查询”接口 `worklog.query`,只镜像操作内容含“采购价”的记录到 `jushuitan_cost_price_history_rows`;经典接口必须在服务器私有配置中使用同一应用的 `JUSHUITAN_PARTNER_ID / JUSHUITAN_PARTNER_KEY / JUSHUITAN_TOKEN`,不得把新开放平台的 App Key / App Secret / Access Token 混用。页面在首次同步完成前明确显示待同步或失败,不把 0 条解释为真实无历史。 | | 本次接入 | 流程明细读取 `operations_replica/rpt.ding_Workflow + rpt.ding_WorkflowScheme` 的结构化审批主表、费用明细和付款明细,写入 `ding_process_detail_mirror` 与 `ding_process_payment_mirror`;主表的 `originatorDeptName` 作为发起人部门单独保留。禁止先把明细表和付款表铺平成 CSV 后逐行求和;付款明细必须按“对应序号”关联到代表明细行,但合计始终读取独立付款事实。每个纳入范围的源审批至少保留一条页面记录,没有费用金额的审批以空金额显示,不静默隐藏。首次全量先运行 `business-mirror:sync-ding-process-details --full --dry-run` 校验,再使用 `business-mirror:sync-ding-process-details --full` 发布;日常通过 `updateAt/createAt` 48 小时重叠增量同步。当前纳入日常报销、应付、线上代发、种菜、快递面单、固定资产、日常物品、样品、一次性采买、板材、工资、外贸费用和种菜充值等 14 个财务流程;发现新的明细流程编码时全量同步报错并保留上一版。 | -| 本次接入 | 财务经营报表 › 管理费用使用 `oa_process_account_mapping` 保存“流程科目映射”工作簿中的 191 个科目编码、科目名称、费用大类、二级、三级、是否进费比和是否分摊规则;当前快照包含 35 个进费比科目和 125 个分摊规则。页面按审批完成时间提供整月选择并默认展示上个月 OA 流程中审批状态为“已完成”、涉及付款且出纳已打款的明细,不涉及付款或出纳未打款的数据不进入汇总,按费用大类和二级汇总进费比/不进费比/未映射金额,点击二级查看三级汇总与 9 列明细模板。审批完成时间写入 `ding_process_detail_mirror.approval_finished_at`。 | +| 本次接入 | 财务经营报表 › 管理费用使用 `oa_process_account_mapping` 保存“流程科目映射”工作簿中的 191 个科目编码、科目名称、费用大类、二级、三级、是否进费比和是否分摊规则;当前快照包含 35 个进费比科目和 125 个分摊规则。页面按审批完成时间提供整月选择并默认展示上个月 OA 流程中审批状态为“已完成”、涉及付款且出纳已打款的明细,不涉及付款或出纳未打款的数据不进入汇总,按费用大类和二级汇总进费比/不进费比/未映射金额,点击二级查看三级汇总与 9 列明细模板。审批完成时间写入 `ding_process_detail_mirror.approval_finished_at`;钉钉副本把费用明细表保存为未命名字段时,按配置中的金额列识别包含该列的未命名表格组件,避免只抓到付款明细而漏掉费用科目和金额。 | 流程明细另设独立定时任务,避免与每分钟科目映射消费争用同一上游目录时被静默跳过。正式发布脚本会覆盖安装 `/etc/cron.d/caiwu-ding-process-details`:每 5 分钟尝试运行 `business-mirror:sync-ding-process-details`,使用独立调度锁并在共用上游锁上最多等待 15 分钟;`scripts/install-ding-process-sync-cron.ps1` 仅作为手工修复入口。任务时间和镜像状态统一使用平台固定时区,审批等待时长按实际小时展示。OA 页面每小时最多把一次当前页“审批中”的审批编号写入定向刷新队列,由每分钟检查一次的批处理命令消费;其他时间直接展示最近镜像,已完成流程不进入即时刷新链路,仍由常规增量和每周全量校准处理付款等后续变化。增量同步除跟进审批主表更新时间外,还以 `ding_WorkflowScheme` 的数字 `id` 高水位发现后补表单行,避免付款明细等新组件写入后主审批 `updateAt` 不变而被漏掉;原地改写同一组件仍由每周全量校准兜底。付款状态只按钉钉“出纳打款”节点判断,分为“出纳状态待同步 / 出纳未开始 / 出纳待打款 / 出纳退回 / 出纳已打款 / 流程终止 / 不涉及付款”。金额差异不参与付款状态判断,另以“付款差额 = 费用合计 − 已付款合计”展示。不能因为金额存在少量差异就覆盖出纳节点事实,也不能因为出纳已通过就隐藏金额差异。 diff --git a/modules/EcommerceFinance/Services/DingProcessDetailMirrorSynchronizer.php b/modules/EcommerceFinance/Services/DingProcessDetailMirrorSynchronizer.php index 1faed039..b79ac833 100644 --- a/modules/EcommerceFinance/Services/DingProcessDetailMirrorSynchronizer.php +++ b/modules/EcommerceFinance/Services/DingProcessDetailMirrorSynchronizer.php @@ -884,6 +884,45 @@ class DingProcessDetailMirrorSynchronizer } } + $processes = (array) config('upstream_mirrors.ding_process_details.processes', []); + $missingIds = []; + foreach ($workflows as $workflow) { + $id = trim((string) ($workflow['processInstanceId'] ?? '')); + $process = (array) ($processes[$workflow['processCode'] ?? ''] ?? []); + if ($id === '' || $process === []) { + continue; + } + foreach (['detail_table', 'payment_table'] as $configurationKey) { + $tableName = trim((string) ($process[$configurationKey] ?? '')); + if ($tableName === '') { + continue; + } + $value = $result[$id][$tableName] ?? null; + if ($value === null || $value === '' || $value === '[]' + || (is_array($value) && $value === [])) { + $missingIds[$id] = true; + break; + } + } + } + + if ($missingIds !== []) { + $missingIds = array_keys($missingIds); + $missingPlaceholders = implode(',', array_fill(0, count($missingIds), '?')); + $unnamedRows = $query->select( + 'SELECT processInstanceId, value FROM rpt.ding_WorkflowScheme ' + . "WHERE processInstanceId IN ({$missingPlaceholders}) " + . 'AND name = ? ORDER BY id', + [...$missingIds, ''], + ); + foreach ($unnamedRows as $row) { + $id = (string) ($row['processInstanceId'] ?? ''); + if ($id !== '') { + $result[$id]['__unnamed_components'][] = $row['value'] ?? null; + } + } + } + return $result; } @@ -1088,7 +1127,21 @@ class DingProcessDetailMirrorSynchronizer array $taskState, ): array { $tableName = trim((string) ($process['detail_table'] ?? '')); - $rows = $tableName !== '' ? $this->tableRows($fields[$tableName] ?? null, $workflow, $tableName, $issues) : []; + $tableValue = $tableName !== '' ? ($fields[$tableName] ?? null) : null; + $resolvedTableName = $tableName; + if ($tableName !== '' && $this->emptyTableValue($tableValue)) { + $unnamedTable = $this->findUnnamedDetailTable( + $fields, + (array) ($process['detail_amount_fields'] ?? []), + ); + if ($unnamedTable !== null) { + $tableValue = $unnamedTable; + $resolvedTableName = ''; + } + } + $rows = $tableName !== '' && $tableValue !== null + ? $this->tableRows($tableValue, $workflow, $tableName, $issues) + : []; if ($tableName === '' && (bool) ($process['scalar_detail'] ?? false)) { $rows = [$this->scalarDetailRow($fields)]; } @@ -1124,7 +1177,64 @@ class DingProcessDetailMirrorSynchronizer ); } - return [$details, $amountCents, $tableName]; + return [$details, $amountCents, $resolvedTableName]; + } + + private function emptyTableValue(mixed $value): bool + { + return $value === null + || $value === '' + || $value === '[]' + || (is_array($value) && $value === []); + } + + /** + * @param array $fields + * @param list $amountFields + * @return list>|null + */ + private function findUnnamedDetailTable(array $fields, array $amountFields): ?array + { + $amountFields = array_values(array_unique(array_filter(array_map( + static fn (mixed $value): string => trim((string) $value), + $amountFields, + )))); + if ($amountFields === []) { + return null; + } + + $best = null; + $bestScore = 0; + foreach ((array) ($fields['__unnamed_components'] ?? []) as $component) { + $candidate = is_string($component) ? json_decode($component, true) : $component; + if (! is_array($candidate) || $candidate === []) { + continue; + } + + $labels = []; + foreach ($candidate as $row) { + foreach ((array) ($row['rowValue'] ?? []) as $cell) { + $label = trim((string) ($cell['label'] ?? '')); + if ($label !== '') { + $labels[$label] = true; + } + } + } + $amountMatches = count(array_intersect($amountFields, array_keys($labels))); + if ($amountMatches === 0) { + continue; + } + + $score = $amountMatches * 10; + $score += isset($labels['科目编码']) ? 2 : 0; + $score += isset($labels['科目名称']) ? 2 : 0; + if ($score > $bestScore) { + $best = $candidate; + $bestScore = $score; + } + } + + return $best; } /** diff --git a/tests/Unit/EcommerceFinance/DingProcessDetailMirrorSynchronizerTest.php b/tests/Unit/EcommerceFinance/DingProcessDetailMirrorSynchronizerTest.php index 49847239..43658e17 100644 --- a/tests/Unit/EcommerceFinance/DingProcessDetailMirrorSynchronizerTest.php +++ b/tests/Unit/EcommerceFinance/DingProcessDetailMirrorSynchronizerTest.php @@ -116,6 +116,55 @@ class DingProcessDetailMirrorSynchronizerTest extends TestCase $this->assertEqualsWithDelta(1114.57, $summary['bank_amount_total'], 0.001); } + public function test_unnamed_detail_table_is_used_when_configured_name_is_missing(): void + { + $this->query->workflows = [[ + 'processInstanceId' => 'process-unnamed-detail', + 'processCode' => 'PROC-ACF09A14-B094-46B1-A9E2-8BB1F4E1FDCF', + 'businessId' => '202610091326000505189', + 'title' => '钰岚提交的线上代发业务报销', + 'status' => 'COMPLETED', + 'result' => 'AGREE', + 'originatorUserId' => 'user-1', + 'originatorDeptName' => '运营中心G-运营一部', + 'joinAt' => '2026-10-09 13:26:00', + 'finishTime' => '2026-10-15 10:32:00', + ]]; + $this->query->scheme = [ + '付款明细' => json_encode([[ + 'rowValue' => [ + ['label' => '对应序号', 'value' => '1'], + ['label' => '付款方式', 'value' => '银行卡'], + ['label' => '付款金额', 'value' => '341.00'], + ['label' => '科目编码', 'value' => '1002.01.584'], + ['label' => '科目名称', 'value' => '农业银行-庆之图'], + ['label' => '打款时间', 'value' => '2026-10-12'], + ], + ]], JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR), + ]; + $this->query->unnamedSchemeValues = [ + "流程使用范围:\n线上代发业务费用报销", + json_encode([[ + 'rowValue' => [ + ['label' => '序号', 'value' => '1'], + ['label' => '代发订单金额', 'value' => '341'], + ['label' => '科目编码', 'value' => '1405.06'], + ['label' => '科目名称', 'value' => '库存商品_线上代发'], + ], + ]], JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR), + ]; + + $this->synchronizer->sync(true); + + $row = DB::connection('business_mirror')->table('ding_process_detail_mirror') + ->where('business_id', '202610091326000505189') + ->first(); + $this->assertSame('1405.06', $row->account_code); + $this->assertEqualsWithDelta(341.0, (float) $row->amount, 0.001); + $this->assertEqualsWithDelta(341.0, (float) $row->bank_amount, 0.001); + $this->assertSame('2026-10-15 10:32:00', $row->approval_finished_at); + } + public function test_completed_workflow_uses_cashier_status_and_keeps_difference_separate(): void { $this->query->workflows = [[ @@ -1033,6 +1082,9 @@ class FakeDingProcessDetailQuery extends UpstreamReadOnlyQuery /** @var array */ public array $scheme = []; + /** @var list */ + public array $unnamedSchemeValues = []; + public array $schemeChangedWorkflows = []; /** @var array> */ @@ -1111,7 +1163,9 @@ class FakeDingProcessDetailQuery extends UpstreamReadOnlyQuery } if (str_contains($sql, 'FROM rpt.ding_WorkflowScheme')) { $rows = []; - $requested = array_fill_keys(array_map('strval', $bindings), true); + $unnamedQuery = str_contains($sql, 'AND name = ?'); + $bindingValues = $unnamedQuery ? array_slice($bindings, 0, -1) : $bindings; + $requested = array_fill_keys(array_map('strval', $bindingValues), true); $known = []; $candidates = [ ...$this->workflows, @@ -1139,6 +1193,16 @@ class FakeDingProcessDetailQuery extends UpstreamReadOnlyQuery } } foreach (array_keys($known) as $processInstanceId) { + if ($unnamedQuery) { + foreach ($this->unnamedSchemeValues as $value) { + $rows[] = [ + 'processInstanceId' => $processInstanceId, + 'name' => '', + 'value' => $value, + ]; + } + continue; + } foreach ($this->scheme as $name => $value) { $rows[] = [ 'processInstanceId' => $processInstanceId,