# Business Logic — JBSbatKKKjhKapZnskIktSikyAdchg.getAplyStdardKjhZnskGengaku() [71 LOC]

| Field | Value |
|-------|-------|
| Fully Qualified Name | `eo.business.service.JBSbatKKKjhKapZnskIktSikyAdchg` |
| Layer | Service |
| Module | `service` (Package: `eo.business.service`) |

## 1. Role

### JBSbatKKKjhKapZnskIktSikyAdchg.getAplyStdardKjhZnskGengaku()

This method retrieves the service contract details needed to determine whether a **standard labor cost remaining balance discount** (標準工事費残債減額) applies to a given service contract in the context of a residence move (住所変更). It acts as a routing dispatcher: based on whether the prior residence had a net service (ネットのサービス), it fetches the net service contract number, otherwise it fetches the telephone service contract number. It then constructs a set of temporal parameters (operation date, last month + end-of-month) and queries the discount service contract table (`KK_T_WRIB_SVC_KEI`) to find matching discount registration records. If no record is found under the primary service contract number, it performs a fallback query using the **first invoice service contract number** (`firstSeikySvcKeiNo`) — a Consumer Protection Act (消費者保護ガイドライン) compliance adjustment added in ticket ANK-4330-00-00. The method implements a **two-phase lookup** design pattern: primary lookup followed by a graceful fallback, ensuring discount eligibility is not missed when a customer's service contract was migrated or billed under a different contract number.

## 2. Processing Pattern (Detailed Business Logic)

```mermaid
flowchart TD
    START(["getAplyStdardKjhZnskGengaku(znskIktSiky, firstSeikySvcKeiNo)"])

    START --> COND1{mNetAriFlg?}

    COND1 -->|true| GET_NET["svcKeiNo = znskIktSiky.get(SVC_CD_IN_SVC).getString(SVC_KEI_NO)"]
    COND1 -->|false| GET_TEL["svcKeiNo = znskIktSiky.get(SVC_CD_TEL_SVC).getString(SVC_KEI_NO)"]

    GET_NET --> BUILD_PARAM
    GET_TEL --> BUILD_PARAM

    BUILD_PARAM["Build setParam array: svcKeiNo, getOpeDate (x3), lastMonth+endOfMonth"]

    BUILD_PARAM --> EXEC_SELECT1["executeKK_T_WRIB_SVC_KEI_KK_SELECT_077(setParam)"]

    EXEC_SELECT1 --> SELECT1["db_KK_T_WRIB_SVC_KEI.selectNext()"]

    SELECT1 --> CHECK_NULL{result == null?}

    CHECK_NULL -->|true| REBUILD["Rebuild setParam: firstSeikySvcKeiNo, getOpeDate (x3), lastMonth+endOfMonth"]
    CHECK_NULL -->|false| RETURN_RESULT

    REBUILD --> EXEC_SELECT2["executeKK_T_WRIB_SVC_KEI_KK_SELECT_079(setParam)"]

    EXEC_SELECT2 --> SELECT2["db_KK_T_WRIB_SVC_KEI.selectNext()"]

    SELECT2 --> RETURN_RESULT["Return result"]

    RETURN_RESULT --> END(["End"])
```

**Branch Descriptions:**

| Branch | Condition | Behavior |
|--------|-----------|----------|
| Branch 1 | `mNetAriFlg == true` | The prior residence had a **net service** (インターネット接続サービス). Extracts the net service contract number from the work execution map using key `SVC_CD_IN_SVC`. |
| Branch 2 | `mNetAriFlg == false` | The prior residence had a **telephone service** (電話サービス). Extracts the telephone service contract number from the work execution map using key `SVC_CD_TEL_SVC`. |
| Branch 3 | Query result is null (primary) | No discount record found under the resolved service contract number. Triggers a **fallback query** using `firstSeikySvcKeiNo` (first invoice service contract number) to comply with Consumer Protection Act guidelines. |
| Branch 4 | Query result is non-null | A matching discount registration record was found in `KK_T_WRIB_SVC_KEI`. Returns the result directly; no fallback query is executed. |

## 3. Parameter Analysis

| No | Parameter Name | Type | Business Description |
|----|---------------|------|---------------------|
| 1 | `znskIktSiky` | `HashMap<String, JBSbatCommonDBInterface>` | Work execution map — a keyed collection of service contract data objects collected during the residence move discount eligibility determination phase (`jdgZnskIktSiky`). The map uses service code keys (`SVC_CD_IN_SVC` for net services, `SVC_CD_TEL_SVC` for telephone services) to retrieve the appropriate `JBSbatCommonDBInterface` record, from which the service contract number (`SVC_KEI_NO`) is extracted. |
| 2 | `firstSeikySvcKeiNo` | `String` | First invoice service contract number — the original service contract number from the first billing request. Used as a fallback contract number when no discount record is found under the primary service contract number. This addition was introduced to comply with Japan's Consumer Protection Act guidelines (ANK-4330-00-00). |

