# `Sp_saveReturn_sCldApply` (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_sCldApply`'`._

## Narrative

**Business context:** 销售管理 / 生产 → 子产品领用申请 — 反写 ("back-write") helper for the 子产品领用申请 (sCldApply / `salchildproductapply*`) module's save lifecycle. When a 子产品领用申请 row is saved or invalidated, this proc rolls the slave-level qty + gift-qty back to the source order/work-order slave (`salsalesorderslave` / `mftworkorderslave`) so the originating bill always shows the latest "已申请领用" totals.

**What it does:** Parses `sGuid` (CSV of 申请 master ids) into `p_t_sId`. Resolves source slave ids into `p_sSrcSlaveId_tmp` by joining `salchildproductapplyslave.sParentId`. Aggregates per `sSrcSlaveId` into `t_data_temp`: `SUM(dProductQty)` and `SUM(dGiveQty)` from `salchildproductapplyslave` joined to `salchildproductapplymaster` where `bInvalid=0`. When `iTag=1` (保存前) excludes the rows being saved (`INSTR(sGuid, M.sId)>0 => 0`); when `iTag=2` (保存后) includes them. Then `UPDATE salsalesorderslave A.dOutProductQty = B.dProductQty, A.dOutGiveQty = B.dGiveQty` and the same against `mftworkorderslave`.

**Invocation:** Called by `Sp_beforeSave_sCldApply` (`iTag=1`) and `Sp_afterSave_sCldApply` (`iTag=2`). The before/after wrappers would attach as `gdsmodule.sSaveProName(Before)` on the 子产品领用申请 form. No such hook row exists in the current `gdsmodule` snapshot — likely a customer-specific module not enabled in this tenant. Candidate for maintainer audit.
