# Business Logic — JBSbatKKIntrCdIkaAdd.createKkifm877() [23 LOC]

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

## 1. Role

### JBSbatKKIntrCdIkaAdd.createKkifm877()

This method is responsible for writing a single row to the referral code batch registration error list output file. When the parent batch process `JBSbatKKIntrCdIkaAdd` processes a batch of referral code registrations, each record that fails validation or encounters a processing error is routed here to be serialized and appended to the error file (file object `kkifm877FileObj`). The method gathers three key data points from the incoming `recordMap` — the system ID, the customer service contract number (referred to as "Customer ID" in the file comments), and the validity expiry date — and pairs them with the error code and error reason extracted from the `errCdEnum` parameter. It implements a simple serializing builder pattern: it constructs an `ArrayList<String>` of field values in the correct output order and delegates the actual file writing to `JKKBatCommon.printDoubleQuoteBusinessFileUtil`. After the row is written, it increments a record counter (`kkifm877RecordCnt`) that is later used for file management tracking (file registration and download management). The method has a single branch — whether `recordMap` is null or not — and a second branch — whether `additionalMessage` is present to enrich the error reason. It does not interact with databases or SC components directly; its sole terminal operation is writing to a business file.

## 2. Processing Pattern (Detailed Business Logic)

```mermaid
flowchart TD
    START(["createKkifm877(params)"])

    START --> INIT["Initialize outputInfo ArrayList"]

    INIT --> COND1{recordMap == null?}

    COND1 -->|Yes| NULL_BRANCH["Add empty strings for SYSID, Customer ID, Validity Expiry Date"]
    COND1 -->|No| VALID_BRANCH["Extract SYSID, SVC_KEI_NO, YK_KIGEN_YMD from recordMap"]

    NULL_BRANCH --> ADD_ERRCODE["Add errCdEnum.toString() as error code"]
    VALID_BRANCH --> ADD_ERRCODE

    ADD_ERRCODE --> ADD_ERRMSG_CHECK{additionalMessage != null?}

    ADD_ERRMSG_CHECK -->|Yes| COMBINED_MSG["Add errCdEnum.message + additionalMessage as error reason"]
    ADD_ERRMSG_CHECK -->|No| ERRONLY_MSG["Add errCdEnum.message as error reason"]

    COMBINED_MSG --> WRITE_FILE["Print outputInfo to error file via printDoubleQuoteBusinessFileUtil"]
    ERRONLY_MSG --> WRITE_FILE

    WRITE_FILE --> INC_CNT["Increment kkifm877RecordCnt"]

    INC_CNT --> END_NODE(["Return / Next"])
```

## 3. Parameter Analysis

| No | Parameter Name | Type | Business Description |
|----|---------------|------|---------------------|
| 1 | `recordMap` | `JBSbatServiceInterfaceMap` | A key-value map carrying the data fields for a single referral code batch registration record. If not null, it provides the system ID (`SYSID`), the customer service contract number (`SVC_KEI_NO`, referred to as "Customer ID" in file output comments), and the validity expiry date (`YK_KIGEN_YMD`, "有効期限年月日" — the date until which the referral code remains valid). If null, empty strings are output for these fields, indicating a malformed or entirely missing record. |
| 2 | `errCdEnum` | `ErrCdEnum` | An error code enum instance representing the specific error that occurred during referral code processing. Its `toString()` method provides the error code string written to the output file, and its `message` field provides the human-readable error reason. This is the primary error classification mechanism used by the batch process. |
| 3 | `additionalMessage` | `String` | An optional supplementary error message. If not null, it is concatenated to the base error message from `errCdEnum.message` to provide additional context about the error. If null, only the base message is written. This allows callers to enrich error output with record-specific details (e.g., the offending data value). |

**Instance fields / external state read:**

| Field | Type | Business Description |
|-------|------|---------------------|
| `kkifm877FileObj` | `JBSbatBusinessFileUtil` | The file output object handle for the referral code batch registration error list file. Created during the batch's initialization phase and closed at cleanup. |
| `kkifm877RecordCnt` | `int` | A counter tracking the total number of error records written to the error file. Initialized to 0 at the start of the batch and incremented after each call to this method. Later used for file management tracking (file registration and download). |

## 4. CRUD Operations / Called Services

