GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
qif-objects.c
1 /*
2  * qif-objects.c -- Objects for the QIF Importer
3  *
4  * Written by: Derek Atkins <[email protected]>
5  * Copyright (c) 2003 Derek Atkins <[email protected]>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License as
9  * published by the Free Software Foundation; either version 2 of
10  * the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, contact:
19  *
20  * Free Software Foundation Voice: +1-617-542-5942
21  * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652
22  * Boston, MA 02110-1301, USA [email protected]
23  */
24 
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28 
29 #include <glib.h>
30 #include <string.h>
31 #include "Account.h"
32 
33 #include "gnc-engine.h"
34 
35 #include "qif-import-p.h"
36 #include "qif-objects-p.h"
37 #include "qif-defaults.h"
38 
39 static QofLogModule log_module = GNC_MOD_IMPORT;
40 
41 /* create a new object of type t, with type-string type and
42  * destroy function dest. Requires 'obj' to be set.
43  */
44 #define qif_object_new(t,typ,dest) ({ \
45  obj = (QifObject) g_new0(t, 1); \
46  obj->type = typ; \
47  obj->destroy = dest; \
48  obj; \
49 })
50 
51 /* Save the string from this "line". Also:
52  * - make sure we're not over-writing anything.
53  * - make sure the 'line' object no longer references the string.
54  */
55 #define qif_save_str(var) { \
56  if (var) { \
57  PERR("duplicate found at line %d: %s", line->lineno, line->line); \
58  g_free(var); \
59  } \
60  (var) = line->line; \
61  line->line = NULL; \
62 }
63 
64 /* QIF Account */
65 static void
66 qif_account_destroy(QifObject obj)
67 {
68  QifAccount acct = (QifAccount) obj;
69 
70  g_free(acct->name);
71  g_free(acct->desc);
72  g_free(acct->limitstr);
73  g_free(acct->budgetstr);
74 
75  g_free(acct);
76 };
77 
78 static QifAccount
79 qif_account_new(void)
80 {
81  QifObject obj;
82  QifAccount acct;
83 
84  obj = qif_object_new(struct _QifAccount, QIF_O_ACCOUNT, qif_account_destroy);
85 
86  acct = (QifAccount)obj;
87  acct->type_list = qif_parse_acct_type("bank", -1);
88 
89  acct->limit = gnc_numeric_zero();
90  acct->budget = gnc_numeric_zero();
91  return acct;
92 }
93 
94 /*
95  * Merge acct into ctx. If this account already exists in ctx then
96  * merge in any new values from acct into the ctx version and return
97  * the existing acct. If the account does not already exist, then
98  * insert it into the ctx and return it.
99  */
101 qif_account_merge(QifContext ctx, QifAccount acct)
102 {
103  QifAccount acct2 =
104  (QifAccount)qif_object_map_lookup(ctx, acct->obj.type, acct->name);
105 
106  if (!acct2)
107  {
108  qif_object_map_insert(ctx, acct->obj.type, (QifObject)acct);
109  return acct;
110  }
111 
112  /* obviously the name is the same, so don't worry about that */
113 
114  if (!acct2->desc && acct->desc)
115  acct2->desc = g_strdup(acct->desc);
116 
117  if (!acct2->type_list && acct->type_list)
118  acct2->type_list = acct->type_list;
119 
120  if (!acct2->limitstr && acct->limitstr)
121  {
122  acct2->limitstr = g_strdup(acct->limitstr);
123  acct2->limit = acct->limit;
124  }
125 
126  if (!acct2->budgetstr && acct->budgetstr)
127  {
128  acct2->budgetstr = g_strdup(acct->budgetstr);
129  acct2->budget = acct->budget;
130  }
131 
132  return acct2;
133 }
134 
135 static QifError
136 qif_account_parse(QifContext ctx, GList *record)
137 {
138  QifAccount acct, temp;
139  QifLine line;
140 
141  g_return_val_if_fail(ctx, QIF_E_INTERNAL);
142  g_return_val_if_fail(record, QIF_E_BADSTATE);
143 
144  acct = qif_account_new();
145 
146  for (; record; record = record->next)
147  {
148  line = record->data;
149 
150  switch (line->type)
151  {
152  case 'N': /* N : account name */
153  qif_save_str(acct->name);
154  break;
155  case 'D': /* D : account description */
156  qif_save_str(acct->desc);
157  break;
158  case 'T': /* T : account type */
159  acct->type_list = qif_parse_acct_type(line->line, line->lineno);
160  break;
161  case 'L': /* L : account limit */
162  qif_save_str(acct->limitstr);
163  break;
164  case 'B': /* B : account budget */
165  qif_save_str(acct->budgetstr);
166  break;
167  default:
168  PERR("Unknown QIF account data at line %d: %s", line->lineno, line->line);
169  }
170  }
171 
172  /* Merge the account into the context */
173  temp = qif_account_merge(ctx, acct);
174  if (! (ctx->parse_flags & QIF_F_IGNORE_ACCOUNTS))
175  ctx->current_acct = temp;
176  if (temp != acct)
177  qif_account_destroy((QifObject)acct);
178 
179  return QIF_E_OK;
180 }
181 
182 /* QIF Category */
183 static void
184 qif_cat_destroy(QifObject obj)
185 {
186  QifCategory cat = (QifCategory) obj;
187 
188  g_free(cat->name);
189  g_free(cat->desc);
190  g_free(cat->taxclass);
191  g_free(cat->budgetstr);
192 
193  g_free(cat);
194 }
195 
196 static QifCategory
197 qif_cat_new(void)
198 {
199  QifObject obj;
200  QifCategory cat;
201 
202  obj = qif_object_new(struct _QifCategory, QIF_O_CATEGORY, qif_cat_destroy);
203  cat = (QifCategory)obj;
204  cat->budget = gnc_numeric_zero();
205 
206  return cat;
207 }
208 
209 /*
210  * Merge cat into ctx. If this category already exists in ctx then
211  * merge in any new values from cat into the ctx version and return
212  * the existing cat. If the category does not already exist, then
213  * insert it into the ctx and return it.
214  */
216 qif_cat_merge(QifContext ctx, QifCategory cat)
217 {
218  QifCategory cat2 =
219  (QifCategory)qif_object_map_lookup(ctx, cat->obj.type, cat->name);
220 
221  if (!cat2)
222  {
223  qif_object_map_insert(ctx, cat->obj.type, (QifObject)cat);
224  return cat;
225  }
226 
227  /* obviously the name is the same, so don't worry about that */
228 
229  if (!cat2->desc && cat->desc)
230  cat2->desc = g_strdup(cat->desc);
231 
232  if (!cat2->taxclass && cat->taxclass)
233  cat2->taxclass = g_strdup(cat->taxclass);
234 
235  cat2->taxable = (cat2->taxable || cat->taxable);
236  cat2->expense = (cat2->expense || cat->expense);
237  cat2->income = (cat2->income || cat->income);
238 
239  if (!cat2->budgetstr && cat->budgetstr)
240  {
241  cat2->budgetstr = g_strdup(cat->budgetstr);
242  cat2->budget = cat->budget;
243  }
244 
245  return cat2;
246 }
247 
248 static QifError
249 qif_cat_parse(QifContext ctx, GList *record)
250 {
251  QifCategory cat;
252  QifLine line;
253 
254  g_return_val_if_fail(ctx, QIF_E_INTERNAL);
255  g_return_val_if_fail(record, QIF_E_BADSTATE);
256 
257  cat = qif_cat_new();
258 
259  for (; record; record = record->next)
260  {
261  line = record->data;
262 
263  switch (line->type)
264  {
265  case 'N': /* N : category name */
266  qif_save_str(cat->name);
267  break;
268  case 'D': /* D : category description */
269  qif_save_str(cat->desc);
270  break;
271  case 'T': /* T : category is taxable? */
272  cat->taxable = TRUE;
273  break;
274  case 'E': /* E : category is expense? */
275  cat->expense = TRUE;
276  break;
277  case 'I': /* I : category is income? */
278  cat->income = TRUE;
279  break;
280  case 'R': /* R : category taxclass XXX */
281  /* XXX: a number? */
282  qif_save_str(cat->taxclass);
283  break;
284  case 'B': /* B : category budget */
285  qif_save_str(cat->budgetstr);
286  break;
287  default:
288  PERR("Unknown QIF category data at line %d: %s", line->lineno, line->line);
289  }
290  }
291 
292  if (qif_cat_merge(ctx, cat) != cat)
293  qif_cat_destroy((QifObject)cat);
294 
295  return QIF_E_OK;
296 }
297 
298 /* QIF Class */
299 static void
300 qif_class_destroy(QifObject obj)
301 {
302  QifClass qclass = (QifClass) obj;
303 
304  g_free(qclass->name);
305  g_free(qclass->desc);
306  g_free(qclass->taxdesig);
307 
308  g_free(qclass);
309 }
310 
311 static QifClass
312 qif_class_new()
313 {
314  QifObject obj;
315 
316  obj = qif_object_new(struct _QifClass, QIF_O_CLASS, qif_class_destroy);
317  return (QifClass)obj;
318 }
319 
320 /*
321  * Merge qclass into ctx. If this class already exists in ctx then
322  * merge in any new values from qclass into the ctx version and return
323  * the existing qclass. If the class does not already exist, then
324  * insert it into the ctx and return it.
325  */
326 QifClass
327 qif_class_merge(QifContext ctx, QifClass qclass)
328 {
329  QifClass qclass2 =
330  (QifClass)qif_object_map_lookup(ctx, qclass->obj.type, qclass->name);
331 
332  if (!qclass2)
333  {
334  qif_object_map_insert(ctx, qclass->obj.type, (QifObject)qclass);
335  return qclass;
336  }
337 
338  /* obviously the name is the same, so don't worry about that */
339 
340  if (!qclass2->desc && qclass->desc)
341  qclass2->desc = g_strdup(qclass->desc);
342 
343  if (!qclass2->taxdesig && qclass->taxdesig)
344  qclass2->taxdesig = g_strdup(qclass->taxdesig);
345 
346  return qclass2;
347 }
348 
349 static QifError
350 qif_class_parse(QifContext ctx, GList *record)
351 {
352  QifClass qclass;
353  QifLine line;
354 
355  g_return_val_if_fail(ctx, QIF_E_INTERNAL);
356  g_return_val_if_fail(record, QIF_E_BADSTATE);
357 
358  qclass = qif_class_new();
359 
360  for (; record; record = record->next)
361  {
362  line = record->data;
363 
364  switch (line->type)
365  {
366  case 'N': /* N : class name */
367  qif_save_str(qclass->name);
368  break;
369  case 'D': /* D : class description */
370  qif_save_str(qclass->desc);
371  break;
372  case 'R': /* R : Tax designator */
373  qif_save_str(qclass->taxdesig);
374  break;
375  default:
376  PERR("Unknown QIF class data at line %d: %s", line->lineno, line->line);
377  }
378  }
379 
380  if (qif_class_merge(ctx, qclass) != qclass)
381  qif_class_destroy((QifObject)qclass);
382 
383  return QIF_E_OK;
384 }
385 
386 /* QIF Security Symbol */
387 static void
388 qif_security_destroy(QifObject obj)
389 {
390  QifSecurity security = (QifSecurity) obj;
391 
392  g_free(security->name);
393  g_free(security->symbol);
394  g_free(security->type);
395 
396  g_free(security);
397 }
398 
399 static QifSecurity
400 qif_security_new()
401 {
402  QifObject obj;
403 
404  obj = qif_object_new(struct _QifSecurity, QIF_O_SECURITY, qif_security_destroy);
405  return (QifSecurity)obj;
406 }
407 
408 /*
409  * Merge security into ctx. If this security already exists in ctx then
410  * merge in any new values from security into the ctx version and return
411  * the existing security. If the security does not already exist, then
412  * insert it into the ctx and return it.
413  */
415 qif_security_merge(QifContext ctx, QifSecurity security)
416 {
417  QifSecurity security2 =
418  (QifSecurity)qif_object_map_lookup(ctx, security->obj.type, security->name);
419 
420  if (!security2)
421  {
422  qif_object_map_insert(ctx, security->obj.type, (QifObject)security);
423  return security;
424  }
425 
426  /* obviously the name is the same, so don't worry about that */
427 
428  if (!security2->symbol && security->symbol)
429  security2->symbol = g_strdup(security->symbol);
430 
431  if (!security2->type && security->type)
432  security2->type = g_strdup(security->type);
433 
434  return security2;
435 }
436 
437 static QifError
438 qif_security_parse(QifContext ctx, GList *record)
439 {
440  QifSecurity security;
441  QifLine line;
442 
443  g_return_val_if_fail(ctx, QIF_E_INTERNAL);
444  g_return_val_if_fail(record, QIF_E_BADSTATE);
445 
446  security = qif_security_new();
447 
448  for (; record; record = record->next)
449  {
450  line = record->data;
451 
452  switch (line->type)
453  {
454  case 'N': /* N : security name */
455  qif_save_str(security->name);
456  break;
457  case 'S': /* S : security symbol */
458  qif_save_str(security->symbol);
459  break;
460  case 'T': /* T : security type */
461  qif_save_str(security->type);
462  break;
463  default:
464  PERR("Unknown QIF security data at line %d: %s", line->lineno, line->line);
465  }
466  }
467 
468  if (qif_security_merge(ctx, security) != security)
469  qif_security_destroy((QifObject)security);
470 
471  return QIF_E_OK;
472 }
473 
474 /********************* TXN *********************/
475 
476 static QifSplit
477 qif_split_new()
478 {
479  QifSplit split = g_new0(struct _QifSplit, 1);
480 
481  /* Initialize to 'zero' (even though they are not valid) */
482  split->amount = gnc_numeric_zero();
483  split->value = gnc_numeric_zero();
484 
485  return split;
486 }
487 
488 static void
489 qif_split_destroy(QifSplit split)
490 {
491  if (!split) return;
492 
493  g_free(split->memo);
494  g_free(split->catstr);
495  g_free(split->amountstr);
496 
497  g_free(split);
498 }
499 
500 static QifSplit
501 qif_split_copy(QifSplit split)
502 {
503  QifSplit s = qif_split_new();
504 
505  memcpy(s, split, sizeof(*s));
506  if (s->memo) s->memo = g_strdup(s->memo);
507  if (s->amountstr) s->amountstr = g_strdup(s->amountstr);
508  if (s->catstr) s->memo = g_strdup(s->catstr);
509 
510  return s;
511 }
512 
513 /* Forward declarations */
514 static void qif_txn_invst_destroy(QifInvstTxn);
515 
516 /* QIF Transaction */
517 
518 static void
519 qif_split_parse_category(QifContext ctx, QifSplit split)
520 {
521  char *cat = NULL;
522  char *cat_class = NULL;
523  char *miscx_cat = NULL;
524  char *miscx_class = NULL;
525 
526  gboolean miscx_is_acct;
527 
528  static GList *types = NULL;
529 
530  g_return_if_fail(ctx);
531  g_return_if_fail(split);
532  g_return_if_fail(split->cat.cat == NULL && split->cat_class == NULL);
533 
534  if (qif_parse_split_category(split->catstr,
535  &cat, &split->cat_is_acct, &cat_class,
536  &miscx_cat, &miscx_is_acct, &miscx_class))
537  {
538  g_assert(cat);
539 
540  if (split->cat_is_acct)
541  {
542  if (types == NULL)
543  types = qif_parse_acct_type("__any_bank__", -1);
544 
545  split->cat.acct = find_or_make_acct(ctx, cat, types);
546 
547  }
548  else
549  split->cat.cat = find_or_make_cat(ctx, cat);
550 
551  if (cat_class)
552  split->cat_class = find_or_make_class(ctx, cat_class);
553 
554  /* miscx isn't used in a normal transaction, so just ignore it */
555  if (miscx_cat)
556  g_free(miscx_cat);
557  if (miscx_class)
558  g_free(miscx_class);
559 
560  }
561  else
562  PERR("Problem parsing split category: %s", split->catstr);
563 }
564 
565 static void
566 qif_txn_destroy(QifObject obj)
567 {
568  QifTxn txn = (QifTxn) obj;
569  GList *node;
570  QifSplit split;
571 
572  g_free(txn->datestr);
573  g_free(txn->payee);
574  g_free(txn->address);
575  g_free(txn->num);
576 
577  if (txn->invst_info)
578  qif_txn_invst_destroy(txn->invst_info);
579 
580  for (node = txn->splits; node; node = node->next)
581  {
582  split = node->data;
583  if (split == txn->default_split)
584  txn->default_split = NULL;
585  if (split == txn->current_split)
586  txn->current_split = NULL;
587 
588  qif_split_destroy(split);
589  }
590 
591  g_list_free(txn->splits);
592  qif_split_destroy(txn->default_split);
593  qif_split_destroy(txn->current_split);
594 
595  g_free(txn);
596 }
597 
598 static QifTxn
599 qif_txn_new(void)
600 {
601  QifObject obj;
602  QifTxn txn;
603 
604  obj = qif_object_new(struct _QifTxn, "qif-txn", qif_txn_destroy);
605  txn = (QifTxn) obj;
606  txn->default_split = qif_split_new();
607 
608  return txn;
609 }
610 
611 static void
612 qif_txn_init(QifContext ctx)
613 {
614  qif_clear_flag(ctx->parse_flags, QIF_F_IGNORE_ACCOUNTS);
615  ctx->parse_state = NULL;
616 }
617 
618 static gboolean
619 qif_is_bad_numeric_string(const char* line)
620 {
621  return (strncmp(line, "...", 3) == 0);
622 }
623 
624 /*
625  * this is called for the first transaction after each !Type: tag.
626  *
627  * if the first transaction after a !Type: tag has a payee of "Opening
628  * Balance" or "Initial Balance", we have to massage the transaction a
629  * little. The meaning of an OB transaction is "transfer from Equity
630  * to the account specified in the L line." Idiomatically, ms-money
631  * and some others use this transaction instead of an Account record
632  * to specify "this" account (the from-account for all following
633  * transactions), so we have to allow for that.
634  *
635  * Even if the payee isn't "Opening Balance", we if we have no default
636  * from-account by this time we need to set one. In that case we set
637  * the default account based on the file name.
638  *
639  * If we DO know the account already, and this is a tranfer to it,
640  * it's also an opening balance regardless of the payee.
641  *
642  * In the end make sure that the context 'current account' is set.
643  */
644 static void
645 qif_process_opening_balance_txn(QifContext ctx, QifTxn txn)
646 {
647  QifSplit split = txn->default_split;
648  QifAccount cur_acct = NULL; /* We know that ctx->current_acct is NULL */
649 
650  g_return_if_fail(txn->invst_info == NULL);
651 
652  if ((!cur_acct && txn->payee &&
653  (!strcasecmp(txn->payee, "Opening Balance") ||
654  !strcasecmp(txn->payee, "Initial Balance")) && split->cat_is_acct) ||
655  (cur_acct &&
656  ((split->cat_is_acct && !strcasecmp(split->cat.acct->name, cur_acct->name))
657  ||
658  (!split->cat_is_acct && !strcasecmp(split->cat.cat->name, cur_acct->name))))
659  )
660  {
661 
662  /* This is an explicit "Opening Balance" transactions. We need to
663  * change the "from account" to point to the equity account that
664  * the opening balance comes from...
665  */
666  if (split->cat_is_acct)
667  cur_acct = split->cat.acct;
668  else
669  {
670  g_assert(split->cat.cat);
671  cur_acct = find_or_make_acct(ctx, g_strdup(split->cat.cat->name),
672  qif_parse_acct_type_guess(txn->txn_type));
673  split->cat_is_acct = TRUE;
674  }
675  split->cat.acct = qif_default_equity_acct(ctx);
676  }
677 
678  /*
679  * If we found an opening balance account then set up the context.
680  * If we didn't actually succeed in finding an account then
681  * set a flag so we can go back later and look for it.
682  */
683 
684  if (cur_acct)
685  {
686  ctx->opening_bal_acct = cur_acct;
687  ctx->current_acct = cur_acct;
688  }
689  else
690  qif_set_flag(ctx->parse_flags, QIF_F_TXN_NEEDS_ACCT);
691 }
692 
693 /* process all the splits in the transaction -- if this is a "split
694  * transaction" then make sure the sum of all the amounts (including
695  * the default split) does NOT equal zero -- if it does then we want
696  * to reverse all the splits. The "amount" should be the 'T' amount
697  * from the txn.
698  */
699 static void
700 qif_txn_fix_amounts(QifTxn txn, gnc_numeric amount)
701 {
702  gnc_numeric sum = amount;
703  QifSplit split;
704  GList *node;
705 
706  g_return_if_fail(txn);
707 
708  /* No current_split, so this is NOT a split transaction. */
709  if (!txn->current_split) return;
710 
711  /* Then add in every split in the split-list */
712  for (node = txn->splits; node; node = node->next)
713  {
714  split = node->data;
715  sum = gnc_numeric_add(sum, split->amount, GNC_DENOM_AUTO, GNC_HOW_DENOM_LCD);
716  }
717 
718  /* if the sum is not zero then reverse all the amounts in the split list */
719  if (!gnc_numeric_zero_p(sum))
720  for (node = txn->splits; node; node = node->next)
721  {
722  split = node->data;
723  split->amount = gnc_numeric_neg(split->amount);
724  }
725 }
726 
727 static QifError
728 qif_txn_parse(QifContext ctx, GList *record)
729 {
730  QifTxn txn;
731  QifLine line;
732  GList *node;
733  QifSplit split;
734 
735  g_return_val_if_fail(ctx, QIF_E_INTERNAL);
736  g_return_val_if_fail(record, QIF_E_BADSTATE);
737 
738  txn = qif_txn_new();
739  txn->txn_type = ctx->parse_type;
740 
741  for (; record; record = record->next)
742  {
743  line = record->data;
744 
745  switch (line->type)
746  {
747  case 'D': /* D : transaction date */
748  qif_save_str(txn->datestr);
749  break;
750  case 'P': /* P : payee */
751  qif_save_str(txn->payee);
752  break;
753  case 'A': /* A : address */
754  /* multiple 'A' lines are appended together with newlines */
755  if (txn->address)
756  {
757  char *tmp = txn->address;
758  txn->address = g_strconcat(tmp, "\n", line->line, NULL);
759  g_free(tmp);
760  }
761  else
762  qif_save_str(txn->address);
763  break;
764  case 'N': /* N : check/transaction number */
765  qif_save_str(txn->num);
766  break;
767  case 'C': /* C : transaction cleared flag */
768  txn->cleared = qif_parse_cleared(line);
769  break;
770  case 'L': /* L : default split category */
771  if (!txn->current_split) qif_save_str(txn->default_split->catstr);
772  break;
773  case 'M': /* M : default split memo */
774  if (!txn->current_split) qif_save_str(txn->default_split->memo);
775  break;
776  case 'T': /* T : total transaction amount */
777  if (!txn->current_split && !qif_is_bad_numeric_string(line->line))
778  qif_save_str(txn->default_split->amountstr);
779  break;
780  case 'S': /* S : split category */
781  /* This implies a quicken-style "split transaction", so we're mostly
782  * going to ignore the default_split except for internal verification.
783  */
784  txn->current_split = qif_split_new();
785  txn->splits = g_list_prepend(txn->splits, txn->current_split);
786  qif_save_str(txn->current_split->catstr);
787  break;
788  case 'E': /* E : split memo */
789  if (txn->current_split)
790  qif_save_str(txn->current_split->memo);
791  break;
792  case '$': /* split amount */
793  if (txn->current_split && !qif_is_bad_numeric_string(line->line))
794  qif_save_str(txn->current_split->amountstr);
795  break;
796  default:
797  PERR("Unknown QIF transaction data at line %d: %s", line->lineno, line->line);
798  }
799  }
800 
801  /* If we have no date string then there is no reason to do anything else */
802  if (txn->datestr)
803  {
804  /* We delay processing the date and amount strings until later.. */
805 
806  /* parse the category on each split */
807  for (node = txn->splits; node; node = node->next)
808  {
809  split = node->data;
810  if (split->catstr)
811  qif_split_parse_category(ctx, split);
812  }
813  /* ... including the default split */
814  if (txn->default_split->catstr)
815  qif_split_parse_category(ctx, txn->default_split);
816 
817  /* if we don't have an account, then deal with the opening balance */
818  if (!ctx->current_acct)
819  qif_process_opening_balance_txn(ctx, txn);
820 
821  /* Set the transaction's from account */
822  txn->from_acct = ctx->current_acct;
823 
824  /* And add it to the process list */
825  ctx->parse_state = g_list_prepend(ctx->parse_state, txn);
826 
827  }
828  else
829  /* no date? Ignore this txn */
830  qif_txn_destroy((QifObject)txn);
831 
832  return QIF_E_OK;
833 }
834 
835 /* after we parse the amounts, fix up the transaction splits */
836 void
837 qif_txn_setup_splits(QifTxn txn)
838 {
839  QifSplit split, this_split;
840  GList *node;
841  gnc_numeric total;
842 
843  if (txn->splits)
844  {
845  /* We have a bunch of "far" splits -- maybe fix up the totals.. */
846  qif_txn_fix_amounts(txn, txn->default_split->amount);
847 
848  /* Re-Compute the total for the "near" (default) split */
849  total = gnc_numeric_zero();
850  for (node = txn->splits; node; node = node->next)
851  {
852  split = node->data;
853  split->value = split->amount;
854  total = gnc_numeric_add(total, split->amount, 0, GNC_HOW_DENOM_LCD);
855  }
856 
857  /* And re-set the default-split amount */
858  txn->default_split->amount = gnc_numeric_neg(total);
859 
860  }
861  else
862  {
863  /* not a split txn. Compute the "far" split by copying the "near"
864  * split and then moving the 'near' split to the far split.
865  */
866 
867  /* First make a copy of this transaction and move the copy to the 'near' */
868  split = txn->default_split;
869  this_split = qif_split_copy(split);
870  txn->default_split = this_split;
871 
872  /* then adjust the 'far' txn */
873  split->amount = gnc_numeric_neg(split->amount);
874  split->value = split->amount;
875  txn->splits = g_list_prepend(txn->splits, split);
876  }
877 
878  /* Set the default-split value from the default-split amount */
879  txn->default_split->value = txn->default_split->amount;
880 }
881 
882 /* This is called when we're done processing an account. We want
883  * to merge the transactions in the "parse_state" into the Qif Context
884  */
885 static QifError
886 qif_txn_end_acct(QifContext ctx)
887 {
888  GList *node;
889  QifTxn txn;
890  gboolean txn_needs_acct;
891 
892  g_return_val_if_fail(ctx, QIF_E_INTERNAL);
893 
894  /* Return now if there is nothing to do. */
895  if (!ctx->parse_state) return QIF_E_OK;
896 
897  /* Walk through the list of transactions. First check if it
898  * needs a from-account; then add it to the context.
899  */
900 
901  txn_needs_acct = (ctx->parse_flags & QIF_F_TXN_NEEDS_ACCT);
902 
903  /* Invert the list so we're working in the right order */
904  ctx->parse_state = g_list_reverse(ctx->parse_state);
905 
906  for (node = ctx->parse_state; node; node = node->next)
907  {
908  txn = node->data;
909 
910  /* If we need a from account, then set it.. */
911  if (txn_needs_acct && ctx->opening_bal_acct && !txn->from_acct)
912  txn->from_acct = ctx->opening_bal_acct;
913 
914  /* merge the txn into the context (prepends to the list) */
915  qif_object_list_insert(ctx, (QifObject)txn);
916  }
917 
918  if (txn_needs_acct && ctx->opening_bal_acct)
919  qif_clear_flag(ctx->parse_flags, QIF_F_TXN_NEEDS_ACCT);
920 
921  /* clean up our state */
922  g_list_free(ctx->parse_state);
923  ctx->parse_state = NULL;
924 
925  return QIF_E_OK;
926 }
927 
928 /* Extra info in an Investment Transaction */
929 static QifInvstTxn
930 qif_invst_txn_new(void)
931 {
932  QifInvstTxn itxn = g_new0(struct _QifInvstTxn, 1);
933 
934  itxn->amount = gnc_numeric_zero();
935  itxn->d_amount = gnc_numeric_zero();
936  itxn->price = gnc_numeric_zero();
937  itxn->shares = gnc_numeric_zero();
938  itxn->commission = gnc_numeric_zero();
939 
940  return itxn;
941 }
942 
943 static void
944 qif_txn_invst_destroy(QifInvstTxn itxn)
945 {
946  if (!itxn) return;
947 
948  g_free(itxn->amountstr);
949  g_free(itxn->d_amountstr);
950  g_free(itxn->pricestr);
951  g_free(itxn->sharesstr);
952  g_free(itxn->commissionstr);
953  g_free(itxn->security);
954 
955  g_free(itxn->catstr);
956 
957  g_free(itxn);
958 }
959 
960 static QifError
961 qif_txn_invst_parse(QifContext ctx, GList *record)
962 {
963  QifTxn txn;
964  QifInvstTxn itxn;
965  QifLine line;
966 
967  g_return_val_if_fail(ctx, QIF_E_INTERNAL);
968  g_return_val_if_fail(record, QIF_E_BADSTATE);
969 
970  txn = qif_txn_new();
971  txn->txn_type = ctx->parse_type;
972  itxn = qif_invst_txn_new();
973  txn->invst_info = itxn;
974 
975  for (; record; record = record->next)
976  {
977  line = record->data;
978 
979  switch (line->type)
980  {
981  case 'D': /* D : transaction date */
982  qif_save_str(txn->datestr);
983  break;
984  case 'P': /* P : txn payee */
985  qif_save_str(txn->payee);
986  break;
987  case 'N': /* N : action */
988  itxn->action = qif_parse_action(line);
989  break;
990  case 'C': /* C : cleared flag */
991  txn->cleared = qif_parse_cleared(line);
992  break;
993  case 'M': /* M : memo */
994  if (!txn->current_split)
995  qif_save_str(txn->default_split->memo);
996  break;
997  case 'T': /* T : total amount */
998  if (!qif_is_bad_numeric_string(line->line))
999  qif_save_str(itxn->amountstr);
1000  break;
1001  case '$': /* $ : transfer amount */
1002  if (!qif_is_bad_numeric_string(line->line))
1003  qif_save_str(itxn->d_amountstr);
1004  break;
1005  case 'I': /* I : share price */
1006  qif_save_str(itxn->pricestr);
1007  break;
1008  case 'Q': /* Q : number of shares */
1009  qif_save_str(itxn->sharesstr);
1010  break;
1011  case 'Y': /* Y : name of security */
1012  qif_save_str(itxn->security);
1013  break;
1014  case 'O': /* O : commission */
1015  qif_save_str(itxn->commissionstr);
1016  break;
1017  case 'L': /* L : category */
1018  qif_save_str(itxn->catstr);
1019  break;
1020  default:
1021  PERR("Unknown QIF Investment transaction data at line %d: %s",
1022  line->lineno, line->line);
1023  }
1024  }
1025 
1026  /* If we have no date string then there is no reason to do anything else */
1027  if (txn->datestr && itxn->action != QIF_A_NONE)
1028  {
1029 
1030  /* Make sure we've got a security name */
1031  if (!itxn->security)
1032  itxn->security = g_strdup(""); /* XXX */
1033 
1034  /* if we don't have a from account, then mark the fact that
1035  * we'll need one later.
1036  */
1037  if (ctx->current_acct)
1038  txn->from_acct = ctx->current_acct;
1039  else
1040  qif_set_flag(ctx->parse_flags, QIF_F_ITXN_NEEDS_ACCT);
1041 
1042  /* Add this transaction to the parse state for later processing */
1043  ctx->parse_state = g_list_prepend(ctx->parse_state, txn);
1044 
1045  }
1046  else
1047  {
1048  /* no date? Just destroy it */
1049  qif_txn_destroy((QifObject)txn);
1050  }
1051 
1052  return QIF_E_OK;
1053 }
1054 
1055 
1056 void
1057 qif_invst_txn_setup_splits(QifContext ctx, QifTxn txn)
1058 {
1059  QifInvstTxn itxn;
1060  QifSplit near_split, far_split, comm_split;
1061  QifAccount from_acct;
1062 
1063  char *cat = NULL;
1064  char *cat_class = NULL;
1065  gboolean cat_is_acct = FALSE;
1066  char *miscx = NULL;
1067  char *miscx_class = NULL;
1068  gboolean miscx_is_acct = FALSE;
1069 
1070  /* Cached account-type lists */
1071  static GList *bank_list = NULL;
1072 
1073  gnc_numeric split_value;
1074 
1075  g_return_if_fail(ctx);
1076  g_return_if_fail(txn);
1077  g_return_if_fail(txn->invst_info);
1078 
1079  itxn = txn->invst_info;
1080 
1081  /* Compute the share value, because we'll probably need it */
1082  split_value = gnc_numeric_mul(itxn->shares, itxn->price, 0, GNC_HOW_DENOM_REDUCE);
1083 
1084  /* Make sure that "amount" is a valid "transaction amount" */
1085  if (!itxn->amountstr && itxn->d_amountstr)
1086  itxn->amount = itxn->d_amount;
1087 
1088  /* near and far splits.. for simplicity */
1089  near_split = txn->default_split;
1090  far_split = qif_split_new();
1091  from_acct = txn->from_acct;
1092 
1093  /* Parse the category string */
1094  if (!qif_parse_split_category(itxn->catstr,
1095  &cat, &cat_is_acct, &cat_class,
1096  &miscx, &miscx_is_acct, &miscx_class))
1097  PERR("Failure parsing category: %s", itxn->catstr);
1098 
1099  /* Make sure we've got a cached list */
1100  if (bank_list == NULL)
1101  bank_list = qif_parse_acct_type("__any_bank__", -1);
1102 
1103  /* find the NEAR account */
1104 
1105  switch (itxn->action)
1106  {
1107  case QIF_A_BUY:
1108  case QIF_A_BUYX:
1109  case QIF_A_REINVDIV:
1110  case QIF_A_REINVINT:
1111  case QIF_A_REINVLG:
1112  case QIF_A_REINVMD:
1113  case QIF_A_REINVSG:
1114  case QIF_A_REINVSH:
1115  case QIF_A_SELL:
1116  case QIF_A_SELLX:
1117  case QIF_A_SHRSIN:
1118  case QIF_A_SHRSOUT:
1119  case QIF_A_STKSPLIT:
1120  txn->from_acct = qif_default_stock_acct(ctx, itxn->security);
1121  break;
1122 
1123  case QIF_A_CGLONG:
1124  case QIF_A_CGMID:
1125  case QIF_A_CGSHORT:
1126  case QIF_A_DIV:
1127  case QIF_A_INTINC:
1128  case QIF_A_MARGINT:
1129  case QIF_A_MISCEXP:
1130  case QIF_A_MISCINC:
1131  case QIF_A_RTRNCAP:
1132  case QIF_A_XIN:
1133  case QIF_A_XOUT:
1134  txn->from_acct = from_acct;
1135  break;
1136 
1137  case QIF_A_CGLONGX:
1138  case QIF_A_CGMIDX:
1139  case QIF_A_CGSHORTX:
1140  case QIF_A_DIVX:
1141  case QIF_A_INTINCX:
1142  case QIF_A_MARGINTX:
1143  case QIF_A_RTRNCAPX:
1144  txn->from_acct = find_or_make_acct(ctx, cat, bank_list);
1145  cat = NULL;
1146  break;
1147 
1148  case QIF_A_MISCEXPX:
1149  case QIF_A_MISCINCX:
1150  txn->from_acct = find_or_make_acct(ctx, miscx, bank_list);
1151  miscx = NULL;
1152  break;
1153 
1154  default:
1155  PERR("Unhandled Action: %d", itxn->action);
1156  break;
1157  }
1158 
1159  /* find the FAR account */
1160 
1161  itxn->far_cat_is_acct = TRUE;
1162  switch (itxn->action)
1163  {
1164  case QIF_A_BUY:
1165  case QIF_A_SELL:
1166  itxn->far_cat.acct = from_acct;
1167  break;
1168 
1169  case QIF_A_BUYX:
1170  case QIF_A_MISCEXP:
1171  case QIF_A_MISCEXPX:
1172  case QIF_A_MISCINC:
1173  case QIF_A_MISCINCX:
1174  case QIF_A_SELLX:
1175  case QIF_A_XIN:
1176  case QIF_A_XOUT:
1177  itxn->far_cat.cat = find_or_make_cat(ctx, cat);
1178  itxn->far_cat_is_acct = FALSE;
1179  cat = NULL;
1180  break;
1181 
1182  case QIF_A_CGLONG:
1183  case QIF_A_CGLONGX:
1184  case QIF_A_REINVLG:
1185  itxn->far_cat.acct = qif_default_cglong_acct(ctx, itxn->security);
1186  break;
1187 
1188  case QIF_A_CGMID:
1189  case QIF_A_CGMIDX:
1190  case QIF_A_REINVMD:
1191  itxn->far_cat.acct = qif_default_cgmid_acct(ctx, itxn->security);
1192  break;
1193 
1194  case QIF_A_CGSHORT:
1195  case QIF_A_CGSHORTX:
1196  case QIF_A_REINVSG:
1197  case QIF_A_REINVSH:
1198  itxn->far_cat.acct = qif_default_cgshort_acct(ctx, itxn->security);
1199  break;
1200 
1201  case QIF_A_DIV:
1202  case QIF_A_DIVX:
1203  case QIF_A_REINVDIV:
1204  itxn->far_cat.acct = qif_default_dividend_acct(ctx, itxn->security);
1205  break;
1206 
1207  case QIF_A_INTINC:
1208  case QIF_A_INTINCX:
1209  case QIF_A_REINVINT:
1210  itxn->far_cat.acct = qif_default_interest_acct(ctx, itxn->security);
1211  break;
1212 
1213  case QIF_A_MARGINT:
1214  case QIF_A_MARGINTX:
1215  itxn->far_cat.acct = qif_default_margin_interest_acct(ctx);
1216  break;
1217 
1218  case QIF_A_RTRNCAP:
1219  case QIF_A_RTRNCAPX:
1220  itxn->far_cat.acct = qif_default_capital_return_acct(ctx, itxn->security);
1221  break;
1222 
1223  case QIF_A_SHRSIN:
1224  case QIF_A_SHRSOUT:
1225  itxn->far_cat.acct = qif_default_equity_holding(ctx, itxn->security);
1226  break;
1227 
1228  case QIF_A_STKSPLIT:
1229  itxn->far_cat.acct = qif_default_stock_acct(ctx, itxn->security);
1230  break;
1231 
1232  default:
1233  break;
1234  }
1235 
1236  /* If we dont have a far acct (or far category) then reset the flag */
1237  if (!itxn->far_cat.obj)
1238  itxn->far_cat_is_acct = FALSE;
1239 
1240  /* And now fill in the "near" and "far" splits. In particular we need
1241  *
1242  * NEAR: txn->from_acct, near_split->amount, value
1243  * FAR: cat, far_split->amount, value
1244  */
1245  switch (itxn->action)
1246  {
1247  case QIF_A_BUY:
1248  case QIF_A_BUYX:
1249  case QIF_A_REINVDIV:
1250  case QIF_A_REINVINT:
1251  case QIF_A_REINVLG:
1252  case QIF_A_REINVMD:
1253  case QIF_A_REINVSG:
1254  case QIF_A_REINVSH:
1255  case QIF_A_SHRSIN:
1256  near_split->amount = itxn->shares;
1257  near_split->value = split_value;
1258  far_split->amount = far_split->value = gnc_numeric_neg(itxn->amount);
1259  break;
1260 
1261  case QIF_A_SELL:
1262  case QIF_A_SELLX:
1263  case QIF_A_SHRSOUT:
1264  near_split->amount = gnc_numeric_neg(itxn->shares);
1265  near_split->value = gnc_numeric_neg(split_value);
1266  far_split->amount = far_split->value = itxn->amount;
1267  break;
1268 
1269  case QIF_A_CGLONG:
1270  case QIF_A_CGLONGX:
1271  case QIF_A_CGMID:
1272  case QIF_A_CGMIDX:
1273  case QIF_A_CGSHORT:
1274  case QIF_A_CGSHORTX:
1275  case QIF_A_DIV:
1276  case QIF_A_DIVX:
1277  case QIF_A_INTINC:
1278  case QIF_A_INTINCX:
1279  case QIF_A_MISCINC:
1280  case QIF_A_MISCINCX:
1281  case QIF_A_RTRNCAP:
1282  case QIF_A_RTRNCAPX:
1283  case QIF_A_XIN:
1284  near_split->amount = near_split->value = itxn->amount;
1285  far_split->amount = far_split->value = gnc_numeric_neg(itxn->amount);
1286  break;
1287 
1288  case QIF_A_MARGINT:
1289  case QIF_A_MARGINTX:
1290  case QIF_A_MISCEXP:
1291  case QIF_A_MISCEXPX:
1292  case QIF_A_XOUT:
1293  near_split->amount = near_split->value = gnc_numeric_neg(itxn->amount);
1294  far_split->amount = far_split->value = itxn->amount;
1295  break;
1296 
1297  case QIF_A_STKSPLIT:
1298  /* QIF just specifies the split ratio, not the number of shares
1299  * in and out, so we have to fetch the number of shares from the
1300  * security account.. FEH!
1301  */
1302 
1303  near_split->value = gnc_numeric_neg(split_value);
1304  far_split->value = split_value;
1305 
1306  /* XXX: FIXME: compute in-shares/out-shares based on ratio here:
1307  *
1308  * splitratio = num-shares / 10;
1309  * in_shares = gnc_account_get_balance(near_acct);
1310  * out_shares = in_shares * splitratio;
1311  *
1312  * near_split->amount = out_shares;
1313  * far_split->amount = gnc_numeric_neg(in_shares);
1314  *
1315  * We know (later) that near_split == txn->default_split and
1316  * far_split == txn->splits->data, so we'll just special-case this
1317  * kind of txn when we convert to GNC later.
1318  */
1319 
1320  break;
1321 
1322  default:
1323  break;
1324  }
1325 
1326  /* Just make sure to set that it's an account, not a category */
1327  far_split->cat.obj = itxn->far_cat.obj;
1328  if (itxn->far_cat_is_acct)
1329  far_split->cat_is_acct = TRUE;
1330 
1331  /* make the commission split if we need it, then add it to the split-list */
1332  if (itxn->commissionstr)
1333  {
1334  comm_split = qif_split_new();
1335  comm_split->cat.acct = qif_default_commission_acct(ctx);
1336  comm_split->cat_is_acct = TRUE;
1337  comm_split->amount = itxn->commission;
1338  comm_split->value = itxn->commission;
1339 
1340  txn->splits = g_list_prepend(txn->splits, comm_split);
1341  }
1342 
1343  /* Push the "far split" into the txn split-list */
1344  txn->splits = g_list_prepend(txn->splits, far_split);
1345 
1346  /* Free parsed strings.. */
1347  g_free(cat);
1348  g_free(cat_class);
1349  g_free(miscx);
1350  g_free(miscx_class);
1351 }
1352 
1353 
1354 /* Other handlers */
1355 static void
1356 qif_autoswitch_set(QifContext ctx)
1357 {
1358  qif_set_flag(ctx->parse_flags, QIF_F_IGNORE_ACCOUNTS);
1359 }
1360 
1361 static void
1362 qif_autoswitch_clear(QifContext ctx)
1363 {
1364  qif_clear_flag(ctx->parse_flags, QIF_F_IGNORE_ACCOUNTS);
1365 }
1366 
1367 /********************************************************************************
1368  * find or make ...
1369  */
1370 
1371 QifAccount
1372 find_or_make_acct(QifContext ctx, char *name, GList *types)
1373 {
1374  QifAccount res;
1375 
1376  res = (QifAccount)qif_object_map_lookup(ctx, QIF_O_ACCOUNT, name);
1377  if (res)
1378  g_free(name);
1379  else
1380  {
1381  res = qif_account_new();
1382  res->name = name;
1383  res->type_list = types;
1384 
1385  qif_object_map_insert(ctx, name, (QifObject)res);
1386  }
1387 
1388  return res;
1389 }
1390 
1392 find_or_make_cat(QifContext ctx, char *name)
1393 {
1394  QifCategory res;
1395 
1396  res = (QifCategory)qif_object_map_lookup(ctx, QIF_O_CATEGORY, name);
1397  if (res)
1398  g_free(name);
1399  else
1400  {
1401  res = qif_cat_new();
1402 
1403  res->name = name;
1404 
1405  qif_object_map_insert(ctx, name, (QifObject)res);
1406  }
1407 
1408  return res;
1409 }
1410 
1411 QifClass
1412 find_or_make_class(QifContext ctx, char *name)
1413 {
1414  QifClass res;
1415 
1416  res = (QifClass)qif_object_map_lookup(ctx, QIF_O_CLASS, name);
1417  if (res)
1418  g_free(name);
1419  else
1420  {
1421  res = qif_class_new();
1422  res->name = name;
1423  qif_object_map_insert(ctx, name, (QifObject)res);
1424  }
1425  return res;
1426 }
1427 
1428 /*****************************************************************************/
1429 
1430 /*
1431  * initialize handlers
1432  */
1433 void
1434 qif_object_init(void)
1435 {
1436  int i;
1437  static struct
1438  {
1439  QifType type;
1440  struct _QifHandler handler;
1441  } handlers[] =
1442  {
1443  { QIF_TYPE_BANK, { qif_txn_init, qif_txn_parse, qif_txn_end_acct } },
1444  { QIF_TYPE_CASH, { qif_txn_init, qif_txn_parse, qif_txn_end_acct } },
1445  { QIF_TYPE_CCARD, { qif_txn_init, qif_txn_parse, qif_txn_end_acct } },
1446  { QIF_TYPE_INVST, { qif_txn_init, qif_txn_invst_parse, qif_txn_end_acct } },
1447  { QIF_TYPE_PORT, { qif_txn_init, qif_txn_invst_parse, qif_txn_end_acct } },
1448  { QIF_TYPE_OTH_A, { qif_txn_init, qif_txn_parse, qif_txn_end_acct } },
1449  { QIF_TYPE_OTH_L, { qif_txn_init, qif_txn_parse, qif_txn_end_acct } },
1450  { QIF_TYPE_CLASS, { NULL, qif_class_parse, NULL } },
1451  { QIF_TYPE_CAT, { NULL, qif_cat_parse, NULL } },
1452  { QIF_TYPE_SECURITY, { NULL, qif_security_parse, NULL } },
1453  { QIF_ACCOUNT, { NULL, qif_account_parse, NULL } },
1454  { QIF_AUTOSWITCH, { qif_autoswitch_set, NULL, NULL } },
1455  { QIF_CLEAR_AUTOSWITCH, { qif_autoswitch_clear, NULL, NULL } },
1456  { 0, {NULL, NULL, NULL} }
1457  };
1458 
1459  for (i = 0; handlers[i].type > 0; i++)
1460  {
1461  if (handlers[i].type <= 0)
1462  {
1463  PERR("Invalid type?!? (%d @ %d)", handlers[i].type, i);
1464  }
1465  else
1466  qif_register_handler(handlers[i].type, &(handlers[i].handler));
1467  }
1468 }
gnc_numeric gnc_numeric_neg(gnc_numeric a)
gnc_numeric gnc_numeric_add(gnc_numeric a, gnc_numeric b, gint64 denom, gint how)
gboolean gnc_numeric_zero_p(gnc_numeric a)
#define PERR(format, args...)
Definition: qoflog.h:237
Account handling public routines.
gnc_numeric gnc_numeric_mul(gnc_numeric a, gnc_numeric b, gint64 denom, gint how)
All type declarations for the whole Gnucash engine.
#define GNC_DENOM_AUTO
Definition: gnc-numeric.h:246
const gchar * QofLogModule
Definition: qofid.h:89