# `Sp_Calc_sMcd` (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_sMcd`'`._

## Narrative

**Business context:** 材料库存 → 材料库单据 → 材料盘点单据 — material stocktake audit / un-audit on `MitMaterialsCheckMaster`/`MitMaterialsCheckSlave`. Approving a stocktake commits the counted snapshot: gain rows boost `MitMaterialsStore`, loss rows decrement it, and a `MitCheckProfitLoss` row records the gain/loss delta so finance can reconcile.

**What it does:** Validates `sGuid`, refuses when `bInvalid=1` or already in the wrong `bCheck` state. On `iFlag=1` flips `MitMaterialsCheckMaster.bCheck=1` plus slave rows, writes/clears `MitCheckProfitLoss`, and `INSERT INTO EleMaterialsStock` to refresh on-hand totals from `MitMaterialsStore`. On `iFlag=0` reverses by `Delete from MitCheckProfitLoss`, restoring prior `MitMaterialsStore` rows, and `Delete FROM EleMaterialsStock` followed by a fresh re-aggregation.

**Invocation:** Bound to `gdsmodule.sProcName` on `材料盘点单据` (sId `192116810113315228128521360`) under 材料库存 → 材料库单据. Dispatched by Java `BusinessBaseServiceImpl.getPrcName(sFormGuid, …)` on the 审核/反审核 button. xly-src ships `script/标版/30100101/Sp_Calc_sMcd.sql` (install) and `script/标版/optimize/Sp_Calc_sMcd.sql` (perf patch).
