diff --git a/database/migrations/2026_11_03_110000_add_store_name_to_ding_process_detail_mirror.php b/database/migrations/2026_11_03_110000_add_store_name_to_ding_process_detail_mirror.php new file mode 100644 index 00000000..9c85736b --- /dev/null +++ b/database/migrations/2026_11_03_110000_add_store_name_to_ding_process_detail_mirror.php @@ -0,0 +1,47 @@ +environment('testing') + || (string) config('system_database.mode', 'bootstrap') === 'bootstrap') { + return; + } + + $schema = Schema::connection($this->connection()); + if (! $schema->hasTable('ding_process_detail_mirror') + || $schema->hasColumn('ding_process_detail_mirror', 'store_name')) { + return; + } + + $schema->table('ding_process_detail_mirror', function (Blueprint $table): void { + $table->string('store_name', 500)->nullable()->after('account_name'); + }); + } + + public function down(): void + { + if (app()->environment('testing') + || (string) config('system_database.mode', 'bootstrap') === 'bootstrap') { + return; + } + + $schema = Schema::connection($this->connection()); + if ($schema->hasTable('ding_process_detail_mirror') + && $schema->hasColumn('ding_process_detail_mirror', 'store_name')) { + $schema->table('ding_process_detail_mirror', function (Blueprint $table): void { + $table->dropColumn('store_name'); + }); + } + } +}; diff --git a/docs/上游业务镜像清单.md b/docs/上游业务镜像清单.md index 9b7a1faa..7f0c1499 100644 --- a/docs/上游业务镜像清单.md +++ b/docs/上游业务镜像清单.md @@ -33,7 +33,7 @@ | 已接入待首次同步 | 店铺营销账户余额的京东账户读取 `js_data_middle_platform.dbo.jd_jzt_finance_records` 镜像 `jsm_jd_jzt_finance_records`;按 `id` 稳定断点、`updated_at` 重叠增量,页面不直连上游。 | | 本次接入 | 普通商品资料的工作平台采购价格接入 `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` 与付款按明细行分摊的 `ding_process_payment_line_mirror`;主表的 `originatorDeptName` 作为发起人部门单独保留。禁止先把明细表和付款表铺平成 CSV 后逐行求和;付款明细必须按“对应序号”关联到代表明细行,但合计始终读取独立付款事实。管理费用按分摊后的付款月份统计,跨月分笔付款不会把整张审批重复计入多月。每个纳入范围的源审批至少保留一条页面记录,没有费用金额的审批以空金额显示,不静默隐藏。首次全量先运行 `business-mirror:sync-ding-process-details --full --dry-run` 校验,再使用 `business-mirror:sync-ding-process-details --full` 发布;日常通过 `updateAt/createAt` 48 小时重叠增量同步。当前纳入日常报销、应付、线上代发、种菜、快递面单、固定资产、日常物品、样品、一次性采买、板材、工资、外贸费用和种菜充值等 14 个财务流程;发现新的明细流程编码时全量同步报错并保留上一版。 | +| 本次接入 | 流程明细读取 `operations_replica/rpt.ding_Workflow + rpt.ding_WorkflowScheme` 的结构化审批主表、费用明细和付款明细,写入 `ding_process_detail_mirror`、`ding_process_payment_mirror` 与付款按明细行分摊的 `ding_process_payment_line_mirror`;主表的 `originatorDeptName` 作为发起人部门单独保留,费用明细行的“店铺”字段规范化写入 `store_name`。禁止先把明细表和付款表铺平成 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 个科目和补充的 `5601.11.01 货代费`、`1221.07 种菜备用金`、`5602.11.03.19 AI阿里云软件费`,共 194 个科目,其中 37 个进费比科目和 126 个分摊规则。页面按打款时间提供整月选择并默认展示上个月 OA 流程中审批状态为“已完成”、涉及付款且出纳已打款的明细,不涉及付款或出纳未打款的数据不进入汇总,按费用大类和二级汇总进费比/不进费比/未映射金额,点击二级查看三级汇总与 9 列明细模板。审批完成时间写入 `ding_process_detail_mirror.approval_finished_at`,页面月份筛选使用 `ding_process_detail_mirror.paid_at`;钉钉副本把费用明细表保存为未命名字段时,按配置中的金额列识别包含该列的未命名表格组件,避免只抓到付款明细而漏掉费用科目和金额。明细行没有科目时,使用同一审批表单表头的 `科目编码 / 科目名称` 回填;行级科目始终优先。 | 流程明细另设独立定时任务,避免与每分钟科目映射消费争用同一上游目录时被静默跳过。正式发布脚本会覆盖安装 `/etc/cron.d/caiwu-ding-process-details`:每 5 分钟尝试运行 `business-mirror:sync-ding-process-details`,页面定向刷新和一周一次的全量校准与增量同步共用 `/run/caiwu-oa-process.lock`,只在这些 OA 镜像任务之间排队,不再被店铺对账、商品同步等无关任务占用的全局上游锁阻塞;`scripts/install-ding-process-sync-cron.ps1` 仅作为手工修复入口。任务时间和镜像状态统一使用平台固定时区,审批等待时长按实际小时展示。OA 页面每小时最多把一次当前页“审批中”的审批编号写入定向刷新队列,由每分钟检查一次的批处理命令消费;其他时间直接展示最近镜像,已完成流程不进入即时刷新链路,仍由常规增量和每周全量校准处理付款等后续变化。进行中审批的钉钉接口按配置并发读取(默认 5 个、上限 10 个),单个审批失败不会丢弃同批成功结果。增量同步除跟进审批主表更新时间外,还以 `ding_WorkflowScheme` 的数字 `id` 高水位发现后补表单行,避免付款明细等新组件写入后主审批 `updateAt` 不变而被漏掉;原地改写同一组件仍由每周全量校准兜底。付款状态只按钉钉“出纳打款”节点判断,分为“出纳状态待同步 / 出纳未开始 / 出纳待打款 / 出纳退回 / 出纳已打款 / 流程终止 / 不涉及付款”。金额差异不参与付款状态判断,另以“付款差额 = 费用合计 − 已付款合计”展示。不能因为金额存在少量差异就覆盖出纳节点事实,也不能因为出纳已通过就隐藏金额差异。 diff --git a/modules/EcommerceFinance/Http/Controllers/ManagementExpenses.php b/modules/EcommerceFinance/Http/Controllers/ManagementExpenses.php index a38a97c5..61b1b90d 100644 --- a/modules/EcommerceFinance/Http/Controllers/ManagementExpenses.php +++ b/modules/EcommerceFinance/Http/Controllers/ManagementExpenses.php @@ -27,4 +27,22 @@ class ManagementExpenses extends Controller ->header('Cache-Control', 'no-store'); } } + + public function storeSummary(Request $request, ManagementExpensesPageData $pageData): JsonResponse + { + try { + return response() + ->json(['success' => true, ...$pageData->storeSummary($request)]) + ->header('Cache-Control', 'no-store'); + } catch (Throwable $exception) { + report($exception); + + return response() + ->json([ + 'success' => false, + 'message' => '店铺汇总暂时无法读取,请稍后重试。', + ], 503) + ->header('Cache-Control', 'no-store'); + } + } } diff --git a/modules/EcommerceFinance/Services/DingProcessDetailMirrorSynchronizer.php b/modules/EcommerceFinance/Services/DingProcessDetailMirrorSynchronizer.php index b31d27e3..b260527d 100644 --- a/modules/EcommerceFinance/Services/DingProcessDetailMirrorSynchronizer.php +++ b/modules/EcommerceFinance/Services/DingProcessDetailMirrorSynchronizer.php @@ -1111,6 +1111,7 @@ class DingProcessDetailMirrorSynchronizer $title, '', '', + $this->storeName([], $fields), $originator, $documentType, $status, @@ -1291,6 +1292,7 @@ class DingProcessDetailMirrorSynchronizer } $accountCode = trim((string) ($row['科目编码'] ?? '')); $accountName = trim((string) ($row['科目名称'] ?? '')); + $storeName = $this->storeName($row, $fields); if ($accountCode === '') { $accountCode = trim((string) $this->scalarValue($fields['科目编码'] ?? null)); } @@ -1313,6 +1315,7 @@ class DingProcessDetailMirrorSynchronizer $this->purpose($row) ?: (string) ($workflow['title'] ?? ''), $accountCode, $accountName, + $storeName, $originator, $documentType, $status, @@ -1354,7 +1357,7 @@ class DingProcessDetailMirrorSynchronizer if ($officialRow === null) { continue; } - foreach (['科目编码', '科目名称'] as $field) { + foreach (['科目编码', '科目名称', '店铺', '店铺名称'] as $field) { if (trim((string) ($row[$field] ?? '')) === '' && trim((string) ($officialRow[$field] ?? '')) !== '') { $row[$field] = (string) $officialRow[$field]; @@ -2066,6 +2069,7 @@ class DingProcessDetailMirrorSynchronizer string $purpose, string $accountCode, string $accountName, + string $storeName, string $originator, string $documentType, string $status, @@ -2108,6 +2112,7 @@ class DingProcessDetailMirrorSynchronizer 'purpose' => $purpose, 'account_code' => mb_substr($accountCode, 0, 64), 'account_name' => mb_substr($accountName, 0, 255), + 'store_name' => mb_substr($storeName, 0, 500), 'amount' => $amountCents === null ? null : $this->moneyString($amountCents), 'amount_cents' => $amountCents ?? 0, 'bank_account_code' => null, @@ -2276,6 +2281,62 @@ class DingProcessDetailMirrorSynchronizer return ''; } + /** + * Store names are commonly nested JSON arrays in DingTalk table cells. + * + * @param array $row + * @param array $fields + */ + private function storeName(array $row, array $fields): string + { + foreach (['店铺', '店铺名称', '店铺名', 'store_name'] as $field) { + $value = $row[$field] ?? null; + if ($value === null || trim((string) $value) === '') { + $value = $fields[$field] ?? null; + } + $normalized = $this->normalizeStoreValue($value); + if ($normalized !== '') { + return mb_substr($normalized, 0, 500); + } + } + + return ''; + } + + private function normalizeStoreValue(mixed $value): string + { + if (is_string($value) && preg_match('/^\s*[\[{]/', $value) === 1) { + $decoded = json_decode($value, true); + if (json_last_error() === JSON_ERROR_NONE) { + $value = $decoded; + } + } + + $names = []; + $collect = function (mixed $candidate) use (&$collect, &$names): void { + if (is_array($candidate)) { + if (array_key_exists('value', $candidate) + && ! is_array($candidate['value']) + && trim((string) $candidate['value']) !== '') { + $names[] = trim((string) $candidate['value']); + + return; + } + foreach ($candidate as $item) { + $collect($item); + } + + return; + } + if (is_scalar($candidate) && trim((string) $candidate) !== '') { + $names[] = trim((string) $candidate); + } + }; + $collect($value); + + return implode('、', array_values(array_unique($names))); + } + /** @param array $process */ private function documentType(array $process, string $title): string { diff --git a/modules/EcommerceFinance/View/ManagementExpensesPageData.php b/modules/EcommerceFinance/View/ManagementExpensesPageData.php index a984575f..19d22a1e 100644 --- a/modules/EcommerceFinance/View/ManagementExpensesPageData.php +++ b/modules/EcommerceFinance/View/ManagementExpensesPageData.php @@ -24,6 +24,7 @@ class ManagementExpensesPageData public const DETAIL_COLUMNS = [ 'business_id' => '审批编号', 'originator_department' => '部门', + 'store_name' => '店铺', 'purpose' => '用途', 'expense_category' => '分类', 'second_level' => '二级', @@ -84,6 +85,10 @@ class ManagementExpensesPageData 'business.financial-operations.management-expenses.details', ['company_id' => company_id() ?: 1], ), + 'storeSummaryUrl' => route( + 'business.financial-operations.management-expenses.store-summary', + ['company_id' => company_id() ?: 1], + ), 'error' => null, ]; } catch (Throwable $exception) { @@ -108,6 +113,10 @@ class ManagementExpensesPageData 'business.financial-operations.management-expenses.details', ['company_id' => company_id() ?: 1], ), + 'storeSummaryUrl' => route( + 'business.financial-operations.management-expenses.store-summary', + ['company_id' => company_id() ?: 1], + ), 'error' => '管理费用数据暂时无法读取,请稍后重试。', ]; } @@ -123,6 +132,32 @@ class ManagementExpensesPageData return $this->detailResult($filters, $page, $perPage); } + /** @return array{rows:list>,total:float} */ + public function storeSummary(Request $request): array + { + $filters = $this->filters($request); + $storeExpression = "COALESCE(NULLIF(d.store_name, ''), '未填写店铺')"; + $rows = $this->baseQuery($filters) + ->selectRaw("{$storeExpression} AS store_name") + ->selectRaw($this->amountSplitExpression()) + ->groupByRaw($storeExpression) + ->orderByRaw('SUM(COALESCE(paid_line.paid_amount, 0)) DESC') + ->orderBy('store_name') + ->get() + ->map(static fn (object $row): array => [ + 'store_name' => (string) $row->store_name, + 'amount' => round((float) $row->amount_total, 2), + 'row_count' => (int) $row->row_count, + 'approval_count' => (int) $row->approval_count, + ]) + ->all(); + + return [ + 'rows' => $rows, + 'total' => round(array_sum(array_column($rows, 'amount')), 2), + ]; + } + /** @return array */ public static function filters(Request $request): array { @@ -220,6 +255,7 @@ class ManagementExpensesPageData ->select([ 'd.business_id', 'd.originator_department', + 'd.store_name', 'd.purpose', 'd.line_no', 'd.process_instance_id', @@ -238,6 +274,7 @@ class ManagementExpensesPageData ->map(static fn (object $row): array => [ 'business_id' => (string) $row->business_id, 'originator_department' => (string) ($row->originator_department ?? ''), + 'store_name' => (string) ($row->store_name ?? ''), 'purpose' => (string) ($row->purpose ?? ''), 'expense_category' => (string) $row->expense_category, 'second_level' => (string) $row->second_level, diff --git a/resources/views/business/pages/financial-operations/management-expenses.blade.php b/resources/views/business/pages/financial-operations/management-expenses.blade.php index 701cd01e..4cf72e24 100644 --- a/resources/views/business/pages/financial-operations/management-expenses.blade.php +++ b/resources/views/business/pages/financial-operations/management-expenses.blade.php @@ -8,6 +8,10 @@ $options ??= []; $perPageOptions ??= \Modules\EcommerceFinance\View\ManagementExpensesPageData::PER_PAGE_OPTIONS; $detailColumns ??= \Modules\EcommerceFinance\View\ManagementExpensesPageData::DETAIL_COLUMNS; + $storeSummaryUrl ??= route( + 'business.financial-operations.management-expenses.store-summary', + ['company_id' => company_id() ?: 1], + ); @endphp