| CRUD | SC / CBS | SC Code | Entity / DB | Operation Description |
|------|----------|---------|-------------|----------------------|
| W | `JKKBatCommon.printDoubleQuoteBusinessFileUtil` | - | `KKIFM877` error output file | Serializes the `outputInfo` ArrayList as a double-quoted business file record and appends it to the referral code batch registration error list file. This is the terminal write operation of the method. |
| R | `JBSbatServiceInterfaceMap.getString` | - | In-memory map | Reads field values (`SYSID`, `SVC_KEI_NO`, `YK_KIGEN_YMD`) from the input record map. |

**Classification rationale:**
- `printDoubleQuoteBusinessFileUtil`: This is a **Write (W)** operation because it appends a new record to a batch output file (the error list file, whose file name template is `KKIFM877`). It does not read or query any data.
- `recordMap.getString(...)`: These are **Read (R)** operations because they retrieve values from an in-memory key-value map, not from a database or external storage.

## 5. Dependency Trace

### Pre-computed evidence from code analysis graph:

No screen/batch entry points found within 8 hops. Direct callers found: 2 methods.
Terminal operations from this method: `printDoubleQuoteBusinessFileUtil` [W], `getString` [R], `getString` [R], `getString` [R]

| # | Caller (Screen/Batch) | Call Chain (Full Path to this Method) | Terminal (SC / CRUD / Entity) |
|---|----------------------|--------------------------------------|-------------------------------|
| 1 | Batch: `JBSbatKKIntrCdIkaAdd.execute()` | `JBSbatKKIntrCdIkaAdd.execute()` -> `JBSbatKKIntrCdIkaAdd.createKkifm877(recordMap, errCdEnum, additionalMessage)` | `printDoubleQuoteBusinessFileUtil [W] KKIFM877 error file` |
| 2 | Batch: `JBSbatKKIntrCdIkaAdd.createKkifm877()` (overloaded/self-call) | `JBSbatKKIntrCdIkaAdd.createKkifm877(recordMap, errCdEnum)` -> `JBSbatKKIntrCdIkaAdd.createKkifm877(recordMap, errCdEnum, additionalMessage)` | `printDoubleQuoteBusinessFileUtil [W] KKIFM877 error file` |

**Instructions:**
- Both callers are within the same batch class (`JBSbatKKIntrCdIkaAdd`).
- The `execute()` method is the batch entry point that iterates over referral code registration records, invokes validation via `checkMain()`, and routes errors to `createKkifm877()` for error file output.
- The overloaded `createKkifm877()` method (with 2 params) delegates to this 3-param version, passing null for `additionalMessage`.

## 6. Per-Branch Detail Blocks

**Block 1** — [NEW] `(initialize outputInfo)` (L769)

Initialize a new `ArrayList<String>` to hold the output fields for the error record.

| # | Type | Code |
|---|------|------|
| 1 | SET | `outputInfo = new ArrayList<String>()` |

**Block 2** — [IF] `(recordMap == null)` (L770)

Conditional branch: if the incoming `recordMap` is null, output empty strings for all three data fields. This handles the case of a completely malformed or missing record.

> **Block 2.1** — [ELSE-BRANCH: null] `recordMap == null` (L771-773)

All three fields are set to empty strings, preserving the column positions in the output file.

| # | Type | Code |
|---|------|------|
| 1 | SET | `outputInfo.add("")` // SYSID (SISID — システムID) |
| 2 | SET | `outputInfo.add("")` // Customer ID (お客様ID) |
| 3 | SET | `outputInfo.add("")` // Validity Expiry Date (有効期限年月日) |

> **Block 2.2** — [ELSE-BRANCH: not null] `recordMap != null` (L774-776)

Extract field values from `recordMap` using the constants defined in `JBSbatKKIFM875`:

- `JBSbatKKIFM875.SYSID` = `"SYSID"` — the system ID identifying which subsystem generated this record.
- `JBSbatKKIFM875.SVC_KEI_NO` = `"SVC_KEI_NO"` — the service contract number (service detail number), referred to as "Customer ID" in the file comments.
- `JBSbatKKIFM875.YK_KIGEN_YMD` = `"YK_KIGEN_YMD"` — the validity expiry date (year-month-day format).

| # | Type | Code |
|---|------|------|
| 1 | CALL | `outputInfo.add(recordMap.getString(JBSbatKKIFM875.SYSID))` // SYSID [-> SYSID="SYSID"] |
| 2 | CALL | `outputInfo.add(recordMap.getString(JBSbatKKIFM875.SVC_KEI_NO))` // Customer ID [-> SVC_KEI_NO="SVC_KEI_NO"] |
| 3 | CALL | `outputInfo.add(recordMap.getString(JBSbatKKIFM875.YK_KIGEN_YMD))` // Validity Expiry Date [-> YK_KIGEN_YMD="YK_KIGEN_YMD"] |

