# `Sp_saveReturn_sOpp` (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_sOpp`'`._

## Narrative

**Business context:** KPI流程操作菜单 → 发外加工流程 → 整单发外 (成品外购) — when a 整单发外 (whole-order outsource purchase) is saved, back-writes the outsourced qty onto the source 生产工单 (`mftworkorderslave`) and 销售订单 (`salsalesorderslave`) so each origin row carries an accurate "已外购" balance.

**What it does:** Splits `sGuid` into `p_t_sId`, collects each line's `sSrcSlaveId` into `p_sSrcSlaveId_tmp`, sums the outsourced qty (with `iTag=1` zeroing the in-flight batch) into `t_data_temp`, then `Update mftworkorderslave` and `Update salsalesorderslave` rows whose `sId = sSrcSlaveId`.

**Invocation:** Called by `Sp_beforeSave_sOpp` (`iTag=1`) and `Sp_afterSave_sOpp` (`iTag=2`). The beforeSave wrapper is bound on `gdsmodule.sSaveProNameBefore` for 整单发外 (`192116810113315234963208090`) under 发外加工流程 — fired by `BusinessBaseServiceImpl` save hooks. `Sp_Invalid_backwriting` / `Sp_delete_backwriting` re-route to `Sp_beforeSave_sOpp` for the same form id (commented `成品外购单据`) on cancel/delete. No xly-src callers.
