# `Sp_calc_materialsstoreLimit` (procedure)

> 材料库存计算：审核包含自身，消审不包含自身 消审要在删除库存明细前调用，审核再插入库存明细后调用


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

## Parameters

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

## Body

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

## Narrative

**Business context:** 库存管理 — material-inventory month-end-limit helper: drives recomputation of `Sp_calc_materialsstoreCalc` for one bill so that running inventory stays consistent when a material movement is checked or unchecked within an accounting period (`sysaccountperiod`). Per the proc comment: "审核包含自身，消审不包含自身 消审要在删除库存明细前调用，审核再插入库存明细后调用" — on check, run *after* inserting detail rows; on uncheck, run *before* deleting them.

**What it does:** Validates all six required params; loads the latest frozen `sysaccountperiod.tEndDate` for the brand/subsidiary; decides whether the bill's `tCreateDate` falls inside the current open period or in a closed one (gated by `bFrozen=1`). Based on `iFlag` (1=check, 0=uncheck) it sets `p_iAdd`/`p_iCountCalc` flags then delegates downstream — typically to materials-balance recompute helpers — using the right date window so closed periods stay locked.

**Invocation:** Called from the `Sp_Calc_s*` materials-bill check procs that mutate material inventory: `Sp_Calc_sCut`, `Sp_Calc_sDgd`, `Sp_Calc_sDgdCL`, `Sp_Calc_sMad`, `Sp_Calc_sMcd`, `Sp_Calc_sMit`, `Sp_Calc_sMma`, `Sp_Calc_sMmm`, `Sp_Calc_sPis`, `Sp_Calc_sPmd`, and others — each calls this proc as part of its bill-check / bill-uncheck routine. xly-src refs are in customer SQL bundles under `script/标版/`.
