# `Sp_MonthlyStatisticalReport` (procedure)


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

## Parameters

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

## Body

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

## Narrative

**Business context:** 生产管理 → 计件工资 — month-end roll-up button that condenses `mftDaySumReport` (daily piecework totals per employee) into one row per employee per month in `mftMonthSumReport`. Operators run this after closing all the daily-sum entries for a 凭证月 (accounting month). Companion to `Sp_MonthlyStatisticalSalary`, which then applies the salary-plan formulas to those monthly rows.

**What it does:** Extracts `tYearMonth` from `$.changeValue[0].tYearMonth` in `sProInParam`; if missing, returns `sCode=-1` with the localised `sMonthlyStatisticalReportNotChooseMonth` ("未选择凭证月，请选择后操作") from `Fun_Sis_GetConstNew`. `DELETE FROM mftMonthSumReport WHERE sBrandsId=sBrId AND sSubsidiaryId=sSuId AND DATE_FORMAT(tVestingDate,'%Y-%m')=DATE_FORMAT(tYearMonth,'%Y-%m')` to discard the previous run. Then `INSERT INTO mftMonthSumReport SELECT newId(), … SUM(dProcessQty), SUM(dHour), SUM(dQualifiedProductionQty), …, SUM(dAllMoney), NOW(), tVestingDate, sEmployeeId FROM mftDaySumReport WHERE … GROUP BY YYYYMM, sEmployeeId`. Finally back-writes `mftDaySumReport.sMonthSumReportId = B.sId` joining on `(YYYYMM, sEmployeeId, sBrandsId, sSubsidiaryId)` so each day row knows which monthly summary it belongs to.

**Invocation:** Dispatched dynamically by `GenericProcedureCallServiceImpl.doGenericProcedureCall()` (POST `/procedureCall/doGenericProcedureCall`) — a 月度汇总 button on a payroll-style form names `Sp_MonthlyStatisticalReport` as the action. xly-src ships `script/标版/30100101/Sp_MonthlyStatisticalReport.sql` (install script). Successor: `Sp_MonthlyStatisticalSalary` (apply salary plan).
