fix: 拆分成本与运费异常标记 #1002

Merged
renshen merged 1 commits from feature/renshen-cost-freight-anomaly-labels into main 2026-11-09 10:22:37 +08:00
6 changed files with 104 additions and 30 deletions

View File

@ -42,7 +42,7 @@
| 待有效凭据后首次同步 | 普通商品当前成本价、商品类型和更新时间继续读取 `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` 作为发起人部门单独保留,费用明细行的“店铺”字段规范化写入 `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`;钉钉副本把费用明细表保存为未命名字段时,按配置中的金额列识别包含该列的未命名表格组件,避免只抓到付款明细而漏掉费用科目和金额。明细行没有科目时,使用同一审批表单表头的 `科目编码 / 科目名称` 回填;行级科目始终优先。 |
| 本次接入 | 订单利润的包材费、快递费、账单费用直接读取聚胜算订单利润页面接口 `pf.erp321.com/WebApi/PF/OrderSku/GetOrderSkuList`(支付/发货时间口径各分页抓取),导入 `ops_shengsuan_order_profit_rows`,页面按“店铺 + 线上/内部单号”原样回填,不把 0 或推算值写入缺口。抓取脚本为 `scripts/order-profit-shengsuan-sync.py`,导入命令为 `order-profit:import-shengsuan-rows`;脚本依赖本机已登录胜算的 Chrome 调试窗口,不能由服务器容器直连页面。`成本/运费异常`按该表“商品成本为0/空且快递费为0/空”筛选,以发货时间为监控日期。 |
| 本次接入 | 订单利润的包材费、快递费、账单费用直接读取聚胜算订单利润页面接口 `pf.erp321.com/WebApi/PF/OrderSku/GetOrderSkuList`(支付/发货时间口径各分页抓取),导入 `ops_shengsuan_order_profit_rows`,页面按“店铺 + 线上/内部单号”原样回填,不把 0 或推算值写入缺口。抓取脚本为 `scripts/order-profit-shengsuan-sync.py`,导入命令为 `order-profit:import-shengsuan-rows`;脚本依赖本机已登录胜算的 Chrome 调试窗口,不能由服务器容器直连页面。`成本/运费异常`按该表“商品成本或快递费任一为0/空”筛选,分别标记“成本异常”“运费异常”,以发货时间为监控日期。 |
流程明细另设独立定时任务,避免与每分钟科目映射消费争用同一上游目录时被静默跳过。正式发布脚本会覆盖安装 `/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` 不变而被漏掉;原地改写同一组件仍由每周全量校准兜底。付款状态只按钉钉“出纳打款”节点判断,分为“出纳状态待同步 / 出纳未开始 / 出纳待打款 / 出纳退回 / 出纳已打款 / 流程终止 / 不涉及付款”。金额差异不参与付款状态判断,另以“付款差额 = 费用合计 − 已付款合计”展示。不能因为金额存在少量差异就覆盖出纳节点事实,也不能因为出纳已通过就隐藏金额差异。

View File

