# `Sp_Calc_sPis` (procedure)

> 采购入库


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

## Parameters

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

## Body

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

## Narrative

**Business context:** KPI流程操作菜单 → 物料采购流程 → 采购入库 — purchase stock-in audit/un-audit on `PurPurchaseInStoreMaster`/`Slave`. The check posts the receipt into material stock and links it back to the purchase-order, arrival, reject, and quality-check upstream rows; the uncheck rolls it all back.

**What it does:** Guards on `bInvalid`/`SysLocking`/`bFrozen` (accounting-period). On `iFlag=1`: deletes any stale `MitMaterialsStore`/`CahPaymentables` keyed to this bill, recomputes per-material stock movement (`call Sp_MaterialsPurchaseStoreVirtual`, `Update MitMaterialsStoreVirtual`/`elematerialsstock`), writes payable rows into `CahPaymentables`, updates last-price into `EleMaterials`/`EleMaterialsSupply`, propagates check-status into `PurPurchaseOrderSlave`/`PurPurchaseArriveSlave`/`PurPurchaseRejectSlave`/`QlyComeMaterialsTestSlave`, enforces `Sp_calc_materialsstoreLimit` (stock-limit refuse on negative), then flips `bCheck=1, sStatus=1` on the master/slave rows. `iFlag=0` calls `Sp_Bill_Used` to refuse uncheck when downstream rows exist, then symmetrically reverses.

**Invocation:** Bound to `gdsmodule.sProcName` on 采购入库 (sId `192116810113315220315711060`, parent 物料采购流程) and 扫码入库(单据) (sId `101251240115016051704122330`, parent 功能操作) — dispatched by `BusinessBaseServiceImpl.getPrcName(sFormGuid, …)` on the audit/un-audit button. Multiple upgrade scripts (`20220126-2861-仓库冻结后，入库、领料、送货单不可消审/Sp_Calc_sPis.sql`, `20250616/sp_calc_spis.sql`, `30100101/Sp_Calc_sPis.sql`) ship in xly-src — verify which is deployed.
