Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

aqbanking / src / tools / aqbanking-cli / listbal.c @ f4a16f5b

History | View | Annotate | Download (9.33 KB)

1
/***************************************************************************
2
 begin       : Tue May 03 2005
3
 copyright   : (C) 2018 by Martin Preuss
4
 email       : martin@libchipcard.de
5

6
 ***************************************************************************
7
 *          Please see toplevel file COPYING for license details           *
8
 ***************************************************************************/
9

    
10
#ifdef HAVE_CONFIG_H
11
# include <config.h>
12
#endif
13

    
14
#include "globals.h"
15
#include <gwenhywfar/text.h>
16

    
17
#include <aqbanking/accstatus.h>
18

    
19
#include <stdio.h>
20

    
21
#include <sys/types.h>
22
#include <sys/stat.h>
23
#include <fcntl.h>
24
#include <string.h>
25
#include <errno.h>
26

    
27

    
28

    
29
static void _dumpBal(const AB_BALANCE *bal, FILE *fd) {
30
  if (bal) {
31
    const GWEN_DATE *bdt;
32
    const AB_VALUE *val;
33
  
34
    bdt=AB_Balance_GetDate(bal);
35
    if (bdt) {
36
      GWEN_BUFFER *tbuf;
37

    
38
      tbuf=GWEN_Buffer_new(0, 24, 0, 1);
39
      GWEN_Date_toStringWithTemplate(bdt, "DD.MM.YYYY", tbuf);
40
      fprintf(fd, "%s\t", GWEN_Buffer_GetStart(tbuf));
41
      GWEN_Buffer_free(tbuf);
42
    }
43
    else {
44
      fprintf(fd, "\t\t");
45
    }
46
  
47
    val=AB_Balance_GetValue(bal);
48
    if (val) {
49
      AB_VALUE *vNew;
50
      GWEN_BUFFER *vbuf;
51
      const char *cur;
52

    
53
      vNew=AB_Value_dup(val);
54
      AB_Value_SetCurrency(vNew, NULL);
55
      vbuf=GWEN_Buffer_new(0, 32, 0, 1);
56
      AB_Value_toHumanReadableString(vNew, vbuf, 2);
57
      fprintf(fd, "%s\t", GWEN_Buffer_GetStart(vbuf));
58
      GWEN_Buffer_free(vbuf);
59
      AB_Value_free(vNew);
60

    
61
      cur=AB_Value_GetCurrency(val);
62
      if (cur)
63
        fprintf(fd, "%s\t", cur);
64
      else
65
        fprintf(fd, "\t");
66
    }
67
    else {
68
      fprintf(fd, "\t\t");
69
    }
70
  }
71
  else {
72
    fprintf(fd, "\t\t\t\t");
73
  }
74
}
75

    
76

    
77

    
78
int listBal(AB_BANKING *ab, GWEN_DB_NODE *dbArgs, int argc, char **argv) {
79
  GWEN_DB_NODE *db;
80
  int rv;
81
  const char *ctxFile;
82
  const char *outFile;
83
  AB_IMEXPORTER_CONTEXT *ctx=0;
84
  AB_IMEXPORTER_ACCOUNTINFO *iea=0;
85
  uint32_t aid;
86
  const char *bankId;
87
  const char *accountId;
88
  const char *bankName;
89
  const char *accountName;
90
  FILE *f;
91
  const GWEN_ARGS args[]={
92
  {
93
    GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
94
    GWEN_ArgsType_Int,            /* type */
95
    "uniqueAccountId",             /* name */
96
    0,                            /* minnum */
97
    1,                            /* maxnum */
98
    NULL,                         /* short option */
99
    "aid",                        /* long option */
100
    "Specify the unique account id",      /* short description */
101
    "Specify the unique account id"       /* long description */
102
  },
103
  {
104
    GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
105
    GWEN_ArgsType_Char,            /* type */
106
    "bankId",                     /* name */
107
    0,                            /* minnum */
108
    1,                            /* maxnum */
109
    "b",                          /* short option */
110
    "bank",                       /* long option */
111
    "Specify the bank code",      /* short description */
112
    "Specify the bank code"       /* long description */
113
  },
114
  {
115
    GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
116
    GWEN_ArgsType_Char,            /* type */
117
    "accountId",                  /* name */
118
    0,                            /* minnum */
119
    1,                            /* maxnum */
120
    "a",                          /* short option */
121
    "account",                    /* long option */
122
    "Specify the account number",     /* short description */
123
    "Specify the account number"      /* long description */
124
  },
125
  {
126
    GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
127
    GWEN_ArgsType_Char,            /* type */
128
    "bankName",                   /* name */
129
    0,                            /* minnum */
130
    1,                            /* maxnum */
131
    "N",                          /* short option */
132
    "bankname",                   /* long option */
133
    "Specify the bank name",      /* short description */
134
    "Specify the bank name"       /* long description */
135
  },
136
  {
137
    GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
138
    GWEN_ArgsType_Char,            /* type */
139
    "accountName",                /* name */
140
    0,                            /* minnum */
141
    1,                            /* maxnum */
142
    "n",                          /* short option */
143
    "accountname",                    /* long option */
144
    "Specify the account name",     /* short description */
145
    "Specify the account name"      /* long description */
146
  },
147
  {
148
    GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
149
    GWEN_ArgsType_Char,            /* type */
150
    "ctxFile",                    /* name */
151
    0,                            /* minnum */
152
    1,                            /* maxnum */
153
    "c",                          /* short option */
154
    "ctxfile",                    /* long option */
155
    "Specify the file to store the context in",   /* short description */
156
    "Specify the file to store the context in"      /* long description */
157
  },
158
  {
159
    GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
160
    GWEN_ArgsType_Char,            /* type */
161
    "outFile",                    /* name */
162
    0,                            /* minnum */
163
    1,                            /* maxnum */
164
    "o",                          /* short option */
165
    "outfile",                    /* long option */
166
    "Specify the file to store the data in",   /* short description */
167
    "Specify the file to store the data in"      /* long description */
168
  },
169
  {
170
    GWEN_ARGS_FLAGS_HELP | GWEN_ARGS_FLAGS_LAST, /* flags */
171
    GWEN_ArgsType_Int,             /* type */
172
    "help",                       /* name */
173
    0,                            /* minnum */
174
    0,                            /* maxnum */
175
    "h",                          /* short option */
176
    "help",                       /* long option */
177
    "Show this help screen",      /* short description */
178
    "Show this help screen"       /* long description */
179
  }
180
  };
181

    
182
  db=GWEN_DB_GetGroup(dbArgs, GWEN_DB_FLAGS_DEFAULT, "local");
183
  rv=GWEN_Args_Check(argc, argv, 1,
184
                     0 /*GWEN_ARGS_MODE_ALLOW_FREEPARAM*/,
185
                     args,
186
                     db);
187
  if (rv==GWEN_ARGS_RESULT_ERROR) {
188
    fprintf(stderr, "ERROR: Could not parse arguments\n");
189
    return 1;
190
  }
191
  else if (rv==GWEN_ARGS_RESULT_HELP) {
192
    GWEN_BUFFER *ubuf;
193

    
194
    ubuf=GWEN_Buffer_new(0, 1024, 0, 1);
195
    if (GWEN_Args_Usage(args, ubuf, GWEN_ArgsOutType_Txt)) {
196
      fprintf(stderr, "ERROR: Could not create help string\n");
197
      return 1;
198
    }
199
    fprintf(stderr, "%s\n", GWEN_Buffer_GetStart(ubuf));
200
    GWEN_Buffer_free(ubuf);
201
    return 0;
202
  }
203

    
204
  aid=(uint32_t)GWEN_DB_GetIntValue(db, "uniqueAccountId", 0, 0);
205
  bankId=GWEN_DB_GetCharValue(db, "bankId", 0, 0);
206
  bankName=GWEN_DB_GetCharValue(db, "bankName", 0, 0);
207
  accountId=GWEN_DB_GetCharValue(db, "accountId", 0, 0);
208
  accountName=GWEN_DB_GetCharValue(db, "accountName", 0, 0);
209

    
210
  rv=AB_Banking_Init(ab);
211
  if (rv) {
212
    DBG_ERROR(0, "Error on init (%d)", rv);
213
    return 2;
214
  }
215

    
216
  ctxFile=GWEN_DB_GetCharValue(db, "ctxfile", 0, 0);
217
  rv=readContext(ctxFile, &ctx, 1);
218
  if (rv<0) {
219
    DBG_ERROR(0, "Error reading context (%d)", rv);
220
    return 4;
221
  }
222

    
223
  /* open output stream */
224
  outFile=GWEN_DB_GetCharValue(db, "outFile", 0, 0);
225
  if (outFile==0)
226
    f=stdout;
227
  else
228
    f=fopen(outFile, "w+");
229
  if (f==0) {
230
    DBG_ERROR(0, "Error selecting output file: %s", strerror(errno));
231
    return 4;
232
  }
233

    
234
  iea=AB_ImExporterContext_GetFirstAccountInfo(ctx);
235
  while(iea) {
236
    int matches=1;
237
    const char *s;
238

    
239
    if (matches && bankId) {
240
      s=AB_ImExporterAccountInfo_GetBankCode(iea);
241
      if (!s || !*s || -1==GWEN_Text_ComparePattern(s, bankId, 0))
242
        matches=0;
243
    }
244

    
245
    if (matches && bankName) {
246
      s=AB_ImExporterAccountInfo_GetBankName(iea);
247
      if (!s || !*s)
248
        s=AB_ImExporterAccountInfo_GetBankName(iea);
249
      if (!s || !*s || -1==GWEN_Text_ComparePattern(s, bankName, 0))
250
        matches=0;
251
    }
252

    
253
    if (matches && aid) {
254
      uint32_t id;
255

    
256
      id=AB_ImExporterAccountInfo_GetAccountId(iea);
257
      if (aid!=id)
258
        matches=0;
259
    }
260

    
261
    if (matches && accountId) {
262
      s=AB_ImExporterAccountInfo_GetAccountNumber(iea);
263
      if (!s || !*s || -1==GWEN_Text_ComparePattern(s, accountId, 0))
264
        matches=0;
265
    }
266
    if (matches && accountName) {
267
      s=AB_ImExporterAccountInfo_GetAccountName(iea);
268
      if (!s || !*s)
269
        s=AB_ImExporterAccountInfo_GetAccountName(iea);
270
      if (!s || !*s || -1==GWEN_Text_ComparePattern(s, accountName, 0))
271
        matches=0;
272
    }
273

    
274
    if (matches) {
275
      AB_BALANCE *bal;
276

    
277
      bal=AB_Balance_List_GetLatestByType(AB_ImExporterAccountInfo_GetBalanceList(iea),
278
                                          AB_Balance_TypeBooked);
279
      if (bal) {
280
        const char *s;
281

    
282
        fprintf(f, "Account\t");
283
        s=AB_ImExporterAccountInfo_GetBankCode(iea);
284
        if (!s)
285
          s="";
286
        fprintf(f, "%s\t", s);
287
        s=AB_ImExporterAccountInfo_GetAccountNumber(iea);
288
        if (!s)
289
          s="";
290
        fprintf(f, "%s\t", s);
291
        s=AB_ImExporterAccountInfo_GetBankName(iea);
292
        if (!s)
293
          s="";
294
        fprintf(f, "%s\t", s);
295
        s=AB_ImExporterAccountInfo_GetAccountName(iea);
296
        if (!s)
297
          s="";
298
        fprintf(f, "%s\t", s);
299

    
300
        _dumpBal(bal, f);
301

    
302
        fprintf(f, "\n");
303
      }
304
    } /* if matches */
305
    iea=AB_ImExporterAccountInfo_List_Next(iea);
306
  } /* while */
307

    
308
  if (outFile) {
309
    if (fclose(f)) {
310
      DBG_ERROR(0, "Error closing output file: %s",
311
                strerror(errno));
312
      return 4;
313
    }
314
  }
315

    
316
  rv=AB_Banking_Fini(ab);
317
  if (rv) {
318
    fprintf(stderr, "ERROR: Error on deinit (%d)\n", rv);
319
    return 5;
320
  }
321

    
322
  return 0;
323
}
324

    
325

    
326

    
327

    
328

    
329