# `Sp_saveReturn_sCmt` (procedure)

> 采购到货、材料检验反写数量


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

## Parameters

| # | Mode | Name | Type |
|---|---|---|---|
| 1 | IN | `sGuid` | `longtext` |
| 2 | IN | `sBrId` | `varchar(100)` |
| 3 | IN | `sSuId` | `varchar(100)` |
| 4 | IN | `iTag` | `int` |
| 5 | OUT | `sCode` | `int` |
| 6 | OUT | `sReturn` | `longtext` |

## Body

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

## Narrative

**Business context:** 质量管理 → 质量管理单据 → 材料检验单据 / 来料入库 — 反写 ("back-write") helper for the `sCmt` (材料检验, `qlycomematerialstest*`) save lifecycle. Propagates each 材料检验 row's qty + sample qty + auxiliary qty back to every upstream slave (`mftworkorderslave`, `PurPurchaseOrderSlave`, `PurPurchaseArriveSlave`, `purpurchaseapplyslave`) so the originating bill shows the latest 已检/已抽样 totals, and flips the `bQlyComplete` flag on the held stock row (`elematerialsstock`).

**What it does:** Parses `sGuid` (CSV of 检验 master ids) into `p_t_sId`. Resolves source slave ids into `p_sSrcSlaveId_tmp` from `qlycomematerialstestmaster.sSrcSlaveId`. Aggregates `dAuxiliaryQty / dSampleQty / dMaterialsQty` per slave into `t_data_temp`, excluding (`iTag=1`) or including (`iTag=2`) the rows being saved. Updates `dQlyQty` on `mftworkorderslave`; `dQlyAuxiliaryQty / dSampleQty / dQlyQty` on `PurPurchaseOrderSlave`; `dMaterialsTestAuxiliaryQty / dMaterialsTestQty` on `PurPurchaseArriveSlave`; `dMaterialsTestAuxiliaryQty` on `purpurchaseapplyslave`. Finally toggles `elematerialsstock.bQlyComplete` and writes the IQC result flags (`bQly`, `iTestResult`, `iTypeResult`) to `purpurchasearriveslave` and `purpurchaseapplyslave` — 0 on pre-save (provisional), 1 on post-save (committed).

**Invocation:** Called by `Sp_beforeSave_sCmt` (`iTag=1`) and `Sp_afterSave_sCmt` (`iTag=2`). The before/after wrappers attach as `gdsmodule.sSaveProName(Before)` on the 材料检验单据 (`sId 15827054010002527067474512475000`), 库存检验 (`sId 101251240115016105078918480`) and 材料检验(mobile单据) (`sId 12012615914116374731613080`) modules — all under 质量管理单据.
