# `Sp_BtnEventProductionmaterialSure_New` (procedure)


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

## Parameters

| # | Mode | Name | Type |
|---|---|---|---|
| 1 | IN | `sProInParam` | `longtext` |
| 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_BtnEventProductionmaterialSure_New`'`._

## Narrative

**Business context:** 成本核算 / 生产领料 — toolbar handler that batch-toggles 确认 on `accproductionmaterialsslave` (生产领料明细) rows for a costing period, then rebuilds the indirect-material expense-entry (`accexpenseentrydepartmentmaster` / 公摊费用录入) and its department/work-center splits keyed on the `elematerials.sOtherCostFrameId` cost-frame.

**What it does:** Reads `$.params[0].value[0].sCostPeriod` and the array of `sId`s; builds `Sp_ids_Table` via PREPARE/EXECUTE. Gates: if `sysaccountperiod.bCost=1` for the period, returns `sCode=-1, sBtnDepartFinished`. Toggles `accproductionmaterialsslave.bConfirm` and stamps `sConfirmMonth` for matching `(sCostPeriod, sBrId, sSuId)`. Then DELETEs prior `accexpenseentrydepartmentmaster/slave` and `accexpenseentryworkcenterslave` rows whose `sMakePerson='系统插入'` for this period and whose 成本架构 sFrameType is one of `auxiliary`, `mainMaterials`, `mainMaterialsBox`. For each remaining distinct `sOtherCostFrameId` (cursor `Cur_ReplaceField`), allocates `SP_Sis_GetMaxNo` and INSERTs a new expense-entry master (`sFormId='101801153119616595200652250'`, `dExpenseeMoney = Σ accproductionmaterialsslave.dMaterialsMoney` joined to `sisdepart`), then inserts `accexpenseentrydepartmentslave` per department and apportions to work-centers via temp tables `accproductionmaterialssumslave_Tmp`, `accmaterialsworkcenter_Tmp`, `accmaterialsdiffworkcenter_Tmp` (rate = `accworkcenter.dProcessQty`; diff truing-up so the work-center splits sum back to the department total). Finally calls `Sp_System_CheckSaveFlowCps` and `Sp_Calc_sCps` to trigger save-flow and recalculation on the new expense-entry master.

**Invocation:** Dispatched dynamically by `GenericProcedureCallServiceImpl.doGenericProcedureCall()` (POST `/procedureCall/doGenericProcedureCall`) — 生产领料(成本) grid's 确认 toolbar button names this proc as its action. Siblings in the cost-confirm family: `Sp_BtnEventDepartSure_New`, `Sp_BtnEventworkcenterSure_New`, `Sp_BtnEventMaterialsSure_New`, `Sp_BtnEventProductInStoreSure_New`, `Sp_BtnEventProductSure_New`.

Flag: the period gate reads `p_sPeriodId` BEFORE it's parsed from `sProInParam` (the `SELECT bCost INTO p_bCost ... WHERE sPeriodId=p_sPeriodId` runs first, with `p_sPeriodId` still empty/NULL), so the gate is effectively skipped on the first call. The parsing block sits after the gate, ordering is wrong. Maintainer audit recommended.
