# `Sp_System_ProductMoney` (procedure)

> @author:杨恒林
							@date:20210521 封存
							@describe: 计算库存产品商品价值并存入月结表 主要思路如下：
							从本月月末之前最近一次订单取价格，如果取不到 从 产品客户表中取 ，再取不到 从产品表中取，再取不到,从最近取对帐
							bUpdate = 1 时，表示月末结帐时调用，0：在库存商品价值表界面 调用


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

## Parameters

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

## Body

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

## Narrative

**Business context:** 成本管理 → 产品存货核算 → 库存商品价值 — month-end recalculation of finished-product inventory value, writing the 收/发/存 (in/out/end) snapshot per `sProductId` to `eleproductstockmonthmoney`. Header comment (@author:杨恒林 @date:20210521 封存) describes the price-lookup chain: closest sales order before month-end → 产品客户表 → 产品表 → most recent 对帐.

**What it does:** Validates the selected `sysaccountperiod` (`bCalProducts=0`, prior period must be already cost-finalised, `eleproductstockmonthcost` for this month must exist, all 销售发票 rows must be confirmed). Builds temp tables `Sp_EleProductStock` and `Sp_PitProductStore` seeded from `accproductstore` + `viw_accproductinit` + `eleproductstockmonthmoney`(prior). Pulls 上期 qty/money via `UPDATE … Inner Join`. Imports current-period confirmed `AccProductStore` rows tagged by `iInOut` (0 in, 1 out, 2 调整, 3 退货, 4 other) — covers 送货单/退货单/发票/存货调整/etc. Auto-generates an offsetting 存货调整确认单 (`pitproductmoneymaster`/`pitproductmoneyslave` with fixed `sId='999917794310946194302692936'`, form `192116810113315232425737501`) for products where `dEndQty=0` but `dEndMoney<>0`. Recomputes 期末 qty/money/price, then `DELETE`/`INSERT` `eleproductstockmonthmoney` and finally flips `sysaccountperiod.bCalProducts=1`.

**Invocation:** Status: appears orphaned. No `gdsmodule` binding, no form-master, no other-proc match, no xly-src caller beyond `script/标版/30100101/cost/Sp_System_ProductMoney.sql` install script. `封存` (sealed) author tag suggests this is the v1 snapshot; the live caller routes through `Sp_System_ProductMoneyAllSure` (batch) instead. Candidate for maintainer audit — may have been superseded.
