# `Sp_Check_sPst` (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_sPst`'`._

## Narrative

**Business context:** 销售管理 → 销售送货流程 → 成品入库 — post-save validator on `pitproductinstoremaster`/`pitproductinstoreslave`. Two-part enforcement: (1) the 入库 qty cannot exceed the 检验合格数 from QC, and (2) overflow across the per-work-order plan qty is auto-shunted into a 余数仓库 by inserting a split-row when the system setting `CkxNoRemainderWarehouse` is on. Handles 红冲 deduplication as well.

**What it does:** Sets `sCode=1`, `iFlag=0`, clears `sReturn`. Numbers slave rows via a session variable, reads `pitproductinstoremaster.sInstoreType`. Detects a 红冲 master and aborts with `sCcprkHaveRedData` if existing 红冲 rows don't balance. For `sInstoreType=1` (normal inbound), aborts with `sProductInStoreQtyUpQualified` ("产品入库数已超检验合格数，请核查是否重复入库") when the summed `dProductQty` per `sSrcSlaveId` exceeds the matching `qlyproducttestmaster.dQualifyQty`. Reads `NetMoney`/`CkxNoRemainderWarehouse`/`NetPrice` precisions. When `CkxNoRemainderWarehouse=1`, opens a cursor over slave rows whose warehouse has `bRemainder=1`; for each row, looks up `siswarehouse.sRemainWareHouseId` (aborts with `sNoRemindWareHouse` if not set), then computes overflow against `mftworkorderslave.dProductQty (+dReturnProductQty)` minus the sum of all in-store rows; on overflow, reduces the original row's qty and `INSERT INTO pitproductinstoreslave` a new row in the 余数仓库 carrying `ABS(overflow)`. Deletes any `dProductQty=0` slave rows, then calls `Sp_afterSave_sPst`. If the master is a 红冲, also calls `Sp_Calc_sPst(1, 0, ...)` to immediately drive the audit.

**Invocation:** Bound to `gdsmodule.sSaveProName` on `成品入库` (`192116810113315228246447250`) — 销售管理 → 销售送货流程 (KPI流程操作菜单). Dispatched by `BusinessBaseServiceImpl.checkUpdate(..., "sSaveProName")` after the save batch commits. Pre-save peer is `Sp_beforeSave_sPst`. xly-src ships `script/标版/30100101/Sp_Check_sPst.sql` as the install body.