**Block 3** — [EXEC] `(add error code)` (L777)

Append the error code from the enum to the output list.

| # | Type | Code |
|---|------|------|
| 1 | CALL | `outputInfo.add(errCdEnum.toString())` // Error Code (エラーコード) |

**Block 4** — [IF] `(additionalMessage != null)` (L778)

Conditional branch: if an additional error message is provided, concatenate it to the base error message. This allows callers to append record-specific details (e.g., the specific value that caused the error) to the error reason field.

> **Block 4.1** — [IF-BRANCH: not null] `additionalMessage != null` (L779)

Concatenate the base message from the error enum with the additional message.

| # | Type | Code |
|---|------|------|
| 1 | CALL | `outputInfo.add(errCdEnum.message + additionalMessage)` // Error Reason (エラー理由) — combined |

> **Block 4.2** — [ELSE-BRANCH: null] `additionalMessage == null` (L780)

Write only the base error message from the enum, without any additional context.

| # | Type | Code |
|---|------|------|
| 1 | CALL | `outputInfo.add(errCdEnum.message)` // Error Reason (エラー理由) — base message only |

**Block 5** — [EXEC] `(write error file row)` (L782)

Delegate to the shared business file utility to write the constructed row as a double-quoted CSV line to the error file.

| # | Type | Code |
|---|------|------|
| 1 | CALL | `JKKBatCommon.printDoubleQuoteBusinessFileUtil(kkifm877FileObj, outputInfo)` // Write error record to KKIFM877 file |

**Block 6** — [EXEC] `(increment record counter)` (L784)

Increment the error record counter that tracks the total number of error lines written to the file.

| # | Type | Code |
|---|------|------|
| 1 | SET | `kkifm877RecordCnt++` // Increment error record count |

## 7. Glossary

| Term | Type | Business Meaning |
|------|------|------------------|
| `SYSID` | Field | System ID — identifies the originating system or subsystem for the referral code registration record. |
| `SVC_KEI_NO` | Field | Service Contract Number — the service detail number (サービス契約番号), used in file output comments as "Customer ID" (お客様ID). Represents the customer's service contract line item identifier. |
| `YK_KIGEN_YMD` | Field | Validity Expiry Date (有効期限年月日) — the date (year-month-day format) until which the referral code remains valid. |
| `errCdEnum` | Parameter | Error Code Enum — an enumeration instance containing both the machine-readable error code (`toString()`) and the human-readable error reason (`message` field). |
| `kkifm877FileObj` | Field | Error File Object — a `JBSbatBusinessFileUtil` instance managing the output stream for the referral code batch registration error list file (file name template: `KKIFM877`). |
| `kkifm877RecordCnt` | Field | Error Record Counter — tracks the total number of error records written to the KKIFM877 error file. Used for file management tracking (file registration and download management). |
| `recordMap` | Parameter | Record Map (`JBSbatServiceInterfaceMap`) — a key-value map carrying the data fields for a single referral code batch registration record. Used to extract SYSID, service contract number, and validity expiry date. |
| `additionalMessage` | Parameter | Additional Error Message — an optional string appended to the base error message for richer error context. |
| KKIFM877 | File | Referral Code Batch Registration Error List File (紹介コード一括登録エラーリストファイル) — the output file where failed referral code registration records are written with their associated error codes and reasons. |
| JBSbatKKIntrCdIkaAdd | Class | Referral Code Batch Registration Service (紹介コード一括登録サービス) — the batch service class that processes bulk referral code registrations, including validation, execution, and error file output. |
| `printDoubleQuoteBusinessFileUtil` | Method | Business File Writer — a shared utility method in `JKKBatCommon` that writes a list of strings as a double-quoted CSV line to a business file. |
| 紹介コード (Shoukai Code) | Japanese term | Referral Code — a code used in marketing campaigns to track referrals from one customer to another. This batch process handles bulk registration of such codes. |
| 一覧登録 (Ichiiri Touroku) | Japanese term | Batch Registration — bulk processing of multiple records at once, as opposed to record-by-record manual entry. |
| エラーリスト (Era Risuto) | Japanese term | Error List — the output file containing records that failed during batch processing, along with their error codes and reasons. |
