# `Sp_CalcCost_BtnEventCalcCost` (procedure)


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

## Parameters

| # | Mode | Name | Type |
|---|---|---|---|
| 1 | IN | `sProInParam` | `varchar(10000)` |
| 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_CalcCost_BtnEventCalcCost`'`._

## Narrative

**Business context:** 业财一体化 / 成本核算 — month-end cost-accounting run. Triggered from the 成本核算 (cost accounting) screen's main 核算 button against one accounting period (`sysaccountperiod`); rolls work-order direct cost, indirect material cost, expense-allocation by cost-centre, and frozen-inventory valuation, then writes back to the order/stock/period tables.

**What it does:** Parses one `sysaccountperiod.sId` out of `sProInParam`, refuses when the prior period has no `sCalCostPerson` (must close prior period first). Stamps `mftworkordermaster.tFactCountCost = period_end + 1` on every checked order whose `tCreateDate` falls in the period (and on un-finalised carry-over orders). Then performs the cost roll-up via dynamic SQL into `TmpCost`, joins back into `MftWorkOrderSlave`/`CotOrderCostAnalysis`/`EleProductStock`/`PitProductInStoreSlave`, and finally writes `sCalCostPerson`, `tCalCostDate`, `bCost=1` (and related flags) on `sysaccountperiod`. Uses `EleWorkCenter` and `EleCostFrame` counts to scope which work-centres/expense frames apply.

**Invocation:** Dispatched dynamically by `GenericProcedureCallServiceImpl.doGenericProcedureCall()` — the 成本核算 form's 核算 toolbar button names `Sp_CalcCost_BtnEventCalcCost` as the action. Companion procs (`Sp_CalcCost_BtnEventReCalcCost`, `Sp_CalcCost_BtnEventReCalcCost_new`) cover re-run scenarios.
