# `Sp_Calc_sPit` (procedure)

> 成品期初


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

## Parameters

| # | Mode | Name | Type |
|---|---|---|---|
| 1 | IN | `iFlag` | `int` |
| 2 | IN | `iTmpCheck` | `int` |
| 3 | IN | `sFormGuid` | `varchar(100)` |
| 4 | IN | `sGuid` | `varchar(100)` |
| 5 | IN | `sLoginId` | `varchar(100)` |
| 6 | OUT | `sReturn` | `varchar(4000)` |
| 7 | IN | `sBrId` | `varchar(100)` |
| 8 | IN | `sSuId` | `varchar(100)` |
| 9 | OUT | `sCode` | `int` |

## Body

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

## Narrative

**Business context:** 系统设置 → 期初设置 → 产品期初单据 — finished-product opening-balance audit on `PitProductInitMaster`/`Slave`. The check posts the opening profit/loss quantity into `PitProductStore` so the closing-period stock report reflects the initial inventory state.

**What it does:** Guards on `bInvalid` against `PitProductInitMaster`. On `iFlag=1`: resets `PitProductInitSlave.dOriginalQty/dOriginalMoney/dProfitLossPrice = 0`, then computes `dProfitLossQty = dProductQty - dOriginalQty` and `dProfitLossMoney = dProductMoney - dOriginalMoney`; deletes any pre-existing `PitProductStore` row keyed on `sBillId=sGuid` and inserts a fresh stock-movement row per non-zero slave line (only when `dProfitLossQty <> 0 OR dProfitLossMoney <> 0`), backdating `tCreateDate` by `-1 MINUTE` to place it before the period start; enforces `Sp_calc_productstoreLimit` (returns `sCode=-8` on stock-shortage); then flips `bCheck=1, sStatus=1, sCheckPerson, tCheckDate` on the master. `iFlag=0` re-runs the limit check and clears the flags.

**Invocation:** Bound to `gdsmodule.sProcName` on 产品期初单据 (sId `101251240115015991142363800`, parent 期初设置) — dispatched by `BusinessBaseServiceImpl.getPrcName(sFormGuid, …)` on the audit/un-audit button. `script/标版/30100101/Sp_Calc_sPit.sql` is the install script.
