# `Sp_saveReturn_sDgd` (procedure)

> 送货单单据反写


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

## Parameters

| # | Mode | Name | Type |
|---|---|---|---|
| 1 | IN | `sGuid` | `longtext` |
| 2 | IN | `sBrId` | `varchar(100)` |
| 3 | IN | `sSuId` | `varchar(100)` |
| 4 | IN | `iTag` | `int` |
| 5 | OUT | `sCode` | `int` |
| 6 | OUT | `sReturn` | `longtext` |

## Body

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

## Narrative

**Business context:** 销售管理 → 销售送货流程 → 送货单单据 (and the 材料送货单 / 扫码送货 mobile variants) — back-writes the delivered quantity onto every upstream source row when a 送货单 (`saldelivergoodsmaster`) is saved, so 销售订单 / 送货通知单 / 销售退货 / 工序发外入库 / 成品入库 remaining-balance columns stay in sync with what has actually shipped.

**What it does:** Splits `sGuid` (comma-separated master ids) into temp table `p_t_sId`, collects every `saldelivergoodsslave.sSrcSlaveId` they touch into `p_sSrcSlaveId_tmp`, sums `dProductQty / dMaterialsQty / dGiveQty / dKnifeMouldMoney / dFreightMoney / dPlateMoney / dProofingMoney / dSparesQty` (with the current batch zeroed out on `iTag=1` pre-save) into `t_data_temp`, then issues UPDATEs against the upstream source identified by `SysSystemSettings('CbxDeliverGoods')`: `SalSalesOrderSlave` (`dDeliverQty / dMaterialsDeliverQty / dPresentQty …` when src=`salesorder`), `SalDeliverNotifySlave`, `SalRejectGoodsSlave`, `OpsOutsideInStoreSlave`, `PitProductInStoreSlave`. Also re-numbers `saldelivergoodsslavechildren.iOrder` on pre-save.

**Invocation:** Called by `Sp_beforeSave_sDgd` with `iTag=1` and by `Sp_afterSave_sDgd` with `iTag=2`. Those wrappers are bound on `gdsmodule.sSaveProNameBefore` for 送货单单据 (`11811781131121915179924491520`), 扫码送货(单据) (`101251240115015959853077870`), 材料送货单 (`101251240115016140459724020`) under 销售管理 → 销售送货流程 / 销售管理 → 销售单据 / 移动端管理 → 现场 → 功能操作 — and trigger via `BusinessBaseServiceImpl` save hooks. On cancel/uncancel/delete the dispatchers `Sp_Invalid_backwriting` and `Sp_delete_backwriting` re-route to the same beforeSave/afterSave wrappers for these form ids. No xly-src callers.
