# `Sp_Calc_sDgd` (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_sDgd`'`._

## Narrative

**Business context:** 销售管理 → 销售送货流程 → 送货单 — sales-delivery-note check / uncheck on `saldelivergoodsmaster`. The 送货单 audit is the central post-shipment trigger: on approval it deducts finished-product stock, posts the movement to `MitMaterialsStore`/`EleMaterialsStock`, back-writes the delivered qty into the upstream sales-order / work-order / outside-in-store / reject slaves, and marks the bill as used in `Sp_Bill_Used`. Same proc serves the desktop 送货单, the mobile 扫码送货 form, and the 材料送货 (material delivery) variant — and is invoked downstream by sales-return audits to reverse a delivery.

**What it does:** Validates `sGuid`. Loads `tCreateDate`, `bInvalid`, customer fields; refuses if `bInvalid=1`; checks `sysaccountperiod.bFrozen` per `SysSystemSettings.CkxIntervalMonthModifyBill`. With `iFlag=1`: refuses if already checked; opens cursors over the slave rows to `Update SalSalesOrderSlave` (delivered qty), `Update MftWorkOrderSlave`, `Update OpsOutsideInStoreSlave`, `Update PitProductInStoreSlave`, `Update PitProductStoreVirtual`, `Update SalRejectGoodsSlave`; `INSERT INTO MitMaterialsStore` posts the movement; `CALL Sp_calc_materialsstoreLimit` / `Sp_calc_productstoreLimit` re-check stock limits; finally `CALL Sp_Bill_Used` registers consumption. With `iFlag=0`: symmetric uncheck reverses each step.

**Invocation:** Bound to `gdsmodule.sProcName` on:
- `送货单单据` (sId `11811781131121915179924491520`) — 销售管理 → 销售送货流程
- `扫码送货(单据)` (sId `101251240115015959853077870`) — 现场 (mobile scan-deliver)
- `材料送货单` (sId `101251240115016140459724020`) — 成品库 → 成品库单据

Also invoked by other stored procedures: `Sp_Calc_sRct`, `Sp_Calc_sRct_copy1`, `Sp_Calc_sRcts` (sales-return audits that reverse a delivery), and `Sp_Sis_CopeEventList` (event-replay helper). xly-src `xlyBusinessService/.../BusinessBaseServiceImpl.java` line ~3047 contains a *commented-out* `Sp_Calc_sDgd` special-case for `partyId`-routing — currently inactive but kept around as a placeholder. xly-src also ships `script/客户/千彩/Sp_Calc_sDgd.sql` (customer-specific override) and `script/标版/upgrade/20220126-2861-仓库冻结后...` (warehouse-freeze restriction patch) — verify which is deployed.
