/*
 * INSTALL_AUTH.pgc - au 모듈 서비스 INSTALL_AUTH (one service = one file).
 * 할부 승인: 개월 수만큼 분할 승인 레코드.
 * Moved verbatim from the former inline au_svr.pgc; runs on the caller's XA
 * branch (no EXEC SQL CONNECT). Advertised by au_svr at tpsvrinit.
 */
#include "INSTALL_AUTH.h"

void INSTALL_AUTH(TPSVCINFO *p)
{
    UBFH *b = (UBFH *)p->data;
    char card[24], merch[64], bizdate[16];
    long months, amount, i, auth_id;
    gets_(b, T_KEY1, card, sizeof(card));
    gets_(b, T_MERCHANT, merch, sizeof(merch));
    amount = getl(b, T_AMOUNT);
    months = getl(b, T_INSTALL_N); if (months < 1) months = 3;
    gets_(b, T_BIZDATE, bizdate, sizeof(bizdate));
    for (i = 1; i <= months; i++) {
        auth_id = audb_next_auth_id();
        if (auth_id < 0) FAIL(b);
        if (audb_insert_auth(auth_id, card, merch, amount / months, "INST00", "INSTALL", "A", bizdate) < 0) FAIL(b);
    }
    if (audb_limit_dec(card, amount) < 0) FAIL(b);
    setl(b, T_COUNT, months);
    OK(b);
}
