# `Sp_Check_sOpi` (procedure)

> 成品外购入库单据保存之前数据校验


- **Type:** PROCEDURE
- **Deterministic:** NO
- **SQL data access:** CONTAINS SQL

## Parameters

| # | Mode | Name | Type |
|---|---|---|---|
| 1 | IN | `sTableName` | `varchar(100)` |
| 2 | IN | `sGuid` | `varchar(100)` |
| 3 | IN | `sFormGuid` | `varchar(100)` |
| 4 | IN | `sLoginId` | `varchar(100)` |
| 5 | IN | `sBrId` | `varchar(100)` |
| 6 | IN | `sSuId` | `varchar(100)` |
| 7 | IN | `iFlag` | `int` |
| 8 | OUT | `sCode` | `int` |
| 9 | OUT | `sReturn` | `varchar(4000)` |

## Body

_Body is not pre-cached. To inspect: `mysql --defaults-file=~/.my.cnf -e 'SHOW CREATE PROCEDURE `Sp_Check_sOpi`'`._

## Narrative

**Business context:** KPI流程操作菜单 → 发外加工流程 → 整单发外入库 — post-save validator on `opsoutsideinstoremaster`. Combines 红冲 (red-impound) consistency checks, product-name reporting for failing slaves, and the standard 正算/反算 money/tax recompute used across the 发外 family.

**What it does:** If `sMinusSrcId` is set, sums each prior-slave's `dMaterialsQty` against the rolled-up qty of all later non-`bInvalid` red-impound rows that reference it; non-zero mismatches abort with `sCcprkHaveRedData` (已存在红冲数据，不允许重复提交). Reads `NetMoney`/`NetPrice` precision from `SysSystemSettings`, lists product names from `opsoutsideinstoreslave` joined to `eleproduct` for downstream error reporting, then opens a cursor over the slave rows and recomputes each line's money/tax/auxiliary-qty fields against the material's 正算/反算 formula and tax rate.

**Invocation:** Bound to `gdsmodule.sSaveProName` (post-save) on `整单发外入库` (`192116810113315235917608210`, under KPI流程操作菜单 → 发外加工流程). Dispatched by `BusinessBaseServiceImpl.checkUpdate(..., "sSaveProName")` after the save batch commits. xly-src ships `script/标版/30100101/Sp_Check_sOpi.sql` as the install body.
