# `Sp_saveReturn_sPtr` (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_sPtr`'`._

## Narrative

**Business context:** 品质管理 → 成品报废 — back-write helper for 成品报废 (`PitProductRejectMaster`). When a scrap doc is saved or cancelled, it re-aggregates scrapped qty back onto the originating 工单 line, the production-plan line, the in-store row being decremented, the 外协 line, the 销售订单 line, the 半成品配套 master, and the related 子件领料 record so each upstream's "已报废" stays consistent.

**What it does:** Splits `sGuid` into `p_t_sId`, builds `t_data_temp`, then updates `MftWorkOrderSlave`, `mftproductionplanbillslave`, `PitProductInStoreSlave`, `opsoutsideprocessslave`, `salchildproductapplyslave`, `salsalesorderslave`, `sgdsemigoodsmatchbillslave`. `iTag=1` (before/delete) excludes current 报废; `iTag=2` (after) includes it.

**Invocation:** Wrapped by `Sp_afterSave_sPtr` (iTag=2) and `Sp_beforeSave_sPtr` (iTag=1). 成品报废 binds through the standard `Sp_Check_*`/`Sp_beforeSave_*` slots; `Sp_afterSave_sPtr` is reached via `Sp_Invalid_backwriting` on cancel/un-cancel. COMMENT "保存前反写数量" predates the iTag-driven refactor.
