# `Sp_saveReturn_sPmt` (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_sPmt`'`._

## Narrative

**Business context:** 财务管理 → 付款单 — back-write helper for 付款单 (`CahPaymentMaster`). The 付款 doc settles many upstream documents at once; whenever it's saved or cancelled this proc fans the paid amount back to every source line — 采购订单/对账/入库/退货, 费用报销, 外协, 财务调整, customer init — so each upstream's "已付" total stays correct.

**What it does:** Splits `sGuid` into `p_t_sId`, builds `t_data_temp` with `sSrcSlaveId` aggregates, then issues parallel UPDATEs across `PurPurchaseOrderSlave`, `PurPurchaseCheckingSlave`, `PurPurchaseInStoreSlave`, `OpsOutsideProcessSlave`, `OpsOutsideInStoreSlave`, `opsoutsidecheckingslave`, `expensereimbursementmaster/slave`, `CahFinancialAdjust`, `CahReceiptMaster` (for refund), `cahsupplyinit`, and `CahPaymentMaster` itself. `iTag=1` excludes the current 付款单 (before-save / delete); `iTag=2` includes it (after-save / uncancel).

**Invocation:** Wrapped by `Sp_afterSave_sPmt` (iTag=2) and `Sp_beforeSave_sPmt` (iTag=1). 付款单 (`sId 192116810113315233387186220`) binds `Sp_Check_sPmt`/`Sp_beforeSave_sPmt`; `Sp_afterSave_sPmt` is reached via `Sp_Invalid_backwriting` on cancel/un-cancel. Routine COMMENT says "保存前反写数量" but body covers both phases via `iTag`.
