# `Sp_saveReturn_sPis` (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_sPis`'`._

## Narrative

**Business context:** 采购管理 → 采购入库 — back-write helper for 采购入库 (`PurPurchaseArriveSlave` receipt flow). When a receipt is saved or cancelled, it re-aggregates the inwarded qty back into the source 采购订单 line, the 采购退货 line, and the incoming-material inspection master so the order's "已入库" and the QC master's "已入" stay in sync.

**What it does:** Parses `sGuid` into `p_t_sId`, collects `sSrcSlaveId/sPurchaseOrderSlaveId` aggregates into `t_data_temp/t_data_temp2/p_sPurchaseOrderSlaveId_tmp`, then updates `PurPurchaseOrderSlave` (received qty), `PurPurchaseArriveSlave` (audit columns), `PurPurchaseRejectSlave` (returned-against-receipt) and `qlycomematerialstestmaster` (inspection coverage). `iTag=1` (before/delete) excludes the current doc; `iTag=2` (after-save) includes it.

**Invocation:** Wrapped by `Sp_afterSave_sPis` (iTag=2) and `Sp_beforeSave_sPis` (iTag=1). `Sp_beforeSave_sPis` sits on `gdsmodule.sSaveProNameBefore` of 采购入库 (`Sp_Check_sPis`/`Sp_beforeSave_sPis`); `Sp_afterSave_sPis` is dispatched by `Sp_Invalid_backwriting` on cancel/un-cancel. Install script: `xly-src/script/标版/30100101/Sp_afterSave_sPis.sql`.
