# `Sp_Apply_Flow_MaterialsBack` (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_MaterialsBack`'`._

## Narrative

**Business context:** 退料申请 → 现场物料管理 → 生产执行 (catalog header: "流程启动前调用-退料申请") — per-form workflow-apply data builder for the material-return-from-floor flow. Backing form is the 退料申请 variant of the 领料申请 single-table `mitproductionapplymaster`. The first approver tier mirrors `Sp_Apply_Flow_MaterialsApplyLL` (领料人员's 二级部门负责人, not the制单人) so that the same person submitting on behalf of multiple departments still routes to the correct boss.

**What it does:** Resolves four approver tiers — 主任 (制单人's 部门负责人 by `eleemployee.sEmployeeName=B.sMakePerson` chain), 物控经理 (`iPosition=6`), 品质经理 (`iPosition=18`), and 领料申请人's 二级部门负责人 (looked up via the originating `mitproductionapplyslave.sSrcId` → `mitproductionmaterialsmaster.sEmployeeId` → `sisdepart` chain, stored in `p_FourFlow`). Aggregates `mitproductionapplyslave` rows into `p_sMaterialsMemo` as `'材料名称：{sMaterialsName}➜退料数量：{dAuxiliaryQty}'`. Translates `sBackType` to text via CASE (1→正常退料, 2→变更换料, 3→质量问题, else→虚拟退料). Emits `sTableParams` (退料单号/退料信息/退料类型/备注) and `sTableParamsValue` (master values + `iFlowBack` + the four 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. The companion update-proc [`Sp_Apply_Flow_MaterialsBack_Proc1`](./Sp_Apply_Flow_MaterialsBack_Proc1.md) writes `iFlowBack` back to the master when the approver changes the route.

Flags: contains a stray `select p_OneFlow;` mid-body — debug echo left in production.
