# `Sp_Apply_Flow_MaterialsChange` (procedure)

> 流程启动前调用-材料变更


- **Type:** PROCEDURE
- **Deterministic:** NO
- **SQL data access:** CONTAINS SQL

## Parameters

| # | Mode | Name | Type |
|---|---|---|---|
| 1 | IN | `sFormGuid` | `varchar(100)` |
| 2 | IN | `sGuid` | `varchar(100)` |
| 3 | IN | `sSrcNo` | `varchar(100)` |
| 4 | IN | `sLoginId` | `varchar(100)` |
| 5 | OUT | `sTableParams` | `longtext` |
| 6 | OUT | `sSrcReportData` | `longtext` |
| 7 | OUT | `sTableParamsValue` | `longtext` |
| 8 | OUT | `sReturn` | `varchar(4000)` |
| 9 | IN | `sBrId` | `varchar(100)` |
| 10 | IN | `sSuId` | `varchar(100)` |
| 11 | OUT | `sCode` | `int` |

## Body

_Body is not pre-cached. To inspect: `mysql --defaults-file=~/.my.cnf -e 'SHOW CREATE PROCEDURE `Sp_Apply_Flow_MaterialsChange`'`._

## Narrative

**Business context:** 材料变更单 → 生产单据 → 生产管理 (catalog header: "流程启动前调用-材料变更"; module sId `101251240115016119087978230`, form sId `101251240115016119087978910`, tbName `mftprocesschangemaster`). Per-form workflow-apply data builder triggered when the shop floor needs to swap a planned material for an alternative — the approval chain is gated by a 差额 (price-difference) threshold of 5000.

**What it does:** Resolves three approver tiers: 物控经理 (`iPosition=6`), 供应链部长 (`iPosition=5`), 技术部长 (`iPosition=9`). Aggregates `mftprocesschangematerials` rows (joined twice to `elematerials` — once for source, once for replacement) into `p_sChangeMemo` as `'源材料：{name}➜{style}➜{qty}***新材料：{name}➜{style}➜{qty}'` joined by newline. Computes `p_dDifferenceMoney` = `SUM(A.dMaterialsQty*C.dRecentPurchasePrice - A.dOriginalMaterialsQty*B.dRecentPurchasePrice)` and sets `p_iOut=1` when the difference exceeds 5000 (gating the escalation node). Emits `sTableParams` (变更单号/差额/变更信息) and `sTableParamsValue` (master values + `iOut` + the three approver name lists).

**Invocation:** Dispatched dynamically by Java `CheckExamineFlowDataServiceImpl.doSendCheckFolwData()` via `doProByNameFlow(..., sApplyProName, ...)` where `sApplyProName` is read from `gdsmoduleflow.sApplyProName` on the 材料变更 workflow config row. `gdsmoduleflow` is empty in this database — workflow configs are tenant-provisioned per deployment.

Flags: 差额 uses `dRecentPurchasePrice` (most-recent purchase price, not the row's frozen price), so the threshold gating depends on live `elematerials` rather than the variant's recorded prices.
