/*
 * ACQ_INSTALL.pgc - ac 모듈 서비스 ACQ_INSTALL (one service = one file).
 * Moved verbatim from the former inline ac_svr.pgc; runs on the caller's XA
 * branch (no EXEC SQL CONNECT). Advertised by ac_svr at tpsvrinit.
 */
#include "ACQ_INSTALL.h"

void ACQ_INSTALL(TPSVCINFO *p)
{
    UBFH *b = (UBFH *)p->data;
    char bizdate[16];
    long months, amount, i;
    EXEC SQL BEGIN DECLARE SECTION;
    long h_id, h_pid, h_seq, h_months, h_mamt;
    char h_bizdate[16];
    EXEC SQL END DECLARE SECTION;

    h_pid = getl(b, T_PURCHASE_ID);
    months = getl(b, T_INSTALL_N); if (months < 1) months = 3;
    amount = getl(b, T_AMOUNT);
    gets_(b, T_BIZDATE, bizdate, sizeof(bizdate));
    strncpy(h_bizdate, bizdate, sizeof(h_bizdate)-1); h_bizdate[sizeof(h_bizdate)-1] = 0;
    h_months = months; h_mamt = amount / months;
    for (i = 1; i <= months; i++) {
        h_seq = i;
        EXEC SQL SELECT nextval('ac_install_seq') INTO :h_id;
        if (sqlca.sqlcode < 0) FAIL(b);
        EXEC SQL INSERT INTO ac_install (install_id, purchase_id, seq_no, months, month_amount, biz_date)
            VALUES (:h_id, :h_pid, :h_seq, :h_months, :h_mamt, :h_bizdate);
        if (sqlca.sqlcode < 0) FAIL(b);
    }
    setl(b, T_COUNT, months);
    OK(b);
}
