/*
 * PAY_SPLIT.pgc - py 모듈 서비스 PAY_SPLIT (one service = one file).
 * 지급 분할: 금액을 N건으로 나눠 각각의 지급건(SPLIT 채널)으로 생성.
 * Moved verbatim from the former inline py_svr.pgc; runs on the caller's XA
 * branch (no EXEC SQL CONNECT). Advertised by py_svr at tpsvrinit.
 */
#include "PAY_SPLIT.h"

void PAY_SPLIT(TPSVCINFO *p)
{
    UBFH *b = (UBFH *)p->data;
    char merch[64], acct[32], bizdate[16];
    long n, amount, i, part, payout_id;
    gets_(b, T_MERCHANT, merch, sizeof(merch));
    gets_(b, T_KEY1, acct, sizeof(acct));
    amount = getl(b, T_AMOUNT);
    n = getl(b, T_COUNT); if (n < 1) n = 2;
    gets_(b, T_BIZDATE, bizdate, sizeof(bizdate));
    part = amount / n;
    for (i = 0; i < n; i++) {
        payout_id = pydb_next_payout_id();
        if (payout_id < 0) FAIL(b);
        if (pydb_insert_payout(payout_id, merch, acct, part, 0, part, "R", "SPLIT", bizdate) < 0) FAIL(b);
    }
    setl(b, T_COUNT, n);
    OK(b);
}