**Instance fields read by this method:**

| Field | Type | Business Description |
|-------|------|---------------------|
| `mNetAriFlg` | `boolean` | Prior residence net availability flag — determines whether the customer had an internet service at the previous address. When `true`, net service contract is used; when `false`, telephone service contract is used. |
| `judgeYmd` | `String` | Judgment reference date — the base date for discount eligibility determination (set during batch invocation, typically the settlement month). Replaces the prior approach of using `getOpeDate()` adjusted by -1 month. |
| `commonItem` | `JBSbatCommonItem` | Common batch parameters — provides the operation date (`getOpeDate()`) used for contract effective date, discount start date, and discount end date. |
| `db_KK_T_WRIB_SVC_KEI` | `JBSbatSQLAccess` | Database access object for the discount service contract table (`KK_T_WRIB_SVC_KEI`). |

## 4. CRUD Operations / Called Services

| CRUD | SC / CBS | SC Code | Entity / DB | Operation Description |
|------|----------|---------|-------------|----------------------|
| R | `executeKK_T_WRIB_SVC_KEI_KK_SELECT_077(setParam)` | KK_SELECT_077 | `KK_T_WRIB_SVC_KEI` | Primary lookup — queries the discount service contract table for records matching the resolved service contract number, operation date, and temporal parameters. |
| R | `db_KK_T_WRIB_SVC_KEI.selectNext()` | - | `KK_T_WRIB_SVC_KEI` | Fetches the next row from the result set of the primary query. Returns null if no matching record exists. |
| R | `executeKK_T_WRIB_SVC_KEI_KK_SELECT_079(setParam)` | KK_SELECT_079 | `KK_T_WRIB_SVC_KEI` | Fallback lookup — queries the discount service contract table using the first invoice service contract number (`firstSeikySvcKeiNo`) when the primary query returns no results. |
| R | `db_KK_T_WRIB_SVC_KEI.selectNext()` | - | `KK_T_WRIB_SVC_KEI` | Fetches the next row from the result set of the fallback query. |
| R | `commonItem.getOpeDate()` | - | - | Retrieves the operation date (business date) used for the contract effective date, discount start date, and discount end date parameters. |
| - | `JBSbatDateUtil.adjustMonth(judgeYmd, -1)` | - | - | Adjusts the judgment reference date backward by 1 month to derive the last month. |
| - | `JBSbatDateUtil.getEndOfMonth(lastMonth)` | - | - | Appends the end-of-day value for the last month (e.g., "28" or "31") to form the last month + end date string. |

## 5. Dependency Trace

### Direct callers of this method:

| # | Caller (Screen/Batch) | Call Chain (Full Path to this Method) | Terminal (SC / CRUD / Entity) |
|---|----------------------|--------------------------------------|-------------------------------|
| 1 | Batch: `JBSbatKKKjhKapZnskIktSikyAdchg.jdgAplyStdardKjhZnskGengaku()` | `JBSbatKKKjhKapZnskIktSikyAdchg.execute()` → `jdgAplyStdardKjhZnskGengaku(znskIktSiky, firstSeikySvcKeiNo)` → `getAplyStdardKjhZnskGengaku(znskIktSiky, firstSeikySvcKeiNo)` | `executeKK_T_WRIB_SVC_KEI_KK_SELECT_077 [R] KK_T_WRIB_SVC_KEI`, `executeKK_T_WRIB_SVC_KEI_KK_SELECT_079 [R] KK_T_WRIB_SVC_KEI` |

**Downstream terminal operations from this method:**
- `executeKK_T_WRIB_SVC_KEI_KK_SELECT_077 [R] KK_T_WRIB_SVC_KEI` — Primary read of discount service contract table
- `executeKK_T_WRIB_SVC_KEI_KK_SELECT_079 [R] KK_T_WRIB_SVC_KEI` — Fallback read of discount service contract table
- `getEndOfMonth [R]` — Date utility (called on `lastMonth`)
- `getOpeDate [R]` — Operation date retrieval (called 3 times for different temporal purposes)

## 6. Per-Branch Detail Blocks

**Block 1** — [IF] `mNetAriFlg` (L1458)

> Determine which service contract number to use based on the prior residence's service type. If the prior residence had a net (internet) service, use the net service contract number; otherwise, use the telephone service contract number.

| # | Type | Code |
|---|------|------|
| 1 | SET | `String svcKeiNo = "";` // Initialize empty contract number variable |
| 2 | IF | `if (mNetAriFlg)` // 【住所変更元ネットあり】(Prior residence had net service) |

