# `Sp_saveReturn_sOpi` (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_sOpi`'`._

## Narrative

**Business context:** KPI流程操作菜单 → 发外加工流程 → 整单发外入库 (成品外购入库 in legacy phrasing) — when a finished-goods outsource-receipt note is saved, back-writes the inbound qty onto the upstream 整单发外 slave (`OpsOutsideProcessSlave`) and onto any linked 成品检验 (`qlyproducttestmaster`).

**What it does:** Splits `sGuid` into `p_t_sId`, materialises both `OpsOutsideProcessSlave.sId` and `qlyproducttestmaster.sId` source ids into `p_sSrcSlaveId_tmp` and `p_sTestSlaveId_tmp`, sums the received qty (with `iTag=1` zeroing the in-flight batch) into `t_data_temp`, then updates `OpsOutsideProcessSlave.dInStoreQty` and `qlyproducttestmaster.dInStoreQty` for matching rows.

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