# `fun_getNextMonth_lasterWeekday` (function)

> 下个月的最后一周 星期一，星期六


- **Type:** FUNCTION
- **Returns:** `varchar(255)`
- **Deterministic:** NO
- **SQL data access:** CONTAINS SQL

## Parameters

| # | Mode | Name | Type |
|---|---|---|---|
| 1 | IN | `tDate` | `datetime` |
| 2 | IN | `iDay` | `int` |

## Body

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

## Narrative

**Business context:** "Last *iDay*-of-week in the month after `tDate`" calculator — e.g. given any date, return next month's last Monday, last Saturday, etc. Used in scheduled-cut / 自动裁纸 procedures that need to set a recurring monthly reference date.

**What it does:** Adds 1 month to `tDate`, takes `LAST_DAY`, computes that month-end's weekday (`WEEKDAY+1`, Mon=1…Sun=7), then steps back so the result is on the requested `iDay` weekday — falling into the prior week when `iDay` exceeds the month-end's weekday.

**Invocation:** Called by `Sp_Calc_sEmt` — the `sEmt` (estimated material cut / 自动估算) calculation procedure. No other callers found.
