# `Sp_Calc_sPrj` (procedure)

> 采购退货


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

## Parameters

| # | Mode | Name | Type |
|---|---|---|---|
| 1 | IN | `iFlag` | `int` |
| 2 | IN | `iTmpCheck` | `int` |
| 3 | IN | `sFormGuid` | `varchar(100)` |
| 4 | IN | `sGuid` | `varchar(100)` |
| 5 | IN | `sLoginId` | `varchar(100)` |
| 6 | OUT | `sReturn` | `varchar(4000)` |
| 7 | IN | `sBrId` | `varchar(100)` |
| 8 | IN | `sSuId` | `varchar(100)` |
| 9 | OUT | `sCode` | `int` |

## Body

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

## Narrative

**Business context:** KPI流程操作菜单 → 物料采购流程 → 2/4 新增采购退货 — purchase-rejection (采购退货, return-to-supplier) audit on `PurPurchaseRejectMaster`/`Slave`. The check posts the return out of material stock, registers the negative payable, and back-stamps the matched stock-in line; the uncheck rolls every leg back.

**What it does:** Guards on `bInvalid`/`SysLocking` via a join on `PurPurchaseRejectSlave`. Aggregates `dMaterialsMoney`/`dMaterialsTaxMoney` from the slave; reads supplier from the master. On `iFlag=1`: enforces `Sp_calc_materialsstoreLimit` to refuse uncheck-blocking shortages; calls `Sp_System_AutoMaterialsPriceSingle` to refresh material last-price post-return; deletes any stale `MitMaterialsStore`/`CahPaymentables` for this bill and reinserts the negated movement rows + payable row (`INSERT INTO MitMaterialsStore`, `INSERT INTO EleMaterialsStock`); updates `EleMaterialsStock` aggregate; back-stamps `dRejectQty` into `purpurchaseinstoreslave`/`purpurchaseorderslave`; flips `bCheck=1, sStatus=1` on `PurPurchaseRejectMaster`. `iFlag=0` calls `Sp_Bill_Used` to refuse uncheck when downstream rows depend on it, then reverses.

**Invocation:** Bound to `gdsmodule.sProcName` on 2/4 新增采购退货 (sId `192116810113315221425660510`, parent 物料采购流程) — dispatched by `BusinessBaseServiceImpl.getPrcName(sFormGuid, …)` on the audit/un-audit button. xly-src ships `script/标版/30100101/Sp_Calc_sPrj.sql` (install) and `Sp_Check_sPrj.sql` (cross-check helper invoked before audit).
