/*
 * ACQ_FEEADJ.pgc - ac 모듈 서비스 ACQ_FEEADJ (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_FEEADJ.h"

void ACQ_FEEADJ(TPSVCINFO *p)
{
    UBFH *b = (UBFH *)p->data;
    char bizdate[16], reason[128];
    long pid, delta, adj;
    EXEC SQL BEGIN DECLARE SECTION;
    long e_pid, e_delta;
    EXEC SQL END DECLARE SECTION;

    pid = getl(b, T_PURCHASE_ID);
    delta = getl(b, T_DELTA);
    gets_(b, T_BIZDATE, bizdate, sizeof(bizdate));
    gets_(b, T_REASON, reason, sizeof(reason)); if (reason[0] == 0) strcpy(reason, "ADJ");
    adj = acdb_insert_fee_adj(pid, delta, reason, bizdate);
    if (adj < 0) FAIL(b);

    e_pid = pid; e_delta = delta;
    EXEC SQL UPDATE purchase SET fee = fee + :e_delta, net = net - :e_delta, updated_at = now()
        WHERE purchase_id = :e_pid;
    if (sqlca.sqlcode < 0 || sqlca.sqlcode == 100) FAIL(b);
    setl(b, T_ADJ, adj);
    OK(b);
}
