/*
 * cl_svc.h - cl (마감/closing) 서비스 공통 카피북 (shared copybook for all cl svc/*.pgc).
 *
 * Every per-service file (svc/<SVCNAME>.pgc) includes its own copybook header
 * (svc/<SVCNAME>.h), which in turn includes THIS shared base. It pulls in the
 * ATMI/UBF runtime, the acq.fd field ids, the cl dbio layer (libcldbio.a) and the
 * common helpers (libacqcommon.a), and provides the tiny UBF helpers + tpreturn
 * macros + the shared 일/월/분기/연 마감 집계 헬퍼(cl_open_close).
 *
 * 마감 상태 O=개시 -> C=마감완료 -> A=승인 -> L=잠금, 취소 X / 재마감 R.
 */
#ifndef CL_SVC_H
#define CL_SVC_H

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <atmi.h>
#include <ubf.h>
#include <userlog.h>
#include <ndebug.h>
#include "acq.fd.h"
#include "cl_dbio.h"
#include "acq_common.h"

/* ----- small UBF helpers (one copy per service TU) ----------------------- */
static long getl(UBFH *b, BFLDID f) { long v = 0; Bget(b, f, 0, (char *)&v, 0L); return v; }
static void gets_(UBFH *b, BFLDID f, char *out, int cap)
{ BFLDLEN l = cap; out[0] = 0; Bget(b, f, 0, out, &l); }
static void setl(UBFH *b, BFLDID f, long v) { Bchg(b, f, 0, (char *)&v, 0L); }

#define FAIL(b) do { tpreturn(TPFAIL, 0, (char *)(b), 0L, 0L); return; } while (0)
#define OK(b)   do { tpreturn(TPSUCCESS, 0, (char *)(b), 0L, 0L); return; } while (0)

/* 공통: 일/월/분기/연 마감 집계+이력생성 래퍼. 실제 EXEC SQL 은 dbio 계층의
 * cldb_open_close() 에 있고(헤더에는 EXEC SQL 금지), 여기서는 순수 C 로 감싸
 * 집계 결과를 UBF(T_COUNT/T_GROSS)에 실어준다. CL_DAILY/MONTHLY/QUARTER/YEAREND 공유. */
static int cl_open_close(UBFH *b, const char *ctype, const char *period,
                         const char *bizdate, long *cid_out)
{
    long cnt = 0, amt = 0;
    if (cldb_open_close(ctype, period, bizdate, cid_out, &cnt, &amt) < 0) return -1;
    setl(b, T_COUNT, cnt);
    setl(b, T_GROSS, amt);
    return 0;
}

#endif /* CL_SVC_H */