@ -75,19 +75,24 @@ class CostFreightAnomalyRepository
->orderByDesc('order_key')
->forPage(max($page, 1), min(max($limit, 1), 100))
->get()
->map(static fn (object $row): array => [
'shop_name' => trim((string) ($row->shop_name ?? '')),
'online_order_no' => trim((string) ($row->online_order_no ?? '')),
'internal_order_no' => trim((string) ($row->internal_order_no ?? '')),
'order_key' => trim((string) ($row->order_key ?? '')),
'ship_time' => $row->ship_time,
'product_cost' => $row->product_cost === null ? null : (float) $row->product_cost,
'matched_package_fee' => $row->matched_package_fee === null ? null : (float) $row->matched_package_fee,
'matched_freight_fee' => $row->matched_freight_fee === null ? null : (float) $row->matched_freight_fee,
'matched_bill_fee' => $row->matched_bill_fee === null ? null : (float) $row->matched_bill_fee,
'synced_at' => $row->synced_at,
'anomaly_type' => '商品成本及快递费缺失',
])
->map(static function (object $row): array {
$anomalyTypes = self::anomalyTypes($row);
return [
'shop_name' => trim((string) ($row->shop_name ?? '')),
'online_order_no' => trim((string) ($row->online_order_no ?? '')),
'internal_order_no' => trim((string) ($row->internal_order_no ?? '')),
'order_key' => trim((string) ($row->order_key ?? '')),
'ship_time' => $row->ship_time,
'product_cost' => $row->product_cost === null ? null : (float) $row->product_cost,
'matched_package_fee' => $row->matched_package_fee === null ? null : (float) $row->matched_package_fee,
'matched_freight_fee' => $row->matched_freight_fee === null ? null : (float) $row->matched_freight_fee,
'matched_bill_fee' => $row->matched_bill_fee === null ? null : (float) $row->matched_bill_fee,
'synced_at' => $row->synced_at,
'anomaly_types' => $anomalyTypes,
'anomaly_type' => implode('、', $anomalyTypes),
];
})
->all();
return [
@ -99,10 +104,12 @@ class CostFreightAnomalyRepository
private function filtered(\Illuminate\Database\Query\Builder $query, array $filters): \Illuminate\Database\Query\Builder
{
$query->where(function ($cost) {
$cost->whereNull('product_cost')->orWhere('product_cost', 0);
})->where(function ($freight) {
$freight->whereNull('matched_freight_fee')->orWhere('matched_freight_fee', 0);
$query->where(function ($anomaly) {
$anomaly->where(function ($cost) {
$cost->whereNull('product_cost')->orWhere('product_cost', 0);
})->orWhere(function ($freight) {
$freight->whereNull('matched_freight_fee')->orWhere('matched_freight_fee', 0);
});
});
$from = trim((string) ($filters['from'] ?? ''));
@ -133,6 +140,25 @@ class CostFreightAnomalyRepository
return $query;
}
/** @return list<string> */
private static function anomalyTypes(object $row): array
{
$types = [];
if (self::isZeroOrMissing($row->product_cost)) {
$types[] = '成本异常';
}
if (self::isZeroOrMissing($row->matched_freight_fee)) {
$types[] = '运费异常';
}
return $types;
}
private static function isZeroOrMissing(mixed $value): bool
{
return $value === null || (float) $value === 0.0;
}
/** @return list<string> */
private function batchValues(string $value): array
{

View File

@ -19,6 +19,28 @@
color: var(--vip-content-muted, #595959);
font-size: .76rem;
}
.cost-freight-anomaly-types {
display: flex;
flex-wrap: wrap;
gap: .3rem;
}
.cost-freight-anomaly-tag {
display: inline-flex;
align-items: center;
padding: .12rem .45rem;
border-radius: 4px;
font-size: .72rem;
font-weight: 600;
white-space: nowrap;
}
.cost-freight-anomaly-tag.is-cost {
background: #fdecec;
color: #a32d2d;
}
.cost-freight-anomaly-tag.is-freight {
background: #fdf3e3;
color: #8a5d10;
}
</style>
@php
@ -62,7 +84,7 @@
<div class="cost-freight-note">
<span class="material-icons-outlined" aria-hidden="true">info</span>
<span>口径:聚胜算订单利润页面中“商品成本”为 0/空,且“快递费(自动匹配)”为 0/空的订单。费用结果来自聚胜算页面接口,按发货时间监控。</span>
<span>口径:聚胜算订单利润页面中,“商品成本”或“快递费(自动匹配)”任一为 0/空即展示;成本异常、运费异常分别标注。费用结果来自聚胜算页面接口,按发货时间监控。</span>
</div>
@if ($costFreightError ?? null)
@ -105,12 +127,28 @@
<td class="is-number">{{ $row->matched_freight_fee === null ? '-' : number_format((float) $row->matched_freight_fee, 2) }}</td>
<td class="is-number">{{ $row->matched_bill_fee === null ? '-' : number_format((float) $row->matched_bill_fee, 2) }}</td>
<td class="is-number">{{ $row->matched_package_fee === null ? '-' : number_format((float) $row->matched_package_fee, 2) }}</td>
<td>{{ $row->anomaly_type }}</td>
<td>
@php
$anomalyTypes = (array) ($row->anomaly_types ?? []);
if ($anomalyTypes === [] && filled($row->anomaly_type ?? null)) {
$anomalyTypes = [$row->anomaly_type];
}
@endphp
<div class="cost-freight-anomaly-types">
@foreach ($anomalyTypes as $anomalyType)
<span @class([
'cost-freight-anomaly-tag',
'is-cost' => $anomalyType === '成本异常',
'is-freight' => $anomalyType === '运费异常',
])>{{ $anomalyType }}</span>
@endforeach
</div>
</td>
<td>{{ $row->synced_at ?: '-' }}</td>
</tr>
@empty
<tr class="is-empty">
<td colspan="10">当前范围内没有成本和快递费同时缺失的订单。</td>
<td colspan="10">当前范围内没有商品成本或快递费为 0/空的订单。</td>
</tr>
@endforelse
</tbody>

View File

@ -29,7 +29,8 @@ class CostFreightAnomalyPageTest extends FeatureTestCase
'matched_freight_fee' => 0.0,
'matched_bill_fee' => -0.5,
'synced_at' => '2026-11-07 17:44:20',
'anomaly_type' => '商品成本及快递费缺失',
'anomaly_types' => ['成本异常', '运费异常'],
'anomaly_type' => '成本异常、运费异常',
]],
'total' => 1,
'has_more' => false,
@ -47,7 +48,8 @@ class CostFreightAnomalyPageTest extends FeatureTestCase
->assertSeeText('商品成本')
->assertSeeText('快递费(自动匹配)')
->assertSeeText('5127581702031023636')
->assertSeeText('商品成本及快递费缺失')
->assertSeeText('成本异常')
->assertSeeText('运费异常')
->assertSeeText('最近同步');
}
}

