# `Sp_MaterialsCarryforward` (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_MaterialsCarryforward`'`._

## Narrative

**Business context:** 成本管理 → 材料成本结转 — period-by-period reconciliation report contrasting business-side and finance-side material stock numbers for the supplied 会计期间 (`sCostPeriod`). Helps a cost accountant confirm that the carry-forward (`结转`) totals tie out before closing the period.

**What it does:** Builds temporary table `TmpMaterialsCarryforward` keyed by `sPeriodId` (filtered against `sysaccountperiod`, frozen-period guard, lower-bound `>=202209`); for each period it derives 14 totals — 上期暂估总数量/金额, 本期抵消总数量/金额, 业务系统登记发票总数量/金额, 本期财务结转发票总数量/金额, 本期业务系统/财务结转材料总出库数量, 上/本期初末结存数量/金额 — by summing `viw_accmaterialsinit`, `accmaterialsstore` (filtered by 物料结转 `sFormId`s), `viw_purpurchasechecking`, `mitmaterialsstore`, and `elematerialsstockmonthcost`. Returns the whole tmp table ordered by `tStartDate`.

**Invocation:** Called from inside `Sp_System_MaterialsCost` during the 全月加权 cost-allocation run; that parent proc is itself a 按钮事件 dispatched on the 成本结转 button. xly-src ships `script/标版/30100101/cost/Sp_MaterialsCarryforward.sql` (install). Read-only — no writes outside the temp table.
