# `Sp_System_ProductionCostBak` (procedure)

> @author:杨恒林
							@date:20210523 封存
							@describe: 从成本月结表中，拿到工单数据，计算本月成本数据后回写到成本月结表	注意点：
							1.本期期初成本从本工单最近一次月结成本表中获取，不一定就是上期
							2.在制品是指所有入库数小于工单数的工单


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

## Parameters

| # | Mode | Name | Type |
|---|---|---|---|
| 1 | IN | `sProInParam` | `varchar(10000)` |
| 2 | IN | `iSecond` | `int` |
| 3 | IN | `sMakePerson` | `varchar(100)` |
| 4 | IN | `sBrId` | `varchar(100)` |
| 5 | IN | `sSuId` | `varchar(100)` |
| 6 | OUT | `sReturn` | `varchar(4000)` |
| 7 | OUT | `sCode` | `int` |

## Body

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

## Narrative

**Business context:** 成本管理 → 工单成本核算 — backup/sealed variant of `Sp_System_ProductionCost` from 杨恒林 (@date:20210523 封存, per the header comment). Computes work-order cost from the cost monthly-summary table and writes the period's cost data back to `accordercostanalysis`/`accworkcenter`/`accdepart`. Period opening (本期期初) cost comes from the work order's most recent month-end cost row (not strictly the prior period); 在制品 means work orders where total inbound qty < ordered qty. Per `封存` (sealed) tag, this is a historical snapshot of the algorithm before further refactoring.

**What it does:** Same operation as the production-active `Sp_System_ProductionCost`: parses `sProInParam` JSON to a `p_sId` period, reads `NetMoney`/`NetPrice` decimal settings from `SysSystemSettings`, joins `sysaccountperiod` to derive `p_tStartDate`/`p_tEndDate`/`p_sPeriod`, validates the period isn't already costed (`bCost=1`) and that 直接材料 (`bCalMaterials=1`) is complete, then iterates the cost centres and expense subjects (`p_sAllCostCenter`, `p_sAllExpense`) to allocate 本期作业汇总, 直接材料, 发外发票, 计件工资, 期初/期末 by `sCostFrameId`. Builds dynamic SQL into `accordercostanalysis` and `accworkcenter`, populates `cotordercostanalysis` snapshot, and at the end flips `sysaccountperiod.bCost=1`.

**Invocation:** Status: appears orphaned. No `gdsmodule` binding, no form-master, no other-proc, no xly-src match — not even an install-script copy. The `封存` (sealed) author tag in the header signals this is a frozen reference copy of `Sp_System_ProductionCost` kept for compare/rollback, not wired to any button. Candidate for maintainer audit / deletion.
