/*
 * LG_DOUBLE.pgc - lg 모듈 서비스 LG_DOUBLE (one service = one file).
 * 신규 전표 즉시 전기(posted): 차변/대변 + 양 계정잔액 반영.
 * 이전 인라인 lg_svr.pgc 에서 분리; 호출자의 XA 브랜치에서 수행(EXEC SQL CONNECT 없음).
 * lg_svr 가 tpsvrinit 에서 tpadvertise 로 광고한다.
 */
#include "LG_DOUBLE.h"

void LG_DOUBLE(TPSVCINFO *p)
{
    UBFH *b = (UBFH *)p->data;
    char dacct[32], cacct[32], bizdate[16], merch[64];
    long vid, amount;

    gets_(b, T_KEY1, dacct, sizeof(dacct)); if (dacct[0] == 0) strcpy(dacct, "RECV");
    gets_(b, T_KEY2, cacct, sizeof(cacct)); if (cacct[0] == 0) strcpy(cacct, "MERCH_PAY");
    gets_(b, T_BIZDATE, bizdate, sizeof(bizdate));
    gets_(b, T_MERCHANT, merch, sizeof(merch));
    amount = getl(b, T_AMOUNT);

    vid = lgdb_next_voucher_id();
    if (vid < 0) FAIL(b);
    if (lgdb_insert_voucher(vid, "DOUBLE", getl(b, T_PURCHASE_ID), "P", bizdate, "복식전기") < 0) FAIL(b);
    if (lgdb_insert_line(vid, dacct, "D", amount, merch, bizdate, "posted") < 0) FAIL(b);
    if (lgdb_insert_line(vid, cacct, "C", amount, merch, bizdate, "posted") < 0) FAIL(b);
    if (lgdb_upsert_acct_bal(dacct, bizdate, amount, 0) < 0) FAIL(b);
    if (lgdb_upsert_acct_bal(cacct, bizdate, 0, amount) < 0) FAIL(b);

    setl(b, T_ID1, vid);
    Bchg(b, T_STATUS, 0, "P", 0L);
    OK(b);
}

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