**Block 1.1** — [IF-BRANCH: true] Prior residence had a net service (L1460-1462)

> Extract the net service contract number from the work execution map.

| # | Type | Code |
|---|------|------|
| 1 | SET | `svcKeiNo = znskIktSiky.get(JBSbatKKConst.SVC_CD_IN_SVC).getString(JBSbatKK_T_SVC_KEI.SVC_KEI_NO);` // 【ネットのサービス契約番号】(Net service contract number) [-> `SVC_CD_IN_SVC` = net service key] |

**Block 1.2** — [IF-ELSE: false] Prior residence had a telephone service (L1463-1465)

> Extract the telephone service contract number from the work execution map.

| # | Type | Code |
|---|------|------|
| 1 | SET | `svcKeiNo = znskIktSiky.get(JBSbatKKConst.SVC_CD_TEL_SVC).getString(JBSbatKK_T_SVC_KEI.SVC_KEI_NO);` // 【電話のサービス契約番号】(Telephone service contract number) [-> `SVC_CD_TEL_SVC` = telephone service key] |

**Block 2** — [SET] Build parameter array (L1467-1478)

> Construct a 5-element parameter array for the DB query. This prepares temporal context: the service contract number, the operation date (reused for effective date, discount start date, and discount end date), and the last month + end-of-month.

| # | Type | Code |
|---|------|------|
| 1 | SET | `Object[] setParam = new Object[5];` // 【設定値のマップを作成する】(Build parameter map) |
| 2 | SET | `setParam[0] = svcKeiNo;` // 【サービス契約番号】(Service contract number) |
| 3 | SET | `setParam[1] = commonItem.getOpeDate();` // 【予約適用年月日】(Reservation effective date: YYYYMMDD) |
| 4 | SET | `setParam[2] = commonItem.getOpeDate();` // 【割引サービス対象サービス適用開始年月日】(Discount service effective start date: YYYYMMDD) |
| 5 | SET | `setParam[3] = commonItem.getOpeDate();` // 【割引サービス対象サービス適用終了年月日】(Discount service effective end date: YYYYMMDD) |
| 6 | SET | `lastMonth = JBSbatDateUtil.adjustMonth(judgeYmd, -1).substring(0, 6);` // 【判定基準日の前月を取得する】(Get the month prior to the judgment reference date) [OM-2019-0000601 MOD] |
| 7 | SET | `setParam[4] = lastMonth + JBSbatDateUtil.getEndOfMonth(lastMonth);` // 【前月 + 前月末日を設定する】(Set last month + last day of last month, e.g., "20240630") |

**Block 3** — [EXEC] Execute primary DB query (L1481-1483)

> Query the discount service contract table for records matching the constructed parameters, then fetch the result.

| # | Type | Code |
|---|------|------|
| 1 | CALL | `executeKK_T_WRIB_SVC_KEI_KK_SELECT_077(setParam);` // 【DBアクセスを実行する】(Execute DB access) |
| 2 | SET | `JBSbatCommonDBInterface result = db_KK_T_WRIB_SVC_KEI.selectNext();` // 【結果を取得する】(Fetch result) |

**Block 4** — [IF] Result is null (Fallback query) (L1487)

> If no matching record was found under the primary service contract number, perform a fallback query using the first invoice service contract number. This ensures discount eligibility is preserved when the contract number differs between billing cycles (Consumer Protection Act compliance).

| # | Type | Code |
|---|------|------|
| 1 | IF | `if (null == result)` // 【結果が取得できなかった場合】(If result could not be obtained) [ANK-4330-00-00 AND] |

**Block 4.1** — [IF-BRANCH: true] Rebuild params with first invoice contract number (L1489-1503)

> Reconstruct the parameter array substituting the first invoice service contract number, then re-execute the query under the fallback SELECT (KK_SELECT_079).

| # | Type | Code |
|---|------|------|
| 1 | SET | `setParam = new Object[5];` // 【設定値のマップを作成する】(Build parameter map) |
| 2 | SET | `setParam[0] = firstSeikySvcKeiNo;` // 【サービス契約番号（初回請求サービス契約番号）】(Service contract number: first invoice service contract number) |
| 3 | SET | `setParam[1] = commonItem.getOpeDate();` // 【予約適用年月日】(Reservation effective date) |
| 4 | SET | `setParam[2] = commonItem.getOpeDate();` // 【割引サービス対象サービス適用開始年月日】(Discount service effective start date) |
| 5 | SET | `setParam[3] = commonItem.getOpeDate();` // 【割引サービス対象サービス適用終了年月日】(Discount service effective end date) |
| 6 | SET | `setParam[4] = lastMonth + JBSbatDateUtil.getEndOfMonth(lastMonth);` // 【前月 + 前月末日を設定する】(Set last month + last day) |
| 7 | CALL | `executeKK_T_WRIB_SVC_KEI_KK_SELECT_079(setParam);` // 【DBアクセスを実行する】(Execute DB access) |
| 8 | SET | `result = db_KK_T_WRIB_SVC_KEI.selectNext();` // 【結果を取得する】(Fetch result) |

