# `Sp_Sale_OrderCompleteApply` (procedure)


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

## Parameters

| # | Mode | Name | Type |
|---|---|---|---|
| 1 | IN | `sBrId` | `varchar(100)` |
| 2 | IN | `sSuId` | `varchar(100)` |
| 3 | IN | `sLoginId` | `varchar(100)` |
| 4 | IN | `bFilter` | `longtext` |
| 5 | IN | `pageNum` | `int` |
| 6 | IN | `pageSize` | `int` |
| 7 | OUT | `totalCount` | `int` |
| 8 | IN | `countCloumn` | `longtext` |
| 9 | OUT | `countMapJson` | `longtext` |
| 10 | IN | `sFilterOrderBy` | `longtext` |
| 11 | IN | `sGroupby_select_sql` | `longtext` |
| 12 | IN | `sGroupby_group_sql` | `longtext` |

## Body

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

## Narrative

**Business context:** 销售管理 → 销售订单 — paged report of 销售订单 (sales orders) whose 送货完结 status still needs operator action: rows where `bDeliverComplete=0 AND bInvalid=0 AND bCheck=1`. Used by the 订单完结申请 grid that lets sales/operations apply for "mark this order delivered/complete" against the order-progress flow.

**What it does:** Builds the wide TEMP `p_TmpTb` (customer + product + sales-rep + qty/money/delivery + completion fields), populates it via dynamic SQL `INSERT INTO p_TmpTb … SELECT … FROM Viw_SalSalesOrder WHERE bDeliverComplete=0 AND bInvalid=0 AND bCheck=1 AND brand/sub`. Applies `Fun_GetLookCustomer(sLoginId, sBrId, sSuId)` as a row-level customer filter. Joins (UPDATE … INNER JOIN) to update: 成品入库 `dProductInStoreQty` from `viw_PitProductInStore`, 发外入库 (`viw_opsoutsideinstore`) summed into `dProductStoreQty`, 送货数 `dDeliverGoodsQty` from `viw_SalDeliverGoods`, 库存数 from `eleproductstock`. Hands the final SQL to the standard paging helper for `bFilter`, `sFilterOrderBy`, `sGroupby_*`, `pageNum/pageSize`, `totalCount`, `countMapJson`.

**Invocation:** Status: appears orphaned in this DB snapshot. Signature matches the standard form-master report contract `(sBrId, sSuId, sLoginId, bFilter, pageNum, pageSize, OUT totalCount, countCloumn, OUT countMapJson, sFilterOrderBy, sGroupby_*)` — expected to be bound to a form via `gdsconfigformmaster.sSqlStr`, but no binding exists here. xly-src ships install + optimize scripts (`script/标版/30100101/`, `script/标版/optimize/`). Candidate for maintainer audit — verify which form sources this proc in production.
