# `Sp_Insert_EstimateInstore` (procedure)


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

## Parameters

| # | Mode | Name | Type |
|---|---|---|---|
| 1 | IN | `p_sPeriod` | `varchar(100)` |
| 2 | IN | `sBrId` | `varchar(100)` |
| 3 | IN | `sSuId` | `varchar(100)` |

## Body

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

## Narrative

**Business context:** 财务管理 → 月结 / 暂估入库生成 — month-end "estimated receipt" (暂估入库) generator. After 财务月结 (financial period-close) runs, this proc rolls every `viw_purpurchaseinstore` row whose source 采购入库 (purchase-into-store) has no matching 采购发票 (purchase invoice) into the period's estimated-receipt accrual so that the cost can be booked even though the invoice hasn't yet arrived. Reversed automatically next period.

**What it does:** Resolves `p_sPriorPeriod` and `p_sNextPeriod` from `sysaccountperiod` (the periods immediately before / after `p_sPeriod`). Builds temp table `accpurchaseinstoremasterTmp` mirroring the `accpurchaseinstoremaster` shape (~30 columns) and seeds it from `viw_purpurchaseinstore` with `bInvalid=0 AND bcheck=1 AND date_format(tCreateDate,'%Y%m')=p_sPeriod`, stamping `sCostPeriod=p_sPeriod`, `sCostNextPeriod=p_sNextPeriod`. The temp table then drives the actual `accpurchaseinstoremaster`/`accpurchaseinstoreslave` insert (plus the offsetting next-period reversal rows) and links each accrual back to the source via `sSrcId`/`sSrcSlaveId`.

**Invocation:** Called by DB proc `Sp_financialClosing_BtnEventCheckout` (财务月结 → 结账 button-event handler) during the month-end-close pipeline alongside `Sp_financialClosing_BtnEventCalcMaterialPrice` and `Sp_plan_mftproductionplanBak`. xly-src ships the install script `script/标版/30100101/Sp_Insert_EstimateInstore.sql` and a copy inside the bundled `Sp_financialClosing_BtnEventCheckout.sql`.
