# `Sp_saveReturn_sSmi` (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_sSmi`'`._

## Narrative

**Business context:** 生产管理 → 半成品入库 — quantity back-write helper for the 半成品入库 (semi-goods in-store, `sgdsemigoodsinstoremaster` / Slave) doc. When a semi-goods receipt is saved or invalidated this proc rolls the received qty/money back onto the upstream 生产工单 slave (`MftWorkOrderSlave`) so the work-order shows its current in-stocked qty.

**What it does:** Parses `sGuid`, finds source rows (`sSrcSlaveId`) on `sgdsemigoodsinstoreslave`, then builds per-source aggregates (`dProductQty`, `dProductMoney`, `dLossQty`, `dMaterialsQty`, plus proofing/plate/knife/freight money breakouts and `dRemainderQty`). With `iTag=1` the rows in the current save-set are excluded from the sums (before-save), with `iTag=2` they are included (after-save). The aggregates are UPDATEd onto `MftWorkOrderSlave`.

**Invocation:** Wrapped by `Sp_beforeSave_sSmi(iTag=1)` / `Sp_afterSave_sSmi(iTag=2)` (and the `Sp_beforeSave_sSmia` / `Sp_afterSave_sSmia` variants for the secondary form). Those wrappers fire on save of the 半成品入库 form and re-fire on invalidate/delete via `Sp_Invalid_backwriting` / `Sp_delete_backwriting`. xly-src install scripts: `script/标版/30100101/Sp_beforeSave_sSmi.sql` and `.../Sp_afterSave_sSmi.sql`.
