# `Sp_Calc_sRgd` (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_sRgd`'`._

## Narrative

**Business context:** 销售管理 → 销售单据 → 材料销售退货单据 (and KPI流程操作菜单 → 销售送货流程 → 2/4 新增销售退货) — audit / un-audit of a 销售退货 (sales return / RGD = reject-goods) bill on `SalRejectGoodsMaster`/`SalRejectGoodsSlave`. On approval the returned material/product is posted back into inventory (`MitMaterialsStore` for material returns, `PitProductStore` for product returns), AR receivables are decremented on `CahReceivables`, and the originating delivery-goods slave is back-written to release the delivered qty.

**What it does:** Body ~74K chars. Validates `sGuid`, refuses when `bInvalid=1`, `CALL Sp_Bill_Used` to verify not consumed. On `iFlag=1` cursors `SalRejectGoodsSlave`, dispatches by material-vs-product: `INSERT INTO MitMaterialsStore` (or `PitProductStore`) the return-in row, `INSERT INTO EleMaterialsStock` history, `UPDATE EleProductStock` to add the returned qty, `CALL Sp_calc_materialsstoreLimit`/`Sp_calc_productstoreLimit`/`SP_Inventory_ProductCalcQty` to refresh on-hand and enforce warehouse caps, `CALL Sp_System_AutoMaterialsPriceSingle` to set the inbound price; back-writes `SalDeliverGoodsSlave` to free the delivered qty; `INSERT INTO CahReceivables` an offsetting negative receivable row; `CALL Sp_System_CheckFlow` to launch the configured workflow when `CkxDefineCheck=1`; flips `SalRejectGoodsMaster.bCheck=1`, `sStatus=1`. `iFlag=0` reverses all of it.

**Invocation:** Bound to `gdsmodule.sProcName` on two modules — `材料销售退货单据` (sId `101251240115016140617006200`, under 销售管理 → 销售单据) and `2/4 新增销售退货` (sId `11811781131121915179924871530`, under KPI流程操作菜单 → 销售送货流程). Dispatched by Java `BusinessBaseServiceImpl.getPrcName(sFormGuid, …)` on the audit/un-audit button. Install scripts: `script/标版/30100101/Sp_Calc_sRgd.sql`, an optimize patch `script/标版/optimize/Sp_Calc_sRgd.sql`, a 20250616 rewrite at `script/标版/20250616/Sp_Calc_sRgd.sql` (with `Sp_Calc_sRgd(old).sql` archived alongside), plus customer override `script/客户/千彩/Sp_Calc_sRgd.sql` — verify which is deployed.
