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 <sys/types.h>
18 #include <sys/stat.h>
19 #include <fcntl.h>
20 #include <string.h>
21 #include <errno.h>
22 #include <unistd.h>
23
24
25
exportCtx(AB_BANKING * ab,GWEN_DB_NODE * dbArgs,int argc,char ** argv)26 int exportCtx(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 const char *outFile;
32 const char *exporterName;
33 const char *profileName;
34 const char *profileFile;
35 AB_IMEXPORTER_CONTEXT *ctx=0;
36 AB_IMEXPORTER_CONTEXT *nctx=0;
37 AB_IMEXPORTER_ACCOUNTINFO *iea=0;
38 uint32_t aid;
39 const char *bankId;
40 const char *accountId;
41 const char *subAccountId;
42 const char *iban;
43 int transactionType=0;
44 int transactionCommand=0;
45 const char *s;
46 const GWEN_ARGS args[]= {
47 {
48 GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
49 GWEN_ArgsType_Int, /* type */
50 "uniqueAccountId", /* name */
51 0, /* minnum */
52 1, /* maxnum */
53 NULL, /* short option */
54 "aid", /* long option */
55 "Specify the unique account id", /* short description */
56 "Specify the unique account id" /* long description */
57 },
58 {
59 GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
60 GWEN_ArgsType_Char, /* type */
61 "bankId", /* name */
62 0, /* minnum */
63 1, /* maxnum */
64 "b", /* short option */
65 "bank", /* long option */
66 "Specify the bank code", /* short description */
67 "Specify the bank code" /* long description */
68 },
69 {
70 GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
71 GWEN_ArgsType_Char, /* type */
72 "accountId", /* name */
73 0, /* minnum */
74 1, /* maxnum */
75 "a", /* short option */
76 "account", /* long option */
77 "Specify the account number", /* short description */
78 "Specify the account number" /* long description */
79 },
80 {
81 GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
82 GWEN_ArgsType_Char, /* type */
83 "subAccountId", /* name */
84 0, /* minnum */
85 1, /* maxnum */
86 "aa", /* short option */
87 "subaccount", /* long option */
88 "Specify the sub account id (Unterkontomerkmal)", /* short description */
89 "Specify the sub account id (Unterkontomerkmal)" /* long description */
90 },
91 {
92 GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
93 GWEN_ArgsType_Char, /* type */
94 "iban", /* name */
95 0, /* minnum */
96 1, /* maxnum */
97 "A", /* short option */
98 "iban", /* long option */
99 "Specify the iban of your account", /* short description */
100 "Specify the iban of your account" /* long description */
101 },
102 {
103 GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
104 GWEN_ArgsType_Char, /* type */
105 "transactionType", /* name */
106 0, /* minnum */
107 1, /* maxnum */
108 "tt", /* short option */
109 "transactiontype", /* long option */
110 "Specify the transaction type to filter", /* short description */
111 "Specify the transaction type to filter" /* long description */
112 },
113 {
114 GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
115 GWEN_ArgsType_Char, /* type */
116 "transactionCommand", /* name */
117 0, /* minnum */
118 1, /* maxnum */
119 "tc", /* short option */
120 "transactioncommand", /* long option */
121 "Specify the transaction command to filter", /* short description */
122 "Specify the transaction command to filter" /* long description */
123 },
124 {
125 GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
126 GWEN_ArgsType_Char, /* type */
127 "ctxFile", /* name */
128 0, /* minnum */
129 1, /* maxnum */
130 "c", /* short option */
131 "ctxfile", /* long option */
132 "Specify the file to store the context in", /* short description */
133 "Specify the file to store the context in" /* long description */
134 },
135 {
136 GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
137 GWEN_ArgsType_Char, /* type */
138 "outFile", /* name */
139 0, /* minnum */
140 1, /* maxnum */
141 "o", /* short option */
142 "outfile", /* long option */
143 "Specify the file to store the data in", /* short description */
144 "Specify the file to store the data in" /* long description */
145 },
146 {
147 GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
148 GWEN_ArgsType_Char, /* type */
149 "exporterName", /* name */
150 0, /* minnum */
151 1, /* maxnum */
152 0, /* short option */
153 "exporter", /* long option */
154 "Specify the exporter to use", /* short description */
155 "Specify the exporter to use" /* long description */
156 },
157 {
158 GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
159 GWEN_ArgsType_Char, /* type */
160 "profileName", /* name */
161 0, /* minnum */
162 1, /* maxnum */
163 0, /* short option */
164 "profile", /* long option */
165 "Specify the export profile to use", /* short description */
166 "Specify the export profile to use" /* long description */
167 },
168 {
169 GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
170 GWEN_ArgsType_Char, /* type */
171 "profileFile", /* name */
172 0, /* minnum */
173 1, /* maxnum */
174 0, /* short option */
175 "profile-file", /* long option */
176 "Specify the file to load the export profile from",/* short description */
177 "Specify the file to load the export profile from" /* long description */
178 },
179 {
180 GWEN_ARGS_FLAGS_HELP | GWEN_ARGS_FLAGS_LAST, /* flags */
181 GWEN_ArgsType_Int, /* type */
182 "help", /* name */
183 0, /* minnum */
184 0, /* maxnum */
185 "h", /* short option */
186 "help", /* long option */
187 "Show this help screen", /* short description */
188 "Show this help screen" /* long description */
189 }
190 };
191
192 db=GWEN_DB_GetGroup(dbArgs, GWEN_DB_FLAGS_DEFAULT, "local");
193 rv=GWEN_Args_Check(argc, argv, 1,
194 0 /*GWEN_ARGS_MODE_ALLOW_FREEPARAM*/,
195 args,
196 db);
197 if (rv==GWEN_ARGS_RESULT_ERROR) {
198 fprintf(stderr, "ERROR: Could not parse arguments\n");
199 return 1;
200 }
201 else if (rv==GWEN_ARGS_RESULT_HELP) {
202 GWEN_BUFFER *ubuf;
203
204 ubuf=GWEN_Buffer_new(0, 1024, 0, 1);
205 if (GWEN_Args_Usage(args, ubuf, GWEN_ArgsOutType_Txt)) {
206 fprintf(stderr, "ERROR: Could not create help string\n");
207 return 1;
208 }
209 fprintf(stdout, "%s\n", GWEN_Buffer_GetStart(ubuf));
210 GWEN_Buffer_free(ubuf);
211 return 0;
212 }
213
214 /* read command line arguments */
215 exporterName=GWEN_DB_GetCharValue(db, "exporterName", 0, "csv");
216 profileName=GWEN_DB_GetCharValue(db, "profileName", 0, "default");
217 profileFile=GWEN_DB_GetCharValue(db, "profileFile", 0, NULL);
218 aid=(uint32_t)GWEN_DB_GetIntValue(db, "uniqueAccountId", 0, 0);
219 bankId=GWEN_DB_GetCharValue(db, "bankId", 0, 0);
220 accountId=GWEN_DB_GetCharValue(db, "accountId", 0, 0);
221 subAccountId=GWEN_DB_GetCharValue(db, "subAccountId", 0, 0);
222 iban=GWEN_DB_GetCharValue(db, "iban", 0, 0);
223 outFile=GWEN_DB_GetCharValue(db, "outFile", 0, 0);
224
225 s=GWEN_DB_GetCharValue(db, "transactionType", 0, NULL);
226 if (s && *s) {
227 transactionType=AB_Transaction_Type_fromString(s);
228 if (transactionType==AB_Transaction_TypeUnknown) {
229 fprintf(stderr, "ERROR: Invalid transaction type \"%s\"\n", s);
230 return 1;
231 }
232 }
233
234 s=GWEN_DB_GetCharValue(db, "transactionCommand", 0, NULL);
235 if (s && *s) {
236 transactionCommand=AB_Transaction_Command_fromString(s);
237 if (transactionCommand==AB_Transaction_CommandUnknown) {
238 fprintf(stderr, "ERROR: Invalid transaction command \"%s\"\n", s);
239 return 1;
240 }
241 }
242
243 /* init AqBanking */
244 rv=AB_Banking_Init(ab);
245 if (rv) {
246 DBG_ERROR(0, "Error on init (%d)", rv);
247 return 2;
248 }
249
250 /* load ctx file */
251 ctxFile=GWEN_DB_GetCharValue(db, "ctxfile", 0, 0);
252 rv=readContext(ctxFile, &ctx, 1);
253 if (rv!=0) {
254 DBG_ERROR(0, "Error reading context (%d)", rv);
255 AB_ImExporterContext_free(ctx);
256 return 4;
257 }
258
259 /* copy context, but only keep wanted accounts and transactions */
260 nctx=AB_ImExporterContext_new();
261 iea=AB_ImExporterContext_GetFirstAccountInfo(ctx);
262 while (iea) {
263 if (AB_ImExporterAccountInfo_Matches(iea,
264 aid, /* unique account id */
265 "*",
266 bankId,
267 accountId,
268 subAccountId,
269 iban,
270 "*", /* currency */
271 AB_AccountType_Unknown)) {
272 AB_IMEXPORTER_ACCOUNTINFO *nai;
273 AB_TRANSACTION_LIST *tl;
274
275 nai=AB_ImExporterAccountInfo_dup(iea);
276 tl=AB_ImExporterAccountInfo_GetTransactionList(nai);
277 if (tl)
278 AB_Transaction_List_KeepByType(tl, transactionType, transactionCommand);
279
280 AB_ImExporterContext_AddAccountInfo(nctx, nai);
281 }
282
283 iea=AB_ImExporterAccountInfo_List_Next(iea);
284 } /* while */
285 AB_ImExporterContext_free(ctx); /* old context */
286
287 /* export new context */
288 rv=AB_Banking_ExportToFileLoadProfile(ab, exporterName, nctx, outFile, profileName, profileFile);
289 if (rv<0) {
290 DBG_ERROR(0, "Error exporting (%d).", rv);
291 AB_ImExporterContext_free(nctx);
292 return 4;
293 }
294 AB_ImExporterContext_free(nctx);
295
296 /* deinit */
297 rv=AB_Banking_Fini(ab);
298 if (rv) {
299 fprintf(stderr, "ERROR: Error on deinit (%d)\n", rv);
300 return 5;
301 }
302
303 return 0;
304 }
305
306
307
308
309
310
311