Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

aqbanking / src / tools / aqbanking-cli / listbal.c @ 0bb40bd4

History | View | Annotate | Download (12.1 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/types/balance.h>
18

    
19

    
20

    
21

    
22
static GWEN_DB_NODE *_readCommandLine(GWEN_DB_NODE *dbArgs, int argc, char **argv);
23

    
24

    
25

    
26
int listBal(AB_BANKING *ab, GWEN_DB_NODE *dbArgs, int argc, char **argv)
27
{
28
  GWEN_DB_NODE *db;
29
  int rv;
30
  const char *ctxFile;
31
  AB_IMEXPORTER_CONTEXT *ctx=0;
32
  AB_IMEXPORTER_ACCOUNTINFO *iea=0;
33
  uint32_t aid;
34
  const char *bankId;
35
  const char *accountId;
36
  const char *subAccountId;
37
  const char *iban;
38
  const char *tmplString;
39
  const char *s;
40
  AB_BALANCE_TYPE bt=AB_Balance_TypeBooked;
41

    
42
  /* parse command line arguments */
43
  db=_readCommandLine(dbArgs, argc, argv);
44
  if (db==NULL) {
45
    /* error in command line */
46
    return 1;
47
  }
48

    
49
  /* read command line arguments */
50
  aid=(uint32_t)GWEN_DB_GetIntValue(db, "uniqueAccountId", 0, 0);
51
  bankId=GWEN_DB_GetCharValue(db, "bankId", 0, 0);
52
  accountId=GWEN_DB_GetCharValue(db, "accountId", 0, 0);
53
  subAccountId=GWEN_DB_GetCharValue(db, "subAccountId", 0, 0);
54
  iban=GWEN_DB_GetCharValue(db, "iban", 0, 0);
55
  tmplString=GWEN_DB_GetCharValue(db, "template", 0,
56
                                  "$(dateAsString)\t"
57
                                  "$(valueAsString)\t"
58
                                  "$(iban)");
59

    
60
  /* determine balance type */
61
  s=GWEN_DB_GetCharValue(db, "balanceType", 0, "booked");
62
  if (s && *s) {
63
    AB_BALANCE_TYPE tempBalanceType;
64

    
65
    tempBalanceType=AB_Balance_Type_fromString(s);
66
    if (tempBalanceType==AB_Balance_TypeUnknown) {
67
      DBG_ERROR(0, "Invalid balance type given (%s)", s);
68
      return 1;
69
    }
70
    bt=tempBalanceType;
71
  }
72

    
73
  /* init AqBanking */
74
  rv=AB_Banking_Init(ab);
75
  if (rv) {
76
    DBG_ERROR(0, "Error on init (%d)", rv);
77
    return 2;
78
  }
79

    
80
  /* load ctx file */
81
  ctxFile=GWEN_DB_GetCharValue(db, "ctxfile", 0, 0);
82
  rv=readContext(ctxFile, &ctx, 1);
83
  if (rv<0) {
84
    DBG_ERROR(0, "Error reading context (%d)", rv);
85
    AB_ImExporterContext_free(ctx);
86
    return 4;
87
  }
88

    
89
  /* copy context, but only keep wanted accounts and transactions */
90
  iea=AB_ImExporterContext_GetFirstAccountInfo(ctx);
91
  while (iea) {
92
    if (AB_ImExporterAccountInfo_Matches(iea,
93
                                         aid,  /* unique account id */
94
                                         "*",
95
                                         bankId,
96
                                         accountId,
97
                                         subAccountId,
98
                                         iban,
99
                                         "*", /* currency */
100
                                         AB_AccountType_Unknown)) {
101
      AB_BALANCE *bal;
102
      GWEN_DB_NODE *dbAccount;
103
      const char *s;
104

    
105
      dbAccount=GWEN_DB_Group_new("dbAccount");
106

    
107
      s=AB_ImExporterAccountInfo_GetBankCode(iea);
108
      if (s && *s)
109
        GWEN_DB_SetCharValue(dbAccount, GWEN_DB_FLAGS_OVERWRITE_VARS, "bankCode", s);
110

    
111
      s=AB_ImExporterAccountInfo_GetAccountNumber(iea);
112
      if (s && *s)
113
        GWEN_DB_SetCharValue(dbAccount, GWEN_DB_FLAGS_OVERWRITE_VARS, "accountNumber", s);
114

    
115
      s=AB_ImExporterAccountInfo_GetBic(iea);
116
      if (s && *s)
117
        GWEN_DB_SetCharValue(dbAccount, GWEN_DB_FLAGS_OVERWRITE_VARS, "bic", s);
118

    
119
      s=AB_ImExporterAccountInfo_GetIban(iea);
120
      if (s && *s)
121
        GWEN_DB_SetCharValue(dbAccount, GWEN_DB_FLAGS_OVERWRITE_VARS, "iban", s);
122

    
123
      bal=AB_Balance_List_GetLatestByType(AB_ImExporterAccountInfo_GetBalanceList(iea), bt);
124
      if (bal) {
125
        GWEN_DB_NODE *dbElement;
126
        const AB_VALUE *v;
127
        const GWEN_DATE *dt;
128
        GWEN_BUFFER *dbuf;
129

    
130
        dbElement=GWEN_DB_Group_dup(dbAccount);
131
        AB_Balance_toDb(bal, dbElement);
132

    
133
        /* translate value */
134
        dbuf=GWEN_Buffer_new(0, 256, 0, 1);
135
        v=AB_Balance_GetValue(bal);
136
        if (v) {
137
          AB_Value_toHumanReadableString(v, dbuf, 2, 0);
138
          GWEN_DB_SetCharValue(dbElement, GWEN_DB_FLAGS_OVERWRITE_VARS, "valueAsString", GWEN_Buffer_GetStart(dbuf));
139
          GWEN_Buffer_Reset(dbuf);
140
        }
141

    
142
        /* translate date */
143
        dt=AB_Balance_GetDate(bal);
144
        if (dt) {
145
          rv=GWEN_Date_toStringWithTemplate(dt, I18N("DD.MM.YYYY"), dbuf);
146
          if (rv>=0) {
147
            GWEN_DB_SetCharValue(dbElement, GWEN_DB_FLAGS_OVERWRITE_VARS, "dateAsString", GWEN_Buffer_GetStart(dbuf));
148
          }
149
          GWEN_Buffer_Reset(dbuf);
150
        }
151

    
152
        GWEN_DB_ReplaceVars(dbElement, tmplString, dbuf);
153
        fprintf(stdout, "%s\n", GWEN_Buffer_GetStart(dbuf));
154
        GWEN_Buffer_free(dbuf);
155
        GWEN_DB_Group_free(dbElement);
156
      } /* if bal */
157

    
158
      GWEN_DB_Group_free(dbAccount);
159
    } /* if account matches */
160

    
161
    iea=AB_ImExporterAccountInfo_List_Next(iea);
162
  } /* while */
163
  AB_ImExporterContext_free(ctx);
164

    
165
  /* deinit */
166
  rv=AB_Banking_Fini(ab);
167
  if (rv) {
168
    fprintf(stderr, "ERROR: Error on deinit (%d)\n", rv);
169
    return 5;
170
  }
171

    
172
  return 0;
173
}
174

    
175

    
176

    
177

    
178
/* parse command line */
179
GWEN_DB_NODE *_readCommandLine(GWEN_DB_NODE *dbArgs, int argc, char **argv)
180
{
181
  GWEN_DB_NODE *db;
182
  int rv;
183
  const GWEN_ARGS args[]= {
184
    {
185
      GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
186
      GWEN_ArgsType_Int,            /* type */
187
      "uniqueAccountId",             /* name */
188
      0,                            /* minnum */
189
      1,                            /* maxnum */
190
      NULL,                         /* short option */
191
      "aid",                        /* long option */
192
      "Specify the unique account id",      /* short description */
193
      "Specify the unique account id"       /* long description */
194
    },
195
    {
196
      GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
197
      GWEN_ArgsType_Char,            /* type */
198
      "bankId",                     /* name */
199
      0,                            /* minnum */
200
      1,                            /* maxnum */
201
      "b",                          /* short option */
202
      "bank",                       /* long option */
203
      "Specify the bank code",      /* short description */
204
      "Specify the bank code"       /* long description */
205
    },
206
    {
207
      GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
208
      GWEN_ArgsType_Char,            /* type */
209
      "accountId",                  /* name */
210
      0,                            /* minnum */
211
      1,                            /* maxnum */
212
      "a",                          /* short option */
213
      "account",                    /* long option */
214
      "Specify the account number",     /* short description */
215
      "Specify the account number"      /* long description */
216
    },
217
    {
218
      GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
219
      GWEN_ArgsType_Char,           /* type */
220
      "subAccountId",                /* name */
221
      0,                            /* minnum */
222
      1,                            /* maxnum */
223
      "aa",                          /* short option */
224
      "subaccount",                   /* long option */
225
      "Specify the sub account id (Unterkontomerkmal)",    /* short description */
226
      "Specify the sub account id (Unterkontomerkmal)"     /* long description */
227
    },
228
    {
229
      GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
230
      GWEN_ArgsType_Char,           /* type */
231
      "iban",                       /* name */
232
      0,                            /* minnum */
233
      1,                            /* maxnum */
234
      "A",                          /* short option */
235
      "iban",                    /* long option */
236
      "Specify the iban of your account",      /* short description */
237
      "Specify the iban of your account"       /* long description */
238
    },
239
    {
240
      GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
241
      GWEN_ArgsType_Char,            /* type */
242
      "ctxFile",                    /* name */
243
      0,                            /* minnum */
244
      1,                            /* maxnum */
245
      "c",                          /* short option */
246
      "ctxfile",                    /* long option */
247
      "Specify the file to store the context in",   /* short description */
248
      "Specify the file to store the context in"      /* long description */
249
    },
250
    {
251
      GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
252
      GWEN_ArgsType_Char,            /* type */
253
      "outFile",                    /* name */
254
      0,                            /* minnum */
255
      1,                            /* maxnum */
256
      "o",                          /* short option */
257
      "outfile",                    /* long option */
258
      "Specify the file to store the data in",   /* short description */
259
      "Specify the file to store the data in"      /* long description */
260
    },
261
    {
262
      GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
263
      GWEN_ArgsType_Char,           /* type */
264
      "balanceType",                /* name */
265
      0,                            /* minnum */
266
      1,                            /* maxnum */
267
      "bt",                          /* short option */
268
      "balanceType",                   /* long option */
269
      "Specify the balance type",    /* short description */
270
      "Specify the balance type (e.g. noted, booked, temporary)"     /* long description */
271
    },
272
    {
273
      GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
274
      GWEN_ArgsType_Char,            /* type */
275
      "template",                    /* name */
276
      0,                            /* minnum */
277
      1,                            /* maxnum */
278
      "T",                          /* short option */
279
      "template",                       /* long option */
280
      "Specify the template for the balance list output",      /* short description */
281
      "Specify the template for the balance list output"       /* long description */
282
    },
283
    {
284
      GWEN_ARGS_FLAGS_HELP | GWEN_ARGS_FLAGS_LAST, /* flags */
285
      GWEN_ArgsType_Int,             /* type */
286
      "help",                       /* name */
287
      0,                            /* minnum */
288
      0,                            /* maxnum */
289
      "h",                          /* short option */
290
      "help",                       /* long option */
291
      "Show this help screen",      /* short description */
292
      "Show this help screen"       /* long description */
293
    }
294
  };
295

    
296

    
297
  db=GWEN_DB_GetGroup(dbArgs, GWEN_DB_FLAGS_DEFAULT, "local");
298
  rv=GWEN_Args_Check(argc, argv, 1,
299
                     0 /*GWEN_ARGS_MODE_ALLOW_FREEPARAM*/,
300
                     args,
301
                     db);
302
  if (rv==GWEN_ARGS_RESULT_ERROR) {
303
    fprintf(stderr, "ERROR: Could not parse arguments\n");
304
    return NULL;
305
  }
306
  else if (rv==GWEN_ARGS_RESULT_HELP) {
307
    GWEN_BUFFER *ubuf;
308

    
309
    ubuf=GWEN_Buffer_new(0, 1024, 0, 1);
310
    if (GWEN_Args_Usage(args, ubuf, GWEN_ArgsOutType_Txt)) {
311
      fprintf(stderr, "ERROR: Could not create help string\n");
312
      return NULL;
313
    }
314
    GWEN_Buffer_AppendString(ubuf, "\n");
315
    GWEN_Buffer_AppendString(ubuf, "The template string given to \"-T\" may contain variables to specify the output.\n");
316
    GWEN_Buffer_AppendString(ubuf, "Default is: \"$(dateAsString)\\t$(valueAsString)\\t$(iban)");
317
    GWEN_Buffer_AppendString(ubuf, "Possible variables are:\n");
318
    GWEN_Buffer_AppendString(ubuf, " $(dateAsString)  : Date of the balance in format ");
319
    GWEN_Buffer_AppendString(ubuf, I18N("DD.MM.YYYY"));
320
    GWEN_Buffer_AppendString(ubuf, " \n");
321
    GWEN_Buffer_AppendString(ubuf, " $(valueAsString) : Amount of the balance\n");
322
    GWEN_Buffer_AppendString(ubuf, " $(iban)          : IBAN of the account this balance comes from\n");
323
    GWEN_Buffer_AppendString(ubuf, " $(bic)           : Account number of the account this balance comes from\n");
324
    GWEN_Buffer_AppendString(ubuf, " $(bankcode)      : Bank code (Bankleitzahl) of the account this balance comes from\n");
325
    GWEN_Buffer_AppendString(ubuf, " $(accountnumber) : Account number of the account this balance comes from\n");
326

    
327
    fprintf(stdout, "%s\n", GWEN_Buffer_GetStart(ubuf));
328
    GWEN_Buffer_free(ubuf);
329
    return NULL;
330
  }
331

    
332
  return db;
333
}
334

    
335

    
336

    
337

    
338