# `Sp_ProductCarryforward` (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_ProductCarryforward`'`._

## Narrative

**Business context:** 成本管理 → 产成品成本结转 — period-close reconciliation report comparing the cost-side production output (`accproductstore`) against the business-side sales/delivery/invoice/freight numbers for one or more accounting periods. The single result set drives the manager-facing 结转校对 view that flags discrepancies before the period is frozen.

**What it does:** Builds temp `TmpProductCarryforward` keyed by `sCostPeriod` (loops `sysaccountperiod` rows ≥ 202209 and ≤ the earliest unfrozen period), then back-fills ~20 measures per period: cost-output money, instore money, business delivery/return qty, finished-goods consumption, current-out qty, freight totals, invoice qty/no-tax money on both business and cost sides, opening/closing balances on both `eleproductstockmonthcost` and `eleproductstockmonthmoney`. Also issues `CALL Sp_SalSalesCheck1` for a side-effect refresh of sales-check totals. Returns `SELECT * FROM TmpProductCarryforward ORDER BY tStartDate`.

**Invocation:** Chained from `Sp_System_ProductCost` (`CALL Sp_ProductCarryforward(p_sPeriod, sBrId, sSuId, @sReturn, @totalQty, @countMapJson, @sCode)` after the cost engine writes its own carryforward rows) — that engine in turn runs from the cost-period button family (`Sp_BtnEventProductInStoreSure_New`, `Sp_financialClosing_BtnEventProductStock_New`). xly-src ships `script/标版/30100101/cost/Sp_ProductCarryforward.sql`. No standalone form/UI dispatcher — strictly a downstream of cost-closing.
