# `Sp_financialClosing_BtnEventCalcMaterialPrice` (procedure)

> @author:杨恒林
		     @date:20210521 封存
				 @describe:月末结帐调用，计算材料与成品的库存与存货金额，并存入月结表，结帐之前检查未审核单据


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

## Parameters

| # | Mode | Name | Type |
|---|---|---|---|
| 1 | IN | `p_sId` | `varchar(100)` |
| 2 | IN | `sMakePerson` | `varchar(100)` |
| 3 | IN | `sBrId` | `varchar(100)` |
| 4 | IN | `sSuId` | `varchar(100)` |
| 5 | OUT | `sReturn` | `varchar(1000)` |
| 6 | OUT | `sCode` | `int` |

## Body

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

## Narrative

**Business context:** 系统设置 → 系统参数 → 财务结账 — original "calculate material price" button on the 财务结账 panel. Per its 2021-05-21 author note, run at month-end to compute material & finished-goods stock value and write to the month-close table; first runs an unaudited-bill check.

**What it does:** Validates `p_sId` is a non-empty `sysaccountperiod.sId`, looks up `sPeriodId, tStartDate, tEndDate, bFrozen` (the `bFrozen` check is commented out in current source). When the period is not the live one, calls `Sp_financialClosing_BtnEventCheckout_Check(0, p_tStart, p_tEnd, 0, '', p_sPeriod, ..., 'admin', sCode, sReturn)` to verify no unaudited bills remain — on `sCode=-1` returns `-8`. Then calls `Sp_System_CalcMaterialsPrice(p_tStart, p_tEnd, 1, ...)` to roll weighted-average material prices into the period's 结存 table and `Sp_System_CalcProductPrice_new(p_tStart, p_tEnd, 1, sMakePerson, ...)` for finished-product prices.

**Invocation:** Dispatched by `GenericProcedureCallServiceImpl.doGenericProcedureCall()` when the user clicks the legacy 计算材料价格 button on 财务结账. Also called from inside `Sp_financialClosing_BtnEventCheckout` (the period-freeze entry point) so that legacy 结账 flows can re-roll prices. Install script: `script/标版/30100101/Sp_financialClosing_BtnEventCalcMaterialPrice.sql`; revised version is `_New`.
