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
listBal(AB_BANKING * ab,GWEN_DB_NODE * dbArgs,int argc,char ** argv)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 "$(ibanOrAccountNumber)");
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 s=AB_ImExporterAccountInfo_GetIban(iea);
124 if (!(s && *s))
125 s=AB_ImExporterAccountInfo_GetAccountNumber(iea);
126 if (s && *s)
127 GWEN_DB_SetCharValue(dbAccount, GWEN_DB_FLAGS_OVERWRITE_VARS, "ibanOrAccountNumber", s);
128
129 bal=AB_Balance_List_GetLatestByType(AB_ImExporterAccountInfo_GetBalanceList(iea), bt);
130 if (bal) {
131 GWEN_DB_NODE *dbElement;
132 const AB_VALUE *v;
133 const GWEN_DATE *dt;
134 GWEN_BUFFER *dbuf;
135
136 dbElement=GWEN_DB_Group_dup(dbAccount);
137 AB_Balance_toDb(bal, dbElement);
138
139 /* translate value */
140 dbuf=GWEN_Buffer_new(0, 256, 0, 1);
141 v=AB_Balance_GetValue(bal);
142 if (v) {
143 AB_Value_toHumanReadableString(v, dbuf, 2, 0);
144 GWEN_DB_SetCharValue(dbElement, GWEN_DB_FLAGS_OVERWRITE_VARS, "valueAsString", GWEN_Buffer_GetStart(dbuf));
145 GWEN_Buffer_Reset(dbuf);
146 }
147
148 /* translate date */
149 dt=AB_Balance_GetDate(bal);
150 if (dt) {
151 rv=GWEN_Date_toStringWithTemplate(dt, I18N("DD.MM.YYYY"), dbuf);
152 if (rv>=0) {
153 GWEN_DB_SetCharValue(dbElement, GWEN_DB_FLAGS_OVERWRITE_VARS, "dateAsString", GWEN_Buffer_GetStart(dbuf));
154 }
155 GWEN_Buffer_Reset(dbuf);
156 }
157
158 GWEN_DB_ReplaceVars(dbElement, tmplString, dbuf);
159 fprintf(stdout, "%s\n", GWEN_Buffer_GetStart(dbuf));
160 GWEN_Buffer_free(dbuf);
161 GWEN_DB_Group_free(dbElement);
162 } /* if bal */
163
164 GWEN_DB_Group_free(dbAccount);
165 } /* if account matches */
166
167 iea=AB_ImExporterAccountInfo_List_Next(iea);
168 } /* while */
169 AB_ImExporterContext_free(ctx);
170
171 /* deinit */
172 rv=AB_Banking_Fini(ab);
173 if (rv) {
174 fprintf(stderr, "ERROR: Error on deinit (%d)\n", rv);
175 return 5;
176 }
177
178 return 0;
179 }
180
181
182
183
184 /* parse command line */
_readCommandLine(GWEN_DB_NODE * dbArgs,int argc,char ** argv)185 GWEN_DB_NODE *_readCommandLine(GWEN_DB_NODE *dbArgs, int argc, char **argv)
186 {
187 GWEN_DB_NODE *db;
188 int rv;
189 const GWEN_ARGS args[]= {
190 {
191 GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
192 GWEN_ArgsType_Int, /* type */
193 "uniqueAccountId", /* name */
194 0, /* minnum */
195 1, /* maxnum */
196 NULL, /* short option */
197 "aid", /* long option */
198 "Specify the unique account id", /* short description */
199 "Specify the unique account id" /* long description */
200 },
201 {
202 GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
203 GWEN_ArgsType_Char, /* type */
204 "bankId", /* name */
205 0, /* minnum */
206 1, /* maxnum */
207 "b", /* short option */
208 "bank", /* long option */
209 "Specify the bank code", /* short description */
210 "Specify the bank code" /* long description */
211 },
212 {
213 GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
214 GWEN_ArgsType_Char, /* type */
215 "accountId", /* name */
216 0, /* minnum */
217 1, /* maxnum */
218 "a", /* short option */
219 "account", /* long option */
220 "Specify the account number", /* short description */
221 "Specify the account number" /* long description */
222 },
223 {
224 GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
225 GWEN_ArgsType_Char, /* type */
226 "subAccountId", /* name */
227 0, /* minnum */
228 1, /* maxnum */
229 "aa", /* short option */
230 "subaccount", /* long option */
231 "Specify the sub account id (Unterkontomerkmal)", /* short description */
232 "Specify the sub account id (Unterkontomerkmal)" /* long description */
233 },
234 {
235 GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
236 GWEN_ArgsType_Char, /* type */
237 "iban", /* name */
238 0, /* minnum */
239 1, /* maxnum */
240 "A", /* short option */
241 "iban", /* long option */
242 "Specify the iban of your account", /* short description */
243 "Specify the iban of your account" /* long description */
244 },
245 {
246 GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
247 GWEN_ArgsType_Char, /* type */
248 "ctxFile", /* name */
249 0, /* minnum */
250 1, /* maxnum */
251 "c", /* short option */
252 "ctxfile", /* long option */
253 "Specify the file to store the context in", /* short description */
254 "Specify the file to store the context in" /* long description */
255 },
256 {
257 GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
258 GWEN_ArgsType_Char, /* type */
259 "balanceType", /* name */
260 0, /* minnum */
261 1, /* maxnum */
262 "bt", /* short option */
263 "balanceType", /* long option */
264 "Specify the balance type", /* short description */
265 "Specify the balance type (e.g. noted, booked, temporary)" /* long description */
266 },
267 {
268 GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
269 GWEN_ArgsType_Char, /* type */
270 "template", /* name */
271 0, /* minnum */
272 1, /* maxnum */
273 "T", /* short option */
274 "template", /* long option */
275 "Specify the template for the balance list output", /* short description */
276 "Specify the template for the balance list output" /* long description */
277 },
278 {
279 GWEN_ARGS_FLAGS_HELP | GWEN_ARGS_FLAGS_LAST, /* flags */
280 GWEN_ArgsType_Int, /* type */
281 "help", /* name */
282 0, /* minnum */
283 0, /* maxnum */
284 "h", /* short option */
285 "help", /* long option */
286 "Show this help screen", /* short description */
287 "Show this help screen" /* long description */
288 }
289 };
290
291
292 db=GWEN_DB_GetGroup(dbArgs, GWEN_DB_FLAGS_DEFAULT, "local");
293 rv=GWEN_Args_Check(argc, argv, 1,
294 0 /*GWEN_ARGS_MODE_ALLOW_FREEPARAM*/,
295 args,
296 db);
297 if (rv==GWEN_ARGS_RESULT_ERROR) {
298 fprintf(stderr, "ERROR: Could not parse arguments\n");
299 return NULL;
300 }
301 else if (rv==GWEN_ARGS_RESULT_HELP) {
302 GWEN_BUFFER *ubuf;
303
304 ubuf=GWEN_Buffer_new(0, 1024, 0, 1);
305 if (GWEN_Args_Usage(args, ubuf, GWEN_ArgsOutType_Txt)) {
306 fprintf(stderr, "ERROR: Could not create help string\n");
307 return NULL;
308 }
309 GWEN_Buffer_AppendString(ubuf, "\n");
310 GWEN_Buffer_AppendString(ubuf, "The template string given to \"-T\" may contain variables to specify the output.\n");
311 GWEN_Buffer_AppendString(ubuf, "Default is: \"$(dateAsString)\\t$(valueAsString)\\t$(iban)");
312 GWEN_Buffer_AppendString(ubuf, "Possible variables are:\n");
313 GWEN_Buffer_AppendString(ubuf, " $(dateAsString) : Date of the balance in format ");
314 GWEN_Buffer_AppendString(ubuf, I18N("DD.MM.YYYY"));
315 GWEN_Buffer_AppendString(ubuf, " \n");
316 GWEN_Buffer_AppendString(ubuf, " $(valueAsString) : Amount of the balance\n");
317 GWEN_Buffer_AppendString(ubuf, " $(iban) : IBAN of the account this balance comes from\n");
318 GWEN_Buffer_AppendString(ubuf, " $(bic) : Account number of the account this balance comes from\n");
319 GWEN_Buffer_AppendString(ubuf, " $(bankcode) : Bank code (Bankleitzahl) of the account this balance comes from\n");
320 GWEN_Buffer_AppendString(ubuf, " $(accountnumber) : Account number of the account this balance comes from\n");
321
322 fprintf(stdout, "%s\n", GWEN_Buffer_GetStart(ubuf));
323 GWEN_Buffer_free(ubuf);
324 return NULL;
325 }
326
327 return db;
328 }
329
330
331
332
333
334