# `Sp_Check_sPkg` (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_sPkg`'`._

## Narrative

**Business context:** 采购管理 → 物料采购流程 → 采购对账 — post-save validator on `purpurchasecheckingmaster`/`purpurchasecheckingslave`/`purpurchasecheckingdetail`. Reconciles a purchase invoice (对账单) against its source purchase orders before the after-save hook fires: enforces 烟包/非烟包 cannot be mixed, runs the 正算/反算 material-quantity formulas, and rolls auxiliary-quantity slave totals back into the detail rows so invoiced money/qty stays consistent with PO qty.

**What it does:** Sets `sCode=1`, `iFlag=0`, clears `sReturn`. Reads `NetMoney`/`NetPrice` precisions from `SysSystemSettings`. Aborts with `sYbNybCantReconciled` ("烟包和非烟包类型的单据不能同步进行对账") if both 烟包 and non-烟包 lines are present. Opens a nested cursor over `purpurchasecheckingslave` × `purpurchasecheckingdetail` joined to the source `purpurchaseorderslave`, dispatches each row to either `Sp_System_ReplaceField(... iComputeType=0)` (forward formula) or the reverse-formula branch using `eleformula.sComputeId`/`sReComputeId`, recomputes `dMaterialsQty`/`dAuxiliaryQty` and the derived money columns (`dProductMoney`, `dProductNoTaxMoney`, `dProductTaxMoney`, `dProductForeignMoney`), updates the slave/detail rows, and finally calls `Sp_afterSave_sPkg`. Aborts with localised messages (`sPcaMaterialInOrder`, `sPuaDzQtyUpPurQty`, `sPuaDzJJQtyUpPurQty`, `sPuaDzJJDzQtyUpPurQty`, `sMatrialNameInInvoice`) when invoice qty exceeds the underlying PO qty.

**Invocation:** Bound to `gdsmodule.sSaveProName` on `采购对账` (`192116810113315220489243410`) — 采购管理 → 物料采购流程 (KPI流程操作菜单). Dispatched by `BusinessBaseServiceImpl.checkUpdate(..., "sSaveProName")` after the save batch commits. Pre-save peer is `Sp_beforeSave_sPkg`. xly-src ships `script/标版/30100101/Sp_Check_sPkg.sql` as the install body.
