Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

aqbanking / src / libs / plugins / backends / aqhbci / ajobs / jobgettransactions.c @ f4a16f5b

History | View | Annotate | Download (19.6 KB)

1
/***************************************************************************
2
    begin       : Mon Mar 01 2004
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

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

    
15

    
16
#include "jobgettransactions_p.h"
17
#include "aqhbci_l.h"
18
#include "accountjob_l.h"
19
#include "job_l.h"
20
#include "user_l.h"
21

    
22
#include <gwenhywfar/debug.h>
23
#include <gwenhywfar/misc.h>
24
#include <gwenhywfar/inherit.h>
25
#include <gwenhywfar/dbio.h>
26
#include <gwenhywfar/gui.h>
27

    
28
#include <gwenhywfar/syncio_memory.h>
29

    
30
#include <stdlib.h>
31
#include <assert.h>
32
#include <string.h>
33
#include <errno.h>
34

    
35

    
36
GWEN_INHERIT(AH_JOB, AH_JOB_GETTRANSACTIONS);
37

    
38

    
39

    
40

    
41
/* --------------------------------------------------------------- FUNCTION */
42
AH_JOB *AH_Job_GetTransactions_new(AB_PROVIDER *pro, AB_USER *u, AB_ACCOUNT *account) {
43
  AH_JOB *j;
44
  AH_JOB_GETTRANSACTIONS *aj;
45
  GWEN_DB_NODE *dbArgs;
46
  GWEN_DB_NODE *updgroup;
47

    
48
  int useCreditCardJob = 0;
49

    
50
  //Check if we should use DKKKU
51
  updgroup=AH_User_GetUpdForAccount(u, account);
52
  if (updgroup) {
53
     GWEN_DB_NODE *n;
54
      n=GWEN_DB_GetFirstGroup(updgroup);
55
      while(n) {
56
        if (strcasecmp(GWEN_DB_GetCharValue(n, "job", 0, ""), "DKKKU")==0) {
57
          useCreditCardJob = 1;
58
          break;
59
        }
60
        n=GWEN_DB_GetNextGroup(n);
61
      } /* while */
62
  } /* if updgroup for the given account found */
63

    
64
  if(useCreditCardJob)
65
    j=AH_AccountJob_new("JobGetTransactionsCreditCard", pro, u, account);
66
  else
67
    j=AH_AccountJob_new("JobGetTransactions", pro, u, account);
68
  if (!j)
69
    return 0;
70

    
71
  GWEN_NEW_OBJECT(AH_JOB_GETTRANSACTIONS, aj);
72
  GWEN_INHERIT_SETDATA(AH_JOB, AH_JOB_GETTRANSACTIONS, j, aj, AH_Job_GetTransactions_FreeData);
73

    
74
  AH_Job_SetSupportedCommand(j, AB_Transaction_CommandGetTransactions);
75

    
76
  /* overwrite some virtual functions */
77
  if(useCreditCardJob)
78
    AH_Job_SetProcessFn(j, AH_Job_GetTransactionsCreditCard_Process);
79
  else
80
    AH_Job_SetProcessFn(j, AH_Job_GetTransactions_Process);
81

    
82
  AH_Job_SetGetLimitsFn(j, AH_Job_GetTransactions_GetLimits);
83
  AH_Job_SetHandleCommandFn(j, AH_Job_GetTransactions_HandleCommand);
84
  AH_Job_SetHandleResultsFn(j, AH_Job_HandleResults_Empty);
85

    
86
  /* set some known arguments */
87
  dbArgs=AH_Job_GetArguments(j);
88
  assert(dbArgs);
89
  if(useCreditCardJob)
90
    GWEN_DB_SetCharValue(dbArgs, GWEN_DB_FLAGS_DEFAULT,
91
                         "accountNumber", AB_Account_GetAccountNumber(account));
92
  else
93
    GWEN_DB_SetCharValue(dbArgs, GWEN_DB_FLAGS_DEFAULT,
94
                       "allAccounts", "N");
95
  return j;
96
}
97

    
98

    
99

    
100
/* --------------------------------------------------------------- FUNCTION */
101
void GWENHYWFAR_CB AH_Job_GetTransactions_FreeData(void *bp, void *p){
102
  AH_JOB_GETTRANSACTIONS *aj;
103

    
104
  aj=(AH_JOB_GETTRANSACTIONS*)p;
105

    
106
  GWEN_FREE_OBJECT(aj);
107
}
108

    
109

    
110

    
111
/* --------------------------------------------------------------- FUNCTION */
112
int AH_Job_GetTransactions__ReadTransactions(AH_JOB *j,
113
                                             AB_IMEXPORTER_ACCOUNTINFO *ai,
114
                                             const char *docType,
115
                                             int noted,
116
                                             GWEN_BUFFER *buf){
117
  GWEN_DBIO *dbio;
118
  GWEN_SYNCIO *sio;
119
  int rv;
120
  GWEN_DB_NODE *db;
121
  GWEN_DB_NODE *dbDay;
122
  GWEN_DB_NODE *dbParams;
123
  AB_ACCOUNT *a;
124
  AB_USER *u;
125
  uint32_t progressId;
126
  uint64_t cnt=0;
127

    
128
  a=AH_AccountJob_GetAccount(j);
129
  assert(a);
130
  u=AH_Job_GetUser(j);
131
  assert(u);
132

    
133
  dbio=GWEN_DBIO_GetPlugin("swift");
134
  if (!dbio) {
135
    DBG_ERROR(AQHBCI_LOGDOMAIN, "Plugin SWIFT is not found");
136
    GWEN_Gui_ProgressLog(0,
137
                         GWEN_LoggerLevel_Error,
138
                         I18N("Plugin \"SWIFT\" not found."));
139
    return AB_ERROR_PLUGIN_MISSING;
140
  }
141

    
142
  GWEN_Buffer_Rewind(buf);
143
  sio=GWEN_SyncIo_Memory_new(buf, 0);
144

    
145
  db=GWEN_DB_Group_new("transactions");
146
  dbParams=GWEN_DB_Group_new("params");
147
  GWEN_DB_SetCharValue(dbParams, GWEN_DB_FLAGS_OVERWRITE_VARS,
148
                       "type", docType);
149
  if (AH_User_GetFlags(u) & AH_USER_FLAGS_KEEP_MULTIPLE_BLANKS)
150
    GWEN_DB_SetIntValue(dbParams, GWEN_DB_FLAGS_OVERWRITE_VARS,
151
                        "keepMultipleBlanks", 1);
152
  else
153
    GWEN_DB_SetIntValue(dbParams, GWEN_DB_FLAGS_OVERWRITE_VARS,
154
                        "keepMultipleBlanks", 0);
155

    
156
  rv=GWEN_DBIO_Import(dbio, sio,
157
                      db, dbParams,
158
                      GWEN_PATH_FLAGS_CREATE_GROUP);
159
  if (rv<0) {
160
    DBG_ERROR(AQHBCI_LOGDOMAIN,
161
              "Error parsing SWIFT %s (%d)",
162
              docType, rv);
163
    GWEN_DB_Group_free(dbParams);
164
    GWEN_DB_Group_free(db);
165
    GWEN_SyncIo_free(sio);
166
    GWEN_DBIO_free(dbio);
167
    return rv;
168
  }
169
  GWEN_DB_Group_free(dbParams);
170
  GWEN_SyncIo_free(sio);
171
  GWEN_DBIO_free(dbio);
172

    
173
  /* first count the groups */
174
  dbDay=GWEN_DB_FindFirstGroup(db, "day");
175
  while(dbDay) {
176
    GWEN_DB_NODE *dbT;
177

    
178
    dbT=GWEN_DB_FindFirstGroup(dbDay, "transaction");
179
    while(dbT) {
180
      cnt++;
181
      dbT=GWEN_DB_FindNextGroup(dbT, "transaction");
182
    } /* while */
183
    dbDay=GWEN_DB_FindNextGroup(dbDay, "day");
184
  } /* while */
185

    
186
  progressId=GWEN_Gui_ProgressStart(GWEN_GUI_PROGRESS_DELAY |
187
                                    GWEN_GUI_PROGRESS_ALLOW_EMBED |
188
                                    GWEN_GUI_PROGRESS_SHOW_PROGRESS |
189
                                    GWEN_GUI_PROGRESS_SHOW_ABORT,
190
                                    I18N("Importing transactions..."),
191
                                    NULL,
192
                                    cnt,
193
                                    0);
194

    
195
  /* add transactions to list */
196
  dbDay=GWEN_DB_FindFirstGroup(db, "day");
197
  while(dbDay) {
198
    GWEN_DB_NODE *dbT;
199

    
200
    dbT=GWEN_DB_FindFirstGroup(dbDay, "transaction");
201
    while(dbT) {
202
      AB_TRANSACTION *t;
203

    
204
      t=AB_Transaction_fromDb(dbT);
205
      if (!t) {
206
        DBG_ERROR(AQHBCI_LOGDOMAIN, "Bad transaction data:");
207
        GWEN_DB_Dump(dbT, 2);
208
      }
209
      else {
210
        const char *s;
211

    
212
        AB_Transaction_SetLocalBankCode(t, AB_User_GetBankCode(u));
213
        AB_Transaction_SetLocalAccountNumber(t, AB_Account_GetAccountNumber(a));
214
        AB_Transaction_SetUniqueAccountId(t, AB_Account_GetUniqueId(a));
215

    
216
        /* some translations */
217
        s=AB_Transaction_GetRemoteIban(t);
218
        if (!(s && *s)) {
219
          const char *sAid;
220

    
221
          /* no remote IBAN set, check whether the bank sends this info in the
222
           * fields for national account specifications (instead of the SWIFT
223
           * field "?38" which was specified for this case) */
224
          sAid=AB_Transaction_GetRemoteAccountNumber(t);
225
          if (sAid && *sAid && AB_Banking_CheckIban(sAid)==0) {
226
            /* there is a remote account number specification, and that is an IBAN,
227
             * so we set that accordingly */
228
            DBG_INFO(AQBANKING_LOGDOMAIN, "Setting remote IBAN from account number");
229
            AB_Transaction_SetRemoteIban(t, sAid);
230

    
231
            /* set remote BIC if it not already is */
232
            s=AB_Transaction_GetRemoteBic(t);
233
            if (!(s && *s)) {
234
              const char *sBid;
235

    
236
              sBid=AB_Transaction_GetRemoteBankCode(t);
237
              if (sBid && *sBid) {
238
                DBG_INFO(AQBANKING_LOGDOMAIN, "Setting remote BIC from bank code");
239
                AB_Transaction_SetRemoteBic(t, sBid);
240
              }
241
            }
242
          }
243
        }
244

    
245
        if (noted)
246
          AB_Transaction_SetType(t, AB_Transaction_TypeNotedStatement);
247
        else
248
          AB_Transaction_SetType(t, AB_Transaction_TypeStatement);
249

    
250
        DBG_INFO(AQHBCI_LOGDOMAIN, "Adding transaction");
251
        AB_ImExporterAccountInfo_AddTransaction(ai, t);
252
      }
253

    
254
      if (GWEN_ERROR_USER_ABORTED==
255
          GWEN_Gui_ProgressAdvance(progressId, GWEN_GUI_PROGRESS_ONE)) {
256
        GWEN_Gui_ProgressEnd(progressId);
257
        return GWEN_ERROR_USER_ABORTED;
258
      }
259

    
260
      dbT=GWEN_DB_FindNextGroup(dbT, "transaction");
261
    } /* while */
262

    
263
    /* read all endsaldos */
264
    if (!noted) {
265
      dbT=GWEN_DB_FindFirstGroup(dbDay, "endSaldo");
266
      while (dbT) {
267
        GWEN_DB_NODE *dbX;
268
        const char *s;
269
        GWEN_DATE *dt=0;
270

    
271
        /* read date */
272
        s=GWEN_DB_GetCharValue(dbT, "date", 0, NULL);
273
        if (s && *s) {
274
          dt=GWEN_Date_fromString(s);
275
          if (dt==NULL) {
276
            DBG_ERROR(AQBANKING_LOGDOMAIN, "Bad date in saldo");
277
          }
278
        }
279

    
280
        /* read value */
281
        dbX=GWEN_DB_GetGroup(dbT, GWEN_PATH_FLAGS_NAMEMUSTEXIST, "value");
282
        if (dbX) {
283
          AB_VALUE *v;
284
  
285
          v=AB_Value_fromDb(dbX);
286
          if (v) {
287
            AB_BALANCE *bal;
288
  
289
            bal=AB_Balance_new();
290
            AB_Balance_SetType(bal, AB_Balance_TypeNoted);
291
            AB_Balance_SetDate(bal, dt);
292
            AB_Balance_SetValue(bal, v);
293
            AB_Value_free(v);
294
            AB_ImExporterAccountInfo_AddBalance(ai, bal);
295
          }
296
        }
297
        GWEN_Date_free(dt);
298
  
299
        dbT=GWEN_DB_FindNextGroup(dbT, "endSaldo");
300
      } /* while */
301
    }
302

    
303
    dbDay=GWEN_DB_FindNextGroup(dbDay, "day");
304
  } /* while */
305

    
306
  GWEN_Gui_ProgressEnd(progressId);
307

    
308
  GWEN_DB_Group_free(db);
309
  return 0;
310
}
311

    
312

    
313

    
314
/* --------------------------------------------------------------- FUNCTION */
315
int AH_Job_GetTransactions_Process(AH_JOB *j, AB_IMEXPORTER_CONTEXT *ctx){
316
  AH_JOB_GETTRANSACTIONS *aj;
317
  AB_ACCOUNT *a;
318
  AB_IMEXPORTER_ACCOUNTINFO *ai;
319
  GWEN_DB_NODE *dbResponses;
320
  GWEN_DB_NODE *dbCurr;
321
  GWEN_BUFFER *tbooked;
322
  GWEN_BUFFER *tnoted;
323
  int rv;
324

    
325
  DBG_INFO(AQHBCI_LOGDOMAIN, "Processing JobGetTransactions");
326

    
327
  assert(j);
328
  aj=GWEN_INHERIT_GETDATA(AH_JOB, AH_JOB_GETTRANSACTIONS, j);
329
  assert(aj);
330

    
331
  tbooked=GWEN_Buffer_new(0, 1024, 0, 1);
332
  tnoted=GWEN_Buffer_new(0, 1024, 0, 1);
333

    
334
  dbResponses=AH_Job_GetResponses(j);
335
  assert(dbResponses);
336

    
337
  /* search for "Transactions" */
338
  dbCurr=GWEN_DB_GetFirstGroup(dbResponses);
339
  while(dbCurr) {
340
    GWEN_DB_NODE *dbXA;
341

    
342
    rv=AH_Job_CheckEncryption(j, dbCurr);
343
    if (rv) {
344
      DBG_INFO(AQHBCI_LOGDOMAIN, "Compromised security (encryption)");
345
      GWEN_Buffer_free(tbooked);
346
      GWEN_Buffer_free(tnoted);
347
      AH_Job_SetStatus(j, AH_JobStatusError);
348
      return rv;
349
    }
350
    rv=AH_Job_CheckSignature(j, dbCurr);
351
    if (rv) {
352
      DBG_INFO(AQHBCI_LOGDOMAIN, "Compromised security (signature)");
353
      GWEN_Buffer_free(tbooked);
354
      GWEN_Buffer_free(tnoted);
355
      AH_Job_SetStatus(j, AH_JobStatusError);
356
      return rv;
357
    }
358

    
359
    dbXA=GWEN_DB_GetGroup(dbCurr, GWEN_PATH_FLAGS_NAMEMUSTEXIST,
360
                          "data/transactions");
361
    if (dbXA) {
362
      const void *p;
363
      unsigned int bs;
364

    
365
      if (GWEN_Logger_GetLevel(0)>=GWEN_LoggerLevel_Debug)
366
        GWEN_DB_Dump(dbXA, 2);
367
      p=GWEN_DB_GetBinValue(dbXA, "booked", 0, 0, 0, &bs);
368
      if (p && bs)
369
        GWEN_Buffer_AppendBytes(tbooked, p, bs);
370
      p=GWEN_DB_GetBinValue(dbXA, "noted", 0, 0, 0, &bs);
371
      if (p && bs)
372
        GWEN_Buffer_AppendBytes(tnoted, p, bs);
373
    } /* if "Transactions" */
374
    dbCurr=GWEN_DB_GetNextGroup(dbCurr);
375
  }
376

    
377
  GWEN_Buffer_Rewind(tbooked);
378
  GWEN_Buffer_Rewind(tnoted);
379

    
380
  /* now the buffers contain data to be parsed by DBIOs */
381
  a=AH_AccountJob_GetAccount(j);
382
  assert(a);
383
  ai=AB_ImExporterContext_GetOrAddAccountInfo(ctx,
384
                                              AB_Account_GetUniqueId(a),
385
                                              AB_Account_GetIban(a),
386
                                              AB_Account_GetBankCode(a),
387
                                              AB_Account_GetAccountNumber(a),
388
                                              AB_Account_GetAccountType(a));
389
  assert(ai);
390

    
391
  /* read booked transactions */
392
  if (GWEN_Buffer_GetUsedBytes(tbooked)) {
393
    if (getenv("AQHBCI_LOGBOOKED")) {
394
      FILE *f;
395

    
396
      f=fopen("/tmp/booked.mt", "w+");
397
      if (f) {
398
        if (fwrite(GWEN_Buffer_GetStart(tbooked),
399
                   GWEN_Buffer_GetUsedBytes(tbooked), 1, f)!=1) {
400
          DBG_ERROR(AQHBCI_LOGDOMAIN, "fwrite: %s", strerror(errno));
401
        }
402
        if (fclose(f)) {
403
          DBG_ERROR(AQHBCI_LOGDOMAIN, "fclose: %s", strerror(errno));
404
        }
405
      }
406
    }
407

    
408
    if (AH_Job_GetTransactions__ReadTransactions(j, ai, "mt940", 0, tbooked)){
409
      GWEN_Buffer_free(tbooked);
410
      GWEN_Buffer_free(tnoted);
411
      DBG_INFO(AQHBCI_LOGDOMAIN, "Error parsing booked transactions");
412
      AH_Job_SetStatus(j, AH_JobStatusError);
413
      return -1;
414
    }
415
  }
416

    
417
  /* read noted transactions */
418
  if (GWEN_Buffer_GetUsedBytes(tnoted)) {
419
    if (getenv("AQHBCI_LOGNOTED")) {
420
      FILE *f;
421

    
422
      f=fopen("/tmp/noted.mt", "w+");
423
      if (f) {
424
        if (fwrite(GWEN_Buffer_GetStart(tnoted),
425
                   GWEN_Buffer_GetUsedBytes(tnoted), 1, f)!=1) {
426
          DBG_ERROR(AQHBCI_LOGDOMAIN, "fwrite: %s", strerror(errno));
427
        }
428
        if (fclose(f)) {
429
          DBG_ERROR(AQHBCI_LOGDOMAIN, "fclose: %s", strerror(errno));
430
        }
431
      }
432
    }
433

    
434
    if (AH_Job_GetTransactions__ReadTransactions(j, ai, "mt942", 1, tnoted)) {
435
      GWEN_Buffer_free(tbooked);
436
      GWEN_Buffer_free(tnoted);
437
      DBG_INFO(AQHBCI_LOGDOMAIN, "Error parsing noted transactions");
438
      AH_Job_SetStatus(j, AH_JobStatusError);
439
      return -1;
440
    }
441
  }
442

    
443
  if (GWEN_Logger_GetLevel(AQHBCI_LOGDOMAIN)>=GWEN_LoggerLevel_Debug) {
444
    GWEN_DB_NODE *gn;
445
    AB_TRANSACTION *ttmp;
446

    
447
    DBG_INFO(AQHBCI_LOGDOMAIN, "*** Dumping transactions *******************");
448
    ttmp=AB_ImExporterAccountInfo_GetFirstTransaction(ai, 0, 0);
449
    while (ttmp) {
450
      DBG_INFO(AQHBCI_LOGDOMAIN, "*** --------------------------------------");
451
      gn=GWEN_DB_Group_new("transaction");
452
      AB_Transaction_toDb(ttmp, gn);
453
      GWEN_DB_Dump(gn, 2);
454
      GWEN_DB_Group_free(gn);
455
      ttmp=AB_Transaction_List_Next(ttmp);
456
    }
457

    
458
    DBG_INFO(AQHBCI_LOGDOMAIN, "*** End dumping transactions ***************");
459
  }
460

    
461
  GWEN_Buffer_free(tbooked);
462
  GWEN_Buffer_free(tnoted);
463
  return 0;
464
}
465

    
466

    
467

    
468
int AH_Job_GetTransactionsCreditCard_Process(AH_JOB *j, AB_IMEXPORTER_CONTEXT *ctx){
469
  AH_JOB_GETTRANSACTIONS *aj;
470
  AB_ACCOUNT *a;
471
  AB_IMEXPORTER_ACCOUNTINFO *ai;
472
  AB_USER *u;
473
  GWEN_DB_NODE *dbResponses;
474
  GWEN_DB_NODE *dbCurr;
475
  GWEN_BUFFER *tbooked;
476
  GWEN_BUFFER *tnoted;
477
  int rv;
478

    
479
  DBG_INFO(AQHBCI_LOGDOMAIN, "Processing JobGetTransactionsCreditCard");
480

    
481
  assert(j);
482
  aj=GWEN_INHERIT_GETDATA(AH_JOB, AH_JOB_GETTRANSACTIONS, j);
483
  assert(aj);
484

    
485
  tbooked=GWEN_Buffer_new(0, 1024, 0, 1);
486
  tnoted=GWEN_Buffer_new(0, 1024, 0, 1);
487

    
488
  dbResponses=AH_Job_GetResponses(j);
489
  assert(dbResponses);
490
  DBG_INFO(AQHBCI_LOGDOMAIN, "Response:");
491
  GWEN_DB_Dump(dbResponses, 2);
492
  DBG_INFO(AQHBCI_LOGDOMAIN, "Response end");
493

    
494

    
495
  a=AH_AccountJob_GetAccount(j);
496
  assert(a);
497
  ai=AB_ImExporterContext_GetOrAddAccountInfo(ctx,
498
                                              AB_Account_GetUniqueId(a),
499
                                              AB_Account_GetIban(a),
500
                                              AB_Account_GetBankCode(a),
501
                                              AB_Account_GetAccountNumber(a),
502
                                              AB_Account_GetAccountType(a));
503
  assert(ai);
504
  AB_ImExporterAccountInfo_SetAccountId(ai, AB_Account_GetUniqueId(a));
505

    
506
  u=AH_Job_GetUser(j);
507
  assert(u);
508

    
509
  /* search for "Transactions" */
510
  dbCurr=GWEN_DB_GetFirstGroup(dbResponses);
511
  while(dbCurr) {
512
    GWEN_DB_NODE *dbXA;
513

    
514
    rv=AH_Job_CheckEncryption(j, dbCurr);
515
    if (rv) {
516
      DBG_INFO(AQHBCI_LOGDOMAIN, "Compromised security (encryption)");
517
      GWEN_Buffer_free(tbooked);
518
      GWEN_Buffer_free(tnoted);
519
      AH_Job_SetStatus(j, AH_JobStatusError);
520
      return rv;
521
    }
522
    rv=AH_Job_CheckSignature(j, dbCurr);
523
    if (rv) {
524
      DBG_INFO(AQHBCI_LOGDOMAIN, "Compromised security (signature)");
525
      GWEN_Buffer_free(tbooked);
526
      GWEN_Buffer_free(tnoted);
527
      AH_Job_SetStatus(j, AH_JobStatusError);
528
      return rv;
529
    }
530

    
531
    dbXA=GWEN_DB_GetGroup(dbCurr, GWEN_PATH_FLAGS_NAMEMUSTEXIST,
532
                          "data/transactionscreditcard");
533
    if (dbXA) {
534
      GWEN_DB_NODE *dbT;
535
      GWEN_DB_NODE *dbV;
536
      GWEN_DATE *date;
537
      GWEN_DATE *valutaDate;
538
      const char *p;
539
      const char *ref;
540
      int i;
541

    
542
      dbT=GWEN_DB_GetGroup(dbXA, GWEN_PATH_FLAGS_NAMEMUSTEXIST,
543
                           "entries");
544
      while (dbT) {
545
        AB_VALUE *v1;
546
        AB_VALUE *v2;
547
        GWEN_STRINGLIST* purpose;
548
        AB_TRANSACTION *t;
549

    
550
        /* read date (Buchungsdatum) */
551
        p=GWEN_DB_GetCharValue(dbT, "date", 0, 0);
552
        if (p)
553
          date=GWEN_Date_fromStringWithTemplate(p, "YYYYMMDD");
554
        else
555
          date=NULL;
556

    
557
        /* read valutaData (Umsatzdatum) */
558
        p=GWEN_DB_GetCharValue(dbT, "valutaDate", 0, 0);
559
        if (p)
560
          valutaDate=GWEN_Date_fromStringWithTemplate(p, "YYYYMMDD");
561
        else
562
          valutaDate=NULL;
563

    
564
        /* read value */
565
        dbV=GWEN_DB_GetGroup(dbT, GWEN_PATH_FLAGS_NAMEMUSTEXIST,
566
                             "value");
567
        if (dbV)
568
          v1=AB_Value_fromDb(dbV);
569
        else
570
          v1=NULL;
571
        v2=0;
572
        if (!v1) {
573
          DBG_ERROR(AQHBCI_LOGDOMAIN, "Error parsing value from DB");
574
        }
575
        else {
576
          p=GWEN_DB_GetCharValue(dbT, "debitMark", 0, 0);
577
          if (p) {
578
            if (strcasecmp(p, "D")==0 ||
579
                strcasecmp(p, "RC")==0) {
580
              v2=AB_Value_dup(v1);
581
              AB_Value_Negate(v2);
582
            }
583
            else if (strcasecmp(p, "C")==0 ||
584
                strcasecmp(p, "RD")==0)
585
              v2=AB_Value_dup(v1);
586
            else {
587
              DBG_ERROR(AQHBCI_LOGDOMAIN, "Bad debit mark \"%s\"", p);
588
              v2=0;
589
            }
590
          }
591
          AB_Value_free(v1);
592
        }
593

    
594
        /* read purpose */
595
        purpose=GWEN_StringList_new();
596
        for (i=0; i<10; i++) {
597
            p=GWEN_DB_GetCharValue(dbT, "purpose", i, 0);
598
            if (!p)
599
                break;
600
            GWEN_StringList_AppendString(purpose, p, 0, 0);
601
        }
602

    
603
        /* read reference */
604
        ref=GWEN_DB_GetCharValue(dbT, "reference", 0, 0);
605
        if (ref)
606
          GWEN_StringList_AppendString(purpose, ref, 0, 0);
607

    
608
        t=AB_Transaction_new();
609
        if(ref)
610
          AB_Transaction_SetFiId(t, ref);
611
        AB_Transaction_SetUniqueAccountId(t, AB_Account_GetUniqueId(a));
612
        AB_Transaction_SetLocalBankCode(t, AB_User_GetBankCode(u));
613
        AB_Transaction_SetLocalAccountNumber(t, AB_Account_GetAccountNumber(a));
614
        AB_Transaction_SetValutaDate(t, valutaDate);
615
        AB_Transaction_SetDate(t, date);
616
        AB_Transaction_SetValue(t, v2);
617
        AB_Transaction_SetPurposeFromStringList(t, purpose);
618
        DBG_INFO(AQHBCI_LOGDOMAIN, "Adding transaction");
619
        AB_ImExporterAccountInfo_AddTransaction(ai, t);
620

    
621
        GWEN_StringList_free(purpose);
622
        AB_Value_free(v2);
623
        GWEN_Date_free(date);
624
        GWEN_Date_free(valutaDate);
625

    
626
        dbT = GWEN_DB_FindNextGroup(dbT, "entries");
627
      } //while (dbT)
628
    } //if (dbXA)
629
    dbCurr=GWEN_DB_GetNextGroup(dbCurr);
630
  }
631

    
632
  return 0;
633
}
634

    
635

    
636

    
637
/* --------------------------------------------------------------- FUNCTION */
638
int AH_Job_GetTransactions_GetLimits(AH_JOB *j, AB_TRANSACTION_LIMITS **pLimits) {
639
  AB_TRANSACTION_LIMITS *tl;
640
  GWEN_DB_NODE *dbParams;
641

    
642
  dbParams=AH_Job_GetParams(j);
643

    
644
  tl=AB_TransactionLimits_new();
645
  AB_TransactionLimits_SetCommand(tl, AH_Job_GetSupportedCommand(j));
646
  AB_TransactionLimits_SetMaxValueSetupTime(tl, GWEN_DB_GetIntValue(dbParams, "storeDays", 0, 0));
647
  /* nothing more to set for this kind of job */
648
  *pLimits=tl;
649
  return 0;
650
}
651

    
652

    
653

    
654
/* --------------------------------------------------------------- FUNCTION */
655
int AH_Job_GetTransactions_HandleCommand(AH_JOB *j, const AB_TRANSACTION *t) {
656
  const GWEN_DATE *da;
657
  
658
  da=AB_Transaction_GetFirstDate(t);
659
  if (da) {
660
    char dbuf[16];
661
    GWEN_DB_NODE *dbArgs;
662
  
663
    dbArgs=AH_Job_GetArguments(j);
664
    snprintf(dbuf, sizeof(dbuf), "%04d%02d%02d",
665
             GWEN_Date_GetYear(da),
666
             GWEN_Date_GetMonth(da),
667
             GWEN_Date_GetDay(da));
668
    GWEN_DB_SetCharValue(dbArgs, GWEN_DB_FLAGS_OVERWRITE_VARS, "fromDate", dbuf);
669
  }
670
  
671
  da=AB_Transaction_GetLastDate(t);
672
  if (da) {
673
    char dbuf[16];
674
    GWEN_DB_NODE *dbArgs;
675
  
676
    dbArgs=AH_Job_GetArguments(j);
677
    snprintf(dbuf, sizeof(dbuf), "%04d%02d%02d",
678
             GWEN_Date_GetYear(da),
679
             GWEN_Date_GetMonth(da),
680
             GWEN_Date_GetDay(da));
681
    GWEN_DB_SetCharValue(dbArgs, GWEN_DB_FLAGS_OVERWRITE_VARS, "toDate", dbuf);
682
  }
683
  
684
  return 0;
685
}
686

    
687

    
688

    
689

    
690

    
691