/*
 * CM_CHECKDIGIT.pgc - cm 모듈 서비스 CM_CHECKDIGIT (one service = one file).
 * Split out of the former inline cm_svr.pgc; runs on the caller's XA
 * branch (no EXEC SQL CONNECT). Advertised by cm_svr at tpsvrinit.
 */
#include "CM_CHECKDIGIT.h"

/* 25. CM_CHECKDIGIT - Luhn 체크디지트 생성 (순수 계산). */
void CM_CHECKDIGIT(TPSVCINFO *p)
{
    UBFH *b = (UBFH *)p->data;
    char base[32], cand[34];
    int d, found = -1, n;
    gets_(b, T_STR2, base, sizeof(base));
    n = (int)strlen(base);
    if (n == 0 || n >= (int)sizeof(cand) - 1) FAIL(b);
    for (d = 0; d <= 9; d++) {
        strcpy(cand, base);
        cand[n] = (char)('0' + d);
        cand[n + 1] = 0;
        if (acq_luhn_ok(cand)) { found = d; break; }
    }
    if (found < 0) FAIL(b);
    setl(b, T_ARG1, found);
    sets_(b, T_STR4, cand);        /* 체크디지트 포함 완성번호 */
    OK(b);
}
