# `Fun_GetFatherProductId` (function)


- **Type:** FUNCTION
- **Returns:** `longtext`
- **Deterministic:** NO
- **SQL data access:** CONTAINS SQL

## Parameters

| # | Mode | Name | Type |
|---|---|---|---|
| 1 | IN | `sProductId` | `varchar(255)` |
| 2 | IN | `sSrcId` | `varchar(255)` |
| 3 | IN | `iFlag` | `int` |

## Body

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

## Narrative

**Business context:** Walks up the multi-level production BOM tree for a manufactured product (parts → semi-finished → finished). Returns either the top-level product sId (`iFlag=0`) or the matching sales-order line sId for that top-level product (`iFlag=1`). Used by work-order calc to resolve "what finished good does this sub-part belong to".

**What it does:** queries `viw_mftproduct` (BOM view) for the row matching `sProductId`, then climbs through `sFatherSlaveId → sSlaveId` ancestors in a WHILE loop until `sFatherSlaveId` is empty (i.e. reached the root). For `iFlag=0` returns the root `sProductId`. For `iFlag=1`, if `sSrcId` (sales-order master sId) is supplied, looks up the matching `salsalesorderslave` line by `sParentId+sProductId` and returns its sId; otherwise returns the root product sId.

**Invocation:** Status: appears orphaned in the live DB — the only references in xly-src are install scripts (`script/标版/30100101/Sp_Calc_sWod.sql` and `script/标版/optimize/Sp_Calc_sWod.sql`). The shipped `Sp_Calc_sWod` body in the live database does **not** reference this function, suggesting the call was removed from the 生产工单/轮转工单/合版工单 `Sp_Calc_sWod` calc proc after the install scripts were authored. Candidate for maintainer audit.