View File

@ -331,7 +331,7 @@ class NavigationTest extends FeatureTestCase
->assertSeeText('成本/运费异常')
->assertSeeText('商品成本')
->assertSeeText('快递费(自动匹配)')
->assertSeeText('成本和快递费同时缺失');
->assertSeeText('任一为 0/空即展示');
$this->loginAs()
->get(route('business.pages.show', [

View File

@ -40,8 +40,10 @@ class CostFreightAnomalyRepositoryTest extends TestCase
$db = DB::connection('business_mirror');
foreach ([
['shop_name' => '异常店', 'online_order_no' => 'NO-1', 'order_key' => '异常店|NO-1', 'ship_time' => '2026-11-05 10:00:00', 'product_cost' => 0, 'matched_freight_fee' => 0],
['shop_name' => '正常店', 'online_order_no' => 'NO-2', 'order_key' => '正常店|NO-2', 'ship_time' => '2026-11-05 11:00:00', 'product_cost' => 100, 'matched_freight_fee' => 0],
['shop_name' => '正常店', 'online_order_no' => 'NO-3', 'order_key' => '正常店|NO-3', 'ship_time' => '2026-11-06 11:00:00', 'product_cost' => 0, 'matched_freight_fee' => 7],
['shop_name' => '运费异常店', 'online_order_no' => 'NO-2', 'order_key' => '运费异常店|NO-2', 'ship_time' => '2026-11-05 11:00:00', 'product_cost' => 100, 'matched_freight_fee' => 0],
['shop_name' => '成本异常店', 'online_order_no' => 'NO-3', 'order_key' => '成本异常店|NO-3', 'ship_time' => '2026-11-06 11:00:00', 'product_cost' => 0, 'matched_freight_fee' => 7],
['shop_name' => '正常店', 'online_order_no' => 'NO-4', 'order_key' => '正常店|NO-4', 'ship_time' => '2026-11-06 12:00:00', 'product_cost' => 100, 'matched_freight_fee' => 7],
['shop_name' => '成本缺失店', 'online_order_no' => 'NO-5', 'order_key' => '成本缺失店|NO-5', 'ship_time' => '2026-11-06 13:00:00', 'product_cost' => null, 'matched_freight_fee' => 9],
] as $row) {
$db->table('ops_shengsuan_order_profit_rows')->insert($row + [
'internal_order_no' => null,
@ -54,7 +56,7 @@ class CostFreightAnomalyRepositoryTest extends TestCase
}
}
public function test_only_orders_with_both_cost_and_freight_missing_are_returned(): void
public function test_orders_with_either_cost_or_freight_missing_are_classified_independently(): void
{
$result = app(CostFreightAnomalyRepository::class)->paginate([
'from' => '2026-11-01',
@ -63,9 +65,15 @@ class CostFreightAnomalyRepositoryTest extends TestCase
'shop_names' => [],
], 1, 25);
$this->assertSame(1, $result['total']);
$this->assertSame('NO-1', $result['rows'][0]['online_order_no']);
$this->assertSame('商品成本及快递费缺失', $result['rows'][0]['anomaly_type']);
$rows = array_column($result['rows'], null, 'online_order_no');
$this->assertSame(4, $result['total']);
$this->assertSame(['成本异常', '运费异常'], $rows['NO-1']['anomaly_types']);
$this->assertSame('成本异常、运费异常', $rows['NO-1']['anomaly_type']);
$this->assertSame(['运费异常'], $rows['NO-2']['anomaly_types']);
$this->assertSame(['成本异常'], $rows['NO-3']['anomaly_types']);
$this->assertSame(['成本异常'], $rows['NO-5']['anomaly_types']);
$this->assertArrayNotHasKey('NO-4', $rows);
}
public function test_order_and_shop_batch_filters_are_applied(): void