# `Sp_saveReturn_sPmd` (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_sPmd`'`._

## Narrative

**Business context:** 生产管理 → 生产领料 / 生产补料 / 生产退料 — back-write helper for 生产领料 (`mitproductionapplymaster`). When a material issue is saved, cancelled, or deleted, it re-syncs the issued qty back into the originating 工单 BOM line, the 采购申请 line that ultimately fed it, and the production-application slave. (Form-master page mis-labels this as "其它入库单据反写"; routine COMMENT and body target 生产领料.)

**What it does:** Splits `sGuid`, builds `t_data_temp` (源 slave 聚合)、`t_data_temp2` + `p_sApplySlaveId_tmp` + `p_sWorkOrderMaterialsTbId_tmp` for the multi-hop joins, then updates `mitproductionapplySlave`, `MitProductionMaterialsSlave`, `purpurchaseapplyslave`, `MftWorkOrderMaterials` and `mftworkorderslave` with the new dApplyQty/dIssueQty. `iTag=1` excludes the current doc; `iTag=2` includes it.

**Invocation:** Wrapped by `Sp_afterSave_sPmd` (iTag=2) and `Sp_beforeSave_sPmd` (iTag=1). The 生产领料 form (`Sp_Check_sPmd`/`Sp_beforeSave_sPmd`) and 生产补料/生产退料 forms (`Sp_Check_spmd`/`Sp_beforeSave_spmd` — lower-case copy) both pivot through these wrappers. Install scripts: `xly-src/script/标版/30100101/Sp_afterSave_sPmd.sql` (+ `Sp_afterSave_sPmdStock.sql` for the stock-side variant).
