// info  nam="SAMPLE07A"  tit="支払調書作成"  dsc="マイナンバー版支払調書作成"  pro=1

  // データ入力表
typedef struct {
  WORD  ser;
  BYTE  suf;
  BYTE  atr;
  char  zip[12];
  char  adr[64];
  char  nam[64];
  char  kan[64];
  char  tel[16];
  REAL  pin;
  char  acc[32];
  char  det[48];
  int   fee;
  int   unp;
  int   tax;
  int   unc;
  char  rem[64];
  char  mem[64];
  int   reg;
  char  usr[32];
  BYTE  rsv[20];
} HOUD;
typedef HOUD * PHOUD;
typedef const HOUD * PCHOUD;

#define  idHOU  0

PVOID fdefSER(PVOID pv, PCVOID pb, PCCOLI co)
{ // 連番既定値
  WORD max = 0;
  PCHOUD pc, pe;

  for (pc = TablePtr(DBTable(idHOU)), pe = pc + NTableRows(DBTable(idHOU)); pc < pe; pc++) {
    if (pc->ser > max) max = pc->ser;
  }
  *(WORD *)pv = (WORD)(max + 1);
  return pv;
}

PVOID fdefSUF(PVOID pv, PCVOID pb, PCCOLI co)
{ // 枝番既定値
  *(BYTE *)pv = 1; return pv;
}

PCSTR SATR[4] = { "", "個人", "法人", "追加" };
ENMS esATR = { SATR, NELEM(SATR) };

PVOID fdefREG(PVOID pv, PCVOID pb, PCCOLI co)
{ // 登録日既定値
  *(int *)pv = CurDate(); return pv;
}

PVOID fdefUSR(PVOID pv, PCVOID pb, PCCOLI co)
{ // 登録者既定値
  return ColumnScan(pv, CurUser(), co);
}

COLI ciHOU[20] = {
  { TID_U2B,  0, "ser", "連番", "連　番", CF_KEY, fdefSER, },
  { TID_U1B,  0, "suf", "枝番", "枝　番", CF_KEY|CF_PAC, fdefSUF, },
  { TID_E1B,  0, "atr", "属性", "属　性", CF_DDL, NULL, NULL, (UINT)&esATR, },
  { ASC(12),  8, "zip", "郵便番号",     },
  { STR(64),  0, "adr", "住所(所在地)", },
  { STR(64), 25, "nam", "氏名(名称)",   },
  { STR(64), 25, "kan", "ふりがな",     },
  { ASC(16), 12, "tel", "電話番号",     },
  { TID_F8B, 13, "pin", "個人(法人)番号", NULL, CF_HZR|CF_GR2, NULL, NULL, (UINT)"%012.0f", },
  { STR(32), 16, "acc", "区分", "区　分", },
  { STR(48), 20, "det", "細目", "細　目", },
  { TID_M4B, 12, "fee", "支払金額",     NULL, CF_HZR, },
  { TID_M4B, 12, "unp", "未払金額",     NULL, CF_HZR|CF_GR2, },
  { TID_M4B, 12, "tax", "源泉徴収税額", NULL, CF_HZR, },
  { TID_M4B, 12, "unc", "未徴収税額",   NULL, CF_HZR|CF_GR2, },
  { STR(64),  0, "rem", "摘要", "摘　要", },
  { STR(64),  0, "mem", "メモ", "メ　モ", },
  { TID_YMD,  0, "reg", "登録日",       NULL, 0, fdefREG, },
  { STR(32), 16, "usr", "登録者",       NULL, CF_PAC, fdefUSR, },
  { BIN(20),  0, "rsv", "−",           },
};
COLS csHOU = { ciHOU, NELEM(ciHOU) };
VIWI viHOU[4] = {
  { "支払調書(簡略)", 0, "ser, suf, atr, adr, nam, pin, acc, det, fee, tax", "", "" },
  { "支払調書(詳細)", 0, "ser, suf, atr, adr, nam, pin, acc, det, fee, unp, tax, unc, rem", "", "" },
  { "宛名ラベル",     0, "ser, suf, atr, zip, adr, nam", "", "" },
  { "全項目",         0, "ser, suf, atr, zip, adr, nam, kan, tel, pin, acc, det, fee, unp, tax, unc, rem, mem, reg, usr", "", "" },
};
VIWS vsHOU = { viHOU, NELEM(viHOU) };

PCSTR SOPR[6] = { "Open", "Update", "Insert", "Delete", "Print(1)", "Print(2)" };

PCSTR LFILE = "HOULOG.LOG";

void oprlog(int opr, int ser, int suf)
{ // 操作ログ書出
  HANDLE hf;
  char bf[256];

  strcpy(bf, dtformat("yyyy/mm/dd hh:mm:ss ", now()));
  strcat(bf, format("%s %s", SOPR[opr], CurUser()));
  if (opr >= 1 && opr <= 3) strcat(bf, format(" %d-%d", ser, suf));
  hf = open_file(LFILE, OP_APD); if (!hf) return;
  set_file_ptr(hf, 0, FILE_END);
  write_line(hf, bf);
  close_file(hf);
}

BOOL ftrgHOU(HTBL tb, int msg, int ir, PVOID pb)
{ // データ入力表操作履歴
  switch (msg) {
  case TRGM_UPDATERECORD:
  case TRGM_INSERTRECORD:
  case TRGM_DELETERECORD:
    oprlog(msg - TRGM_UPDATERECORD + 1, ((PHOUD)pb)->ser, ((PHOUD)pb)->suf);
    break;
  }
  return DefaultTableTrigger(tb, msg, ir, pb);
}

TBLI tiHOU[1] = {
  { &csHOU, &vsHOU, "HOUDAT",  "データ入力表", NULL, TF_CYP, ftrgHOU, NULL, 0, sizeof(HOUD) },
};
TBLS tsHOU = { tiHOU, NELEM(tiHOU) };

BDBI dbHOU = { &tsHOU, "SAMPLE07A", "支払調書作成", "マイナンバー版支払調書作成" };

BOOL ConnectMain(void)
{ // サンプル
  int re;

  re = ConnectDBX(&dbHOU);
  if (re) NewDBFiles();
  return re;
}
