# `Sp_saveReturn_sOiv` (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_sOiv`'`._

## Narrative

**Business context:** KPI流程操作菜单 → 发外加工流程 → 发外发票 — when a 发外发票 (`OpsOutsideInvoiceMaster`) is saved, back-writes the invoiced amount onto the upstream 发外对账 row (`OpsOutsideCheckingSlave`), keeps the supplier running account (`CahSupplyInit`) and any financial adjustment (`CahFinancialAdjust`) in sync, and flips the invoice-state flag on the invoice master itself.

**What it does:** Splits `sGuid` into `p_t_sId`, materialises the source `sSrcSlaveId` set (the 发外对账 rows) into `p_sSrcSlaveId_tmp`, sums the invoiced amount/qty (with `iTag=1` zeroing the in-flight batch) into `p_data_tmp`, then updates `OpsOutsideCheckingSlave.dInvoiceMoney …`, `CahFinancialAdjust`, `CahSupplyInit` (supplier ledger), and `OpsOutsideInvoiceMaster` for any post-save status flags.

**Invocation:** Called by `Sp_beforeSave_sOiv` (`iTag=1`) and `Sp_afterSave_sOiv` (`iTag=2`). The beforeSave wrapper is bound on `gdsmodule.sSaveProNameBefore` for 发外发票 (`101251168116315832142387420`) under 发外加工流程 — fired by `BusinessBaseServiceImpl` save hooks. No xly-src callers.
