Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

aqbanking / src / libs / plugins / imexporters / swift / swift.c @ f4a16f5b

History | View | Annotate | Download (8.77 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
#ifdef HAVE_CONFIG_H
11
# include <config.h>
12
#endif
13

    
14
#include "swift_p.h"
15
#include "i18n_l.h"
16

    
17
#include <aqbanking/banking.h>
18
#include <aqbanking/accstatus.h>
19

    
20
#include <gwenhywfar/debug.h>
21
#include <gwenhywfar/misc.h>
22
#include <gwenhywfar/gui.h>
23
#include <gwenhywfar/inherit.h>
24

    
25

    
26

    
27
GWEN_INHERIT(AB_IMEXPORTER, AH_IMEXPORTER_SWIFT);
28

    
29

    
30

    
31
AB_IMEXPORTER *AB_ImExporterSWIFT_new(AB_BANKING *ab){
32
  AB_IMEXPORTER *ie;
33
  AH_IMEXPORTER_SWIFT *ieh;
34

    
35
  ie=AB_ImExporter_new(ab, "swift");
36
  GWEN_NEW_OBJECT(AH_IMEXPORTER_SWIFT, ieh);
37
  GWEN_INHERIT_SETDATA(AB_IMEXPORTER, AH_IMEXPORTER_SWIFT, ie, ieh,
38
                       AH_ImExporterSWIFT_FreeData);
39
  ieh->dbio=GWEN_DBIO_GetPlugin("swift");
40
  if (!ieh->dbio) {
41
    DBG_ERROR(AQBANKING_LOGDOMAIN,
42
              "GWEN DBIO plugin \"SWIFT\" not available");
43
    AB_ImExporter_free(ie);
44
    return 0;
45
  }
46

    
47
  AB_ImExporter_SetImportFn(ie, AH_ImExporterSWIFT_Import);
48
  AB_ImExporter_SetCheckFileFn(ie, AH_ImExporterSWIFT_CheckFile);
49
  return ie;
50
}
51

    
52

    
53

    
54
void GWENHYWFAR_CB AH_ImExporterSWIFT_FreeData(void *bp, void *p){
55
  AH_IMEXPORTER_SWIFT *ieh;
56

    
57
  ieh=(AH_IMEXPORTER_SWIFT*)p;
58
  GWEN_DBIO_free(ieh->dbio);
59
  GWEN_FREE_OBJECT(ieh);
60
}
61

    
62

    
63

    
64
int AH_ImExporterSWIFT_Import(AB_IMEXPORTER *ie,
65
                              AB_IMEXPORTER_CONTEXT *ctx,
66
                              GWEN_SYNCIO *sio,
67
                              GWEN_DB_NODE *params){
68
  AH_IMEXPORTER_SWIFT *ieh;
69
  GWEN_DB_NODE *dbData;
70
  GWEN_DB_NODE *dbSubParams;
71
  int rv;
72

    
73
  assert(ie);
74
  ieh=GWEN_INHERIT_GETDATA(AB_IMEXPORTER, AH_IMEXPORTER_SWIFT, ie);
75
  assert(ieh);
76
  assert(ieh->dbio);
77

    
78
  dbSubParams=GWEN_DB_GetGroup(params, GWEN_PATH_FLAGS_NAMEMUSTEXIST,
79
                               "params");
80
  dbData=GWEN_DB_Group_new("transactions");
81
  GWEN_Gui_ProgressLog(0, GWEN_LoggerLevel_Debug,
82
                       I18N("Reading file..."));
83

    
84
  rv=GWEN_DBIO_Import(ieh->dbio,
85
                      sio,
86
                      dbData,
87
                      dbSubParams,
88
                      GWEN_DB_FLAGS_DEFAULT |
89
                      GWEN_PATH_FLAGS_CREATE_GROUP);
90
  if (rv) {
91
    DBG_ERROR(AQBANKING_LOGDOMAIN, "Error importing data (%d)", rv);
92
    GWEN_Gui_ProgressLog(0, GWEN_LoggerLevel_Error,
93
                         I18N("Error importing data"));
94
    GWEN_DB_Group_free(dbData);
95
    return GWEN_ERROR_BAD_DATA;
96
  }
97

    
98
  /* transform DB to transactions */
99
  GWEN_Gui_ProgressLog(0, GWEN_LoggerLevel_Debug,
100
                       "Data imported, transforming to transactions");
101
  rv=AH_ImExporterSWIFT__ImportFromGroup(ctx, dbData, params);
102
  if (rv) {
103
    GWEN_Gui_ProgressLog(0, GWEN_LoggerLevel_Error,
104
                         "Error importing data");
105
    GWEN_DB_Group_free(dbData);
106
    return rv;
107
  }
108

    
109
  GWEN_DB_Group_free(dbData);
110
  return 0;
111
}
112

    
113

    
114

    
115
int AH_ImExporterSWIFT__ImportFromGroup(AB_IMEXPORTER_CONTEXT *ctx,
116
                                        GWEN_DB_NODE *db,
117
                                        GWEN_DB_NODE *dbParams) {
118
  GWEN_DB_NODE *dbT;
119
  uint32_t progressId;
120

    
121
  progressId=GWEN_Gui_ProgressStart(GWEN_GUI_PROGRESS_DELAY |
122
                                    GWEN_GUI_PROGRESS_ALLOW_EMBED |
123
                                    GWEN_GUI_PROGRESS_SHOW_PROGRESS |
124
                                    GWEN_GUI_PROGRESS_SHOW_ABORT,
125
                                    I18N("Importing parsed data..."),
126
                                    NULL,
127
                                    GWEN_DB_Groups_Count(db),
128
                                    0);
129
  dbT=GWEN_DB_GetFirstGroup(db);
130
  while(dbT) {
131
    int matches;
132
    int i;
133
    const char *p;
134
    const char *gn;
135

    
136
    // check whether the name of the current groups matches
137
    matches=0;
138
    gn=GWEN_DB_GroupName(dbT);
139
    for (i=0; ; i++) {
140
      p=GWEN_DB_GetCharValue(dbParams, "groupNames", i, 0);
141
      if (!p)
142
        break;
143
      if (strcasecmp(gn, p)==0) {
144
        matches=1;
145
        break;
146
      }
147
    } // for
148

    
149
    if (!matches && i==0) {
150
      // no names given, check default
151
      if ((strcasecmp(GWEN_DB_GroupName(dbT), "transaction")==0) ||
152
          (strcasecmp(GWEN_DB_GroupName(dbT), "debitnote")==0))
153
        matches=1;
154
    }
155

    
156
    if (matches) {
157
      AB_TRANSACTION *t;
158
      const char *s;
159

    
160
      t=AB_Transaction_fromDb(dbT);
161
      if (!t) {
162
        DBG_ERROR(AQBANKING_LOGDOMAIN, "Error in config file");
163
        GWEN_Gui_ProgressLog(0, GWEN_LoggerLevel_Error,
164
                             I18N("Error in config file"));
165
        return GWEN_ERROR_GENERIC;
166
      }
167

    
168
      /* some translations */
169
      s=AB_Transaction_GetRemoteIban(t);
170
      if (!(s && *s)) {
171
        const char *sAid;
172

    
173
        /* no remote IBAN set, check whether the bank sends this info in the
174
         * fields for national account specifications (instead of the SWIFT
175
         * field "?38" which was specified for this case) */
176
        sAid=AB_Transaction_GetRemoteAccountNumber(t);
177
        if (sAid && *sAid && AB_Banking_CheckIban(sAid)==0) {
178
          /* there is a remote account number specification, and that is an IBAN,
179
           * so we set that accordingly */
180
          DBG_INFO(AQBANKING_LOGDOMAIN, "Setting remote IBAN from account number");
181
          AB_Transaction_SetRemoteIban(t, sAid);
182

    
183
          /* set remote BIC if it not already is */
184
          s=AB_Transaction_GetRemoteBic(t);
185
          if (!(s && *s)) {
186
            const char *sBid;
187

    
188
            sBid=AB_Transaction_GetRemoteBankCode(t);
189
            if (sBid && *sBid) {
190
              DBG_INFO(AQBANKING_LOGDOMAIN, "Setting remote BIC from bank code");
191
              AB_Transaction_SetRemoteBic(t, sBid);
192
            }
193
          }
194
        }
195
      }
196

    
197
      /* ABWA+: replace remote name with ABWA+ content */
198
      s=GWEN_DB_GetCharValue(dbT, "sepa/ABWA", 0, NULL);
199
      if (s && *s) {
200
        int i;
201

    
202
        //DBG_ERROR(AQBANKING_LOGDOMAIN, "Replacing remote name with ABWA value [%s]", s);
203
        for (i=0; i<2; i++) {
204
          s=GWEN_DB_GetCharValue(dbT, "sepa/ABWA", i, NULL);
205
          if (s && *s) {
206
            AB_Transaction_SetRemoteName(t, s);
207
          }
208
        }
209
      }
210

    
211
      /* ABWE+: replace local name with ABWE+ content */
212
      s=GWEN_DB_GetCharValue(dbT, "sepa/ABWE", 0, NULL);
213
      if (s && *s) {
214
        AB_Transaction_SetLocalName(t, s);
215
      }
216

    
217

    
218
      /* add transaction */
219
      DBG_DEBUG(AQBANKING_LOGDOMAIN, "Adding transaction");
220
      GWEN_Gui_ProgressLog(0, GWEN_LoggerLevel_Debug, I18N("Adding transaction"));
221
      AB_ImExporterContext_AddTransaction(ctx, t);
222
    }
223
    else if (strcasecmp(GWEN_DB_GroupName(dbT), "startSaldo")==0) {
224
      /* ignore start saldo, but since the existence of this group shows
225
       * that we in fact are within a swift DB group we don't need recursions.
226
       */
227
    }
228
    else if (strcasecmp(GWEN_DB_GroupName(dbT), "endSaldo")==0) {
229
      GWEN_DB_NODE *dbX;
230
      GWEN_DATE *dt=0;
231
      const char *s;
232
      const char *bankCode;
233
      const char *accountNumber;
234
      const char *iban;
235

    
236
      bankCode=GWEN_DB_GetCharValue(dbT, "localBankCode", 0, 0);
237
      accountNumber=GWEN_DB_GetCharValue(dbT, "localAccountNumber", 0, 0);
238
      iban=GWEN_DB_GetCharValue(dbT, "localIban", 0, 0);
239

    
240
      /* read date */
241
      s=GWEN_DB_GetCharValue(dbT, "date", 0, NULL);
242
      if (s && *s) {
243
        dt=GWEN_Date_fromString(s);
244
        if (dt==NULL) {
245
          DBG_ERROR(AQBANKING_LOGDOMAIN, "Bad date in saldo");
246
        }
247
      }
248

    
249
      dbX=GWEN_DB_GetGroup(dbT, GWEN_PATH_FLAGS_NAMEMUSTEXIST, "value");
250
      if (dbX) {
251
        AB_VALUE *v;
252

    
253
        v=AB_Value_fromDb(dbX);
254
        if (v) {
255
          AB_BALANCE *bal;
256
          AB_IMEXPORTER_ACCOUNTINFO *iea;
257

    
258
          bal=AB_Balance_new();
259
          AB_Balance_SetDate(bal, dt);
260
          AB_Balance_SetValue(bal, v);
261
          AB_Value_free(v);
262
          AB_Balance_SetType(bal, AB_Balance_TypeNoted); /* TODO: maybe use "booked" here? */
263

    
264
          iea=AB_ImExporterContext_GetOrAddAccountInfo(ctx, 0, iban, bankCode, accountNumber, 0);
265
          AB_ImExporterAccountInfo_AddBalance(iea, bal);
266
        }
267
      }
268
      GWEN_Date_free(dt);
269
    }
270
    else {
271
      int rv;
272

    
273
      // not a transaction, check subgroups
274
      rv=AH_ImExporterSWIFT__ImportFromGroup(ctx, dbT, dbParams);
275
      if (rv) {
276
        GWEN_Gui_ProgressEnd(progressId);
277
        return rv;
278
      }
279
    }
280

    
281
    if (GWEN_Gui_ProgressAdvance(progressId, GWEN_GUI_PROGRESS_ONE)==
282
        GWEN_ERROR_USER_ABORTED) {
283
      GWEN_Gui_ProgressLog(0, GWEN_LoggerLevel_Error,
284
                           I18N("Aborted by user"));
285
      GWEN_Gui_ProgressEnd(progressId);
286
      return GWEN_ERROR_USER_ABORTED;
287
    }
288
    dbT=GWEN_DB_GetNextGroup(dbT);
289
  } // while
290

    
291
  GWEN_Gui_ProgressEnd(progressId);
292
  return 0;
293
}
294

    
295

    
296

    
297
int AH_ImExporterSWIFT_CheckFile(AB_IMEXPORTER *ie, const char *fname){
298
  AH_IMEXPORTER_SWIFT *ieh;
299
  GWEN_DBIO_CHECKFILE_RESULT rv;
300

    
301
  assert(ie);
302
  ieh=GWEN_INHERIT_GETDATA(AB_IMEXPORTER, AH_IMEXPORTER_SWIFT, ie);
303
  assert(ieh);
304
  assert(ieh->dbio);
305

    
306
  rv=GWEN_DBIO_CheckFile(ieh->dbio, fname);
307
  switch(rv) {
308
  case GWEN_DBIO_CheckFileResultOk:      return 0;
309
  case GWEN_DBIO_CheckFileResultNotOk:   return GWEN_ERROR_BAD_DATA;
310
  case GWEN_DBIO_CheckFileResultUnknown: return AB_ERROR_INDIFFERENT;
311
  default:                               return GWEN_ERROR_GENERIC;
312
  } /* switch */
313
}
314

    
315

    
316

    
317

    
318

    
319

    
320