# `Sp_saveReturn_sOpc` (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_sOpc`'`._

## Narrative

**Business context:** KPI流程操作菜单 → 发外加工流程 → 工序发外 — when a 工序发外单 (`opsoutsideprocessmaster`) is saved (the upstream document that issues a process to an external supplier), back-writes the outsourced qty onto the originating 工单工序 (`mftworkorderprocess`/`MftWorkOrderProcess`) and 销售订单工序 (`salsalesorderprocess`/`salsalesorderslave`) so each source line carries an accurate "已发外" balance.

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

**Invocation:** Called by `Sp_beforeSave_sOpc` (`iTag=1`) and `Sp_afterSave_sOpc` (`iTag=2`). The beforeSave wrapper is bound on `gdsmodule.sSaveProNameBefore` for 工序发外 (`192116810113315232400087230`) under 发外加工流程 — fired by `BusinessBaseServiceImpl` save hooks. `Sp_Invalid_backwriting` / `Sp_delete_backwriting` re-route to `Sp_beforeSave_sOpc` for form id `101801153119616594919790770` (an alternate 工序发外 form) on cancel/delete. No xly-src callers.
