# `Sp_afterSave_sPkg` (procedure)

> 采购发票（对账）保存后反写数量


- **Type:** PROCEDURE
- **Deterministic:** NO
- **SQL data access:** CONTAINS SQL

## Parameters

| # | Mode | Name | Type |
|---|---|---|---|
| 1 | IN | `sGuid` | `varchar(2000)` |
| 2 | IN | `sBrId` | `varchar(100)` |
| 3 | IN | `sSuId` | `varchar(100)` |
| 4 | OUT | `sCode` | `int` |
| 5 | OUT | `sReturn` | `varchar(4000)` |

## Body

_Body is not pre-cached. To inspect: `mysql --defaults-file=~/.my.cnf -e 'SHOW CREATE PROCEDURE `Sp_afterSave_sPkg`'`._

## Narrative

**Business context:** 采购对账 → 物料采购流程 → KPI流程操作菜单 — header says 采购发票（对账）保存后反写数量. Save-phase 反写 wrapper in the `Sp_afterSave_s<Code>` family for 采购对账 (purchase reconciliation) bills. Unlike most family members this one carries non-trivial body logic: it redistributes master-grouped purchase qty/auxiliary-qty totals back across the per-batch `purpurchasecheckingdetail` rows that source them, then calls the standard `Sp_saveReturn_sPkg` to apply the back-write.

**What it does:** counts master groups in `purpurchasecheckingslave` (grouped by `sMaterialsId`/`sMaterialsStyle`/`sParentId`); for each group, runs a nested cursor over `purpurchasecheckingdetail` ordered by `dAuxiliaryQty` ASC and distributes the slave's total `dMaterialsQty`/`dAuxiliaryQty` across detail rows — early rows get their full `dSrcQty`/`dSrcAuxiliaryQty`, the remainder consumes the residual on the final row. Then double-quote-wraps `sGuid` and calls `Sp_saveReturn_sPkg(sGuid, sBrId, sSuId, 2, @sCode, @sReturn)` (iFlag=2 = "after save" phase).

**Invocation:** Called by `Sp_Check_sPkg` (the save-commit dispatch proc bound via `gdsmodule.sSaveProName` on 采购对账) after a successful save; also dispatched by `Sp_Invalid_backwriting` for form sId `192116810113315220489243410` (采购对账) on 作废/取消作废, triggered from Java `CheckmodelServiceImpl` via `Sp_Invalidation`. Does not propagate the inner `@sCode`/`@sReturn` back to its own OUT params.