**Block 5** — [RETURN] Return the result (L1505)

> Return the query result — either from the primary lookup, the fallback lookup, or null if neither found a matching discount registration.

| # | Type | Code |
|---|------|------|
| 1 | RETURN | `return result;` // 【サービス契約情報を返却する】(Return service contract information) |

## 7. Glossary

| Term | Type | Business Meaning |
|------|------|------------------|
| `svcKeiNo` | Field | Service contract number — the internal identifier for a service contract line (e.g., internet service, telephone service). Used to query discount registration records. |
| `firstSeikySvcKeiNo` | Field | First invoice service contract number — the original service contract number from the first billing request. Used as a fallback when the primary contract number yields no discount record. |
| `mNetAriFlg` | Field | Prior residence net availability flag — `true` when the customer had an internet connection service at their previous address; `false` otherwise. Controls which service contract number to look up. |
| `judgeYmd` | Field | Judgment reference date — the base date used to determine the discount eligibility month. Typically set to the settlement/batch execution month. Added in OM-2019-0000601. |
| `znskIktSiky` | Parameter | Work execution map — a HashMap keyed by service code, containing `JBSbatCommonDBInterface` objects with service contract data. Collects all service details (net, telephone, TV) from the residence move record. |
| `KK_T_WRIB_SVC_KEI` | Table | Discount service contract table — stores records of service contracts eligible for discounts (割合サービス契約). Contains the discount registration records queried by this method. |
| `KK_SELECT_077` | SQL Key | Primary SELECT query definition — queries `KK_T_WRIB_SVC_KEI` using the resolved service contract number and temporal parameters. |
| `KK_SELECT_079` | SQL Key | Fallback SELECT query definition — queries `KK_T_WRIB_SVC_KEI` using the first invoice service contract number when the primary query returns null. |
| `SVC_CD_IN_SVC` | Constant | Internet service service code key — the HashMap key used to retrieve the net service record from `znskIktSiky`. |
| `SVC_CD_TEL_SVC` | Constant | Telephone service service code key — the HashMap key used to retrieve the telephone service record from `znskIktSiky`. |
| `SVC_KEI_NO` | Constant | Service contract number field identifier — the field name used to extract the contract number from a `JBSbatCommonDBInterface` record. |
| `getOpeDate()` | Method | Operation date getter — returns the batch's business/operation date (YYYYMMDD format) used as the effective date for contracts and discount periods. |
| `adjustMonth()` | Method | Month adjustment utility — shifts a YYYYMMDD date string backward (or forward) by a specified number of months and returns the adjusted string. |
| `getEndOfMonth()` | Method | End-of-month utility — given a YYYYMM string, returns the day-of-month value for the last day (e.g., "30" for June, "31" for July). |
| 標準工事費残債減額 | Business term | Standard labor cost remaining balance discount — a discount applied to the remaining balance of standard installation labor fees when a customer moves (residence change). This method determines whether such a discount applies. |
| 住所変更 | Business term | Residence change (address move) — the business context in which this method operates. The batch processes residence move records to determine discount eligibility for the prior service contracts. |
| 割合サービス契約 | Business term | Discount service contract — a service contract associated with a partial discount (rate-based). Records in `KK_T_WRIB_SVC_KEI` represent these discount registrations. |
| 初回請求 | Business term | First invoice/billing — the initial billing event for a service contract. The `firstSeikySvcKeiNo` refers to the contract number used in this first billing, which may differ from subsequent billing contract numbers. |
| ANK-4330-00-00 | Change ticket | Consumer Protection Act guideline compliance fix — introduced the `firstSeikySvcKeiNo` parameter and fallback query to ensure customers are not denied discounts due to contract number changes across billing cycles. |
| OM-2019-0000601 | Change ticket | Bug fix that changed the last-month calculation from using `getOpeDate()` adjusted by -1 month to using the `judgeYmd` (judgment reference date) instead. |
| `KK_T_SVC_KEI` | Table | Service contract master table — the source table from which individual service contract numbers (net, telephone) are extracted. |
| `JBSbatCommonDBInterface` | Type | Database interface item — a data carrier object that wraps database column values and provides typed getters (e.g., `getString()`, `getInteger()`). |
