# `Sp_WorkCostCarryforward` (procedure)


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

## Parameters

| # | Mode | Name | Type |
|---|---|---|---|
| 1 | IN | `sCostPeriod` | `varchar(100)` |
| 2 | IN | `sBrId` | `varchar(100)` |
| 3 | IN | `sSuId` | `varchar(100)` |
| 4 | OUT | `sReturn` | `varchar(1000)` |
| 5 | OUT | `totalQty` | `decimal(18,6)` |
| 6 | OUT | `countMapJson` | `longtext` |
| 7 | OUT | `sCode` | `int` |

## Body

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

## Narrative

**Business context:** 成本核算 / 月结结转 — period-close pre-flight that builds a `TmpWorkCostCarryforward` reconciliation table for the closing accountant. For every period in `sCostPeriod` that's not yet frozen and ≥ 202209, it sums hours/qty/money across the work-hour, work-center, department, materials-issue, outside-process estimate, and cost-allocation modules so the operator can spot mismatches before locking the period.

**What it does:** Loads candidate `sysaccountperiod` rows into a temp table, populates 25+ aggregates per period (e.g. `dWorkHourDetailHour`/`dWorkHourSumHour` from `accordercostanalysis`, `dWorkCenterHourSumHour` from `accworkcenter`, `dDepartHourSumHour` from `accdepart`, materials issuance from `Accmaterialsstore`/`accproductionmaterialsslave`, outside-process estimate from `viw_accProductDoing`, expense entries from `accexpenseentry`/`accdepartexpense`/`accworkcenterexpense`/`accworkexpense`), then validates that detail vs. summary totals match and returns the discrepancy rows.

**Invocation:** Called from `Sp_System_ProductionCost` (`call Sp_WorkCostCarryforward(p_sPeriod, sBrId, sSuId, …)`), the cost-period close orchestrator that drives the monthly cost run. Install script: `script/标版/30100101/cost/Sp_WorkCostCarryforward.sql`.
