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

/* 24. ST_CANCEL - 정산 취소: 상태 CANCELLED + 취소 조정 (dbio). */
void ST_CANCEL(TPSVCINFO *p)
{
    UBFH *b = (UBFH *)p->data;
    char bizdate[16];
    long adj;
    EXEC SQL BEGIN DECLARE SECTION;
    long h_pid, h_net;
    EXEC SQL END DECLARE SECTION;
    h_pid = getl(b, T_PURCHASE_ID);
    gets_(b, T_BIZDATE, bizdate, sizeof(bizdate));
    EXEC SQL SELECT coalesce(sum(net),0) INTO :h_net FROM settlement WHERE purchase_id = :h_pid;
    if (sqlca.sqlcode < 0) FAIL(b);
    EXEC SQL UPDATE settlement SET status = 'CANCELLED' WHERE purchase_id = :h_pid;
    if (sqlca.sqlcode < 0) FAIL(b);
    adj = stdb_insert_adjust(0, h_pid, -h_net, "SETTLE_CANCEL", bizdate);
    if (adj < 0) FAIL(b);
    Bchg(b, T_STATUS, 0, "C", 0L);
    OK(b);
}

/* vim: set ts=4 sw=4 et smartindent: */
