Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

aqbanking / src / libs / plugins / imexporters / ofx / parser / g_stmtrs.c @ f4a16f5b

History | View | Annotate | Download (7.84 KB)

1
/***************************************************************************
2
 $RCSfile$
3
 -------------------
4
 begin       : Mon Jan 07 2008
5
 copyright   : (C) 2008 by Martin Preuss
6
 email       : martin@libchipcard.de
7

8
 ***************************************************************************
9
 *          Please see toplevel file COPYING for license details           *
10
 ***************************************************************************/
11

    
12

    
13
#ifdef HAVE_CONFIG_H
14
# include <config.h>
15
#endif
16

    
17
#include "g_stmtrs_p.h"
18
#include "ofxxmlctx_l.h"
19
#include "i18n_l.h"
20

    
21
#include "g_generic_l.h"
22
#include "g_ignore_l.h"
23
#include "g_bankacc_l.h"
24
#include "g_banktranlist_l.h"
25
#include "g_bal_l.h"
26

    
27
#include <aqbanking/accstatus.h>
28

    
29
#include <gwenhywfar/misc.h>
30
#include <gwenhywfar/debug.h>
31
#include <gwenhywfar/buffer.h>
32
#include <gwenhywfar/gui.h>
33

    
34

    
35

    
36
GWEN_INHERIT(AIO_OFX_GROUP, AIO_OFX_GROUP_STMTRS)
37

    
38

    
39

    
40

    
41
AIO_OFX_GROUP *AIO_OfxGroup_STMTRS_new(const char *groupName,
42
                                       AIO_OFX_GROUP *parent,
43
                                       GWEN_XML_CONTEXT *ctx) {
44
  AIO_OFX_GROUP *g;
45
  AIO_OFX_GROUP_STMTRS *xg;
46

    
47
  /* create base group */
48
  g=AIO_OfxGroup_Generic_new(groupName, parent, ctx);
49
  assert(g);
50

    
51
  GWEN_NEW_OBJECT(AIO_OFX_GROUP_STMTRS, xg);
52
  assert(xg);
53
  GWEN_INHERIT_SETDATA(AIO_OFX_GROUP, AIO_OFX_GROUP_STMTRS, g, xg,
54
                       AIO_OfxGroup_STMTRS_FreeData);
55

    
56
  /* set virtual functions */
57
  AIO_OfxGroup_SetStartTagFn(g, AIO_OfxGroup_STMTRS_StartTag);
58
  AIO_OfxGroup_SetAddDataFn(g, AIO_OfxGroup_STMTRS_AddData);
59
  AIO_OfxGroup_SetEndSubGroupFn(g, AIO_OfxGroup_STMTRS_EndSubGroup);
60

    
61
  return g;
62
}
63

    
64

    
65

    
66
GWENHYWFAR_CB
67
void AIO_OfxGroup_STMTRS_FreeData(void *bp, void *p) {
68
  AIO_OFX_GROUP_STMTRS *xg;
69

    
70
  xg=(AIO_OFX_GROUP_STMTRS*)p;
71
  assert(xg);
72
  free(xg->currency);
73
  free(xg->currentElement);
74
  GWEN_FREE_OBJECT(xg);
75
}
76

    
77

    
78

    
79
int AIO_OfxGroup_STMTRS_StartTag(AIO_OFX_GROUP *g,
80
                                 const char *tagName) {
81
  AIO_OFX_GROUP_STMTRS *xg;
82
  GWEN_XML_CONTEXT *ctx;
83
  AIO_OFX_GROUP *gNew=NULL;
84

    
85
  assert(g);
86
  xg=GWEN_INHERIT_GETDATA(AIO_OFX_GROUP, AIO_OFX_GROUP_STMTRS, g);
87
  assert(xg);
88

    
89
  ctx=AIO_OfxGroup_GetXmlContext(g);
90

    
91
  free(xg->currentElement);
92
  xg->currentElement=NULL;
93

    
94
  if (strcasecmp(tagName, "CURDEF")==0) {
95
    xg->currentElement=strdup(tagName);
96
  }
97
  else if (strcasecmp(tagName, "BANKACCTFROM")==0) {
98
    gNew=AIO_OfxGroup_BANKACC_new(tagName, g, ctx);
99
  }
100
  else if (strcasecmp(tagName, "CCACCTFROM")==0) {
101
    gNew=AIO_OfxGroup_BANKACC_new(tagName, g, ctx);
102
  }
103
  else if (strcasecmp(tagName, "BANKTRANLIST")==0) {
104
    gNew=AIO_OfxGroup_BANKTRANLIST_new(tagName, g, ctx);
105
  }
106
  else if (strcasecmp(tagName, "LEDGERBAL")==0) {
107
    gNew=AIO_OfxGroup_BAL_new(tagName, g, ctx);
108
  }
109
  else if (strcasecmp(tagName, "AVAILBAL")==0) {
110
    gNew=AIO_OfxGroup_BAL_new(tagName, g, ctx);
111
  }
112
  else if (strcasecmp(tagName, "MKTGINFO")==0) {
113
    /* ignore marketing info */
114
  }
115
  else {
116
    DBG_WARN(AQBANKING_LOGDOMAIN,
117
             "Ignoring group [%s]", tagName);
118
    gNew=AIO_OfxGroup_Ignore_new(tagName, g, ctx);
119
  }
120

    
121
  if (gNew) {
122
    AIO_OfxXmlCtx_SetCurrentGroup(ctx, gNew);
123
    GWEN_XmlCtx_IncDepth(ctx);
124
  }
125

    
126
  return 0;
127
}
128

    
129

    
130

    
131
int AIO_OfxGroup_STMTRS_AddData(AIO_OFX_GROUP *g, const char *data) {
132
  AIO_OFX_GROUP_STMTRS *xg;
133

    
134
  assert(g);
135
  xg=GWEN_INHERIT_GETDATA(AIO_OFX_GROUP, AIO_OFX_GROUP_STMTRS, g);
136
  assert(xg);
137

    
138
  if (xg->currentElement) {
139
    GWEN_BUFFER *buf;
140
    int rv;
141
    const char *s;
142

    
143
    buf=GWEN_Buffer_new(0, strlen(data), 0, 1);
144
    rv=AIO_OfxXmlCtx_SanitizeData(AIO_OfxGroup_GetXmlContext(g), data, buf);
145
    if (rv<0) {
146
      DBG_INFO(AQBANKING_LOGDOMAIN, "here (%d)", rv);
147
      GWEN_Buffer_free(buf);
148
      return rv;
149
    }
150
    s=GWEN_Buffer_GetStart(buf);
151
    if (*s) {
152
      DBG_INFO(AQBANKING_LOGDOMAIN,
153
               "AddData: %s=[%s]", xg->currentElement, s);
154
      if (strcasecmp(xg->currentElement, "CURDEF")==0) {
155
        free(xg->currency);
156
        xg->currency=strdup(s);
157
      }
158
      else {
159
        DBG_INFO(AQBANKING_LOGDOMAIN,
160
                 "Ignoring data for unknown element [%s]",
161
                 xg->currentElement);
162
      }
163
    }
164
    GWEN_Buffer_free(buf);
165
  }
166

    
167
  return 0;
168
}
169

    
170

    
171

    
172
int AIO_OfxGroup_STMTRS_EndSubGroup(AIO_OFX_GROUP *g, AIO_OFX_GROUP *sg) {
173
  AIO_OFX_GROUP_STMTRS *xg;
174
  const char *s;
175
  GWEN_XML_CONTEXT *ctx;
176

    
177
  assert(g);
178
  xg=GWEN_INHERIT_GETDATA(AIO_OFX_GROUP, AIO_OFX_GROUP_STMTRS, g);
179
  assert(xg);
180

    
181
  ctx=AIO_OfxGroup_GetXmlContext(g);
182
  assert(ctx);
183

    
184
  s=AIO_OfxGroup_GetGroupName(sg);
185
  if (strcasecmp(s, "BANKACCTFROM")==0 ||
186
      strcasecmp(s, "CCACCTFROM")==0) {
187
    AB_IMEXPORTER_ACCOUNTINFO *ai;
188
    const char *s;
189

    
190
    DBG_INFO(AQBANKING_LOGDOMAIN,
191
             "Importing account %s/%s",
192
             AIO_OfxGroup_BANKACC_GetBankId(sg),
193
             AIO_OfxGroup_BANKACC_GetAccId(sg));
194
    ai=AB_ImExporterAccountInfo_new();
195
    assert(ai);
196

    
197
    s=AIO_OfxGroup_BANKACC_GetBankId(sg);
198
    if (s)
199
      AB_ImExporterAccountInfo_SetBankCode(ai, s);
200
    s=AIO_OfxGroup_BANKACC_GetAccId(sg);
201
    if (s)
202
      AB_ImExporterAccountInfo_SetAccountNumber(ai, s);
203

    
204
    /* set currency */
205
    if (xg->currency)
206
      AB_ImExporterAccountInfo_SetCurrency(ai, xg->currency);
207

    
208
    /* set account type, if known */
209
    s=AIO_OfxGroup_BANKACC_GetAccType(sg);
210
    if (!s)
211
      s="BANK"; /* not a real code */
212
    if (s) {
213
      AB_ACCOUNT_TYPE t;
214

    
215
      t=AIO_OfxGroup_Generic_AccountTypeFromString(s);
216
      AB_ImExporterAccountInfo_SetAccountType(ai, t);
217
    }
218

    
219
    DBG_INFO(AQBANKING_LOGDOMAIN, "Adding account");
220
    AB_ImExporterContext_AddAccountInfo(AIO_OfxXmlCtx_GetIoContext(ctx), ai);
221
    xg->accountInfo=ai;
222
  }
223
  else if (strcasecmp(s, "BANKTRANLIST")==0) {
224
    AB_TRANSACTION_LIST2 *tl;
225

    
226
    tl=AIO_OfxGroup_BANKTRANLIST_TakeTransactionList(sg);
227
    if (tl) {
228
      AB_TRANSACTION_LIST2_ITERATOR *it;
229

    
230
      it=AB_Transaction_List2_First(tl);
231
      if (it) {
232
        AB_TRANSACTION *t;
233

    
234
        t=AB_Transaction_List2Iterator_Data(it);
235
        while(t) {
236
          DBG_INFO(AQBANKING_LOGDOMAIN, "Importing transaction");
237
          /* set currency if missing */
238
          if (xg->currency) {
239
            const AB_VALUE *v;
240

    
241
            v=AB_Transaction_GetValue(t);
242
            if (v && AB_Value_GetCurrency(v)==NULL) {
243
              AB_VALUE *v2;
244

    
245
              v2=AB_Value_dup(v);
246
              AB_Value_SetCurrency(v2, xg->currency);
247
              AB_Transaction_SetValue(t, v2);
248
              AB_Value_free(v2);
249
            }
250
          }
251
          AB_ImExporterAccountInfo_AddTransaction(xg->accountInfo, t);
252
          t=AB_Transaction_List2Iterator_Next(it);
253
        }
254
        AB_Transaction_List2Iterator_free(it);
255
      }
256
      /* don't call AB_Transaction_List2_freeAll(), because the transactions
257
       * from the list have been taken over by the AccountInfo object */
258
      AB_Transaction_List2_free(tl);
259
    }
260
  }
261
  else if (strcasecmp(s, "LEDGERBAL")==0) {
262
    const GWEN_DATE *dt;
263
    const AB_VALUE *v;
264

    
265
    dt=AIO_OfxGroup_BAL_GetDate(sg);
266
    v=AIO_OfxGroup_BAL_GetValue(sg);
267
    if (v) {
268
      AB_BALANCE *bal;
269

    
270
      bal=AB_Balance_new();
271
      AB_Balance_SetType(bal, AB_Balance_TypeBooked);
272
      AB_Balance_SetDate(bal, dt);
273
      AB_Balance_SetValue(bal, v);
274
      if (xg->currency && AB_Value_GetCurrency(v)==NULL) {
275
        AB_VALUE *v2;
276

    
277
        v2=AB_Value_dup(v);
278
        AB_Value_SetCurrency(v2, xg->currency);
279
        AB_Balance_SetValue(bal, v2);
280
        AB_Value_free(v2);
281
      }
282
      DBG_INFO(AQBANKING_LOGDOMAIN, "Adding balance");
283
      AB_ImExporterAccountInfo_AddBalance(xg->accountInfo, bal);
284
    }
285
  }
286
  else if (strcasecmp(s, "AVAILBAL")==0) {
287
    const GWEN_DATE *dt;
288
    const AB_VALUE *v;
289

    
290
    dt=AIO_OfxGroup_BAL_GetDate(sg);
291
    v=AIO_OfxGroup_BAL_GetValue(sg);
292
    if (v) {
293
      AB_BALANCE *bal;
294

    
295
      bal=AB_Balance_new();
296
      AB_Balance_SetType(bal, AB_Balance_TypeNoted);
297
      AB_Balance_SetDate(bal, dt);
298
      AB_Balance_SetValue(bal, v);
299
      if (xg->currency && AB_Value_GetCurrency(v)==NULL) {
300
        AB_VALUE *v2;
301

    
302
        v2=AB_Value_dup(v);
303
        AB_Value_SetCurrency(v2, xg->currency);
304
        AB_Balance_SetValue(bal, v2);
305
        AB_Value_free(v2);
306
      }
307

    
308
      DBG_INFO(AQBANKING_LOGDOMAIN, "Adding balance");
309
      AB_ImExporterAccountInfo_AddBalance(xg->accountInfo, bal);
310
    }
311
  }
312

    
313
  return 0;
314
}
315

    
316

    
317

    
318

    
319

    
320

    
321

    
322