# `Sp_Calc_sPmd` (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_sPmd`'`._

## Narrative

**Business context:** Unified material-movement audit (`COMMENT '生产领料、补料、退料、报废'`) — single procedure that handles production issue (领料), re-issue (补料), return (退料), scrap (报废), and several inbound variants, all keyed by `sFormGuid` against `MitProductionMaterialsMaster`. Posts movements into `MitMaterialsStore` (the unified material-stock ledger) on check; reverses on uncheck.

**What it does:** Guards on `bInvalid`/`SysLocking`. Reads `SysSystemSettings.CkxMaterialsStockLimit` to decide whether stock-shortage refusal is hard. On `iFlag=1` enforces `Sp_calc_materialsstoreLimit` per slave row (returns negative `sCode` with the offending material list), calls `Sp_MaterialsStoreVirtual` to recompute the virtual-stock projection, inserts movement rows into `MitMaterialsStore` keyed on `sFormId` (issue vs return etc. controlled by the source form's sign convention), then flips `bCheck=1, sStatus=1, sCheckPerson, tCheckDate` on `MitProductionMaterialsMaster`. `iFlag=0` symmetrically reverses (deletes the `MitMaterialsStore` rows and clears flags).

**Invocation:** Bound to `gdsmodule.sProcName` on a wide set of forms under 物料领用流程 (生产领料单据, 生产退料单据, 1/1 新增生产补料) and 材料库 (来料入库单据, 其它入库单据, 其它出库单据, 生产领料(新), 生产退料(唯一码), 生产补料(唯一码)), plus 扫码领料(单据) under 功能操作 — all dispatched by `BusinessBaseServiceImpl.getPrcName(sFormGuid, …)`. xly-src ships current (`script/标版/20250616/Sp_Calc_sPmd.sql`) and legacy (`Sp_Calc_sPmd(old).sql`) versions plus an upgrade patch — verify which is deployed.
