# `Sp_ProductStoreVirtual` (procedure)

> 印件-虚拟库存明细


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

## Parameters

| # | Mode | Name | Type |
|---|---|---|---|
| 1 | IN | `sGuid` | `varchar(4000)` |
| 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_ProductStoreVirtual`'`._

## Narrative

**Business context:** 印件 / 虚拟库存 — rebuilds the virtual-stock projection table `PitProductStoreVirtual` for the bills passed in `sGuid` (comma-separated). "Virtual stock" is the qty that *will* be available once the upstream bill (sales order, work order, in-store, or delivery-notify) is delivered/completed. Used by the 印件 delivery flow to show planners "how much do we owe customers vs. how much is on its way".

**What it does:** Reads `CbxDeliverGoods` and `CbxDeliverGoodsNotify` from `SysSystemSettings` to pick the source mode. Splits `sGuid` into a temp `p_t_sId`. Deletes prior `PitProductStoreVirtual` rows for those bills. Depending on the source (`salesorder` / `workorder` / `productinstore` / `delivernotify` with sub-mode `salesorder` or work-order-joined), inserts new rows computed as `dProductQty + dGiveQty - dDeliverQty` (or the corresponding delivered/in-store sum) joined to `SalSalesOrderSlave` and `salsalesordermaster`, skipping rows already `bDeliverComplete=1`. Finally deletes virtual rows with `dProductQty <= 0`.

**Invocation:** Called from at least eight other procs in the audit/calc family: `Sp_Calc_sDevApply`, `Sp_Calc_sDgd` (送货单), `Sp_Calc_sDgdCL`, `Sp_Calc_sDgd_1226`, `Sp_Calc_sDgd_20250612`, `Sp_Calc_sDYDgd`, `Sp_Calc_sScd`, `Sp_Calc_sSod` (销售订单) — i.e. every audit/un-audit of an order/work-order/delivery refreshes the virtual stock for the affected bills. Install script ships at `script/标版/30100101/Sp_ProductStoreVirtual.sql` plus a 高旺-customer variant `script/客户/高旺/20211101成品库存多单位/Sp_ProductStoreVirtual.sql` — verify which is deployed.
