# `Sp_saveReturn_sMad` (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_sMad`'`._

## Narrative

**Business context:** 材料调拨 (`192116810113315227212629260`) — when a 材料调拨单 (`mitproductionapplymaster`) is saved, back-writes the transferred quantity onto the upstream 调拨申请 slave (`mitproductionapplyslave`) so the source application reflects "已调拨" qty correctly.

**What it does:** Splits `sGuid` into temp table `p_t_sId`, materialises the affected `sSrcSlaveId` into `p_sSrcSlaveId_tmp`, sums the transferred qty (with `iTag=1` zeroing the in-flight batch on pre-save) into `t_data_temp`, then `Update mitproductionapplyslave` rows whose `sId = sSrcSlaveId`.

**Invocation:** Called by `Sp_beforeSave_sMad` (`iTag=1`) and `Sp_afterSave_sMad` (`iTag=2`). Reached via `Sp_delete_backwriting` / `Sp_Invalid_backwriting` for `sFormGuid='192116810113315227212629260'` (材料调拨). The matching save-side `gdsmodule` row is not present in this DB (likely a tenant-form configuration), but the cancel/delete dispatcher confirms the live mapping. No xly-src callers.
